Ask Your Question
2

Treeplot()?

asked 2011-03-29 12:49:35 +0200

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!

edit retag flag offensive close merge delete

1 Answer

Sort by ยป oldest newest most voted
5

answered 2011-03-29 14:07:08 +0200

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.

edit flag offensive delete link more

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: 2011-03-29 12:49:35 +0200

Seen: 597 times

Last updated: Mar 29 '11