Search code examples
graphvizdot

Graphviz ordering of nodes on the same rank


I made a graph and it looks like I want it to with one exception: the cluster containing nodes D and E should be placed between nodes B and C.

enter image description here

Here is the .dot:

digraph {
  node[shape=rectangle]
  graph [
        newrank=true
    ];

  Z
  A
  B
  C

  {
    rank=same;
    A -> B -> D;
    D -> C[style=invis];
  }

  subgraph clusterSS {
    style=dashed;
    D[group=ss]
    E[group=ss]
    D -> E[style="dashed"; dir = none];
  }

  Z -> A
  Z -> B
  Z -> D
  Z -> C
  A -> P[dir = back]
  B -> S
  S -> B[constraint = false];

  C -> S
  S -> E[constraint = false]
  E -> S[label = "Label"]
}

I expected that having edges between nodes

  {
    rank=same;
    A -> B -> D;
    D -> C[style=invis];
  }

would ensure correct ordering, but it doesn't.

The ordering becomes correct once I delete the edge label between S and E, but I do need to keep the labels.

enter image description here


Solution

  • [non-trivial, not guaranteed to work as desired in any other circumstance] Added clusters, invisible nodes,and more. ugh

    digraph {
      node[shape=rectangle]
      graph [
            newrank=true
        ];
    
      {
        rank=same;
        A -> B -> D;
        D -> C[style=invis];
      }
    
      subgraph clusterSS {
        style=dashed;
        D [group=ss]
        E [group=ss]
        D -> E[style="dashed"; dir = none];
      }
      subgraph clusterAP {
        peripheries=0
        A -> P[dir = back]
      }
      subgraph clusterB {
        peripheries=0
        B
        bogus    [style=invis shape=plain]
        B->bogus [style=invis]
      }
    
      Z -> A
      Z -> B
      Z -> D
      Z -> C
    
      S [group=ss]
    
      B -> S:nw
      B -> S:w [dir=back]  
      
      C -> S
      S -> E[constraint = false]
      E -> S[label = "Label"]
    }
    

    Giving:
    enter image description here