230 likes | 345 Views
Dijkstra’s Algorithm. We are given a directed weighted graph There are no negative weights. We need to solve the single source shortest path problem. The source is given to be 1. 29. 34. 27. 22. 32. 26. 27. 20. 2. 2. 4. 7. 8. 9. 4. 3. 5. 1. 9. 10. 20. 3. 8. 19. 17. 15.
E N D
Dijkstra’s Algorithm We are given a directed weighted graph There are no negative weights. We need to solve the single source shortest path problem. The source is given to be 1.
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 Processed nodes will be shown in red. Partially processed nodes will be shown in green. The partially processed nodes will be kept in a heap, where the key is value. Unprocessed nodes will be shown in black. Search tree pointers will be shown in red. The back pointers are the reverse of the search tree pointers.
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 18 19 20 value 0 back *
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 11:1 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 9:2 20:27 value 0 2 1 34 27 back * 1 1 1 1 17:34 The value of a node is the weight of the shortest path found so far.
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 9:2 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 14:4 20:27 value 0 32 2 1 4 34 27 back * 11 1 1 11 1 1 17:34 6:32
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 14:4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 10:4 20:27 value 0 32 2 4 1 4 34 27 back * 11 1 9 1 11 1 1 17:34 6:32
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 10:4 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 12:6 20:27 value 0 32 2 4 1 6 4 34 27 back * 11 1 9 1 14 11 1 1 17:34 6:32
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 12:6 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 6:32 20:8 value 0 32 2 4 1 6 4 34 8 back * 11 1 9 1 14 11 1 10 17:34 A better path to 20 is found; its value and backpointer are updated.
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 20:8 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 5:10 17:34 value 0 10 32 2 4 1 6 4 34 8 back * 12 11 1 9 1 14 11 1 10 6:32
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 5:10 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 3:15 19:28 value 0 15 10 32 2 4 1 6 4 30 34 28 8 back * 20 12 11 1 9 1 14 11 20 1 20 10 6:32 15:30 17:34
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 3:14 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 15:30 2:18 value 0 16 14 10 32 2 4 1 6 4 30 34 28 8 back * 5 5 12 11 1 9 1 14 11 20 1 20 10 6:32 17:34 19:28
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 2:18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 15:30 8:22 value 0 16 14 10 32 22 2 4 1 6 4 30 34 28 8 back * 5 5 12 11 3 1 9 1 14 11 20 1 20 10 6:32 17:34 19:28
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 8:18 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 15:30 7:25 value 0 16 14 10 32 25 18 2 4 1 6 4 30 34 28 8 back * 5 5 12 11 2 2 1 9 1 14 11 20 1 20 10 6:32 17:34 19:28
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 7:25 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 15:30 19:27 value 0 16 14 10 32 25 18 2 4 1 6 4 30 34 27 8 back * 5 5 12 11 2 2 1 9 1 14 11 20 1 8 10 6:32 17:34
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 19:26 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 6:28 17:34 value 0 16 14 10 28 25 18 2 4 1 6 4 30 34 26 8 back * 5 5 12 7 2 2 1 9 1 14 11 20 1 7 10 15:30
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 6:28 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 15:30 17:30 value 0 16 14 10 28 25 18 2 4 1 6 4 30 30 26 8 back * 5 5 12 7 2 2 1 9 1 14 11 20 19 7 10
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 17:30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 15:30 18:35 value 0 16 14 10 28 25 18 2 4 1 6 4 30 30 35 26 8 back * 5 5 12 7 2 2 1 9 1 14 11 20 19 6 7 10
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 15:30 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 18:35 value 0 16 14 10 28 25 18 2 4 1 6 4 30 30 35 26 8 back * 5 5 12 7 2 2 1 9 1 14 11 20 19 6 7 10
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 18:35 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 4:37 16:35 value 0 16 14 37 10 28 25 18 2 4 1 6 4 30 35 30 35 26 8 back * 5 5 15 12 7 2 2 1 9 1 14 11 20 15 19 6 7 10
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 16:35 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 4:37 value 0 16 14 37 10 28 25 18 2 4 1 6 4 30 35 30 26 8 back * 5 5 15 12 7 2 2 1 9 1 14 11 20 15 19 7 10
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 4:37 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 13:40 value 0 16 14 37 10 28 25 18 2 4 1 6 40 4 30 35 30 35 26 8 back * 5 5 15 12 7 2 2 1 9 1 14 16 11 20 15 19 6 7 10
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap 13:40 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 value 0 16 14 37 10 28 25 18 2 4 1 6 40 4 30 35 30 35 26 8 back * 5 5 15 12 7 2 2 1 9 1 14 16 11 20 15 19 6 7 10
29 34 27 22 32 26 27 20 2 2 4 7 8 9 4 3 5 1 9 10 20 3 8 19 17 15 16 9 5 8 7 6 3 6 1 1 1 3 7 3 5 5 7 6 2 2 4 8 2 8 1 6 5 5 5 5 5 5 2 7 2 8 6 2 2 6 3 3 9 7 9 4 4 3 3 2 4 6 9 3 7 8 8 11 14 12 5 2 7 6 18 4 13 3 4 7 7 3 2 1 1 2 22 28 30 35 31 31 heap empty 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 value 0 16 14 37 10 28 25 18 2 4 1 6 40 4 30 35 30 35 26 8 back * 5 5 15 12 7 2 2 1 9 1 14 16 11 20 15 19 6 7 10 The heap is empty and we are done.