First page Back Continue Last page Graphics
Keeping track of path info
shortestPath(theGraph, weight)
{
mark[origin] = 1; // this tags vertex 0 as belonging to vertexSet.
N = number of vertices in theGraph;
previous[origin] = origin;
for (v = 0 through n-1) {
weight[v] = matrix[origin][v];
previous[v] = origin; // we would get to v from the origin.
}
for (steps 2 through n) { // n-1 iterations
// find the smallest weight[v] such that v is
// not in not in vertexSet (mark[v]==0)
mark[v] = 1;
for (all vertices u not in vertexSet [mark[u]==0]) {
if (weight[u] > weight[v] + matrix[v][u]) {
weight[u] = weight[v] + matrix[v][u];
previous[u] = v; // we would get to u from v
}
}
}
}