How to specify in DOT file that edges go upwards - graph

I want to render a directed graph like:
A
^ ^
/ \
/ \
B C
But no matter what order I put the statements in, dot insists on generating an image that looks like:
B C
\ /
\ /
v v
A
I've tried specifying the port, but then the edges just wrap around. It doesn't change the position of the nodes.

The solution is rankdir
digraph G {
rankdir="BT"
B->A
C->A
}

rankdir is the solution if you want to build your whole graph from bottom to top. A more flexible approach is to just tell the edges to point backward:
digraph G
{
A -> B[ dir = back ];
A -> C[ dir = back ];
}
yields
You could also write
A -> { B C }[ dir = back ];
Or you could give the general instructions for all edges defined after this instruction to point backward:
edge[ dir = back ];
This can be undone by
edge[ dir = forw ];
Hence,
digraph G
{
edge[ dir = back ];
A -> B;
A -> C;
edge[ dir = forw ];
{ B C } -> D;
}
yields

Related

create fixed edge directional graphs in dot format with graphviz

Is there anyway to create defined fixed edges directional graphs using dot notation? the following dot notation (fig 2) generates an automated edges that are curved. It doesnt have notations which will generate directions with straight edges (fig 1). Been on hours trying to find anything close, any hints will be great. thank you.
# fig 1
box A --- box B
|. \
|. \
box C. \
box D
# fig 2
digraph G {
node [shape=record];
rankdir="BT"
a -> b [color = red][arrowhead = diamond][taillabel = "tail"]
b -> c [shape = box]
c -> a
}
Not sure what a fixed edge is, but if you don't want splines for edges, look at the splines attribute (https://graphviz.org/docs/attrs/splines/).
Here is you graph with splines=false. You can also try splines=polyline.
You might also connect the edges to specific ports on one or both of the nodes (https://graphviz.org/docs/attr-types/portPos/).
# fig 2
digraph G {
node [shape=record];
// see https://graphviz.org/docs/attrs/splines/
// also look at ports https://graphviz.org/docs/attr-types/portPos/
splines=false // or try splines=polyline
rankdir="BT"
a -> b [color = red][arrowhead = diamond][taillabel = "tail"]
b -> c [shape = box]
c -> a
}
Giving:

Ordering flowchart in graphviz

The graph I'd like to produce is a something like this a left-to-right flowchart with a main process at the top, and a series of groupings of stuff below that feed in and out at various points like this:
(though this is a dummy example and I want lots of stuff coming in and out from the top code box, which is why a horizontal layout works better than the default
The problem is that this is made in powerpoint...
I can get something close with this:
digraph example {
graph [
rankdir = LR
]
subgraph cluster_code {
label = "code";
A;
B;
C;
D;
}
subgraph cluster_data {
label = "data";
data_1;
data_2;
}
subgraph cluster_source {
label = "source"
source_1;
source_2
}
A -> B
B -> C
C -> D
data_1 -> A
data_2 -> B
A -> output_1
output_1 -> C
source_1 -> data_1
source_2 -> data_2
#{rank = same; source_2; data_2; A}
}
But if I try to bring the source and data clusters underneath the code cluster using {rank = same; source_2; data_2; A} (this is hashed out above, and I don't repeat the whole code for brevity of the post), I then A, data_2 and source_2 drop out of the box. I think this is something do do with rank and clusters not playing nicely together.
Any hints on getting something like the first graph above?
Am running graphviz via R/Rstudio and DiagrammeR.
It seems that you want to change "rankdir" in the middle of the graph. Quite reasonable, but Graphviz doesn't support it. Here is your graph, using default rankdir and the not-that-well-documented ability to effectively change rankdir by using rank=same in a subgraph. It also reverses edge arrowhead direction - a kludge, but it works.
digraph example {
node [width=1.5]
subgraph cluster_code {
label = "code";
{rank=same
A -> B -> C -> D
}
}
subgraph cluster_data {
label = "data";
data_1;
data_2;
}
subgraph cluster_source {
label = "source"
source_1;
source_2
}
A -> output_1
output_1 -> C
edge[dir=back minlen=2] // minlen makes (rank) space
A -> data_1 // -> A
A -> data_2 // -> A
edge[dir=back minlen=1]
data_1 -> source_1 // -> data_1
data_2 -> source_2 // -> data_2
}
Giving this:

How do I graph the Thompson's construction using graphviz?

I am trying to graph the construction of Thompson using graphviz, and I would like to know if anyone could help me graph one of the rules so that I can do the others.
I attach a reference image: https://en.wikipedia.org/wiki/Thompson%27s_construction#/media/File:Thompson-kleene-star.svg
digraph finite_state_machine {
rankdir=LR;
size="8,5"
node [shape = doublecircle]; s3;
node [shape = circle];
s0 -> s1 [ label = "ε" ];
s0 -> s3 [ label = "ε" ];
s1 -> s2 [ label = "ε" ];
s2 -> s1 [ label = "ε" ];
s2 -> s3 [ label = "ε" ];
}
Graphviz programs try to avoid placing nodes on top of other nodes. You can get the node placement by explicitly providing pos attributes for all the nodes. (Not that difficult, but a nuisance.) You can get neato to generate all straight edges, but you will have to provide the (spline) coordinates for all the arcs. Otherwise you get this:
As an alternative, instead of graphviz, if you use dpic or gpic, this program:
.PS
.defcolor pink rgb #FFC0CB
circlerad=circlerad*.8
## we need to place the large oval before we place nodes on it
Qx: circle invis ; line invis; circle invis; A: line invis;
ellipseht=ellipseht*2;
ellipsewid=ellipsewid*2
E:ellipse at A.c shaded "pink" " N(s)"
move to Qx.w
Q: circle "q" ; arrow "ε" ""; C1: circle ; A: line invis; C2: circle ; arrow "ε" ""; F: circle "f";
circlerad=circlerad*.8
F1:circle at last circle
move to E.n; up; P1: box invis "ε"
arc -> from C2.n to C1.n
arcrad=2
arc -> from Q.s to F.s
### gpic version of greek chars:
# move to E.s; down; box invis "" "\[*e]"
########################################
### dpic/svg version of greek chars
move to E.s; down; box invis "" "ε"
.PE
produced this:
gpic is part of the GNU (Linux) groff package.
dpic can be found here: https://ece.uwaterloo.ca/~aplevich/dpic/

Detect cycle in undirected graph in Ocaml

Does someone has idea how to detect if there is a cycle in undirected graph in OCaml?
Here's the type I'm using for graph:
type 'a graph = { nodes : 'a list; edges : ('a * 'a * int) list }
And for example, I would like to check if this graph contains cycles:
let graph = { nodes = ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'j';];
edges = [('c', 'j', 9); ('d', 'e', 8); ('a', 'b', 8); ('b', 'c', 7); ('f', 'g', 6); ('b', 'h', 4); ('a', 'd', 4); ('g', 'h', 2); ('b', 'f', 2); ('e', 'g', 1)]}
In both directed and undirected graphs, the presence of a cycle is detected using depth first search. Roughly, you traverse a graph and if a walk contains repetitive nodes, then there is a cycle.
Commonly, an additional data structure is employed for labeling already visited nodes. For example, we can employ a set data structure (using vanilla OCaml),
module Nodes = Set.Make(struct
type t = int
let compare = compare
end)
let dfs {edges} f x =
let rec loop visited x = function
| [] -> x
| (src,dst,data) :: rest ->
let x = f src data in
let visited = Nodes.add src visited in
if Nodes.mem dst visited
then loop visited x rest
else ... in
loop Nodes.empty x edges
You can also use an imperative hash table instead of a pure functional set. There is also an algorithm, called Iterative Deepening DFS, that can traverse cyclic graphs without labeling all visited nodes, which is useful when your graph is huge (and won't fit into the memory).
Unless you're doing this for an exercise, I would suggest you using some existing Graph library in OCaml, e.g., OCamlgraph (docs) or Graphlib (docs).
It is also possible to avoid visiting the same edge twice by removing it from the list of available edges; assuming order does not matter in among edges, you can remove an edge as follows:
let edges_remove_edge edges edge =
let (src, dst, _) = edge in
let rec iter edges res = match edges with
| [] -> res
| ((s, d, _) as e)::edges ->
if (s = src && d = dst) then
res # edges
else
iter edges (e::res)
in iter edges []
Removing an edge from a graph is then done by building a new graph that shares data with the previous graph, but with a modified list of edges:
let graph_remove_edge graph edge =
{ nodes = graph.nodes;
edges = edges_remove_edge graph.edges edge }
You can then transform the graph along the recursive calls of your graph traversal; the example does nothing interesting here, it is just to demonstrate the structure:
let choose_edge graph = match graph.edges with
| [] -> None
| e::_ -> Some e;;
let rec visit graph = match (choose_edge graph) with
| None -> graph
| Some e -> visit (graph_remove_edge graph e);;
# visit graph;;
- : char graph =
{nodes = ['a'; 'b'; 'c'; 'd'; 'e'; 'f'; 'g'; 'h'; 'j']; edges = []}
Or, you keep track of the current graph with a ref:
let visit2 graph =
let g = ref graph in
let rec v () = match (choose_edge !g) with
| None -> ()
| Some e -> begin g := graph_remove_edge !g e; v () end
in v(); !g
I managed to detect cycle by using union-find data structure.
A structure to represent a subset for union-find:
let create n =
{parent = Array.init n (fun i -> i);
rank = Array.init n (fun i -> 0)}
A utility function to find set of an element. It uses path compression technique:
let rec find uf i =
let pi = uf.parent.(i) in
if pi == i then
i
else begin
let ci = find uf pi in
uf.parent.(i) <- ci;
ci
end
A function that does union of two sets of x and y. It uses union by rank:
let union ({ parent = p; rank = r } as uf) x y =
let cx = find uf x in
let cy = find uf y in
if cx == cy then raise (Failure "Cycle detected") else begin
if r.(cx) > r.(cy) then
p.(cy) <- cx
else if r.(cx) < r.(cy) then
p.(cx) <- cy
else begin
r.(cx) <- r.(cx) + 1;
p.(cy) <- cx
end
end
I created function for checking if there is a cycle.
let thereIsCycle c1 c2 g subset =
let isCycle = try Some (union subset (findIndex c1 g.nodes) (findIndex c2 g.nodes)) with _ -> None in
match isCycle with
| Some isCycle -> false
| None -> true
let rec findIndex x lst =
match lst with
| [] -> raise (Failure "Not Found")
| h :: t -> if x = h then 0 else 1 + findIndex x t

create a register

I have to create a type tree which would be used to store words, like every node of the tree would hold a letter and the list of the next characters (so words with the same root would share the same "part/branch of the tree). the tree is basically a n-ary one, used as a dictionnary.
All using Caml language
Well, I don't know if it's a homework or not but I'll still answer :
First, we need to define a signature type for letters.
module type LS = sig
type t
val compare : t -> t -> int
end
Then, we need to define our structure :
module Make (L : LS) = struct
module M = Map.Make(L)
type elt = L.t list
type t = { word : bool; branches : t M.t }
let empty = { word = false; branches = M.empty }
let is_empty t = not t.word && M.is_empty t.branches
let rec mem x t =
match x with
| [] -> t.word
| c :: cl -> try mem cl (M.find c t.branches)
with Not_found -> false
let rec add x t =
match x with
| [] -> if t.word then t else { t with word = true }
| c :: cl ->
let b = try M.find c t.branches with Not_found -> empty in
{ t with branches = M.add c (add cl b) t.branches }
end
Now, step by step :
module Make (L : LS) = struct is a functor that will return a new module if we give it a module of type LS as an argument
module M = Map.Make(L)
type elt = L.t list
type t = { word : bool; branches : t M.t }
This is the complex point, once you have it, everything begins clear. We need to represent a node (as you can see in the Wikipedia page of tries). My representation is this : a node is
a truth value stating that this node represent a word (which means that all the letters from the root to this node form a word)
the branches that goes from it. To represent this branches, I need a dictionary and luckily there's a Map functor in OCaml. So, my field branches is a field associating to some letters a trie (which is why I wrote that branches : t M.t). An element is then a list of letters and you'll find out why I chose this type rather than a string.
let empty = { word = false; branches = M.empty } the empty trie is the record with no branches (so, just the root), and this root is not a word (so word = false) (same idea for is_empty)
let rec mem x t =
match x with
| [] -> t.word
| c :: cl -> try mem cl (M.find c t.branches)
with Not_found -> false
Here it becomes interesting. My word being a list of letters, if I want to know if a word is in my trie, I need to make a recursive functions going through this list.
If I reached the point where my list is empty it means that I reached a node where the path from the root to it is composed by all the letters of my word. I just need to know, then, if the value word at this node is true or false.
If I still have at least one letter I need to find the branch corresponding to this letter.
If I find this branch (which will be a trie), I just need to make a recursive call to find the rest of the word (cl) in it
If I don't find it I know that my word doesn't exist in my trie so I can return false.
let rec add x t =
match x with
| [] -> if t.word then t else { t with word = true }
| c :: cl ->
let b = try M.find c t.branches with Not_found -> empty in
{ t with branches = M.add c (add cl b) t.branches }
Same idea. If I want to add a word :
If my list is empty it means that I added all the letters and I've reached the node corresponding to my word. In that case, if word is already true it means that this word was already added, I don't do anything. If word is false I just return the same branch (trie) but with word equal to true.
If my list contains at least a letter c, I find in the current node the branch corresponding to it (try M.find c t.branches with Not_found -> empty) and I there's no such branch, I just return an empty one and then I recursively add the rest of my letters to this branch and add this new branch to the branches of my current node associated to the letter c (if this branch already existed, it will be replaced since I use a dictionary)
Here, we start with the empty trie and we add the word to, top and tea.
In case we don't want to use functors, we can do it this way :
type elt = char list
type t = { word : bool; branches : (char * t) list }
let empty = { word = false; branches = [] }
let is_empty t = not t.word && t.branches = []
let find c l =
let rec aux = function
| [] -> raise Not_found
| (c', t) :: tl when c' = c -> t
| _ :: tl -> aux tl
in aux l
let rec mem x t =
match x with
| [] -> t.word
| c :: cl -> try mem cl (find c t.branches)
with Not_found -> false
let rec add x t =
match x with
| [] -> if t.word then t else { t with word = true }
| c :: cl ->
let b = try find c t.branches with Not_found -> empty in
{ t with branches = (c, (add cl b)) :: t.branches }

Resources