graphviz Tree Layout - graphviz

I am using graphviz for the first time. I just need a tree layout so that all the childs are at the same level.
For example,
A->B
A->C
A->D
THEN B, C AND D SHOULD BE AT THE SAME LEVEL.
Following is the code I am using.
digraph unix {
size="6,6";
node [color=lightblue2, style=filled];
"A:1000" -> "B:300";
"A:1000" -> "C:300";
"A:1000" -> "D:200";
"B:300" -> "E:140";
"B:300" -> "F:164";
"B:300" -> "G:75";
"C:300" -> "H:135";
"C:300" -> "I:91";
"D:200" -> "E:140";
"D:200" -> "F:164";
"D:200" -> "G:75";
"E:140" -> "F:164";
"E:140" -> "G:75";
"F:164" -> "G:75";
"G:75" -> "H:135";
"H:135" -> "I:91";
}
How do I make sure that the childs are at the same level?

To get nodes on the same level, say "B:300" and "C:300", add the following line:
{rank=same; "B:300" "C:300"}

The graph you presented does not represent a tree, but a directed acyclic graph (In a tree there is only one distinct path between every pair of nodes). If your input would have been a tree, then just using dot would produce what you want. If you also want to add non-tree edges, like "C:300" -> "H:135" in your example, you could specify a lower weight for them, to make sure that dot doesn't try to optimize the layout with respect to these edges.
"C:300" -> "H:135" [weight=0];
"C:300" -> "I:91" [weight=0];
Note that these two edges become very long (and to dot, ugly) with this setting, and this is the reason why the node "C:300" is placed as it is in your original graph.

Related

In GraphViz, in a strict graph, is it possible to have 2 edges between nodes if they have different attributes?

What I want to achieve is, if I have something like
Strict Digraph G {
a -> b [label = edge1]
a -> b [label = edge2]
a -> b
a -> b
a -> b [label = edge1]
}
then I should get a graph with 2 nodes and 3 edges between them (One with label edge1, one with edge2 and one without a label).
It doesn't seem to be possible, with the strict keyword with this input, it only draws one edge.
Sorry, not "legal". From https://graphviz.org/doc/info/lang.html
A graph may also be described as strict. This forbids the creation of multi-edges, i.e., there can be at most one edge with a given tail node and head node in the directed case.

Invert edge trajectory graphviz

i am trying to graph a doubly linked circular list in .dot language but it does not display as i want. Both of the circular edges are under the nodes but i want 1 of them above
this is what i want:
expected result
but i get this instead:
given result
here is my .dot code: https://dreampuf.github.io/GraphvizOnline/#digraph%20{%0A%20%20node[shape=record];%0A%20%20graph[pencolor=transparent];%0A%20%20rankdir=LR;%0A%20%20p1[label=%22{%3Cprev%3E|%3Cdata%3E%2012|%3Cnext%3E}%22];%0A%20%20p2[label=%22{%3Cprev%3E|%3Cdata%3E%2012|%3Cnext%3E}%22];%0A%20%20p3[label=%22{%3Cprev%3E|%3Cdata%3E%2012|%3Cnext%3E}%22];%0A%0A%0A%20%20%20%20p1:next%20-%3E%20p2:prev;%0A%20%20p2:next%20-%3E%20p3:prev;%0A%20%20p2:prev%20-%3E%20p1:next;%0A%20%20p3:prev%20-%3E%20p2:next;%0A%20%20%20%20edge[tailclip=false,dir=%22forward%22%20splines=%22compound%22%20constraint=%20%22false%22];%0A%20%0A%20%0Ap3:next%20-%3E%20p1:prev;%0A%0Ap1:prev%20-%3E%20p3:data;%0A%0A%20%20%20%20%0A}
thank you
Main change was to add a port position (https://graphviz.org/docs/attr-types/portPos/) to the head & tail of the edge. Also removed some unneeded bits.
digraph {
node[shape=record];
graph[pencolor=transparent];
rankdir=LR;
p1[label="{<prev>|<data> 12|<next>}"];
p2[label="{<prev>|<data> 12|<next>}"];
p3[label="{<prev>|<data> 12|<next>}"];
p1:next -> p2:prev;
p2:next -> p3:prev;
p2:prev -> p1:next;
p3:prev -> p2:next;
p1:prev:n -> p3:data:n;
// edge[tailclip=false,dir="forward" splines="compound" constraint= "false"];
p3:next -> p1:prev;
}
Giving:

Weird node placement of dot graph

I have the following source code for a graph in dot:
digraph name {
rankdir="LR";
node [shape="record"];
1 [label="OUTPUT"];
A [label="FWD|<i>i|<r_in>r_in|<r_out>r_out|<o>o"];
B [label="FIFO|<r_in>r_in|<o>o"];
C [label="Cons|<i>i|<r_out>r_out|<o>o"];
A:o:e -> C:i:w;
C:r_out:w -> A:r_in:e;
B:o:e -> A:i:w;
C:o:e -> 1:w;
A:r_out:w -> B:r_in:e;
}
It consists of 4 nodes, which essentially could be placed one after another, in the order B -> A -> C -> OUTPUT. If dot would place the nodes in this order, only few edge would cross between two nodes.
However, calling dot like this:
dot mygraph.dot -Tpng -o mygraph.png
creates the following mess:
Edit: It seems, the order of the nodes in the source is important. However, as the source is generated from a program, outputting its internal signal flow structure, I cannot rely on it to put the nodes in the right order. I thought, dot and its graph layout engine can figure out on its own, which nodes are the first ones, such that the wires do not cross.
Just define the nodes in the desired order:
digraph name {
rankdir="LR";
node [shape="record"];
B [label="FIFO|<r_in>r_in|<o>o"];
A [label="FWD|<i>i|<r_in>r_in|<r_out>r_out|<o>o"];
C [label="Cons|<i>i|<r_out>r_out|<o>o"];
1 [label="OUTPUT"];
A:o:e -> C:i:w;
C:r_out:w -> A:r_in:e;
B:o:e -> A:i:w;
C:o:e -> 1:w;
A:r_out:w -> B:r_in:e;
}
yields

DOT - how to reduce the lines connecting two nodes

I have a function that records the how methods are called at run-time. I am trying to use dot to visualize this information. Some methods are called many times- eg, in the case of a loop - in the graph i would want to have only one line connecting both nodes - Is there a switch do to this... See example diagram "RawFitsData._method" calls "RawFitsData.init"; multiple times, in the graph, i would like to display only one line
digraph G{
splines=false;
ranksep=1;
node[shape=box, color=grey, style=filled];
"DBProxy.fetch_from_database" -> "RawFitsData._method";
"RawFitsData._method" -> "RawFitsData.__init__";
"RawFitsData._method" -> "RawFitsData.__init__";
"RawFitsData._method" -> "RawFitsData.__init__";
"RawFitsData._method" -> "RawFitsData.__init__";
"RawFitsData._method" -> "RawFitsData.__init__";
"RawFitsData.__init__" -> "RawFitsData._method";
"RawFitsData._method" -> "RawFitsData.__init__";
"RawFitsData.__init__" -> "RawFitsData._set_pathname";
"RawFitsData._set_pathname" ->"split";
"RawFitsData._set_pathname" ->"setter";
"RawFitsData.__init__" -> "RawFitsData._get_pathname";
"RawFitsData._get_pathname" ->"getter";
"RawFitsData._get_pathname" ->"join";
"deepcopy" ->"RawFitsData._method";
"RawFitsData._method" -> "RawFitsData.__reduce__";
"RawFitsData.__reduce__" -> "WeakValueDictionary.__setitem__";
"RawFitsData.__reduce__" ->"getter";
"RawFitsData.__reduce__" -> "RawFitsData._get_pathname";
"RawFitsData._get_pathname" ->"getter";
}
Use strict digraph.
strict digraph G {
splines=false;
...
It will omit repeating edges. Reference: DOT documentation.

simple "T shaped" graph in graphviz

Need draw a graph with dot/graphviz like this image:
The texts can be above arrows, like graphviz does it. But how to achieve the T-layout? Need make a cluster for the top row?
This is one possibility using rank=same for a subgraph:
digraph g {
node[shape=point, width=0.2];
{
rank=same;
p1 -> n [label="text1"];
n -> p2 [label="text2"];
}
n -> p3 [label="text3", dir=back];
n[label="node", shape=rect, style=rounded];
}
You could also use a left-right layout instead of top-down.
An other possibility is to disable the effect of some edges using constraint=false:
digraph g {
node[shape=point, width=0.2];
p1 -> n [label="text1", constraint=false];
n -> p2 [label="text2", constraint=false];
n -> p3 [label="text3", dir=back];
n[label="node", shape=rect, style=rounded];
}
The result is the same.
dot usually layouts trees in layers. To force an edge to not be a layer separation you can add the constraint=false option. So something like:
digraph {
A [shape=point]
B [shape=point]
C [shape=point]
N [label="node"]
A -> N [label="text1", constraint=false]
N -> B [label="text2", constraint=false]
N -> C [label="text3", dir=back]
}
should work.
Note that the edge from the lower node to "node" has to be backwards, since dot layouts trees from top to bottom. Therefore the logical edge direction has to be from top to bottom, even though the display direction might be the other way round (which is the case here).

Resources