Professional Documents
Culture Documents
6.006
Lecture 16
Prof. Piotr Indyk
Menu
Last week: Bellman-Ford
O(VE) time
general weights
Today: Dijkstra
O( (V+E)logV ) time
non-negative weights
Introduction to Algorithms
4/5/11
4/5/11
BFS
1
Weighted graphs
7
b
3
f
5
c
8
2
5
d
g
4
4/5/11
Weighted graphs
3b
3
7
5
c
8
2
5
d
4
5
Algorithm:
Create an unweighted graph by splitting each
edge with weight w into w pieces
Run BFS
4/5/11
Greedy approach
IDEA: Greedy.
1. Maintain a set S of vertices whose shortestpath distances from s are known.
2. At each step add to S the vertex v V S
whose distance estimate from s is minimal.
3. Update the distance estimates of vertices
adjacent to v.
Introduction to Algorithms
4/5/11
Dijkstras algorithm
d[s] 0
for each v V {s}
do d[v]
S
QV
Q is a priority queue maintaining V S
while Q
do u EXTRACT-MIN(Q)
S S {u}
for each v Adj[u]
relaxation
do if d[v] > d[u] + w(u, v)
then d[v] d[u] + w(u, v)
step
Implicit DECREASE-KEY
Introduction to Algorithms
4/5/11
Dijkstra: Example
(,-)
initialization
3
(0,*)+
(,-)
f
7
5
c
8
(,-)
5
8
g
(,-)
(,-)
h
j (,-)
d
(,-)
(,-)
e
6
i
(,-)
Dijkstra: Example
(3,a)+
1st iteration
3
(0,*)+
(,-)
f
7
5
c
8
(8,a)
5
8
g
(,-)
(,-)
h
j (,-)
d
(5,a)
(,-)
e
6
i
(,-)
Dijkstra: Example
2nd iteration
3
(0,*)+
(10,b)
f
(3,a)+ b
c
8
(8,a)
d
(5,a)+
(,-)
e
g
(,-)
Introduction to Algorithms
(,-)
h
j (,-)
6
i
(,-)
4/5/11
11
Dijkstra: Example
3rd iteration
3
(0,*)+
(10,b)
f
(3,a)+ b
c
8
(7,d)+
d
(5,a)+
(,-)
e
g
(9,d)
(,-)
h
j (,-)
6
i
(,-)
Dijkstra: Example
4th iteration
3
(0,*)+
(10,b)
f
(3,a)+ b
c
8
(7,d)+
5
8
g
(9,d)+
Introduction to Algorithms
(,-)
h
j (,-)
d
(5,a)+
(15,c)
e
6
i
(,-)
4/5/11
13
Dijkstra: Example
(3,a)+
5th iteration
3
(0,*)+
(10,b)+
f
7
5
c
8
(7,d)+
d
(5,a)+
(15,c)
e
g
(9,d)+
(,-)
h
j (,-)
6
i
(13,g)
Dijkstra: Example
(3,a)+
6th iteration
3
(0,*)+
(10,b)+
f
7
5
c
8
(7,d)+
5
8
g
(9,d)+
Introduction to Algorithms
(16,f)
h
j (,-)
d
(5,a)+
(15,c)
e
6
i
(13,g)+
4/5/11
15
Dijkstra: Example
7th iteration
3
(0,*)+
(10,b)+
f
(3,a)+ b
c
8
(7,d)+
d
(5,a)+
(14,i)+
e
g
(9,d)+
j (19,i)
6
4
(16,f)
h
6
i
(13,g)+
Dijkstra: Example
(3,a)+
8th iteration
3
(0,*)+
(10,b)+
f
7
5
c
8
(7,d)+
5
8
g
(9,d)+
Introduction to Algorithms
(15,e)+
h
j (19,i)
d
(5,a)+
(14,i)+
e
6
i
(13,g)+
4/5/11
17
Dijkstra: Example
(3,a)+
9th iteration
3
(0,*)+
c
8
(7,d)+
6
5
g
(9,d)+
(7,d)+
d
(5,a)+
g
(9,d)+
2
j (17,h)+
(15,e)+
h
(14,i)+
j (17,h)+
i
(13,g)+
3
(0,*)+
(10,b)+
f
(15,e)+
h
d
(5,a)+
(3,a)+
(14,i)+
e
Shortest-path tree
(10,b)+
f
i
(13,g)+
Correctness Part I
Lemma. Initializing d[s] 0 and d[v] for all
v V {s} establishes d[v] (s, v) for all v V,
and this invariant is maintained over any sequence
of relaxation steps.
Proof. Recall relaxation step:
if d[v] > d[u] + w(u, v) set d[v] d[u] + w(u, v)
d[u]
s
d[v]
Introduction to Algorithms
w(u, v)
v
4/5/11
19
Correctness Part II
Theorem. Dijkstras algorithm terminates with
d[v] = (s, v) for all v V.
Proof.
It suffices to show that d[v] = (s, v) for every v V
when v is added to S
Suppose u is the first vertex added to S for which
d[u] (s, u) . Let y be the first vertex in V S along a
shortest path from s to u, and let x be its predecessor:
u
S, just before
adding u.
s
Introduction to Algorithms
y
4/5/11
20
Correctness Part II
(continued)
S
s
4/5/11
21
Analysis of Dijkstra
|V |
times
while Q
do u EXTRACT-MIN(Q)
S S {u}
for each v Adj[u]
degree(u)
do if d[v] > d[u] + w(u, v)
times
then d[v] d[u] + w(u, v)
DECREASE-KEY
Introduction to Algorithms
4/5/11
22
Analysis of Dijkstra
(continued)
Time = (V)TEXTRACT-MIN + (E)TDECREASE-KEY
Q
array
TEXTRACT-MIN TDECREASE-KEY
O(V)
binary
O(lg V)
heap
Fibonacci O(lg V)
heap
amortized
Total
O(1)
O(V2)
O(lg V)
O(E lg V)
O(1)
O(E + V lg V)
amortized worst case
Introduction to Algorithms
4/5/11
23
initialization
relaxation
step
u
w(u, v)
d[v]
4/5/11
24