Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
(CS345/CS345A)
Lecture 18
Shortest Paths in Directed Graphs with Negative Weights
1
Problem Definition
Input: A directed graph 𝑮 = (𝑽, 𝑬) with 𝝎: 𝑬 → 𝑹 and a source vertex 𝒔 ∈ 𝑽
Notations:
𝒏 = |𝑽| , 𝒎 = |𝑬|
𝜹(𝒖, 𝒗) : distance from 𝒖 to 𝒗.
𝑷(𝒖, 𝒗) : The shortest path from 𝒖 to 𝒗.
Aim:
• Compute 𝜹(𝒔, 𝒗) for all 𝒗 ∈ 𝑽\{𝒔}
• Compute 𝑷(𝒔, 𝒗) for all 𝒗 ∈ 𝑽\{𝒔}
2
Last class
3
SHORTEST PATHS IN A GRAPH
Negative weights
BUT No negative cycle
4
Exploiting the Optimal subpath property
𝑷(𝒔, 𝒗)
𝟏 𝟐 … 𝒊
𝒔 𝒙 𝒗
5
The brilliant idea of Bellman Ford
𝒊
𝒊−𝟏
𝟏 𝟐 … 𝒊
𝒔 𝒙 𝒗
8
Recursive
The brilliantFormulation for 𝑳 𝒗,Ford
idea of Bellman 𝒊
Case 1: 𝑷(𝒗, 𝒊) has < 𝒊 edges 𝑳(𝒗, 𝒊) = 𝑳(𝒗, 𝒊 − 𝟏)
Case 2: 𝑷(𝒗, 𝒊) has exactly 𝒊 edges
𝑳(𝒗, 𝒊) = min 𝑳 𝒙, 𝒊 − 𝟏 + 𝝎 𝒙, 𝒗
(𝒙,𝒗 ∈𝑬)
𝒊
𝒊−𝟏
𝒊
𝒊−𝟏
But we do not
𝟏 Paths with edges ≤ 𝒊
know which one of
these 2 cases
occurs ? Paths with edges≤ 𝒊 − 𝟏
10
Base case: 𝑳(𝒗, 1)
−𝟏𝟎 𝒕
𝟏𝟐 𝟕
G 𝒙 𝒔
−𝟑
𝟓 𝒖
𝒚
𝑳 𝒔, 𝟏 = 𝟎.
If (𝒔, 𝒗) ∈ 𝑬 then 𝑳[𝒗, 𝟏] = ?𝝎 𝒔, 𝒗
else 𝑳[𝒗, 𝟏] = ?∞
11
𝑳(𝒗, 𝒊) : length of the shortest 𝒔 ⇝ 𝒗 having at most 𝒊 edges
12
BELLMAN-FORD ALGORITHM
For shortest paths in a graph with
Negative weights
BUT No negative cycle
13
Bellman-Ford’s algorithm
Bellman-Ford-algo(𝒔,𝑮)
{
For each 𝒗 ∈ 𝑽\{𝒔} do
If (𝒔, 𝒗) ∈ 𝑬 then 𝑳[𝒗, 𝟏] ?𝝎 𝒔, 𝒗
Initializing 𝑳[∗, 𝟏]
else 𝑳[𝒗, 𝟏] ?∞;
𝑳[𝒔, 𝟏] 𝟎; ?
For 𝒊 = 𝟐 to 𝒏 − 𝟏 do
{ For each 𝒗 ∈ 𝑽 do
{ 𝑳[𝒗, 𝒊]𝑳[𝒗, 𝒊 − 𝟏];
For each (𝒙, 𝒗) ∈ 𝑬 do Computing 𝑳[𝒗, 𝒊]
𝑳[𝒗, 𝒊] min( 𝑳[𝒗,
? 𝒊] , 𝑳[𝒙, 𝒊 − 𝟏]
? + 𝝎 𝒙, 𝒗 )
}
}
}
Lemma: 𝑳[𝒗, 𝒊] stores the shortest path from 𝒔 to 𝒗 having at most 𝒊 edges. 14
Bellman-Ford’s algorithm
15
GETTING INSIGHT INTO THE
BELLMAN-FORD ALGORITHM
An example
16
Execution of Bellman-Ford algorithm
-2
𝒗 𝒙
5 𝒚 ∞
6
8 6 7 𝒙 ∞
𝒔
-4 7
7 𝒗 𝟔
4 𝒚 𝒖 𝟕
𝒖
𝒔 𝟎
𝟏 𝟐 𝟑 𝟒
17
Execution of Bellman-Ford algorithm
-2
𝒗 𝒙
5 𝒚 ∞ 𝟏𝟏
6
8 6 7 𝒙 ∞ 𝟑
𝒔
-4 7
7 𝒗 𝟔 𝟔
4 𝒚 𝒖 𝟕 𝟕
𝒖
𝒔 𝟎 𝟎
𝟏 𝟐 𝟑 𝟒
18
Execution of Bellman-Ford algorithm
-2
𝒗 𝒙
5 𝒚 ∞ 𝟏𝟏 𝟗
6
8 6 7 𝒙 ∞ 𝟑 𝟑
𝒔
-4 7
7 𝒗 𝟔 𝟔 𝟏
4 𝒚 𝒖 𝟕 𝟕 𝟕
𝒖
𝒔 𝟎 𝟎 𝟎
𝟏 𝟐 𝟑 𝟒
19
Execution of Bellman-Ford algorithm
-2
𝒗 𝒙
5 𝒚 ∞ 𝟏𝟏 𝟗 𝟖
6
8 6 7 𝒙 ∞ 𝟑 𝟑 𝟑
𝒔
-4 7
7 𝒗 𝟔 𝟔 𝟏 𝟏
4 𝒚 𝒖 𝟕 𝟕 𝟕 𝟕
𝒖
𝒔 𝟎 𝟎 𝟎 𝟎
𝟏 𝟐 𝟑 𝟒
20
KEY OBSERVATIONS ON
BELLMAN-FORD ALGORITHM
21
Observation 1
Bellman-Ford-algo(𝒔,𝑮)
{
For each 𝒗 ∈ 𝑽\{𝒔} do
If (𝒔, 𝒗) ∈ 𝑬 then 𝑳[𝒗, 𝟏] ?𝝎 𝒔, 𝒗
else 𝑳[𝒗, 𝟏] ?∞;
𝑳[𝒔, 𝟏] 𝟎;? 𝑳
For 𝒊 = 𝟐 to 𝒏 − 𝟏 do
{ For each 𝒗 ∈ 𝑽 do 𝑽
{ 𝑳[𝒗, 𝒊]𝑳[𝒗, 𝒊 − 𝟏];
For each (𝒙, 𝒗) ∈ 𝑬 do
𝒊
𝑳[𝒗, 𝒊] min(𝑳[𝒗, 𝒊], 𝑳[𝒙, 𝒊 − 𝟏] + 𝝎 𝒙, 𝒗 )
} What if 𝑳 𝒗, 𝒊 = 𝑳 [𝒗, 𝒊 − 𝟏] for
} all 𝒗 ∈ 𝑽 ?
}
Answer: 𝑳 𝒗, 𝒋 = 𝑳 [𝒗, 𝒊 − 𝟏] for all 𝒋 > 𝒊 − 𝟏 , 𝒗 ∈ 𝑽 ?
22
Observations 2
Question: If there is no negative cycle,
then what happens if we execute Bellman-Ford algorithm for 𝒏 iteration?
Answer: 𝑳 𝒗, 𝒏 = 𝑳 𝒗, 𝒏 − 𝟏 for each 𝒗.
No negative cycle
Optimal Subpath
property holds
𝑳[𝒗, 𝒊] ≤
Length of shortest path? to 𝒗 with at most 𝒊 edges
𝑳[𝒗, 𝒏 − 𝟏] =
? path to 𝒗
Length of the shortest 23
= 𝑳[𝒗, 𝒏]
Observations 3
24
DETECTING NEGATIVE CYCLE
IN 𝑮
25
Question: Once the cycle 𝑪 has been reached by 𝒔,
say in 𝒊th iteration
𝒗𝟏 what will happen to labels of its vertices in future?
Answer: Label of at least one vertex in 𝑪 will change
in each subsequent iteration.
𝒗𝒌
𝒗𝟐 Proof: Consider 𝒋th iteration for any 𝒋 > 𝒊 .
𝑪: It follows from the algorithm that
a negative cycle 𝑳 𝒗𝟏 , 𝒋 ≤ 𝑳[𝒗𝒌 , 𝒋 − 𝟏] + 𝝎 𝒗𝒌 , 𝒗𝟏
𝑳 𝒗𝟐 , 𝒋 ≤ 𝑳[𝒗𝟏 , 𝒋 − 𝟏] + 𝝎 𝒗𝟏 , 𝒗𝟐
𝒗𝟑
𝒔 𝑳 𝒗𝟑 , 𝒋 ≤ 𝑳[𝒗𝟐 , 𝒋 − 𝟏] + 𝝎 𝒗𝟐 , 𝒗𝟑
:
𝒗𝟒 𝑳 𝒗𝒌−𝟏 , 𝒋 ≤ 𝑳[𝒗𝒌−𝟐 , 𝒋 − 𝟏] + 𝝎 𝒗𝒌−𝟐 , 𝒗𝒌−𝟏
𝒗𝟔 𝑳 𝒗𝒌 , 𝒋 ≤ 𝑳[𝒗𝒌−𝟏 , 𝒋 − 𝟏] + 𝝎 𝒗𝒌−𝟏 , 𝒗𝒌
If no label changes during 𝒋th iteration then
𝒗𝟓 𝑳 𝒗ℓ , 𝒋 =𝑳 𝒗ℓ , 𝒋 − 𝟏 for each 𝒗ℓ in cycle 𝑪
𝟎 ≤ 𝝎 𝒗𝒌 , 𝒗𝟏 + 𝝎 𝒗𝟏 , 𝒗𝟐 + ⋯ + 𝝎 𝒗𝒌−𝟏 , 𝒗𝒌
Weight of 𝑪
A contradiction !! 26
Detecting negative cycle in 𝑮
3. If 𝑳 𝒗, 𝒏 ≠ 𝑳 𝒗, 𝒏 − 𝟏 for any 𝒗 ∈ 𝑽 ,
declare “there is a negative cycle”
else
declare “𝑳 𝒗, 𝒏 − 𝟏 is the distance to 𝒗 for each 𝒗”
27
SHORTEST PATHS IN A GRAPH
28
Shortest paths in presence of negative weight
cycles
Given a directed 𝑮 = (𝑽, 𝑬) with 𝝎: 𝑬 → 𝑹,
if there is a negative weight cycle reachable from source,
• There is no polynomial time algorithm till date that can compute shortest
paths from 𝒔.
29