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?
neato is a reasonable default tool to use for undirected graphs that aren't too large (about 100 nodes), when you don't know anything else about the graph. neato attempts to minimize a global energy function, which is equivalent to statistical multi-dimensional scaling.
Ranks and Subgraphs To work out the layout, Graphviz uses a system it calls "ranks". Each node is assigned a higher rank than the highest ranked node that point to it. If your rank direction is set to left to right ( rankdir=LR ), then nodes with a higher rank are placed further to the right.
Graphviz is an open-source python module that is used to create graph objects which can be completed using different nodes and edges. It is based on the DOT language of the Graphviz software and in python it allows us to download the source code of the graph in DOT language.
To get nodes on the same level, say "B:300" and "C:300", add the following line:
{rank=same; "B:300" "C:300"}
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With