# 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:

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

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.

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:

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:

A directed graph has the form of:

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:

Put that into a file named: ‘graph.dot’, run the command: ```dot -Tpng graph.dot -o graph.png``` and the result will be: 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:

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: 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:

now the graph will look like: # 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!