I'm building a CFG (Context-free grammar), and I'd like the exit node to always be at the bottom of the graph. Sometimes it happens naturally, sometimes it doesn't.
Example:
digraph G {
0;
1;
4;
5;
7;
8;
0 -> 4;
5 -> 7;
7 -> 8;
7 -> 1;
8 -> 5;
4 -> 7;
}
Draws (using dot):
Node 1 is my exit node, I'd like that to be at the bottom. Suggestions?
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.
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.
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.
Use rank specification for the subgraph. In this case sink should make it at the bottom and max will make it on the same level as 5:
digraph G {
0;
{ rank = sink; 1; }
5;
4;
7;
8;
0 -> 4;
5 -> 7;
7 -> 8;
7 -> 1;
8 -> 5;
4 -> 7;
}
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