Control layout using graphviz twopi - graph

I am trying to draw a graph showing search prefixes using twopi. I have a simple input file and am getting this output:
alt text http://www.freeimagehosting.net/uploads/th.ac6cd51528.png
(full image)
Here is the input file:
digraph search {
// ordering=out;
// color=blue;
// rank=same;
// overlap=scale;
rankdir=LR;
root=root;
ranksep=1.25;
overlap=true;
"root";
a [color=none,fontsize=12];
b [color=none,fontsize=12];
c [color=none,fontsize=12];
d [color=none,fontsize=12];
e [color=none,fontsize=12];
f [color=none,fontsize=12];
#g [color=none,fontsize=12];
h [color=none,fontsize=12];
i [color=none,fontsize=12];
j [color=none,fontsize=12];
k [color=none,fontsize=12];
l [color=none,fontsize=12];
m [color=none,fontsize=12];
n [color=none,fontsize=12];
o [color=none,fontsize=12];
p [color=none,fontsize=12];
q [color=none,fontsize=12];
r [color=none,fontsize=12];
s [color=none,fontsize=12];
t [color=none,fontsize=12];
u [color=none,fontsize=12];
v [color=none,fontsize=12];
w [color=none,fontsize=12];
x [color=none,fontsize=12];
y [color=none,fontsize=12];
#ga [color=none,fontsize=12];
gb [color=none,fontsize=12];
gc [color=none,fontsize=12];
gd [color=none,fontsize=12];
ge [color=none,fontsize=12];
gf [color=none,fontsize=12];
gg [color=none,fontsize=12];
gh [color=none,fontsize=12];
gi [color=none,fontsize=12];
gj [color=none,fontsize=12];
gk [color=none,fontsize=12];
gl [color=none,fontsize=12];
gm [color=none,fontsize=12];
gn [color=none,fontsize=12];
go [color=none,fontsize=12];
gp [color=none,fontsize=12];
gq [color=none,fontsize=12];
gr [color=none,fontsize=12];
gs [color=none,fontsize=12];
gt [color=none,fontsize=12];
gu [color=none,fontsize=12];
gv [color=none,fontsize=12];
gw [color=none,fontsize=12];
gx [color=none,fontsize=12];
gy [color=none,fontsize=12];
gaa [color=none,fontsize=12];
gab [color=none,fontsize=12];
gac [color=none,fontsize=12];
gad [color=none,fontsize=12];
gae [color=none,fontsize=12];
gaf [color=none,fontsize=12];
gag [color=none,fontsize=12];
gah [color=none,fontsize=12];
gai [color=none,fontsize=12];
gaj [color=none,fontsize=12];
gak [color=none,fontsize=12];
gal [color=none,fontsize=12];
gam [color=none,fontsize=12];
gan [color=none,fontsize=12];
gao [color=none,fontsize=12];
gap [color=none,fontsize=12];
gaq [color=none,fontsize=12];
#gaz [color=none,fontsize=12];
gas [color=none,fontsize=12];
gat [color=none,fontsize=12];
gau [color=none,fontsize=12];
gav [color=none,fontsize=12];
gaw [color=none,fontsize=12];
gax [color=none,fontsize=12];
gay [color=none,fontsize=12];
gaza [color=none,fontsize=12];
gazb [color=none,fontsize=12];
gazc [color=none,fontsize=12];
gazd [color=none,fontsize=12];
gaze [color=none,fontsize=12];
#gazf [color=none,fontsize=12];
gazg [color=none,fontsize=12];
gazh [color=none,fontsize=12];
gazi [color=none,fontsize=12];
gazj [color=none,fontsize=12];
gazk [color=none,fontsize=12];
gazl [color=none,fontsize=12];
gazm [color=none,fontsize=12];
gazn [color=none,fontsize=12];
gazo [color=none,fontsize=12];
gazp [color=none,fontsize=12];
gazq [color=none,fontsize=12];
gazr [color=none,fontsize=12];
gazs [color=none,fontsize=12];
gazt [color=none,fontsize=12];
gazu [color=none,fontsize=12];
gazv [color=none,fontsize=12];
gazw [color=none,fontsize=12];
gazx [color=none,fontsize=12];
gazy [color=none,fontsize=12];
root -> a [minlen=2];
root -> b [minlen=2];
root -> c [minlen=2];
root -> d [minlen=2];
root -> e [minlen=2];
root -> f [minlen=2];
root -> g [minlen=2];
root -> h [minlen=2];
root -> i [minlen=2];
root -> j [minlen=2];
root -> k [minlen=2];
root -> l [minlen=2];
root -> m [minlen=2];
root -> n [minlen=2];
root -> o [minlen=2];
root -> p [minlen=2];
root -> q [minlen=2];
root -> r [minlen=2];
root -> s [minlen=20];
root -> t [minlen=2];
root -> u [minlen=2];
root -> v [minlen=2];
root -> w [minlen=2];
root -> x [minlen=2];
root -> y [minlen=2];
root -> 0 [minlen=2];
root -> 1 [minlen=2];
root -> 2 [minlen=2];
root -> 3 [minlen=2];
root -> 4 [minlen=2];
root -> 5 [minlen=2];
root -> 6 [minlen=2];
root -> 7 [minlen=2];
root -> 8 [minlen=2];
root -> 9 [minlen=2];
root -> "." [minlen=2];
g -> ga ;
g -> gb ;
g -> gc ;
g -> gd ;
g -> ge ;
g -> gf ;
g -> gg ;
g -> gh ;
g -> gi ;
g -> gj ;
g -> gk ;
g -> gl ;
g -> gm ;
g -> gn ;
g -> go ;
g -> gp ;
g -> gq ;
g -> gr ;
g -> gs ;
g -> gt ;
g -> gu ;
g -> gv ;
g -> gw ;
g -> gx ;
g -> gy ;
ga -> gaa ;
ga -> gab ;
ga -> gac ;
ga -> gad ;
ga -> gae ;
ga -> gaf ;
ga -> gag ;
ga -> gah ;
ga -> gai ;
ga -> gaj ;
ga -> gak ;
ga -> gal ;
ga -> gam ;
ga -> gan ;
ga -> gao ;
ga -> gap ;
ga -> gaq ;
ga -> gaz ;
ga -> gas ;
ga -> gat ;
ga -> gau ;
ga -> gav ;
ga -> gaw ;
ga -> gax ;
ga -> gay ;
gaz -> gaza ;
gaz -> gazb ;
gaz -> gazc ;
gaz -> gazd ;
gaz -> gaze ;
gaz -> gazf ;
gaz -> gazg ;
gaz -> gazh ;
gaz -> gazi ;
gaz -> gazj ;
gaz -> gazk ;
gaz -> gazl ;
gaz -> gazm ;
gaz -> gazn ;
gaz -> gazo ;
gaz -> gazp ;
gaz -> gazq ;
gaz -> gazr ;
gaz -> gazs ;
gaz -> gazt ;
gaz -> gazu ;
gaz -> gazv ;
gaz -> gazw ;
gaz -> gazx ;
gaz -> gazy ;
gazo -> "Blue Tuesday" ;
"Blue Tuesday" [ fontsize=10];
// Layout engines: circo dot fdp neato nop nop1 nop2 osage patchwork sfdp twopi
}
This output is generated with:
twopi -os1.png -Tpng s1.dot
I'm posting here because the printout is pretty dreadful. All of the nodes hung of "gaz" are overlapping; I've tried specifying nodesep and it is simply ignored. I would like to see the lines from root to the single letters further apart, but again, I can't control that.
This seems to be a bug in twopi. The documentation says it should clearly follow these directives, but it doesn't seem to.
My questions:
Is there any way to make twopi behave?
Failing that, is there a better layout engine to use?
Thanks.

I have found twopi to be the most difficult to work with of all the graphviz layout engines.
I made a few small changes to your original dot file. The rendered graph is shown below.
Here are the diffs:
graph attributes:
size="7.75,10.25"; // ensures that
entire graph fits on a single 8.5 x
11 page;
orientation="landscape";
ranksep=3.0; nodesep=2.0;
deleted: "root=root"; and
"overlap=true"
edge attributes:
i set the edge weight equal to 0.1
for all edges: [weight=0.1]
Reducing the edge weight by 90% for all edges has the net effect of increasing the edge length (or more precisely relaxing the constraint on edge length); obviously, this helps the nodes to "fan out". Doing this also increases the distance between "g" and "root", giving the child nodes of "root" more room.
I also tweaked a few purely aesthetic attributes--not having any effect on the layout, but that to me, seem to matter when you need to draw even a moderately dense graph on a single page:
graph attributes:
fontname="Helvetica";
fontsize=13
node attributes:
"root" [color="goldenrod4", shape="diamond", style="filled"]
"g" [color="lightsteelblue2", shape="diamond", style="filled"]
"ga" [color="darkorange", shape="diamond", style="filled"]
"gaz" [color="bisque4", shape="diamond", style="filled"]
edge attributes:
[arrowsize=0.4, penwidth=1.2]

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:

Remove line-crossing in Graphviz; layout different in jupyter and webgraphviz

I'm looking for a way to remove an unacceptable line crossing in a simple dot script. There is no line-crossing when the code is executed in http://www.webgraphviz.com/, but there is a line-crossing when the exact same code is executed in a Jupyter notebook. I'd be grateful for a suggestion of how to remove the line-crossing in Jupyter, that is, to make the layout in Jupyter look like the layout in webgraphviz.
Here is the dot code:
Digraph X {
rankdir=LR;
{rank=same; a b}
{rank=same; d c}
z -> a
a -> b
b -> c
c -> d
d -> a
}
In http://www.webgraphviz.com/, it displays as follows:
But, in Jupyter, it displays as follows, with an unacceptable line-crossing:
The jupyter code is the following:
import graphviz
graphviz.Source('''Digraph X {
rankdir=LR;
{rank=same; a b}
{rank=same; d c}
z -> a
a -> b
b -> c
c -> d
d -> a
}''')
This took some trial and error and piecing together clues from https://graphviz.gitlab.io/_pages/doc/info/attrs.html
import graphviz
graphviz.Source('''Digraph X {
rankdir=RL;
{rank=sink; z}
{rank=same; c d}
{rank=same; a b}
z -> a
a -> b
b -> c [constraint=false]
c -> d [constraint=false]
d -> a
}''')

2D map to graph in Sml

I have a 2D map as input from a file ,line by line and I want to save it and run a Bfs or Dijkstra to find the shortest path from Start (marked as S ) to end (marked as E),what's the proper structure to save the information? My implementation in c++ was with a graph,but I find it hard to do so in sml,because I can't link the nodes both ways.
You can do it using mutable arrays. This is an extremely minimal adjacency matrix implementation using an array of array of int option. The edge between vertex i and j is given by the value in the ith row and jth column. If the value is None then there is no edge and if the value is Some w than the edge exists and has weight w.
let is_some opt =
match opt with
| None -> false
| _ -> true;;
let unwrap opt =
match opt with
| None -> raise (Failure "unwrapped None")
| Some x -> x;;
let add_edge g u v w =
let us = Array.get g u in
Array.set us v (Some w);;
let get_edge g u v =
let us = Array.get g u in
Array.get us v;;
let make_graph vertices =
Array.make_matrix vertices vertices None;;
let neighbors g u =
Array.get g u |>
Array.to_list |>
List.filter is_some |>
List.mapi (fun i o -> (i, unwrap o));;
let g = make_graph 4;;
add_edge g 0 1 2;;
add_edge g 0 2 3;;
add_edge g 0 3 5;;
add_edge g 1 2 7;;
add_edge g 1 3 11;;
add_edge g 2 3 13;;
add_edge g 3 0 17;;
List.iter (fun (v, e) -> Printf.printf "0-(%i)->%i\n" e v) (neighbors g 0);;
This is in OCaml, but the differences between SML and OCaml are, for the most part, minor.

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

How to force position of edges in graphviz?

I'm having problems with edges overlapping each other. In my previous question of how to force the nodes to be in the same column, found out how to force the nodes in to one column, but this causes some other problems to appear.
digraph exmp {
A -> B -> C -> D -> E
C -> F [constraint=false]
A -> C [style="dotted", constraint=false]
A -> D [style="dotted", constraint=false]
B -> D [constraint=false]
D -> A [style="dashed", constraint=false]
C -> A [style="dashed", constraint=false]
E -> F [style="invis"]
F -> G
E -> C [constraint="false"]
}
Renders to:
alt text http://img98.imageshack.us/img98/8324/wrong2.gif
My problem is that the edges from E -> C and C -> F are starting/ending at the same point in the node C and the dashed and dotted edges are all on the right side of the nodes.
How could I tell specific edges to go to the right side of the node?
After a message in the graphviz mail list I've found that's at least possible to remove the E -> C and C -> F overlapping problem.
The simplest solution is to use a
compass point port:
C:e -> F [constraint=false]
-- Emden

Resources