Anda di halaman 1dari 29

Design and Analysis of Algorithms

(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

Theorem: Given a directed 𝑮 = (𝑽, 𝑬) with 𝝎: 𝑬 → 𝑹,


if there are no negative cycles,
all shortest paths in 𝑮 possess optimal subpath property

3
SHORTEST PATHS IN A GRAPH

Negative weights
BUT No negative cycle

4
Exploiting the Optimal subpath property

𝑷(𝒔, 𝒗)
𝟏 𝟐 … 𝒊

𝒔 𝒙 𝒗

Observation: If 𝑷(𝒔, 𝒗) has 𝒊 edges, 𝑷(𝒔, 𝒙) has 𝒊 − 𝟏 edges.

5
The brilliant idea of Bellman Ford

All paths from 𝒔

How to search for the 𝒏 − 𝟏


shortest paths from 𝒔
among exponential number of paths?
6
Recursive
The brilliantFormulation for 𝑳 𝒗,Ford
idea of Bellman 𝒊
Aim: To compute 𝑳(𝒗, 𝒏 − 𝟏) for all 𝒗
𝒏−𝟏
𝒏−𝟐

𝒊
𝒊−𝟏

𝟏 Paths with edges ≤ 𝒊

All paths from 𝒔


Paths with edges≤ 𝒊 − 𝟏

𝑷(𝒗, 𝒊) : the shortest 𝒔 ⇝ 𝒗 path having at most 𝒊 edges.


7
𝑳(𝒗, 𝒊) : length of 𝑷(𝒗, 𝒊)
Recursive
The brilliantFormulation for 𝑳 𝒗,Ford
idea of Bellman 𝒊
Case 1: 𝑷(𝒗, 𝒊) has < 𝒊 edges 𝑳(𝒗, 𝒊) = 𝑳(𝒗, 𝒊 − 𝟏)
Case 2: 𝑷(𝒗, 𝒊) has exactly 𝒊 edges
There are 𝟐 cases
Important Note : Here we assume that
for 𝑷(𝒗, 𝒊). Can you
𝑷(𝒙, 𝒊 − 𝟏) does not pass through 𝒗. spot them ?
But it is indeed true. Otherwise there 𝒊
must be a negative cycle. The proof is 𝒊−𝟏
identical to the proof given in the
previous class. Do it as a homework.
𝟏 Paths with edges ≤ 𝒊

Paths with edges≤ 𝒊 − 𝟏

If this is longer than 𝑷(𝒙, 𝒊 − 𝟏), then


replacing it with 𝑷(𝒙, 𝒊 − 𝟏)
will only give a shorter 𝑷(𝒗, 𝒊).
A contradiction! 𝑷(𝒙,
? 𝒊 − 𝟏)

𝟏 𝟐 … 𝒊
𝒔 𝒙 𝒗
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 the exact in-
neighbor 𝒙 ?
Paths with edges≤ 𝒊 − 𝟏

Consider all in-neighbours of 𝒗.


𝟏 𝟐 … 𝒊
𝒔 𝒙 𝒗
9
𝑷(𝒙, 𝒊?− 𝟏)
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≤ 𝒊 − 𝟏

𝑳[𝒗, 𝒊] min( 𝑳(𝒗, ?𝒊 − 𝟏) , min 𝑳 𝒙, 𝒊?− 𝟏 + 𝝎 𝒙, 𝒗 )


(𝒙,𝒗 ∈𝑬)

10
Base case: 𝑳(𝒗, 1)

−𝟏𝟎 𝒕
𝟏𝟐 𝟕
G 𝒙 𝒔
−𝟑
𝟓 𝒖
𝒚

𝑳 𝒔, 𝟏 = 𝟎.
If (𝒔, 𝒗) ∈ 𝑬 then 𝑳[𝒗, 𝟏] = ?𝝎 𝒔, 𝒗
else 𝑳[𝒗, 𝟏] = ?∞
11
𝑳(𝒗, 𝒊) : length of the shortest 𝒔 ⇝ 𝒗 having at most 𝒊 edges

Aim: To compute 𝑳(𝒗, 𝒏 −?𝟏 ) for each 𝒗.

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

Theorem: Given a directed 𝑮 = (𝑽, 𝑬) with 𝝎: 𝑬 → 𝑹 and 𝒔 ∈ 𝑽 ,


if there is no negative cycle, then we can compute shortest paths from 𝒔
1. in O(𝒎𝒏) time
2. using O(𝒏𝟐 ) space

Question : How can we reduce the space to O(𝒏) ?


Answer: Computation of column 𝑳 ∗, 𝒊 requires
only column 𝑳[∗, 𝒊 − 𝟏].

 So keeping only two arrays of size O(𝒏) suffice 

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

Question: Suppose there is a path from 𝒔 to 𝒗 consisting of 𝒌 edges,


if after 𝒋th iteration 𝑳 𝒗, 𝒋 stores a finite value,
what is relation between 𝒋 and 𝒌?
Answer: 𝒋 ≤ 𝒌

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 𝑮

1. Execute Bellman-Ford algorithm.

2. Run one more iteration of the For loop

3. If 𝑳 𝒗, 𝒏 ≠ 𝑳 𝒗, 𝒏 − 𝟏 for any 𝒗 ∈ 𝑽 ,
declare “there is a negative cycle”
else
declare “𝑳 𝒗, 𝒏 − 𝟏 is the distance to 𝒗 for each 𝒗”

27
SHORTEST PATHS IN A GRAPH

Having Negative weight cycle

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 𝒔.

• A firm belief : no such algorithm can ever be designed unless 𝑷=𝑵𝑷.


[We shall revisit it when we discuss NP-complete problems towards the end
of this course]

29

Anda mungkin juga menyukai