Graph - How to avoid reprocessing one edge twice in Depth First Search?

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) {
             /* temporary pointer */
             /* successor vertex */
             /* allow for search termination */
             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], .

+5
1

, processed[y] == true true.

[] DFS:

v0,v1,v2

graph example

v0 , dfs(v1) (v0,v1). v1 (v1,v2) dfs(v2). v2 , v0 else if , (v0,v2) , parent[v2] != v0 [v2 v1].

, , v0, "" - v2. v2 , else if, v2 v0, !processed[y] (v0,v2) .

, - hapenning, , processed[y] == true

+5

All Articles