How to left-align a node in Graphviz? - graphviz

I am trying to display digital circuit netlist with graphviz.
I am resorting to :
record shapes to represent input/output ports.
rankdir=LR to suggest a left to right alignment
The following code works fine. However, during layout, some inputs (here "i3") can be misaligned : I would expect i3 to be left-aligned, as for i1 and i2.
How can I do that ?
digraph G {
graph [rankdir = LR];
node[shape=record];
c1[ label="{ {<i1>i1|<i2>i2}| c1 | {<f>f} }"];
c2[ label="{ {<i0>i0}| c2 | {<out_0>out_0} }"];
c3[ label="{ {<i0>i0}| c3 | {<out_0>out_0} }"];
c4[ label="{ {<i0>i0|<i1>i1}| c4 | {<out_0>out_0} }"];
i1; i2; i3; f; i1 -> c2:i0[ label="w(1)"];
i2 -> c3:i0[ label="w(2)"];
i3 -> c4:i1[ label="w(4)"];
c1:f -> c4:i0[label="w(3)"];
c2:out_0 -> c1:i1[label="w(5)"];
c3:out_0 -> c1:i2[label="w(6)"];
c4:out_0 -> f[label="w(7)"];
}

Just specify that i1, i2 and i3 share the same rank:
digraph G {
graph [rankdir = LR];
node[shape=record];
c1[ label="{ {<i1>i1|<i2>i2}| c1 | {<f>f} }"];
c2[ label="{ {<i0>i0}| c2 | {<out_0>out_0} }"];
c3[ label="{ {<i0>i0}| c3 | {<out_0>out_0} }"];
c4[ label="{ {<i0>i0|<i1>i1}| c4 | {<out_0>out_0} }"];
i1; i2; i3; f; i1 -> c2:i0[ label="w(1)"];
i2 -> c3:i0[ label="w(2)"];
i3 -> c4:i1[ label="w(4)"];
c1:f -> c4:i0[label="w(3)"];
c2:out_0 -> c1:i1[label="w(5)"];
c3:out_0 -> c1:i2[label="w(6)"];
c4:out_0 -> f[label="w(7)"];
{rank=same; i1; i2; i3}
}

Related

Align nodes of subgraph with another subgraph nodes

How do I align nodes with each other between subgraphs?
In the example, I would like all the 0 nodes aligned vertically as well as the 1, 2, 3 and 4 nodes. It would be good if all the right edges of subgraphs were extended as necessary to align too.
digraph AlignWhenMissing {
rankdir=LR;
node [shape=box]
subgraph clusterA {label = "A Shift a4 along (no a3)";
a0 -> a1;
a1 -> a2;
a2 -> a4;
}
subgraph clusterB {label = "B Shift b2 above a2";
b0 -> b2;
b2 -> b3;
}
subgraph clusterC {label = "C Shift c3 above b3";
c0 -> c3;
}
A -> a0[lhead=clusterA];
B -> b0[lhead=clusterB];
C -> c0[lhead=clusterC];
a0 -> b0[constraint=false];
b3 -> c3[constraint=false];
a2 -> b2[constraint=false];
}
The minlen attribute (https://graphviz.org/docs/attrs/minlen/) applies to edges to lengthen them (i.e. to push them "out" by N ranks).
Then added invisible nodes and edges to increase cluster dimension.
digraph AlignWhenMissing {
rankdir=LR;
node [shape=box]
subgraph clusterA {label = "A Shift a4 along (no a3)";
a0 -> a1;
a1 -> a2;
a2 -> a4 [minlen=2];
}
subgraph clusterB {label = "B Shift b2 above a2";
b0 -> b2 [minlen=2]
b2 -> b3;
b4 [style=invis]
b3 -> b4 [style=invis]
}
subgraph clusterC {label = "C Shift c3 above b3";
c0 -> c3 [minlen=3];
c4 [style=invis]
c3 -> c4 [style=invis]
}
A -> a0[lhead=clusterA];
B -> b0[lhead=clusterB];
C -> c0[lhead=clusterC];
a0 -> b0[constraint=false];
b3 -> c3[constraint=false];
a2 -> b2[constraint=false];
}
Giving:

How to change the order of same rank nodes in Graphviz?

I'd like the column a ... i to be the first column from the left and a1 ... f1 the first column from the right. I tried to setup same rank, different weights, but with no luck. How can I control the order of columns?
digraph G {
newrank = true;
node [shape=record, style="rounded,filled", color="#5F6368", fillcolor="#F7F7F7", fontname="Arial", fontsize=10];
graph [fontname = "Arial"]; edge [fontname = "Arial"];
style = "dashed";
subgraph cluster0 {
color="#34A853";
a; a1; b; b1; c; c1; d; d1;
}
subgraph cluster1 {
color="#EA4335";
e1; f1;
subgraph cluster2 {
color="#FBBC05";
e; f; g; h; i;
}
}
a -> b -> c -> d [color="#34A853"];
a1 -> b1 -> c1 -> d1 [color="#4285F4"];
edge[style=invis];
{rank="same"; a -> a1 [constraint=false]}
{rank="same"; b -> b1 [constraint=false]}
{rank="same"; c -> c1 [constraint=false]}
{rank="same"; d -> d1 [constraint=false]}
{rank="same"; e -> e1 [constraint=false]}
{rank="same"; f -> f1 [constraint=false]}
d -> e -> f -> g -> h -> i;
d1 -> e1 -> f1;
}

Vertical alignment of nodes in graphviz

I am trying to make a graph where {C1;C2} are aligned vertically. Same with {A;B;C} and {E1;E2;E3}
Here is my code so far:
digraph G{
rankdir="LR";
S -> C1 [label="150"];
S -> C2 [label="130"];
C1 -> A [label="70"];
C1 -> B [label="80"];
C2 -> B [label="60"];
C2 -> C [label="70"];
C -> B [label="10"];
A -> E1 [label="70"];
B -> E1 [label="80"];
B -> D [label="70"];
C -> E3 [label="60"];
D -> E1 [label="40"];
D -> E2 [label="0"];
D -> E3 [label="30"];
E1 -> t [label="190"];
E2 -> t [label="0"];
E3 -> t [label="90"];
}
Here is the result so far:
{ rank = same; } is your friend. I have added invisible edges between A, B and C to keep them tighter together, and reversed one of your connections in orde to keep A over B over C etc.
digraph G
{
rankdir="LR";
{ rank = same; C1 C2 }
{ rank = same; A -> B -> C[ style = invis ] }
{ rank = same; E1 E2 E3 }
S -> C1 [label="150"];
S -> C2 [label="130"];
C1 -> A [label="70"];
C1 -> B [label="80"];
C2 -> B [label="60"];
C2 -> C [label="70"];
B -> C [dir = back, label="10"]; // to keep B above C
A -> E1 [label="70"];
B -> E1 [label="80"];
B -> D [label="70"];
C -> E3 [label="60"];
D -> E1 [label="40"];
D -> E2 [label="0"];
D -> E3 [label="30"];
E1 -> t [label="190"];
E2 -> t [label="0"];
E3 -> t [label="90"];
}
yields

How can I prevent graphviz from drawing edges over labels?

If I run graphviz on this digraph:
digraph G {
subgraph cluster_0 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
a0; a1; a2; a3;
label = "sources";
}
subgraph cluster_1 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
b0; b1; b2; b3;
label = "intermediaries";
}
a0 -> b0; a1 -> b0;
a0 -> b1; a1 -> b1;
a2 -> b2; b0 -> b2;
b1 -> b2; a3 -> b3;
b0 -> b3; b1 -> b3;
}
I get
with many edges intersecting the "intermediaries" label. How do I get graphviz to make edges avoid labels?
As a workaround:
digraph G {
subgraph cluster_0 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
a0; a1; a2; a3;
label = "sources";
}
subgraph cluster_1 {
style=filled;
color=lightgrey;
node [style=filled,color=white];
nodelabel [label="intermediaries"; style=filled; color=lightgrey]
nodelabel -> b1 [style=invis];
nodelabel -> b0 [style=invis];
b0; b1; b2; b3;
}
a0 -> b0; a1 -> b0;
a0 -> b1; a1 -> b1;
a2 -> b2; b0 -> b2;
b1 -> b2; a3 -> b3;
b0 -> b3; b1 -> b3;
}
produce:

Subgraph layout in graphviz

I have code to display two subgraphs:
graph {
rankdir=LR;
subgraph cluster01 {
label="t=0"
a0 [label="A"];
a1 [label="B"];
a2 [label="C"];
a5 [label="E"];
a0 -- a1;
a1 -- a2 ;
a2 -- a0;
};
subgraph cluster02
{
label="t=10"
b0 [label="A"];
b5 [label="E"];
b1 [label="B"];
b2 [label="C"];
b0 -- b1;
b2 -- b5;
};
a0--b0 [style=dotted];
a1--b1 [style=dotted];
a2--b2 [style=dotted];
a5--b5 [style=dotted];
}
This code displays two subgraphs like this:
But I want to have it like this:
I hope someone will help me fix the "rankdir" to get it done.
The following was achieved by using invisible edges and constraint=false on some edges:
graph {
rankdir=LR;
subgraph cluster01 {
label="t=0";
a0 [label="A"];
a1 [label="B"];
a2 [label="C"];
a5 [label="E"];
a0 -- a1;
a1 -- a2;
a2 -- a5 [style=invis];
a2 -- a0 [constraint=false];
};
subgraph cluster02
{
label="t=10"
b0 [label="A"];
b5 [label="E"];
b1 [label="B"];
b2 [label="C"];
b0 -- b1;
b1 -- b2 [style=invis];
b2 -- b5;
};
edge[constraint=false];
a0--b0 [style=dotted];
a1--b1 [style=dotted];
a2--b2 [style=dotted];
a5--b5 [style=dotted];
}

Resources