150 likes | 232 Views
T. 11. A. 3. 4. 5. S. 2. B. 6. D. 1. 4. 4. C. The Shortest Path Problem. Find the shortest path from S to T. An example would be planning a journey to go from Norwich to Manchester. 8. Dijkstra's Algorithm Each vertex is given a label box as shown. Order of
E N D
T 11 A 3 4 5 S 2 B 6 D 1 4 4 C The Shortest Path Problem Find the shortest path from S to T. An example would be planning a journey to go from Norwich to Manchester 8
Dijkstra's Algorithm Each vertex is given a label box as shown Order of permanent labelling Permanent distance label Temporary distance label
T 11 A 3 4 5 S 2 B 6 D 1 4 4 C • Label start vertex with permanent label 1 • and permanent distance 0.
T 11 A 3 4 5 S 2 B 6 D 1 4 4 C 2. Consider all the edges joining the vertex just labelled i.e. edges joining S to A , B and C. Calculate the sum of the previous permanent distance label plus the distance to the new vertex. This is the temporary distance label.
8 T 11 A 3 4 5 S 2 B 6 D 1 4 4 C 2 3 Choose the smallest temporary distance label i.e. A and label it with a permanent distance label of 3 and a permanent order label of 2
8 T 11 A 3 4 5 S 2 B 6 D 1 4 4 C Bigger than 6 so delete 7 3 4 Choose the smallest temporary distance label i.e. C and label it with a permanent distance label of 4 and a permanent order label of 3 Repeat step 2 but this time consider all the vertices connected to A i.e T and B.
8 T 11 A 3 4 5 S 2 B 6 D 1 4 4 C 4 5 5 3 4 Choose the smallest temporary distance label i.e. B and label it with a permanent distance label of 5 and a permanent order label of 4 Repeat step 2 but this time consider all the vertices connected to C i.e D and B.
T 11 A 3 4 5 2 B 6 D 1 4 4 C 13 8 4 5 S 5 7 5 7 3 4 Choose the smallest temporary distance label i.e. D and label it with a permanent distance label of 7 and a permanent order label of 5 Repeat step 2 but this time consider all the vertices connected to B i.e T and D.
T 11 A 3 4 5 2 B 6 D 1 4 4 C 12 6 13 12 8 4 5 S 5 7 5 7 3 4 Choose the smallest temporary distance label i.e. T and label it with a permanent distance label of 12 and a permanent order label of 6 Repeat step 2 but this time consider all the vertices connected to D i.e T.
2 3 12 6 3 13 12 14 8 4 5 S 1 0 T 5 6 11 7 5 A 3 4 7 8 5 2 B 3 4 6 4 D 1 4 4 C Find shortest path by working backwards from the finish and choosing only those edges whose length is exactly equal to the difference between the permanent distance labels at either end.
2 3 12 6 3 13 12 14 8 4 5 S 1 0 T 5 6 11 7 5 A 3 4 7 8 5 2 B 3 4 6 4 D 1 4 4 C From T 12 - 5 = 7 ie vertex D . This is the vertex to choose From T 12 - 8 5 so cannot choose B From T 12 - 11 3 so cannot choose A
2 3 12 6 3 13 12 14 8 4 5 S 1 0 T 5 6 11 7 5 A 3 4 7 8 5 2 B 3 4 6 4 D 1 4 4 C From D 7 - 2 = 5 ie vertex B . This is the vertex to choose From D 7- 4 4 so cannot choose C
2 3 12 6 3 13 12 14 8 4 5 S 1 0 T 5 6 11 7 5 A 3 4 7 8 5 2 B 3 4 6 4 D 1 4 4 C From B 5 - 1 = 4 ie vertex C . This is the vertex to choose From B 5 - 4 3 so cannot choose AFrom B 5 - 6 0 so cannot choose S
2 3 12 6 3 13 12 14 8 4 5 S 1 0 T 5 6 11 7 5 A 3 4 7 8 5 2 B 3 4 6 4 D 1 4 4 C From C 4 - 4 = 0 ie vertex S . This is the vertex to choose
2 3 12 6 3 13 12 14 8 4 5 S 1 0 T 5 6 11 7 5 A 3 4 7 8 5 2 B 3 4 6 4 D 1 4 4 C Route T,D,B,C,S. So starting from S the route is S,C,B,D,T and the distance is 12