graphviz/dot graph: Order nodes within a cluster/a subgraph - graph

Help sort nodes within subgraphs or clusters such as subgraph A and B below.
graph {
splines=line;
subgraph cluster_0 {
label="Subgraph A";
a; b; c
}
subgraph cluster_1 {
label="Subgraph B";
d; e;
}
a -- e;
a -- d;
b -- d;
b -- e;
c -- d;
c -- e;
}
This graph is not desirable. Help us sort "a", "b", "c" and "d", and "e" from left-to-right, please.
https://graphs.grevian.org/resources/static/images/example6a.png

I found this difficult to do, and don't guarantee that it works with a more complex graph. In addition to getting the nodes in the desired sequence, it is quite difficult to position clusters where you want. That said, this:
graph {
splines=line;
rankdir=LR
subgraph cluster_0 {
label="Subgraph A";
rank=same
a -- b -- c [style=invis]
}
subgraph cluster_1 {
label="Subgraph B";
rank=same
d -- e [style=invis]
}
edge[constraint=false]
a -- e;
a -- d;
b -- d;
b -- e;
c -- d;
c -- e;
}
produces this:

Related

How can I define which node goes next to another one in grViz

I am trying to plot a regular graph with grViz in R like the below:
using the following code:
grViz("
graph {
# a graph statement
graph [layout = circo]
# a node statement
node [shape = circle,
style = filled,
color = grey,
fillcolor = orange];
nodesep=4
A; B; C; D; E; F; G; H;
# an edge statement
edge [color = grey]
A -- B
B -- C
C -- D
D -- E
E -- F
F -- G
G -- H
H -- A
A -- D
H -- E
B -- G
C -- F
}")
However, I cannot place the nodes in sequential order despite trying the different options for rankdir:

How to draw diagonal edges in Graphviz

How can I draw diagonal lines between two nodes in Graphviz (Dot Language).
I want to draw the following shape.
I tried:
graph G {
V[pos="0,0"];
N[pos="40,40!"];
V -- N [label="NSUBJ"]
}
But it draws orthogonal lines. besides I want to draw multiple subgraphs with an order number as above!
It's possible to use pos attribute, but in that case you'd have to use neato and set pos for every node:
graph G {
V [pos = "0,100"]
N [pos = "50,0"]
S [pos = "200,100"]
VPC [pos = "150,0"]
NPC [pos = "250,0"]
V -- N [label="NSUBJ"]
S -- VPC
S -- NPC
}
Then use the following:
neato -n -Tpng -o test.png test.gv
The output is the following:
Also it's possible to make lines diagonal if use invisible nodes:
graph G {
node [shape=plaintext]
labelloc="b"
pencolor=transparent
subgraph cluster_1 {
label="(1)"
S_1 [label="S"]
VPC_1 [label="VPC"]
NPC_1 [label="NPC"]
V_1 [label="V"]
invis_1 [style=invis]
N_1 [label="N"]
V_1 -- invis_1 [style=invis]
V_1 -- N_1 [label="NSUBJ"]
S_1 -- VPC_1
S_1 -- NPC_1
}
subgraph cluster_2 {
label="(2)"
VPC_2 [label="VPC"]
V_2_2 [label="V"]
NPC_2 [label="NPC"]
V_2 [label="V"]
invis_2 [style=invis]
N_2 [label="N"]
V_2 -- invis_2 [style=invis]
V_2 -- N_2 [label="COPCOMP"]
VPC_2 -- V_2_2
VPC_2 -- NPC_2
}
}
Running this sequence of commands produces the following output:
ccomps -Cx test.gv | dot | gvpack -array_1 | neato -n2 -Tpng > test.png

Creating an independent set from a graph in Alloy 4.2

I've created a test graph to try and create an independent set from. I know and independent set is a set of vertices that aren't connected, but I'm not sure how to accomplish this in alloy 4.2. Here's what I have:
abstract sig Vertex {
e: set Vertex -- e is the edge relation
}
-- the test graph has vertices A, B, C, D, E, F
one sig A, B, C, D, E, F extends Vertex { }
pred independentSet[e: Vertex->Vertex, s: set Vertex] {
--code here?
}
pred show {
-- setting up the edge relation
(A->A + A->B + A->C + A->D) +
(B->A + B->B + B->C + B->E) +
(C->A + C->B + C->C + C->F) = e
}
run show for 6
all i, j: s | not i -> j in e
That's the answer I put, I think it's sort of correct and it gives an instance. I think it's missing a point but am not sure how to fully express it. I hope it gives you an idea!
no e is shorter. (But too short for a comment, so this is a filler.)

How to put a node in the center of the dot-generated graph

With the following dot code
digraph DG {
G -> V;
G -> E;
G -> P;
G -> C;
}
I generate the following graph
How could I move the node G in the centre? That is I wish to get something like this:
p.s. My experiments with setting the rank of the edge didn't work out.
For the general case, the easiest thing to do is to use twopi or neato instead of dot as your layout engine.
Twopi:
Neato:
If you're truly confined to dot, this will give you close to what you want, though you'll have to customize each graph.
digraph g
{
P -> G [dir=back];
subgraph clusterGVE {
{rank=same V; G; E;}
G -> V [constraint=false];
G -> E;
color=invis;
};
G -> C;
}

Newbie graphviz edge direction issue

I have an undirected graph with the following schema:
graph G {
A -- B;
B -- C;
C -- D;
D -- A;
}
The graph is top to bottom as you see yourself. But I wanted to be in the form of a rectangle where A is top-left and D is bottom-right and I don't really have a clue how to do it.
graph G {
A [
label = A
pos = "0,0!"
]
B [
label = B
pos = "5,0!"
]
C [
label = C
pos = "5,5!"
]
D [
label = D
pos = "0,5!"
]
A -- B;
B -- C;
C -- D;
D -- A;
}
and use neato to generate the image file. (it did not work with dot for me)
I have got another solution without neato I guess that renders with dot properly.
graph G{
{rank=same A B}
{rank=same C D}
A -- B;
B -- C;
D -- A;
D -- C;
}

Resources