Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I place nodes on the same level in DOT?

Tags:

graphviz

dot

I want to render several trees simultaneously and place all root nodes and all leaf nodes on the same level.

Here's an example of what I'm trying to do. Root nodes A and X are on the same level, and so are leaf nodes B, D, and Z.

Graph I want

I unsuccessfully tried putting roots in one rank and leaves in another as follows:

digraph G {  rankdir = TB; subgraph { A -> B A -> C C -> D X -> Y rank = same; A; X; rank = same; B; D; Y; } /* closing subgraph */ } 

And got this outcome where everything is on the same rank. enter image description here

Any suggestions about what I should be trying? I've already got roots and leaves identified.

like image 209
Thalecress Avatar asked Sep 08 '14 22:09

Thalecress


2 Answers

Putting the rank = same; ... statements in braces, e.g.:

digraph G {    rankdir = TB;   subgraph {     A -> B     A -> C     C -> D     X -> Y     // note that rank is used in the subgraph     {rank = same; A; X;}     {rank = same; B; D; Y;}   } /* closing subgraph */ } 

... gives the desired result:

enter image description here

like image 155
Simon Avatar answered Oct 16 '22 09:10

Simon


The ideal structure is actually rank max and rank min. No need for a subgraph or any other shenanigans. GraphViz has explicit facilities for this.

With complex graphs, rank=same will often end up near the middle of the graph. If you mean top and bottom, say top and bottom.

digraph G {     rankdir = TB;    A -> B;   A -> C -> D;   X -> Y;    { rank=min; A; X; }   { rank=max; B; D; Y; }  } 

enter image description here

like image 44
John Haugeland Avatar answered Oct 16 '22 10:10

John Haugeland