Download presentation
Presentation is loading. Please wait.
Published byAdrian Seidel Modified over 6 years ago
1
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.
2
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.
3
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 value back *
4
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 11:1 9:2 20:27 value back * 17:34 The value of a node is the weight of the shortest path found so far.
5
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 14:4 20:27 value back * 17:34 6:32
6
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 10:4 20:27 value back * 17:34 6:32
7
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 12:6 20:27 value back * 17:34 6:32
8
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 12:6 6:32 20:8 value back * 17:34 A better path to 20 is found; its value and backpointer are updated.
9
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 5:10 17:34 value back * 6:32
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 5:10 3:15 19:28 value back * 6:32 15:30 17:34
11
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 15:30 2:18 value back * 6:32 17:34 19:28
12
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 15:30 8:22 value back * 6:32 17:34 19:28
13
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 15:30 7:25 value back * 6:32 17:34 19:28
14
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 15:30 19:27 value back * 6:32 17:34
15
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 6:28 17:34 value back * 15:30
16
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 15:30 17:30 value back *
17
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 15:30 18:35 value back *
18
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 18:35 value back *
19
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 4:37 16:35 value back *
20
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 4:37 value back *
21
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 13:40 value back *
22
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 value back *
23
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 value back * The heap is empty and we are done.
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.