Increase gap between levels in Graphviz output - graphviz

Using the following command:
dot -Tpng so.dot -o so.png
the graphviz code shown produces the (650x255) image further down. I like that there are 3 distinct horizontal levels in the output image, but I'd prefer the difference in height between these 3 levels to be increased; perhaps producing a taller image. Can anyone help?
digraph G {
node [shape="circle"];
1 -> { 2; 3; 4 }
2 -> { 5; 6; 7 }
3 -> { 8; 9; 10 }
4 -> { 11; 12; 13 }
}

Try
dot -Tpng -Granksep=3.0 so.dot -o so.png
which gives
Documentation: http://soc.if.usp.br/manual/graphviz/html/info/attrs.html#a:ranksep

Related

Graphviz - how to set different layouts for each subgraph?

Graph is built by Graphviz:
graph {
layout=circo;
subgraph cluster_0 {
1 -- { 2 3 4 5 6 }
2 -- { 3 4 5 6 }
3 -- { 4 5 6 }
4 -- { 5 6 }
5 -- { 6 }
};
subgraph cluster_1 {
a -- b;
a -- c;
a -- d;
1 -- a;
};
}
This command is executed to save output as PNG file
dot -Tpng example.dot -o example.png
The result:
I would like to set different layouts for each subgraphs. For example, circo for cluster_0 and twopi for cluster_1. How can I do it?

How do i get all answers that lead to a specific output

So i am starting with Prolog and had an example task, where i have some points that form a maze and i want to know all the nodes that can lead to node 1.
connected(1,2).
connected(3,4).
connected(5,6).
connected(7,8).
connected(9,10).
connected(12,13).
connected(13,14).
connected(15,16).
connected(17,18).
connected(19,20).
connected(4,1).
connected(6,3).
connected(4,7).
connected(6,11).
connected(14,9).
connected(11,15).
connected(16,12).
connected(14,17).
connected(16,19).
reachable(X,Z) :- connected(X,Z).
reachable(X,Z) :- connected(X,Y),reachable(Y,Z).
I used the following query:
reachable(X,1).
which resulted in a simple false and not all nodes that lead to 1.
I on the other hand expected something like this:
x = 4;
It should be noted that this is a directed Graph.
If I execute the query reachable(X,1)., I get the expected result:
?- reachable(N,1).
N = 4 ;
N = 3 ;
N = 5 ;
N = 6 ;
false.
Which is what one expects based on the following graph:
generated with: dot -Tpng < conn.dot > conn.png and conn.dot:
digraph G {
1 -> 2;
3 -> 4;
5 -> 6;
7 -> 8;
9 -> 10;
12 -> 13;
13 -> 14;
15 -> 16;
17 -> 18;
19 -> 20;
4 -> 1;
6 -> 3;
4 -> 7;
6 -> 11;
14 -> 9;
11 -> 15;
16 -> 12;
14 -> 17;
16 -> 19;
}
Please check whether you have made a small typo or space error in your Prolog file.

Grid sweeps (traversal) represented with flat vector

I have a grid represented with a flat vector, that is:
-------------
| 6 | 7 | 8 |
-------------
| 3 | 4 | 5 |
-------------
| 0 | 1 | 2 |
-------------
I access the elements with the indices from 0 to grid.size()-1. I want to implement the Fast Sweeping Method. The main purpose of that method is that it does sweeps, that is, grid traversals in specific directions. For the 2D case:
Sweep 1: Right-top
for [row = 0 : nrows-1]
for [col = 0 : ncols-1] --> Result: 0 1 2 3 4 5 6 7 8
Sweep 2: Left-top
for [row = 0 : nrows-1]
for [col = ncols-1 : 0] --> Result: 2 1 0 5 4 3 8 7 6
Sweep 3: Right-bottom
for [row = nrows-1 : 0]
for [col = 0 : ncols-1] --> Result: 6 7 8 3 4 5 0 1 2
Sweep 4: Left-bottom
for [row = nrows-1 : 0]
for [col = ncols-1 : 0] --> Result: 8 7 6 5 4 3 2 1 0
And then computing idx = row*ncols + col.
This implementation is straightforward and its generalization to n dimensions as well, when just nesting for loops. However, I am working on a n-dimensional implementation and I am trying to generalize it in just 2 loops:
while (keepSweeping)
++sweep;
for (idx = init, idx == end, idx += inc)
// Traverse the grid
Computing init, end and inc is being really challenging. Also inc depends on ncols and changes dynamically. For instance, for sweep 2 inc = -1, but every ncols times inc = -1 + 2*ncols, so I achieve to go from 0 to 5.
Any help on how to do it? I am focusing firstly on the 2D case.
EDIT: I saw these threads http://www.cplusplus.com/forum/beginner/68434/ variable nested for loops that suggest to implement the loops recursively. Since I am looking for maximum performance, do you think that is a good idea?
Thank you!
Ok here is my try to answer your problem in the 2D case, using only one loop. Hopefully this is not too far from what you are looking for:
// ****** INITIALIZATION ******
int ncols = 4; // number of columns
int nrows = 3; // number of rows
boolean right = true; // direction of sweep on horizontal axis
boolean top = true; // direction of sweep on vertical axis
int counter = 0; // number of positions explored
if (right) {
colIterator = 0;
}
else {
colIterator = ncols - 1;
}
if (top) {
rowIterator = 0;
}
else {
rowIterator = nrows - 1;
}
// ****** CONTINUATION CONDITION ******
while (counter != nrows*ncols) {
// ****** DO SOMETHING ******
System.out.println(rowIterator*ncols + colIterator);
// ****** PROGRESSION PHASE ******
counter++;
// Have we completed a row?
if ((counter % ncols) == 0) {
if (top) {
// We have to move up
rowIterator++;
}
else {
// we have to move down
rowIterator--;
}
if (right) {
colIterator = 0;
}
else {
colIterator = ncols - 1;
}
}
else {
// We have not yet completed a row
if (right) {
// We have to move right
colIterator++;
}
else {
// or left
colIterator--;
}
}
}
Note: this code has been tested with Groovy.
A bit of upper-level explanation: it works with one loop because in 2D, we can find a global metric of the advancement of the work we want to do (here this metric is the counter variable) and can use the metric to determine, at each iteration of the loop, if we have completed a row (or not) by using the modulus operation.
I don't think it is mathematically possible to generalize this algorithm to upper dimensions (i.e. above 2) with only one loop, because there will be no mathematical operator that will tell us if we have finished part of the work on one given dimension and should start working on the outter dimensions (here, the modulus tells us that we have to modify the rowIterator because we have reached a border of the grid, but in dimension 3 or above 3, what would be the mathematical operator to use?).
Good luck and please post what you find, it's an interesting challenge.

Center last node of a graph

digraph {
1 -> 2 -> 3;
2 -> { rank = sink; end};
}
How can I force the "end" node to be centered horizontally (in the same column as the first node)?
You may use the group attribute of the nodes:
digraph {
3[group=b];
node[group=a]
1 -> 2 -> end;
2 -> 3;
}
See also this and this answer.

Graphviz: how to set 'default' arrow style?

Consider this dot language code:
digraph graphname {
subgraph clusterA {
node [shape=plaintext,style=filled];
1 -> 2 [arrowhead=normal,arrowtail=dot];
2 -> 3 -> X2 -> 5;
6;
7;
label = "A";
color=blue
}
}
In the above example, only the 1 -> 2 connection will have the arrowhead=normal,arrowtail=dot style applied; all the other arrows will be of the "default" style.
My question is - how do I set the arrow style (for the entire subgraph - or for the entire graph), without having to copy paste "[arrowhead=normal,arrowtail=dot];" next to each edge connection?
EDIT: Just for reference - the answer from Jesse didn't contain any code; I wrote that snippet and had it in this space here - for unknown reasons, a moderator cut it off from here and pasted it into Jesse's answer.
Use the edge attribute statement, as stated in the DOT Language documentation.
digraph graphname {
subgraph clusterA {
node [shape=plaintext,style=filled];
edge [arrowhead=normal,arrowtail=dot];
1 -> 2 ;
2 -> 3 -> X2 -> 5;
6;
7;
label = "A";
color=blue
}
}
Just like you did for nodes, but using edge, e.g. edge[style=dashed]

Resources