コード例 #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;
        }
コード例 #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;
 }
コード例 #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;
         }
     }
 }
コード例 #4
0
ファイル: AcyclicSP.cs プロジェクト: PizzaServices/Algorithms
        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;
            }
        }
コード例 #5
0
ファイル: DijkstraSP.cs プロジェクト: wubh110/Algorithms
        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]);
                    }
                }
            }
        }
コード例 #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();
                }
            }
        }
コード例 #7
0
        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;
        }
コード例 #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]);
                }
            }
        }