3
3
int cs_dfs (int j, cs *G, int top, int *xi, int *pstack, const int *pinv)
5
5
int i, p, p2, done, jnew, head = 0, *Gp, *Gi ;
6
if (!CS_CSC (G) || !xi || !pstack) return (-1) ; /* check inputs */
6
if (!CS_CSC (G) || !xi || !pstack) return (-1) ; /* check inputs */
7
7
Gp = G->p ; Gi = G->i ;
8
xi [0] = j ; /* initialize the recursion stack */
8
xi [0] = j ; /* initialize the recursion stack */
11
j = xi [head] ; /* get j from the top of the recursion stack */
12
jnew = pinv ? (pinv [j]) : j ;
13
if (!CS_MARKED (Gp, j))
15
CS_MARK (Gp, j) ; /* mark node j as visited */
16
pstack [head] = (jnew < 0) ? 0 : CS_UNFLIP (Gp [jnew]) ;
18
done = 1 ; /* node j done if no unvisited neighbors */
19
p2 = (jnew < 0) ? 0 : CS_UNFLIP (Gp [jnew+1]) ;
20
for (p = pstack [head] ; p < p2 ; p++) /* examine all neighbors of j */
22
i = Gi [p] ; /* consider neighbor node i */
23
if (CS_MARKED (Gp, i)) continue ; /* skip visited node i */
24
pstack [head] = p ; /* pause depth-first search of node j */
25
xi [++head] = i ; /* start dfs at node i */
26
done = 0 ; /* node j is not done */
27
break ; /* break, to start dfs (i) */
29
if (done) /* depth-first search at node j is done */
31
head-- ; /* remove j from the recursion stack */
32
xi [--top] = j ; /* and place in the output stack */
11
j = xi [head] ; /* get j from the top of the recursion stack */
12
jnew = pinv ? (pinv [j]) : j ;
13
if (!CS_MARKED (Gp, j))
15
CS_MARK (Gp, j) ; /* mark node j as visited */
16
pstack [head] = (jnew < 0) ? 0 : CS_UNFLIP (Gp [jnew]) ;
18
done = 1 ; /* node j done if no unvisited neighbors */
19
p2 = (jnew < 0) ? 0 : CS_UNFLIP (Gp [jnew+1]) ;
20
for (p = pstack [head] ; p < p2 ; p++) /* examine all neighbors of j */
22
i = Gi [p] ; /* consider neighbor node i */
23
if (CS_MARKED (Gp, i)) continue ; /* skip visited node i */
24
pstack [head] = p ; /* pause depth-first search of node j */
25
xi [++head] = i ; /* start dfs at node i */
26
done = 0 ; /* node j is not done */
27
break ; /* break, to start dfs (i) */
29
if (done) /* depth-first search at node j is done */
31
head-- ; /* remove j from the recursion stack */
32
xi [--top] = j ; /* and place in the output stack */