Ask Your Question

Revision history [back]

To create a graph from an adjacency matrix, use the following:

sage: M = matrix(ZZ,8,8,[0,1,1,1,0,0,0,0,
                         1,0,0,0,1,1,0,0,
                         1,0,0,0,0,1,1,0,
                         1,0,0,0,1,0,1,0,
                         0,1,0,1,0,0,0,0,
                         0,1,1,0,0,0,0,0,
                         0,0,1,1,0,0,0,1,
                         0,0,0,0,1,1,1,0])
sage: G = Graph(M,format='adjacency_matrix')

You can find more ways to create graphs by running "Graph?" in Sage. To plot this graph you use the Graph.plot() command.

sage: G.plot()

If the graph is a tree there's a special argument you can provide to plot() that will make the formatting nicer.

sage: G.plot(layout='tree')
Traceback (click to the left of this block for traceback)
...
RuntimeError: Cannot use tree layout on this graph: self.is_tree()
returns False.

However, the graph you provided isn't really a tree. Run the first three commands above to see what I mean, just in case I entered your adjacency matrix incorrectly. Finally, if you do have a tree running

sage: G.plot?

will explain various other plotting options that might prove useful. In particular, options for specifying tree roots and such.