Search code examples
graphviz

Preserve order of nodes in grahpviz subgraph


How can I make the graphviz preserve the order of nodes in subgraph?

See the image and the code below. For some reason the second node (with the number 2) is displayed at the top and the first node is at the bottom.

I took a look at this questions but it did not solve the issue (or then I did not understand how to implement the suggestions properly):

enter image description here

digraph G {

        rankdir=LR
        splines=line
        nodesep=0.3;
        ranksep=2
        
        node [label=""];
        // edge [dir=none];
        edge[arrowhead="empty"];
        // graph [ordering="out"];

        subgraph cluster_0 {
        color=white;
                // {rank=same;x1;x2;x3;x4;x5}
                node [style=solid,color=blue4, shape=circle];
        x1 [label="1"] x2 [label="2"] x3 [label="3"] x4 [label="4"] x5 [label="5"];
    }

    subgraph cluster_1 {
        color=white;
        node [style=solid,color=red2, shape=circle];
                edge[style=invisible]
        a12 [label="1"] a22 [label="2"] a32 [label="3"] a42 [label="4"];
    }

    subgraph cluster_2 {
        color=white;
        node [style=solid,color=red2, shape=circle];
        a13 a23 a33 a43;
    }

    subgraph cluster_3 {
        color=white;
        node [style=solid,color=seagreen2, shape=circle];
        O1 O2 O3;
    }

        x1 -> a12;
        x1 -> a22;
        x1 -> a32;
        x1 -> a42;

        x2 -> a12;
        x2 -> a22;
        x2 -> a32;
        x2 -> a42;
 
        x3 -> a12;
        x3 -> a22;
        x3 -> a32;
        x3 -> a42;

        x4 -> a12;
        x4 -> a22;
        x4 -> a32;
        x4 -> a42;

        x5 -> a12;
        x5 -> a22;
        x5 -> a32;
        x5 -> a42;

        a12 -> a13
        a22 -> a13
        a32 -> a13
        a42 -> a13

        a12 -> a23
        a22 -> a23
        a32 -> a23
        a42 -> a23

        a12 -> a33
        a22 -> a33
        a32 -> a33
        a42 -> a33

        a12 -> a43
        a22 -> a43
        a32 -> a43
        a42 -> a43

        a13 -> O1
        a23 -> O1
        a33 -> O1
        a43 -> O1

        a13 -> O2
        a23 -> O2
        a33 -> O2
        a43 -> O2

        a13 -> O3
        a23 -> O3
        a33 -> O3
        a43 -> O3

}

Solution

  • I could not find a way to use ordering successfully, either. Because they did not seem to be used, I removed the cluster references. This version uses weight attributes to keep the nodes in order.

    digraph G {
            rankdir=LR
            splines=line
            nodesep=0.3;
            ranksep=2
            
            node [label=""];
            edge[arrowhead="empty"];
    
         {
            rank=same
            node [style=solid,color=blue4, shape=circle];
            x1 [label="1"] x2 [label="2"] x3 [label="3"] x4 [label="4"] x5 [label="5"];
        edge [weight=50]  // arbitrarily large integer
        edge [style=invis]
        x1->x2 x2->x3 x3->x4 x4->x5
         }
         {
            rank=same
            node [style=solid,color=red2, shape=circle];
            edge[style=invisible]
            a12 [label="1"] a22 [label="2"] a32 [label="3"] a42 [label="4"];
        
        edge [weight=50]  // arbitrarily large integer
        edge [style=invis]
        a12->a22 a22->a32 a32->a42
        }
        {
            rank=same
            node [style=solid,color=red2, shape=circle];
            a13 a23 a33 a43;
        }
        {
            rank=same
            node [style=solid,color=seagreen2, shape=circle];
            O1 O2 O3;
        }
            x1 -> a12;
            x1 -> a22;
            x1 -> a32;
            x1 -> a42;
    
            x2 -> a12;
            x2 -> a22;
            x2 -> a32;
            x2 -> a42;
     
            x3 -> a12;
            x3 -> a22;
            x3 -> a32;
            x3 -> a42;
    
            x4 -> a12;
            x4 -> a22;
            x4 -> a32;
            x4 -> a42;
    
            x5 -> a12;
            x5 -> a22;
            x5 -> a32;
            x5 -> a42;
    
            a12 -> a13
            a22 -> a13
            a32 -> a13
            a42 -> a13
    
            a12 -> a23
            a22 -> a23
            a32 -> a23
            a42 -> a23
    
            a12 -> a33
            a22 -> a33
            a32 -> a33
            a42 -> a33
    
            a12 -> a43
            a22 -> a43
            a32 -> a43
            a42 -> a43
    
            a13 -> O1
            a23 -> O1
            a33 -> O1
            a43 -> O1
    
            a13 -> O2
            a23 -> O2
            a33 -> O2
            a43 -> O2
    
            a13 -> O3
            a23 -> O3
            a33 -> O3
            a43 -> O3
    }
    

    Giving:
    enter image description here
    (yes, there many failures in between)