EMIS 8374 Dijkstras Algorithm Updated 18 February 2008
EMIS 8374 Dijkstra’s Algorithm Updated 18 February 2008 Slide 1
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 Slide 2
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 begin 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; Slide 3
Dijkstra’s Algorithm Example 4 4 0 1 1 2 7 2 2 5 3 6 1 3 2 P = {}, T = {1, 2, 3, 4, 5, 6} 6 Slide 4
Dijkstra’s Algorithm Example 0 4 4 4 1 2 7 2 1 5 3 6 1 2 3 2 7 P = {1}, T = {2, 3, 4, 5, 6} 6 Slide 5
Dijkstra’s Algorithm Example 0 4 1 4 4 2 7 2 1 6 5 3 6 1 2 3 2 7 5 P = {1, 4}, T = {2, 3, 5, 6} 6 Slide 6
Dijkstra’s Algorithm Example 4 4 0 1 4 2 5 1 2 7 6 2 7 2 3 6 11 1 6 3 5 P = {1, 4, 3}, T = {2, 5, 6} Slide 7
Dijkstra’s Algorithm Example 4 4 0 1 4 2 5 3 1 2 7 6 2 7 2 P = {1, 4, 3, 5}, T = {2, 6} 9 6 11 1 3 6 5 Slide 8
Dijkstra’s Algorithm Example 4 4 0 1 4 2 7 2 1 66 5 3 6 1 2 3 2 5 7 P = {1, 4, 3, 5, 2}, T = {6} 11 9 6 Slide 9
Dijkstra’s Algorithm Example 4 4 0 1 4 2 5 1 2 7 66 2 7 2 3 6 1 3 5 P = {1, 4, 3, 5, 2, 6}, T = {} 9 6 Slide 10
Shortest Path Tree 4 4 0 1 4 2 5 3 1 2 7 66 2 7 2 6 1 3 9 6 5 Slide 11
Shortest Path Tree 4 4 0 1 4 2 5 1 2 7 66 2 7 2 3 11 6 99 1 3 6 5 Slide 12
Correctness of Dijkstra’s Algorithm At the end of any iteration the following statements hold: 1. 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. 2. 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. Slide 13
Statement 2 Example: d(6) after scanning node 3 4 4 0 1 4 2 5 3 1 2 7 6 2 7 2 6 11 1 3 6 5 Shortest path in Statement 2 uses internal nodes 1, 4, and 3 Slide 14
Statement 2 Example: d(6) after scanning node 5 4 4 0 1 4 2 5 3 1 2 7 6 2 7 2 6 9 1 3 6 5 Shortest path in Statement 2 uses internal nodes 1, 4, and 5 Slide 15
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 Slide 16
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: 1. If j is in A(s), then d(j) = csj 2. If j is not in A(s), then d(j) = . Slide 17
Illustration of Base Cases 0 14 1 14 4 1 2 7 2 2 5 3 6 1 3 2 7 P = {1}, T = {2, 3, 4, 5, 6} 6 Slide 18
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) Slide 19
Proof that Statement 1 Holds after Iteration k+1 • There are two cases to consider for B 1. All internal nodes of B are in P 2. At least one internal node of B is in T • Case 1: Statement 2 implies that d(i) ≤ L(B) Slide 20
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) Slide 21
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. Slide 22
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. Slide 23
Diagram for Case 2 P i s j The length of path B from s to i is at least d(j). Slide 24
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). Slide 25
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). Slide 26
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. Slide 27
Diagram for Statement 2 s P T pred(j) j i Move node i from T to P If d(j) d(i) + cij then Statement 2 still holds for node j. Slide 28
Diagram for Statement 2 T P old pred(j) s j i 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 Slide 29
Complexity of Dijkstra’s Algorithm 1. Node selections Must compare the distance labels of all nodes in T: O(n + (n-1) + (n-2) + … + 1) = O(n 2) 2. Total work for scanning nodes is O(m) 3. Overall complexity is O(n 2 + m) = O(n 2) 4. Can be improved to O(m + n log n) with Fibonacci heaps Slide 30
- Slides: 30