Title: EMIS 8374 Dijkstra
1EMIS 8374 Dijkstras Algorithm Updated 18
February 2008
2Dijkstras 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
3Dijkstras Algorithm (pg 109)
begin P T N d(i) ? for each
node i in N d(s) 0 and pred(s) 0
while P lt 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) gt
d(i) cij then d(j) d(i) cij
pred(j) i end end
4Dijkstras Algorithm Example
?
?
?
2
4
5
4
3
1
6
1
?
0
2
1
6
7
2
3
2
?
?
P , T 1, 2, 3, 4, 5, 6
5Dijkstras Algorithm Example
?
4
?
?
?
2
4
5
4
3
0
1
6
1
?
2
1
6
7
2
3
2
?
?
7
?
P 1, T 2, 3, 4, 5, 6
6Dijkstras Algorithm Example
4
?
4
6
?
?
2
4
5
4
3
0
1
6
1
?
2
1
6
7
2
3
2
?
?
7
5
?
?
P 1,4, T 2, 3, 5, 6
7Dijkstras Algorithm Example
4
6
2
4
5
4
3
0
1
6
1
?
?
11
2
1
6
7
2
3
2
?
7
?
5
5
P 1,4,3, T 2, 5, 6
8Dijkstras Algorithm Example
6
4
6
2
4
5
4
3
0
1
6
1
9
11
2
1
6
7
2
3
2
?
7
?
5
P 1,4,3,5, T 2, 6
9Dijkstras Algorithm Example
4
6
?
6
2
4
5
4
3
0
?
1
6
1
11
9
2
1
6
7
2
3
2
?
?
5
7
7
P 1,4,3,5,2, T 6
10Dijkstras Algorithm Example
4
6
?
6
2
4
5
4
3
0
1
6
1
9
9
2
1
6
7
2
3
2
?
?
5
7
P 1,4,3,5,2,6, T
11Shortest Path Tree
4
6
?
6
2
4
5
4
3
0
1
6
1
9
2
1
6
7
2
3
2
?
?
5
7
12Shortest Path Tree
4
6
?
6
2
4
5
4
3
?
1
6
1
11
0
?
9
9
2
1
6
7
2
3
2
?
?
5
7
13Correctness of Dijkstras 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.
14Statement 2 Example d(6) after scanning node 3
4
6
2
4
5
4
3
0
1
6
1
11
2
1
6
7
2
3
2
7
5
Shortest path in Statement 2 uses internal nodes
1, 4, and 3
15Statement 2 Example d(6) after scanning node 5
4
6
2
4
5
4
3
0
1
6
1
9
2
1
6
7
2
3
2
7
5
Shortest path in Statement 2 uses internal nodes
1, 4, and 5
16Proof
- 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 k1
17Base 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) ?.
18Illustration of Base Cases
?
14
2
4
5
14
3
0
1
6
1
?
2
1
6
7
2
3
2
?
7
P 1, T 2, 3, 4, 5, 6
19Proof that Statement 1 Holds after Iteration k1
- Let i be the node moved from T to P in iteration
k1 - 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)
20Proof that Statement 1 Holds after Iteration k1
- 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)
21Proof that Statement 1 Holds after Iteration k1
- 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)
22Diagram 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.
23Diagram 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.
24Diagram for Case 2
P
i
s
j
The length of path B from s to i is at least d(j).
25Diagram 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).
26Diagram for Case 2
P
i
s
j
Since d(i) L(B) for any path B from s to i ,
d(i) d(i).
27Proof 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) gt 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.
28Diagram for Statement 2
T
P
j
pred(j)
s
i
Move node i from T to P
If d(j) ? d(i) cij then Statement 2 still holds
for node j.
29Diagram for Statement 2
T
P
old pred(j)
j
s
i
new pred(j)
Move node i from T to P
If d(j) gt d(i) cij then d(j) gets reset to d(i)
cij
30Complexity of Dijkstras 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