0

from this website's pseudocode:

Given a graph, G, with edges E of the form (v1, v2) and vertices V, and a
source vertex, s

dist : array of distances from the source to each vertex
prev : array of pointers to preceding vertices
i    : loop index
F    : list of finished vertices
U    : list or heap unfinished vertices

/* Initialization: set every distance to INFINITY until we discover a path */
for i = 0 to |V| - 1
    dist[i] = INFINITY
    prev[i] = NULL
end

/* The distance from the source to the source is defined to be zero */
dist[s] = 0

/* This loop corresponds to sending out the explorers walking the paths, where
 * the step of picking "the vertex, v, with the shortest path to s" corresponds
 * to an explorer arriving at an unexplored vertex */

while(F is missing a vertex)
   pick the vertex, v, in U with the shortest path to s
   add v to F
   for each edge of v, (v1, v2)
        /* The next step is sometimes given the confusing name "relaxation"
        if(dist[v1] + length(v1, v2) < dist[v2])
            dist[v2] = dist[v1] + length(v1, v2)
            prev[v2] = v1
            possibly update U, depending on implementation
        end if
    end for
end while

what is meant by: if(dist[v1] + length(v1, v2) < dist[v2])?

particularly: length(v1, v2).

shouldn't: dist[v1] < dist[v2] enough?

kmlkz
  • 63
  • 5
  • the invariant is that `dist[i]` contains the smallest distance of `i` from source. so whenever you set `dist[j]` from `i` you're interested in how far `j` is from the source, and that is `dist[i] + length(i,j)` – mangusta Sep 07 '16 at 02:32
  • `dist[i] < dist[j]` is not enough to state that you can reach vertex `j` from `i` faster than it (i.e. vertex `j`) was reached from some other vertex – mangusta Sep 07 '16 at 02:41

1 Answers1

0

length(v1, v2) is the weight of the edge from node v1 to v2.

This condition checks whatever the path to v2 can be improved by going to v1 and then through edge (v1,v2).

TomekW
  • 1
  • 1