# draw a tree diagram

Good afternoon, I have defined a recursive function and I would like to vizualise the 'calls' of the function trought a tree diagram. The nodes should be the elements of a group (Extended affine weyl group). So expressions of this type s0s1s2s4s5s6s0s0s7 (not hashable). Is there a easy way to do that ? Maybe using just matplotlib? So without instal graphviz. I need something simple like latex \tree. I have just tried something like this G = graphs.RandomGNM(4,5) G.relabel({0:'zero' , 1:'one', 2:'two', 3:'three'}) G.show(figsize=[4,4], graph_border=True) But the labels on the vertices are not readable. Really, I need something like a diagram not a graph.

edit retag close merge delete

To display inline code, like z = x*y, use backticks.

To display blocks of code or error messages, skip a line above and below, and do one of the following (all give the same result):

• indent all code lines with 4 spaces
• select all code lines and click the "code" button (the icon with '101 010')
• select all code lines and hit ctrl-K

For instance, typing

If we define f by

def f(x, y):
return (x, y)

then f(2, 3) returns (2, 3) but f(2) gives:

TypeError: f() takes exactly 2 arguments (1 given)


produces:

If we define f by

def f(x, y):
return (x, y)


then f(2, 3) returns (2, 3) but f(2) gives:

TypeError: f() takes exactly 2 arguments (1 given)


( 2018-06-05 13:30:13 -0500 )edit

Sort by » oldest newest most voted

You can do this using RecursivelyEnumeratedSet. Here is one easy example :

sage: def f(a):
....:     return [2*a, 2*a+1]
sage: R = RecursivelyEnumeratedSet([1], f)
sage: R
A recursively enumerated set (breadth first search)
sage: G = R.to_digraph(max_depth=4)
sage: G
Looped multi-digraph on 63 vertices
sage: view(G)


Using optional package slabbe (installed with sage -pip install slabbe) + graphviz + dot2tex, you may draw a nicer looking graph:

sage: from slabbe import TikzPicture
sage: tikz = TikzPicture.from_graph(G)
sage: tikz.pdf()


more