Ejemplo n.º 1
0
        private void Dfs(Digraph digraph, int vertex)
        {
            onStack[vertex] = true;
            marked[vertex]  = true;

            foreach (var headVertex in digraph.Adjacency(vertex))
            {
                if (HasCycle())
                {
                    return;
                }
                else if (!marked[headVertex])
                {
                    edgeTo[headVertex] = vertex;
                    Dfs(digraph, headVertex);
                }
                else if (onStack[headVertex])
                {
                    cycle = new Stack <int>();
                    for (int x = vertex; x != headVertex; x = edgeTo[x])
                    {
                        cycle.Push(x);
                    }

                    cycle.Push(headVertex);
                    cycle.Push(vertex);
                }
            }
            onStack[vertex] = false;
        }
Ejemplo n.º 2
0
 private void DepthFirstSearch(Digraph digraph, int vertex)
 {
     marked[vertex] = true;
     foreach (var headVertex in digraph.Adjacency(vertex))
     {
         if (!marked[headVertex])
         {
             DepthFirstSearch(digraph, headVertex);
         }
     }
 }
Ejemplo n.º 3
0
 private void Dfs(Digraph graph, int vertex)
 {
     marked[vertex] = true;
     pre[vertex]    = preCounter++;
     preOrder.Enqueue(vertex);
     foreach (var w in graph.Adjacency(vertex))
     {
         if (!marked[w])
         {
             Dfs(graph, w);
         }
     }
     postOrder.Enqueue(vertex);
     post[vertex] = postCounter++;
 }