2

I'd like to make a graph with parent nodes aligned at the "center" of the children nodes.

For the tree below I get the following image.

graph {
rankdir = "LR"
splines = line
A -- B
A -- C
B -- D
B -- E
C -- F
D -- G
D -- H
D -- I
E -- J
E -- K
F -- L
}

image of graph

I would like to have node D aligned with node H, node E aligned halfway between J and K, and F aligned with L. At the next rank, I'd like to have B aligned halfway between E and D, and C aligned with F. At the top (leftmost) rank, I'd like to have A aligned halfway between B and C.

Is there a way to make this happen without specifying positions of nodes?

2 Answers2

1

I couldn't figure out a way to get everything I want, but I got a lot closer.

The secrets:

  1. Use groups to force horizontal edges (would be vertical in TD rankdir) as shown in Graphviz Dot vertical alignment of nodes
  2. Add invisible edges where needed

Note that I couldn't get E where I wanted it, but I got F, D, B, and C there. And I can make this file programmatically based on the parent-child relationships that I already know.

While this is not a perfect solution, it's better for my usage than entering coordinates.

graph {
rankdir = "LR"
splines = line
//packmode="clust"
A
B [group=g3]
C [group=g2]
D [group=g1]
E
F [group=g2]
G
H [group=g1]
I [group=g3]
J
K
L [group=g2]
A -- {B ; C;} 
B -- D
B -- E
C -- F
D  -- {G; H; I;} 
E -- {J; K;} 
F [group=g2]
G
H [group=g1]
I [group=g3]
J
K
L [group=g2]
A -- {B ; C;} 
B -- D //[weight=10]
B -- E
C -- F
D  -- {G; H /*[group=g1]*/; I;} 
E -- {J; K;} 
F -- L
B -- I [style=invis]
}

image of graph

0

Probably not. Even if you included invisible nodes (style=invis), you would still be fighting the dot algorithm. It would be much easier to explicitly specify the X/Y coordinates.

sroush
  • 5,375
  • 2
  • 5
  • 11