Example #1
0
 // scan vertex v
 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.DecreaseKey(w, distTo[w]);
             }
             else
             {
                 pq.Insert(w, distTo[w]);
             }
         }
     }
 }
Example #2
0
        // relax edge e and update pq if changed
        private void relax(Edge e, int v)
        {
            int w = e.Other(v);

            if (distTo[w] > distTo[v] + e.Weight)
            {
                distTo[w] = distTo[v] + e.Weight;
                edgeTo[w] = e;
                if (pq.Contains(w))
                {
                    pq.DecreaseKey(w, distTo[w]);
                }
                else
                {
                    pq.Insert(w, distTo[w]);
                }
            }
        }
Example #3
0
        // relax edge e and update pq if changed
        private void relax(DirectedEdge e)
        {
            int v = e.From, w = e.To;

            if (distTo[w] > distTo[v] + e.Weight)
            {
                distTo[w] = distTo[v] + e.Weight;
                edgeTo[w] = e;
                if (pq.Contains(w))
                {
                    pq.DecreaseKey(w, distTo[w]);
                }
                else
                {
                    pq.Insert(w, distTo[w]);
                }
            }
        }