Graphs for Developers
Today I’m introducing you to a tool set that I know I wish I’d learned about years ago. GraphViz is a generic graph drawing package by the good people at AT&T. When I say “graph drawing” I am talking about the nodes and arcs (aka boxes and arrows) kind, not bar charts.
GraphViz is available under an open source license called the Common Public License that isn’t as strict as the GPL so it’s worth considering for commercial applications as well as open source; just check the license conditions for yourself to be sure that you’re OK with the specifics.
The Basics
At work I use it for producing quick graphs of data structures in my program. I simply make objects capable of outputting “dot” format graph drawing commands to a stream and the rest is trivial.
We’ll start by drawing a simple graph representing something a very wise person once said:
File graphviz_eg1.dot:
digraph "Example 1" {
Fear->Anger
Anger->Hate
Hate->Suffering
}
Run this file through dot -Tgif graphviz_eg1.dot -o mygraph.gif and
you have a gif of the graph. Use -Tps for postscript, or look through
the documentation for several other output formats. The output looks
like this:

That was easy enough. Name your arcs and they’re there. You can also place nodesseparately by just putting their name on an otherwise empty line.
If you want to get fancy there is quite a scary range of attributes you can add to the nodes and arcs. I’ll keep it simple here but it’s possible to place nodes that contain full HTML tables with color tags and the arrows from arcs pointing to particular table cells.
Here’s an example with a few common attributes added:
File graphviz_eg2.dot:
digraph "Example 2" {
MyNodeA [label="A", shape="box"]
B
NodeC [label="C", shape="ellipse"]
MyNodeA->B [label="a-to-b"]
B->NodeC
MyNodeA->NodeC
}

Labels on nodes and arcs, and elliptical nodes, no sweat! Just add
attributes in square brackets after the node or arc. For two or
more lines in a label just use \n, for example: [label="Line
One\nLine Two"].
For examples that push GraphViz a bit harder than I have here, why not check out the GraphViz Gallery?
Programs That Output Graphs
Aside from obvious applications for this, like web applications that can display graphs, the fact that it’s so darned easy to crank out graphs using this tool means it’s perfect as a debugging tool. Any time I’m creating a complex data structure like a scary variation on a graph or tree, I’ll throw in functions to output the data structures to a stream in “dot” format. This simple trick has found a large number of bugs already. I can output a series of .dot files and flick through them in an image viewer to “animate” the way my data structures grow and change. It’s so easy to do!
Documentation is easy using GraphViz; and several existing packages (eg Doxygen) use GraphViz to draw call graphs, inheritance graphs, you name it! There’s even a Perl module that draws graphs of your Makefile dependencies!
There’s a lot more in store for Perl programmers too: GraphViz is a Perl module which is much the same thing but can automate things like graphical dumps of data structures. Perl’s GraphViz package contains generic graph drawing tools as well as tools to export any data structure as a graph, or Yacc and similar Parses as a graph, or even XML and regular expressions as graphs.
If you’re feeling you’ve missed out because you don’t use Perl, then don’t worry: Google confirms that there are similar libraries around for C++, Python, Java, and probably most popular languages.
A Picture is Worth…
Well you get the idea. Graphviz is a way cool tool for drawing graphs that’s so easy to use it is practical in debugging. It is also surprisingly powerful as some of the more complex examples on their web site demonstrate. It’s available as a Perl module with all kinds of extras. And on top of all that it’s open source.
0 Comments
Please use the DP Forums for further discussion of this topic.


