File: ccomps.c

package info (click to toggle)
graphviz 14.0.5-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 139,388 kB
  • sloc: ansic: 141,938; cpp: 11,957; python: 7,766; makefile: 4,043; yacc: 3,030; xml: 2,972; tcl: 2,495; sh: 1,388; objc: 1,159; java: 560; lex: 423; perl: 243; awk: 156; pascal: 139; php: 58; ruby: 49; cs: 31; sed: 1
file content (528 lines) | stat: -rw-r--r-- 14,059 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
/*************************************************************************
 * Copyright (c) 2011 AT&T Intellectual Property
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * https://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors: Details at https://graphviz.org
 *************************************************************************/

#include <cgraph/cgraph.h>
#include <common/render.h>
#include <common/utils.h>
#include <limits.h>
#include <pack/pack.h>
#include <stdbool.h>
#include <stdlib.h>
#include <util/agxbuf.h>
#include <util/alloc.h>
#include <util/gv_ctype.h>
#include <util/list.h>
#include <util/prisize_t.h>

typedef LIST(Agnode_t *) node_stack_t;

typedef struct {
  node_stack_t data;
  void (*actionfn)(Agnode_t *, void *);
  bool (*markfn)(Agnode_t *, int);
} stk_t;

/// does `n` have a mark set?
static bool marked(const stk_t *stk, Agnode_t *n) { return stk->markfn(n, -1); }

/// set a mark on `n`
static void mark(const stk_t *stk, Agnode_t *n) { stk->markfn(n, 1); }

/// unset a mark on `n`
static void unmark(const stk_t *stk, Agnode_t *n) { stk->markfn(n, 0); }

static void initStk(stk_t *sp, void (*actionfn)(Agnode_t *, void *),
                    bool (*markfn)(Agnode_t *, int)) {
  sp->data = (node_stack_t){0};
  sp->actionfn = actionfn;
  sp->markfn = markfn;
}

static void freeStk(stk_t *sp) { LIST_FREE(&sp->data); }

static void push(stk_t *sp, Agnode_t *np) {
  mark(sp, np);
  LIST_PUSH_BACK(&sp->data, np);
}

static Agnode_t *pop(stk_t *sp) {
  if (LIST_IS_EMPTY(&sp->data)) {
    return NULL;
  }

  return LIST_POP_BACK(&sp->data);
}

static size_t dfs(Agraph_t *g, Agnode_t *n, void *state, stk_t *stk) {
  Agedge_t *e;
  Agnode_t *other;
  size_t cnt = 0;

  push(stk, n);
  while ((n = pop(stk))) {
    cnt++;
    if (stk->actionfn)
      stk->actionfn(n, state);
    for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
      if ((other = agtail(e)) == n)
        other = aghead(e);
      if (!marked(stk, other))
        push(stk, other);
    }
  }
  return cnt;
}

static bool isLegal(const char *p) {
  char c;

  while ((c = *p++)) {
    if (c != '_' && !gv_isalnum(c))
      return false;
  }

  return true;
}

static void insertFn(Agnode_t *n, void *state) { agsubnode(state, n, 1); }

static bool markFn(Agnode_t *n, int v) {
  if (v < 0)
    return ND_mark(n) != 0;
  const size_t ret = ND_mark(n);
  ND_mark(n) = v != 0;
  return ret != 0;
}

static void setPrefix(agxbuf *xb, const char *pfx) {
  if (!pfx || !isLegal(pfx)) {
    pfx = "_cc_";
  }
  agxbput(xb, pfx);
}

/* pccomps:
 * Return an array of subgraphs consisting of the connected
 * components of graph g. The number of components is returned in ncc.
 * All pinned nodes are in one component.
 * If pfx is non-null and a legal graph name, we use it as the prefix
 * for the name of the subgraphs created. If not, a simple default is used.
 * If pinned is non-null, *pinned set to 1 if pinned nodes found
 * and the first component is the one containing the pinned nodes.
 * Note that the component subgraphs do not contain any edges. These must
 * be obtained from the root graph.
 * Return NULL if graph is empty.
 */
Agraph_t **pccomps(Agraph_t *g, size_t *ncc, char *pfx, bool *pinned) {
  agxbuf name = {0};
  Agraph_t *out = NULL;
  Agnode_t *n;
  bool pin = false;
  stk_t stk;

  if (agnnodes(g) == 0) {
    *ncc = 0;
    return NULL;
  }

  LIST(Agraph_t *) ccs = {0};

  initStk(&stk, insertFn, markFn);
  for (n = agfstnode(g); n; n = agnxtnode(g, n))
    unmark(&stk, n);

  /* Component with pinned nodes */
  for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
    if (marked(&stk, n) || !isPinned(n))
      continue;
    if (!out) {
      setPrefix(&name, pfx);
      agxbprint(&name, "%" PRISIZE_T, LIST_SIZE(&ccs));
      out = agsubg(g, agxbuse(&name), 1);
      agbindrec(out, "Agraphinfo_t", sizeof(Agraphinfo_t),
                true); // node custom data
      LIST_APPEND(&ccs, out);
      pin = true;
    }
    dfs(g, n, out, &stk);
  }

  /* Remaining nodes */
  for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
    if (marked(&stk, n))
      continue;
    setPrefix(&name, pfx);
    agxbprint(&name, "%" PRISIZE_T, LIST_SIZE(&ccs));
    out = agsubg(g, agxbuse(&name), 1);
    agbindrec(out, "Agraphinfo_t", sizeof(Agraphinfo_t),
              true); // node custom data
    dfs(g, n, out, &stk);
    LIST_APPEND(&ccs, out);
  }
  freeStk(&stk);
  agxbfree(&name);
  *pinned = pin;
  Agraph_t **ret;
  LIST_DETACH(&ccs, &ret, ncc);
  return ret;
}

/* ccomps:
 * Return an array of subgraphs consisting of the connected
 * components of graph g. The number of components is returned in ncc.
 * If pfx is non-null and a legal graph name, we use it as the prefix
 * for the name of the subgraphs created. If not, a simple default is used.
 * Note that the component subgraphs do not contain any edges. These must
 * be obtained from the root graph.
 * Returns NULL on error or if graph is empty.
 */
Agraph_t **ccomps(Agraph_t *g, size_t *ncc, char *pfx) {
  agxbuf name = {0};
  Agraph_t *out;
  Agnode_t *n;
  stk_t stk;

  if (agnnodes(g) == 0) {
    *ncc = 0;
    return NULL;
  }

  LIST(Agraph_t *) ccs = {0};
  initStk(&stk, insertFn, markFn);
  for (n = agfstnode(g); n; n = agnxtnode(g, n))
    unmark(&stk, n);

  for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
    if (marked(&stk, n))
      continue;
    setPrefix(&name, pfx);
    agxbprint(&name, "%" PRISIZE_T, LIST_SIZE(&ccs));
    out = agsubg(g, agxbuse(&name), 1);
    agbindrec(out, "Agraphinfo_t", sizeof(Agraphinfo_t),
              true); // node custom data
    dfs(g, n, out, &stk);
    LIST_APPEND(&ccs, out);
  }
  freeStk(&stk);
  agxbfree(&name);
  Agraph_t **ret;
  LIST_DETACH(&ccs, &ret, ncc);
  return ret;
}

typedef struct {
  Agrec_t h;
  char cc_subg; /* true iff subgraph corresponds to a component */
} ccgraphinfo_t;

typedef struct {
  Agrec_t h;
  char mark;
  union {
    Agraph_t *g;
    Agnode_t *n;
    void *v;
  } ptr;
} ccgnodeinfo_t;

#define GRECNAME "ccgraphinfo"
#define NRECNAME "ccgnodeinfo"
#define GD_cc_subg(g) (((ccgraphinfo_t *)aggetrec(g, GRECNAME, 0))->cc_subg)
#ifdef DEBUG
Agnode_t *dnodeOf(Agnode_t *v) {
  ccgnodeinfo_t *ip = (ccgnodeinfo_t *)aggetrec(v, NRECNAME, 0);
  if (ip)
    return ip->ptr.n;
  fprintf(stderr, "nodeinfo undefined\n");
  return NULL;
}
void dnodeSet(Agnode_t *v, Agnode_t *n) {
  ((ccgnodeinfo_t *)aggetrec(v, NRECNAME, 0))->ptr.n = n;
}
#else
#define dnodeOf(v) (((ccgnodeinfo_t *)aggetrec(v, NRECNAME, 0))->ptr.n)
#define dnodeSet(v, w) (((ccgnodeinfo_t *)aggetrec(v, NRECNAME, 0))->ptr.n = w)
#endif

#define ptrOf(np) (((ccgnodeinfo_t *)((np)->base.data))->ptr.v)
#define nodeOf(np) (((ccgnodeinfo_t *)((np)->base.data))->ptr.n)
#define clustOf(np) (((ccgnodeinfo_t *)((np)->base.data))->ptr.g)
#define clMark(n) (((ccgnodeinfo_t *)(n->base.data))->mark)

/* deriveClusters:
 * Construct nodes in derived graph corresponding top-level clusters.
 * Since a cluster might be wrapped in a subgraph, we need to traverse
 * down into the tree of subgraphs
 */
static void deriveClusters(Agraph_t *dg, Agraph_t *g) {
  Agraph_t *subg;
  Agnode_t *dn;
  Agnode_t *n;

  for (subg = agfstsubg(g); subg; subg = agnxtsubg(subg)) {
    if (is_a_cluster(subg)) {
      dn = agnode(dg, agnameof(subg), 1);
      agbindrec(dn, NRECNAME, sizeof(ccgnodeinfo_t), true);
      clustOf(dn) = subg;
      for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) {
        if (dnodeOf(n)) {
          fprintf(stderr,
                  "Error: node \"%s\" belongs to two non-nested clusters "
                  "\"%s\" and \"%s\"\n",
                  agnameof(n), agnameof(subg), agnameof(dnodeOf(n)));
        }
        dnodeSet(n, dn);
      }
    } else {
      deriveClusters(dg, subg);
    }
  }
}

/* deriveGraph:
 * Create derived graph dg of g where nodes correspond to top-level nodes
 * or clusters, and there is an edge in dg if there is an edge in g
 * between any nodes in the respective clusters.
 */
static Agraph_t *deriveGraph(Agraph_t *g) {
  Agraph_t *dg;
  Agnode_t *dn;
  Agnode_t *n;

  dg = agopen("dg", Agstrictundirected, NULL);

  deriveClusters(dg, g);

  for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
    if (dnodeOf(n))
      continue;
    dn = agnode(dg, agnameof(n), 1);
    agbindrec(dn, NRECNAME, sizeof(ccgnodeinfo_t), true);
    nodeOf(dn) = n;
    dnodeSet(n, dn);
  }

  for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
    Agedge_t *e;
    Agnode_t *hd;
    Agnode_t *tl = dnodeOf(n);
    for (e = agfstout(g, n); e; e = agnxtout(g, e)) {
      hd = aghead(e);
      hd = dnodeOf(hd);
      if (hd == tl)
        continue;
      if (hd > tl)
        agedge(dg, tl, hd, NULL, 1);
      else
        agedge(dg, hd, tl, NULL, 1);
    }
  }

  return dg;
}

/* unionNodes:
 * Add all nodes in cluster nodes of dg to g
 */
static void unionNodes(Agraph_t *dg, Agraph_t *g) {
  Agnode_t *n;
  Agnode_t *dn;
  Agraph_t *clust;

  for (dn = agfstnode(dg); dn; dn = agnxtnode(dg, dn)) {
    if (AGTYPE(ptrOf(dn)) == AGNODE) {
      agsubnode(g, nodeOf(dn), 1);
    } else {
      clust = clustOf(dn);
      for (n = agfstnode(clust); n; n = agnxtnode(clust, n))
        agsubnode(g, n, 1);
    }
  }
}

static bool clMarkFn(Agnode_t *n, int v) {
  int ret;
  if (v < 0)
    return clMark(n) != 0;
  ret = clMark(n);
  clMark(n) = (char)v;
  return ret != 0;
}

typedef struct {
  Agrec_t h;
  Agraph_t *orig;
} orig_t;

#define ORIG_REC "orig"

Agraph_t *mapClust(Agraph_t *cl) {
  orig_t *op = (orig_t *)aggetrec(cl, ORIG_REC, 0);
  assert(op);
  return op->orig;
}

/* projectG:
 * If any nodes of subg are in g, create a subgraph of g
 * and fill it with all nodes of subg in g and their induced
 * edges in subg. Copy the attributes of subg to g. Return the subgraph.
 * If not, return null.
 * If subg is a cluster, the new subgraph will contain a pointer to it
 * in the record "orig".
 */
static Agraph_t *projectG(Agraph_t *subg, Agraph_t *g, int inCluster) {
  Agraph_t *proj = NULL;
  Agnode_t *n;
  Agnode_t *m;
  orig_t *op;

  for (n = agfstnode(subg); n; n = agnxtnode(subg, n)) {
    if ((m = agfindnode(g, agnameof(n)))) {
      if (proj == NULL) {
        proj = agsubg(g, agnameof(subg), 1);
      }
      agsubnode(proj, m, 1);
    }
  }
  if (!proj && inCluster) {
    proj = agsubg(g, agnameof(subg), 1);
  }
  if (proj) {
    (void)graphviz_node_induce(proj, subg);
    agcopyattr(subg, proj);
    if (is_a_cluster(proj)) {
      op = agbindrec(proj, ORIG_REC, sizeof(orig_t), false);
      op->orig = subg;
    }
  }

  return proj;
}

/* subgInduce:
 * Project subgraphs of root graph on subgraph.
 * If non-empty, add to subgraph.
 */
static void subgInduce(Agraph_t *root, Agraph_t *g, int inCluster) {
  Agraph_t *subg;
  Agraph_t *proj;
  int in_cluster;

  for (subg = agfstsubg(root); subg; subg = agnxtsubg(subg)) {
    if (GD_cc_subg(subg))
      continue;
    if ((proj = projectG(subg, g, inCluster))) {
      in_cluster = (inCluster || is_a_cluster(subg));
      subgInduce(subg, proj, in_cluster);
    }
  }
}

static void subGInduce(Agraph_t *g, Agraph_t *out) { subgInduce(g, out, 0); }

/* cccomps:
 * Decompose g into "connected" components, where nodes are connected
 * either by an edge or by being in the same cluster. The components
 * are returned in an array of subgraphs. ncc indicates how many components
 * there are. The subgraphs use the prefix pfx in their names, if non-NULL.
 * Note that cluster subgraph of the main graph, corresponding to a component,
 * is cloned within the subgraph. Each cloned cluster contains a record pointing
 * to the real cluster.
 */
Agraph_t **cccomps(Agraph_t *g, size_t *ncc, char *pfx) {
  Agraph_t *dg;
  size_t n_cnt, e_cnt;
  agxbuf name = {0};
  Agraph_t *out;
  Agraph_t *dout;
  Agnode_t *dn;
  stk_t stk;
  int sz = (int)sizeof(ccgraphinfo_t);

  if (agnnodes(g) == 0) {
    *ncc = 0;
    return NULL;
  }

  /* Bind ccgraphinfo to graph and all subgraphs */
  aginit(g, AGRAPH, GRECNAME, -sz, false);

  /* Bind ccgraphinfo to graph and all subgraphs */
  aginit(g, AGNODE, NRECNAME, sizeof(ccgnodeinfo_t), false);

  dg = deriveGraph(g);

  size_t ccs_length = (size_t)agnnodes(dg);
  LIST(Agraph_t *) ccs = {0};
  LIST_RESERVE(&ccs, ccs_length);
  initStk(&stk, insertFn, clMarkFn);

  for (dn = agfstnode(dg); dn; dn = agnxtnode(dg, dn)) {
    if (marked(&stk, dn))
      continue;
    setPrefix(&name, pfx);
    agxbprint(&name, "%" PRISIZE_T, LIST_SIZE(&ccs));
    char *name_str = agxbuse(&name);
    dout = agsubg(dg, name_str, 1);
    out = agsubg(g, name_str, 1);
    agbindrec(out, GRECNAME, sizeof(ccgraphinfo_t), false);
    GD_cc_subg(out) = 1;
    n_cnt = dfs(dg, dn, dout, &stk);
    unionNodes(dout, out);
    e_cnt = graphviz_node_induce(out, NULL);
    subGInduce(g, out);
    LIST_APPEND(&ccs, out);
    agdelete(dg, dout);
    if (Verbose)
      fprintf(stderr,
              "(%4" PRISIZE_T ") %7" PRISIZE_T " nodes %7" PRISIZE_T " edges\n",
              LIST_SIZE(&ccs) - 1, n_cnt, e_cnt);
  }

  if (Verbose)
    fprintf(stderr,
            "       %7d nodes %7d edges %7" PRISIZE_T " components %s\n",
            agnnodes(g), agnedges(g), LIST_SIZE(&ccs), agnameof(g));

  agclose(dg);
  agclean(g, AGRAPH, GRECNAME);
  agclean(g, AGNODE, NRECNAME);
  freeStk(&stk);
  agxbfree(&name);
  Agraph_t **ret;
  LIST_DETACH(&ccs, &ret, ncc);
  return ret;
}

/* isConnected:
 * Returns 1 if the graph is connected.
 * Returns 0 if the graph is not connected.
 * Returns -1 if the graph is error.
 */
int isConnected(Agraph_t *g) {
  Agnode_t *n;
  int ret = 1;
  size_t cnt = 0;
  stk_t stk;

  if (agnnodes(g) == 0)
    return 1;

  initStk(&stk, NULL, markFn);
  for (n = agfstnode(g); n; n = agnxtnode(g, n))
    unmark(&stk, n);

  n = agfstnode(g);
  cnt = dfs(g, agfstnode(g), NULL, &stk);
  freeStk(&stk);
  if (cnt != (size_t)agnnodes(g))
    ret = 0;
  return ret;
}