macadamianut
macadamianut

Reputation: 13

Graphviz: How to get fixed node positions

I am quite new to Graphviz, sorry if my question is too naif.

In dot, I am trying to have several images of graphs, with nodes in a fix position, but with different arcs. However, even a minor modification in the arc configuration, makes a huge difference in the layout. I found some workarounds (by keeping certain hedges as invisible, or by using "back" option). However, I am wondering if there is a more elegant way to avoid situations like this, where the only modification of one arc direction gives a totally different layout.

For example, see this:

digraph exempleUPFM {
label=mylab;
ratio = 0.5;
spline = line; forcelabels=true;
node [shape = circle;style=filled; color=lightgrey;];
1 [xlabel="10" fontcolor= blue group=g1];
4 [xlabel="-6" fontcolor= red group=g1];
2 [xlabel="4" fontcolor= blue group=g2];
5 [xlabel="-8" fontcolor= red group=g2];
node [shape = circle; style=""]
{rank=same; 1 2}
{rank=same; 4 5}
1 -> 2 [label=" 1"]
1 -> 3 [label=" 8"]
1 -> 4 [label=" 1"]
2 -> 3 [label=" 2"]
3 -> 4 [label=" 1"]
3 -> 5 [label=" 4"]
4 -> 5 [label=" 12"]
5 -> 2 [label=" -7"]
}

or this

digraph exemple2UPFM {
label=mylab;
ratio = 0.5;
spline = line; forcelabels=true;
node [shape = circle;style=filled; color=lightgrey;];
1 [xlabel="10" fontcolor= blue group=g1];
4 [xlabel="-6" fontcolor= red group=g1];
2 [xlabel="4" fontcolor= blue group=g2];
5 [xlabel="-8" fontcolor= red group=g2];
node [shape = circle; style=""];
{rank=same; 1 2}
{rank=same; 4 5}
1 -> 2 [label=" 1"]
1 -> 3 [label=" 8"]
1 -> 4 [label=" 1"]
2 -> 3 [label=" 2"]
3 -> 4 [label=" 1"]
3 -> 5 [label=" 4"]
5 -> 4 [label="-12"]
5 -> 2 [label=" -7"]
}

In this specific case, the only difference is the direction of arc between 5 and 4. But the difference in the graph layout is brutal. In the second example, if I keep the arc 4 -> 5 and I add the "back" option, the layout comes back to the original.

Do you have any clue why this, and if there is a way to avoid the position of the nodes to change at any minimum arc modification?

Thank you very much for your help.

Upvotes: 1

Views: 1901

Answers (1)

vaettchen
vaettchen

Reputation: 7659

graphviz doesn't know how you want the graph to look like. It figures it out following the instructions you give. In the default layout (Top to Bottom) when you say A -> B it puts the first node on top of the second, and in case they are of the same rank, it puts the first to the left of the second. If you look at the "brutally" changed layout, you see the logic.

So if you want to stay with the node arrangement of your first listing, it's a safer bet to say 2 -> 5[ dir = back ] in both cases, and the 4 -> 5[ dir = back ] should come as no surprise. If you keep graphviz' logic in mind, you should be able to get your layouts sorted in most cases. Invisible nodes and edges, or adding weight to edges is the next level.

Just to illustrate it:

digraph exempleSO 
{
    label=mylab;
    ratio = 0.5;
    spline = line;
    forcelabels=true;

    node [shape = circle;style=filled; color=lightgrey;];
    1 [xlabel="10" fontcolor= blue group=g1];
    4 [xlabel="-6" fontcolor= red group=g1];
    2 [xlabel="4" fontcolor= blue group=g2];
    5 [xlabel="-8" fontcolor= red group=g2];

    node [shape = circle; style=""]
    {rank=same; 1 2}
    {rank=same; 4 5}
    1 -> 2 [label=" 1"]
    1 -> 3 [label=" 8"]
    1 -> 4 [label=" 1"]
    2 -> 3 [label=" 2"]
    3 -> 4 [label=" 1"]
    3 -> 5 [label=" 4"]
    4 -> 5 [dir = back, label=" 12 back"]
    2 -> 5 [dir = back, label=" -7 back"]
}

yields

enter image description here

Upvotes: 2

Related Questions