Dijkstra’s Algorithm Shortest Path (Single Source) Comparison to Floyd’s Algorithm: Solves a simpler problem O(n2) vs. O(n3)
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
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
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]
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
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
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
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.
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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
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)
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
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
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
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
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
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.
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
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
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
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
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);
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);
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);
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); }
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); }
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); }
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); }
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
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); }