An Introduction to Graphviz

What is Graphviz and how to use it?

by Mihalis Tsoukalos

Introduction

This article presents GraphViz, a very flexible and handy tool that is freely available under an open source license. Graphviz helps you draw, illustrate and present graph structures. Do not be discouraged and please do not think that "drawing graph structures" looks restrictive and limiting - I can promise you that by the end of the article, you will have changed your mind.

The good thing is that Graphviz algorithmically arranges the graph nodes so that the output is both practical and appealing!

The article focuses on using GraphViz from the command line but it also presents the PixelGlow Graphviz version (an application with a GUI) that is exclusively designed for Macs. It also presents Omnigraffle that can also render Graphviz files.

Graphviz can be used in domains such as software engineering, networking, bioinformatics, databases, web structures and knowledge representation. The central part of Graphviz consists of implementations of algorithms for graph layout. Most Graphviz is written in C.

Graphviz in a nutshell

GraphViz (or Graphviz or graphviz) is a collection of tools for manipulating graph structures and generating graph layouts. Graphviz supports either directed or undirected graphs. GraphViz offers both graphical and command-line tools. A Perl to Graphviz interface library is also available, but it is not covered here for reasons of generality. There is also a C++ interface.

Strictly speaking and according to the "The Design and Analysis of Computer Algorithms" book, a graph G=(V, E) consists of a finite and nonempty set of vertices V and a set of edges E. If the edges are ordered pairs of vertices, then the graph is said to be directed. If the edges are unordered pairs, the graph is said to be undirected.

Although it may look strange, the fact is that you can draw remarkable illustrations using Graphviz! Figure 1 demonstrates such a draw - and you did not even have to draw a line!

Graphviz has its own dialect that you will have to learn. The language is simple, elegant and powerful. The good thing about Graphviz is that you can write its code using a simple plain text editor - a side effect of it is that you can easily write scripts that generate Graphviz code. In fact, this article has such a script that is written in Perl - my favorite scripting language.

GraphViz is comprised of the following programs and libraries:

The dot program: a utility program for drawing directed graphs. It accepts input in the dot language. The dot language can define three kinds of objects: graphs, nodes and edges. dot uses a Sugiyama-style hierarchical layout.

The NEATO program: a utility program for drawing undirected graphs. This kind of graph commonly is used for telecommunications and computer programming tasks. NEATO uses an implementation of the Kamada-Kawai algorithm for symmetric layouts.

The twopi program: a utility program for drawing graphs using a circular layout. One node is chosen as the center, and the other nodes are placed around the center in a circular pattern. If a node is connected to the center node, it is placed at distance 1. If a node is connected to a node directly connected to the center node, it is placed at distance 2 and so on.

dotty, tcldot and lefty: three graphical programs. dotty is a customizable interface for the X Window System written in lefty. tcldot is a customizable graphical interface written in Tcl 7. lefty is a graphics editor for technical pictures.

libgraph and libagraph: the drawing libraries. Their presence means an application can use GraphViz as a library rather than as a software tool.

Drawing Basic Graphs

Before I start showing you Graphviz code, I should first describe to you some important information about Graphviz nodes and edges.

Table 1 shows some of the node attributes whereas table 2 shows some of the edge attributes. You can check the Graphviz documentation for the full list of node and edge attributes.

Table 1: Node attributes

Table 2: Edge attributes.

I will now present you with the Graphviz code that generates Figure 1:

I found this example in the /opt/local/share/graphviz/graphs/directed directory (I use the Macports version of Graphviz). The file is called unix2.dot and (as I told you before) is a plain text file, which means that you only need a simple plain text editor in order to write Graphviz files.

The node [color=lightblue2, style=filled]; line of code declares some global properties about each node of the graph. You can later overwrite the global properties for any given node if you want. The digraph command says that the graph is a directed one. The -> notation is for declaring a directed connection between nodes. Each line of code ends with a semicolon.

In order to create the output file using the command line Graphviz version you will have to type the following in the Mac OS X command line (using the Terminal application):

The -o parameter defines the output file name. Note that both the -T and the -o switches are next to their respective parameter values without a space character between them.

More advanced Graphviz examples

This article section will present some more advanced Graphviz examples.

Please take a look at figure 2. This is a binary tree representation using Graphviz and the dot language. As you will see it is very easy to create it - I think that it would be a lot harder to illustrate it in either Adobe Illustrator or Adobe Photoshop. Better yet, it is also easier to make small or big changes to it.

As you can see, each node has is divided into three parts. Each part has a name: <f0> for the first part, <f1> for the second part and <f2> for the third part. In order to call a given part of a node, the notation is node0:f0 - for the first part of node 0. The symbolic name has nothing to do with the displayed label. Also, as you may understand, a node part can be empty but still have a symbolic name.

Figure 2: Drawing a binary tree using Graphviz

The Graphviz code for creating our next example (figure 3) is the following:

This is a database schema, visualized with the help of Graphviz. The presented schema is simple; nevertheless you can still understand how elegant this is. By reading the Graphviz code you can understand that lines beginning with the # character are comments.

Figure 3: Creating a DB Schema using Graphviz.

Using Graphviz on a Mac Part 1: PixelGlow

I first have to tell you that if you decide to use the PixelGlow Graphviz version, you will not need the command line tools. PixelGlow's version will render the Graphviz code for you. Next, I should tell you that PixelGlow's Graphviz won Best Mac OS X Open Source Product and was runner-up in Best Product to Mac OS X in the 2004 Apple Design Awards.

The Mac OS X version supports native fonts, exporting to all Quicktime image formats, on-line viewing of the output, etc.

Figure 4: The PixelGlow Graphviz GUI

You may find it surprising, but the presented graph in figure 4 -that also shows the PixelGlow Graphviz GUI- uses the same Graphviz code that created figure 6! I only changed some Graphviz properties using the PixelGlow version and, as you can see, the new output is totally different!

Using Graphviz on a Mac Part 2: OmniGraffle Professional

Macintosh users have another option for rendering Graphviz files: OmniGraflle.

What is special about Omnigraffle is that it allows you to drag-and-drop a node or a group of nodes in order to rearrange your graph according to your needs. This is an excellent feature that allows you to fine tune your output.

Figure 5 shows Omnigraffle processing a Graphviz file. Again, you do not need the command line Graphviz tools to render Graphviz code when using Omnigraffle.

Figure 5: Using OmniGraffle with Graphviz files

A perl script that produces Graphviz code

When I was writing my eBook "Programming Dashboard Widgets", I wanted to visualize the structure of most of the presented Widgets. I decided to use Graphviz and I wrote the presented Perl script in order to automatically create the Graphviz code.

I am not going to explain you the perl code as this not the purpose of this article, but I will give you an example of its output. By running the perl script (./Wstruct.pl /Library/Widgets/Weather.wdgt) you will get the following Graphviz code:

Figure 6 shows the graph that you will get after manually compiling the generated code using dot.

Figure 6: Using the Wstruct.pl perl script - an example.

Please note that the Wstruct.pl perl script does not include PNG files in its output. This was a design decision in order to avoid the busy output that some Widgets may have because they contained a plethora of PNG files. Also note that only regular files have their size in bytes next to them.

Conclusions

I hope that you find Graphviz both entertaining and interesting. I think that it is an exceptional piece of software that is very capable. Finally, there is plenty of useful material available in the web links provided, so you are bound to find some benefits through experimenting.