SlideShare a Scribd company logo
1 of 20
Download to read offline
Bellman Ford Algorithm
Dr. Kiran K
Assistant Professor
Department of CSE
UVCE
Bengaluru, India.
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.
• 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
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
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)
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
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
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
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
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
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)
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)
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)
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 < ꝏ.
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)
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.
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)
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.
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.
References:
• Thomas H Cormen. Charles E Leiserson, Ronald L Rivest, Clifford Stein,
Introduction to Algorithms, Third Edition, The MIT Press Cambridge,
Massachusetts London, England.

More Related Content

What's hot

01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtrackingmandlapure
 
Floyd warshall algorithm
Floyd warshall algorithmFloyd warshall algorithm
Floyd warshall algorithmA. S. M. Shafi
 
Ford Fulkerson Algorithm
Ford Fulkerson AlgorithmFord Fulkerson Algorithm
Ford Fulkerson AlgorithmAdarsh Rotte
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Shuvongkor Barman
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's AlgorithmTanmay Baranwal
 
InduSoft Web License Activation
InduSoft Web License ActivationInduSoft Web License Activation
InduSoft Web License ActivationAVEVA
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)Ishucs
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptshanthishyam
 
Ford fulkerson
Ford fulkersonFord fulkerson
Ford fulkersonbat coder
 
Percepio Tracealyzer for FreeRTOS on MiniZED
Percepio Tracealyzer for FreeRTOS on MiniZEDPercepio Tracealyzer for FreeRTOS on MiniZED
Percepio Tracealyzer for FreeRTOS on MiniZEDVincent Claes
 
Np completeness
Np completenessNp completeness
Np completenessRajendran
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithmgsp1294
 
Embedded Recipes 2018 - Yoctoception: Containers in the embedded world - Jéré...
Embedded Recipes 2018 - Yoctoception: Containers in the embedded world - Jéré...Embedded Recipes 2018 - Yoctoception: Containers in the embedded world - Jéré...
Embedded Recipes 2018 - Yoctoception: Containers in the embedded world - Jéré...Anne Nicolas
 
Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me? Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me? DVClub
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10chidabdu
 
Introduction to NP Completeness
Introduction to NP CompletenessIntroduction to NP Completeness
Introduction to NP CompletenessGene Moo Lee
 
The Linux Command Cheat Sheet
The Linux Command Cheat SheetThe Linux Command Cheat Sheet
The Linux Command Cheat SheetTola LENG
 

What's hot (20)

01 knapsack using backtracking
01 knapsack using backtracking01 knapsack using backtracking
01 knapsack using backtracking
 
Floyd warshall algorithm
Floyd warshall algorithmFloyd warshall algorithm
Floyd warshall algorithm
 
Ford Fulkerson Algorithm
Ford Fulkerson AlgorithmFord Fulkerson Algorithm
Ford Fulkerson Algorithm
 
Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)Presentation on Breadth First Search (BFS)
Presentation on Breadth First Search (BFS)
 
Embedded Operating System - Linux
Embedded Operating System - LinuxEmbedded Operating System - Linux
Embedded Operating System - Linux
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Bellman Ford's Algorithm
Bellman Ford's AlgorithmBellman Ford's Algorithm
Bellman Ford's Algorithm
 
InduSoft Web License Activation
InduSoft Web License ActivationInduSoft Web License Activation
InduSoft Web License Activation
 
Breadth first search (Bfs)
Breadth first search (Bfs)Breadth first search (Bfs)
Breadth first search (Bfs)
 
SINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.pptSINGLE SOURCE SHORTEST PATH.ppt
SINGLE SOURCE SHORTEST PATH.ppt
 
Ford fulkerson
Ford fulkersonFord fulkerson
Ford fulkerson
 
Percepio Tracealyzer for FreeRTOS on MiniZED
Percepio Tracealyzer for FreeRTOS on MiniZEDPercepio Tracealyzer for FreeRTOS on MiniZED
Percepio Tracealyzer for FreeRTOS on MiniZED
 
Np completeness
Np completenessNp completeness
Np completeness
 
Dijkstra's algorithm
Dijkstra's algorithmDijkstra's algorithm
Dijkstra's algorithm
 
Shortest Path in Graph
Shortest Path in GraphShortest Path in Graph
Shortest Path in Graph
 
Embedded Recipes 2018 - Yoctoception: Containers in the embedded world - Jéré...
Embedded Recipes 2018 - Yoctoception: Containers in the embedded world - Jéré...Embedded Recipes 2018 - Yoctoception: Containers in the embedded world - Jéré...
Embedded Recipes 2018 - Yoctoception: Containers in the embedded world - Jéré...
 
Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me? Verilator: Fast, Free, But for Me?
Verilator: Fast, Free, But for Me?
 
Algorithm chapter 10
Algorithm chapter 10Algorithm chapter 10
Algorithm chapter 10
 
Introduction to NP Completeness
Introduction to NP CompletenessIntroduction to NP Completeness
Introduction to NP Completeness
 
The Linux Command Cheat Sheet
The Linux Command Cheat SheetThe Linux Command Cheat Sheet
The Linux Command Cheat Sheet
 

Similar to Bellman-Ford Algorithm Negative Weight Cycles

Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dagKiran K
 
lecture 21
lecture 21lecture 21
lecture 21sajinsc
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithmKiran K
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Traian Rebedea
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsBenjamin Sach
 
Capitulo 4, 7ma edición
Capitulo 4, 7ma ediciónCapitulo 4, 7ma edición
Capitulo 4, 7ma ediciónSohar Carr
 
35 tangent and arc length in polar coordinates
35 tangent and arc length in polar coordinates35 tangent and arc length in polar coordinates
35 tangent and arc length in polar coordinatesmath266
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptprithivr1
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpathKrish_ver2
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithmsAyesha Tahir
 
Introduccio al calculo vectorial
Introduccio  al calculo vectorialIntroduccio  al calculo vectorial
Introduccio al calculo vectorialEDESMITCRUZ1
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Naor Ami
 
Modeling Transformations
Modeling TransformationsModeling Transformations
Modeling TransformationsTarun Gehlot
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiIbrahim Alfayoumi
 

Similar to Bellman-Ford Algorithm Negative Weight Cycles (20)

Single source shortes path in dag
Single source shortes path in dagSingle source shortes path in dag
Single source shortes path in dag
 
Daa chpater14
Daa chpater14Daa chpater14
Daa chpater14
 
lecture 21
lecture 21lecture 21
lecture 21
 
Johnson's algorithm
Johnson's algorithmJohnson's algorithm
Johnson's algorithm
 
Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10Algorithm Design and Complexity - Course 10
Algorithm Design and Complexity - Course 10
 
Shortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairsShortest Paths Part 2: Negative Weights and All-pairs
Shortest Paths Part 2: Negative Weights and All-pairs
 
Capitulo 4, 7ma edición
Capitulo 4, 7ma ediciónCapitulo 4, 7ma edición
Capitulo 4, 7ma edición
 
35 tangent and arc length in polar coordinates
35 tangent and arc length in polar coordinates35 tangent and arc length in polar coordinates
35 tangent and arc length in polar coordinates
 
Unit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.pptUnit 5 session 2 MinimumSpanningTrees.ppt
Unit 5 session 2 MinimumSpanningTrees.ppt
 
Dijksatra
DijksatraDijksatra
Dijksatra
 
2.6 all pairsshortestpath
2.6 all pairsshortestpath2.6 all pairsshortestpath
2.6 all pairsshortestpath
 
Prims & kruskal algorithms
Prims & kruskal algorithmsPrims & kruskal algorithms
Prims & kruskal algorithms
 
Introduccio al calculo vectorial
Introduccio  al calculo vectorialIntroduccio  al calculo vectorial
Introduccio al calculo vectorial
 
Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14Inroduction_To_Algorithms_Lect14
Inroduction_To_Algorithms_Lect14
 
Ch4
Ch4Ch4
Ch4
 
Minimum spanning tree
Minimum spanning treeMinimum spanning tree
Minimum spanning tree
 
Tcu12 crc multi
Tcu12 crc multiTcu12 crc multi
Tcu12 crc multi
 
Modeling Transformations
Modeling TransformationsModeling Transformations
Modeling Transformations
 
Minimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumiMinimum spanning tree algorithms by ibrahim_alfayoumi
Minimum spanning tree algorithms by ibrahim_alfayoumi
 
Chapter 04 drill_solution
Chapter 04 drill_solutionChapter 04 drill_solution
Chapter 04 drill_solution
 

Recently uploaded

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdfssuser54595a
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...M56BOOKSTORE PRODUCT/SERVICE
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersSabitha Banu
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxsocialsciencegdgrohi
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTiammrhaywood
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxthorishapillay1
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaVirag Sontakke
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsanshu789521
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceSamikshaHamane
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfMahmoud M. Sallam
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...jaredbarbolino94
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,Virag Sontakke
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatYousafMalik24
 

Recently uploaded (20)

18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
18-04-UA_REPORT_MEDIALITERAСY_INDEX-DM_23-1-final-eng.pdf
 
OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...OS-operating systems- ch04 (Threads) ...
OS-operating systems- ch04 (Threads) ...
 
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
KSHARA STURA .pptx---KSHARA KARMA THERAPY (CAUSTIC THERAPY)————IMP.OF KSHARA ...
 
DATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginnersDATA STRUCTURE AND ALGORITHM for beginners
DATA STRUCTURE AND ALGORITHM for beginners
 
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Tilak Nagar Delhi reach out to us at 🔝9953056974🔝
 
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptxHistory Class XII Ch. 3 Kinship, Caste and Class (1).pptx
History Class XII Ch. 3 Kinship, Caste and Class (1).pptx
 
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPTECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
ECONOMIC CONTEXT - LONG FORM TV DRAMA - PPT
 
How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Proudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptxProudly South Africa powerpoint Thorisha.pptx
Proudly South Africa powerpoint Thorisha.pptx
 
Painted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of IndiaPainted Grey Ware.pptx, PGW Culture of India
Painted Grey Ware.pptx, PGW Culture of India
 
Presiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha electionsPresiding Officer Training module 2024 lok sabha elections
Presiding Officer Training module 2024 lok sabha elections
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
Roles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in PharmacovigilanceRoles & Responsibilities in Pharmacovigilance
Roles & Responsibilities in Pharmacovigilance
 
ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)ESSENTIAL of (CS/IT/IS) class 06 (database)
ESSENTIAL of (CS/IT/IS) class 06 (database)
 
Pharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdfPharmacognosy Flower 3. Compositae 2023.pdf
Pharmacognosy Flower 3. Compositae 2023.pdf
 
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝Model Call Girl in Bikash Puri  Delhi reach out to us at 🔝9953056974🔝
Model Call Girl in Bikash Puri Delhi reach out to us at 🔝9953056974🔝
 
Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...Historical philosophical, theoretical, and legal foundations of special and i...
Historical philosophical, theoretical, and legal foundations of special and i...
 
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,भारत-रोम व्यापार.pptx, Indo-Roman Trade,
भारत-रोम व्यापार.pptx, Indo-Roman Trade,
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
Earth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice greatEarth Day Presentation wow hello nice great
Earth Day Presentation wow hello nice great
 

Bellman-Ford Algorithm Negative Weight Cycles

  • 1. Bellman Ford Algorithm Dr. Kiran K Assistant Professor Department of CSE UVCE Bengaluru, India.
  • 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.