Karachi   ->   Sweden   ->   Karachi, again   ->   Dubai   ->   Bahrain   ->   Karachi, once more   ->   London and Leeds

Saturday, May 19, 2007

Generating Graphs from Code

In memory representation of graphs using adjacency matrices (very poor for sparse graphs) or lists of nodes and associated edges is pretty easy to code. What's a show-stopper for me is generating a visualization of that graph. Even more complex is providing editing features on the nodes and edges. You might have guessed that I'm not good at UI programming.

I've been thinking of using MS Visio's Object Model to generate vsd diagrams. This approach has two drawbacks; the first and the important one is that the sheer size of the object model is daunting, specially when you don't know where to start from. Secondly, Visio is heavy! However, if you are interested in Visio based development, this might be a good start.

I recently stumbled upon WinGraphviz (a COM component that reads graphs written in DOT language and generates variety of outputs including PNG images).

The DOT language is extremely simple. The following example is taken straight out of the manual:
digraph G {
main -> parse -> execute;
main -> init;
main -> cleanup;
execute -> make_string;
execute -> printf
init -> make_string;
main -> printf;
execute -> compare;
}

which results in the following diagram:



You just pass the above mentioned textual description as a string and get back SVG, PS or an image file as output from the COM object. Easy, isn't it? I can't think of anything more simple than that. Besides, the DOT language is very rich and WinGraphviz can generate extremely complex diagrams. And as if that wasn't enough, the tool is open source!