Color a specific node and its children nodes/edges in one color, and grey everything else out

Here is a gvpr (http://www.graphviz.org/pdf/gvpr.1.pdf) program that takes a standard Graphviz input file and outputs the starting node in yellow, children (and edges) in green and other nodes and edges in lightgrey.

BEGIN{
  node_t   aNode,  Start;
  graph_t  aGraph, Root;
  int      seenE[], seenN[];
  int      Ecnt=0;
  string   start;

  /////////////////////////////////////////////////////////////////////
  // the anEdge argument is just for bookkeeping
  //   each call creates a new instance
  //   so the nxtedge call does not over-write
  void nodeTraverse(node_t thisNode,   edge_t   anEdge){
    print("// NODE: ", thisNode.name, "  seen: ", seenN[thisNode]);
    if (seenN[thisNode]!=1){
      seenN[thisNode]=1;
      thisNode.fillcolor="green";
      thisNode.style="filled";      
      for (anEdge = fstout(thisNode); anEdge; anEdge = nxtout(anEdge)){
        print("// edge: ", anEdge.name, "  ", anEdge.tail, "  ", anEdge.head, "  seen: ", seenE[anEdge]);
        if (seenE[anEdge]==0){
	  anEdge.color="green";
          seenE[anEdge]=1;
          print("// recurse: ", anEdge.head);
          nodeTraverse(anEdge.head, anEdge);
        }
      }
    }
    print ("//  DONE: ", thisNode.name);
  }  // end of nodeTraverse
  //////////////////////////////////////////////
}
BEG_G{
  Root=$G;
  start=ARGV[0];
  print ("//   start: ", start);
  Start=isNode($G, start);
  if (Start==NULL){
    printf(2, "Error: unknown node >%s<\n", start);
    Ecnt++;
  }
  if (Ecnt>0) exit(9);
}
//
// grey out all nodes & edges
// we will color the ones we want later
//
N{
  $.style="filled";
  $.fillcolor="lightgrey";
}
E{
  $.color="lightgrey";
}
// now find all the children & color them & their edges
END_G{
  nodeTraverse(Start);  // no edge parameter on this call
//  Start=isNode($G, start);
print("//  Start: ", Start.name,Start.color);
  Start.fillcolor="yellow";
}

The (Linux) command line is something like this. Note the -a estimator piece of the command that specifies the starting node

f=colorChildren0.gv;T=png;F=`basename -s .gv $f`; gvpr -c -a estimator -f colorChildren2.gvpr $f | dot -T$T  >$F.$T

The above command produced this:

1 Like