Red Green Repeat Adventures of a Spec Driven Junkie

Graphviz: A Quick Intro

I’m getting back into algorithms and started to work on Minimum Spanning Tree algorithm again. I was looking for some sample data to test and found a forum post with test data that was just:

A B 1
A C 2
B D 3
C E 4
output: 6

Which is great to have sample data and correct output, but I couldn’t see the result right away. I wanted to have it graphed and the easiest way to graph is to use: GraphViz

I found the Graphviz documentation a bit… rough. Graphviz is a huge program that will graph anything, but I just wanted to graph a simple list!

Install GraphViz

Goto Graphviz download page and follow the directions for your system.

Note, although the package ‘graphviz’ is installed, the main program used in this document is dot, a command line program in Graphviz.

Installation check

Typing dot at the command prompt should not give any output and wait for input.

[email protected]:~$ dot
_

Also, on unix based systems, man dot will produce the man page for dot.

Now that Graphviz is installed, let’s start graphing!

Generating Graphs

The easiest way to create PNG graphs in Graphviz is to use the following command:

dot -Tpng <dot file> -o graph.png

Each part of the command explained:

  • dot: although the software is called ‘graphviz’, the command is ‘dot’, as in ‘dot’ interpreter, which is the language to write Graphviz files
  • -Tpng: set the output type to PNG
  • <dot file>: the input file, in the dot language
  • -o graph.png: the output flag and the file name of the output

Basic Graphing in Dot

Graphviz is an extensive graphing system. For me, I just want to graph simple lists with weights. I will go over how to create these types of graphs:

  • undirected graph
  • directed graph
  • weighted graph

Each are very similar to each other and can be combined.

Syntax

Dot syntax is very simple. There are basic mark up to describe the type of graph, nodes, line ending, and edges.

Graph Type

To describe the graph type, it is the first word in a dot file. Graphs covered in this document are undirected or directed graphs .

An undirected graph has the form of:

graph {...}

A directed graph has the form of:

digraph {...}

The form of the graph will determine the edge mark up to be used.

Nodes

Nodes can be represented by any letter, number, or word in dot. So, ‘A’, ‘b’, ‘1’, ‘node’ are valid node names.

Edges

Edges are represented by a connector between nodes. Edges can be ‘–’ for undirected or ‘->’ or a directed’

Line Ending

All statements in dot need to have an ending of semi-colon. ‘;’

Undirected Graph

In dot, this is a basic undirected graph:

graph {
  A -- B;
  A -- C;
  B -- D;
  C -- E;
}

Put that into a file named: ‘graph.dot’, run the command: dot -Tpng graph.dot -o graph.png and the result will be:

graph

Key parts:

  • “graph {“: which tells dot to generate an undirected graph
  • ”–”: which tells dot the edges between each node are undirected.

Easy, peasy.

Directed Graph

In dot, this is a basic directed graph:

digraph {
  A -> B;
  A -> C;
  B -> D;
  C -> E;
}

If this was in a file named: ‘directed_graph.dot’, graphing it in dot would us the command: dot -Tpng directed_graph.dot -o graph.png and the result will be:

directed graph

This is very similar to the undirected graph, key parts:

  • “digraph {“: which tells dot to generate a directed graph
  • ”->”: which tells dot the edges between each node are directed.

Weights

In dot, to include weights, just add a label to the edge in the form of:

<source node> -- <destination node> [label="<value>"]

so, for the above directed graph, including all the weights:

digraph {
  A -> B [label="1"];
  A -> C [label="2"];
  B -> D [label="3"];
  C -> E [label="4"];
}

now the graph will look like:

weighted directed graph

Script

As simple as dot is, creating dot files by hand from a list of nodes will become tedious, so I have a script for it, which can be found here:

https://github.com/a-leung/list2dot

Conclusion

Once basic primitives of dot are understood, making graphs in Graphviz is a great way to visualize graphs being worked on. Now with a basic understanding, I will not hesitate to draw a graph to see what is going on, especially for Minimum Spanning Trees!