3

Why does this code produce this graph?

digraph {
 rankdir = TB;
 1 -> 2 -> 3 -> 1;
}

1

How can I get graphviz/dot to produce a clockwise direction like this?

2

Update

This is the final graph I want to generate (afaik logically correct this way)

digraph {
  rankdir = TB
  start -> 1
  1 -> 2 -> 3 -> 1
  3 -> end
  3 -> increment
  end -> product
  {rank = same; 2; 3; increment}
  {rank = same; end; product}
}

Which produces this result

3

While I want this

4

bad_coder
  • 11,289
  • 20
  • 44
  • 72
Alex Lawrence
  • 1,160
  • 3
  • 10
  • 19
  • `1 -> 2 -> 3 -> 4 -> 1; {rank = same; 2; 4}` also produces a counter clockwise graph while it could well be clockwise – Alex Lawrence Feb 04 '11 at 13:47
  • 1
    Are you looking for an answer on how to do this the "correct" way? I'm 99% sure there is no "magic" instruction which automatically produces this output, and whatever else might be possible to achieve the desired output (invisible nodes etc.) will be a lot "hackier" than reversing edge directions. Graphviz's strength is to *automatically* layout graphs, and laying out nodes from left to right seems correct to me. If you need to manually fine-tune them, other tools may be more useful and faster to achieve the result. – marapet Feb 06 '11 at 20:55
  • Then I won´t search for a "correct" way anymore :) I just thought since everything is layed out counter clockwise (afaik) there has to be a setting to reverse that. But I´m really fine with reverting the edge directions if there is none. – Alex Lawrence Feb 07 '11 at 18:40
  • An other related SO question http://stackoverflow.com/questions/1510784/right-to-left-edges-in-dot-graphviz – marapet Feb 07 '11 at 20:04

1 Answers1

10

Why does this code produce this graph?

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

2 -> 3 -> 1 -> 2;

node 2 will be on top, and when using

3 -> 1 -> 2 -> 3;

node 3 will be the top node.

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

neato layout

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

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

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

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

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

This hack yields the following result:

hack

marapet
  • 54,856
  • 12
  • 170
  • 184
  • Thanks for the explanation. I am using dottex in LaTeX. To be honest I have almost no experience in graphviz/dot/neato or whatever I need. If I write `layout=neato` the graph get´s all messed up. – Alex Lawrence Feb 03 '11 at 21:25
  • But your last mentioned hack does the trick for me. While I know this may be not the "right" answer, it is all I need right now. Thanks – Alex Lawrence Feb 03 '11 at 21:46
  • 1
    Thanks for the `[dir=back]` trick, that helped me create my desired graph layout in Sphinx (which uses the dot layout engine by default). – vdboor May 02 '12 at 12:41
  • `[dir=back]` lets you do control system graphs where feed back paths are usually on the bottom with a reverse direction. See: [tikz/latex example](http://www.texample.net/tikz/examples/control-system-principles/). – artless noise Nov 17 '19 at 06:43