Debian Codegraph @

doc/libgraph/sccmap.c

raw
1 /*************************************************************************
2 * Copyright (c) 2011 AT&T Intellectual Property
3 * All rights reserved. This program and the accompanying materials
4 * are made available under the terms of the Eclipse Public License v1.0
5 * which accompanies this distribution, and is available at
6 * https://www.eclipse.org/legal/epl-v10.html
7 *
8 * Contributors: See CVS logs. Details at http://www.graphviz.org/
9 *************************************************************************/
10
11 #include "config.h"
12
13 #include <stdbool.h>
14 #include <stdio.h>
15 #include <stdlib.h>
16 #include "cgraph.h"
17 #include "exit.h"
18 #include "ingraphs.h"
19
20 #include <getopt.h>
21
22 #define INF ((unsigned int)(-1))
23
24 typedef struct Agraphinfo_t {
25 Agrec_t h;
26 Agnode_t *rep;
27 } Agraphinfo_t;
28
29 typedef struct Agnodeinfo_t {
30 Agrec_t h;
31 unsigned int val;
32 Agraph_t *scc;
33 } Agnodeinfo_t;
34
35 #define getrep(g) (((Agraphinfo_t*)(g->base.data))->rep)
36 #define setrep(g,rep) (getrep(g) = rep)
37 #define getscc(n) (((Agnodeinfo_t*)((n)->base.data))->scc)
38 #define setscc(n,sub) (getscc(n) = sub)
39 #define getval(n) (((Agnodeinfo_t*)((n)->base.data))->val)
40 #define setval(n,newval) (getval(n) = newval)
41
42 /********* stack ***********/
43 typedef struct {
44 Agnode_t **data;
45 Agnode_t **ptr;
46 } Stack;
47
48 static void initStack(Stack * sp, int sz)
49 {
50 sp->data = malloc(sz * sizeof(Agnode_t *));
51 sp->ptr = sp->data;
52 }
53
54 static void freeStack(Stack * sp)
55 {
56 free(sp->data);
57 }
58
59 static void push(Stack * sp, Agnode_t * n)
60 {
61 *(sp->ptr++) = n;
62 }
63
64 static Agnode_t *top(Stack * sp)
65 {
66 return *(sp->ptr - 1);
67 }
68
69 static Agnode_t *pop(Stack * sp)
70 {
71 sp->ptr--;
72 return *(sp->ptr);
73 }
74
75 /********* end stack ***********/
76
77 typedef struct {
78 int Comp;
79 int ID;
80 int N_nodes_in_nontriv_SCC;
81 } sccstate;
82
83 static int wantDegenerateComp;
84 static int Silent;
85 static int StatsOnly;
86 static int Verbose;
87 static char *CmdName;
88 static char **Files;
89 static FILE *outfp; /* output; stdout by default */
90
91 static void nodeInduce(Agraph_t * g, Agraph_t * map)
92 {
93 Agnode_t *n;
94 Agedge_t *e;
95 Agraph_t *rootg = agroot(g);
96
97
98 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
99 for (e = agfstout(rootg, n); e; e = agnxtout(rootg, e)) {
100 if (agsubnode(g, aghead(e), 0))
101 agsubedge(g, e, 1);
102 else {
103 Agraph_t *tscc = getscc(agtail(e));
104 Agraph_t *hscc = getscc(aghead(e));
105 if (tscc && hscc)
106 agedge(map, getrep(tscc), getrep(hscc), NULL, 1);
107 }
108 }
109 }
110 }
111
112 static int visit(Agnode_t * n, Agraph_t * map, Stack * sp, sccstate * st)
113 {
114 unsigned int m, min;
115 Agnode_t *t;
116 Agraph_t *subg;
117 Agedge_t *e;
118
119 min = ++(st->ID);
120 setval(n, min);
121 push(sp, n);
122
123 for (e = agfstout(n->root, n); e; e = agnxtout(n->root, e)) {
124 t = aghead(e);
125 if (getval(t) == 0)
126 m = visit(t, map, sp, st);
127 else
128 m = getval(t);
129 if (m < min)
130 min = m;
131 }
132
133 if (getval(n) == min) {
134 if (!wantDegenerateComp && (top(sp) == n)) {
135 setval(n, INF);
136 pop(sp);
137 } else {
138 char name[32];
139 Agraph_t *G = agraphof(n);;
140 snprintf(name, sizeof(name), "cluster_%d", (st->Comp)++);
141 subg = agsubg(G, name, 1);
142 agbindrec(subg, "scc_graph", sizeof(Agraphinfo_t), true);
143 setrep(subg, agnode(map, name, 1));
144 do {
145 t = pop(sp);
146 agsubnode(subg, t, 1);
147 setval(t, INF);
148 setscc(t, subg);
149 st->N_nodes_in_nontriv_SCC++;
150 } while (t != n);
151 nodeInduce(subg, map);
152 if (!StatsOnly)
153 agwrite(subg, outfp);
154 }
155 }
156 return min;
157 }
158
159 static int label(Agnode_t * n, int nodecnt, int *edgecnt)
160 {
161 Agedge_t *e;
162
163 setval(n, 1);
164 nodecnt++;
165 for (e = agfstedge(n->root, n); e; e = agnxtedge(n->root, e, n)) {
166 (*edgecnt) += 1;
167 if (e->node == n)
168 e = agopp(e);
169 if (!getval(e->node))
170 nodecnt = label(e->node, nodecnt, edgecnt);
171 }
172 return nodecnt;
173 }
174
175 static int
176 countComponents(Agraph_t * g, int *max_degree, float *nontree_frac)
177 {
178 int nc = 0;
179 int sum_edges = 0;
180 int sum_nontree = 0;
181 int deg;
182 int n_edges;
183 int n_nodes;
184 Agnode_t *n;
185
186 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
187 if (!getval(n)) {
188 nc++;
189 n_edges = 0;
190 n_nodes = label(n, 0, &n_edges);
191 sum_edges += n_edges;
192 sum_nontree += (n_edges - n_nodes + 1);
193 }
194 }
195 if (max_degree) {
196 int maxd = 0;
197 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
198 deg = agdegree(g, n, 1, 1);
199 if (maxd < deg)
200 maxd = deg;
201 setval(n, 0);
202 }
203 *max_degree = maxd;
204 }
205 if (nontree_frac) {
206 if (sum_edges > 0)
207 *nontree_frac = (float) sum_nontree / (float) sum_edges;
208 else
209 *nontree_frac = 0.0;
210 }
211 return nc;
212 }
213
214 static void process(Agraph_t * G)
215 {
216 Agnode_t *n;
217 Agraph_t *map;
218 int nc = 0;
219 float nontree_frac = 0;
220 int Maxdegree = 0;
221 Stack stack;
222 sccstate state;
223
224 aginit(G, AGRAPH, "scc_graph", sizeof(Agraphinfo_t), true);
225 aginit(G, AGNODE, "scc_node", sizeof(Agnodeinfo_t), true);
226 state.Comp = state.ID = 0;
227 state.N_nodes_in_nontriv_SCC = 0;
228
229 if (Verbose)
230 nc = countComponents(G, &Maxdegree, &nontree_frac);
231
232 initStack(&stack, agnnodes(G) + 1);
233 map = agopen("scc_map", Agdirected, (Agdisc_t *) 0);
234 for (n = agfstnode(G); n; n = agnxtnode(G, n))
235 if (getval(n) == 0)
236 visit(n, map, &stack, &state);
237 freeStack(&stack);
238 if (!StatsOnly)
239 agwrite(map, outfp);
240 agclose(map);
241
242 if (Verbose)
243 fprintf(stderr, "%d %d %d %d %.4f %d %.4f\n",
244 agnnodes(G), agnedges(G), nc, state.Comp,
245 state.N_nodes_in_nontriv_SCC / (double) agnnodes(G),
246 Maxdegree, nontree_frac);
247 else if (!Silent)
248 fprintf(stderr, "%d nodes, %d edges, %d strong components\n",
249 agnnodes(G), agnedges(G), state.Comp);
250
251 }
252
253 static FILE *openFile(const char *name)
254 {
255 FILE *fp;
256
257 fp = fopen(name, "w");
258 if (!fp) {
259 fprintf(stderr, "gvpack: could not open file %s for writing\n", name);
260 graphviz_exit(1);
261 }
262 return (fp);
263 }
264
265 static char *useString = "Usage: %s [-sdv?] <files>\n\
266 -s - only produce statistics\n\
267 -S - silent\n\
268 -d - allow degenerate components\n\
269 -o<outfile> - write to <outfile> (stdout)\n\
270 -v - verbose\n\
271 -? - print usage\n\
272 If no files are specified, stdin is used\n";
273
274 static void usage(int v)
275 {
276 printf(useString, CmdName);
277 graphviz_exit(v);
278 }
279
280 static void scanArgs(int argc, char **argv)
281 {
282 int c;
283
284 CmdName = argv[0];
285 opterr = 0;
286 while ((c = getopt(argc, argv, ":o:sdvS?")) != EOF) {
287 switch (c) {
288 case 's':
289 StatsOnly = 1;
290 break;
291 case 'd':
292 wantDegenerateComp = 1;
293 break;
294 case 'o':
295 outfp = openFile(optarg);
296 break;
297 case 'v':
298 Verbose = 1;
299 break;
300 case 'S':
301 Verbose = 0;
302 Silent = 1;
303 break;
304 case '?':
305 if (optopt == '\0')
306 usage(0);
307 else {
308 fprintf(stderr, "%s: option -%c unrecognized\n",
309 CmdName, optopt);
310 usage(1);
311 }
312 break;
313 }
314 }
315 argv += optind;
316 argc -= optind;
317
318 if (argc)
319 Files = argv;
320 if (!outfp)
321 outfp = stdout; /* stdout the default */
322 }
323
324 int main(int argc, char **argv)
325 {
326 Agraph_t *g;
327 ingraph_state ig;
328
329 scanArgs(argc, argv);
330 newIngraph(&ig, Files);
331
332 while ((g = nextGraph(&ig)) != 0) {
333 if (agisdirected(g))
334 process(g);
335 else
336 fprintf(stderr, "Graph %s in %s is undirected - ignored\n",
337 agnameof(g), fileName(&ig));
338 agclose(g);
339 }
340
341 return 0;
342 }