SlideShare a Scribd company logo
Data Structures and Algorithms – COMS21103
Shortest Paths Revisited
Negative Weights and All-Pairs
Benjamin Sach
inspired by slides by Ashley Montanaro
In today’s lectures we’ll be revisiting the shortest paths problem
In particular we’ll be interested in algorithms which allow negative edge weights
in a weighted, directed graph. . .
The shortest path from MVB to Temple Meads
(according to Google Maps)
and algorithms which compute the shortest path between all pairs of vertices
Part one
Single Source Shortest Paths with negative weights
Single source shortest paths with negative weights
in a weighted, directed graph. . .
Bellman-Ford’s algorithm solves the single source shortest paths problem
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
Single source shortest paths with negative weights
in a weighted, directed graph. . .
Bellman-Ford’s algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
to every other vertex
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
Single source shortest paths with negative weights
in a weighted, directed graph. . .
Bellman-Ford’s algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
The weights are allowed to be
to every other vertex
positive or negative
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
Single source shortest paths with negative weights
in a weighted, directed graph. . .
Bellman-Ford’s algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
The weights are allowed to be
to every other vertex
The graph is stored as an Adjacency List
positive or negative
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
Single source shortest paths with negative weights
in a weighted, directed graph. . .
Bellman-Ford’s algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
The weights are allowed to be
to every other vertex
We will not need any
non-elementary data structures
The graph is stored as an Adjacency List
positive or negative
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
Single source shortest paths with negative weights
in a weighted, directed graph. . .
Bellman-Ford’s algorithm solves the single source shortest paths problem
It finds the shortest path from
a given source vertex
The weights are allowed to be
to every other vertex
We will not need any
non-elementary data structures
Previously we saw that Dijkstra’s algorithm (implemented with a binary heap) solves this
problem in O((|V | + |E|) log |V |) time when the edges have non-negative weights
|V | is the number of vertices and |E| is the number of edges
The graph is stored as an Adjacency List
positive or negative
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
Negative weight cycles
If some of the edges in the graph have negative weights
the idea of a shortest path might not make sense:
If there is a path from s to t which includes a negative weight cycle,
A X
Z
Y B
What is the shortest path from A to B?
(-1)
1 1 1
A negative weight cycle is a path from a vertex v back to v such that
the sum of the edge weights is negative
there is no shortest path from s to t
A X
Z
Y B
-1 -1
1 1 1
Negative weight cycles
If some of the edges in the graph have negative weights
the idea of a shortest path might not make sense:
If there is a path from s to t which includes a negative weight cycle,
A X
Z
Y B
What is the shortest path from A to B?
(-1)
1 1 1
A negative weight cycle is a path from a vertex v back to v such that
the sum of the edge weights is negative
there is no shortest path from s to t
We will first discuss a (slightly) simpler version of Bellman-Ford
that assumes there are no such cycles
A X
Z
Y B
-1 -1
1 1 1
Most of the Bellman-Ford algorithm
MOSTOFBELLMAN-FORD(s)
weight(u, v) is the weight of
the edge from u to v
(u, v) ∈ E iff there is an
edge from u to v
dist(v) is the length of the shortest
path between s and v, found so far
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
Most of the Bellman-Ford algorithm
MOSTOFBELLMAN-FORD(s)
weight(u, v) is the weight of
the edge from u to v
(u, v) ∈ E iff there is an
edge from u to v
dist(v) is the length of the shortest
path between s and v, found so far
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
The algorithm repeatedly asks, for each edge (u, v)
“can I find a shorter route to v if I go via u?”
Most of the Bellman-Ford algorithm
MOSTOFBELLMAN-FORD(s)
weight(u, v) is the weight of
the edge from u to v
(u, v) ∈ E iff there is an
edge from u to v
dist(v) is the length of the shortest
path between s and v, found so far
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
s
u
v
The algorithm repeatedly asks, for each edge (u, v)
“can I find a shorter route to v if I go via u?”
Most of the Bellman-Ford algorithm
MOSTOFBELLMAN-FORD(s)
weight(u, v) is the weight of
the edge from u to v
(u, v) ∈ E iff there is an
edge from u to v
dist(v) is the length of the shortest
path between s and v, found so far
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
This is called RELAXING edge (u, v)
s
u
v
The algorithm repeatedly asks, for each edge (u, v)
“can I find a shorter route to v if I go via u?”
Most of the Bellman-Ford algorithm
MOSTOFBELLMAN-FORD(s)
for each vertex v, dist(v) is the length of the shortest path between s and v
Claim When the MOSTOFBELLMAN-FORD algorithm terminates,
weight(u, v) is the weight of
the edge from u to v
(u, v) ∈ E iff there is an
edge from u to v
dist(v) is the length of the shortest
path between s and v, found so far
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
If dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
(assuming there are no-negative weight cycles)
This is called RELAXING edge (u, v)
s
u
v
The algorithm repeatedly asks, for each edge (u, v)
“can I find a shorter route to v if I go via u?”
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
We start by setting dist(s) = 0 and every other dist(v) = ∞. . .
- the length of the best path
between s and v, found so far
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
∞ > 0 + (−1)
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
∞ > 0 + (−1)
-1
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
∞ > 0 + 1
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0 ∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
∞ > 0 + 1
1
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0 ∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0 ∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1
−1 < 1 + 1
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0 ∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1
−1 < 1 + 1
(so we don’t change dist)
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0 ∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0 ∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1
∞ > 1 + 3
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1
∞ > 1 + 3
4
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
−1 < ∞ + 2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
−1 < ∞ + 2
(so we don’t change dist)
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
∞ < ∞ + 2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
∞ < ∞ + 2 (dist is still ∞)
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0 ∞
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
∞ > −1 + (−2)
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
∞ > −1 + (−2)
-3
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
-3
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
-3
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
-3
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
-3
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
-3
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
-3
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
1 4
-3
1 > −3 + 1
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
1 > −3 + 1
-2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞ ∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
We now start iteration 1. . .
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
This is the end of iteration 1
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
this looks good
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
so does this
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
this doesn’t look so good
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
neither does this
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
neither does this
(it’s not the shortest path length)
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
neither does this
(it’s not the shortest path length)
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
neither does this
(it’s not the shortest path length)
This path has length −1
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
we aren’t done
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
but it seems like we made progresswe aren’t done
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
but it seems like we made progresswe aren’t done
does every iteration make progress?
RELAX(u,v)
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
In each iteration we RELAX every edge (u, v)
MOSTOFBELLMAN-FORD runs |V | iterations,
dist(v)
37 vertex v
s
(in the order they occur in the adjacency list)
0
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
We’re going to simulate
MOSTOFBELLMAN-FORD(s)
- the length of the best path
between s and v, found so far
-1
4
-3
-2
-2
2
How are things looking?
but it seems like we made progresswe aren’t done
does every iteration make progress?
are |V | iterations enough?
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
Iteration 1:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
Iteration 1:
“relax this”
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
Iteration 1:
“relax this”
-1
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
Iteration 1:
-1
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
Iteration 2:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
Iteration 2:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
Iteration 2:
“relax this”
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
Iteration 2:
“relax this”
-3
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
Iteration 2:
-3
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
Iteration 3:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
Iteration 3:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
“relax this”
Iteration 3:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
“relax this”
-2
Iteration 3:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
Iteration 3:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
Iteration 4:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
Iteration 4:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
“relax this”
Iteration 4:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
“relax this”
-1
Iteration 4:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
Iteration 4:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
Iteration 5:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
Iteration 5:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
“relax this”
Iteration 5:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
“relax this”
0
Iteration 5:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
0
Iteration 5:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
0
Iteration 6:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
0
Iteration 6:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
0
“relax this”
Iteration 6:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
0
“relax this”
-1
Iteration 6:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
t
This is a shortest path from s to t
-1
-3
-2
-1
0
-1
Iteration 6:
The proof idea
Imagine a different algorithm where in each iteration. . .
s
0
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
∞
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you only relax one edge (rather than all edges)
Further, imagine that (magically or otherwise), the edge that you relax in iteration i
is the i-th edge in a shortest path from s to some vertex t
When this algorithm terminates. . .
dist(t) is the length of the shortest path from s to t
t
This is a shortest path from s to t
-1
-3
-2
-1
0
-1
Iteration 6:
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
t
Consider the same
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
we relax this
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
at some point
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
we relax this
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
at some point
Don’t worry about what ? was before. RELAX picks the smaller of
? and 0 + (−1) so. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
we relax this
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
at some point
Don’t worry about what ? was before. RELAX picks the smaller of
? and 0 + (−1) so. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
Iteration 1:
-1
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
relax this
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
relax this
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 2:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
relax this
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
relax this
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 3:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
relax this
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
relax this
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 4:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
relax this
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
relax this
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 5:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
relax this
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
relax this
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
At some point in iteration i
t
-1
Iteration 6:
Consider the same
you relax the i-th edge in the shortest path from s to t. . .
shortest path from s to t
-3
-2
-1
0
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
t
-1
Iteration 6:
Consider the same
shortest path from s to t
-3
-2
-1
0
-1
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
So after enough iterations. . .
dist(t) is the length of a path from s to t
t
-1
Iteration 6:
Consider the same
shortest path from s to t
-3
-2
-1
0
-1
which is at least as short as the shortest path. . .
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
So after enough iterations. . .
dist(t) is the length of a path from s to t
t
-1
Iteration 6:
Consider the same
shortest path from s to t
-3
-2
-1
0
-1
which is at least as short as the shortest path. . .
In other words dist(t) is the length of a shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
So after enough iterations. . .
dist(t) is the length of a path from s to t
t
-1
Iteration 6:
Consider the same
shortest path from s to t
-3
-2
-1
0
-1
which is at least as short as the shortest path. . .
In other words dist(t) is the length of a shortest path from s to t
how many iterations are needed?
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
t
-1
Iteration 6:
Consider the same
shortest path from s to t
-3
-2
-1
0
-1
For this argument to hold we need to do one iteration
for every edge in the shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
t
-1
Iteration 6:
Consider the same
shortest path from s to t
-3
-2
-1
0
-1
For this argument to hold we need to do one iteration
We will now prove that if there are no negative cycles in the graph,
the shortest path between s and t contains at most |V | edges
for every edge in the shortest path from s to t
The proof idea
Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . .
s
0
?
?
?
?
?
?
?
?
?
?
?
3
1
1
1
4
-2
2
2
-2
-1
1
1
-1
1
3 -1
-1
2
1
-2
1
you relax every edge (rather than one edge)
t
-1
Iteration 6:
Consider the same
shortest path from s to t
-3
-2
-1
0
-1
For this argument to hold we need to do one iteration
We will now prove that if there are no negative cycles in the graph,
the shortest path between s and t contains at most |V | edges
for every edge in the shortest path from s to t
and therefore |V | iterations will be enough
Claim if there are no negative weight cycles in the graph,
there is a shortest path between s and t containing at most |V | edges
(or there is no path from s to t)
Claim if there are no negative weight cycles in the graph,
there is a shortest path between s and t containing at most |V | edges
Consider a path between s and t with a cycle. . .
(or there is no path from s to t)
Claim if there are no negative weight cycles in the graph,
there is a shortest path between s and t containing at most |V | edges
Consider a path between s and t with a cycle. . .
s
t
3 1 2 -1
-1
3
-1
(or there is no path from s to t)
Claim if there are no negative weight cycles in the graph,
there is a shortest path between s and t containing at most |V | edges
Consider a path between s and t with a cycle. . .
s
t
3 1 2 -1
-1
3
-1
As there are no negative weight cycles
deleting this cycle from the path cannot increase it’s length
(or there is no path from s to t)
Claim if there are no negative weight cycles in the graph,
there is a shortest path between s and t containing at most |V | edges
Consider a path between s and t with a cycle. . .
s
t
3 1 2 -1
-1
3
-1
As there are no negative weight cycles
deleting this cycle from the path cannot increase it’s length
path length 6
(or there is no path from s to t)
Claim if there are no negative weight cycles in the graph,
there is a shortest path between s and t containing at most |V | edges
Consider a path between s and t with a cycle. . .
s
t
3 1 2 -1
-1
3
-1
As there are no negative weight cycles
deleting this cycle from the path cannot increase it’s length
path length 6
path length 5
(or there is no path from s to t)
Claim if there are no negative weight cycles in the graph,
there is a shortest path between s and t containing at most |V | edges
Consider a path between s and t with a cycle. . .
s
t
3 1 2 -1
-1
3
-1
As there are no negative weight cycles
deleting this cycle from the path cannot increase it’s length
path length 6
path length 5
Therefore, there is a shortest path between s and t containing no cycles
(or there is no path from s to t)
Claim if there are no negative weight cycles in the graph,
there is a shortest path between s and t containing at most |V | edges
Consider a path between s and t with a cycle. . .
s
t
3 1 2 -1
-1
3
-1
As there are no negative weight cycles
deleting this cycle from the path cannot increase it’s length
path length 6
path length 5
Therefore, there is a shortest path between s and t containing no cycles
A path with no cycles enters each vertex at most once
so contains at most |V | edges
(or there is no path from s to t)
MOSTOFBELLMAN-FORD Summary
for each vertex v, dist(v) is the distance between s and v
Claim When the MOSTOFBELLMAN-FORD algorithm terminates,
(assuming there are no-negative weight cycles)
MOSTOFBELLMAN-FORD Summary
for each vertex v, dist(v) is the distance between s and v
Claim When the MOSTOFBELLMAN-FORD algorithm terminates,
(assuming there are no-negative weight cycles)
If there is no path between s and v then
at termination, dist(v) = ∞
by the algorithm description, MOSTOFBELLMAN-FORD
doesn’t find paths that aren’t there
MOSTOFBELLMAN-FORD Summary
for each vertex v, dist(v) is the distance between s and v
Claim When the MOSTOFBELLMAN-FORD algorithm terminates,
(assuming there are no-negative weight cycles)
If there is no path between s and v then
at termination, dist(v) = ∞
by the algorithm description, MOSTOFBELLMAN-FORD
doesn’t find paths that aren’t there
If there is a shortest path between s and v then
(assuming there are no-negative weight cycles)
it contains at most |V | edges
MOSTOFBELLMAN-FORD Summary
for each vertex v, dist(v) is the distance between s and v
Claim When the MOSTOFBELLMAN-FORD algorithm terminates,
(assuming there are no-negative weight cycles)
If there is no path between s and v then
at termination, dist(v) = ∞
by the algorithm description, MOSTOFBELLMAN-FORD
doesn’t find paths that aren’t there
If there is a shortest path between s and v then
(assuming there are no-negative weight cycles)
it contains at most |V | edges
After each iteration, the algorithm
has taken (at least) one more ‘step’ along this path
MOSTOFBELLMAN-FORD Summary
for each vertex v, dist(v) is the distance between s and v
Claim When the MOSTOFBELLMAN-FORD algorithm terminates,
(assuming there are no-negative weight cycles)
If there is no path between s and v then
at termination, dist(v) = ∞
by the algorithm description, MOSTOFBELLMAN-FORD
doesn’t find paths that aren’t there
If there is a shortest path between s and v then
(assuming there are no-negative weight cycles)
it contains at most |V | edges
After each iteration, the algorithm
has taken (at least) one more ‘step’ along this path
After |V | iterations of the algorithm
dist(v) is the length of the shortest path between s and v
So what is the rest of the algorithm?
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
So what is the rest of the algorithm?
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
We’ve added a final check which determines whether
another iteration would find an even shorter path from s to some v
So what is the rest of the algorithm?
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
We’ve added a final check which determines whether
another iteration would find an even shorter path from s to some v
We will prove that this is happens if and only if
there is a negative weight cycle
BELLMAN-FORD without negative weight cycles
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
We first argue that the algorithm still works when there is no negative weight cycle
BELLMAN-FORD without negative weight cycles
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
After the first |V | iterations
for each vertex v,
We first argue that the algorithm still works when there is no negative weight cycle
(which haven’t changed),
dist(v) is the length
of the shortest path between s and v
BELLMAN-FORD without negative weight cycles
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
After the first |V | iterations
for each vertex v,
‘Negative weight cycle found’If the final check outputs:
then there is a path from s to some v (via u)
We first argue that the algorithm still works when there is no negative weight cycle
(which haven’t changed),
dist(v) is the length
of the shortest path between s and v
with length dist(u) + weight(u, v) < dist(v)
BELLMAN-FORD without negative weight cycles
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
After the first |V | iterations
for each vertex v,
‘Negative weight cycle found’If the final check outputs:
then there is a path from s to some v (via u)
We first argue that the algorithm still works when there is no negative weight cycle
(which haven’t changed),
dist(v) is the length
of the shortest path between s and v
with length dist(u) + weight(u, v) < dist(v)
i.e. a path which is shorter than the shortest path
BELLMAN-FORD without negative weight cycles
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
After the first |V | iterations
for each vertex v,
‘Negative weight cycle found’If the final check outputs:
then there is a path from s to some v (via u)
this is a contradiction.
We first argue that the algorithm still works when there is no negative weight cycle
(which haven’t changed),
dist(v) is the length
of the shortest path between s and v
with length dist(u) + weight(u, v) < dist(v)
i.e. a path which is shorter than the shortest path
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
We have that,
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
We have that,
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
weight(v1, v2) dist(v2) − dist(v1)
We have that,
because we ran the following checks
but found no negative weight cycle:
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
weight(v1, v2) dist(v2) − dist(v1)
So,
We have that,
dist(v2) dist(v1) + weight(v1, v2)
(then rearrange)
because we ran the following checks
but found no negative weight cycle:
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
We have that,
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
We have that,
and by the same argument. . .
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
weight(v2, v3) dist(v3) − dist(v2)
We have that,
and by the same argument. . .
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
weight(v2, v3) dist(v3) − dist(v2)
weight(v3, v4) dist(v4) − dist(v3)
We have that,
and by the same argument. . .
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
weight(v2, v3) dist(v3) − dist(v2)
weight(v3, v4) dist(v4) − dist(v3)
We have that,
and by the same argument. . .
weight(v4, v5) dist(v5) − dist(v4)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
weight(v2, v3) dist(v3) − dist(v2)
weight(v3, v4) dist(v4) − dist(v3)
We have that,
and by the same argument. . .
weight(v4, v5) dist(v5) − dist(v4)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
weight(v2, v3) dist(v3) − dist(v2)
weight(v3, v4) dist(v4) − dist(v3)
We have that,
and by the same argument. . .
weight(v4, v5) dist(v5) − dist(v4)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
weight(v2, v3) dist(v3) − dist(v2)
weight(v3, v4) dist(v4) − dist(v3)
We have that,
and by the same argument. . .
weight(v4, v5) dist(v5) − dist(v4)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(v1, v2) dist(v2) − dist(v1)
weight(v2, v3) dist(v3) − dist(v2)
weight(v3, v4) dist(v4) − dist(v3)
We have that,
and by the same argument. . .
weight(v4, v5) dist(v5) − dist(v4)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(vj, vj+1) dist(vj+1) − dist(vj)
In general for every edge (vj, vj+1), we have
(where vk+1 = v1)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(vj, vj+1) dist(vj+1) − dist(vj)
In general for every edge (vj, vj+1), we have
(where vk+1 = v1)
But the cycle has negative weight so,
k
j=1
weight(vj, vj+1) < 0
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(vj, vj+1) dist(vj+1) − dist(vj)
In general for every edge (vj, vj+1), we have
(where vk+1 = v1)
However, if we sum all the edge weights using the top expressions we get,
But the cycle has negative weight so,
k
j=1
weight(vj, vj+1) < 0
k
j=1
weight(vj, vj+1)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(vj, vj+1) dist(vj+1) − dist(vj)
In general for every edge (vj, vj+1), we have
(where vk+1 = v1)
However, if we sum all the edge weights using the top expressions we get,
But the cycle has negative weight so,
k
j=1
weight(vj, vj+1) < 0
k
j=1
weight(vj, vj+1)
k
j=1
dist(vj+1) − dist(vj)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(vj, vj+1) dist(vj+1) − dist(vj)
In general for every edge (vj, vj+1), we have
(where vk+1 = v1)
However, if we sum all the edge weights using the top expressions we get,
But the cycle has negative weight so,
k
j=1
weight(vj, vj+1) < 0
k
j=1
dist(vj+1) −
k
j=1
dist(vj)
k
j=1
weight(vj, vj+1)
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(vj, vj+1) dist(vj+1) − dist(vj)
In general for every edge (vj, vj+1), we have
(where vk+1 = v1)
However, if we sum all the edge weights using the top expressions we get,
But the cycle has negative weight so,
k
j=1
weight(vj, vj+1) < 0
k
j=1
dist(vj+1) −
k
j=1
dist(vj)
k
j=1
weight(vj, vj+1) = 0
BELLMAN-FORD with a negative weight cycle
Let v1, v2, . . . vk ∈ V be a negative weight cycle
and assume for a contradiction, that it wasn’t reported by the algorithm
v1
v2
v3
v4
v5
v6
vk
weight(vj, vj+1) dist(vj+1) − dist(vj)
In general for every edge (vj, vj+1), we have
(where vk+1 = v1)
However, if we sum all the edge weights using the top expressions we get,
But the cycle has negative weight so,
k
j=1
weight(vj, vj+1) < 0
k
j=1
dist(vj+1) −
k
j=1
dist(vj)
k
j=1
weight(vj, vj+1) = 0
Contradiction!
BELLMAN-FORD Summary
for each vertex v, dist(v) is the distance between s and v
When the BELLMAN-FORD algorithm terminates,
(or it reports that there is a negative weight cycle)
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
BELLMAN-FORD Summary
for each vertex v, dist(v) is the distance between s and v
When the BELLMAN-FORD algorithm terminates,
(or it reports that there is a negative weight cycle)
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
What is the time complexity of the BELLMAN-FORD algorithm?
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
O(|V |) time
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
O(|V |) time
(we store dist in an array of length |V |)
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
O(|V |) time
(we store dist in an array of length |V |)
O(1) time
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
O(|V |) time
(we store dist in an array of length |V |)
O(1) time
O(|E|) time for each iteration
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
O(|V |) time
(we store dist in an array of length |V |)
O(1) time
O(|E|) time for each iteration
. . . and there are |V | iterations
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
O(|V |) time
(we store dist in an array of length |V |)
O(1) time
O(|V ||E|) time overall
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
O(|V |) time
(we store dist in an array of length |V |)
O(|E|) time
O(1) time
O(|V ||E|) time overall
Time Complexity
BELLMAN-FORD(s)
For all v, set dist(v) = ∞
set dist(s) = 0
For i = 1, 2, . . . , |V |,
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
dist(v) = dist(u) + weight(u, v)
For every edge (u, v) ∈ E
if dist(v) > dist(u) + weight(u, v)
Output ‘Negative weight cycle found’
O(|V ||E|) time
O(|V |) time
(we store dist in an array of length |V |)
O(|E|) time
O(1) time
so overall the BELLMAN-FORD algorithm takes
O(|V ||E|) time overall
Summary
If there is a negative weight cycle, the algorithm reports this and aborts
The BELLMAN-FORD algorithm runs in O(|V ||E|) time
This is not as good as DIJKSTRA’s algorithm which runs in O((|V | + |E|) log |V |) time
The BELLMAN-FORD algorithm solves the single source shortest paths problem
and uses no non-elementary data structures
in a directed, weighted graph with positive and negative edge weights
(in this case, the problem is not well-defined)
(when implemented using a binary heap)
However, DIJKSTRA’s algorithm only works for graphs with
non-negative edge weights
End of part one
Part two
All-Pairs Shortest Paths
All-Pairs Shortest Paths
in a weighted, directed graph. . .
In previous lectures, we have focused on the single source shortest paths problem
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
All-Pairs Shortest Paths
in a weighted, directed graph. . .
In previous lectures, we have focused on the single source shortest paths problem
i.e. in finding the shortest paths from a single given source vertex
to every other vertex
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
All-Pairs Shortest Paths
in a weighted, directed graph. . .
In previous lectures, we have focused on the single source shortest paths problem
i.e. in finding the shortest paths from a single given source vertex
to every other vertex
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
-21
2
1
1 2 4
3
1
2
-1
A
B E
D
C F
G
Use something we saw before
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
Use something we saw before
We already have two options:
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
Use something we saw before
We already have two options:
If the graph has non-negative edge weights,
we could run DIJKSTRA’s algorithm |V | times, once with each vertex as the source
this takes O(|V ||E| log |V |) time
(if the priority queue is a binary heap)
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
Use something we saw before
We already have two options:
If the graph has non-negative edge weights,
we could run DIJKSTRA’s algorithm |V | times, once with each vertex as the source
this takes O(|V ||E| log |V |) time
(if the priority queue is a binary heap)
If the graph has positive and negative edge weights,
we could run BELLMAN-FORD’s algorithm |V | times, once for each vertex
this takes O(|V |2|E|) time
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good?
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better?
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
The output contains the length of the shortest path between every pair of vertices
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
The output contains the length of the shortest path between every pair of vertices
There are |V | · (|V | − 1) pairs of vertices
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
The output contains the length of the shortest path between every pair of vertices
so we can’t expect to do better than O(|V |2) time
There are |V | · (|V | − 1) pairs of vertices
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
Imagine that |E| ≈
|V |2
4 (the graph is very dense)
e.g. each vertex has an edge to about half of the other vertices
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
Imagine that |E| ≈
|V |2
4 (the graph is very dense)
e.g. each vertex has an edge to about half of the other vertices
this becomes O(|V |3 log |V |)
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
Imagine that |E| ≈
|V |2
4 (the graph is very dense)
e.g. each vertex has an edge to about half of the other vertices
this becomes O(|V |4)
this becomes O(|V |3 log |V |)
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
imagine that |E| ≈ 5|V | (the graph is very sparse)
e.g. each vertex has an edge to about 5 other vertices
Instead,
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
imagine that |E| ≈ 5|V | (the graph is very sparse)
e.g. each vertex has an edge to about 5 other vertices
Instead,
this becomes O(|V |2 log |V |)
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
imagine that |E| ≈ 5|V | (the graph is very sparse)
e.g. each vertex has an edge to about 5 other vertices
Instead,
this becomes O(|V |2 log |V |)
this becomes O(|V |3)
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
Are these any good? Can we do better? How does |V | compare to |E|?
imagine that |E| ≈ 5|V | (the graph is very sparse)
e.g. each vertex has an edge to about 5 other vertices
Instead,
this becomes O(|V |2 log |V |)
this becomes O(|V |3)
O(|V |2 log |V |) is a lot better than O(|V |3)
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
In this lecture, we will discuss JOHNSON’s algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
in graphs with positive and negative edge weights
What should we do if we want to find the shortest paths from every vertex
to every other vertex?
If the graph has non-negative edge weights,
repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time
repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time
If the graph has positive and negative edge weights,
In this lecture, we will discuss JOHNSON’s algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
in graphs with positive and negative edge weights
It will use both DIJKSTRA’s algorithm (implemented with a binary heap)
and BELLMAN-FORD to achieve this
JOHNSON’s algorithm - the overall approach
We have already seen one algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
JOHNSON’s algorithm - the overall approach
We have already seen one algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
If the graph had non-negative edge weights,
we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm
JOHNSON’s algorithm - the overall approach
We have already seen one algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
If the graph had non-negative edge weights,
we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm
However, we are interested in graphs with both positive and negative edge weights,
JOHNSON’s algorithm - the overall approach
We have already seen one algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
If the graph had non-negative edge weights,
we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm
However, we are interested in graphs with both positive and negative edge weights,
A
B
C
D
1 4
-2
-1
0
1
JOHNSON’s algorithm - the overall approach
We have already seen one algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
If the graph had non-negative edge weights,
we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm
The approach employed by JOHNSON’s algorithm is to reweight the edges
However, we are interested in graphs with both positive and negative edge weights,
so that the resulting graph has non-negative edge weights,
and then (repeatedly) run DIJKSTRA’s algorithm
A
B
C
D
1 4
-2
-1
0
1
JOHNSON’s algorithm - the overall approach
We have already seen one algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
If the graph had non-negative edge weights,
we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm
The approach employed by JOHNSON’s algorithm is to reweight the edges
However, we are interested in graphs with both positive and negative edge weights,
so that the resulting graph has non-negative edge weights,
and then (repeatedly) run DIJKSTRA’s algorithm
A
B
C
D
1 4
becomes-2
-1
0
1
A
B
C
D
5
1
0
0
0
0
JOHNSON’s algorithm - the overall approach
We have already seen one algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
If the graph had non-negative edge weights,
we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm
The approach employed by JOHNSON’s algorithm is to reweight the edges
However, we are interested in graphs with both positive and negative edge weights,
so that the resulting graph has non-negative edge weights,
and then (repeatedly) run DIJKSTRA’s algorithm
A
B
C
D
1 4
becomes-2
-1
0
1
A
B
C
D
5
1
0
0
0
0
JOHNSON’s algorithm - the overall approach
We have already seen one algorithm for all-pairs shortest paths
which takes O(|V ||E| log |V |) time
If the graph had non-negative edge weights,
we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm
The approach employed by JOHNSON’s algorithm is to reweight the edges
However, we are interested in graphs with both positive and negative edge weights,
so that the resulting graph has non-negative edge weights,
and then (repeatedly) run DIJKSTRA’s algorithm
A
B
C
D
1 4
becomes-2
-1
0
1
A
B
C
D
5
1
0
0
0
0
the shortest path length may change
but the route remains the same
A first attempt
One natural attempt at reweighting is to increase every edge weight
by the same amount. . .
A first attempt
One natural attempt at reweighting is to increase every edge weight
A
B
C
D
1 4
-2
-1
0
1
by the same amount. . .
A first attempt
One natural attempt at reweighting is to increase every edge weight
A
B
C
D
1 4
becomes-2
-1
0
1
A
B
C
D
14
10
11
8
9
11
if we add 10 to every edge. . .
by the same amount. . .
A first attempt
One natural attempt at reweighting is to increase every edge weight
A
B
C
D
1 4
becomes-2
-1
0
1
A
B
C
D
14
10
11
8
9
11
if we add 10 to every edge. . .
by the same amount. . .
The shortest path from C to B
goes this way in the original graph
A first attempt
One natural attempt at reweighting is to increase every edge weight
A
B
C
D
1 4
becomes-2
-1
0
1
A
B
C
D
14
10
11
8
9
11
if we add 10 to every edge. . .
by the same amount. . .
The shortest path from C to B
goes this way in the original graph
The shortest path from C to B
goes this way in the reweighted graph
A first attempt
One natural attempt at reweighting is to increase every edge weight
A
B
C
D
1 4
becomes-2
-1
0
1
A
B
C
D
14
10
11
8
9
11
if we add 10 to every edge. . .
by the same amount. . .
Unfortunately, if we reweight the graph like this,
both the shortest paths lengths and the routes might change
The shortest path from C to B
goes this way in the original graph
The shortest path from C to B
goes this way in the reweighted graph
A first attempt
One natural attempt at reweighting is to increase every edge weight
A
B
C
D
1 4
becomes-2
-1
0
1
A
B
C
D
14
10
11
8
9
11
if we add 10 to every edge. . .
by the same amount. . .
Unfortunately, if we reweight the graph like this,
both the shortest paths lengths and the routes might change
this doesn’t look good
The shortest path from C to B
goes this way in the original graph
The shortest path from C to B
goes this way in the reweighted graph
Reweighting based on vertex potential
We are going to associate a value h(v) with each vertex v ∈ V
- we will call this the potential of v
To overcome this, we are going to reweight each edge differently
the new weight of each edge will depend on which vertices it connects
Reweighting based on vertex potential
We are going to associate a value h(v) with each vertex v ∈ V
- we will call this the potential of v
To overcome this, we are going to reweight each edge differently
the new weight of each edge will depend on which vertices it connects
A Bweight(A,B)
Reweighting based on vertex potential
We are going to associate a value h(v) with each vertex v ∈ V
- we will call this the potential of v
To overcome this, we are going to reweight each edge differently
the new weight of each edge will depend on which vertices it connects
A B
h(A) h(B)
weight(A,B)
Reweighting based on vertex potential
We are going to associate a value h(v) with each vertex v ∈ V
- we will call this the potential of v
To overcome this, we are going to reweight each edge differently
the new weight of each edge will depend on which vertices it connects
A B
h(A) h(B)
weight(A,B)
becomes
weight’(A,B)
where weight’(A,B) = weight(A,B) + h(A) − h(B)
A B
Reweighting based on vertex potential
We are going to associate a value h(v) with each vertex v ∈ V
- we will call this the potential of v
To overcome this, we are going to reweight each edge differently
the new weight of each edge will depend on which vertices it connects
A B
h(A) h(B)
weight(A,B)
becomes
weight’(A,B)
where weight’(A,B) = weight(A,B) + h(A) − h(B)
A B
Why is this better than the first attempt?
Reweighted paths
Each each vertex v ∈ V has a value h(v) - called the potential of v
we will pick these values carefully later
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
we will pick these values carefully later
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6-2 3 1 7 4
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6-2 3 1 7 4
Pick some potential values
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
-2 3 1 7 4
Pick some potential values
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
-2 3 1 7 4
(for now you can think of the values as arbitrary)
Pick some potential values
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
-2 3 1 7 4
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
weight’(v1, v2) = weight(v1, v2) + h(v1) − h(v2)
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
weight’(v1, v2) = weight(v1, v2) + h(v1) − h(v2)
=(−2) + (5) − (2) = 1
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
- this path has length 13
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
- this path has length 13
- this path has length 14
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
- this path has length 13
- this path has length 14 = 13 + 5 − 4 = 13 + h(v1) − h(v6)
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
- this path has length 13
- this path has length 14 = 13 + 5 − 4 = 13 + h(v1) − h(v6)
Reweighted paths
Consider the following path as an example. . .
Each each vertex v ∈ V has a value h(v) - called the potential of v
v1
we will pick these values carefully later
v2 v3 v4 v5 v6
h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4
where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1)
-2 3 1 7 4
Reweight the whole graph using weight’ (we don’t just reweight this path). . .
v1 v2 v3 v4 v5 v61 5 0 6 2
- this path has length 13
- this path has length 14 = 13 + 5 − 4 = 13 + h(v1) − h(v6)
is this a coincidence?
Reweighted paths
Consider an arbitrary path. . .
v1 v2 v3 v4 v5 vk
h(v1) h(v2) h(v3) h(v4) h(v5) h(vk)
? ? ? ?
Reweighted paths
Consider an arbitrary path. . .
v1 v2 v3 v4 v5 vk
h(v1) h(v2) h(v3) h(v4) h(v5) h(vk)
? ? ? ?
In the original graph, the path length is
k−1
i=1
weight(vi, vi+1)
Reweighted paths
Consider an arbitrary path. . .
v1 v2 v3 v4 v5 vk
h(v1) h(v2) h(v3) h(v4) h(v5) h(vk)
? ? ? ?
In the original graph, the path length is
k−1
i=1
weight(vi, vi+1)
In the reweighted graph, the path length is
k−1
i=1
weight’(vi, vi+1)
Reweighted paths
Consider an arbitrary path. . .
v1 v2 v3 v4 v5 vk
h(v1) h(v2) h(v3) h(v4) h(v5) h(vk)
? ? ? ?
In the original graph, the path length is
k−1
i=1
weight(vi, vi+1)
In the reweighted graph, the path length is
k−1
i=1
weight’(vi, vi+1) =
k−1
i=1
weight(vi, vi+1) + h(vi) − h(vi+1)
Reweighted paths
Consider an arbitrary path. . .
v1 v2 v3 v4 v5 vk
h(v1) h(v2) h(v3) h(v4) h(v5) h(vk)
? ? ? ?
In the original graph, the path length is
k−1
i=1
weight(vi, vi+1)
In the reweighted graph, the path length is
k−1
i=1
weight’(vi, vi+1) =
k−1
i=1
weight(vi, vi+1) + h(vi) − h(vi+1)
=
k−1
i=1
weight(vi, vi+1) +h(v1)−h(vk)
Reweighted paths
Consider an arbitrary path. . .
v1 v2 v3 v4 v5 vk
h(v1) h(v2) h(v3) h(v4) h(v5) h(vk)
? ? ? ?
In the original graph, the path length is
k−1
i=1
weight(vi, vi+1)
In the reweighted graph, the path length is
k−1
i=1
weight’(vi, vi+1) =
k−1
i=1
weight(vi, vi+1) + h(vi) − h(vi+1)
=
k−1
i=1
weight(vi, vi+1) +h(v1)−h(vk)
So the weight of a path only changes by the potential values of the end points. . .
Reweighted shortest paths
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Reweighted shortest paths
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
Reweighted shortest paths
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
u vu v
path p1
path p2
Reweighted shortest paths
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
u vu v
path p1
path p2
Let l1 be the length of p1
in the original graph
Reweighted shortest paths
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
u vu v
path p1
path p2
Let l1 be the length of p1
in the original graph
Let l2 be the length of p2
in the original graph
Reweighted shortest paths
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
u vu v
path p1
path p2
Let l1 be the length of p1
in the original graph
Let l2 be the length of p2
in the original graph
l1 + h(u) − h(v)
in the reweighted graph
is the length of p1
Reweighted shortest paths
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
u vu v
path p1
path p2
Let l1 be the length of p1
in the original graph
Let l2 be the length of p2
in the original graph
l1 + h(u) − h(v)
in the reweighted graph
l2 + h(u) − h(v)
in the reweighted graph
is the length of p1
is the length of p2
Reweighted shortest paths
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
u vu v
path p1
path p2
Let l1 be the length of p1
in the original graph
Let l2 be the length of p2
in the original graph
l1 + h(u) − h(v)
in the reweighted graph
l2 + h(u) − h(v)
in the reweighted graph
is the length of p1
is the length of p2
l1 l2 if and only if
l1 + h(u) − h(v) l2 + h(u) − h(v)
Reweighted negative cycles
Let v1, v2, . . . vk ∈ V
v1
v2
v3v4
v5
v6
vk
be a negative weight cycle
Reweighted negative cycles
Let v1, v2, . . . vk ∈ V
v1
v2
v3v4
v5
v6
vk
(where vk+1 = v1)
The weight of this cycle
k
i=1
weight(vi, vi+1) < 0
in the original graph is,
be a negative weight cycle
Reweighted negative cycles
Let v1, v2, . . . vk ∈ V
v1
v2
v3v4
v5
v6
vk
(where vk+1 = v1)
The weight of this cycle
k
i=1
weight(vi, vi+1) < 0
in the original graph is,
The weight of this cycle in the reweighted graph is
be a negative weight cycle
Reweighted negative cycles
Let v1, v2, . . . vk ∈ V
v1
v2
v3v4
v5
v6
vk
(where vk+1 = v1)
The weight of this cycle
k
i=1
weight(vi, vi+1) < 0
in the original graph is,
k
i=1
weight’(vi, vi+1)
The weight of this cycle in the reweighted graph is
be a negative weight cycle
Reweighted negative cycles
Let v1, v2, . . . vk ∈ V
v1
v2
v3v4
v5
v6
vk
(where vk+1 = v1)
The weight of this cycle
k
i=1
weight(vi, vi+1) < 0
in the original graph is,
k
i=1
weight’(vi, vi+1)
The weight of this cycle in the reweighted graph is
=
k
i=1
weight(vi, vi+1) +h(v1)−h(v1)
be a negative weight cycle
Reweighted negative cycles
Let v1, v2, . . . vk ∈ V
v1
v2
v3v4
v5
v6
vk
(where vk+1 = v1)
The weight of this cycle
k
i=1
weight(vi, vi+1) < 0
in the original graph is,
k
i=1
weight’(vi, vi+1)
The weight of this cycle in the reweighted graph is
=
k
i=1
weight(vi, vi+1)
be a negative weight cycle
Reweighted negative cycles
Let v1, v2, . . . vk ∈ V
v1
v2
v3v4
v5
v6
vk
(where vk+1 = v1)
The weight of this cycle
k
i=1
weight(vi, vi+1) < 0
in the original graph is,
k
i=1
weight’(vi, vi+1)
The weight of this cycle in the reweighted graph is
=
k
i=1
weight(vi, vi+1) < 0
be a negative weight cycle
Reweighted negative cycles
Let v1, v2, . . . vk ∈ V
v1
v2
v3v4
v5
v6
vk
(where vk+1 = v1)
The weight of this cycle
k
i=1
weight(vi, vi+1) < 0
in the original graph is,
k
i=1
weight’(vi, vi+1)
The weight of this cycle in the reweighted graph is
=
k
i=1
weight(vi, vi+1) < 0
be a negative weight cycle
So reweighting doesn’t affect negative cycles
Reweighting summary
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Reweighting summary
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma Any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
Reweighting summary
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma Any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
Fact If is the length of a path from u to v in the original graph
+ h(u) − h(v) is its length in the reweighted graph
Reweighting summary
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma Any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
Fact If is the length of a path from u to v in the original graph
+ h(u) − h(v) is its length in the reweighted graph
Fact A cycle has negative weight in the original graph
if and only if it has negative weight in the reweighted graph
Reweighting summary
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma Any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
Fact If is the length of a path from u to v in the original graph
+ h(u) − h(v) is its length in the reweighted graph
Fact A cycle has negative weight in the original graph
if and only if it has negative weight in the reweighted graph
So if we solve the all-pairs shortest paths problem on the reweighted graph. . .
we can recover the shortest path lengths for the original graph
Reweighting summary
Let the function h give a value h(v) for each vertex v ∈ V
weight’(u,v) = weight(u,v) + h(u) − h(v)
Change the weight of every edge (u, v) to be
Lemma Any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
Fact If is the length of a path from u to v in the original graph
+ h(u) − h(v) is its length in the reweighted graph
Fact A cycle has negative weight in the original graph
if and only if it has negative weight in the reweighted graph
So if we solve the all-pairs shortest paths problem on the reweighted graph. . .
we can recover the shortest path lengths for the original graph
To take advantage of this, we need to make all the edge weights non-negative
How do we choose h?
We first add one additional vertex called s to the original graph
How do we choose h?
We first add one additional vertex called s to the original graph
s
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
s
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
s
0
0
0
0
0
0
0
0
0
0
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
s
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
Warning: If the original graph contains a negative weight cycle,
δ(s, v) may be undefined
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
Warning: If the original graph contains a negative weight cycle,
δ(s, v) may be undefined
JOHNSON’s algorithm will detect this and abort
Let’s continue under the assumption that there is no negative weight cycle
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
This follows because there is a path from s to v via u
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
This follows because there is a path from s to v via u
with length δ(s, u) + weight(u, v)
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
This follows because there is a path from s to v via u
with length δ(s, u) + weight(u, v)
so the shortest path can’t be longer
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
Rearranging we have, weight(u, v) + δ(s, u) − δ(s, v) 0
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
weight’(u,v) = weight(u,v) + h(u) − h(v)
The new weight of an edge (u, v) then becomes
Rearranging we have, weight(u, v) + δ(s, u) − δ(s, v) 0
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
weight’(u,v) = weight(u,v) + h(u) − h(v)
The new weight of an edge (u, v) then becomes
Rearranging we have, weight(u, v) + δ(s, u) − δ(s, v) 0
= weight(u, v) + δ(s, u) − δ(s, v) 0
Consider any edge (u, v) ∈ E
How do we choose h?
We first add one additional vertex called s to the original graph
We also add an edge (s, v) from s to each other vertex v ∈ V
This does not introduce any new negative weight cycless
each of these edges has weight 00
0
0
0
0
0
0
0
0
0
For each v, let δ(s, v) denote the length of the shortest path from s to v
we then define h(v) to equal δ(s, v)
The key observation is that δ(s, v) δ(s, u) + weight(u, v)
weight’(u,v) = weight(u,v) + h(u) − h(v)
The new weight of an edge (u, v) then becomes
Rearranging we have, weight(u, v) + δ(s, u) − δ(s, v) 0
= weight(u, v) + δ(s, u) − δ(s, v) 0
So all the reweighted edge weights are non-negative
Consider any edge (u, v) ∈ E
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
- this calculates the shortest path lengths δ(s, v) for all v
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
- this calculates the shortest path lengths δ(s, v) for all v
if there is a negative weight cycle in the original graph, it will be detected here
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
- this calculates the shortest path lengths δ(s, v) for all v
Step 4: Reweight each edge (u, v) ∈ E so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
where for all v, h(v) = δ(s, v)
if there is a negative weight cycle in the original graph, it will be detected here
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
- this calculates the shortest path lengths δ(s, v) for all v
Step 4: Reweight each edge (u, v) ∈ E so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
where for all v, h(v) = δ(s, v)
Step 5: For each vertex u ∈ V , run DIJKSTRA’s algorithm with source s = u.
- this calculates the shortest path lengths δ (u, v) for all u, v
if there is a negative weight cycle in the original graph, it will be detected here
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
- this calculates the shortest path lengths δ(s, v) for all v
Step 4: Reweight each edge (u, v) ∈ E so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
where for all v, h(v) = δ(s, v)
Step 5: For each vertex u ∈ V , run DIJKSTRA’s algorithm with source s = u.
- this calculates the shortest path lengths δ (u, v) for all u, v
these are the shortest path lengths in the reweighted graph
if there is a negative weight cycle in the original graph, it will be detected here
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
- this calculates the shortest path lengths δ(s, v) for all v
Step 4: Reweight each edge (u, v) ∈ E so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
where for all v, h(v) = δ(s, v)
Step 5: For each vertex u ∈ V , run DIJKSTRA’s algorithm with source s = u.
- this calculates the shortest path lengths δ (u, v) for all u, v
these are the shortest path lengths in the reweighted graph
Step 6: For each pair of vertices u, v ∈ V , compute
δ(u, v) = δ (u, v) + h(v) − h(u)
if there is a negative weight cycle in the original graph, it will be detected here
JOHNSON’s algorithm
We can now piece together JOHNSON’s algorithm which operates as follows:
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
- this calculates the shortest path lengths δ(s, v) for all v
Step 4: Reweight each edge (u, v) ∈ E so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
where for all v, h(v) = δ(s, v)
Step 5: For each vertex u ∈ V , run DIJKSTRA’s algorithm with source s = u.
- this calculates the shortest path lengths δ (u, v) for all u, v
these are the shortest path lengths in the reweighted graph
Step 6: For each pair of vertices u, v ∈ V , compute
δ(u, v) = δ (u, v) + h(v) − h(u)
these are the shortest path lengths in the original graph
if there is a negative weight cycle in the original graph, it will be detected here
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take?
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take? O(1) time
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take? O(1) time
O(|V |) time
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take? O(1) time
O(|V |) time
O(|V ||E|) time
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take? O(1) time
O(|V |) time
O(|V ||E|) time
O(|E|) time
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take? O(1) time
O(|V |) time
O(|V ||E|) time
O(|E|) time
O(|E| log |V |) time per iteration
(using a binary heap)
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take? O(1) time
O(|V |) time
O(|V ||E|) time
O(|E|) time
O(|V ||E| log |V |) time overall
(using a binary heap)
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take? O(1) time
O(|V |) time
O(|V ||E|) time
O(|E|) time
O(|V ||E| log |V |) time overall
(using a binary heap)
O(|V |2) time
(The previous version of this slide said O(|E|) for Step 6 which was a typo)
Time Complexity
Step 1: Add one additional vertex called s to the original graph
Step 2: For each vertex, add an edge (s, v) with weight 0
Step 3: Run the BELLMAN-FORD algorithm with source s
Step 4: Reweight each edge (u, v) so that,
weight’(u,v) = weight(u,v) + h(u) − h(v)
Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u.
Step 6: For each pair of vertices u, v, compute
δ(u, v) = δ (u, v) + h(v) − h(u)
How long does all this take? O(1) time
O(|V |) time
O(|V ||E|) time
O(|E|) time
O(|V ||E| log |V |) time overall
(using a binary heap)
So the overall time complexity is O(|V ||E| log |V |)
O(|V |2) time
(The previous version of this slide said O(|E|) for Step 6 which was a typo)
This is the complexity for (strongly) connected graphs (actually any graph without isolated vertices). . .
In such graphs we have that |E| > |V |/2 so O(|V |2) = O(|V ||E|)
where every pair of
vertices is connected For unconnected graphs there is a (non-examinable and fiddly) fix. . .
see next slide
Johnson’s algorithm on graphs with |E| < |V |/2
This slide contains non-examinable material and was added post-lecture.
It covers the corner case that Johnson’s algorithm is run on a graph with |E| < |V |/2.
The simple answer: In this case, the algorithm (as discussed) takes O(|V |2 + |V ||E| log |V |) time
We can’t “hide” the O(|V |2) term because |V | could be much much larger than |E|
A more convoluted answer: With a simple preprocessing step, we can improve the time complexity to
O(|V |2 + |V ||E| log |V |) for this caseO(|V |2 + |V ||E| log |V |) for this case
Sketch Proof
We define a vertex to be isolated if it contains no incoming and no outgoing edges.
(in the diagram the are isolated and the are not)
Add a new step to the start of Johnson’s algorithm,
Step 0: Delete every isolated vertex O(|V | + |E|) time
In the new graph |E | = |E|, |V | |V | and |E | |V |/2
Therefore the modified algorithm takes
O(|V | + |E| + |V ||E | log |V |) = O(|V ||E| log |V |) time
Notice that the isolated vertices have distince ∞ to everywhere so deleting them doesn’t change anything
each remaining vertex is at
one end of at least one edge
JOHNSON’s algorithm summary
Lemma Any path is a shortest path in the original graph
if and only if it is a shortest path in the reweighted graph
Fact The length of a shortest path in the original graph
can be calculated from its length in the reweighted graph
Fact A cycle has negative weight in the original graph
if and only if it has negative weight in the reweighted graph
The overall approach taken by JOHNSON’s algorithm
is to reweight the edges in the graph
The key properties of the reweighted graph are:
Fact If there are no negative weight cycles in the original graph,
(using new weights picked using BELLMAN-FORD)
After reweighting, all-pairs shortest paths are calculated
using DIJSKTRAS algorithm, once with each vertex v as the source
Overall this takes O(|V ||E| log |V |) time
−ve
all of the edges in the reweighted graph have non-negative weights
+/0
Shortest paths algorithms: the summary
unweighted: Use Breadth First Search in O(|V | + |E|) time
non-negative edge weights: Use DIJKSTRA’s algorithm
which takes O(|V ||E| log |V |) time
positive and negative edge weights: Use BELLMAN-FORD which takes O(|V ||E|) time
To compute single source shortest paths in a directed graph which is/has:
which takes O((|V | + |E|) log |V |) time (when implemented using a binary heap)
unweighted: Use Breadth First Search once for each vertex in O(|V |2 + |V ||E|) time
non-negative edge weights: Use DIJKSTRA’s algorithm once for each vertex,
positive and negative edge weights: Use JOHNSON’S algorithm
To compute all-pairs shortest paths in a directed graph which is/has:
which takes O(|V ||E| log |V |) time (when implemented using a binary heap)
(when DIJKSTRA’s algorithm is implemented using a binary heap)

More Related Content

PDF
Shortest path algorithms
PDF
7). mechanical waves (finished)
PDF
Me307 machine elements formula sheet Erdi Karaçal Mechanical Engineer Univers...
PPT
Top schools in ghaziabad
DOC
Formul me-3074683 Erdi Karaçal Mechanical Engineer University of Gaziantep
PDF
Structural Mechanics: Deflections of Beams in Bending
PDF
Whole Procedure of Equations of motion.
PDF
Second Order Active RC Blocks
Shortest path algorithms
7). mechanical waves (finished)
Me307 machine elements formula sheet Erdi Karaçal Mechanical Engineer Univers...
Top schools in ghaziabad
Formul me-3074683 Erdi Karaçal Mechanical Engineer University of Gaziantep
Structural Mechanics: Deflections of Beams in Bending
Whole Procedure of Equations of motion.
Second Order Active RC Blocks

What's hot (14)

PPTX
Transmission Line
PDF
Is ellipse really a section of cone
PDF
Structur e very helpfull
PDF
PDF
Jawaban soal-latihan1mekanika
PPTX
Dynamics slideshare
PDF
Complex strains (2nd year)
PDF
Fundamentals of Physics "VECTORS"
PPTX
Algebra-taller2
PDF
Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)
PDF
Solutions manual for engineering mechanics dynamics 13th edition by hibbeler
PPT
5 cramer-rao lower bound
PPTX
Nonlinear Structural Dynamics: The Fundamentals Tutorial
Transmission Line
Is ellipse really a section of cone
Structur e very helpfull
Jawaban soal-latihan1mekanika
Dynamics slideshare
Complex strains (2nd year)
Fundamentals of Physics "VECTORS"
Algebra-taller2
Fundamentals of Physics (MOTION ALONG A STRAIGHT LINE)
Solutions manual for engineering mechanics dynamics 13th edition by hibbeler
5 cramer-rao lower bound
Nonlinear Structural Dynamics: The Fundamentals Tutorial
Ad

Viewers also liked (16)

PDF
Pattern Matching Part Two: k-mismatches
PDF
Minimum Spanning Trees (via Disjoint Sets)
PDF
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
PDF
Approximation Algorithms Part One: Constant factor approximations
PDF
Hashing Part Two: Cuckoo Hashing
PDF
Depth First Search and Breadth First Search
PDF
Range Minimum Queries
PDF
Approximation Algorithms Part Two: More Constant factor approximations
PDF
Pattern Matching Part Two: Suffix Arrays
PDF
Lowest Common Ancestor
PDF
Approximation Algorithms Part Three: (F)PTAS
PDF
Bloom Filters
PDF
Pattern Matching Part One: Suffix Trees
PDF
van Emde Boas trees
PDF
Pattern Matching Part Three: Hamming Distance
PDF
Probability Recap
Pattern Matching Part Two: k-mismatches
Minimum Spanning Trees (via Disjoint Sets)
Shortest Paths Part 1: Priority Queues and Dijkstra's Algorithm
Approximation Algorithms Part One: Constant factor approximations
Hashing Part Two: Cuckoo Hashing
Depth First Search and Breadth First Search
Range Minimum Queries
Approximation Algorithms Part Two: More Constant factor approximations
Pattern Matching Part Two: Suffix Arrays
Lowest Common Ancestor
Approximation Algorithms Part Three: (F)PTAS
Bloom Filters
Pattern Matching Part One: Suffix Trees
van Emde Boas trees
Pattern Matching Part Three: Hamming Distance
Probability Recap
Ad

Similar to Shortest Paths Part 2: Negative Weights and All-pairs (20)

PPTX
Shortest path algorithm
PDF
shortestpathalgorithm-180109112405 (1).pdf
PPTX
BELLMAN_FORD _ALGORITHM IN DATA STRUCTURES
PPTX
Bellmanford . montaser hamza.iraq
DOCX
PPTX
Bellman Ford Algorithm
PPTX
Algorithms Analysis: Bellman-Ford Algorithm
PDF
Daa chpater14
PDF
Bellman Ford algorithm and shortest source path algorithm
PPTX
Hasnain_Khalid_DAA_ppt.pptx
PPTX
Bellman Fords Algorithm Presentation.pptx
PPT
Chapter 25 aoa
PPTX
Bellman ford algorithm for single source
PDF
Shortest path, Bellman-Ford's algorithm, Dijkastra's algorithm, their Java co...
PPT
Design and Analysis of Algorithm -Shortest paths problem
PPTX
Single sourceshortestpath by emad
PDF
Shortest Path in Graph
PPTX
PDF
Bellman ford
PPTX
Bellman ford Algorithm
Shortest path algorithm
shortestpathalgorithm-180109112405 (1).pdf
BELLMAN_FORD _ALGORITHM IN DATA STRUCTURES
Bellmanford . montaser hamza.iraq
Bellman Ford Algorithm
Algorithms Analysis: Bellman-Ford Algorithm
Daa chpater14
Bellman Ford algorithm and shortest source path algorithm
Hasnain_Khalid_DAA_ppt.pptx
Bellman Fords Algorithm Presentation.pptx
Chapter 25 aoa
Bellman ford algorithm for single source
Shortest path, Bellman-Ford's algorithm, Dijkastra's algorithm, their Java co...
Design and Analysis of Algorithm -Shortest paths problem
Single sourceshortestpath by emad
Shortest Path in Graph
Bellman ford
Bellman ford Algorithm

More from Benjamin Sach (7)

PDF
Approximation Algorithms Part Four: APTAS
PDF
Orthogonal Range Searching
PDF
Hashing Part Two: Static Perfect Hashing
PDF
Hashing Part One
PDF
Dynamic Programming
PDF
Line Segment Intersections
PDF
Self-balancing Trees and Skip Lists
Approximation Algorithms Part Four: APTAS
Orthogonal Range Searching
Hashing Part Two: Static Perfect Hashing
Hashing Part One
Dynamic Programming
Line Segment Intersections
Self-balancing Trees and Skip Lists

Recently uploaded (20)

PPTX
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
PDF
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PDF
A systematic review of self-coping strategies used by university students to ...
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
PDF
Complications of Minimal Access Surgery at WLH
PDF
Abdominal Access Techniques with Prof. Dr. R K Mishra
PPTX
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
PDF
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
PPTX
Final Presentation General Medicine 03-08-2024.pptx
PPTX
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
PDF
VCE English Exam - Section C Student Revision Booklet
PPTX
Cell Types and Its function , kingdom of life
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PPTX
Microbial diseases, their pathogenesis and prophylaxis
Tissue processing ( HISTOPATHOLOGICAL TECHNIQUE
GENETICS IN BIOLOGY IN SECONDARY LEVEL FORM 3
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
A systematic review of self-coping strategies used by university students to ...
FourierSeries-QuestionsWithAnswers(Part-A).pdf
grade 11-chemistry_fetena_net_5883.pdf teacher guide for all student
Complications of Minimal Access Surgery at WLH
Abdominal Access Techniques with Prof. Dr. R K Mishra
1st Inaugural Professorial Lecture held on 19th February 2020 (Governance and...
OBE - B.A.(HON'S) IN INTERIOR ARCHITECTURE -Ar.MOHIUDDIN.pdf
Final Presentation General Medicine 03-08-2024.pptx
Introduction-to-Literarature-and-Literary-Studies-week-Prelim-coverage.pptx
VCE English Exam - Section C Student Revision Booklet
Cell Types and Its function , kingdom of life
human mycosis Human fungal infections are called human mycosis..pptx
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
O7-L3 Supply Chain Operations - ICLT Program
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
Microbial diseases, their pathogenesis and prophylaxis

Shortest Paths Part 2: Negative Weights and All-pairs

  • 1. Data Structures and Algorithms – COMS21103 Shortest Paths Revisited Negative Weights and All-Pairs Benjamin Sach inspired by slides by Ashley Montanaro
  • 2. In today’s lectures we’ll be revisiting the shortest paths problem In particular we’ll be interested in algorithms which allow negative edge weights in a weighted, directed graph. . . The shortest path from MVB to Temple Meads (according to Google Maps) and algorithms which compute the shortest path between all pairs of vertices
  • 3. Part one Single Source Shortest Paths with negative weights
  • 4. Single source shortest paths with negative weights in a weighted, directed graph. . . Bellman-Ford’s algorithm solves the single source shortest paths problem -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 5. Single source shortest paths with negative weights in a weighted, directed graph. . . Bellman-Ford’s algorithm solves the single source shortest paths problem It finds the shortest path from a given source vertex to every other vertex -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 6. Single source shortest paths with negative weights in a weighted, directed graph. . . Bellman-Ford’s algorithm solves the single source shortest paths problem It finds the shortest path from a given source vertex The weights are allowed to be to every other vertex positive or negative -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 7. Single source shortest paths with negative weights in a weighted, directed graph. . . Bellman-Ford’s algorithm solves the single source shortest paths problem It finds the shortest path from a given source vertex The weights are allowed to be to every other vertex The graph is stored as an Adjacency List positive or negative -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 8. Single source shortest paths with negative weights in a weighted, directed graph. . . Bellman-Ford’s algorithm solves the single source shortest paths problem It finds the shortest path from a given source vertex The weights are allowed to be to every other vertex We will not need any non-elementary data structures The graph is stored as an Adjacency List positive or negative -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 9. Single source shortest paths with negative weights in a weighted, directed graph. . . Bellman-Ford’s algorithm solves the single source shortest paths problem It finds the shortest path from a given source vertex The weights are allowed to be to every other vertex We will not need any non-elementary data structures Previously we saw that Dijkstra’s algorithm (implemented with a binary heap) solves this problem in O((|V | + |E|) log |V |) time when the edges have non-negative weights |V | is the number of vertices and |E| is the number of edges The graph is stored as an Adjacency List positive or negative -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 10. Negative weight cycles If some of the edges in the graph have negative weights the idea of a shortest path might not make sense: If there is a path from s to t which includes a negative weight cycle, A X Z Y B What is the shortest path from A to B? (-1) 1 1 1 A negative weight cycle is a path from a vertex v back to v such that the sum of the edge weights is negative there is no shortest path from s to t A X Z Y B -1 -1 1 1 1
  • 11. Negative weight cycles If some of the edges in the graph have negative weights the idea of a shortest path might not make sense: If there is a path from s to t which includes a negative weight cycle, A X Z Y B What is the shortest path from A to B? (-1) 1 1 1 A negative weight cycle is a path from a vertex v back to v such that the sum of the edge weights is negative there is no shortest path from s to t We will first discuss a (slightly) simpler version of Bellman-Ford that assumes there are no such cycles A X Z Y B -1 -1 1 1 1
  • 12. Most of the Bellman-Ford algorithm MOSTOFBELLMAN-FORD(s) weight(u, v) is the weight of the edge from u to v (u, v) ∈ E iff there is an edge from u to v dist(v) is the length of the shortest path between s and v, found so far For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E If dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v)
  • 13. Most of the Bellman-Ford algorithm MOSTOFBELLMAN-FORD(s) weight(u, v) is the weight of the edge from u to v (u, v) ∈ E iff there is an edge from u to v dist(v) is the length of the shortest path between s and v, found so far For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E If dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) The algorithm repeatedly asks, for each edge (u, v) “can I find a shorter route to v if I go via u?”
  • 14. Most of the Bellman-Ford algorithm MOSTOFBELLMAN-FORD(s) weight(u, v) is the weight of the edge from u to v (u, v) ∈ E iff there is an edge from u to v dist(v) is the length of the shortest path between s and v, found so far For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E If dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) s u v The algorithm repeatedly asks, for each edge (u, v) “can I find a shorter route to v if I go via u?”
  • 15. Most of the Bellman-Ford algorithm MOSTOFBELLMAN-FORD(s) weight(u, v) is the weight of the edge from u to v (u, v) ∈ E iff there is an edge from u to v dist(v) is the length of the shortest path between s and v, found so far For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E If dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) This is called RELAXING edge (u, v) s u v The algorithm repeatedly asks, for each edge (u, v) “can I find a shorter route to v if I go via u?”
  • 16. Most of the Bellman-Ford algorithm MOSTOFBELLMAN-FORD(s) for each vertex v, dist(v) is the length of the shortest path between s and v Claim When the MOSTOFBELLMAN-FORD algorithm terminates, weight(u, v) is the weight of the edge from u to v (u, v) ∈ E iff there is an edge from u to v dist(v) is the length of the shortest path between s and v, found so far For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E If dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) (assuming there are no-negative weight cycles) This is called RELAXING edge (u, v) s u v The algorithm repeatedly asks, for each edge (u, v) “can I find a shorter route to v if I go via u?”
  • 17. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far
  • 18. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) We start by setting dist(s) = 0 and every other dist(v) = ∞. . . - the length of the best path between s and v, found so far
  • 19. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far
  • 20. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far We now start iteration 1. . .
  • 21. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far We now start iteration 1. . .
  • 22. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far ∞ > 0 + (−1) We now start iteration 1. . .
  • 23. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far ∞ > 0 + (−1) -1 We now start iteration 1. . .
  • 24. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 We now start iteration 1. . .
  • 25. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 ∞ > 0 + 1 We now start iteration 1. . .
  • 26. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 ∞ > 0 + 1 1 We now start iteration 1. . .
  • 27. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 We now start iteration 1. . .
  • 28. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 −1 < 1 + 1 We now start iteration 1. . .
  • 29. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 −1 < 1 + 1 (so we don’t change dist) We now start iteration 1. . .
  • 30. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 We now start iteration 1. . .
  • 31. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 ∞ > 1 + 3 We now start iteration 1. . .
  • 32. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 ∞ > 1 + 3 4 We now start iteration 1. . .
  • 33. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 We now start iteration 1. . .
  • 34. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 We now start iteration 1. . .
  • 35. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 −1 < ∞ + 2 We now start iteration 1. . .
  • 36. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 −1 < ∞ + 2 (so we don’t change dist) We now start iteration 1. . .
  • 37. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 We now start iteration 1. . .
  • 38. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 ∞ < ∞ + 2 We now start iteration 1. . .
  • 39. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 ∞ < ∞ + 2 (dist is still ∞) We now start iteration 1. . .
  • 40. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 We now start iteration 1. . .
  • 41. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 ∞ > −1 + (−2) We now start iteration 1. . .
  • 42. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 ∞ > −1 + (−2) -3 We now start iteration 1. . .
  • 43. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 -3 We now start iteration 1. . .
  • 44. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 -3 We now start iteration 1. . .
  • 45. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 -3 We now start iteration 1. . .
  • 46. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 -3 We now start iteration 1. . .
  • 47. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 -3 We now start iteration 1. . .
  • 48. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 -3 We now start iteration 1. . .
  • 49. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 1 4 -3 1 > −3 + 1 We now start iteration 1. . .
  • 50. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 1 > −3 + 1 -2 We now start iteration 1. . .
  • 51. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 We now start iteration 1. . .
  • 52. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 We now start iteration 1. . .
  • 53. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 We now start iteration 1. . .
  • 54. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 We now start iteration 1. . .
  • 55. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 We now start iteration 1. . .
  • 56. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 We now start iteration 1. . .
  • 57. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 We now start iteration 1. . .
  • 58. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 We now start iteration 1. . .
  • 59. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 We now start iteration 1. . .
  • 60. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 We now start iteration 1. . .
  • 61. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 We now start iteration 1. . .
  • 62. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 This is the end of iteration 1
  • 63. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2
  • 64. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking?
  • 65. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? this looks good
  • 66. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? so does this
  • 67. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? this doesn’t look so good
  • 68. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? neither does this
  • 69. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? neither does this (it’s not the shortest path length)
  • 70. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? neither does this (it’s not the shortest path length)
  • 71. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? neither does this (it’s not the shortest path length) This path has length −1
  • 72. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking?
  • 73. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? we aren’t done
  • 74. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? but it seems like we made progresswe aren’t done
  • 75. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? but it seems like we made progresswe aren’t done does every iteration make progress?
  • 76. RELAX(u,v) if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) In each iteration we RELAX every edge (u, v) MOSTOFBELLMAN-FORD runs |V | iterations, dist(v) 37 vertex v s (in the order they occur in the adjacency list) 0 ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 We’re going to simulate MOSTOFBELLMAN-FORD(s) - the length of the best path between s and v, found so far -1 4 -3 -2 -2 2 How are things looking? but it seems like we made progresswe aren’t done does every iteration make progress? are |V | iterations enough?
  • 77. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges)
  • 78. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t
  • 79. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t
  • 80. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t Iteration 1:
  • 81. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t Iteration 1: “relax this”
  • 82. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t Iteration 1: “relax this” -1
  • 83. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t Iteration 1: -1
  • 84. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 Iteration 2:
  • 85. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 Iteration 2:
  • 86. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 Iteration 2: “relax this”
  • 87. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 Iteration 2: “relax this” -3
  • 88. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 Iteration 2: -3
  • 89. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 Iteration 3:
  • 90. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 Iteration 3:
  • 91. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 “relax this” Iteration 3:
  • 92. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 “relax this” -2 Iteration 3:
  • 93. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 Iteration 3:
  • 94. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 Iteration 4:
  • 95. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 Iteration 4:
  • 96. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 “relax this” Iteration 4:
  • 97. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 “relax this” -1 Iteration 4:
  • 98. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 Iteration 4:
  • 99. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 Iteration 5:
  • 100. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 Iteration 5:
  • 101. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 “relax this” Iteration 5:
  • 102. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 “relax this” 0 Iteration 5:
  • 103. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 0 Iteration 5:
  • 104. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 0 Iteration 6:
  • 105. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 0 Iteration 6:
  • 106. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 0 “relax this” Iteration 6:
  • 107. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 0 “relax this” -1 Iteration 6:
  • 108. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t t This is a shortest path from s to t -1 -3 -2 -1 0 -1 Iteration 6:
  • 109. The proof idea Imagine a different algorithm where in each iteration. . . s 0 ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ ∞ 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you only relax one edge (rather than all edges) Further, imagine that (magically or otherwise), the edge that you relax in iteration i is the i-th edge in a shortest path from s to some vertex t When this algorithm terminates. . . dist(t) is the length of the shortest path from s to t t This is a shortest path from s to t -1 -3 -2 -1 0 -1 Iteration 6:
  • 110. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge)
  • 111. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) t Consider the same shortest path from s to t
  • 112. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 113. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 114. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 115. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 116. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 117. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 118. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 119. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: we relax this Consider the same you relax the i-th edge in the shortest path from s to t. . . at some point shortest path from s to t
  • 120. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: we relax this Consider the same you relax the i-th edge in the shortest path from s to t. . . at some point Don’t worry about what ? was before. RELAX picks the smaller of ? and 0 + (−1) so. . . shortest path from s to t
  • 121. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: we relax this -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . at some point Don’t worry about what ? was before. RELAX picks the smaller of ? and 0 + (−1) so. . . shortest path from s to t
  • 122. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 123. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 124. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 125. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 126. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 127. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 128. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 129. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 130. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 131. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 132. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 133. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 134. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 135. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 136. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 137. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 138. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t Iteration 1: -1 Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 139. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 140. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 141. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 142. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 143. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 144. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 145. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: relax this Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t
  • 146. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: relax this Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 147. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 148. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 149. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 150. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 151. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 152. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 153. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 154. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 155. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 156. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 157. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 158. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 159. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 160. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 161. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 162. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 163. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 164. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 165. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 166. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 2: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 167. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 168. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 169. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 170. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 171. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 172. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 173. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 relax this Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3
  • 174. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 relax this Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 175. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 176. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 177. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 178. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 179. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 180. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 181. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 182. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 183. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 3: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 184. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 185. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 186. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 187. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 188. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 189. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 190. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 191. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 192. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 193. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 relax this Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2
  • 194. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 relax this Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 195. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 196. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 197. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 198. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 199. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 200. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 201. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 4: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 202. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 203. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 204. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 205. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 206. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 207. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 208. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 209. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 210. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 211. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 212. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 213. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 214. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 215. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 216. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 relax this Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1
  • 217. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 relax this Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 218. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 219. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 220. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 221. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 222. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 223. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 5: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 224. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 225. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 226. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 227. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 228. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 229. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 230. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 231. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 232. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 233. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 234. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 235. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 236. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 237. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 relax this Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0
  • 238. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 relax this Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0 -1
  • 239. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) At some point in iteration i t -1 Iteration 6: Consider the same you relax the i-th edge in the shortest path from s to t. . . shortest path from s to t -3 -2 -1 0 -1
  • 240. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) t -1 Iteration 6: Consider the same shortest path from s to t -3 -2 -1 0 -1
  • 241. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) So after enough iterations. . . dist(t) is the length of a path from s to t t -1 Iteration 6: Consider the same shortest path from s to t -3 -2 -1 0 -1 which is at least as short as the shortest path. . .
  • 242. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) So after enough iterations. . . dist(t) is the length of a path from s to t t -1 Iteration 6: Consider the same shortest path from s to t -3 -2 -1 0 -1 which is at least as short as the shortest path. . . In other words dist(t) is the length of a shortest path from s to t
  • 243. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) So after enough iterations. . . dist(t) is the length of a path from s to t t -1 Iteration 6: Consider the same shortest path from s to t -3 -2 -1 0 -1 which is at least as short as the shortest path. . . In other words dist(t) is the length of a shortest path from s to t how many iterations are needed?
  • 244. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) t -1 Iteration 6: Consider the same shortest path from s to t -3 -2 -1 0 -1 For this argument to hold we need to do one iteration for every edge in the shortest path from s to t
  • 245. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) t -1 Iteration 6: Consider the same shortest path from s to t -3 -2 -1 0 -1 For this argument to hold we need to do one iteration We will now prove that if there are no negative cycles in the graph, the shortest path between s and t contains at most |V | edges for every edge in the shortest path from s to t
  • 246. The proof idea Now consider the MOSTOFBELLMAN-FORD algorithm where in each iteration. . . s 0 ? ? ? ? ? ? ? ? ? ? ? 3 1 1 1 4 -2 2 2 -2 -1 1 1 -1 1 3 -1 -1 2 1 -2 1 you relax every edge (rather than one edge) t -1 Iteration 6: Consider the same shortest path from s to t -3 -2 -1 0 -1 For this argument to hold we need to do one iteration We will now prove that if there are no negative cycles in the graph, the shortest path between s and t contains at most |V | edges for every edge in the shortest path from s to t and therefore |V | iterations will be enough
  • 247. Claim if there are no negative weight cycles in the graph, there is a shortest path between s and t containing at most |V | edges (or there is no path from s to t)
  • 248. Claim if there are no negative weight cycles in the graph, there is a shortest path between s and t containing at most |V | edges Consider a path between s and t with a cycle. . . (or there is no path from s to t)
  • 249. Claim if there are no negative weight cycles in the graph, there is a shortest path between s and t containing at most |V | edges Consider a path between s and t with a cycle. . . s t 3 1 2 -1 -1 3 -1 (or there is no path from s to t)
  • 250. Claim if there are no negative weight cycles in the graph, there is a shortest path between s and t containing at most |V | edges Consider a path between s and t with a cycle. . . s t 3 1 2 -1 -1 3 -1 As there are no negative weight cycles deleting this cycle from the path cannot increase it’s length (or there is no path from s to t)
  • 251. Claim if there are no negative weight cycles in the graph, there is a shortest path between s and t containing at most |V | edges Consider a path between s and t with a cycle. . . s t 3 1 2 -1 -1 3 -1 As there are no negative weight cycles deleting this cycle from the path cannot increase it’s length path length 6 (or there is no path from s to t)
  • 252. Claim if there are no negative weight cycles in the graph, there is a shortest path between s and t containing at most |V | edges Consider a path between s and t with a cycle. . . s t 3 1 2 -1 -1 3 -1 As there are no negative weight cycles deleting this cycle from the path cannot increase it’s length path length 6 path length 5 (or there is no path from s to t)
  • 253. Claim if there are no negative weight cycles in the graph, there is a shortest path between s and t containing at most |V | edges Consider a path between s and t with a cycle. . . s t 3 1 2 -1 -1 3 -1 As there are no negative weight cycles deleting this cycle from the path cannot increase it’s length path length 6 path length 5 Therefore, there is a shortest path between s and t containing no cycles (or there is no path from s to t)
  • 254. Claim if there are no negative weight cycles in the graph, there is a shortest path between s and t containing at most |V | edges Consider a path between s and t with a cycle. . . s t 3 1 2 -1 -1 3 -1 As there are no negative weight cycles deleting this cycle from the path cannot increase it’s length path length 6 path length 5 Therefore, there is a shortest path between s and t containing no cycles A path with no cycles enters each vertex at most once so contains at most |V | edges (or there is no path from s to t)
  • 255. MOSTOFBELLMAN-FORD Summary for each vertex v, dist(v) is the distance between s and v Claim When the MOSTOFBELLMAN-FORD algorithm terminates, (assuming there are no-negative weight cycles)
  • 256. MOSTOFBELLMAN-FORD Summary for each vertex v, dist(v) is the distance between s and v Claim When the MOSTOFBELLMAN-FORD algorithm terminates, (assuming there are no-negative weight cycles) If there is no path between s and v then at termination, dist(v) = ∞ by the algorithm description, MOSTOFBELLMAN-FORD doesn’t find paths that aren’t there
  • 257. MOSTOFBELLMAN-FORD Summary for each vertex v, dist(v) is the distance between s and v Claim When the MOSTOFBELLMAN-FORD algorithm terminates, (assuming there are no-negative weight cycles) If there is no path between s and v then at termination, dist(v) = ∞ by the algorithm description, MOSTOFBELLMAN-FORD doesn’t find paths that aren’t there If there is a shortest path between s and v then (assuming there are no-negative weight cycles) it contains at most |V | edges
  • 258. MOSTOFBELLMAN-FORD Summary for each vertex v, dist(v) is the distance between s and v Claim When the MOSTOFBELLMAN-FORD algorithm terminates, (assuming there are no-negative weight cycles) If there is no path between s and v then at termination, dist(v) = ∞ by the algorithm description, MOSTOFBELLMAN-FORD doesn’t find paths that aren’t there If there is a shortest path between s and v then (assuming there are no-negative weight cycles) it contains at most |V | edges After each iteration, the algorithm has taken (at least) one more ‘step’ along this path
  • 259. MOSTOFBELLMAN-FORD Summary for each vertex v, dist(v) is the distance between s and v Claim When the MOSTOFBELLMAN-FORD algorithm terminates, (assuming there are no-negative weight cycles) If there is no path between s and v then at termination, dist(v) = ∞ by the algorithm description, MOSTOFBELLMAN-FORD doesn’t find paths that aren’t there If there is a shortest path between s and v then (assuming there are no-negative weight cycles) it contains at most |V | edges After each iteration, the algorithm has taken (at least) one more ‘step’ along this path After |V | iterations of the algorithm dist(v) is the length of the shortest path between s and v
  • 260. So what is the rest of the algorithm? BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’
  • 261. So what is the rest of the algorithm? BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ We’ve added a final check which determines whether another iteration would find an even shorter path from s to some v
  • 262. So what is the rest of the algorithm? BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ We’ve added a final check which determines whether another iteration would find an even shorter path from s to some v We will prove that this is happens if and only if there is a negative weight cycle
  • 263. BELLMAN-FORD without negative weight cycles BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ We first argue that the algorithm still works when there is no negative weight cycle
  • 264. BELLMAN-FORD without negative weight cycles BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ After the first |V | iterations for each vertex v, We first argue that the algorithm still works when there is no negative weight cycle (which haven’t changed), dist(v) is the length of the shortest path between s and v
  • 265. BELLMAN-FORD without negative weight cycles BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ After the first |V | iterations for each vertex v, ‘Negative weight cycle found’If the final check outputs: then there is a path from s to some v (via u) We first argue that the algorithm still works when there is no negative weight cycle (which haven’t changed), dist(v) is the length of the shortest path between s and v with length dist(u) + weight(u, v) < dist(v)
  • 266. BELLMAN-FORD without negative weight cycles BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ After the first |V | iterations for each vertex v, ‘Negative weight cycle found’If the final check outputs: then there is a path from s to some v (via u) We first argue that the algorithm still works when there is no negative weight cycle (which haven’t changed), dist(v) is the length of the shortest path between s and v with length dist(u) + weight(u, v) < dist(v) i.e. a path which is shorter than the shortest path
  • 267. BELLMAN-FORD without negative weight cycles BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ After the first |V | iterations for each vertex v, ‘Negative weight cycle found’If the final check outputs: then there is a path from s to some v (via u) this is a contradiction. We first argue that the algorithm still works when there is no negative weight cycle (which haven’t changed), dist(v) is the length of the shortest path between s and v with length dist(u) + weight(u, v) < dist(v) i.e. a path which is shorter than the shortest path
  • 268. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk
  • 269. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) We have that,
  • 270. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) We have that,
  • 271. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ weight(v1, v2) dist(v2) − dist(v1) We have that, because we ran the following checks but found no negative weight cycle:
  • 272. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ weight(v1, v2) dist(v2) − dist(v1) So, We have that, dist(v2) dist(v1) + weight(v1, v2) (then rearrange) because we ran the following checks but found no negative weight cycle:
  • 273. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) We have that,
  • 274. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) We have that, and by the same argument. . .
  • 275. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) weight(v2, v3) dist(v3) − dist(v2) We have that, and by the same argument. . .
  • 276. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) weight(v2, v3) dist(v3) − dist(v2) weight(v3, v4) dist(v4) − dist(v3) We have that, and by the same argument. . .
  • 277. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) weight(v2, v3) dist(v3) − dist(v2) weight(v3, v4) dist(v4) − dist(v3) We have that, and by the same argument. . . weight(v4, v5) dist(v5) − dist(v4)
  • 278. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) weight(v2, v3) dist(v3) − dist(v2) weight(v3, v4) dist(v4) − dist(v3) We have that, and by the same argument. . . weight(v4, v5) dist(v5) − dist(v4)
  • 279. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) weight(v2, v3) dist(v3) − dist(v2) weight(v3, v4) dist(v4) − dist(v3) We have that, and by the same argument. . . weight(v4, v5) dist(v5) − dist(v4)
  • 280. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) weight(v2, v3) dist(v3) − dist(v2) weight(v3, v4) dist(v4) − dist(v3) We have that, and by the same argument. . . weight(v4, v5) dist(v5) − dist(v4)
  • 281. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(v1, v2) dist(v2) − dist(v1) weight(v2, v3) dist(v3) − dist(v2) weight(v3, v4) dist(v4) − dist(v3) We have that, and by the same argument. . . weight(v4, v5) dist(v5) − dist(v4)
  • 282. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(vj, vj+1) dist(vj+1) − dist(vj) In general for every edge (vj, vj+1), we have (where vk+1 = v1)
  • 283. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(vj, vj+1) dist(vj+1) − dist(vj) In general for every edge (vj, vj+1), we have (where vk+1 = v1) But the cycle has negative weight so, k j=1 weight(vj, vj+1) < 0
  • 284. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(vj, vj+1) dist(vj+1) − dist(vj) In general for every edge (vj, vj+1), we have (where vk+1 = v1) However, if we sum all the edge weights using the top expressions we get, But the cycle has negative weight so, k j=1 weight(vj, vj+1) < 0 k j=1 weight(vj, vj+1)
  • 285. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(vj, vj+1) dist(vj+1) − dist(vj) In general for every edge (vj, vj+1), we have (where vk+1 = v1) However, if we sum all the edge weights using the top expressions we get, But the cycle has negative weight so, k j=1 weight(vj, vj+1) < 0 k j=1 weight(vj, vj+1) k j=1 dist(vj+1) − dist(vj)
  • 286. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(vj, vj+1) dist(vj+1) − dist(vj) In general for every edge (vj, vj+1), we have (where vk+1 = v1) However, if we sum all the edge weights using the top expressions we get, But the cycle has negative weight so, k j=1 weight(vj, vj+1) < 0 k j=1 dist(vj+1) − k j=1 dist(vj) k j=1 weight(vj, vj+1)
  • 287. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(vj, vj+1) dist(vj+1) − dist(vj) In general for every edge (vj, vj+1), we have (where vk+1 = v1) However, if we sum all the edge weights using the top expressions we get, But the cycle has negative weight so, k j=1 weight(vj, vj+1) < 0 k j=1 dist(vj+1) − k j=1 dist(vj) k j=1 weight(vj, vj+1) = 0
  • 288. BELLMAN-FORD with a negative weight cycle Let v1, v2, . . . vk ∈ V be a negative weight cycle and assume for a contradiction, that it wasn’t reported by the algorithm v1 v2 v3 v4 v5 v6 vk weight(vj, vj+1) dist(vj+1) − dist(vj) In general for every edge (vj, vj+1), we have (where vk+1 = v1) However, if we sum all the edge weights using the top expressions we get, But the cycle has negative weight so, k j=1 weight(vj, vj+1) < 0 k j=1 dist(vj+1) − k j=1 dist(vj) k j=1 weight(vj, vj+1) = 0 Contradiction!
  • 289. BELLMAN-FORD Summary for each vertex v, dist(v) is the distance between s and v When the BELLMAN-FORD algorithm terminates, (or it reports that there is a negative weight cycle) BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’
  • 290. BELLMAN-FORD Summary for each vertex v, dist(v) is the distance between s and v When the BELLMAN-FORD algorithm terminates, (or it reports that there is a negative weight cycle) BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ What is the time complexity of the BELLMAN-FORD algorithm?
  • 291. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’
  • 292. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ O(|V |) time
  • 293. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ O(|V |) time (we store dist in an array of length |V |)
  • 294. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ O(|V |) time (we store dist in an array of length |V |) O(1) time
  • 295. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ O(|V |) time (we store dist in an array of length |V |) O(1) time O(|E|) time for each iteration
  • 296. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ O(|V |) time (we store dist in an array of length |V |) O(1) time O(|E|) time for each iteration . . . and there are |V | iterations
  • 297. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ O(|V |) time (we store dist in an array of length |V |) O(1) time O(|V ||E|) time overall
  • 298. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ O(|V |) time (we store dist in an array of length |V |) O(|E|) time O(1) time O(|V ||E|) time overall
  • 299. Time Complexity BELLMAN-FORD(s) For all v, set dist(v) = ∞ set dist(s) = 0 For i = 1, 2, . . . , |V |, For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) dist(v) = dist(u) + weight(u, v) For every edge (u, v) ∈ E if dist(v) > dist(u) + weight(u, v) Output ‘Negative weight cycle found’ O(|V ||E|) time O(|V |) time (we store dist in an array of length |V |) O(|E|) time O(1) time so overall the BELLMAN-FORD algorithm takes O(|V ||E|) time overall
  • 300. Summary If there is a negative weight cycle, the algorithm reports this and aborts The BELLMAN-FORD algorithm runs in O(|V ||E|) time This is not as good as DIJKSTRA’s algorithm which runs in O((|V | + |E|) log |V |) time The BELLMAN-FORD algorithm solves the single source shortest paths problem and uses no non-elementary data structures in a directed, weighted graph with positive and negative edge weights (in this case, the problem is not well-defined) (when implemented using a binary heap) However, DIJKSTRA’s algorithm only works for graphs with non-negative edge weights
  • 301. End of part one
  • 303. All-Pairs Shortest Paths in a weighted, directed graph. . . In previous lectures, we have focused on the single source shortest paths problem -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 304. All-Pairs Shortest Paths in a weighted, directed graph. . . In previous lectures, we have focused on the single source shortest paths problem i.e. in finding the shortest paths from a single given source vertex to every other vertex -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 305. All-Pairs Shortest Paths in a weighted, directed graph. . . In previous lectures, we have focused on the single source shortest paths problem i.e. in finding the shortest paths from a single given source vertex to every other vertex What should we do if we want to find the shortest paths from every vertex to every other vertex? -21 2 1 1 2 4 3 1 2 -1 A B E D C F G
  • 306. Use something we saw before What should we do if we want to find the shortest paths from every vertex to every other vertex?
  • 307. Use something we saw before We already have two options: What should we do if we want to find the shortest paths from every vertex to every other vertex?
  • 308. Use something we saw before We already have two options: If the graph has non-negative edge weights, we could run DIJKSTRA’s algorithm |V | times, once with each vertex as the source this takes O(|V ||E| log |V |) time (if the priority queue is a binary heap) What should we do if we want to find the shortest paths from every vertex to every other vertex?
  • 309. Use something we saw before We already have two options: If the graph has non-negative edge weights, we could run DIJKSTRA’s algorithm |V | times, once with each vertex as the source this takes O(|V ||E| log |V |) time (if the priority queue is a binary heap) If the graph has positive and negative edge weights, we could run BELLMAN-FORD’s algorithm |V | times, once for each vertex this takes O(|V |2|E|) time What should we do if we want to find the shortest paths from every vertex to every other vertex?
  • 310. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights,
  • 311. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good?
  • 312. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better?
  • 313. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|?
  • 314. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? The output contains the length of the shortest path between every pair of vertices
  • 315. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? The output contains the length of the shortest path between every pair of vertices There are |V | · (|V | − 1) pairs of vertices
  • 316. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? The output contains the length of the shortest path between every pair of vertices so we can’t expect to do better than O(|V |2) time There are |V | · (|V | − 1) pairs of vertices
  • 317. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|?
  • 318. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? Imagine that |E| ≈ |V |2 4 (the graph is very dense) e.g. each vertex has an edge to about half of the other vertices
  • 319. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? Imagine that |E| ≈ |V |2 4 (the graph is very dense) e.g. each vertex has an edge to about half of the other vertices this becomes O(|V |3 log |V |)
  • 320. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? Imagine that |E| ≈ |V |2 4 (the graph is very dense) e.g. each vertex has an edge to about half of the other vertices this becomes O(|V |4) this becomes O(|V |3 log |V |)
  • 321. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|?
  • 322. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|?
  • 323. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? imagine that |E| ≈ 5|V | (the graph is very sparse) e.g. each vertex has an edge to about 5 other vertices Instead,
  • 324. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? imagine that |E| ≈ 5|V | (the graph is very sparse) e.g. each vertex has an edge to about 5 other vertices Instead, this becomes O(|V |2 log |V |)
  • 325. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? imagine that |E| ≈ 5|V | (the graph is very sparse) e.g. each vertex has an edge to about 5 other vertices Instead, this becomes O(|V |2 log |V |) this becomes O(|V |3)
  • 326. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, Are these any good? Can we do better? How does |V | compare to |E|? imagine that |E| ≈ 5|V | (the graph is very sparse) e.g. each vertex has an edge to about 5 other vertices Instead, this becomes O(|V |2 log |V |) this becomes O(|V |3) O(|V |2 log |V |) is a lot better than O(|V |3)
  • 327. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights,
  • 328. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, In this lecture, we will discuss JOHNSON’s algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time in graphs with positive and negative edge weights
  • 329. What should we do if we want to find the shortest paths from every vertex to every other vertex? If the graph has non-negative edge weights, repeatedly running DIJKSTRA’s algorithm takes O(|V ||E| log |V |) time repeatedly running BELLMAN-FORD’s algorithm takes O(|V |2|E|) time If the graph has positive and negative edge weights, In this lecture, we will discuss JOHNSON’s algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time in graphs with positive and negative edge weights It will use both DIJKSTRA’s algorithm (implemented with a binary heap) and BELLMAN-FORD to achieve this
  • 330. JOHNSON’s algorithm - the overall approach We have already seen one algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time
  • 331. JOHNSON’s algorithm - the overall approach We have already seen one algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time If the graph had non-negative edge weights, we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm
  • 332. JOHNSON’s algorithm - the overall approach We have already seen one algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time If the graph had non-negative edge weights, we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm However, we are interested in graphs with both positive and negative edge weights,
  • 333. JOHNSON’s algorithm - the overall approach We have already seen one algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time If the graph had non-negative edge weights, we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm However, we are interested in graphs with both positive and negative edge weights, A B C D 1 4 -2 -1 0 1
  • 334. JOHNSON’s algorithm - the overall approach We have already seen one algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time If the graph had non-negative edge weights, we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm The approach employed by JOHNSON’s algorithm is to reweight the edges However, we are interested in graphs with both positive and negative edge weights, so that the resulting graph has non-negative edge weights, and then (repeatedly) run DIJKSTRA’s algorithm A B C D 1 4 -2 -1 0 1
  • 335. JOHNSON’s algorithm - the overall approach We have already seen one algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time If the graph had non-negative edge weights, we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm The approach employed by JOHNSON’s algorithm is to reweight the edges However, we are interested in graphs with both positive and negative edge weights, so that the resulting graph has non-negative edge weights, and then (repeatedly) run DIJKSTRA’s algorithm A B C D 1 4 becomes-2 -1 0 1 A B C D 5 1 0 0 0 0
  • 336. JOHNSON’s algorithm - the overall approach We have already seen one algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time If the graph had non-negative edge weights, we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm The approach employed by JOHNSON’s algorithm is to reweight the edges However, we are interested in graphs with both positive and negative edge weights, so that the resulting graph has non-negative edge weights, and then (repeatedly) run DIJKSTRA’s algorithm A B C D 1 4 becomes-2 -1 0 1 A B C D 5 1 0 0 0 0
  • 337. JOHNSON’s algorithm - the overall approach We have already seen one algorithm for all-pairs shortest paths which takes O(|V ||E| log |V |) time If the graph had non-negative edge weights, we could have achieved this complexity by repeatedly running DIJKSTRA’s algorithm The approach employed by JOHNSON’s algorithm is to reweight the edges However, we are interested in graphs with both positive and negative edge weights, so that the resulting graph has non-negative edge weights, and then (repeatedly) run DIJKSTRA’s algorithm A B C D 1 4 becomes-2 -1 0 1 A B C D 5 1 0 0 0 0 the shortest path length may change but the route remains the same
  • 338. A first attempt One natural attempt at reweighting is to increase every edge weight by the same amount. . .
  • 339. A first attempt One natural attempt at reweighting is to increase every edge weight A B C D 1 4 -2 -1 0 1 by the same amount. . .
  • 340. A first attempt One natural attempt at reweighting is to increase every edge weight A B C D 1 4 becomes-2 -1 0 1 A B C D 14 10 11 8 9 11 if we add 10 to every edge. . . by the same amount. . .
  • 341. A first attempt One natural attempt at reweighting is to increase every edge weight A B C D 1 4 becomes-2 -1 0 1 A B C D 14 10 11 8 9 11 if we add 10 to every edge. . . by the same amount. . . The shortest path from C to B goes this way in the original graph
  • 342. A first attempt One natural attempt at reweighting is to increase every edge weight A B C D 1 4 becomes-2 -1 0 1 A B C D 14 10 11 8 9 11 if we add 10 to every edge. . . by the same amount. . . The shortest path from C to B goes this way in the original graph The shortest path from C to B goes this way in the reweighted graph
  • 343. A first attempt One natural attempt at reweighting is to increase every edge weight A B C D 1 4 becomes-2 -1 0 1 A B C D 14 10 11 8 9 11 if we add 10 to every edge. . . by the same amount. . . Unfortunately, if we reweight the graph like this, both the shortest paths lengths and the routes might change The shortest path from C to B goes this way in the original graph The shortest path from C to B goes this way in the reweighted graph
  • 344. A first attempt One natural attempt at reweighting is to increase every edge weight A B C D 1 4 becomes-2 -1 0 1 A B C D 14 10 11 8 9 11 if we add 10 to every edge. . . by the same amount. . . Unfortunately, if we reweight the graph like this, both the shortest paths lengths and the routes might change this doesn’t look good The shortest path from C to B goes this way in the original graph The shortest path from C to B goes this way in the reweighted graph
  • 345. Reweighting based on vertex potential We are going to associate a value h(v) with each vertex v ∈ V - we will call this the potential of v To overcome this, we are going to reweight each edge differently the new weight of each edge will depend on which vertices it connects
  • 346. Reweighting based on vertex potential We are going to associate a value h(v) with each vertex v ∈ V - we will call this the potential of v To overcome this, we are going to reweight each edge differently the new weight of each edge will depend on which vertices it connects A Bweight(A,B)
  • 347. Reweighting based on vertex potential We are going to associate a value h(v) with each vertex v ∈ V - we will call this the potential of v To overcome this, we are going to reweight each edge differently the new weight of each edge will depend on which vertices it connects A B h(A) h(B) weight(A,B)
  • 348. Reweighting based on vertex potential We are going to associate a value h(v) with each vertex v ∈ V - we will call this the potential of v To overcome this, we are going to reweight each edge differently the new weight of each edge will depend on which vertices it connects A B h(A) h(B) weight(A,B) becomes weight’(A,B) where weight’(A,B) = weight(A,B) + h(A) − h(B) A B
  • 349. Reweighting based on vertex potential We are going to associate a value h(v) with each vertex v ∈ V - we will call this the potential of v To overcome this, we are going to reweight each edge differently the new weight of each edge will depend on which vertices it connects A B h(A) h(B) weight(A,B) becomes weight’(A,B) where weight’(A,B) = weight(A,B) + h(A) − h(B) A B Why is this better than the first attempt?
  • 350. Reweighted paths Each each vertex v ∈ V has a value h(v) - called the potential of v we will pick these values carefully later
  • 351. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v we will pick these values carefully later
  • 352. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6-2 3 1 7 4
  • 353. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6-2 3 1 7 4 Pick some potential values
  • 354. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 -2 3 1 7 4 Pick some potential values
  • 355. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 -2 3 1 7 4 (for now you can think of the values as arbitrary) Pick some potential values
  • 356. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 -2 3 1 7 4
  • 357. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2
  • 358. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2
  • 359. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2 weight’(v1, v2) = weight(v1, v2) + h(v1) − h(v2)
  • 360. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2 weight’(v1, v2) = weight(v1, v2) + h(v1) − h(v2) =(−2) + (5) − (2) = 1
  • 361. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2
  • 362. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2 - this path has length 13
  • 363. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2 - this path has length 13 - this path has length 14
  • 364. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2 - this path has length 13 - this path has length 14 = 13 + 5 − 4 = 13 + h(v1) − h(v6)
  • 365. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2 - this path has length 13 - this path has length 14 = 13 + 5 − 4 = 13 + h(v1) − h(v6)
  • 366. Reweighted paths Consider the following path as an example. . . Each each vertex v ∈ V has a value h(v) - called the potential of v v1 we will pick these values carefully later v2 v3 v4 v5 v6 h(v1) = 5 h(v2) = 2 h(v3) = 0 h(v4) = 1 h(v5) = 2 h(v6) = 4 where weight’(vi, vi+1) = weight(vi, vi+1) + h(vi) − h(vi+1) -2 3 1 7 4 Reweight the whole graph using weight’ (we don’t just reweight this path). . . v1 v2 v3 v4 v5 v61 5 0 6 2 - this path has length 13 - this path has length 14 = 13 + 5 − 4 = 13 + h(v1) − h(v6) is this a coincidence?
  • 367. Reweighted paths Consider an arbitrary path. . . v1 v2 v3 v4 v5 vk h(v1) h(v2) h(v3) h(v4) h(v5) h(vk) ? ? ? ?
  • 368. Reweighted paths Consider an arbitrary path. . . v1 v2 v3 v4 v5 vk h(v1) h(v2) h(v3) h(v4) h(v5) h(vk) ? ? ? ? In the original graph, the path length is k−1 i=1 weight(vi, vi+1)
  • 369. Reweighted paths Consider an arbitrary path. . . v1 v2 v3 v4 v5 vk h(v1) h(v2) h(v3) h(v4) h(v5) h(vk) ? ? ? ? In the original graph, the path length is k−1 i=1 weight(vi, vi+1) In the reweighted graph, the path length is k−1 i=1 weight’(vi, vi+1)
  • 370. Reweighted paths Consider an arbitrary path. . . v1 v2 v3 v4 v5 vk h(v1) h(v2) h(v3) h(v4) h(v5) h(vk) ? ? ? ? In the original graph, the path length is k−1 i=1 weight(vi, vi+1) In the reweighted graph, the path length is k−1 i=1 weight’(vi, vi+1) = k−1 i=1 weight(vi, vi+1) + h(vi) − h(vi+1)
  • 371. Reweighted paths Consider an arbitrary path. . . v1 v2 v3 v4 v5 vk h(v1) h(v2) h(v3) h(v4) h(v5) h(vk) ? ? ? ? In the original graph, the path length is k−1 i=1 weight(vi, vi+1) In the reweighted graph, the path length is k−1 i=1 weight’(vi, vi+1) = k−1 i=1 weight(vi, vi+1) + h(vi) − h(vi+1) = k−1 i=1 weight(vi, vi+1) +h(v1)−h(vk)
  • 372. Reweighted paths Consider an arbitrary path. . . v1 v2 v3 v4 v5 vk h(v1) h(v2) h(v3) h(v4) h(v5) h(vk) ? ? ? ? In the original graph, the path length is k−1 i=1 weight(vi, vi+1) In the reweighted graph, the path length is k−1 i=1 weight’(vi, vi+1) = k−1 i=1 weight(vi, vi+1) + h(vi) − h(vi+1) = k−1 i=1 weight(vi, vi+1) +h(v1)−h(vk) So the weight of a path only changes by the potential values of the end points. . .
  • 373. Reweighted shortest paths Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be
  • 374. Reweighted shortest paths Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph
  • 375. Reweighted shortest paths Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph u vu v path p1 path p2
  • 376. Reweighted shortest paths Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph u vu v path p1 path p2 Let l1 be the length of p1 in the original graph
  • 377. Reweighted shortest paths Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph u vu v path p1 path p2 Let l1 be the length of p1 in the original graph Let l2 be the length of p2 in the original graph
  • 378. Reweighted shortest paths Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph u vu v path p1 path p2 Let l1 be the length of p1 in the original graph Let l2 be the length of p2 in the original graph l1 + h(u) − h(v) in the reweighted graph is the length of p1
  • 379. Reweighted shortest paths Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph u vu v path p1 path p2 Let l1 be the length of p1 in the original graph Let l2 be the length of p2 in the original graph l1 + h(u) − h(v) in the reweighted graph l2 + h(u) − h(v) in the reweighted graph is the length of p1 is the length of p2
  • 380. Reweighted shortest paths Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph u vu v path p1 path p2 Let l1 be the length of p1 in the original graph Let l2 be the length of p2 in the original graph l1 + h(u) − h(v) in the reweighted graph l2 + h(u) − h(v) in the reweighted graph is the length of p1 is the length of p2 l1 l2 if and only if l1 + h(u) − h(v) l2 + h(u) − h(v)
  • 381. Reweighted negative cycles Let v1, v2, . . . vk ∈ V v1 v2 v3v4 v5 v6 vk be a negative weight cycle
  • 382. Reweighted negative cycles Let v1, v2, . . . vk ∈ V v1 v2 v3v4 v5 v6 vk (where vk+1 = v1) The weight of this cycle k i=1 weight(vi, vi+1) < 0 in the original graph is, be a negative weight cycle
  • 383. Reweighted negative cycles Let v1, v2, . . . vk ∈ V v1 v2 v3v4 v5 v6 vk (where vk+1 = v1) The weight of this cycle k i=1 weight(vi, vi+1) < 0 in the original graph is, The weight of this cycle in the reweighted graph is be a negative weight cycle
  • 384. Reweighted negative cycles Let v1, v2, . . . vk ∈ V v1 v2 v3v4 v5 v6 vk (where vk+1 = v1) The weight of this cycle k i=1 weight(vi, vi+1) < 0 in the original graph is, k i=1 weight’(vi, vi+1) The weight of this cycle in the reweighted graph is be a negative weight cycle
  • 385. Reweighted negative cycles Let v1, v2, . . . vk ∈ V v1 v2 v3v4 v5 v6 vk (where vk+1 = v1) The weight of this cycle k i=1 weight(vi, vi+1) < 0 in the original graph is, k i=1 weight’(vi, vi+1) The weight of this cycle in the reweighted graph is = k i=1 weight(vi, vi+1) +h(v1)−h(v1) be a negative weight cycle
  • 386. Reweighted negative cycles Let v1, v2, . . . vk ∈ V v1 v2 v3v4 v5 v6 vk (where vk+1 = v1) The weight of this cycle k i=1 weight(vi, vi+1) < 0 in the original graph is, k i=1 weight’(vi, vi+1) The weight of this cycle in the reweighted graph is = k i=1 weight(vi, vi+1) be a negative weight cycle
  • 387. Reweighted negative cycles Let v1, v2, . . . vk ∈ V v1 v2 v3v4 v5 v6 vk (where vk+1 = v1) The weight of this cycle k i=1 weight(vi, vi+1) < 0 in the original graph is, k i=1 weight’(vi, vi+1) The weight of this cycle in the reweighted graph is = k i=1 weight(vi, vi+1) < 0 be a negative weight cycle
  • 388. Reweighted negative cycles Let v1, v2, . . . vk ∈ V v1 v2 v3v4 v5 v6 vk (where vk+1 = v1) The weight of this cycle k i=1 weight(vi, vi+1) < 0 in the original graph is, k i=1 weight’(vi, vi+1) The weight of this cycle in the reweighted graph is = k i=1 weight(vi, vi+1) < 0 be a negative weight cycle So reweighting doesn’t affect negative cycles
  • 389. Reweighting summary Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be
  • 390. Reweighting summary Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma Any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph
  • 391. Reweighting summary Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma Any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph Fact If is the length of a path from u to v in the original graph + h(u) − h(v) is its length in the reweighted graph
  • 392. Reweighting summary Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma Any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph Fact If is the length of a path from u to v in the original graph + h(u) − h(v) is its length in the reweighted graph Fact A cycle has negative weight in the original graph if and only if it has negative weight in the reweighted graph
  • 393. Reweighting summary Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma Any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph Fact If is the length of a path from u to v in the original graph + h(u) − h(v) is its length in the reweighted graph Fact A cycle has negative weight in the original graph if and only if it has negative weight in the reweighted graph So if we solve the all-pairs shortest paths problem on the reweighted graph. . . we can recover the shortest path lengths for the original graph
  • 394. Reweighting summary Let the function h give a value h(v) for each vertex v ∈ V weight’(u,v) = weight(u,v) + h(u) − h(v) Change the weight of every edge (u, v) to be Lemma Any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph Fact If is the length of a path from u to v in the original graph + h(u) − h(v) is its length in the reweighted graph Fact A cycle has negative weight in the original graph if and only if it has negative weight in the reweighted graph So if we solve the all-pairs shortest paths problem on the reweighted graph. . . we can recover the shortest path lengths for the original graph To take advantage of this, we need to make all the edge weights non-negative
  • 395. How do we choose h? We first add one additional vertex called s to the original graph
  • 396. How do we choose h? We first add one additional vertex called s to the original graph s
  • 397. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V s
  • 398. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V s 0 0 0 0 0 0 0 0 0 0
  • 399. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V s each of these edges has weight 00 0 0 0 0 0 0 0 0 0
  • 400. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0
  • 401. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v
  • 402. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v Warning: If the original graph contains a negative weight cycle, δ(s, v) may be undefined
  • 403. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v Warning: If the original graph contains a negative weight cycle, δ(s, v) may be undefined JOHNSON’s algorithm will detect this and abort Let’s continue under the assumption that there is no negative weight cycle
  • 404. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v
  • 405. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v)
  • 406. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) Consider any edge (u, v) ∈ E
  • 407. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) Consider any edge (u, v) ∈ E
  • 408. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) This follows because there is a path from s to v via u Consider any edge (u, v) ∈ E
  • 409. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) This follows because there is a path from s to v via u with length δ(s, u) + weight(u, v) Consider any edge (u, v) ∈ E
  • 410. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) This follows because there is a path from s to v via u with length δ(s, u) + weight(u, v) so the shortest path can’t be longer Consider any edge (u, v) ∈ E
  • 411. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) Consider any edge (u, v) ∈ E
  • 412. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) Rearranging we have, weight(u, v) + δ(s, u) − δ(s, v) 0 Consider any edge (u, v) ∈ E
  • 413. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) weight’(u,v) = weight(u,v) + h(u) − h(v) The new weight of an edge (u, v) then becomes Rearranging we have, weight(u, v) + δ(s, u) − δ(s, v) 0 Consider any edge (u, v) ∈ E
  • 414. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) weight’(u,v) = weight(u,v) + h(u) − h(v) The new weight of an edge (u, v) then becomes Rearranging we have, weight(u, v) + δ(s, u) − δ(s, v) 0 = weight(u, v) + δ(s, u) − δ(s, v) 0 Consider any edge (u, v) ∈ E
  • 415. How do we choose h? We first add one additional vertex called s to the original graph We also add an edge (s, v) from s to each other vertex v ∈ V This does not introduce any new negative weight cycless each of these edges has weight 00 0 0 0 0 0 0 0 0 0 For each v, let δ(s, v) denote the length of the shortest path from s to v we then define h(v) to equal δ(s, v) The key observation is that δ(s, v) δ(s, u) + weight(u, v) weight’(u,v) = weight(u,v) + h(u) − h(v) The new weight of an edge (u, v) then becomes Rearranging we have, weight(u, v) + δ(s, u) − δ(s, v) 0 = weight(u, v) + δ(s, u) − δ(s, v) 0 So all the reweighted edge weights are non-negative Consider any edge (u, v) ∈ E
  • 416. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows:
  • 417. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph
  • 418. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0
  • 419. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s - this calculates the shortest path lengths δ(s, v) for all v
  • 420. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s - this calculates the shortest path lengths δ(s, v) for all v if there is a negative weight cycle in the original graph, it will be detected here
  • 421. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s - this calculates the shortest path lengths δ(s, v) for all v Step 4: Reweight each edge (u, v) ∈ E so that, weight’(u,v) = weight(u,v) + h(u) − h(v) where for all v, h(v) = δ(s, v) if there is a negative weight cycle in the original graph, it will be detected here
  • 422. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s - this calculates the shortest path lengths δ(s, v) for all v Step 4: Reweight each edge (u, v) ∈ E so that, weight’(u,v) = weight(u,v) + h(u) − h(v) where for all v, h(v) = δ(s, v) Step 5: For each vertex u ∈ V , run DIJKSTRA’s algorithm with source s = u. - this calculates the shortest path lengths δ (u, v) for all u, v if there is a negative weight cycle in the original graph, it will be detected here
  • 423. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s - this calculates the shortest path lengths δ(s, v) for all v Step 4: Reweight each edge (u, v) ∈ E so that, weight’(u,v) = weight(u,v) + h(u) − h(v) where for all v, h(v) = δ(s, v) Step 5: For each vertex u ∈ V , run DIJKSTRA’s algorithm with source s = u. - this calculates the shortest path lengths δ (u, v) for all u, v these are the shortest path lengths in the reweighted graph if there is a negative weight cycle in the original graph, it will be detected here
  • 424. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s - this calculates the shortest path lengths δ(s, v) for all v Step 4: Reweight each edge (u, v) ∈ E so that, weight’(u,v) = weight(u,v) + h(u) − h(v) where for all v, h(v) = δ(s, v) Step 5: For each vertex u ∈ V , run DIJKSTRA’s algorithm with source s = u. - this calculates the shortest path lengths δ (u, v) for all u, v these are the shortest path lengths in the reweighted graph Step 6: For each pair of vertices u, v ∈ V , compute δ(u, v) = δ (u, v) + h(v) − h(u) if there is a negative weight cycle in the original graph, it will be detected here
  • 425. JOHNSON’s algorithm We can now piece together JOHNSON’s algorithm which operates as follows: Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s - this calculates the shortest path lengths δ(s, v) for all v Step 4: Reweight each edge (u, v) ∈ E so that, weight’(u,v) = weight(u,v) + h(u) − h(v) where for all v, h(v) = δ(s, v) Step 5: For each vertex u ∈ V , run DIJKSTRA’s algorithm with source s = u. - this calculates the shortest path lengths δ (u, v) for all u, v these are the shortest path lengths in the reweighted graph Step 6: For each pair of vertices u, v ∈ V , compute δ(u, v) = δ (u, v) + h(v) − h(u) these are the shortest path lengths in the original graph if there is a negative weight cycle in the original graph, it will be detected here
  • 426. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take?
  • 427. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take? O(1) time
  • 428. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take? O(1) time O(|V |) time
  • 429. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take? O(1) time O(|V |) time O(|V ||E|) time
  • 430. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take? O(1) time O(|V |) time O(|V ||E|) time O(|E|) time
  • 431. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take? O(1) time O(|V |) time O(|V ||E|) time O(|E|) time O(|E| log |V |) time per iteration (using a binary heap)
  • 432. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take? O(1) time O(|V |) time O(|V ||E|) time O(|E|) time O(|V ||E| log |V |) time overall (using a binary heap)
  • 433. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take? O(1) time O(|V |) time O(|V ||E|) time O(|E|) time O(|V ||E| log |V |) time overall (using a binary heap) O(|V |2) time (The previous version of this slide said O(|E|) for Step 6 which was a typo)
  • 434. Time Complexity Step 1: Add one additional vertex called s to the original graph Step 2: For each vertex, add an edge (s, v) with weight 0 Step 3: Run the BELLMAN-FORD algorithm with source s Step 4: Reweight each edge (u, v) so that, weight’(u,v) = weight(u,v) + h(u) − h(v) Step 5: For each vertex u, run DIJKSTRA’s algorithm with source s = u. Step 6: For each pair of vertices u, v, compute δ(u, v) = δ (u, v) + h(v) − h(u) How long does all this take? O(1) time O(|V |) time O(|V ||E|) time O(|E|) time O(|V ||E| log |V |) time overall (using a binary heap) So the overall time complexity is O(|V ||E| log |V |) O(|V |2) time (The previous version of this slide said O(|E|) for Step 6 which was a typo) This is the complexity for (strongly) connected graphs (actually any graph without isolated vertices). . . In such graphs we have that |E| > |V |/2 so O(|V |2) = O(|V ||E|) where every pair of vertices is connected For unconnected graphs there is a (non-examinable and fiddly) fix. . . see next slide
  • 435. Johnson’s algorithm on graphs with |E| < |V |/2 This slide contains non-examinable material and was added post-lecture. It covers the corner case that Johnson’s algorithm is run on a graph with |E| < |V |/2. The simple answer: In this case, the algorithm (as discussed) takes O(|V |2 + |V ||E| log |V |) time We can’t “hide” the O(|V |2) term because |V | could be much much larger than |E| A more convoluted answer: With a simple preprocessing step, we can improve the time complexity to O(|V |2 + |V ||E| log |V |) for this caseO(|V |2 + |V ||E| log |V |) for this case Sketch Proof We define a vertex to be isolated if it contains no incoming and no outgoing edges. (in the diagram the are isolated and the are not) Add a new step to the start of Johnson’s algorithm, Step 0: Delete every isolated vertex O(|V | + |E|) time In the new graph |E | = |E|, |V | |V | and |E | |V |/2 Therefore the modified algorithm takes O(|V | + |E| + |V ||E | log |V |) = O(|V ||E| log |V |) time Notice that the isolated vertices have distince ∞ to everywhere so deleting them doesn’t change anything each remaining vertex is at one end of at least one edge
  • 436. JOHNSON’s algorithm summary Lemma Any path is a shortest path in the original graph if and only if it is a shortest path in the reweighted graph Fact The length of a shortest path in the original graph can be calculated from its length in the reweighted graph Fact A cycle has negative weight in the original graph if and only if it has negative weight in the reweighted graph The overall approach taken by JOHNSON’s algorithm is to reweight the edges in the graph The key properties of the reweighted graph are: Fact If there are no negative weight cycles in the original graph, (using new weights picked using BELLMAN-FORD) After reweighting, all-pairs shortest paths are calculated using DIJSKTRAS algorithm, once with each vertex v as the source Overall this takes O(|V ||E| log |V |) time −ve all of the edges in the reweighted graph have non-negative weights +/0
  • 437. Shortest paths algorithms: the summary unweighted: Use Breadth First Search in O(|V | + |E|) time non-negative edge weights: Use DIJKSTRA’s algorithm which takes O(|V ||E| log |V |) time positive and negative edge weights: Use BELLMAN-FORD which takes O(|V ||E|) time To compute single source shortest paths in a directed graph which is/has: which takes O((|V | + |E|) log |V |) time (when implemented using a binary heap) unweighted: Use Breadth First Search once for each vertex in O(|V |2 + |V ||E|) time non-negative edge weights: Use DIJKSTRA’s algorithm once for each vertex, positive and negative edge weights: Use JOHNSON’S algorithm To compute all-pairs shortest paths in a directed graph which is/has: which takes O(|V ||E| log |V |) time (when implemented using a binary heap) (when DIJKSTRA’s algorithm is implemented using a binary heap)