Pierre-Henri Wuillemin
Pierre-Henri Wuillemin

Reputation: 541

graphviz : how to fix the order and alignment of clusters

I try to represent a graph called "dynamic Bayesian network". This graph is composed of a sequence of cluster representing different timeslices (from 0 to N). In each timeslice, I would have the same node, in the same order. Edges only are between nodes in the same timeslice or from a timeslice to the next one.

For instance :

good dBN

However, from time to time, the ordering of the clusters is not correct. Here timeslice 2 is before timeslice 1 ...

bad dBN

Here is the dot file I have for the second one :

digraph G {
    rankdir=TD;
    splines=ortho;
    node [color="#000000", fillcolor=white, style=filled];
    subgraph cluster_0 {
        bgcolor="#DDDDDD";
        label="Time slice 0";
        rankdir=TD;
        "P0" [label="P"];
        "R0" [label="R"];
        "dP0" [label="dP"];
        "dR0" [label="dR"];
    }

    subgraph cluster_1 {
        bgcolor="#DDDDDD";
        label="Time slice 1";
        rankdir=TD;
        "P1" [label="P"];
        "R1" [label="R"];
        "dP1" [label="dP"];
        "dR1" [label="dR"];
    }
    
    subgraph cluster_2 {
        bgcolor="#DDDDDD";
        label="Time slice 2";
        rankdir=TD;
        "P2" [label="P"];
        "R2" [label="R"];
        "dP2" [label="dP"];
        "dR2" [label="dR"];
    }
    
    edge [color=blue, constraint=False];
    "P0" -> "P1";
    "P1" -> "dP1";
    "P2" -> "dP2";
    "R1" -> "R2";
    "R1" -> "dP1";
    "R0" -> "dP0";
    "dR1" -> "R2";
    "R2" -> "dR2";
    "dR0" -> "R1";
    "dP1" -> "P2";
    "P0" -> "dP0";
    "P1" -> "P2";
    "R2" -> "dP2";
    "R0" -> "dR0";
    "dP0" -> "P1";
    "R0" -> "R1";
    "P0" -> "dR0";
    "P1" -> "dR1";
    "P2" -> "dR2";
    "R1" -> "dR1";
    
    edge [constraint=True, style=invis];
    "P0" -> "R0";
    "R0" -> "dP0";
    "dP0" -> "dR0";
    "P1" -> "R1";
    "R1" -> "dP1";
    "dP1" -> "dR1";
    "P2" -> "R2";
    "R2" -> "dP2";
    "dP2" -> "dR2";
}

If I try just to add constraints such as "P0->P1;P1->P2", the order is correct but the clusters are not aligned any more ...

bad aligned dBN

Does someone know how to fix the ordering between the clusters in this graph ?

Upvotes: 0

Views: 186

Answers (1)

sroush
sroush

Reputation: 6773

Changed rankdir direction to guarantee correct sequence of clusters. That in-turn forced rank=same within each cluster, plus a few other tweaks.

digraph G {
    rankdir=LR  // was rankdir=TD;
    splines=ortho;

    node [color="#000000", fillcolor=white, style=filled];
    subgraph cluster_0 {
        bgcolor="#DDDDDD";
        label="Time slice 0";
        //  only applies to Root graph: rankdir=TD;
    {rank=same  // because we changed rankdir
        "P0" [label="P"];
        "R0" [label="R"];
        "dP0" [label="dP"];
        "dR0" [label="dR"];
    }
    }

    subgraph cluster_1 {
        bgcolor="#DDDDDD";
        label="Time slice 1";
    {rank=same
        "P1" [label="P"];
        "R1" [label="R"];
        "dP1" [label="dP"];
        "dR1" [label="dR"];
    }
    }
    
    subgraph cluster_2 {
        bgcolor="#DDDDDD";
        label="Time slice 2";
    {rank=same
        "P2" [label="P"];
        "R2" [label="R"];
        "dP2" [label="dP"];
        "dR2" [label="dR"];
    }
    }
 
    edge [color=blue, constraint=False];
    "P0" -> "P1";
    "P1" -> "dP1";
    "P2" -> "dP2";
    "R1" -> "R2";
    "R1" -> "dP1";
    "R0" -> "dP0";
    "dR1" -> "R2";
    "R2" -> "dR2";
    "dR0" -> "R1";
    "dP1" -> "P2";
    "P0" -> "dP0";
    "P1" -> "P2";
    "R2" -> "dP2";
    "R0" -> "dR0";
    "dP0" -> "P1";
    "R0" -> "R1";
    "P0" -> "dR0";
    "P1" -> "dR1";
    "P2" -> "dR2";
    "R1" -> "dR1";

     // establish positions
     edge [style=invis constraint=true]
     P0 -> P1 -> P2
     R0 -> R1 -> R2
     dP0 -> dP1 -> dP2
     dR0 -> dR1 -> dR2
}

Giving:
enter image description here

Upvotes: 1

Related Questions