Iteration over all graph edges

This example shows how one can read graph from a file and iterate over all graph edges. For each edge visited its label is printed.
The input file is assumed to be in GraphViz DOT language.

Sample undirected graph for this example is shown in the figure below:

 1 #pragma package ".."
 2 #include "chagraph.h"
 3 #include <stdlib.h>
 4 
 5 int main(int argc, char *argv[])
 6 {
 7   Agraph_t *g;
 8   Agnode_t *n;
 9   Agedge_t *e;
10 
11   if (argc < 2) {
12     printf("Usage:  %s file\n"
13            "   file -- file in GraphViz dot format\n",
14            argv[0]);
15     exit(EXIT_FAILURE);
16   }
17   FILE *f = fopen(argv[1], "r");
18   g = agread(f, 0);
19   fclose(f);
20 
21   for (n = agfstnode(g); n; n = agnxtnode(n)) {
22     for (e = agfstout(n); e; e = agnxtout(e)) {
23       printf("edge: %s\n", agget(e, "label"));
24     }
25   } 
26  
27   agclose(g); 
28   return 0;
29 }

Copyright 2006 Roman Putanowicz

Email: putanowr at twins.pk.edu.pl

Last Modified: Thu, 29 Jun 2006 18:02:58 CEST

Made with PubTal 3.1.3