Sunil
Sunil

Reputation: 51

graphviz Tree Layout

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?

Upvotes: 5

Views: 9698

Answers (2)

grddev
grddev

Reputation: 2832

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.

Upvotes: 3

ars
ars

Reputation: 123468

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

{rank=same; "B:300" "C:300"}

Upvotes: 10

Related Questions