Section 4.6 – Shortest Path Algorithm A 2 B 5 6 3 6 6 4 6 D 2 E 1 F 4 1 1 2 6 5 3 H 4 J 2 C G K Problem: Find the shortest path from A to K A B C D E F G H J K vertex w with smallest t value vertex used when t updated There are three values associated with each vertex: temp(u) = temporary shortest distance from the start to vertex u; these are the values written in the table done(u) = yes if temp(u) is the actual shortest distance, no otherwise. When yes the value has been circled in the table and that vertex is finished with last(u) = the vertex used when temp(u) was last changed – this is stored in an array at the foot of the table, update entry when temp is changed Algorithm: for each vertex u set temp(u) = sum of all the weights, done(u) = no, last(u) = empty set temp(start) = 0 and done(start) = yes set current vertex (denote by u) to start repeat until done(finish) = yes loop look at each vertex v which is adjacent to the current vertex, u, and has done = no (i.e. those for which the shortest distance has not yet been found) calculate temp(u) + weight of edge uv if smaller than temp(v) then use it to update temp(v) and set last(v) to u end loop determine the vertex with the smallest temporary distance and done = no and set its done to yes and set current vertex to this vertex end repeat The last values are then used to find the route from start to finish by tracking backwards.
© Copyright 2026 Paperzz