Exemple #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;
        }
Exemple #2
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;
         }
     }
 }
Exemple #3
0
 public EdgeWeightedDirectedCycle(EdgeWeightedDigraph graph)
 {
     marked  = new bool[graph.Vertices];
     onStack = new bool[graph.Vertices];
     edgeTo  = new DirectedEdge[graph.Vertices];
     for (int v = 0; v < graph.Vertices; v++)
     {
         if (!marked[v])
         {
             Dfs(graph, v);
         }
     }
 }
Exemple #4
0
 public EdgeWeightedCycleFinder(EdgeWeightedDigraph graph)
 {
     this.graph = graph;
     onStack    = new bool[graph.Vertices];
     edgeTo     = new DirectedEdge[graph.Vertices];
     marked     = new bool[graph.Vertices];
     for (int i = 0; i < graph.Vertices; ++i)
     {
         if (!marked[i] && !HasCycle())
         {
             Dfs(i);
         }
     }
 }
        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;
            }
        }
Exemple #6
0
        private void FindNegativeCycle()
        {
            int vertex = EdgeTo.Length;
            var spt    = new EdgeWeightedDigraph(vertex);

            for (int v = 0; v < vertex; v++)
            {
                if (EdgeTo[v] != null)
                {
                    spt.AddEdge(EdgeTo[v]);
                }
            }

            var cf = new EdgeWeightedCycleFinder(spt);

            cycle = cf.Cycle();
        }
Exemple #7
0
        public AcyclicSP(EdgeWeightedDigraph graph, int startVertex)
        {
            edgeTo = new DirectedEdge[graph.Vertices];
            distTo = new double[graph.Vertices];

            for (int v = 0; v < graph.Vertices; v++)
            {
                distTo[v] = double.MaxValue;
            }

            distTo[startVertex] = 0.0f;

            var topological = new Topological(graph);

            foreach (var v in topological.Order())
            {
                Relax(graph, v);
            }
        }
Exemple #8
0
        public DijkstraSP(EdgeWeightedDigraph graph, int startVertex)
        {
            edgeTo        = new DirectedEdge[graph.Vertices];
            distTo        = new double[graph.Vertices];
            priorityQueue = new IndexMinPQ <double>(graph.Vertices);

            for (int v = 0; v < graph.Vertices; v++)
            {
                distTo[v] = double.MaxValue;
            }

            distTo[startVertex] = 0.0f;

            priorityQueue.Insert(startVertex, 0.0f);

            while (!priorityQueue.IsEmpty())
            {
                Relax(graph, priorityQueue.DelMin());
            }
        }
Exemple #9
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]);
                    }
                }
            }
        }
Exemple #10
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();
                }
            }
        }
Exemple #11
0
        public BellmanFordSp(EdgeWeightedDigraph graph, int startVertex)
        {
            DistTo  = new double[graph.Vertices];
            EdgeTo  = new DirectedEdge[graph.Vertices];
            onQueue = new bool[graph.Vertices];
            queue   = new Queue <int>();

            for (int v = 0; v < graph.Vertices; v++)
            {
                DistTo[v] = double.MaxValue;
            }

            DistTo[startVertex] = 0.0f;
            queue.Enqueue(startVertex);
            onQueue[startVertex] = true;

            while (!queue.Any() && HasNegativeCycle())
            {
                int v = queue.Dequeue();
                onQueue[v] = false;
                Relax(graph, v);
            }
        }
Exemple #12
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]);
                }
            }
        }