Graphviz 13.0.0~dev.20241220.2304
Loading...
Searching...
No Matches
bcomps.c
Go to the documentation of this file.
1
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 * Generate biconnected components
19 *
20 * Written by Emden Gansner
21 */
22#include "config.h"
23
24#include <stdbool.h>
25#include <stdio.h>
26#include <string.h>
27#include <assert.h>
28#include <getopt.h>
29
30#include <stdlib.h>
31#include <cgraph/cgraph.h>
32#include <cgraph/ingraphs.h>
33#include <cgraph/list.h>
34#include <util/agxbuf.h>
35#include <util/alloc.h>
36#include <util/exit.h>
37#include <util/gv_math.h>
38#include <util/unreachable.h>
39
40typedef struct {
44
45typedef struct {
48
49typedef struct {
51 int low;
52 int val;
53 int isCut;
55
56#define Low(n) (((Agnodeinfo_t*)(n->base.data))->low)
57#define Cut(n) (((Agnodeinfo_t*)(n->base.data))->isCut)
58#define N(n) (((Agnodeinfo_t*)(n->base.data))->val)
59#define NEXTBLK(g) (((Agraphinfo_t*)(g->base.data))->next)
60
61char **Files;
64char *outfile = 0;
65char *path = 0;
66char *suffix = 0;
67int external; /* emit blocks as root graphs */
68int doTree; /* emit block-cutpoint tree */
69
70DEFINE_LIST(edge_stack, Agedge_t *)
71
72typedef struct {
73 int count;
74 int nComp;
75 edge_stack_t stk;
77} bcstate;
78
79static char *blockName(agxbuf *xb, char *gname, int d) {
80 if (*gname == '%') /* anonymous graph */
81 agxbprint(xb, "_%s_bcc_%d", gname, d);
82 else
83 agxbprint(xb, "%s_bcc_%d", gname, d);
84 return agxbuse(xb);
85}
86
87/* getName:
88 * Generate name for output using input template.
89 * Has form path_<g>_<i>.suffix, for ith write for the gth graph.
90 * If isTree, use path_<g>_t.suffix.
91 * If sufcnt is zero and graph 0, use outfile
92 */
93static char *getName(int ng, int nb)
94{
95 agxbuf name = {0};
96
97 if (ng == 0 && nb == 0)
98 agxbput(&name, outfile);
99 else {
100 if (suffix) {
101 if (nb < 0)
102 agxbprint(&name, "%s_%d_T.%s", path, ng, suffix);
103 else
104 agxbprint(&name, "%s_%d_%d.%s", path, ng, nb, suffix);
105 } else {
106 if (nb < 0)
107 agxbprint(&name, "%s_%d_T", path, ng);
108 else
109 agxbprint(&name, "%s_%d_%d", path, ng, nb);
110 }
111 }
112 return agxbdisown(&name);
113}
114
115static void gwrite(Agraph_t * g, int ng, int nb)
116{
117 FILE *outf;
118 char *name;
119
120 if (silent)
121 return;
122 if (!outfile) {
123 agwrite(g, stdout);
124 fflush(stdout);
125 } else {
126 name = getName(ng, nb);
127 outf = fopen(name, "w");
128 if (!outf) {
129 fprintf(stderr, "Could not open %s for writing\n", name);
130 perror("bcomps");
131 free(name);
132 graphviz_exit(1);
133 }
134 free(name);
135 agwrite(g, outf);
136 fclose(outf);
137 }
138}
139
140static Agraph_t *mkBlock(Agraph_t * g, bcstate * stp)
141{
142 Agraph_t *sg;
143
144 stp->nComp++;
145 agxbuf xb = {0};
146 sg = agsubg(g, blockName(&xb, agnameof(g), stp->nComp), 1);
147 agxbfree(&xb);
148 agbindrec(sg, "info", sizeof(Agraphinfo_t), true);
149 NEXTBLK(sg) = stp->blks;
150 stp->blks = sg;
151 return sg;
152}
153
154static void
156{
157 Agnode_t *v;
158 Agedge_t *e;
159 Agedge_t *ep;
160 Agraph_t *sg;
161
162 stp->count++;
163 Low(u) = N(u) = stp->count;
164 for (e = agfstedge(g, u); e; e = agnxtedge(g, e, u)) {
165 if ((v = aghead(e)) == u)
166 v = agtail(e);
167 if (v == u)
168 continue;
169 if (N(v) == 0) {
170 edge_stack_push_back(&stp->stk, e);
171 dfs(g, v, stp, u);
172 Low(u) = imin(Low(u), Low(v));
173 if (Low(v) >= N(u)) { /* u is an articulation point */
174 Cut(u) = 1;
175 sg = mkBlock(g, stp);
176 do {
177 ep = edge_stack_pop_back(&stp->stk);
178 agsubnode(sg, aghead(ep), 1);
179 agsubnode(sg, agtail(ep), 1);
180 } while (ep != e);
181 }
182 } else if (parent != v) {
183 Low(u) = imin(Low(u), N(v));
184 if (N(v) < N(u))
185 edge_stack_push_back(&stp->stk, e);
186 }
187 }
188}
189
190static void addCutPts(Agraph_t * tree, Agraph_t * blk)
191{
192 Agnode_t *n;
193 Agnode_t *bn;
194 Agnode_t *cn;
195
196 bn = agnode(tree, agnameof(blk), 1);
197 for (n = agfstnode(blk); n; n = agnxtnode(blk, n)) {
198 if (Cut(n)) {
199 cn = agnode(tree, agnameof(n), 1);
200 agedge(tree, bn, cn, 0, 1);
201 }
202 }
203}
204
205static int process(Agraph_t * g, int gcnt)
206{
207 Agnode_t *n;
208 bcstate state;
209 Agraph_t *blk;
210 Agraph_t *tree;
211 int bcnt;
212
213 aginit(g, AGNODE, "info", sizeof(Agnodeinfo_t), true);
214 aginit(g, AGEDGE, "info", sizeof(Agedgeinfo_t), true);
215 aginit(g, AGRAPH, "info", sizeof(Agraphinfo_t), true);
216
217 state.count = 0;
218 state.nComp = 0;
219 state.stk = (edge_stack_t){0};
220 state.blks = 0;
221
222 for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
223 if (N(n) == 0)
224 dfs(g, n, &state, 0);
225 }
226 for (blk = state.blks; blk; blk = NEXTBLK(blk)) {
227 (void)graphviz_node_induce(blk, g);
228 }
229 if (external) {
230 bcnt = 0;
231 for (blk = state.blks; blk; blk = NEXTBLK(blk)) {
232 gwrite(blk, gcnt, bcnt++);
233 }
234 } else
235 gwrite(g, gcnt, 0);
236 if (doTree) {
237 tree = agopen("blkcut_tree", Agstrictundirected, 0);
238 for (blk = state.blks; blk; blk = NEXTBLK(blk))
239 addCutPts(tree, blk);
240 gwrite(tree, gcnt, -1);
241 agclose(tree);
242 }
243 if (verbose) {
244 int cuts = 0;
245 bcnt = 0;
246 for (blk = state.blks; blk; blk = NEXTBLK(blk))
247 bcnt++;
248 for (n = agfstnode(g); n; n = agnxtnode(g, n))
249 if (Cut(n))
250 cuts++;
251 fprintf(stderr, "%s: %d blocks %d cutpoints\n", agnameof(g), bcnt,
252 cuts);
253 }
254 edge_stack_free(&state.stk);
255 if (state.blks && NEXTBLK(state.blks))
256 return 1; /* >= 2 blocks */
257 else
258 return 0;
259}
260
261static char *useString =
262 "Usage: bcomps [-stvx?] [-o<out template>] <files>\n\
263 -o - output file template\n\
264 -s - don't print components\n\
265 -t - emit block-cutpoint tree\n\
266 -v - verbose\n\
267 -x - external\n\
268 -? - print usage\n\
269If no files are specified, stdin is used\n";
270
271static void usage(int v)
272{
273 printf("%s",useString);
274 graphviz_exit(v);
275}
276
277static void split(char *name)
278{
279 char *sfx = 0;
280
281 sfx = strrchr(name, '.');
282 if (sfx) {
283 size_t size = (size_t)(sfx - name);
284 suffix = sfx + 1;
285 path = gv_strndup(name, size);
286 } else {
287 path = name;
288 }
289}
290
291static void init(int argc, char *argv[])
292{
293 int c;
294
295 opterr = 0;
296 while ((c = getopt(argc, argv, ":o:xstv?")) != -1) {
297 switch (c) {
298 case 'o':
299 outfile = optarg;
300 split(outfile);
301 break;
302 case 's':
303 verbose = 1;
304 silent = 1;
305 break;
306 case 'v':
307 verbose = 1;
308 break;
309 case 't':
310 doTree = 1;
311 break;
312 case 'x':
313 external = 1;
314 break;
315 case ':':
316 fprintf(stderr, "bcomps: option -%c missing argument - ignored\n", optopt);
317 break;
318 case '?':
319 if (optopt == '\0' || optopt == '?')
320 usage(0);
321 else {
322 fprintf(stderr,
323 "bcomps: option -%c unrecognized\n", optopt);
324 usage(1);
325 }
326 break;
327 default:
328 UNREACHABLE();
329 }
330 }
331 argv += optind;
332 argc -= optind;
333
334 if (argc > 0)
335 Files = argv;
336}
337
338int main(int argc, char *argv[])
339{
340 Agraph_t *g;
341 ingraph_state ig;
342 int r = 0;
343 int gcnt = 0;
344
345 init(argc, argv);
346 newIngraph(&ig, Files);
347
348 while ((g = nextGraph(&ig)) != 0) {
349 r |= process(g, gcnt);
350 agclose(g);
351 gcnt++;
352 }
353
354 graphviz_exit(r);
355}
static void agxbfree(agxbuf *xb)
free any malloced resources
Definition agxbuf.h:78
static int agxbprint(agxbuf *xb, const char *fmt,...)
Printf-style output to an agxbuf.
Definition agxbuf.h:234
static WUR char * agxbuse(agxbuf *xb)
Definition agxbuf.h:307
static char * agxbdisown(agxbuf *xb)
Definition agxbuf.h:327
Memory allocation wrappers that exit on failure.
static char * gv_strndup(const char *original, size_t length)
Definition alloc.h:114
int doTree
Definition bcomps.c:68
static void init(int argc, char *argv[])
Definition bcomps.c:291
int verbose
Definition bcomps.c:62
#define Low(n)
Definition bcomps.c:56
#define N(n)
Definition bcomps.c:58
int silent
Definition bcomps.c:63
char * suffix
Definition bcomps.c:66
static void gwrite(Agraph_t *g, int ng, int nb)
Definition bcomps.c:115
int external
Definition bcomps.c:67
static int process(Agraph_t *g, int gcnt)
Definition bcomps.c:205
static char * blockName(agxbuf *xb, char *gname, int d)
Definition bcomps.c:79
static char * useString
Definition bcomps.c:261
static void dfs(Agraph_t *g, Agnode_t *u, bcstate *stp, Agnode_t *parent)
Definition bcomps.c:155
char * outfile
Definition bcomps.c:64
#define NEXTBLK(g)
Definition bcomps.c:59
static void addCutPts(Agraph_t *tree, Agraph_t *blk)
Definition bcomps.c:190
char ** Files
Definition bcomps.c:61
static Agraph_t * mkBlock(Agraph_t *g, bcstate *stp)
Definition bcomps.c:140
#define Cut(n)
Definition bcomps.c:57
abstract graph C library, Cgraph API
#define parent(i)
Definition closest.c:80
static void split(void)
Definition ccomps.c:102
static char * getName(void)
Definition ccomps.c:260
static NORETURN void graphviz_exit(int status)
Definition exit.h:23
static char * gname
Definition gml2gv.c:24
void free(void *)
size_t graphviz_node_induce(Agraph_t *g, Agraph_t *edgeset)
Definition node_induce.c:9
Agedge_t * agedge(Agraph_t *g, Agnode_t *t, Agnode_t *h, char *name, int createflag)
Definition edge.c:260
#define agtail(e)
Definition cgraph.h:880
Agedge_t * agnxtedge(Agraph_t *g, Agedge_t *e, Agnode_t *n)
Definition edge.c:94
#define aghead(e)
Definition cgraph.h:881
Agedge_t * agfstedge(Agraph_t *g, Agnode_t *n)
Definition edge.c:85
Agdesc_t Agstrictundirected
strict undirected
Definition graph.c:287
int agclose(Agraph_t *g)
deletes a graph, freeing its associated storage
Definition graph.c:102
Agraph_t * agopen(char *name, Agdesc_t desc, Agdisc_t *disc)
creates a new graph with the given name and kind
Definition graph.c:44
int agwrite(Agraph_t *g, void *chan)
Return 0 on success, EOF on failure.
Definition write.c:730
Agnode_t * agnode(Agraph_t *g, char *name, int createflag)
Definition node.c:145
Agnode_t * agnxtnode(Agraph_t *g, Agnode_t *n)
Definition node.c:47
Agnode_t * agfstnode(Agraph_t *g)
Definition node.c:40
Agnode_t * agsubnode(Agraph_t *g, Agnode_t *n, int createflag)
Definition node.c:259
char * agnameof(void *)
returns a string descriptor for the object.
Definition id.c:158
@ AGEDGE
Definition cgraph.h:207
@ AGNODE
Definition cgraph.h:207
@ AGRAPH
Definition cgraph.h:207
void aginit(Agraph_t *g, int kind, const char *rec_name, int rec_size, int move_to_front)
attach new records to objects of specified kind
Definition rec.c:170
void * agbindrec(void *obj, const char *name, unsigned int recsize, int move_to_front)
attaches a new record of the given size to the object
Definition rec.c:89
Agraph_t * agsubg(Agraph_t *g, char *name, int cflag)
Definition subg.c:58
Arithmetic helper functions.
static int imin(int a, int b)
minimum of two integers
Definition gv_math.h:27
agxbput(xb, staging)
@ tree
Definition gvgen.c:33
static const char * usage
Definition gvpr.c:51
Agraph_t * nextGraph(ingraph_state *sp)
Definition ingraphs.c:61
ingraph_state * newIngraph(ingraph_state *sp, char **files)
Definition ingraphs.c:140
supports user-supplied data
#define DEFINE_LIST(name, type)
Definition list.h:26
Agrec_t h
Definition bcomps.c:46
Agrec_t h
Definition bcomps.c:50
int isCut
Definition bcomps.c:53
graph or subgraph
Definition cgraph.h:425
Agraph_t * next
Definition bcomps.c:42
Agrec_t h
Definition bcomps.c:41
implementation of Agrec_t
Definition cgraph.h:172
edge_stack_t stk
Definition bcomps.c:75
int count
Definition bcomps.c:73
int nComp
Definition bcomps.c:74
Agraph_t * blks
Definition bcomps.c:76
Definition types.h:81
int main()
#define UNREACHABLE()
Definition unreachable.h:30