Lecture 11

2
IEOR 266 Network Flows and Graphs October 12, 2008 Lecture 11 1 Shortest paths (continued) 1.1 Dijkstra’s Algorithm (continued) Theorem 1 (The correctness of Dijkstra’s algorithm). Once a node joins P its label is the shortest path label. Proof. At each iteration the nodes are partitioned into subsets P and T . We will prove by induction on the size of P that the label for each node i P is the shortest distance from node 1. Base: When |P | = 1, the only node in P is s. It is correctly labeled with a distance 0. Inductive step: Assume for |P | = k and prove for |P | = k + 1. Suppose that node i with d(i)= min{d(j ): j T }, was added to P , but its label d(i) is not the shortest path label. Then there should be some nodes in T along the shortest path 1 to i. Let j be the first node in T on the shortest path from 1 to i. Then the length of shortest path from 1 to i = d(j )+ c ji . Since d(i) is not the shortest label, d(j )+ c ji <d(i), and since c ji is positive, we have d(j ) <d(i). This contradicts that d(i) is the minimum of d(j ) T . We next prove two invariants that are preserved during Dijkstra’s algorithm execution. Proposition 2. The label for each j T is the shortest distance from s such that all nodes of the path are in P (i.e. it would be the shortest path if we eliminated all arcs with both endpoints in T ). Proof. We again use induction on the size of P . Base: When |P | = 1, the only node in P is s. Also, all distance labels are initialized as follows: d(j ) := c sj for all j A(1). Thus the labels are indeed the shortest distance from s, using allowing only the path (s, j ). Inductive step: Assume for |P | = k and prove for |P | = k + 1. After a node i in T is labeled permanently, the distance labels in T \{i} might decrease. After permanently labeling node i, the algorithm sets d(j )= d(i)+ c ij if d(j ) >d(i)+ c ij , for (i, j ) A. Therefore, after the distance update, by the inductive hypothesis, the path from source to j satisfies d(j ) d(i)+ c ij , (i, j ) A s.t i P . Thus, the distance label of each node in T \{i} is the length of the shortest path subject to the restriction that each every node in the path must belong to P S {i}. Proposition 3. The labels of nodes joining P can only increase in successive iterations. Proof. We again use induction on the size of P . Base: When |P | = 1, the only node in P is s, which has a label 0. Since all costs are nonnegative, all other nodes will have labels 0. Inductive step: Assume for |P | = k and prove for |P | = k + 1. Assume by contradiction that node i with d(i)= min{d(j ): j T }, was added to P , but in a later iteration node j is added to P having a label d(j ) <d(i). This contradicts the Theorem 1.1 since if the graph had a zero-cost arc (j, i), then the shortest distance from s to i would be d(j )(<d(i) which was the label of node i when added to the permanent set). 1

description

dijkastra algorithm

Transcript of Lecture 11

Page 1: Lecture 11

IEOR 266 Network Flows and Graphs October 12, 2008Lecture 11

1 Shortest paths (continued)

1.1 Dijkstra’s Algorithm (continued)

Theorem 1 (The correctness of Dijkstra’s algorithm). Once a node joins P its label is the shortestpath label.

Proof. At each iteration the nodes are partitioned into subsets P and T . We will prove by inductionon the size of P that the label for each node i ∈ P is the shortest distance from node 1.Base: When |P | = 1, the only node in P is s. It is correctly labeled with a distance 0.Inductive step: Assume for |P | = k and prove for |P | = k + 1.Suppose that node i with d(i) = min{d(j) : j ∈ T}, was added to P , but its label d(i) is not theshortest path label. Then there should be some nodes in T along the shortest path 1 to i. Let jbe the first node in T on the shortest path from 1 to i. Then the length of shortest path from 1to i = d(j) + cji. Since d(i) is not the shortest label, d(j) + cji < d(i), and since cji is positive, wehave d(j) < d(i). This contradicts that d(i) is the minimum of d(j) ∈ T .

We next prove two invariants that are preserved during Dijkstra’s algorithm execution.

Proposition 2. The label for each j ∈ T is the shortest distance from s such that all nodes of thepath are in P (i.e. it would be the shortest path if we eliminated all arcs with both endpoints in T ).

Proof. We again use induction on the size of P .Base: When |P | = 1, the only node in P is s. Also, all distance labels are initialized as follows:d(j) := csj for all j ∈ A(1). Thus the labels are indeed the shortest distance from s, using allowingonly the path (s, j).Inductive step: Assume for |P | = k and prove for |P | = k + 1.After a node i in T is labeled permanently, the distance labels in T \ {i} might decrease. Afterpermanently labeling node i, the algorithm sets d(j) = d(i) + cij if d(j) > d(i) + cij , for (i, j) ∈ A.Therefore, after the distance update, by the inductive hypothesis, the path from source to j satisfiesd(j) ≤ d(i) + cij , ∀ (i, j) ∈ A s.t i ∈ P . Thus, the distance label of each node in T \ {i} is thelength of the shortest path subject to the restriction that each every node in the path must belongto P

⋃{i}.

Proposition 3. The labels of nodes joining P can only increase in successive iterations.

Proof. We again use induction on the size of P .Base: When |P | = 1, the only node in P is s, which has a label 0. Since all costs are nonnegative,all other nodes will have labels ≥ 0.Inductive step: Assume for |P | = k and prove for |P | = k + 1.Assume by contradiction that node i with d(i) = min{d(j) : j ∈ T}, was added to P , but in a lateriteration node j is added to P having a label d(j) < d(i). This contradicts the Theorem 1.1 sinceif the graph had a zero-cost arc (j, i), then the shortest distance from s to i would be d(j) (< d(i)which was the label of node i when added to the permanent set).

1

Page 2: Lecture 11

Complexity AnalysisThere are two major operations in Dijkstra’s algorithm :Find minimum, which has O(n2) complexity; andUpdate labels, which has O(m) complexity.Therefore, the complexity of Dijkstra’s algorithm is = O(n2 + m).

There are several implementations that improve upon this running time.One improvement uses Binary Heaps: whenever a label is updated, use binary search to insert

that label properly in the sorted array. This requires O(log n). Therefore, the complexity offinding the minimum label in temporary set is O(m log n), and complexity of the algorithm isO(m + m log n) = O(m log n).

Another improvement is Radix Heap which has complexity O(m+n√

log C) where C = max(i,j)∈Acij .But this is not a strongly polynomial complexity.

Currently, the best strongly polynomial implementation of Dijkstra’s algorithm uses FibonacciHeaps and has complexity O(m + n log n). Since Fibonacci heaps are hard to program, this imple-mentation is not used in practice.

It is important to stress that Dijkstra’s algorithm does not work correctly in the presence ofnegative edge weights. In this algorithm once a node is included in the permanent set it will not bechecked later again, but because of the presence of negative edge that is not correct. The problemis that the distance label of a node in the permanent set might be reduced after its inclusion in thepermanent set (when considering negative cost arcs).

See Figure 1 for an example such that Dijkstra’s algorithm does not work in presence of negativeedge weight. Using the algorithm, we get d(3) = 3. But the actual value of d(3) is 2.

-24

3

2

3

1

�����

BBBBBBN

XXXXXXz

������������

Figure 1: Network for which Dijkstra’s algorithm fails

2