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
|
/* $Id: blocktree.c,v 1.1.1.1 2004/12/23 04:04:29 ellson Exp $ $Revision: 1.1.1.1 $ */
/* 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 <blocktree.h>
static int min_value(int x, int y)
{
if (x < y)
return x;
return y;
}
static void addNode(block_t * bp, Agnode_t * n)
{
aginsert(bp->sub_graph, n);
SET_BCDONE(n);
BLOCK(n) = bp;
}
static Agraph_t *makeBlockGraph(Agraph_t * g, circ_state * state)
{
char name[SMALLBUF];
Agraph_t *subg;
sprintf(name, "_block_%d", state->blockCount++);
subg = agsubg(g, name);
return subg;
}
static block_t *makeBlock(Agraph_t * g, circ_state * state)
{
Agraph_t *subg = makeBlockGraph(g, state);
block_t *bp = mkBlock(subg);
return bp;
}
/* dfs:
*
* Current scheme adds articulation point to first non-trivial child
* block. If none exists, it will be added to its parent's block, if
* non-trivial, or else given its own block.
*
* FIX:
* This should be modified to:
* - allow user to specify which block gets a node, perhaps on per-node basis.
* - if an articulation point is not used in one of its non-trivial blocks,
* dummy edges should be added to preserve biconnectivity
* - turn on user-supplied blocks.
*
*/
static void dfs(Agraph_t * g, Agnode_t * n, circ_state * state, int isRoot)
{
Agedge_t *e;
Agnode_t *curtop;
LOWVAL(n) = VAL(n) = state->orderCount++;
stackPush(state->bcstack, n);
for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
Agnode_t *neighbor = e->head;
if (neighbor == n)
neighbor = e->tail;
if (neighbor == PARENT(n))
continue;
if (VAL(neighbor)) {
LOWVAL(n) = min_value(LOWVAL(n), VAL(neighbor));
continue;
}
if (!stackCheck(state->bcstack, n)) {
stackPush(state->bcstack, n);
}
PARENT(neighbor) = n;
curtop = top(state->bcstack);
dfs(g, neighbor, state, 0);
LOWVAL(n) = min_value(LOWVAL(n), LOWVAL(neighbor));
if (LOWVAL(neighbor) >= VAL(n)) {
block_t *block = NULL;
Agnode_t *np;
if (top(state->bcstack) != curtop)
do {
np = stackPop(state->bcstack);
if (!BCDONE(np)) {
if (!block)
block = makeBlock(g, state);
addNode(block, np);
}
} while (np != n);
if (block) { /* If block != NULL, it's not empty */
if (isRoot && (BLOCK(n) == block))
insertBlock(&state->bl, block);
else
appendBlock(&state->bl, block);
}
if ((LOWVAL(n) < VAL(n)) && (!stackCheck(state->bcstack, n))) {
stackPush(state->bcstack, n);
}
}
}
if ((LOWVAL(n) == VAL(n)) && !BCDONE(n)) {
block_t *block = makeBlock(g, state);
stackPop(state->bcstack);
addNode(block, n);
if (isRoot)
insertBlock(&state->bl, block);
else
appendBlock(&state->bl, block);
}
}
#ifdef USER_BLOCKS
/* findUnvisited:
* Look for unvisited node n in visited block (i.e., some nodes in
* the block have been visited) with neighbor not in block. Note
* that therefore neighbor is unvisited. Set neighbor's parent to n
* and return neighbor.
* Guaranteed blp is non-empty.
*
*/
static Agnode_t *findUnvisited(blocklist_t * blp)
{
Agnode_t *retn = NULL;
FIX:finish Agnode_t * n;
Agnode_t *newn;
graph_t *clust_subg;
edge_t *e;
block_t *bp;
block_t *prev = NULL;
for (bp = blp->first; prev != blp->last; bp = bp->next) {
prev = bp;
clust = bp->sub_graph;
if (DONE(bp))
continue;
if (PARTIAL(bp)) {
for (n = agfstnode(clust); n; n = agnxtnode(clust, n)) {
if (!VISITED(n)) {
for (e = agfstedge(g, n); e; e = agnxtedge(g, e, n)) {
newn = e->head;
if (newn == n)
newn = e->tail;
if ((BLOCK(newn) != bp)) {
retn = newn;
return;
}
}
/* mark n visited */
}
}
/* mark bp done */
} else {
}
}
return retn;
}
#endif
/* find_blocks:
*/
static void find_blocks(Agraph_t * g, circ_state * state)
{
Agnode_t *n;
Agnode_t *root = NULL;
block_t *rootBlock = NULL;
blocklist_t ublks;
#ifdef USER_BLOCKS
graph_t *clust_subg;
graph_t *mg;
edge_t *me;
node_t *mm;
int isRoot;
#endif
initBlocklist(&ublks);
/* check to see if there is a node which is set to be the root
*/
if (state->rootname) {
root = agfindnode(g, state->rootname);
}
if (!root && state->N_root) {
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
if (late_bool(ORIGN(n), state->N_root, 0)) {
root = n;
break;
}
}
}
#ifdef USER_BLOCKS
/* process clusters first */
/* by construction, all subgraphs are blocks and are non-empty */
mm = g->meta_node;
mg = mm->graph;
for (me = agfstout(mg, mm); me; me = agnxtout(mg, me)) {
block_t *block;
clust_subg = agusergraph(me->head);
isRoot = 0;
block = mkBlock(clust_subg);
/* block = makeBlock(g, state); */
for (n = agfstnode(clust_subg); n; n = agnxtnode(clust_subg, n)) {
if (!BCDONE(n)) { /* test not necessary if blocks disjoint */
SET_BCDONE(n);
BLOCK(n) = block;
if (n == root)
isRoot = 1;
}
}
if (isRoot) {
/* Assume blocks are disjoint, so don't check if rootBlock is
* already assigned.
*/
rootBlock = block;
insertBlock(&state->bl, block);
} else {
appendBlock(&state->bl, block);
}
}
ublks.first = state->bl.first;
ublks.last = state->bl.last;
#endif
if (!root)
root = agfstnode(g);
dfs(g, root, state, !rootBlock);
#ifdef USER_BLOCKS
/* If g has user-supplied blocks, it may be disconnected.
* We then fall into the following ugly loop.
* We are guaranteed !VISITED(n) and PARENT(n) has been
* set to a visited node.
*/
if (ublks.first) {
while (n = findUnvisited(&ublks)) {
dfs(g, n, state, 0);
}
}
#endif
}
/* create_block_tree:
* Construct block tree by peeling nodes from block list in state.
* When done, return root. The block list is empty
* FIX: use largest block as root
*/
block_t *createBlocktree(Agraph_t * g, circ_state * state)
{
block_t *bp;
block_t *next;
block_t *root;
int min;
/* int ordercnt; */
find_blocks(g, state);
bp = state->bl.first; /* if root chosen, will be first */
/* Otherwise, just pick first as root */
root = bp;
/* Find node with minimum VAL value to find parent block */
/* FIX: Should be some way to avoid search below. */
/* ordercnt = state->orderCount; */
for (bp = bp->next; bp; bp = next) {
Agnode_t *n;
Agnode_t *parent;
Agnode_t *child;
Agraph_t *subg = bp->sub_graph;
child = n = agfstnode(subg);
min = VAL(n);
parent = PARENT(n);
for (n = agnxtnode(subg, n); n; n = agnxtnode(subg, n)) {
if (VAL(n) < min) {
child = n;
min = VAL(n);
parent = PARENT(n);
}
}
SET_PARENT(parent);
CHILD(bp) = child;
next = bp->next; /* save next since list insertion destroys it */
appendBlock(&(BLOCK(parent)->children), bp);
}
initBlocklist(&state->bl); /* zero out list */
return root;
}
void freeBlocktree(block_t * bp)
{
block_t *child;
block_t *next;
for (child = bp->children.first; child; child = next) {
next = child->next;
freeBlocktree(child);
}
freeBlock(bp);
}
#ifdef DEBUG
static void indent(int i)
{
while (i--)
fputs(" ", stderr);
}
void print_blocktree(block_t * sn, int depth)
{
block_t *child;
Agnode_t *n;
Agraph_t *g;
indent(depth);
g = sn->sub_graph;
fprintf(stderr, "%s:", g->name);
for (n = agfstnode(g); n; n = agnxtnode(g, n)) {
fprintf(stderr, " %s", n->name);
}
fputs("\n", stderr);
depth++;
for (child = sn->children.first; child; child = child->next) {
print_blocktree(child, depth);
}
}
#endif
|