Creating graph from scratch

This example shows how to to create graph from scratch and how to set some of node and edge attributes.

The program produces the following graph:

 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   Agnode_t *m;
10   Agedge_t *e;
11 
12   g = agopen("g", Agdirected, 0);
13 
14   agattr(g, AGNODE, "shape", "");
15   agattr(g, AGNODE, "color", "");
16   agattr(g, AGNODE, "style", "");
17   agattr(g, AGNODE, "peripheries", "");
18   agattr(g, AGNODE, "label", "");
19   agattr(g, AGEDGE, "style", "");
20   agattr(g, AGEDGE, "weight", "");
21   agattr(g, AGEDGE, "dir", "");
22  
23   n = agnode(g, "a", TRUE);
24   agset(n, "shape", "hexagon");
25   agset(n, "color", "red");
26 
27   m = agnode(g, "b", TRUE);
28   agset(m, "style", "filled");
29   
30   e = agedge(n,m, "e1", TRUE);
31   agset(e, "weight", "8");
32 
33   m = agnode(g, "c", TRUE);
34   agset(m, "label", "Node c");
35   agset(m, "shape", "polygon");
36   agset(m, "peripheries", "2");
37 
38   e = agedge(n,m, "e2", TRUE);
39   agset(e, "style", "dotted");
40   agset(e, "dir", "back");
41 
42   FILE *outf = fopen(argv[1], "w");
43   agwrite(g, outf);
44 
45   agclose(g); 
46   return 0;
47 }

Copyright 2006 Roman Putanowicz

Email: putanowr at twins.pk.edu.pl

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

Made with PubTal 3.1.3