MIT6 006F11 Lec15
MIT6 006F11 Lec15
• General Approach
• Negative Edges
• Optimal Substructure
Readings
CLRS, Sections 24 (Intro)
Motivation:
Shortest way to drive from A to B Google maps “get directions”
Application
• Find shortest path from CalTech to MIT
1
Lecture 15 Shortest Paths I: Intro 6.006 Fall 2011
Weighted Graphs:
Notation:
p
means p is a path from v0 to vk . (v0 ) is a path from v0 to v0 of weight
v0 −→ vk
0.
Definition:
Shortest path weight from u to v as
p
min w(p) : if ∃ any such path
δ(u, v) = u −→ v
∞
otherwise (v unreachable from u)
2
Lecture 15 Shortest Paths I: Intro 6.006 Fall 2011
Example:
A C E
5 3
1 5 3
1
3 3
0 1 2 1 4
S
2
1 1
2 3 4
B D F
Negative-Weight Edges:
• Natural in some applications (e.g., logarithms used for weights)
• If you have negative weight edges, you might also have negative weight cycles
=⇒ may make certain shortest paths undefined!
Example:
See Figure 2
3
Lecture 15 Shortest Paths I: Intro 6.006 Fall 2011
3 -2
B
4 E
S 2
-6 1
2
A D
d [v] ← ∞
Initialize: for v ∈ V :
Π [v] ← NIL
d[S] ← 0
Main: repeat
select edge (u, v) [somehow]
if d[v] > d[u] + w(u, v) :
“Relax” edge (u, v) d[v] ← d[u] + w(u, v)
π[v] ← u
until all edges have d[v] ≤ d[u] + w(u, v)
4
Lecture 15 Shortest Paths I: Intro 6.006 Fall 2011
Complexity:
Termination? (needs to be shown even without negative cycles)
Could be exponential time with poor choice of edges.
v0 v1 v2 v3 v4 v5 v6 v7
4 8 10 12 13 14
T(0) = 0 v0, v1 13
v1, v2 10 11 12
T(n+2) = 3 + 2T(n) v2, vn 11
v0, v2 4 6 8 9 10
T(n) = θ(2 )n/2
v2, vn 9
6 7 8
7
Figure 3: Running Generic Algorithm. The outgoing edges from v0 and v1 have
weight 4, the outgoing edges from v2 and v3 have weight 2, the outgoing edges from
v4 and v5 have weight 1.
Optimal Substructure:
Theorem: Subpaths of shortest paths are shortest paths
Let p = < v0 , v1 , . . . vk > be a shortest path
Let pij = < vi , vi+1 , . . . vj > 0 ≤ i ≤ j ≤ k
5
Lecture 15 Shortest Paths I: Intro 6.006 Fall 2011
Contradiction.
Triangle Inequality:
Theorem: For all u, v, x ∈ X, we have
Proof:
δ (u,v)
u v
δ (u,x) δ (x,v)
x
6
MIT OpenCourseWare
http://ocw.mit.edu
For information about citing these materials or our Terms of Use, visit: http://ocw.mit.edu/terms.