Debian Codegraph @

cmd/tools/sccmap.c

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