Search code examples
graphvizranksubgraph

Subgraph layout in graphviz


I have code to display two subgraphs:

graph {
    rankdir=LR;
    subgraph cluster01 {
        label="t=0"
        a0 [label="A"];
        a1 [label="B"];
        a2 [label="C"];
        a5 [label="E"];
        a0 -- a1;
        a1 -- a2 ;
        a2 -- a0;
    };

    subgraph cluster02
    {
        label="t=10"
        b0 [label="A"];
        b5 [label="E"];
        b1 [label="B"];
        b2 [label="C"];

        b0 -- b1;
        b2 -- b5;
    };

    a0--b0 [style=dotted];
    a1--b1 [style=dotted];
    a2--b2 [style=dotted];
    a5--b5 [style=dotted];
}

This code displays two subgraphs like this:

https://i.sstatic.net/F23SY.png

But I want to have it like this:

https://i.sstatic.net/jUpIp.png

I hope someone will help me fix the "rankdir" to get it done.


Solution

  • The following was achieved by using invisible edges and constraint=false on some edges:

    graphviz output

    graph {
        rankdir=LR;
        subgraph cluster01 {
            label="t=0";
            a0 [label="A"];
            a1 [label="B"];
            a2 [label="C"];
            a5 [label="E"];
            a0 -- a1;
            a1 -- a2;
            a2 -- a5 [style=invis];
            a2 -- a0 [constraint=false];
        };
    
        subgraph cluster02
        {
            label="t=10"
            b0 [label="A"];
            b5 [label="E"];
            b1 [label="B"];
            b2 [label="C"];
    
            b0 -- b1;
            b1 -- b2 [style=invis];
            b2 -- b5;
        };
    
        edge[constraint=false];
        a0--b0 [style=dotted];
        a1--b1 [style=dotted];
        a2--b2 [style=dotted];
        a5--b5 [style=dotted];
    }