The algorithm development guide describes BFS and DFS quite well. The code for dfs in the book has a problem in deciding whether to avoid double-edged edges. I found Errata and applied errors to the code, but, nevertheless, I think that the advanced code has the problem of checking the edges of double processing.
I insert the refined code as follows:
dfs(graph *g, int v) {
edgenode *p;
int y;
if (finished) return;
discovered[v] = TRUE;
time = time + 1;
entry_time[v] = time;
process_vertex_early(v);
p = g->edges[v];
while (p != NULL) {
y = p->y;
if (discovered[y] == FALSE) {
parent[y] = v;
process_edge(v,y);
dfs(g,y);
}
else if (**(!processed[y] && parent[v] != y)** || (g->directed))
process_edge(v,y);
if (finished) return;
p = p->next;
}
process_vertex_late(v);
time = time + 1;
exit_time[v] = time;
processed[v] = TRUE;
}
The place where I think there is a problem is marked with ** **.
Thus, a dubious place is one of the conditions. Suppose this is an undirected graph, so we can simply ignore the condition (g->directed).
, parent[v] != y. parent[v] == y, , v- > y , y- > v y.
parent[v] != y, , !processed[y] && .
, y v , processed[y] == false, , y ( else if), , y v , , .
, , processed[y] == true? , "" , .
? , , processed[y] . , , y, , , ? , , " " v?
, dfs , ?
, , !processed[y], .