[ACCEPTED]-Why does a top to bottom graphviz dot graph get layed out counter clockwise?-dot

Accepted answer
Score: 10

Why does this code produce this graph?

A 22 directed graph puts its nodes on different 21 ranks depending on their relations. Since 20 1 points to 2, it must be above 2, and since 19 2 points to 3 it gets to be above 3. But 18 since 3 also points to 1, the circle is 17 completed - any of the 3 nodes could be 16 on top. Graphviz simply puts the first mentioned 15 node on top. Therefore, if you write instead:

2 -> 3 -> 1 -> 2;

node 14 2 will be on top, and when using

3 -> 1 -> 2 -> 3;

node 3 will 13 be the top node.

Probably the layout engine 12 neato would be more appropriate for this graph, producing 11 a graph with a clockwise direction:

neato layout

If you 10 absolutely must use the dot layout engine, the 9 following dot code

digraph {
  rankdir = TB;
  1 -> 2;
  3 -> 2 [dir=back];
  3 -> 1;
  {rank=same; 2; 3;}
}

produces the desired output 8 by changing the edge 2->3 into 3->2 and 7 at the same time inverting the direction 6 of the arrow.

Or, an other variant of the 5 same technique, easier to explain: We reverse 4 the order of all arrows (1->3->2->1), but display 3 them backwards (dir=back), and force node 2 2 and 3 to be on the same rank:

rankdir = TB;
edge[dir=back];
1 -> 3 -> 2 -> 1;
{rank=same; 2;3;}

This hack 1 yields the following result:

hack

More Related questions