17. Bellman-Ford Algorithm (2)
17. Bellman-Ford Algorithm (2)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
Relaxation:
3. for each edge (u,v) G.E
Make |V|-1 passes,
4. RELAX(u, v, w) relaxing each edge
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v) Test whether negative
-weight cycle exists
7. return FALSE
8. return TRUE
Edge weights may be negative
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s) O(|V|)
2. for i=1 to |G.V|-1 O(|V||E|)
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E O(|E|)
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
Running time O(|V||E|)
Lemma 24.2
Let G=(V,E) be a weighted, directed graph with source s and
weight function w:E→R.
Assume that G contains no negative-weight cycles that are
reachable from s.
Then, after the |V|-1 iterations of the for loop of lines 2 - 4 of
BELLMAN-FORD, we have v.d=δ(s,v) for all vertices that are
reachable from s.
Lemma 24.2
(Proof)
We prove the lemma by appealing to the path-relaxation
property.
Consider any vertex that is reachable from s, and let
p=<v0,v1,…,vk>, where v0= s and vk = v, be any shortest path
from s to v.
Because shortest paths are simple, p has at most |V|-1 edges,
and so k≤|V|-1. Each of the |V|-1 iterations of the for loop of
lines 2–4 relaxes all |E| edges.
The edge (vi-1, vi) is one among the edges relaxed in the i-th
iteration, for i = 1, 2, …, k.
By the path-relaxation property, therefore, v.d=vk.d=δ(s,vk)=
δ(s,v).
Corollary 24.3
Let G=(V,E) be a weighted, directed graph with source vertex s
and weight function w:E→R.
Assume that G contains no negative-weight cycles that are
reachable from s.
Then, for each vertex v ∈ V, there is a path from s to v if and
only if BELLMAN-FORD terminates with v.d < ∞ when it is run
on G.
The proof is left as Exercise 24.1-2.
Correctness of the Bellman-
Ford Algorithm
Let BELLMAN-FORD be run on a weighted, directed graph
G=(V,E) with source s and weight function w:E→R.
If G contains no negative-weight cycles that are reachable from
s, then the algorithm returns TRUE, we have v.d=δ(s,v) for all
vertices v ∈ V, and the predecessor subgraph G is a shortest-
paths tree rooted at s.
It G does contain a negative-weight cycle reachable from s, then
the algorithm returns FALSE
Correctness of the Bellman-
Ford Algorithm (Proof)
Suppose that G contains no negative-weight cycles that are reachable from s.
We first prove the claim that at termination, v.d=δ(s,v) for all vertices v ∈ V.
If vertex v is reachable from s, then Lemma 24.2 proves this.
If v is not reachable from s, then the claim follows from the no-path
property.
Thus, the claim is proven.
The predecessor-subgraph property, along with the claim, implies that G.𝜋 is a
shortest-paths tree.
Now we use the claim to show that BELLMAN-FORD returns TRUE.
At termination, we have for all edges (u,v) ∈ E,
v.d = δ(s,v)
≤ δ(s,u) + w(u,v) (by the triangle inequality)
= u.d+w(u,v)
and so none of the tests in line 6 causes BELLMAN-FORD to return FALSE.
Thus, it returns TRUE.
Correctness of the Bellman-Ford
Algorithm (Proof)
Suppose G contains a negative-weight cycle that is reachable from s.
Let this cycle c=<v0,v1,…,vk>, where v0=vk and σ𝒌𝒊=𝟏 𝒘 𝒗𝒊−𝟏 , 𝒗𝒊 < 𝟎 (24.1)
𝑘 𝑘
𝑣𝑖 . 𝑑 ≤ 𝑣𝑖−1 . 𝑑 + 𝑤 𝑣𝑖−1 , 𝑣𝑖
𝑖=1 𝑖=1
Since v0=vk, each vertex in c appears exactly once in each of the summations, σ𝑘𝑖=1 𝑣𝑖 . 𝑑 =
σ𝑘𝑖=1 𝑣𝑖−1 . 𝑑.
6 -2
-3
s 8
7
-4
7 2
9
y z
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6 -2
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6
-2
6
-3
s 0 8
7
-4
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=1
8. return TRUE
t 5 x
6
-2
6
-3
s 0 8
7
-4
7 2
7
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 11
-2
6
-3
s 0 8
7
-4
7 2
7
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 11
-2
6
-3
s 0 8
7
-4
7 2
7
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 11
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 11
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=2
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
6 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=3
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE i=4
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)
Bellman-Ford Algorithm
BELLMAN-FORD(G, w, s)
1. INITIALIZE-SINGLE-SOURCE(G,s)
2. for i=1 to |G.V|-1
3. for each edge (u,v) G.E
4. RELAX(u, v, w)
5. for each edge (u,v) G.E
6. if v.d > u.d + w(u,v)
7. return FALSE
8. return TRUE
t 5 x
2 4
-2
6
-3
s 0 8
7
-4
7 2
7 -2
9
y z
G.E (t,x) (t,y) (t,z) (x,t) (y,x) (y,z) (z,x) (z,s) (s,t) (s,y)