Friday, May 27, 2011

Subgraphs in Graphviz Dot

Graphviz Dot is an extremely useful program for visualizing graph data (e.g., Finite State Machines, network topologies, etc.).  What makes Dot even more powerful is the ability to draw subgraphs (graphs within graphs).  This capability is a bit tricky, however, so here is the simplest "Hello World" I can give:

  1 digraph dfg {
  2    compound = true;
  3    subgraph cluster_parent {
  4       color=blue;
  5       label="Parent";
  6       A->B [lhead=cluster_child_1];
  7       A->C;
  8       subgraph cluster_child_1 {
  9          color=green;
 10          label="Child #1";
 11          B->C;
 12       }
 13       subgraph cluster_child_2 {
 14          color=green;
 15          label="Child #2";
 16          C->D [ltail=cluster_child_1];
 17       }
 18       D->E;
 19       E->B [lhead=cluster_child_1];
 20    }
 21 }

The program above produces:
The key (non-intuitive) pieces for working with subgraphs are:
  1. Subgraph names must start with the string "cluster" --- for example, cluster_child_1.
  2. Edges that terminate at a subgraph (e.g., Node A to Child #1) must have the attribute "lhead" defined as the subgraph name.
  3. Edges that start at a subgraph (e.g., Child #1 to Node D) must have the attribute "ltail" defined as the subgraph name.

No comments:

Post a Comment