Beispiel #1
0
        private void dfs(GraphDigraph g, int v)
        {
            // 将顶点v设置成已搜索
            this.markeds[v] = true;
            // 把当前顶点进栈
            this.onStack[v] = true;
            // 进行深度搜索
            IEnumerator ie = g.adjGet(v).GetEnumerator();

            while (ie.MoveNext())
            {
                int w = (int)ie.Current;
                if (!this.markeds[w])
                {
                    dfs(g, w);
                }
                else
                {
                    // 判断当前顶点是否已经在栈中, true表示是环,
                    if (this.onStack[w])
                    {
                        this.hasCycleBool = true;
                        return;
                    }
                }
            }
            // 把当前顶点出栈
            this.onStack[v] = false;
        }
Beispiel #2
0
        /// <summary>
        /// 基于深度优先搜索,生成顶点线性排序
        /// </summary>
        /// <param name="g"></param>
        /// <param name="v"></param>
        private void dfs(GraphDigraph g, int v)
        {
            // 标记当前v已经被搜索
            this.markeds[v] = true;
            // 通过循环深度搜索v
            IEnumerator ie = g.adjGet(v).GetEnumerator();

            while (ie.MoveNext())
            {
                int w = (int)ie.Current;
                if (!this.markeds[w])
                {
                    dfs(g, w);
                }
            }
            // 让当前这个顶点进栈
            this.reversePostStack.push(v);
        }