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
|
/* $Id: ccomps.c,v 1.2 2005/10/18 18:42:59 ellson Exp $ $Revision: 1.2 $ */
/* vim:set shiftwidth=4 ts=8: */
/**********************************************************
* This software is part of the graphviz package *
* http://www.graphviz.org/ *
* *
* Copyright (c) 1994-2004 AT&T Corp. *
* and is licensed under the *
* Common Public License, Version 1.0 *
* by AT&T Corp. *
* *
* Information and Software Systems Research *
* AT&T Research, Florham Park NJ *
**********************************************************/
#include <render.h>
#include <pack.h>
#include <ctype.h>
#define MARKED(n) ((n)->u.mark)
#define MARK(n) ((n)->u.mark = 1)
#define UNMARK(n) ((n)->u.mark = 0)
typedef void (*dfsfn) (Agnode_t *, void *);
static void dfs(Agraph_t * g, Agnode_t * n, dfsfn action, void *state)
{
Agedge_t *e;
Agnode_t *other;
MARK(n);
action(n, state);
for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
if ((other = e->tail) == n)
other = e->head;
if (!MARKED(other))
dfs(g, other, action, state);
}
}
static int isLegal(char *p)
{
unsigned char c;
while ((c = *(unsigned char *) p++)) {
if ((c != '_') && !isalnum(c))
return 0;
}
return 1;
}
/* insertFn:
*/
static void insertFn(Agnode_t * n, void *state)
{
aginsert((Agraph_t *) state, n);
}
/* 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.
*/
Agraph_t **pccomps(Agraph_t * g, int *ncc, char *pfx, bool * pinned)
{
int c_cnt = 0;
char buffer[SMALLBUF];
char *name;
Agraph_t *out = 0;
Agnode_t *n;
Agraph_t **ccs;
int len;
int bnd = 10;
bool pin = FALSE;
if (agnnodes(g) == 0) {
*ncc = 0;
return 0;
}
if (!pfx || !isLegal(pfx)) {
pfx = "_cc_";
}
len = strlen(pfx);
if (len + 25 <= SMALLBUF)
name = buffer;
else
name = (char *) gmalloc(len + 25);
strcpy(name, pfx);
for (n = agfstnode(g); n; n = agnxtnode(g, n))
UNMARK(n);
ccs = N_GNEW(bnd, Agraph_t *);
/* Component with pinned nodes */
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
if (MARKED(n) || !isPinned(n))
continue;
if (!out) {
sprintf(name + len, "%d", c_cnt);
out = agsubg(g, name);
ccs[c_cnt] = out;
c_cnt++;
pin = TRUE;
}
dfs(g, n, insertFn, out);
}
/* Remaining nodes */
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
if (MARKED(n))
continue;
sprintf(name + len, "%d", c_cnt);
out = agsubg(g, name);
dfs(g, n, insertFn, out);
if (c_cnt == bnd) {
bnd *= 2;
ccs = RALLOC(bnd, ccs, Agraph_t *);
}
ccs[c_cnt] = out;
c_cnt++;
}
ccs = RALLOC(c_cnt, ccs, Agraph_t *);
if (name != buffer)
free(name);
*ncc = c_cnt;
*pinned = pin;
return ccs;
}
/* 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.
*/
Agraph_t **ccomps(Agraph_t * g, int *ncc, char *pfx)
{
int c_cnt = 0;
char buffer[SMALLBUF];
char *name;
Agraph_t *out;
Agnode_t *n;
Agraph_t **ccs;
int len;
int bnd = 10;
if (agnnodes(g) == 0) {
*ncc = 0;
return 0;
}
if (!pfx || !isLegal(pfx)) {
pfx = "_cc_";
}
len = strlen(pfx);
if (len + 25 <= SMALLBUF)
name = buffer;
else
name = (char *) gmalloc(len + 25);
strcpy(name, pfx);
for (n = agfstnode(g); n; n = agnxtnode(g, n))
UNMARK(n);
ccs = N_GNEW(bnd, Agraph_t *);
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
if (MARKED(n))
continue;
sprintf(name + len, "%d", c_cnt);
out = agsubg(g, name);
dfs(g, n, insertFn, out);
if (c_cnt == bnd) {
bnd *= 2;
ccs = RALLOC(bnd, ccs, Agraph_t *);
}
ccs[c_cnt] = out;
c_cnt++;
}
ccs = RALLOC(c_cnt, ccs, Agraph_t *);
if (name != buffer)
free(name);
*ncc = c_cnt;
return ccs;
}
/* cntFn:
*/
static void cntFn(Agnode_t * n, void *s)
{
*(int *) s += 1;
}
/* isConnected:
* Returns true if the graph is connected.
*/
int isConnected(Agraph_t * g)
{
Agnode_t *n;
int ret = 1;
int cnt = 0;
for (n = agfstnode(g); n; n = agnxtnode(g, n))
UNMARK(n);
n = agfstnode(g);
if (n) {
dfs(g, n, cntFn, &cnt);
if (cnt != agnnodes(g))
ret = 0;
}
return ret;
}
/* nodeInduce:
* Given a subgraph, adds all edges in the root graph both of whose
* endpoints are in the subgraph.
* If g is a connected component, this will be all edges attached to
* any node in g.
* Returns the number of edges added.
*/
int nodeInduce(Agraph_t * g)
{
Agnode_t *n;
Agraph_t *root = g->root;
Agedge_t *e;
int e_cnt = 0;
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
for (e = agfstout(root, n); e; e = agnxtout(root, e)) {
if (agcontains(g, e->head)) { /* test will always be true */
aginsert(g, e); /* for connected component */
e_cnt++;
}
}
}
return e_cnt;
}
|