# Dot and Graphviz, Tools for drawing graphs

I came across the tutorial in Chinese [1], and reread the English original version [2]. Here I only present a short but practical note about it, which might be used in future. The original tutorials are detailed for reference.

Download Graphviz (if you’re in mainland of China, VPN might be necessary). New file “graph1.gv”, and start the examples.

Type in:

```graph graphExample1 {
a -- b;
b -- c;
b -- d;
d -- a;
}
```

Press the “Layout” button if you use the Windows version. It shows:

#### Here are some other examples:

Sometimes you need a different kind of layout.

```graph graphExample2 {
rankdir = LR;	//Rank Direction; and this is a comment
a -- b;
b -- c;
b -- d;
d -- a;
}
```

Sometimes you may need a directed graph. Notice that the label of a node is the same as the id by default.

```digraph graphExample3 {
Teacher -> b;
//b -> c;	//Yes, this is still a comment
Teacher -> c;
b -> Teacher;
}
```

Need some colors?

```digraph graphExample4 {
T [label = "Teacher"];
P [label = "b", fontcolor = red];	//Notice the ID is P
T -> b;			//Notice the ID is b
T -> Pupil [label = "Instructions", fontcolor = darkgreen];
b -> T;
}
```

Shapes, fontsize and background colors can also be self-defined.

```digraph graphExample5 {
T [label = "Teacher", color = blue, fontsize = 24, shape = box];
P [label = "b", fontcolor = red, shape = triangle, style = filled, fillcolor = yellow];
T -> b;
T -> Pupil [label = "Instructions", fontcolor = darkgreen, style = filled, fillcolor = pink];
b -> T;
}
```

More parameters can be found here: http://www.graphviz.org/content/node-shapes

#### Here are more advanced examples.

Nodes and edges’ styles can be defined at one time.

```digraph graphExample6 {
nodesep = 1.5;	//The distance between nodes
node [ color = blue, fontname = Courier, fontsize = 24, shape = box];	//Define all nodes
edge [ color = purple, fontcolor = darkgreen, style = dashed, fillcolor = pink];	//Define all edges

T [label = "Teacher"];
P [label = "b", fontcolor = red, shape = triangle, style = filled, fillcolor = yellow];
T -> {b, Pupil};
b -> T;

{rank = same; P b Pupil}	//Put on the same level
}
```

Each node can contain a record table.

```digraph graphExample7 {
node [color = blue, fontname = Courier, shape = record];
edge [color = purple, fontcolor = darkgreen, style = dashed, fillcolor = pink];

node1 [label = " left |  spa\ ce |  right"];
node2 [label = "{ one |  two \n\n\n multiline}", shape = Mrecord];
node3 [label = "hello\nworld | {b | {c |  d | e} | f} | g | h"];

node1:f1 -> node2:f0;
node1:f0 -> node3:f1;	//f0, f1, ... are signed automatically
node3:here -> node1:f2;
}
```

Using example 6 and 7, it’s easy to draw Finite State Machine and Data Flow Diagrams [2].

Subgraph entity is also supported. Here I use the digraph dfd2 in [2] as an example.

```digraph graphExample8 {
node[shape=record]
subgraph level0{
enti1 [label="Customer" shape=box];
enti2 [label="Manager" shape=box];
}
subgraph cluster_level1{
label ="Level 1";
proc1 [label="{ 1.0| One process here\n\n\n}" shape=Mrecord];
proc2 [label="{ 2.0| Other process here\n\n\n}" shape=Mrecord];
store1 [label="    | Data store one"];
store2 [label="   | Data store two"];
{rank=same; store1, store2}

}
enti1 -> proc1
enti2 -> proc2
store1 -> proc1
store2 -> proc2
proc1 -> store2
store2 -> proc1
}
```

The full list of attributes can be found in [3]. More examples can be found in [4] and [5].

### Reference

[1] 使用DOT语言和Graphviz绘图, http://casatwy.com/shi-yong-dotyu-yan-he-graphvizhui-tu-fan-yi.html

[2] Tony Ballantyne, “Drawing Graphs using Dot and Graphviz,” http://www.tonyballantyne.com/graphs.html

[3] Full list of attributes, http://graphviz.org/doc/info/attrs.html

[4] GraphViz for discrete math students, http://graphs.grevian.org

[5] An Introduction to GraphViz and dot, http://www.linuxdevcenter.com/…/graphviz_dot.html