Beispiel #1
0
 private void Dfs(EdgeWeightedDigraph G, int v) // v is start vertex here
 {
     onStack[v] = true;
     marked[v]  = true;
     foreach (DirectedEdge e in G.GetAdj(v))
     {
         int w = e.To();
         // short circuit if directed cycle found
         if (cycle != null)
         {
             return;
         }
         // found new vertex, so recur
         else if (!marked[w])
         {
             edgeTo[w] = v;
             Dfs(G, w);
         }
         // trace back directed cycle
         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);
             // Debug.Assert(Check());
         }
     }
     onStack[v] = false;
 }
Beispiel #2
0
        public DijkstraSP(EdgeWeightedDigraph G, int s)
        {
            foreach (DirectedEdge e in G.GetEdges())
            {
                if (e.GetWeight() < 0)
                {
                    throw new ArgumentException("Negative weight");
                }
            }
            distTo = new double[G.GetVertices()];
            edgeTo = new DirectedEdge[G.GetVertices()];
            ValidateVertex(s);
            for (int v = 0; v < G.GetVertices(); v++)
            {
                distTo[v] = double.PositiveInfinity;
            }
            distTo[s] = 0.0;
            // relax vertices in order of distance from s
            Comparer <double> comparer = Comparer <double> .Default;

            pq = new IndexMinPQ <Double>(G.GetVertices(), comparer);
            pq.Insert(s, distTo[s]);
            while (!pq.IsEmpty())
            {
                pq.DelMin(out int index, out double key);
                int v = index;
                foreach (DirectedEdge e in G.GetAdj(v))
                {
                    Relax(e);
                }
            }
        }
Beispiel #3
0
 // relax vertex v and put other endpoints on queue if changed
 private void Relax(EdgeWeightedDigraph G, int v)
 {
     //for (DirectedEdge e : G.adj(v))
     foreach (DirectedEdge e in G.GetAdj(v))
     {
         int w = e.To();
         if (distTo[w] > distTo[v] + e.GetWeight())
         {
             distTo[w] = distTo[v] + e.GetWeight();
             edgeTo[w] = e;
             if (!onQueue[w])
             {
                 queue.Enqueue(w);
                 onQueue[w] = true;
             }
         }
         if (cost++ % G.GetVertices() == 0)
         {
             FindNegativeCycle();
             if (IsNegativeCycle())
             {
                 return;                     // found a negative cycle
             }
         }
     }
 }
Beispiel #4
0
 private void Dfs(EdgeWeightedDigraph G, int v)
 {
     marked[v] = true;
     Pre.Enqueue(v);
     foreach (DirectedEdge e in G.GetAdj(v))
     {
         int w = e.To();
         if (!marked[w])
         {
             Dfs(G, w);
         }
     }
     Post.Enqueue(v);
     ReversePost.Push(v);
 }
Beispiel #5
0
        // check that algorithm computes either the topological order or finds a directed cycle
        private void Dfs(EdgeWeightedDigraph G, int v)
        {
            onStack[v] = true;
            marked[v]  = true;
            foreach (DirectedEdge e in G.GetAdj(v))
            {
                int w = e.To();
                // short circuit if directed cycle found
                if (cycle != null)
                {
                    return;
                }
                // found new vertex, so recur
                else if (!marked[w])
                {
                    edgeTo[w] = e;
                    Dfs(G, w);
                }

                // trace back directed cycle
                else if (onStack[w])
                {
                    cycle = new Stack <DirectedEdge>();

                    DirectedEdge f = e;
                    while (f.From() != w)
                    {
                        cycle.Push(f);
                        f = edgeTo[f.From()];
                    }
                    cycle.Push(f);
                    return;
                }
            }
            onStack[v] = false;
        }
Beispiel #6
0
        public AcyclicSP(EdgeWeightedDigraph G, int s)
        {
            distTo = new double[G.GetVertices()];
            edgeTo = new DirectedEdge[G.GetVertices()];
            for (int v = 0; v < G.GetVertices(); v++)
            {
                distTo[v] = Double.PositiveInfinity;
            }
            distTo[s] = 0.0;
            ValidateVertex(s);
            Topological topological = new Topological(G);

            if (!topological.IsOrder())
            {
                throw new ArgumentException("Digraph is not acyclic.");
            }
            foreach (int v in topological.ReversePost)
            {
                foreach (DirectedEdge e in G.GetAdj(v))
                {
                    Relax(e);
                }
            }
        }