在以下的深度优先搜索中,为什么ProcessEdge方法中的
if(Parent[currVertex] != successorVertex)
检查会检测到一个循环呢?此代码遵循S.Skiena的《算法设计手册》中给出的算法。这可能是一个笔误,并且应该是if(Parent[successorVertex] != currVertex)
。如有任何疑问,请询问。我真的被卡住了。 public void Search(int start)
{
/* NOTE: the differences from BFS are: this uses a stack instead of a queue AND this maintains 'time' variable */
Stack<int> s = new Stack<int>();
int currVertex;
int successorVertex;
int time = 0;
s.Push(start);
Discovered[start] = true;
while (s.Count != 0)
{
currVertex = s.Pop();
// time increments every time we enter a node (when discovered) and every time we exit a node (when processed_late, i.e. when all its neighbours have been processed)
time++;
EntryTime[currVertex] = time;
ProcessVertexEarly(currVertex);
Processed[currVertex] = true;
for (int i = 0; i < Graph.Vertices[currVertex].Count; i++)
{
successorVertex = Graph.Vertices[currVertex][i].Y;
if (!Processed[successorVertex] || Graph.IsDirected)
{
ProcessEdge(currVertex, successorVertex);
}
if (!Discovered[successorVertex])
{
s.Push(successorVertex);
Discovered[successorVertex] = true;
Parent[successorVertex] = currVertex;
}
}
// time increments every time we enter a node (when discovered) and every time we exit a node (when processed_late, i.e. when all its neighbours have been processed)
time++;
ExitTime[currVertex] = time;
ProcessVertexLate(currVertex);
}
}
private void ProcessEdge(int currVertex, int successorVertex)
{
if(Parent[currVertex] != successorVertex) // then we've found a cycle
{
/* Found cycle*/
}
}
更新
在勘误表http://www.cs.sunysb.edu/~skiena/algorist/book/errata中找到了此代码的更正内容。请看第173页的(*)process_edge过程 -- 正确的测试应该是:
if (discovered[y] && (parent[x] != y)) { /* found back edge */
但这样能检测到环吗?因为在 DFS 方法中,只有当 discovered[y] == false
时才会调用 process_edge
,所以 if 判断永远不会通过。
dfs
在y
未被发现或未被处理时调用process_edge
。只有当探索完离开顶点的每条边后,顶点才会被标记为已处理。 - David Eisenstat