Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to control node placement in graphviz (i.e. avoid edge crossings)

I'm using graphviz (dot) to generate the graph you can see below. The node in the lower left corner (red ellipse) causes annoyance as its edges cross several edges of the adjacent node. Is there a way to restrain node placement to a certain area?

graph with edge crossing several other edges

like image 645
jnns Avatar asked Mar 19 '10 10:03

jnns


2 Answers

You could create an invisible constraint, to cause the red node to appear to the left of all other nodes.

redNode -> leftmostNode [style=invis]; 

Before:

before

After:

after

like image 88
smokris Avatar answered Sep 27 '22 18:09

smokris


I like @smokris' [style=invis] trick for persuading graphviz to put things where you want them, by adding edges which affect layout but aren't visible.


Another trick is the constraint attribute, which lets you add edges which are visible but don't affect layout.

If adding a new edge messes up your graph, set [constraint=false] on that edge: now graphviz will ignore it when placing nodes. 🙂

If false, the edge is not used in ranking the nodes. For example, in the graph

digraph G {     a -> c;     a -> b;     b -> c [constraint=false];   } 

the edge b -> c does not add a constraint during rank assignment, so the only constraints are that a be above b and c, yielding the graph:

like image 29
tjvr Avatar answered Sep 27 '22 18:09

tjvr