Showing Arbitrary Graph Nodes and Edges for a Network in GraphViz - graph

I'd like to draw a network, as a fully connected directed graph, with arbitrary number of nodes and edges as shown in the pic.
What would be the easy way to draw this with GraphViz?

graphviz gallery has a very similar entry, with fancy graphic: it's as easy as replacing the names start, etc... with your own in the generating script. Feed it to dot program.
digraph G {bgcolor="red:cyan" gradientangle=0
subgraph cluster_0 {
style=filled;
color=lightgrey;
fillcolor="blue:yellow";
gradientangle=90;
node [fillcolor="yellow:green" style=filled gradientangle=270] a0;
node [fillcolor="green:red"] a1;
node [fillcolor="red:cyan"] a2;
node [fillcolor="cyan:blue"] a3;
a0 -> a1 -> a2 -> a3;
label = "process #1";
}
subgraph cluster_1 {
node [fillcolor="yellow:magenta"
style=filled gradientangle=270] b0;
node [fillcolor="magenta:cyan"] b1;
node [fillcolor="cyan:red"] b2;
node [fillcolor="red:blue"] b3;
b0 -> b1 -> b2 -> b3;
label = "process #2";
color=blue
fillcolor="blue:yellow";
style=filled;
gradientangle=90;
}
start -> a0;
start -> b0;
a1 -> b3;
b2 -> a3;
a3 -> a0;
a3 -> end;
b3 -> end;
start [shape=Mdiamond ,
fillcolor="yellow:brown",
gradientangle=90,
style=radial];
end [shape=Msquare,
fillcolor="orange:blue",
style=radial,
gradientangle=90];
}

Related

Orthogonal edges in ER diagram

I'm trying to make an Entity-relationship diagram using the Graphviz DOT language. In my model I have a self-referencing relationship and here I would like the edges to be orthogonal, i.e. consist only of vertical and horizontal lines. Currently I have this diagram:
digraph G {
edge [arrowhead = none]
Entity [shape = box]
Relationship [shape = diamond]
Entity -> Relationship [headport = w, tailport = w]
Relationship -> Entity [headport = e, tailport = e]
}
I tried adding graph [splines = ortho] but then the diagram renders like this:
Any ideas? I'm using version 2.43.0 of DOT.
The ortho implementation is rather flawed, Here is a DIY version. It adds invisible point-shaped nodes to each side of the two ER nodes and then draws edges to connect everything to look "ortho".
digraph G {
splines=false
edge [arrowhead = none]
{ rank=same
Entity [shape = box]
node [shape=point label="" style=invis]
x1 [group=G1]
x2 [group=g2]
x1 -> Entity [tailclip=false]
Entity -> x2 [headclip=false]
}
{ rank=same
Relationship [shape = diamond]
node [shape=point label="" style=invis]
y1 [group=G1]
y2 [group=g2]
y1 -> Relationship [tailclip=false]
Relationship -> y2 [headclip=false]
}
x1 -> y1 [headclip=false tailclip=false]
x2 -> y2 [headclip=false tailclip=false]
}
Giving:

Graphviz Draw graph nodes using BFS order

I've a graph file (.dot) with nodes that has depth and size "properties". I want to use graphviz to draw this graph considering "depth" as the depth of a Breadth First Search and the size property as a relative size of the node.
I've tried using twopi and setting all nodes that have the same depth on the same rank, but with no success. Any suggestion on how I can accomplish this?
Sample graph:
strict digraph {
root = 0;
ranksep = 5;
{rank=same;
0
14754221888142813049
}
{rank=same;
17110173155996056797
8860119836345733269
12488214955576990298
5164430016200693425
6534280015544966791
16035081712171710670
7728255721346380016
3065692608114503807
15537038826989012875
15464545769657918305
7964210557574239786
17237485240931079100
9667703542183627069
9063621412962345275
4956992700610885217
703599774521196210
}
0 [label="A"];
14754221888142813049 [label="B"];
17110173155996056797 [label="C"];
8860119836345733269 [label="D"];
12488214955576990298 [label="E"];
5164430016200693425 [label="F"];
6534280015544966791 [label="G"];
16035081712171710670 [label="H"];
7728255721346380016 [label="I"];
3065692608114503807 [label="J"];
15537038826989012875 [label="K"];
15464545769657918305 [label="L"];
7964210557574239786 [label="M"];
17237485240931079100 [label="N"];
9667703542183627069 [label="O"];
9063621412962345275 [label="P"];
4956992700610885217 [label="Q"];
703599774521196210 [label="R"];
0 -> 14754221888142813049
0 -> 12488214955576990298
0 -> 5164430016200693425 [weight=0, constraint=false];
0 -> 15537038826989012875
0 -> 7964210557574239786
0 -> 9667703542183627069
0 -> 17237485240931079100
5164430016200693425 -> 8860119836345733269 [weight=0, constraint=false];
8860119836345733269 -> 4956992700610885217
8860119836345733269 -> 9063621412962345275
8860119836345733269 -> 703599774521196210
8860119836345733269 -> 17110173155996056797
8860119836345733269 -> 6534280015544966791
8860119836345733269 -> 16035081712171710670
8860119836345733269 -> 7728255721346380016
8860119836345733269 -> 3065692608114503807
8860119836345733269 -> 15464545769657918305
}
You are trying to capture hierarchical structure so you want to use the dot layout. By default, if you have an edge a->b, node b will be 1 level below node a. If you have a tree, and make sure the node of depth 0 appears first, you'll get the layout you want by default. If your graph is more complex, the edge criteria mentioned, or cycle breaking, may interfere with what you want. In that case, you need to make the depth constraints more explicit and turn off other edge constraints. There are several ways to do this.
Suppose you have the following graph where the numbers in the node names indicate the depth.
digraph {
0 -> a1
0 -> b1
a1 -> a2
a1 -> b2
b1 -> c2
b1 -> d2
a2 -> d2
a2 -> b1
}
A simple way to get what you want is to do a BFS, and set any edge not part of the BFS tree to have constraint=false.
digraph {
0 -> a1
0 -> b1
a1 -> a2
a1 -> b2
b1 -> c2
b1 -> d2
a2 -> d2 [constraint=false]
a2 -> b1 [constraint=false]
}
Or you can use rank=same to make sure nodes are placed on ranks corresponding to their depths, again turning off edges that conflict. (This assumes you have at least one chain of edges going from depth 0 to the bottom. If not, you can introduce an invisible chain of dummy nodes and edges satisfying this, and adding each dummy node to its rank=same subgraph.}
digraph {
0 -> a1
0 -> b1
a1 -> a2
a1 -> b2
b1 -> c2
b1 -> d2
a2 -> d2 [constraint=false]
a2 -> b1 [constraint=false]
{rank=same a1 b1}
{rank=same a2 b2 c2 d2}
}
This allows the depth function to not be actually tied to a BFS tree.
By the way, twopi should have worked, but with the layout center out instead of top down, as its levels are based on a BFS from a root node. This assumes you set the root attribute and, as you have an explicit BFS in mind, turning off non-tree edges by setting weight=0.
digraph {
root=0
0 -> a1
0 -> b1
a1 -> a2
a1 -> b2
b1 -> c2
b1 -> d2
a2 -> d2 [weight=0]
a2 -> b1 [weight=0]
}

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;
}

Make boxes of the same width

I'm trying to make a flowchart. In this flowchart, I want to have boxed nodes with varying labels (and thus varying label sizes), but I want the nodes to have the same size.
The source code is the following:
digraph G {
a0 [ label="this is a loooong label" ]
a3 [ label="short label" ]
b0 [ label="this is a long label" ]
b3 [ label="short label" ]
a0, a1, a2, a3 [ shape=box ]
b0, b1, b2, b3 [ shape=box ]
subgraph cluster_one {
style=filled;
color=lightgrey;
a0 -> a1 -> a2 -> a3;
label = "This is nice and grey";
}
subgraph cluster_two {
b0 -> b1 -> b2 -> b3;
label = "This is nice and white";
color=blue;
}
}
I get this image:
I want the boxes in the subgraphs to have the same width. How can I do this?
Thank you in advance :)
You can set default values for nodes via node [width=5]. this sets the minimum size. If you set fixed-size=true this will be the actual size.
You can define a global node style, like this:
digraph G {
node[width=4]
...

Drawing reflexive edges in State Machines

I have to draw a small finite state machine that has some reflexive transitions (meaning the start and the end state of the transition are equal.
The problem is that rendering that in Graphviz has ugly results.
digraph finite_state_machine {
edge [fontsize=11];
S0 -> S0 [label = "td=1\n-/e2"];
S0 -> S1 [label = "td=3 \n-/e3" ];
S1 -> S0 [label = "td=3\n-/-\nt=0"];
S0 -> S2 [label = "P:i1/e4"];
S2 -> S0 [label = "td=0\n-/-" ];
S0 -> S0 [label = "i1/e1\ntd+=1"];
}
Is there a way to make this look a little better?
BTW: I tried head/tailport but they don't work on my version of Graphviz (1.13 on Mac OS X)
I am not limited to the dot engine, I only want a nice looking graph and don't care about the renderer/language.
Thanks a lot
So, if found a workaround, but not really an answer to my problem.
The trick is to have an invisible node that connections to the starting state. the starting state is then not the top of the hierarchy and one has a little bit more freedom in placing the nodes. Also then the head/tailport attributes work as they should.
The result is - if not a pretty as I would like it to be - ok to look at.
digraph finite_state_machine {
edge [fontsize=7];
fontsize = 11;
rankdir=LR;
{rank = same;null}
{rank = same; S0}
{rank = same; S1 S2}
nodesep = 1;
ranksep = 1;
null [shape = plaintext label=""];
null -> S0;
S0 -> S0 [label = "td=1\n-/e2", tailport = n, headport = n];
S0 -> S1 [label = "td=3 \n-/e3" ];
S1 -> S0 [label = "td=3\n-/-\nt=0"];
S0 -> S2 [label = "P:i1/e4"];
S2 -> S0 [label = "td=0\n-/-" ];
S0 -> S0 [label = "i1/e1\ntd+=1" headport = s tailport = s];
}
a rendering of the state machine http://img532.imageshack.us/img532/4083/previewd.png
While this works (for this particular example) I would still very much like some advice on dot/Graphviz or an alternative for rendering finite state machines in a pleasing way.

Resources