3

给定以下深度优先搜索,为什么if(Parent[currVertex] != successorVertex)ProcessEdge 方法中的检查会检测到循环?此代码遵循 S.Skiena 的 Algortim Design Manual 一书中给出的算法。支票可能是一个错字,应该是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 */

但这会检测周期吗?if 检查永远不会通过,因为在 DFS 方法中,process_edge仅在 时调用discovered[y] == false

4

1 回答 1

1

您发布的代码与 Skiena 的原始代码存在显着差异:bfs-dfs.cfindcycle.c以及其余部分。Skiena 的代码有问题(试试图 3 2 1 2 2 3,一条两条边路径),所以也许将它音译成 Java 的人尝试了一些修复。不幸的是,修复后的版本似乎也有问题,虽然我不能确定没有完整的程序。

我相信您突出显示的那行的意图如下。对于无向图中的深度优先搜索,有两种类型的边,treeback。当且仅当存在后边时,该图才具有循环。现在,Skiena 选择的无向图表示是将每个无向边存储为两个有向弧,每个方向一个。如果我们使用深度优先搜索来检测这个有向图中的循环,那么由对应于单个无向边的两条有向弧组成的长度为 2 的循环将错误地报告为循环。如所写,检查确保候选后弧y->x不是树弧的反向x->y

于 2013-08-25T13:52:22.260 回答