Bellman-Ford Algorithm
Bellman-Ford Algorithm
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.
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.
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
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):
📢 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.
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?