EMIS 8374 Dijkstra’s Algorithm Updated 18 February 2008
Dijkstra’s Algorithm Network may have cycles, but all arc lengths must be nonnegative. Maintains a partition of N into two subsets: Set P: permanently labeled nodes Set T: temporarily labeled nodes Moves nodes from T into P one at a time in non-decreasing order by shortest-path distance from the source node
Dijkstra’s Algorithm (pg 109) begin P := {}; T := N; d(i) := for each node i in N; d(s) := 0 and pred(s) := 0; while |P| < n do pick i in T with minimum d(i) value; move i from T to P; for each (i, j) in A(i) do if d(j) > d(i) + cij then d(j) := d(i) + cij; pred(j) := i; end;
Dijkstra’s Algorithm Example 2 4 5 4 3 1 1 6 2 1 6 7 2 3 2 P = {}, T = {1, 2, 3, 4, 5, 6}
Dijkstra’s Algorithm Example 4 2 4 5 4 3 1 1 6 2 1 6 7 2 3 2 7 P = {1}, T = {2, 3, 4, 5, 6}
Dijkstra’s Algorithm Example 4 4 6 2 4 5 4 3 1 1 6 2 1 6 7 2 3 2 7 5 P = {1,4}, T = {2, 3, 5, 6}
Dijkstra’s Algorithm Example 4 6 2 4 4 5 3 1 1 6 11 2 1 6 7 2 3 2 7 5 5 P = {1,4,3}, T = {2, 5, 6}
Dijkstra’s Algorithm Example 4 6 6 2 4 4 5 3 1 1 6 11 9 2 1 6 7 2 3 2 7 5 P = {1,4,3,5}, T = {2, 6}
Dijkstra’s Algorithm Example 4 6 6 2 4 5 4 3 1 6 1 11 9 2 1 6 7 2 3 2 7 7 5 P = {1,4,3,5,2}, T = {6}
Dijkstra’s Algorithm Example 4 6 6 2 4 5 4 3 1 1 6 9 9 2 1 6 7 2 3 2 7 5 P = {1,4,3,5,2,6}, T = {}
Shortest Path Tree 4 6 6 2 4 5 4 3 1 1 6 9 2 1 6 7 2 3 2 7 5
Shortest Path Tree 4 6 6 2 4 5 4 3 1 1 6 11 9 9 2 1 6 7 2 3 2 11 9 9 2 1 6 7 2 3 2 7 5
Correctness of Dijkstra’s Algorithm At the end of any iteration the following statements hold: The distance label d(i) is optimal for any node i in the set P. That is, d(i) = d*(i) for all i in P. The distance label d(i) for any node i in T is the length of a shortest path from the source to i such that all internal nodes are in P.
Statement 2 Example: d(6) after scanning node 3 4 6 2 4 4 5 3 1 1 6 11 2 1 6 7 2 3 2 7 5 Shortest path in Statement 2 uses internal nodes 1, 4, and 3
Statement 2 Example: d(6) after scanning node 5 4 6 2 4 4 5 3 1 1 6 9 2 1 6 7 2 3 2 7 5 Shortest path in Statement 2 uses internal nodes 1, 4, and 5
Proof Statements 1 and 2 are clearly true after the first iteration. Assume they are true after iteration k and prove that they hold after iteration k+1
Base Cases for Proof by Induction After the first iteration the only node in P is the source with d(s) = 0. After the first iteration there are two cases for a node j in T: If j is in A(s), then d(j) = csj If j is not in A(s), then d(j) = .
Illustration of Base Cases 14 2 4 5 14 3 1 1 6 2 1 6 7 2 3 2 7 P = {1}, T = {2, 3, 4, 5, 6}
Proof that Statement 1 Holds after Iteration k+1 Let i be the node moved from T to P in iteration k+1 Need to show d(i) = d*(i) Idea Let B be a path from s to i Let L(B) be the length of B Show that d(i) ≤ L(B)
Proof that Statement 1 Holds after Iteration k+1 There are two cases to consider for B All internal nodes of B are in P At least one internal node of B is in T Case 1: Statement 2 implies that d(i) ≤ L(B)
Proof that Statement 1 Holds after Iteration k+1 Case 2: Let j be first internal node of B that is in T By Statement 2, the length of the subpath of B from s to j is at least d(j) Since all arc lengths are non-negative, the length of the subpath of B from j to i is at least zero Thus, L(B) ≥ d(j) Since node i was picked to move to P, d(i) ≤ d(j) Therefore, d(i) ≤ L(B)
Diagram for Case 2 P i s j Since Statement 2 holds for the first k iterations, d(j) ≤ length of the subpath of B from s to j.
Diagram for Case 2 P i s j Since the arc lengths are non-negative, the length of the subpath of B from j to i is at least zero.
Diagram for Case 2 P i s j The length of path B from s to i is at least d(j).
Diagram for Case 2 P i s j Since d(i) ≤ d(j), the length of path B from s to i is at least d(i).
Diagram for Case 2 P i s j Since d(i) ≤ L(B) for any path B from s to i , d(i) = d*(i).
Proof of Statement 2 When node i moves from T to P, the algorithm changes pred(j) to i and sets d(j) = d(i) + cij for all j in T such that d(j) > d(i) + cij . Thus, the path defined by the predecessor indices from j to s satisfies Property 4.2. Therefore, d(j) is the length of a shortest path from s to j subject to the restriction that each internal node in the path is in P.
Diagram for Statement 2 j s i T P pred(j) Move node i from T to P If d(j) d(i) + cij then Statement 2 still holds for node j.
Diagram for Statement 2 j s i T P old pred(j) new pred(j) Move node i from T to P If d(j) > d(i) + cij then d(j) gets reset to d(i) + cij
Complexity of Dijkstra’s Algorithm Node selections Must compare the distance labels of all nodes in T: O(n + (n-1) + (n-2) + … + 1) = O(n2) Total work for scanning nodes is O(m) Overall complexity is O(n2 + m) = O(n2) Can be improved to O(m + n log n) with Fibonacci heaps