private void scan(EdgeWeightedGraph g, int v)
 {
     marked[v] = true;
     foreach (Edge e in g.Adj(v))
     {
         int w = e.Other(v);
         if (marked[w])
         {
             continue;//v-w is obsolete edge
         }
         if (e.Weight() < distTo[w])
         {
             distTo[w] = e.Weight();
             edgeTo[w] = e;
             if (pq.Contains(w))
             {
                 pq.ChangeKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Example #2
0
        /// <summary>
        /// Dijkstra algorithm (shortest path) based on graph g for start vertice s. Positive cycles are allowed in shortest path algorithm
        /// </summary>
        /// <param name="g">Graph for search</param>
        /// <param name="s">Start vertice</param>
        public static PathStats ShortestPath(GraphBase g, int s)
        {
            var ps = new PathStats(g.V);

            for (var i = 0; i < ps.Dist.Length; i++)
            {
                ps.Dist[i] = int.MaxValue;
                ps.Prev[i] = -1;
            }

            ps.Dist[s] = 0;//start vertice

            var pq = new IndexMinPQ<Distance>(ps.Dist.Length);
            for (int i = 0; i < ps.Dist.Length; i++)
            {
                pq.Insert(i, new Distance { V = i, Dist = ps.Dist[i] });
            }

            while (!pq.IsEmpty())
            {
                var v = pq.DelRoot();

                foreach (var e in g.Adjacent(v))
                {
                    if (ps.Dist[e.V2] > ps.Dist[v] + e.Weight)
                    {
                        ps.Dist[e.V2] = ps.Dist[v] + e.Weight;
                        ps.Prev[e.V2] = v;
                        pq.ChangeKey(e.V2, new Distance { V = e.V2, Dist = ps.Dist[e.V2] });
                    }
                }
            }

            return ps;
        }
Example #3
0
        private void Visit(EdgeWeightedGraph graph, int vertex)
        {
            marked[vertex] = true;
            foreach (var edge in graph.Adj(vertex))
            {
                int otherVertex = edge.Other(vertex);
                if (marked[otherVertex])
                {
                    continue;
                }

                if (!(edge.Weight < distTo[otherVertex]))
                {
                    continue;
                }

                edgeTo[otherVertex] = edge;
                distTo[otherVertex] = edge.Weight;

                if (prioryQueue.Contains(otherVertex))
                {
                    prioryQueue.ChangeKey(otherVertex, distTo[otherVertex]);
                }
                else
                {
                    prioryQueue.Insert(otherVertex, distTo[otherVertex]);
                }
            }
        }
Example #4
0
 private void Visit(IEdgeWeightGraph g, int v)
 {
     marked[v] = true;
     foreach (var e in g.Adj(v))
     {
         int w = e.Other(v);
         if (marked[w])
         {
             continue;
         }
         if (e.Weight < distTo[w]) //更新最佳的边
         {
             edgeTo[w] = e;
             distTo[w] = e.Weight;
             if (pq.Contains(w))
             {
                 pq.ChangeKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Example #5
0
        public void IndexMinPQTest1()
        {
            const int    MaxSize  = 8;
            const double MinValue = 3.9;
            const double MaxValue = MinValue * MaxSize + 32;
            int          index;

            // MinValue index == 3, MaxValue index == 4
            double[] items = { MinValue * 2, MinValue * 3, MinValue * 4, MinValue, MaxValue, MinValue * 5, MinValue * 6, MinValue * 7 };
            StdRandom.Seed = 101;

            IndexMinPQ <double> pq = new IndexMinPQ <double>(MaxSize);

            index = StdRandom.Uniform(items.Length);
            Assert.IsFalse(pq.Contains(index));
            Assert.IsTrue(pq.IsEmpty);
            Assert.AreEqual(0, pq.Count);

            try
            {
                index = pq.DelMin();
                Assert.Fail("Failed to catch exception");
            }
            catch (InvalidOperationException) { }

            for (int i = 0; i < items.Length; i++)
            {
                pq.Insert(i, items[i]);
            }
            Assert.AreEqual(items.Length, pq.Count);
            Assert.AreEqual(MinValue, pq.MinKey);
            Assert.AreEqual(3, pq.MinIndex);
            Assert.AreEqual(MaxValue, pq.KeyOf(4));

            index = StdRandom.Uniform(items.Length);
            Assert.AreEqual(items[index], pq.KeyOf(index));

            pq.ChangeKey(1, pq.MinKey * 0.9); // make it the smallest item
            Assert.AreEqual(1, pq.MinIndex);

            pq.DecreaseKey(3, pq.MinKey * 0.87);
            Assert.AreEqual(3, pq.MinIndex);

            pq.Delete(3);
            Assert.AreNotEqual(3, pq.MinIndex);

            Assert.AreEqual(1, pq.DelMin());
        }
 private void Relax(IEdgeWeightedDIgraph g, int v)
 {
     foreach (var e in g.Adj(v))
     {
         int w = e.To;
         if (distTo[w] > distTo[v] + e.Weight)
         {
             distTo[w] = distTo[v] + e.Weight;
             edgeTo[w] = e;
             if (pq.Contains(w))
             {
                 pq.ChangeKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Example #7
0
 void Relax(EdgeWeightedDiagraph g, int v)
 {
     foreach (DirectedEdge e in g.AdjList(v))
     {
         int    w      = e.To();
         double weight = e.Weight();
         if (distTo[w] > distTo[v] + weight)
         {
             //relax
             edgeTo[w] = e;
             distTo[w] = distTo[v] + weight;
             if (pq.Contains(w))
             {
                 pq.ChangeKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Example #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]);
                }
            }
        }