Ask Your Question
2

Treeplot()?

asked 14 years ago

unraisedarc gravatar image

Hi,

I'm aware that Mathematica has a treeplot() function which basically plots a rooted tree. I've been searching for a while now to see if Sage has similar functionality. I basically want to plot a tree diagram with the following adjacency matrix:

01110000
10001100
10000110
10001010
01010000
01100000
00110001
00001110

Can anyone help me? (Maybe I have to use networkx somehow...).

Thanks!

Preview: (hide)

1 Answer

Sort by » oldest newest most voted
5

answered 14 years ago

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.

Preview: (hide)
link

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 14 years ago

Seen: 1,016 times

Last updated: Mar 29 '11