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

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


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


Here's a simple example inspired by @William John Holden's comment -

graph {
  rankdir=LR;
  a -- b -- c;
  d -- e -- f;
  b -- d; {rank = same; b; d;};
}

graph

Tags:

Graphviz

Dot