- Single-Source Shortest Paths Revisited
1.1 The Single-Source Shortest Path Problem.
Input: Directed graph G=(V,E) G = ( V , E ) , edge lengths ce c e for each e∈E e ∈ E , source vertex s∈V s ∈ V . [Can assume no parallel edges].
1.2 Goal: For every destination v∈V v ∈ V , compute the length(sum of edge costs) pf a shortest s-v path. - On Dijkstra’s Algorithm
2.1 Good news: O(mlogn) O ( m l o g n ) running time using heaps.
Bad news:
(1) Not always correct with negative edge lengths.
(2)Not very distributed (relevant for Internet routing)
Solution: The Bellman-Ford algorithm.
On Negative Cycles.
Question:How to define shortest path when G G has a negative cycle?
Soution #1: Compute the shortest s-v path, which cycles allowed.
Problem: Undefined or .
Solution #2: Computes shortest cycle-free s-v path.
Problem: NP-hard (no polynomial algorithm, unless P=NP)
Solution #3: (For now) Assume input graph has no negative cycles.
Single-Source Shortest Path Problem, Revisited.
Input: Directed graph G=(V,E) G = ( V , E ) , edge lengths ce c e ,
source vertex s∈V s ∈ V .
Goal: either
(A) For all destinations v∈V v ∈ V ,compute the length of a shortest s−v s − v path. OR
(B) Output a negative cycle.
Optimal Substructure(Informal)
Intuition: Exploit sequential nature of paths. Subpath of a shortest path should itself be shortest.
Issue: Not clear how to define “smaller” & “larger” subproblems.
Key idea: Artificially restrict the number of edges in a path.
Subproblem size ⇔ ⇔ Number of permitted edges.
Lemma: Let G=(V,E) G = ( V , E ) be a directed graph with edge lengths ce c e and source vertex s.
[G might or might not have a negative cycle]
For every v∈V,i∈{1,2...} v ∈ V , i ∈ { 1 , 2... } , let P= P = shortest s−v s − v path with at most i i edges. (Cycles are permitted)
Case 1: If P has edges, it is a shortest s−v s − v path with ≤(i−1) ≤ ( i − 1 ) edges.
Case 2: If P has i i edges with last hop(, v v ), then is a shortest s−w s − w path with ≤(i−1) ≤ ( i − 1 ) edges.
The Basic Algorithm
The Recurrence
Notation: Let Li,v= L i , v = minimum length of a s-v path with ≤i ≤ i edges.
− − With cycles allowed
Defined as +∞ + ∞ if no s−v s − v paths with ≤i ≤ i edges.
Recurrence: For every v∈V,i∈{1,2,...} v ∈ V , i ∈ { 1 , 2 , . . . }
If No Negative Cycles
Now: Suppose input graph G G has negative cycles.
Shortest paths do not have cycles.
[removing a cycle only decrease length]
⇒ ⇒ Have ≤(n−1) ≤ ( n − 1 ) egdes.
Point: If G G has negative cycle, only need to solve subproblems up to .
Point: If G G has no negative cycle, only need to solve subproblems up to .
Subproblems:
Compute Li,v L i , v for all i∈{0,1,...,n−1} i ∈ { 0 , 1 , . . . , n − 1 } and all v∈V v ∈ V .
The Bellman-Ford Algorithm
(1) Let A=2−D A = 2 − D array(indexed by i i and )
Base case:
A[0,s]=0;A[0,v]=+∞ A [ 0 , s ] = 0 ; A [ 0 , v ] = + ∞ for all v≠s v ≠ s /
For i=1,2,...,n−1 i = 1 , 2 , . . . , n − 1
For each v∈V v ∈ V
As discussed: IF G has no negative cycle, then algorithm is correct.
[with final answer = A[n−1,v]′s A [ n − 1 , v ] ′ s ]
Whatis the running time of the Bellman-Ford algorithm?
Reason: Total work is
O(n∑v∈Vin.deg(v))
O
(
n
∑
v
∈
V
i
n
.
d
e
g
(
v
)
)
=
O(mn)
O
(
m
n
)
入度。
Checking for a Negative Cycle
Question: What if the input graph G has a negative cycle.
Claim:
G has a no negative-cost cycle
⇔
⇔
In the extended Bellman-Ford algorithm,
A[n−1,v]=A[n,v]
A
[
n
−
1
,
v
]
=
A
[
n
,
v
]
for all
v∈V
v
∈
V
.
Consequence: Can check for a negative cycle just by running Bellman-Ford for one extra iteration(running time still
O(mn)
O
(
m
n
)
).
Proof of Claim
(
⇒
⇒
) Already proved in correctness of Bellman-Ford.
(
⇐
⇐
) Assume
A[n−1,v]=A[n,v]
A
[
n
−
1
,
v
]
=
A
[
n
,
v
]
for all
v∈V
v
∈
V
Consequence: Can check for a negative cycle just by jsut running Bellman-Ford for one extra iteration.
(running time still
O(mn)
O
(
m
n
)
) .
Let
d(v)
d
(
v
)
denote the common value of
A[n−1,v]
A
[
n
−
1
,
v
]
and
A[n,v]
A
[
n
,
v
]
.
Recall algorithm:
其中 A[n,v]→d(v),A[n−1,w]→d(w) A [ n , v ] → d ( v ) , A [ n − 1 , w ] → d ( w )
Thus: d(v)≤d(w)+cwv d ( v ) ≤ d ( w ) + c w v for all edges (w,v)∈E ( w , v ) ∈ E
Equivalently : d(v)−d(w)≤cwv d ( v ) − d ( w ) ≤ c w v