0% found this document useful (0 votes)
15 views37 pages

Bellman-Ford Algorithm

The Bellman-Ford algorithm is designed to find the shortest paths in directed graphs with negative edge weights, updating distances through a relaxation process repeated N-1 times. It is particularly useful when Dijkstra's algorithm fails due to negative edges. The document includes a detailed explanation of the algorithm's mechanics, including manual runs with examples to illustrate the relaxation procedure.

Uploaded by

mahfujapple95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views37 pages

Bellman-Ford Algorithm

The Bellman-Ford algorithm is designed to find the shortest paths in directed graphs with negative edge weights, updating distances through a relaxation process repeated N-1 times. It is particularly useful when Dijkstra's algorithm fails due to negative edges. The document includes a detailed explanation of the algorithm's mechanics, including manual runs with examples to illustrate the relaxation procedure.

Uploaded by

mahfujapple95
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 37

Bellman-Ford

Algorithm
A Single Source Shortest Path
Introduction

Presented by Presented to
Md Redwanul Karim Nurul Amin
ID: 202331067171 Nahid
& Lecturer
Md Mustakim Department of
ID: 202331067067 CSE
Bangladesh
University
The Shortest Path Problem

Dijkstra's Algorithm

Bellman-Ford Algorithm
What is Bellman-Ford
Algorithm?
The Bellman-Ford algorithm is best suited to find the
shortest paths in a directed graph, with one or more
negative edge weights, from the source vertex to all
other vertices.
It does so by repeatedly checking all the edges in the
graph for shorter paths, as many times as there are
vertices in the graph (N-1 times).
Why Bellman-Ford?
Dijkstra's Algorithm fails when theme is negative edge.

Ex: Selects vertex B immediately after A. But short path A to


B is -
A -> C -> D -> B
4
A C

2 6

B D
-9
How it works
Set initial distance to zero for the source vertex, and set
initial distances to infinity for all other vertices.

For each edge, check if a shorter distance can be calculated,


and update the distance if the calculated distance is shorter
(Relaxation Procedure).
Check all edges (step 2) V−1 times. This is as many times as
there are vertices (V) minus one.

Optional: Check for negative cycles. This will be explained in


better detail later.
Relaxation Procedure
If there is an edge between a pair of vertices (u,v) then check,
The distance of [u] + cost of (u,v) < distance of [v] --- If this is true

d[u] + c(u,v) < d[v]


Then replace the final distance by following this,

d[v] = d[u] + c(u,v)

Otherwise leave it as it is.


Manual Run
Example: A directed graph, with one or more negative edge weights.

5
A C 1
2

2 E
1

-3
2
B D
-2
Manual Run
Example:
 List of edges:
5
A C (A,C)
1 (A,D)
2 (C,E)
(C,B)
2 E (C,D)
1
(B,A)
-3 (B,D)
2 (D,E)
B D
-2 Total edges = 8
Total vertices = 5
Now we have to Relax all these edges for
V-1 or 4 times.
Manual Run
Example: Initializing vertex A’s distance as 0. And all other  List of edges:
vertices distance as infinity. Now we can start relaxing the (A,C)
edges. (A,D)

0 ∞ (C,E)
5 (C,B)
A C (C,D)
1
(B,A)
2
(B,D)
(D,E)
1 2 E ∞
Total edges = 8
-3 Total vertices = 5
2
B D
-2


Manual Run  List of edges:
(A,C)
Example: Running for the 1st time, (A,D)
(C,E)
Relaxing the 1st edge (A,C) - 5 (C,B)

0 ∞ (C,D)
5 (B,A)
A C (B,D)
1
2 (D,E)

Total edges = 8
1 2 E
Total vertices = 5

-3 d[u] + c(u,v) <


2 d[v]
B D
-2 d[v] = d[u] +
∞ c(u,v)

Manual Run
 List of edges:
Example: Running for the 1st time, (A,C)
(A,D)
Relaxing the 2nd edge (A,D) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 (D,E)

1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
∞ c(u,v)
2
Manual Run
 List of edges:
Example: Running for the 1st time, (A,C)
(A,D)
Relaxing the 3rd edge (C,E) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
∞ c(u,v)
2
Manual Run
 List of edges:
Example: Running for the 1st time, (A,C)
(A,D)
Relaxing the 4th edge (C,B) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
Manual Run
 List of edges:
Example: Running for the 1st time, (A,C)
(A,D)
Relaxing the 5th edge (C,D) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
Manual Run
 List of edges:
Example: Running for the 1st time, (A,C)
(A,D)
Relaxing the 6th edge (B,A) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
Manual Run
 List of edges:
Example: Running for the 1st time, (A,C)
(A,D)
Relaxing the 7th edge (B,D) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 1st time, (A,C)
(A,D)
Relaxing the 8th edge (D,E) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 2nd time, (A,C)
(A,D)
Relaxing the 1st edge (A,C) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 2nd time, (A,C)
(A,D)
Relaxing the 2nd edge (A,D) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 2nd time, (A,C)
(A,D)
Relaxing the 3rd edge (C,E) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 2nd time, (A,C)
(A,D)
Relaxing the 4th edge (C,B) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 2nd time, (A,C)
(A,D)
Relaxing the 5th edge (C,D) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 2nd time, (A,C)
(A,D)
Relaxing the 6th edge (B,A) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 2nd time, (A,C)
(A,D)
Relaxing the 7th edge (B,D) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
 List of edges:
Example: Running for the 2nd time, (A,C)
(A,D)
Relaxing the 8th edge (D,E) - (C,E)
5
(C,B)
0 ∞ (C,D)
5
A C (B,A)
1 (B,D)
2 2 (D,E)
6∞
1 2 E Total edges = 8
Total vertices = 5
-3
2 d[u] + c(u,v) <
B D d[v]
-2
∞ d[v] = d[u] +
2 ∞ c(u,v)
2
0
Manual Run
Example:
So, the shortest path are
E = ACBDE = {5+(-3)+(-2)+2} = 2

5
A C 1
2

2 E
1

-3
2
B D
-2
Negative Cycles in The Bellman-Ford
Algorithm
If we can go in circles in a graph and the sum of edges in that circle is
negative, we have a negative cycle. Every time we check these edges with
the Bellman-Ford algorithm, the distances we calculate and update just
become lower and lower.
The problem with negative cycles is that a shortest path does
not exist, because we can always go one more round to get a
path that is shorter. 4
A C

5 5 -10

B D
3
That is why it is useful to implement the Bellman-
Ford algorithm with detection for negative cycles.
Negative Cycles in The Bellman-Ford
Algorithm -4
-6
-8

-2
4 4 4
0 A C 0
∞ A C

5 -10 5 -10
5 5

B D B D
∞ 3 ∞ ∞ 3 ∞
5 8
3 6
1 4
-1 2

We can see that, if there is a negative weight cycle then the runtime for
relaxation is more than (N-1) time.
PSUEDOCODE
BellmanFord(vertices, edges, source):

1. Initialize distance for all vertices:


for each vertex v in vertices:
dist[v] = infinity
dist[source] = 0

2. Relax all edges (V-1) times:


for i = 1 to (number of vertices - 1):
for each edge (u, v, weight) in edges:
if dist[u] + weight < dist[v]:
dist[v] = dist[u] + weight

3. Check for negative-weight cycles:


for each edge (u, v, weight) in edges:
if dist[u] + weight < dist[v]:
print "Graph contains a negative weight cycle"
return

4. Print the distances:


for each vertex v in vertices:
print v, dist[v]
Coding Part
output
Time and Space Complexity
Time taken by each part
1. Initialization:
Setting all distances to infinity takes O(V) time.

2.Main relaxation loop:


Outer loop runs (V-1) times.
Inside that, you loop over E edges.
Each relaxation step (checking and updating) takes O(1) time.

So main loop time ,


(V−1) × E× O(1)=O(VE)

Negative cycle check:


One extra pass over all edges → O(E) time.

3.Add all times together


Total time:
O(V)(initialize) + O(VE)(main relaxation) + O(E)(negative cycle check)
But O(VE) dominates the others (since for big V and E, VE is much larger than V or E individually).

📢 Final Answer:
✅ Therefore, the worst-case time complexity of Bellman-Ford is: O(VE)
Applications
🔹 1. Shortest Path in Graphs with Negative Weights
Bellman-Ford can handle graphs with negative weight edges, unlike Dijkstra’s.
Useful in routing algorithms where costs can decrease over time.

🔹 2. Detecting Negative Weight Cycles


One of its key features: it can detect if a negative cycle exists (i.e., a loop that can reduce path cost infinitely).
This is helpful in financial modeling or arbitrage detection where such cycles can mean potential profit.

🔹 3. Network Routing Protocols


Used in distance-vector routing protocols like:
RIP (Routing Information Protocol)
Each router maintains a table (vector) of minimum distances to every destination.
Periodically updates using Bellman-Ford to exchange info with neighbors.

🔹 4. Currency Arbitrage Detection


Currencies and exchange rates can be modeled as a graph with edges representing conversion rates.
Log of rates → weights. If a negative cycle exists → arbitrage opportunity.
Conclusion

The Bellman-Ford algorithm is a powerful and versatile tool for finding shortest
paths in graphs, especially when negative weights are involved.
While it may not be the fastest option for all scenarios, its ability to handle
negative edge weights and detect negative weight cycles makes
it invaluable in a wide range of real-world applications—from
network routing to financial analysis. Understanding this algorithm not only broadens
your toolkit for solving graph problems but also deepens your insight into how complex
systems can be modeled and optimized.
We are done here
Do you have any
question?

You might also like