previous | start | next

Relaxing an Edge

Suppose e is an edge from v to a vertex w.

distTo[v] is the length of some path from s to v

distTo[w] is the length of some path from s to w

We may be able to relax (i.e., decrease distTo[w]) by considering a different path from s to w:

      s to v, followed by the edge from v to w.
   

whose length is

      distTo[v] + e.weight()    // where e is the edge from v to w
   
 void relax(DirectedEdge e)
 {
   int v = e.from();
   int w = e.to();

   if (distTo[w] > distTo[v] + e.weight()) {
      distTo[w] = distTo[v] + e.weight();
      edgeTo[w] = v;
   }
 }
   


previous | start | next