Ask Your Question

Tree layout of a graph

asked 2015-12-12 05:42:23 -0600

Peter Luschny gravatar image

updated 2015-12-13 00:39:21 -0600

Consider the graph of this tree:

T = Graph()
E = [(0, 1), (1, 2), (2, 3), (2, 4)]

image description

I would like to see the edges displayed in the order as given in the list E from left to right. In other words, I would like to have node(4) exchange its position with node(3).

How can I achieve this?

Edit: To clarify: Changing the order of the edge-list is no option (besides not working either). The order of the edges must be adhered to the order given.

edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted

answered 2015-12-14 13:15:45 -0600

FrédéricC gravatar image

You can use instead planar graphs by setting the embedding:

sage: T = Graph()
sage: E = [(0, 1), (1, 2), (2, 3), (2, 4)]
sage: T.add_edges(E)
sage: T.set_embedding({0:[1],1:[0,2],2:[1,3,4],3:[2],4:[2]})
edit flag offensive delete link more


This might work; however the 'set_embedding' requires additional work with the edges and is not satisfying as it is completely unnecessary since the requested embedding is already there: implicitly in the list of the edges. All I want is that Sage respects the given order instead of messing it up. Thus my question seems to amount to a quite natural feature request.

Peter Luschny gravatar imagePeter Luschny ( 2015-12-21 07:21:58 -0600 )edit

answered 2015-12-12 14:18:11 -0600

ndomes gravatar image

I tried with success changing the order of edges in E and converting to strings.

T = Graph()
E = [(0, 1), (1, 2), (2, 4), (2, 3)]
E = [map(str,e) for e in E]
edit flag offensive delete link more


I do not want to change the order of the edges. This is a minimal example; in reality the edges are a large and complicated sequence computed by a program. I want to persuade Sage to respect the order of the edges as given.

Peter Luschny gravatar imagePeter Luschny ( 2015-12-12 15:40:18 -0600 )edit

Your Answer

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

Add Answer

Question Tools

1 follower


Asked: 2015-12-12 05:42:23 -0600

Seen: 176 times

Last updated: Dec 14 '15