Vertically stack square nodes horizontally arranged with vertical connections

Question

How do you stack nodes and keep them stacked in the presence of vertical edges?

MWE

digraph ec {
  rankdir=LR
  node [label=""]
  edge [style="invis"]

  subgraph subset {
    node [shape="square"]
    s0
    s3
    s5
    s6

    node [shape="none"]
    s1
    s2
    s4
    s7

    s0 -> s1 -> s2 -> s3 -> s4 -> s5 -> s6 -> s7
  }

  subgraph fullset {
    node [shape="square"]
    fs0
    fs1
    fs2
    fs3
    fs4
    fs5
    fs6
    fs7

    fs0 -> fs1 -> fs2 -> fs3 -> fs4 -> fs5 -> fs6 -> fs7
  }

  // s3 -> fs3 [style="line"]  // vertical connection
}

Have

Without vertical connection (last line commented)

Perfect vertical stack, but subset is below the fullset.

With vertical connection (last line uncommented).

Subset is below the fullset, but no longer vertically stacked (i.e., s0 is not directly above fs0, s1 not directly above fs1, and so on; subset is shifted left).

Want

Subset above the fullset, vertically stacked, with vertical connection.

If there’s a way to vertically stack without using invisible nodes in the subset, even better.

You can use constraint=false for that edge and reorder the subgraphs to keep the full set at the top.

Try it yourself here.

That is very cool! Thank you!

Re-ordering the subgraphs in the input does not work. Re-ordirering the edge from s3 -> fs3 to fs3 -> s3 does not work either.

That is a very cool tool you made! I can’t figure out how to share my changes with you (hovering over the buttons doesn’t give me info and I tried clicking all of them in turn).

1 Like

Changed rankdir, shape & style, added rank

digraph ec {
//  rankdir=LR
  node [label=""]
  edge [style="invis"]

  subgraph subset {
    rank=source  // added
    node [shape="square"]
    s0
    s3
    s5
    s6

    node [style=invis]  // different shape sometimes different size
    s1
    s2
    s4
    s7

    s0 -> s1 -> s2 -> s3 -> s4 -> s5 -> s6 -> s7
  }

  subgraph fullset {
    rank=sink  // added
    node [shape="square"]
    fs0
    fs1
    fs2
    fs3
    fs4
    fs5
    fs6
    fs7

    fs0 -> fs1 -> fs2 -> fs3 -> fs4 -> fs5 -> fs6 -> fs7
  }

  s3 -> fs3 [style="solid"]  //  (style=line is not valid) vertical connection
}

Giving:
stack2rows1

Thank you for these corrections. I am marking this as the solution.

I also found @magjac’s [constraint=false] to be very edifying.

1 Like

Thanks.

You have two export functions reachable from the hamburger menu icon at the top left.

I misunderstood. I thought you wanted the full set on top as in the first graph. That’s why I reordered. But I see now that that wasn’t what you wanted.

Thanks that will be very useful!

P.S.
How are you able to quote the entire post but have it collapsed and highlighted on the part you want? I found “Quote whole post” but not sure how to specify which part to show. If this is the wrong place to ask this question lmk.

I’m surprised there is not a digraph attribute that when set, uses the ordering of the input.

Just select the text you want to quote and a Quote button shows up.

There are two (or more) issues around the idea of using input order to (partially) drive the layout:

But note Stephen’s comments about difficulty

Super cool! Thanks!

Thanks! Wasn’t aware of these.