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