Professional Documents
Culture Documents
6.046J/18.401J
LECTURE 17
Shortest Paths I
•Properties of shortest paths
•Dijkstra’s algorithm
•Correctness
•Analysis
•Breadth-first search
Example:
v v v
4 v –2 –5 v 1
1 3 5
2 4
w(p) = –2
Proof.
δ(u, v)
u v
δ(u, x) δ(x, v)
x
Example: …
<0
u v
3
C 2 E
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ ∞ ∞
S: {}
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ ∞ ∞
S: { A }
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ 3 ∞
10 3 ∞ ∞
S: { A }
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ 3 ∞
10 3 ∞ ∞
S: { A, C }
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ 3 5
10 3 ∞ ∞
7 11 5
S: { A, C }
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ 3 5
10 3 ∞ ∞
7 11 5
S: { A, C, E }
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ 3 5
10 3 ∞ ∞
7 11 5
7 11 S: { A, C, E }
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ 3 5
10 3 ∞ ∞
7 11 5
7 11 S: { A, C, E, B }
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ 3 5
10 3 ∞ ∞
7 11 5
7 11 S: { A, C, E, B }
9
November Copyright © 2001-5 by Erik D. 25
Example of Dijkstra’s
algorithm
7 9
“D” ← EXTRACT-MIN(Q): 2
B D
10
8
0 A 1 4 7 9
3
Q: A B C D E C 2 E
0 ∞ ∞ ∞ ∞ 3 5
10 3 ∞ ∞
7 11 5
7 11 S: { A, C, E, B, D }
9
November Copyright © 2001-5 by Erik D. 26
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.
u
s
S, just before x y
adding u.
November Copyright © 2001-5 by Erik D. 32
Correctness — Part III
(continued)
S u
s
x y
a f h
d
b g
e i
c
Q:
November Copyright © 2001-5 by Erik D. 47
Example of breadth-first
search
0 a f h
d
b g
e i
c
0
Q: a
November Copyright © 2001-5 by Erik D. 48
Example of breadth-first
search
0 a 1 f h
d
1 b g
e i
c
1 1
Q: a b d
November Copyright © 2001-5 by Erik D. 49
Example of breadth-first
search
0 a 1 f h
d
1 b g
e i
2 c 2
1 2 2
Q: a b d c e
November Copyright © 2001-5 by Erik D. 50
Example of breadth-first
search
0 a 1 f h
d
1 b g
e i
2 c 2
2 2
Q: a b d c e
November Copyright © 2001-5 by Erik D. 51
Example of breadth-first
search
0 a 1 f h
d
1 b g
e i
2 c 2
2
Q: a b d c e
November Copyright © 2001-5 by Erik D. 52
Example of breadth-first
search
0 a 1 f h
d
3
1 b g
e i
2 c 2 3
3 3
Q: a b d c e g i
November Copyright © 2001-5 by Erik D. 53
Example of breadth-first
search
4
0 a 1 f h
d
3
1 b g
e i
2 c 2 3
3 4
Q: a b d c e g i f
November Copyright © 2001-5 by Erik D. 54
Example of breadth-first
search
4 4
0 a 1 f h
d
3
1 b g
e i
2 c 2 3
4 4
Q: a b d c e g i f h
November Copyright © 2001-5 by Erik D. 55
Example of breadth-first
search
4 4
0 a 1 f h
d
3
1 b g
e i
2 c 2 3
4
Q: a b d c e g i f h
November Copyright © 2001-5 by Erik D. 56
Example of breadth-first
search
4 4
0 a 1 f h
d
3
1 b g
e i
2 c 2 3
Q: a b d c e g i f h
November Copyright © 2001-5 by Erik D. 57
Example of breadth-first
search
4 4
0 a 1 f h
d
3
1 b g
e i
2 c 2 3
Q: a b d c e g i f h
November Copyright © 2001-5 by Erik D. 58
Correctness of BFS
while Q ≠ ∅
do u ← DEQUEUE(Q)
for each v ∈ Adj[u]
do if d[v] = ∞
then d[v] ← d[u] + 1
ENQUEUE(Q, v)
Key idea:
The FIFO Q in breadth-first search mimics
the priority queue Q in Dijkstra.
•Invariant: v comes after u in Q implies that
d[v] = d[u] or d[v] = d[u] + 1.