tags:

views:

66

answers:

2

Hi,

I am using graphviz for the first time. I just need a tree layout so that all the childs are at the same level.

For example, A->B A->C A->D

THEN B, C AND D SHOULD BE AT THE SAME LEVEL.

Following is the code I am using.

digraph unix {
    size="6,6";
    node [color=lightblue2, style=filled];

    "A:1000" -> "B:300";
    "A:1000" -> "C:300";
    "A:1000" -> "D:200";
    "B:300" -> "E:140";
    "B:300" -> "F:164";
    "B:300" -> "G:75";
    "C:300" -> "H:135";
    "C:300" -> "I:91";
    "D:200" -> "E:140";
    "D:200" -> "F:164";
    "D:200" -> "G:75";
    "E:140" -> "F:164";
    "E:140" -> "G:75";
    "F:164" -> "G:75";
    "G:75" -> "H:135";
    "H:135" -> "I:91";
}

How do I make sure that the childs are at the same level?

A: 

To get nodes on the same level, say "B:300" and "C:300", add the following line:

{rank=same; "B:300" "C:300"}
ars
Thanks arsBut if I have to generate the graph dynamically, I won't know which nodes are siblings rite. I was thinking that if you write, A->B, A->C, A->D .. graphviz would see that B,C and D are from same nodes hence need to be at same level.
Sunil
If you're generating it dynamically, it might be simpler to add a `rank` line since you can determine the child nodes within your program.
ars
+2  A: 

The graph you presented does not represent a tree, but a directed acyclic graph (In a tree there is only one distinct path between every pair of nodes). If your input would have been a tree, then just using dot would produce what you want. If you also want to add non-tree edges, like "C:300" -> "H:135" in your example, you could specify a lower weight for them, to make sure that dot doesn't try to optimize the layout with respect to these edges.

"C:300" -> "H:135" [weight=0];
"C:300" -> "I:91" [weight=0];

Note that these two edges become very long (and to dot, ugly) with this setting, and this is the reason why the node "C:300" is placed as it is in your original graph.

grddev