How to display identical captions of nodes - graphviz

I wanted to draw RB-Tree as in Wiki 1,
but I can't find a possibility of display of the identical caption for different nodes(in this case nil - nodes). It is possible?
digraph "rb-tree"{
bgcolor = whitesmoke;
forcelabels = true;
margin = 0;
node [shape = circle,
style = filled,
fontsize = 14,
margin = 0,
fillcolor = black,
fontcolor = white];
edge [fontsize = 10,
arrowhead = vee];
8 [fillcolor = red];
17 [fillcolor = red];
nil_8l [shape = box];
nil_8r [shape = box];
nil_17l [shape = box];
nil_17r [shape = box];
13->8;
13->17;
8->nil_8l;
8->nil_8r;
17->nil_17l;
17->nil_17r;
}

Graphiz has the possibility to set labels. So use:
digraph "rb-tree"{
bgcolor = whitesmoke;
forcelabels = true;
margin = 0;
node [shape = circle,
style = filled,
fontsize = 14,
margin = 0,
fillcolor = black,
fontcolor = white];
edge [fontsize = 10,
arrowhead = vee];
8 [fillcolor = red];
17 [fillcolor = red];
nil_8l [shape = box label="nil"];
nil_8r [shape = box label="nil"];
nil_17l [shape = box label="nil"];
nil_17r [shape = box label="nil"];
13->8;
13->17;
8->nil_8l;
8->nil_8r;
17->nil_17l;
17->nil_17r;
}

Related

Forcing the edge between subgraphs to go left or right

I have a PATRICA trie for which I'm generating a GraphViz file. The internal nodes are the skip values and the edges are dotted 0 and solid 1. It is preferred that 0 is to the left of 1, giving an alphabetical order of the leaves. I re-arranged the order I visit the graph so dot gives this result. However, when I group them in trees in a forest using subgraphs, I can't seem to force dot to reliably respect the order for inter-subgraph edges.
digraph {
rankdir=TB;
node [shape = box, style = filled, fillcolor = lightsteelblue];
// forest size 2.
subgraph cluster_tree0 {
style = filled; fillcolor = lightgray; label = "tree 0";
// branches
branch0_0 [label = "3", shape = none, fillcolor = none];
branch0_0 -> branch0_1;
branch0_1 [label = "0", shape = none, fillcolor = none];
branch0_1 -> branch0_2 [style = dashed];
branch0_2 [label = "1", shape = none, fillcolor = none];
branch0_2 -> leaf0_1 [style = dashed, color = royalblue];
branch0_2 -> leaf0_2 [color = royalblue];
branch0_1 -> branch0_3;
branch0_3 [label = "2", shape = none, fillcolor = none];
branch0_3 -> leaf0_3 [style = dashed, color = royalblue];
branch0_3 -> leaf0_4 [color = royalblue];
// leaves
leaf0_1 [label = "u"];
leaf0_2 [label = "v"];
leaf0_3 [label = "x"];
leaf0_4 [label = "y"];
}
branch0_0 -> branch1_0 [lhead = cluster_tree0, ltail = cluster_tree1, color = firebrick, style = dashed];
subgraph cluster_tree1 {
style = filled; fillcolor = lightgray; label = "tree 1";
// branches
branch1_0 [label = "0", shape = none, fillcolor = none];
branch1_0 -> leaf1_0 [style = dashed, color = royalblue];
branch1_0 -> branch1_1;
branch1_1 [label = "1", shape = none, fillcolor = none];
branch1_1 -> leaf1_1 [style = dashed, color = royalblue];
branch1_1 -> branch1_2;
branch1_2 [label = "0", shape = none, fillcolor = none];
branch1_2 -> leaf1_2 [style = dashed, color = royalblue];
branch1_2 -> leaf1_3 [color = royalblue];
// leaves
leaf1_0 [label = "f"];
leaf1_1 [label = "m"];
leaf1_2 [label = "n"];
leaf1_3 [label = "o"];
}
}
On one graph it works fine, but it the subgraphs are reversed to the order I want them.
I reversed the order in the file and it still looks the same. I played around with it and I could get it turned around by rank=same, ordering=out, and invis edges somehow, but I want it to be programmatic. Is there any easy way to draw the red dotted line, representing 0, to the left, instead of to the right, of the solid line, representing 1?
OK, a total kludge, but probably fully scriptable.
First, your input somewhat reworked:
digraph {
rankdir=TB;
newrank=true // helps
graph [splines=false]
node [shape = box, style = filled, fillcolor = lightsteelblue];
// forest size 2.
subgraph cluster_tree1 {
style = filled; fillcolor = lightgray; label = "tree 1";
// branches
branch1_0 [label = "0", shape = none, fillcolor = none];
branch1_0 -> leaf1_0 [style = dashed, color = royalblue];
branch1_0 -> branch1_1;
branch1_1 [label = "1", shape = none, fillcolor = none];
branch1_1 -> leaf1_1 [style = dashed, color = royalblue];
branch1_1 -> branch1_2;
branch1_2 [label = "0", shape = none, fillcolor = none];
branch1_2 -> leaf1_2 [style = dashed, color = royalblue];
branch1_2 -> leaf1_3 [color = royalblue];
// leaves
leaf1_0 [label = "f"];
leaf1_1 [label = "m"];
leaf1_2 [label = "n"];
leaf1_3 [label = "o"];
}
subgraph cluster_tree0 {
style = filled; fillcolor = lightgray; label = "tree 0";
// branches
branch0_0 [label = "3", shape = none, fillcolor = none];
branch0_0 -> branch0_1;
branch0_1 [label = "0", shape = none, fillcolor = none];
branch0_1 -> branch0_2 [style = dashed];
branch0_2 [label = "1", shape = none, fillcolor = none];
branch0_2 -> leaf0_1 [style = dashed, color = royalblue];
branch0_2 -> leaf0_2 [color = royalblue];
branch0_1 -> branch0_3;
branch0_3 [label = "2", shape = none, fillcolor = none];
branch0_3 -> leaf0_3 [style = dashed, color = royalblue];
branch0_3 -> leaf0_4 [color = royalblue];
// leaves
leaf0_1 [label = "u"];
leaf0_2 [label = "v"];
leaf0_3 [label = "x"];
leaf0_4 [label = "y"];
}
// position the clusters (trees)
{rank=same branch1_0 -> branch0_1 [style=invis weight=0]}
// a kludge, we'll add this edge in later
graph [comment="branch0_0 -> branch1_0 [color = firebrick, style = dashed constraint=false weight=0 ];"]
}
The "problem" is the cluster-to-cluster branch, so we remove it (for the first dot pass). And we add the invisible edge to position the clusters where we want them.
Run this input into dot -Tdot >myfile.dot. This sets positions for all nodes & edges.
Run that thru gawk (any language) to un-comment the commented branch(s) and insert into the file.
Finally, neato -n2 -Tpng fixedfile >fixed.png
(yuck, but it works)
f=atrie4.gv;
T=png; F=`basename $f .gv`;dot -Tdot $f >$F.dot;
gawk '
$1~/comment/{
sub(/[\t ]*comment="/,"")
sub(/"[\],;]?[\t ]*$/,"")
add[++a]=$0
next
}
{oline[++o]=$0}
END{
for (i=1;i<o;i++)print oline[i]
for (i=1;i<=a;i++)print add[i]
print oline[o]
}' $F.dot|
neato -n2 -Tpng >$F.$T
firefox $F.$T

self loop edges too short and ugly in graphviz

I drawed a picture using graphviz. Please see FSM.
I think It is ugly because self loop edges are so short.
The attribute "minlen" of edges doesn't work for me.
And I tried several ports of the node, but it all shows a mess except my current implementation. Do you have a clever idea for me ?
Code is here:
digraph finite_state_machine {
rankdir=LR;
size="8,2"
fontname="Verdana"
node [shape = doublecircle]; Idle;
node [shape = circle,nodesep = "2.0"];
Working:s -> Working:s [ label = "response[j]?" ,minlen = 50000];
Idle -> Working [ label = "boot" ];
Working:n -> Working:n [ label = "sendtx[i]!",minlen = 50000 ];
Working:e -> Working:e [ label = "qry!" ,minlen = 50000];
}
Adding nodesep=1; makes loops larger, although not nicer. So this would help:
digraph finite_state_machine {
rankdir=LR;
size="8,2"
fontname="Verdana"
node [shape = doublecircle]; Idle;
node [shape = circle,nodesep = "2.0"];
Working:s -> Working:s [ label = "response[j]?" ,minlen = 50000];
Idle -> Working [ label = "boot" ];
Working:n -> Working:n [ label = "sendtx[i]!" ];
Working:e -> Working:e [ label = "qry!"];
nodesep=1;
}
Will produce something like:
Dot Output

Node placement in family tree visualization with Dot/Graphviz

I'm trying to generate family tree visualizations from a database using Dot/Graphviz. First results look promising, but there is one layout issue that I haven't been able to fix yet.
When I use the code listed below, it will produce
I'm totally happy with this. But as soon as I try to add another node between families F4/M4/M5 and F2/M2, which can be done by uncommenting the two lines in the code below, it will give me
Male2 is now placed far away from Female2 and between Female4 and Male4. So the families F2/M2 and F4/M4/M5 are completely torn apart. I tried to increase the weight for the family connections (value is 100) in order to make sure families F2/M2 and F4/M4/M5 are placed together, but this doesn't work. Also changing order of nodes or connections did not help so far. The best solution would be, if family F4/M4/M5 could be placed on the left, MaleX in the center and family F2/M2 on the right.
Does anyone have a suggestion? I would prefer to not change the order in which the nodes and connections are defined in the code, because this is done by a script and is kind of predefined by the database structure.
graph Test {
rankdir = BT;
splines = ortho;
center = true;
{
rank = same;
NodeFemale1 [label = Female1];
NodeMale1 [label = Male1];
ConnectionFemale1Male1 [shape = box, label = "", height = 0, width = 1, margin = 0, penwidth = 1];
NodeFemale1 -- ConnectionFemale1Male1 -- NodeMale1 [weight = 100, penwidth = 2];
}
ConnectionChildren11 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
ConnectionFemale1Male1 -- ConnectionChildren11 [penwidth = 2];
{
rank = same;
NodeFemale2 [label = Female2];
NodeMale2 [label = Male2];
ConnectionFemale2Male2 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
NodeFemale2 -- ConnectionFemale2Male2 -- NodeMale2 [weight = 100, penwidth = 2];
}
ConnectionChildren11 -- NodeMale2 [penwidth = 2];
ConnectionChildren22 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
ConnectionFemale2Male2 -- ConnectionChildren22 [penwidth = 2];
NodeMale3 [label = Male3];
ConnectionChildren22 -- NodeMale3 [weight = 10, penwidth = 2];
NodeFemale3 [label = Female3];
ConnectionChildren22 -- NodeFemale3 [penwidth = 2];
// NodeMaleX [label = MaleX];
// ConnectionChildren11 -- NodeMaleX [weight = 10, penwidth = 2];
{
rank = same;
NodeFemale4 [label = Female4];
NodeMale4 [label = Male4];
NodeMale5 [label = Male5];
ConnectionFemale4Male4 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
NodeFemale4 -- ConnectionFemale4Male4 -- NodeMale4 [weight = 100, penwidth = 2];
ConnectionMale4Male5 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
NodeMale4 -- ConnectionMale4Male5 -- NodeMale5 [weight = 100, penwidth = 2];
}
ConnectionChildren11 -- NodeFemale4 [penwidth = 2];
ConnectionChildren44 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
ConnectionFemale4Male4 -- ConnectionChildren44 [penwidth = 2];
NodeFemale6 [label = Female6];
ConnectionChildren44 -- NodeFemale6 [weight = 10, penwidth = 2];
NodeFemale7 [label = Female7];
ConnectionChildren44 -- NodeFemale7 [penwidth = 2];
ConnectionChildren45 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
ConnectionMale4Male5 -- ConnectionChildren45 [penwidth = 2];
NodeFemale8 [label = Female8];
ConnectionChildren45 -- NodeFemale8 [penwidth = 2];
}
I don't think you can take control the way you want without resorting to subgraph. This may or may not be helpful in the context of your script and database that provides you with the nodes and edges but I have tried something that hopefully comes close to what you want:
graph Test
{
/* layout / format */
rankdir = BT; // bottoms to top
splines = ortho; // edges orthogonal
center = true; // page center
edge [ penwidth = 2 ]; // edge thickness
node [ width = 1.1 ]; // conistent node size
/* node and edge definitions as produced by the script (?) */
NodeFemale1 [label = Female1];
NodeMale1 [label = Male1];
ConnectionFemale1Male1 [shape = box, label = "", height = 0, width = 1, margin = 0, penwidth = 1];
// 1 NodeFemale1 -- ConnectionFemale1Male1 -- NodeMale1 [weight = 100, penwidth = 2];
ConnectionChildren11 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
// 2 ConnectionFemale1Male1 -- ConnectionChildren11 [penwidth = 2];
NodeFemale2 [label = Female2];
NodeMale2 [label = Male2];
ConnectionFemale2Male2 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
// 3 NodeFemale2 -- ConnectionFemale2Male2 -- NodeMale2 [weight = 100, penwidth = 2];
// 4 ConnectionChildren11 -- NodeMale2 [penwidth = 2];
ConnectionChildren22 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
// 5 ConnectionFemale2Male2 -- ConnectionChildren22 [penwidth = 2];
NodeMale3 [label = Male3];
// 6 ConnectionChildren22 -- NodeMale3 [weight = 10, penwidth = 2];
NodeFemale3 [label = Female3];
// 7 ConnectionChildren22 -- NodeFemale3 [penwidth = 2];
NodeMaleX [label = MaleX];
// 8 ConnectionChildren11 -- NodeMaleX [weight = 10, penwidth = 2];
NodeFemale4 [label = Female4];
NodeMale4 [label = Male4];
NodeMale5 [label = Male5];
ConnectionFemale4Male4 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
// 9 NodeFemale4 -- ConnectionFemale4Male4 -- NodeMale4 [weight = 100, penwidth = 2];
ConnectionMale4Male5 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
// 10 NodeMale4 -- ConnectionMale4Male5 -- NodeMale5 [weight = 100, penwidth = 2];
// 11 ConnectionChildren11 -- NodeFemale4 [penwidth = 2];
ConnectionChildren44 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
// 12 ConnectionFemale4Male4 -- ConnectionChildren44 [penwidth = 2];
NodeFemale6 [label = Female6];
// 13 ConnectionChildren44 -- NodeFemale6 [weight = 10, penwidth = 2];
NodeFemale7 [label = Female7];
// 14 ConnectionChildren44 -- NodeFemale7 [penwidth = 2];
ConnectionChildren45 [shape = box, label = "", height = 0, width = 0, margin = 0, penwidth = 1];
// 15 ConnectionMale4Male5 -- ConnectionChildren45 [penwidth = 2];
NodeFemale8 [label = Female8];
// 16 ConnectionChildren45 -- NodeFemale8 [penwidth = 2];
/* family / generation subgraphs */
subgraph cluster0
{
style = filled;
fillcolor = lightgrey;
color = white;
{ rank = same; NodeFemale1; ConnectionFemale1Male1; NodeMale1 }
NodeFemale1 -- ConnectionFemale1Male1 -- NodeMale1;
}
ConnectionFemale1Male1 -- ConnectionChildren11;
subgraph cluster1
{
{ rank = same; NodeMale2; ConnectionFemale2Male2; NodeFemale2 }
NodeFemale2 -- ConnectionFemale2Male2 -- NodeMale2;
}
ConnectionChildren11 -- NodeMale2;
subgraph cluster2
{
NodeMaleX;
}
ConnectionChildren11 -- NodeMaleX;
subgraph cluster3
{
{ rank = same; NodeFemale4; NodeMale4; NodeMale5; ConnectionFemale4Male4; ConnectionMale4Male5 }
NodeFemale4 -- ConnectionFemale4Male4 -- NodeMale4 -- ConnectionMale4Male5 -- NodeMale5;
}
ConnectionChildren11 -- NodeFemale4;
subgraph cluster4
{
color = white;
{ rank = same; NodeMale3; NodeFemale3 }
}
ConnectionFemale2Male2 --ConnectionChildren22;
ConnectionChildren22 -- { NodeMale3 NodeFemale3 };
subgraph cluster5
{
color = white;
{ rank = same; NodeFemale6; NodeFemale7 }
}
ConnectionFemale4Male4 --ConnectionChildren44;
ConnectionChildren44 -- { NodeFemale6 NodeFemale7 };
subgraph cluster6
{
color = white;
NodeFemale8;
}
ConnectionMale4Male5 --ConnectionChildren45;
ConnectionChildren45 -- NodeFemale8;
}
The clusters can be customized, as demonstrated on the parents level. If you just set color = white they become "invisible", as shown in the grandchildren generation.

graphviz - how to create a 'roundtrip' flow

I'm trying to create a 'round trip' graph using the graphviz. Given the result below, my objective is to have the PINK squares between the NET and the COM (note from the picture below that they are pushed to the right after the NET).
the COM must be the first node on the LEFT.
the dot code:
digraph {
rankdir = LR;
graph [fontname = "helvetica" ];
node [fontname = "helvetica"];
edge [fontname = "helvetica"];
COM [shape = circle, fillcolor = yellow, style = filled];
NET [shape = circle, fillcolor = yellow, style = filled];
fn1 [shape = BOX, fillcolor = green, style = filled, label = "PORT006"];
clazz1 [shape = BOX, fillcolor = red, style = filled, label = "O"];
ddate1 [shape = BOX, fillcolor = red, style = filled, label = "21-Apr-13"];
status1 [shape = BOX, fillcolor = red, style = filled, label = "OJ3COM6M"];
clazz1exch [shape = BOX, fillcolor = green, style = filled, label = "G"];
ddate1exch [shape = BOX, fillcolor = green, style = filled, label = "13-May-13"];
status1exch [shape = BOX, fillcolor = green, style = filled, label = "GJ3COM6M"];
fn2 [shape = BOX, fillcolor = pink, style = filled, label = "PORT005"];
rbd2 [shape = BOX, fillcolor = pink, style = filled, label = "O"];
ddate2 [shape = BOX, fillcolor = pink, style = filled, label = "29-Apr-13"];
fare2 [shape = BOX, fillcolor = pink, style = filled, label = "OJ3COM6M"];
{ rank=same; clazz1 -> clazz1exch; }
{ rank= same; ddate1 -> ddate1exch; }
{ rank=same; status1 -> status1exch; }
COM -> fn1 -> clazz1exch -> ddate1exch -> status1exch -> NET;
NET -> fn2 -> rbd2 -> ddate2 -> fare2 -> COM;
}
If you simply reverse the direction of the edges going back (dir=back) by changing the line
NET -> fn2 -> rbd2 -> ddate2 -> fare2 -> COM;
into
edge[dir=back];
COM -> fare2 -> ddate2 -> rbd2 -> fn2 -> NET;
you should get:

Why does Graphviz/Dot act so weirdly to this particular code?

I am using GraphViz 2.28 (the current stable version) on Windows 7, and Graphviz/Dot crashes for the following code.
digraph G {
ranksep = 1.0; size = "10,10";
{
node [shape = plaintext, fontsize = 20];
GAWM1 -> GAWM2 -> 0;
}
node [shape = box];
{rank = same;0;wx1;wx2;rx1;}
wx1 -> wx2;
wx2 -> rx1;
wx1 -> rx1[color = blue];
subgraph struct
{
node [shape = record];
rx11 [shape = record, label = "rx1 | [x=[wx2]]"];
}
{rank = same; GAWM1; "rx11";}
// W'WR Order:
wx2 -> wx1[style = dashed, color = red, label = "1"]; }
Things become weird when I perform some experiments on this example:
It works well when only the last statement "wx2 -> wx1[style = dashed, color = red, label = "1"];" is removed;
It works well when only the eighth line "{rank = same;0;wx1;wx2;rx1;}" is removed;
It also works well when only the label (that is ", label = "1"") in the last statement is removed.
I have reported the problem to Graphviz Issue Tracker, without reply yet. Could you help to find out the reason?
Thank you.
The following variation of your script does not crash:
digraph G {
ranksep = 1.0; size = "10,10";
{
node [shape = plaintext, fontsize = 20];
GAWM1 -> GAWM2 -> 0;
}
node [shape = box];
{rank = same;0;wx1;wx2;rx1;}
wx1 -> wx2;
wx2 -> rx1;
wx1 -> rx1[color = blue];
subgraph struct
{
node [shape = record];
rx11 [shape = record, label = "rx1 | [x=[wx2]]"];
}
{rank = same; GAWM1; "rx11";}
// W'WR Order:
//wx2 -> wx1[style = dashed, color = red, label = "1"];
wx2 -> wx1 [style=dashed, color=red];
}
Looks like a bug with the label of the short edge between wx1 and wx2.

Resources