Graphviz Dot vertical alignment of nodes

user1839433 picture user1839433 · Nov 23, 2014 · Viewed 16.7k times · Source

I got this dot graph and want the nodes A and D, B and E and C and F to be aligned. Here is the related dot code:

digraph{

A
B
C
D
E
F

{rank = same; B; C}
{rank = same; E; F}

A -> B [label="2", weight=2]
A -> C [label="0", style=dashed, weight=2]
B -> C [label="0", style=dashed, weight=2]
B -> D [label="2", style=dashed, weight=2]
C -> D [label="0", weight=2]
D -> E [label="1", style=dashed, weight=2]
D -> F [label="0", weight=2]
E -> F [label="0", weight=2]
F -> A
}

As you can see I already tried to apply weights to the edges, but that didn't work out

enter image description here

Answer

marapet picture marapet · Nov 23, 2014

It is possible to use the group attribute of the nodes to suggest aligning the edges between nodes of the same group in a straight line.

Declare the nodes with the group attribute:

A [group=g1]
{rank = same; B[group=g2]; C[group=g3]}
D [group=g1]
{rank = same; E[group=g2]; F[group=g3]}

Then make sure all of those nodes have an (invisible) edge between them:

edge[style=invis];
A -> D
B -> E
C -> F

Everything together:

digraph G {
  A [group=g1]
  {rank = same; B[group=g2]; C[group=g3]}
  D [group=g1]
  {rank = same; E[group=g2]; F[group=g3]}

  A -> B [label="2", weight=2]
  A -> C [label="0", style=dashed, weight=2]
  B -> C [label="0", style=dashed, weight=2]
  B -> D [label="2", style=dashed, weight=2]
  C -> D [label="0", weight=2]
  D -> E [label="1", style=dashed, weight=2]
  D -> F [label="0", weight=2]
  E -> F [label="0", weight=2]
  F -> A

  edge[style=invis];
  A -> D
  B -> E
  C -> F
}

Graphviz diagram