Exemplo n.º 1
0
        private void Dfs(EdgeWeightedDigraph graph, int vertex)
        {
            onStack[vertex] = true;
            marked[vertex]  = true;
            foreach (DirectedEdge edge in graph.Adj(vertex))
            {
                int w = edge.DestinationVertex;

                if (Cycle != null)
                {
                    return;
                }
                else if (!marked[w])
                {
                    edgeTo[w] = edge;
                    Dfs(graph, w);
                }
                else if (onStack[w])
                {
                    Cycle = new Stack <DirectedEdge>();

                    DirectedEdge f = edge;
                    while (f.StartVertex != w)
                    {
                        Cycle.Push(f);
                        f = edgeTo[f.StartVertex];
                    }
                    Cycle.Push(f);

                    return;
                }
            }

            onStack[vertex] = false;
        }
Exemplo n.º 2
0
 private void Dfs(int v)
 {
     marked[v]  = true;
     onStack[v] = true;
     foreach (var e in graph.Adj(v))
     {
         if (hasCycle)
         {
             break;
         }
         int w = e.DestinationVertex;
         if (onStack[w])
         {
             hasCycle = true;
             cycle    = new Stack <DirectedEdge>();
             int p = v;
             cycle.Push(e);
             while (p != w)
             {
                 cycle.Push(edgeTo[p]);
                 p = edgeTo[p].StartVertex;
             }
         }
         else
         {
             if (!marked[w])
             {
                 edgeTo[w] = e;
                 Dfs(w);
             }
         }
     }
     onStack[v] = false;
 }
Exemplo n.º 3
0
 private void Relax(EdgeWeightedDigraph graph, int vertex)
 {
     foreach (var edge in graph.Adj(vertex))
     {
         int w = edge.DestinationVertex;
         if (distTo[w] > distTo[vertex] + edge.Weight)
         {
             distTo[w] = distTo[vertex] + edge.Weight;
             edgeTo[w] = edge;
         }
     }
 }
Exemplo n.º 4
0
        private void Relax(EdgeWeightedDigraph graph, int vertex)
        {
            foreach (var edge in graph.Adj(vertex))
            {
                int w = edge.DestinationVertex;

                if (!(DistTo[w] > DistTo[vertex] + edge.Weight))
                {
                    continue;
                }

                DistTo[w] = DistTo[vertex] + edge.Weight;
                EdgeTo[w] = edge;
            }
        }
Exemplo n.º 5
0
        private void Relax(EdgeWeightedDigraph graph, int vertex)
        {
            foreach (var edge in graph.Adj(vertex))
            {
                int w = edge.DestinationVertex;
                if (distTo[w] > distTo[vertex] + edge.Weight)
                {
                    distTo[w] = distTo[vertex] + edge.Weight;
                    edgeTo[w] = edge;

                    if (priorityQueue.Contains(w))
                    {
                        priorityQueue.Change(w, distTo[w]);
                    }
                    else
                    {
                        priorityQueue.Insert(w, distTo[w]);
                    }
                }
            }
        }
Exemplo n.º 6
0
        private void Relax(EdgeWeightedDigraph graph, int vertex)
        {
            foreach (var edge in graph.Adj(vertex))
            {
                int w = edge.DestinationVertex;
                if (DistTo[w] > DistTo[vertex] + edge.Weight)
                {
                    DistTo[w] = DistTo[vertex] + edge.Weight;
                    EdgeTo[w] = edge;

                    if (!onQueue[w])
                    {
                        queue.Enqueue(w);
                        onQueue[w] = true;
                    }
                }
                if (cost++ % graph.Vertices == 0)
                {
                    FindNegativeCycle();
                }
            }
        }
        private void Dfs(int vertex)
        {
            marked[vertex]  = true;
            onStack[vertex] = true;
            foreach (var e in graph.Adj(vertex))
            {
                if (hasCycle)
                {
                    break;
                }

                int destinationVertex = e.DestinationVertex;
                if (onStack[destinationVertex])
                {
                    hasCycle = true;
                    cycle    = new Stack <DirectedEdge>();
                    int tmpVertex = vertex;
                    cycle.Push(e);
                    while (tmpVertex != destinationVertex)
                    {
                        cycle.Push(edgeTo[tmpVertex]);
                        tmpVertex = edgeTo[tmpVertex].StartVertex;
                    }
                }
                else
                {
                    if (marked[destinationVertex])
                    {
                        continue;
                    }

                    edgeTo[destinationVertex] = e;
                    Dfs(destinationVertex);
                }
            }
            onStack[vertex] = false;
        }
Exemplo n.º 8
0
        private void Relax(EdgeWeightedDigraph graph, int vertex)
        {
            foreach (var edge in graph.Adj(vertex))
            {
                int w = edge.DestinationVertex;

                if (!(DistTo[w] > DistTo[vertex] + edge.Weight))
                {
                    continue;
                }

                DistTo[w] = DistTo[vertex] + edge.Weight;
                EdgeTo[w] = edge;

                if (priorityQueue.Contains(w))
                {
                    priorityQueue.ChangeKey(w, DistTo[w]);
                }
                else
                {
                    priorityQueue.Insert(w, DistTo[w]);
                }
            }
        }