Page 291 Exercise 5
This version uses an array based version of the stack for ease in programming.
int top = -1; int stack[MAX_NODES][2]; void add(int,int); void delete(int *,int *); |
void add(int x, int y)
{
stack[++top][0] = x;
stack[top][1] = y;
}
|
void delete(int *x, int *y)
{
*x = stack[top][0];
*y = stack[top--][1];
} |
void bicon(int u, int v)
{
/* compute dfn and low, and output the edges of G by their
biconnected components, v is the parent (if any) of the spanning
tree of u. dfn[0] to dfn[MAX_SIZE] is initialized to -1, and
num is set to 0 before the function begins */
NodePointer ptr;
int w,x,y;
dfn[u] = low[u] = num++;
for (ptr = graph[u]; ptr; ptr = ptr->link) {
w = ptr->vertex;
if (v != w && dfn[w] < dfn[u])
add(u,w);
if (dfn[w] <0) { /* w has not been visited */
bicon(w,u);
low[u] = MIN2(low[u],low[w]);
if (low[w] >= dfn[u]) {
printf("New biconnected component: ");
do {
delete(&x,&y);
printf("\t<%2d,%2d>",x,y);
} while (!((x == u) && (y == w)));
printf("\n");
}
}
else if (w != v)
low[u] = MIN2(low[u],dfn[w]);
}
} |