Download presentation
1
Dijkstra’s Algorithm Shortest Path (Single Source)
Comparison to Floyd’s Algorithm: Solves a simpler problem O(n2) vs. O(n3)
2
Dijkstra’s: A Greedy Algorithm
What does greedy mean? At the beginning of each step, the algorithm picks the best option. Q: What other problem used a greedy algorithm? A: Knapsack problem Recall that there were 3 greedy strategies Highest $$$ Lowest weight Highest $$$/weight ratio
3
Dijkstra’s: A Dynamic Programming Algorithm
Recall that dynamic programming refers to an algorithm that chooses to store information rather than re-compute it. To really be considered Dynamic Programming Store or update information at each step Use the collective information to compute the next step
4
Recall Floyd’s Algorithm
for (int k = 0; k < N; k++) // For each vertex // Update the matrix for (int i = 0; i < N; i++) for (int j = 0; j < N; j++) // Allow vertex k to be an intermediate hop if (M[i][k]+M[k][j] < M[i][j]) then M[i][j] = M[i][k]+M[k][j]
5
Recall Floyd’s Algorithm
The beauty of Floyd’s algorithm is that it progressively minimizes the distance between vertices Each step incorporates more and more possibilities, i.e., we add another intermediate hop. Step 0 i j k1 Step 1 k2 k3 Step 2 Step 3
6
Recall Floyd’s Algorithm
However, at the end of each step, the algorithm can rule out inferior possibilities. if (M[i][k]+M[k][j] < M[i][j]) then M[i][j] = M[i][k]+M[k][j] If the i k j is a better option than i j then we never consider i j in the future. We always know that hopping through k is better
7
Recall Floyd’s Algorithm
Implicitly Floyd’s algorithm computes the minimum path between any two vertices given O(n!) possibilities. But, it actually does NOT compute all the possibilities hops that are not optimal are quickly identified and never considered in the future. k 4 5 i j 8
8
How is Dijkstra’s algorithm better?
Its really not better. Its more appropriate when you have a designated starting vertex. Since you know the source vertex, you can limit your exploration. If you limit your exploration properly, you can compute the minimum distance to all vertices in O(n2) computations.
9
a b c d e f g h i j Initial Step: Given a graph Adjacency List a null
28 13 17 8 14 38 6 7 32 5 20 21 4 26 11 30 10 45 3 46 Initial Step: Given a graph Adjacency List V Prev Dist a null b c d e f g h i j a d b 5 28 c 8 13 f 6 17 g 11 14 32 e i 4 26 38 46 3 7 h j 45 10 20 21 30
10
Pick the V with the smallest dist
28 8 Step 1: Pick the V with the smallest dist V Prev Dist a null b c d e f g h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a d b 5 28 c 8 13 f 6 17 g 11 14 32 e i 4 26 38 46 3 7 h j 45 10 20 21 30 g h i 11 45 10 5 j
11
Iterate over its adj. list and update the table
28 8 Step 1: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b 28 c d 5 e f g h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
12
Pick v with smallest dist.
28 8 Step 2: Pick v with smallest dist. V Prev Dist a null b 28 c d 5 e f g h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
13
Iterate over its adj. list and update the table
28 8 Step 2: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 5 e f g 16 h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
14
Pick v with smallest dist.
28 8 Step 3: Pick v with smallest dist. V Prev Dist a null b d 19 c 5 e f g 16 h i j a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
15
Iterate over its adj. list and update the table
28 8 Step 3: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 5 e f g 16 h 21 i j 61 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
16
Pick v with smallest dist.
28 8 Step 4: Pick v with smallest dist. V Prev Dist a null b d 19 c 5 e f g 16 h 21 i j 61 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
17
Iterate over its adj. list and update the table
28 8 Step 4: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e f g 16 h 21 i j 61 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
18
Pick v with smallest dist.
28 8 Step 5: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e f g 16 h 21 i j 61 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
19
Iterate over its adj. list and update the table
28 8 Step 5: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e h 42 f g 16 21 i 51 j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
20
Pick v with smallest dist.
28 8 Step 6: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e h 42 f g 16 21 i 51 j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
21
Iterate over its adj. list and update the table
28 8 Step 6: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e h 42 f 33 g 16 21 i 51 j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
22
Pick v with smallest dist.
28 8 Step 7: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e h 42 f 33 g 16 21 i 51 j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
23
Iterate over its adj. list and update the table
28 8 Step 7: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e h 42 f 33 g 16 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
24
Pick v with smallest dist.
28 8 Step 8: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e h 42 f 33 g 16 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
25
Iterate over its adj. list and update the table
28 8 Step 8: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e f 36 33 g 16 h 21 i j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
26
Pick v with smallest dist.
28 8 Step 9: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e f 36 33 g 16 h 21 i j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
27
Iterate over its adj. list and update the table
28 8 Step 9: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e f 36 33 g 16 h 21 i j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
28
Pick v with smallest dist.
28 8 Step 10: Pick v with smallest dist. V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
29
Iterate over its adj. list and update the table
28 8 Step 10: Mark it as visited. Iterate over its adj. list and update the table V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 a 5 d 28 b g h i b 8 c 13 a 11 c 6 f 17 b d 11 g 14 b 32 a 45 10 5 e 4 i 26 d 38 b 46 f f 3 e 7 c j g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
30
28 8 Traceback The table can be used to recursively trace all the shortest paths from vertex a V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 g h i 11 45 10 5 j
31
What is the shortest path to vertex e?
28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 36 33 g 16 h 21 i j 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 g h i 11 45 10 5 e j
32
What is the shortest path to vertex e?
28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 g h i 11 45 10 5 f e j
33
What is the shortest path to vertex e?
28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 g h i 11 c 45 10 5 f e j
34
What is the shortest path to vertex e?
28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 5 30 b g h i 11 c 45 10 5 f e j
35
What is the shortest path to vertex e?
28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 11 20 21 4 26 d 5 30 b g h i 11 c 45 10 5 f e j
36
What is the shortest path to vertex e?
28 8 Traceback What is the shortest path to vertex e? V Prev Dist a null b d 19 c 27 5 e f 39 33 g 16 h 21 i j 36 31 a b c 13 17 5 32 14 38 6 7 46 d e f 26 3 a 11 20 21 4 26 d 5 30 b g h i 11 c 45 10 5 f e j
37
How to code Dijkstra’s Algorithm
Data Structures: AdjacencyList (adjList) adjList.find(label) Finds a vertex in constant time Uses the vertex label as the hash index Sets an iterator to point to label’s adjacency list
38
Vertex v = new Vertex(‘h’); adjList.find(v.getLabel())
5 28 c 8 13 f 6 17 g 11 14 32 e i 4 26 38 46 3 7 h j 45 10 20 21 30
39
Vertex v = new Vertex(‘h’); adjList.find(v.getLabel()) // Finds h
5 28 a d b 8 13 b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i
40
Vertex v = new Vertex(‘h’); adjList.find(v.getLabel())
// Moves iterator to front of h’s adjacency list 5 28 a d b 8 13 b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i
41
How to code Dijkstra’s Algorithm
Data Structures: AdjacencyList (adjList) adjList.getNext(); Returns the edge Moves the iterator to the next edge Returns null if it reaches the end of label’s adjacency list
42
while (e = adjList.getNext()) print(e.getLabel());
5 28 a d b 8 13 b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i
43
How to code Dijkstra’s Algorithm
Data Structures: Edge (e) e.getLabel() Returns the label of the vertex that the edge points to e.getDist() Returns the edge’s distance (weight of the edge)
44
j while (e = adjList.getNext()) print(e.getLabel()); a d b b c a c f b
5 28 a d b 8 13 j b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i
45
j d while (e = adjList.getNext()) println(e.getLabel()); a d b b c a c
5 28 a d b 8 13 j d b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i
46
j d e while (e = adjList.getNext()) println(e.getLabel()); a d b b c a
5 28 a d b 8 13 j d e b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i
47
j d e i while (e = adjList.getNext()) println(e.getLabel()); a d b b c
5 28 a d b 8 13 j d e i b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i 11 26 i h f 5 j i
48
j d e i while (e = adjList.getNext()) println(e.getLabel()); a d b b c
5 28 a d b 8 13 j d e i loop terminates b c a 6 17 c f b 11 14 32 d g b a 4 26 38 46 e i d b f 3 7 f e c 5 45 g h j 10 20 21 30 h j d e i null 11 26 i h f 5 j i
49
How to code Dijkstra’s Algorithm
Data Structures: PathTable (pTable) pTable.getMin() Finds the minimum distance vertex and sets it as visited. Returns the vertex Returns null if all vertices are marked visited.
50
Vertex v = pTable.getMin(); a null b d 19 c 5 e f g 16 h 21 i j 61
Prev Dist a null b d 19 c 5 e f g 16 h 21 i j 61
51
Vertex v = pTable.getMin(); a null b d 19 c 5 e f g 16 h 21 i j 61
Prev Dist a null b d 19 c 5 e f g 16 h 21 i j 61
52
Vertex v = pTable.getMin(); a null b d 19 c 5 e f g 16 h 21 i j 61 v
Prev Dist a null b d 19 c 5 e f g 16 h 21 i j 61 v label = ‘b’ dist = 19
53
How to code Dijkstra’s Algorithm
Data Structures: PathTable (pTable) pTable.update(vertex, prev, dist) Updates previous vertex and total distance Label used as the hash index Constant time update
54
pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
Prev Dist a null b d 19 c 5 e f g 16 h 21 i j 61 v label = ‘b’ dist = 19 pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
55
pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
Prev Dist a null b d 19 c 5 e f g 16 h 21 i j 61 v label = ‘b’ dist = 19 pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
56
pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
Prev Dist a null b d 19 c 29 5 e f g 16 h 21 i j 61 v label = ‘b’ dist = 19 pTable.update(‘c’, v.getLabel(), v.getDist() + 10);
57
How to code Dijkstra’s Algorithm
Weight w = 0; Edge e = null; Label v = new Label(‘a’); pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }
58
How to code Dijkstra’s Algorithm
Weight w = 0; Edge e = null; Label v = new Label(‘a’); // variables pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }
59
How to code Dijkstra’s Algorithm
Weight w = 0; Edge e = null; Label v = new Label(‘a’); pTable.update(v, null, 0); // Sets A as the start vertex while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }
60
How to code Dijkstra’s Algorithm
V Prev Dist a null b c d e f g h i j Label v = new Label(‘a’); pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }
61
How to code Dijkstra’s Algorithm
5 d 28 b Label v = new Label(‘a’); pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); } b 8 c 13 a c 6 f 17 b d 11 g 14 b 32 a e 4 i 26 d 38 b 46 f f 3 e 7 c g 5 h 45 j h 10 j 20 d 21 e 30 i i 11 h 26 f j 5 i
62
How to code Dijkstra’s Algorithm
V Prev Dist a null b 28 c d 5 e f g h i j a 5 d 28 b Label v = new Label(‘a’); pTable.update(v, null, 0); while(v = pTable.getMin()) { adjList.find(v.getLabel()); while (e = adjList.getNext()) { w = e.getDist() + v.getDist(); pTable.update(e.getLabel(), v.getLabel(), w); }
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.