Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have a Graph object that contains a forest with directed trees. I want to draw the forest such that the root of the trees are on the top and the leaves are on the bottom. If I use the following line of code:

Graph[{1, 2, 3, 4, 5, 6, 7}, {1 \[DirectedEdge] 2, 
  2 \[DirectedEdge] 3, 4 \[DirectedEdge] 6, 5 \[DirectedEdge] 6, 
  7 \[DirectedEdge] 4}, VertexLabels -> "Name", 
 GraphLayout -> "LayeredDrawing"]

I get this image:

forest

Everything looks fine except the root (#3) is not on top. How can I resolve the issue?

share|improve this question
    
I changed the graph style to basic black from the context menu. I don't think it changes the semantic that I want to express. –  Mohsen Oct 10 '12 at 5:21
    
In your rules 7 connects to 6. In your picture 7 connects to 4. –  Vitaliy Kaurov Oct 10 '12 at 5:28
    
@VitaliyKaurov: Thank you. I fixed the issue. I hope everything is fine now. –  Mohsen Oct 10 '12 at 5:39
    
In version 9 there's also the "RootVertex" suboption of GraphLayout, which might help. –  Szabolcs Apr 12 '13 at 21:01
add comment

2 Answers

up vote 2 down vote accepted

This is what you need.

LayeredGraphPlot[{1 -> 2, 2 -> 3, 4 -> 6, 5 -> 6, 7 -> 4}, Bottom, VertexLabeling -> True]

enter image description here

EDIT

This may not be the best way to do this but this is one way to do it: introduce single nodes by just connecting it to itself and shade the edge white. Also EdgeRenderingFunction and VertexRenderingFunction allow you to play with your LayeredGraphPlot. There is also VertexCoordinateRules that enables you to place your vertices wherever you like.

LayeredGraphPlot[{1 -> 2, 2 -> 3, 4 -> 6, 5 -> 6, 7 -> 4,9 -> 9}, Bottom, VertexLabeling -> True, VertexRenderingFunction -> ({White, EdgeForm[Black], Disk[#, .1], 
 Black, Text[#2, #1]} &), EdgeRenderingFunction -> (If[Intersection[{{9, 9}}, {#2}] != {}, {White, Line[#1]}, If[Intersection[{{1, 2}, {4, 6}}, {#2}] != {}, {Blue,       Arrow[#1]}, {Red, Line[#1]}]] &)]

enter image description here

share|improve this answer
    
Thanks Amatya. "Bottom" is the keyword that I was looking for. What if I want to show some additional nodes without edges? And how about path highlighting? With a Graph I can easily do all of those things. –  Mohsen Oct 10 '12 at 7:15
    
@Mohsen I edited the answer to address your concerns. –  Amatya Oct 10 '12 at 8:32
    
I found the you can pass Graph objects to LayeredGraphPlot as well. So, this does the trick for the my first concern: LayeredGraphPlot[ Graph[{1, 2, 3, 4, 5, 6, 7, 8}, {1 \[DirectedEdge] 2, 2 \[DirectedEdge] 3, 4 \[DirectedEdge] 6, 5 \[DirectedEdge] 6, 7 \[DirectedEdge] 6}], Bottom, VertexLabeling -> True] –  Mohsen Oct 10 '12 at 8:36
    
@Mohsen oh cool. I didn't know one could do that! Thx! –  Amatya Oct 10 '12 at 9:02
2  
@Mohsen Thank you for the accept! This is my first answer.. I feel so excited and useful. lol. –  Amatya Oct 10 '12 at 21:10
add comment

In version 9, one can use the new built in layout version of Graph for trees: GraphLayout -> "LayeredDigraphEmbedding".

Graph[{1, 2, 3, 4, 5, 6, 7}, {1 -> 2, 2 -> 3, 4 -> 6, 5 -> 6, 7 -> 4},
    VertexLabels -> "Name", ImageSize -> 200, ImagePadding -> 10,
    GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Bottom}]

enter image description here

share|improve this answer
    
Actually the problem seems to be with determining the root vertex of the tree. The reason why using an embedding designed for directed graphs seems to fix the problem is that the directedness makes it clearer which node should be considered the root. But Mathematica seems to consider the root to have outgoing edges, not incoming ones. So here the root is really 1 and not 3, it's just that the whole layout is turned upside down. This observation becomes important when starting to play with the "RootVertex" suboption of GraphLayout: specifying a node with only incoming edges ... –  Szabolcs Apr 12 '13 at 21:05
    
... as the root will confuse Mathematica because otherwise it still wants to layout the graph so that the arrows point towards "lower levels" ("lower" meaning further from the root). Try this: Graph[{1, 2, 3}, {1 -> 2, 2 -> 3}, VertexLabels -> "Name", ImagePadding -> 10, GraphLayout -> {"LayeredDigraphEmbedding", "Orientation" -> Bottom, "RootVertex" -> 3}] –  Szabolcs Apr 12 '13 at 21:07
    
@Szabolcs Furthermore (or "something completely different"): Graph[{1, 2, 3}, {1 -> 2, 2 -> 3}, DirectedEdges -> True, GraphLayout -> "LayeredDigraphEmbedding"] draws an undirected graph. So I guess LayeredDigraphEmbedding is only reliable in the simplest cases. –  István Zachar Apr 13 '13 at 8:53
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.