First page Back Continue Last page Graphics
Generalized
shortestPath(origin, weight, previous)
{
int marked = 0;
For (int i=0;i<numberOfNodes;i++) // estimate distances to all nodes as unreachable
weight[i] = Infinity;
weight[origin] = 0; // by default it is zero units to reach the origin
previous[origin] = origin;
while (marked < numberOfNodes) { // n iterations
v = smallestUnmarked(weight, mark);
mark[v] = 1;
for (int u=0;u<numberOfNodes;u++)
if (mark[u]==0 && weight[u] > weight[v] + matrix[v][u]) {
weight[u] = weight[v] + matrix[v][u];
previous[u] = v; // we would get to u from v
}
}
}
}