private void dfs(Digraph g, int v)
        {
            onStack[v] = true;
            marked[v] = true;

            foreach (int w in g.Adj(v))
            {
                if (this.HasCycle()) return;
                else if (!marked[w])
                {
                    edgeTo[w] = v; dfs(g, w);
                }
                else if (onStack[w])
                {
                    cycle = new Stack<int>();

                    for (int x = v; x != w; x = edgeTo[x])
                        cycle.Push(x);
                    cycle.Push(w);
                    cycle.Push(v);
                }
            }

            onStack[v] = false;
        }
Exemplo n.º 2
0
 private void dfs(Digraph graph, int v)
 {
     foreach (int w in graph.Adj(v))
     {
         if (!marked[w])
         {
             dfs(graph, w);
         }
     }
 }
        private void dfs(Digraph graph, int v)
        {
            pre.Enqueue(v);
            marked[v] = true;

            foreach (int w in graph.Adj(v))
            {
                if (!marked[w])
                {
                    edgeTo[w] = v;
                    dfs(graph, w);
                }
            }

            post.Enqueue(v);
            reversPost.Push(v);
        }
        private void bfs(Digraph graph, int s)
        {
            Queue<int> queue = new Queue<int>();
            marked[s] = true;
            queue.Enqueue(s);
            while (queue.Any())
            {
                int v = queue.Dequeue();

                foreach (int w in graph.Adj(v))
                {
                    if (!marked[w])
                    {
                        edgeTo[w] = v;
                        marked[w] = true;
                        queue.Enqueue(w);
                    }
                }
            }
        }