2. Introduction
• It solves the Single-Source Shortest-Paths problem in the general case in which
edges can have Negative Weight.
• Given a Weighted, Directed Graph G = (V, E) with source s and weight function
w : E → R, the Bellman-Ford algorithm returns a Boolean value indicating whether
or not there is a Negative-Weight Cycle that is reachable from the source.
V – Set of Vertices
E – Set of Edges
R – Set of Real Numbers
w - weight function mapping edges to real-valued weights
• If there is a Negative Cycle, the algorithm indicates that No Solution exists.
• If there is No Negative Cycle, the algorithm produces the Shortest Paths from s
and their Weights.
3. • v.d : Shortest Path Estimate; Estimate of the shortest path from the source s to a
vertex v.
• v.π : Predecessor of v in the path from s to v.
• δ (s, v) : Shortest path from the source s to vertex v.
• Relax (u, v) : Process of testing whether the shortest path to v (from s) found so far
can be improved by going through u and if so, updating v.d and v.π accordingly.
Working Principle:
• The algorithm Relaxes Edges, progressively decreasing v.d of each vertex v є V
until it achieves the actual Shortest-Path Weight δ (s, v).
Algorithm
4. Algorithm…
BELLMAN-FORD (G, w, s)
INITIALIZE-SINGLE-SOURCE (G, s)
For (i = 1 to | G.V | - 1)
For (each Edge (u, v) є G.E)
RELAX (u, v, w)
For (Each Edge (u, v) є G.E)
If (v.d > (u.d + w (u, v))
Return FALSE
Return TRUE
INITIALIZE-SINGLE-SOURCE (G, s)
For (Each Vertex v є G.V)
v.d = ꝏ
v.π = NIL
s.d = 0
RELAX (u, v, w)
If (v.d > (u.d + w (u, v)))
v.d = u.d + w (u, v)
v.π = u
5. Algorithm…
Note:
• Relax (u, v) is equivalent to finding whether the shortest path to v found so far is
minimum or the path through u is minimum. i.e,
v.d = min (v.d, u.d + w (u, v))
• The edges can be relaxed in any order.
Running Time:
INITIALIZE-SINGLE-SOURCE : Ө (V). (v.d and v.π are initialized for |V| vertices)
RELAX : O (1) (updates v.d and v.π for 1 vertex)
BELLMAN-FORD : O (VE) (All the |E| edges are relaxed |V| - 1 times)
6. Example
The order in which the Edges are Relaxed in each pass:
(t, x); (t, y); (t, z); (x, t); (y, x); (y, z); (z, x); (z, s); (s, t); (s, y)
Relax (t, x): x.d = min (x.d, t.d + w (t, x)) = (ꝏ, ꝏ + 5) = ꝏ
Relax (t, y): y.d = min (y.d, t.d + w (t, y)) = (ꝏ, ꝏ + 8) = ꝏ
Relax (t, z): z.d = min (z.d, t.d + w (t, z)) = (ꝏ, ꝏ + (-4)) = ꝏ
Relax (x, t): t.d = min (t.d, x.d + w (x, t)) = (ꝏ, ꝏ + (-2)) = ꝏ
Relax (y, x): x.d = min (x.d, y.d + w (y, x)) = (ꝏ, ꝏ + (-3)) = ꝏ
Relax (y, z): z.d = min (z.d, y.d + w (y, z)) = (ꝏ, ꝏ + 9) = ꝏ
Relax (z, x): x.d = min (x.d, z.d + w (z, x)) = (ꝏ, ꝏ + 7) = ꝏ
Relax (z, s): s.d = min (s.d, z.d + w (z, s)) = (0, ꝏ + 2) = 0
Relax (s, t): t.d = min (t.d, s.d + w (s, t)) = (ꝏ, 0 + 6) = 6; t.π = s
Relax (s, y): y.d = min (y.d, s.d + w (s, y)) = (ꝏ, 0 + 7) = 7; y.π = s
7. Example…
Relax (t, x): x.d = min (x.d, t.d + w (t, x)) = (ꝏ, 6 + 5) = 11, x.π = t
Relax (t, y): y.d = min (y.d, t.d + w (t, y)) = (7, 6 + 8) = 7
Relax (t, z): z.d = min (z.d, t.d + w (t, z)) = (ꝏ, 6 + (-4)) = 2, z.π = t
Relax (x, t): t.d = min (t.d, x.d + w (x, t)) = (6, 11 + (-2)) = 6
Relax (y, x): x.d = min (x.d, y.d + w (y, x)) = (11, 7 + (-3)) = 4; x.π = y
Relax (y, z): z.d = min (z.d, y.d + w (y, z)) = (2, 7 + 9) = 2
Relax (z, x): x.d = min (x.d, z.d + w (z, x)) = (4, 2 + 7) = 4
Relax (z, s): s.d = min (s.d, z.d + w (z, s)) = (0, 2 + 2) = 0
Relax (s, t): t.d = min (t.d, s.d + w (s, t)) = (6, 0 + 6) = 6
Relax (s, y): y.d = min (y.d, s.d + w (s, y)) = (7, 0 + 7) = 7
8. Example…
Relax (t, x): x.d = min (x.d, t.d + w (t, x)) = (4, 6 + 5) = 4
Relax (t, y): y.d = min (y.d, t.d + w (t, y)) = (7, 6 + 8) = 7
Relax (t, z): z.d = min (z.d, t.d + w (t, z)) = (2, 6 + (-4)) = 2; z.π = t
Relax (x, t): t.d = min (t.d, x.d + w (x, t)) = (6, 4 + (-2)) = 2; t.π = x
Relax (y, x): x.d = min (x.d, y.d + w (y, x)) = (4, 7 + (-3)) = 4; x.π = y
Relax (y, z): z.d = min (z.d, y.d + w (y, z)) = (2, 7 + 9) = 2
Relax (z, x): x.d = min (x.d, z.d + w (z, x)) = (4, 2 + 7) = 4
Relax (z, s): s.d = min (s.d, z.d + w (z, s)) = (0, 2 + 2) = 0
Relax (s, t): t.d = min (t.d, s.d + w (s, t)) = (6, 0 + 6) = 6
Relax (s, y): y.d = min (y.d, s.d + w (s, y)) = (7, 0 + 7) = 7
9. Example…
Relax (t, x): x.d = min (x.d, t.d + w (t, x)) = (4, 6 + 5) = 4
Relax (t, y): y.d = min (y.d, t.d + w (t, y)) = (7, 6 + 8) = 7
Relax (t, z): z.d = min (z.d, t.d + w (t, z)) = (2, 2 + (-4)) = -2, z.π = t
Relax (x, t): t.d = min (t.d, x.d + w (x, t)) = (2, 4 + (-2)) = 2
Relax (y, x): x.d = min (x.d, y.d + w (y, x)) = (4, 7 + (-3)) = 4
Relax (y, z): z.d = min (z.d, y.d + w (y, z)) = (2, 7 + 9) = 2
Relax (z, x): x.d = min (x.d, z.d + w (z, x)) = (4, 2 + 7) = 4
Relax (z, s): s.d = min (s.d, z.d + w (z, s)) = (0, 2 + 2) = 0
Relax (s, t): t.d = min (t.d, s.d + w (s, t)) = (6, 0 + 6) = 6
Relax (s, y): y.d = min (y.d, s.d + w (s, y)) = (7, 0 + 7) = 7
10. Example…
The order in which the Edges are Relaxed in each pass:
(t, x); (t, y); (t, z); (x, t); (y, x); (y, z); (z, x); (z, s); (s, t); (s, y)
Pass 2Pass 1
Pass 4Pass 3
11. Example…
The Shortest-Paths from source s to all other vertices is:
Destination Path Cost
s - 0
t s → y → x → t
(t.π = x, x.π = y, y.π = s)
2
(7 + (-3) + (-2)
y s → y
(y.π = s)
7
x s → y → x
(x.π = y, y.π = s)
4
(7 + (-3))
z s → y → x → t → z
(z.π = t, t.π = x, x.π = y, y.π = s)
-2
(7 + (-3) + (-2) + (-4)
12. Lemma
Let G = (V, E) be a weighted, directed graph with source s and weight function
w : E → R, and assume that G contains no negative-weight cycles that are reachable
from s. Then After the |V| - 1 iterations v.d = δ (s, v) for all vertices v that are
reachable from s.
Proof:
Let p = <v0, v1, . . , vk>: Shortest Path from s to a vertex v, v0 = s and vk = v. (L1)
(L1) → Number of Vertices in p, |V| = k + 1 (L2)
w.k.t Max. Number of Edges in p, |E| = |V| - 1 = k (Shortest paths are simple) (L3)
(L3) → Path p can have fewer than k edges also. i.e., k ≤ |E| or k ≤ |V| - 1 (L4)
Each of the |V| - 1 iterations Relaxes all |E| edges. (L5)
(L4) and (L5) → Among the edges relaxed in the ith iteration,
for i = 1, 2, . . . , k, is (vi - 1, vi) (L6)
(L1) and (L6) → v.d = vk.d = δ (s, vk) = δ (s, v) (Path Relaxation Property)
13. Corollary
Let G = (V, E) be a weighted, directed graph with source s and weight function
w : E → R, and 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 < ꝏ.
Proof:
(1) For each vertex v є V, BELLMAN-FORD terminates with v.d < ꝏ if there is a path
from s to v
Let p = <v0, v1, . . . , vk> be a path from s to a vertex v
v.d is initialized to ꝏ for all the vertices in the beginning. (C1)
Bellman-Ford relaxes All the edges |V| - 1 times. (C2)
Everytime an edge is relaxed,
vi.d = vi - 1.d + w (vi - 1, vi) only if ((vi - 1.d + w (vi - 1, vi)) < vi.d) (C3)
14. Corollary…
(C1) (C2) and (C3) → v.d < ꝏ for a vertex that has a path from s to v (C4)
(C4) → Bellman-Ford terminates with v.d < ꝏ if there a path from s to v for all the
vertices v є V. (C5)
(2) If v.d < ꝏ for all the vertices v є V then there is a path from s to v
v.d < ꝏ → v.d = u.d + w (u, v) (C6)
(C6) → vertex v has a predecessor vertex u, and an edge to v from u. (C7)
(C7) → if v.d < ꝏ for all the vertices v є V then there is a path from s to v. (C8)
(C5) and (C8) → there is a path from s to v if and only if BELLMAN-FORD
terminates with v.d < ꝏ.
15. Correctness of Bellman-Ford Algorithm
Let G = (V, E) be a weighted, directed graph 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. If G does contain a
negative-weight cycle reachable from s, then the algorithm returns FALSE.
Proof:
(1) v.d = δ (s, v)
If there is a path from s to v, then v.d = δ (s, v) (Lemma)
If there is no path from s to v, then v.d = δ (s, v) = ꝏ (No-Path Property)
(2) Gπ is a shortest-paths tree rooted at s
Since v.d = δ (s, v) (From (1)), the predecessor subgraph Gπ is a shortest-paths tree
rooted at s (Predecessor-Subgraph Property)
16. Correctness of Bellman-Ford Algorithm…
(3) If G contains no negative-weight cycles that are reachable from s, then the
algorithm returns TRUE
At termination, for all edges (u, v) є E, v.d = δ (s, v) (From (1)) (B3.1)
(B3.1) → v.d ≤ δ (s, u) + w (u, v) (Triangle Inequality) (B3.2)
(B3.2) → v.d = u.d + w (u, v)
Hence, the statement If (v.d > (u.d + w (u, v)) does not return FALSE for any edge
in the algorithm BELLMAN-FORD. (B3.3)
(B3.3) → The algorithm returns TRUE if G contains no negative-weight cycles
that are reachable from s.
17. Correctness of Bellman-Ford Algorithm…
(4) If G contains negative-weight cycles that are reachable from s, then the algorithm
returns FALSE
Let, c = <v0, v1, . . . , vk>, where v0 = vk be a negative-weight cycle reachable from s (B4.1)
(B4.1) → (B4.2)
Assumption: Algorithm returns TRUE (B4.3)
(B4.3) → vi.d ≤ vi - 1.d + w (vi - 1, vi) for i = 1, 2, . . . , k. (B4.4)
Summing equation in (B4.4)
≤ (B4.5)
≤ (B4.6)
18. Correctness of Bellman-Ford Algorithm…
- ≤ (B4.7)
Note that = (v0 = vk appears exactly once on both
sides and all other vertices are same) (B4.8)
From equations (B4.7) and (B4.8),
0 ≤ (B4.9)
Equation (B4.9) contradicts the assumption (B4.2).
Hence, Bellman-Ford algorithm returns TRUE if graph G contains no negative-
weight cycles reachable from the source, and FALSE otherwise.
19. Appendix
Triangle inequality
For any edge (u, v) є E, δ (s, v) ≤ δ (s, u) + w (u, v).
No-path property
If there is no path from s to v, then v.d = δ (s, v) = ꝏ always.
Path-relaxation property
If p = <v0, v1, . . . , vk> is a shortest path from s = v0 to vk , then vk.d = δ (s, vk) if the
edges of p are Relaxed in the order (v0, v1), (v1, v2), . . . , (vk - 1, vk).
Predecessor-subgraph property
Once v.d = δ (s, v) for all v є V , the predecessor subgraph is a shortest-paths tree
rooted at s.
20. References:
• Thomas H Cormen. Charles E Leiserson, Ronald L Rivest, Clifford Stein,
Introduction to Algorithms, Third Edition, The MIT Press Cambridge,
Massachusetts London, England.