The Bellman-Ford Algorithm is a well-known algorithm in computer science that finds the shortest path from a source node to all other nodes in a weighted graph. It is capable of handling negative-weight edges. So, it is useful in computer networks and other networking applications. The algorithm is based on the idea of relaxation. Relaxation updates the shortest known distance for each vertex. In this blog, you will learn about the Bellman-Ford Algorithm, the steps to implement it, and a real-life example.
Table of Contents:
What is Bellman-Ford Algorithm?
In computer science, the Bellman-Ford algorithm is one of the most popular algorithms that is used for finding the shortest path. This algorithm in Computer Networks is used to find the shortest path from a single source node to all other nodes in a weighted graph. This algorithm is named after Richard Bellman and Lester Ford, who invented this algorithm. It is useful in cases when a graph contains negative-weight edges
Unlock Python: Power Up Your Programming Skills!
Dive into real-world coding projects and become confident in writing clean, efficient Python code
How does the Bellman-Ford Algorithm Work?
This algorithm works on the concept called relaxation, which means updating the shortest distance of every vertex step by step.
Let us understand the working of this algorithm:
Step 1: Start with a source vertex.
Step 2: Set the distance to the source as 0 and all other vertices as infinity.
Step 3: For each edge, check if the distance to the destination can be made shorter by going through another vertex.
Step 4: Repeat this process for all vertices (V-1) times, where V is the number of vertices.
Step 5: Finally, check for negative weight cycles. If any distance can still be reduced, then the graph contains a negative cycle.
The algorithm repeats the process several times to make sure that every shortest path is correctly found.
To apply the Bellman-Ford algorithm, we need:
- A set of nodes (vertices)
- A list of edges, each containing a starting vertex, an ending vertex, and a weight.
- A source vertex from which we want to find the shortest paths.
Now, let us understand the procedure:
Step 1: Initialization
Initialize the distance from the source vertex to itself as 0, and set the distance to all other vertices as infinity.
Step 2: Relaxation
- Check if the distance to vertex v can be made shorter by going through the vertex u for edge (u,v).
- If yes, then update the distance of v.
- Repeat step 2 for all edges (V – 1) times.
Step 3: Check for Negative Weight Cycles
Check all the edges again, and if any distance can still be reduced, then there is a negative-weight cycle in the graph.
Bellman-Ford Algorithm Example: Step-by-Step
Let us understand the approach with the help of an example:
The above graph has five vertices (1 to 5), and with the help of the this algorithm, we will find the shortest distance from the source vertex 1 to every other vertex.
Step 1: Initialization
Initially, the distance to the vertex at the source(1) is set to 0, and all the other vertices are set to infinity as they are not reachable yet.
Step 2: Relaxation
There are 5 vertices, so in step 2, we will relax all the edges 4 times (V-1=4).
This step helps in checking a shorter path through another vertex.
Iteration 1
- 1 => 2 with weight 6 => distance(2) = 0 + 6 = 6
- 1 => 3 with weight 5 => distance(3) = 0 + 5 = 5
- 2 => 4 with weight -1 => distance(4) = 6 – 1 = 5
- 3 => 2 with weight -2 => distance(2) = min(6, 5 – 2) = 3
- 3 => 4 with weight 4 => distance(4) = min(5, 5 + 4) = 5
- 3 => 5 with weight 3 => distance(5) = 5 + 3 = 8
- 4 => 5 with weight 3 => distance(5) = min(8, 5 + 3) = 8
Iteration 2
- 3 => 2 = -2 => no improvement (distance 2 stays 3)
- 2 => 4 = -1 => distance(4) = min(5, 3 – 1) = 2
- 4 => 5 = 3 => distance(5) = min(8, 2 + 3) = 5
Iteration 3:
No shorter paths are found. Distances remain the same.
Iteration 4:
Again, no changes, so the distances are final.
Step 3: Check for Negative Weight Cycles
Run one more iteration, and if any distance changes, then a negative weight cycle exists. Here, there is no change in the distances, so this graph does not contain any negative-weight cycle.
Final Shortest Distance Result
Explanation:
- The algorithm originates at vertex 1 and successively checks all edges several times to update all distances.
- Negative weights (such as -1 and -2) are handled without issues with the help of the relaxation technique.
- On each pass, all the shortest paths have been updated correctly.
- At the end of the process, the shortest distance from vertex 1 to all other vertices was computed to be [0, 3, 5, 2, 5].
- The algorithm also verifies that there is no negative weight cycle.
Pseudocode of Bellman-Ford Algorithm
Let us have a look at the Pseudocode:
def BellmanFord(Edges, src, V):
INF = 999999999
dis = [INF] * V
dis[src] = 0
for _ in range(V - 1):
for edge in Edges:
u, v, wt = edge
dis[v] = min(dis[v], dis[u] + wt)
for edge in Edges:
u, v, wt = edge
if dis[v] > dis[u] + wt:
print("Negative Weight cycle exists.")
return dis
Here, the INF denotes infinity.
Code Implementations of the Bellman-Ford Algorithm (C, Java, Python)
Let us have a look at the implementation of the algorithm with different programming languages:
1. C/C++ Implementation
Here is how this algorithm in C works:
#include <vector>
#include <climits>
using namespace std;
vector<int> bellmanFord(vector<vector<int>> edges, int src, int V) {
vector<int> dis(V, INT_MAX);
dis[src] = 0;
for (int i = 0; i < V - 1; i++) {
for (int j = 0; j < edges.size(); j++) {
int u = edges[j][0];
int v = edges[j][1];
int wt = edges[j][2];
if (dis[u] != INT_MAX && dis[v] > dis[u] + wt)
dis[v] = dis[u] + wt;
}
}
for (int j = 0; j < edges.size(); j++) {
int u = edges[j][0];
int v = edges[j][1];
int wt = edges[j][2];
if (dis[u] != INT_MAX && dis[v] > dis[u] + wt)
return {}; // Negative cycle detected
}
return dis;
}
2. Java Implementation
Here is how this algorithm in Java works:
import java.util.Arrays;
int[] bellmanFord(int[][] edges, int V, int src) {
int[] dis = new int[V];
Arrays.fill(dis, Integer.MAX_VALUE);
dis[src] = 0;
for (int i = 0; i < V - 1; i++) {
for (int j = 0; j < edges.length; j++) {
int u = edges[j][0];
int v = edges[j][1];
int wt = edges[j][2];
if (dis[u] != Integer.MAX_VALUE && dis[u] + wt < dis[v])
dis[v] = dis[u] + wt;
}
}
for (int j = 0; j < edges.length; j++) {
int u = edges[j][0];
int v = edges[j][1];
int wt = edges[j][2];
if (dis[u] != Integer.MAX_VALUE && dis[u] + wt < dis[v])
return new int[0]; // Negative cycle detected
}
return dis;
}
3. Python Implementation
Here is how this algorithm in Java works:
def bellmanFord(V, edges, src):
INF = 999999999
dis = [INF] * V
dis[src] = 0
for _ in range(V - 1):
for u, v, wt in edges:
if dis[u] != INF and dis[u] + wt < dis[v]:
dis[v] = dis[u] + wt
for u, v, wt in edges:
if dis[u] != INF and dis[u] + wt < dis[v]:
return [] # Negative cycle detected
return dis
Complexity Analysis of Bellman-Ford Algorithm
Complexity refers to how fast the algorithm runs (time) and how much memory it uses (space). Let us look at the time and space complexity of the shortest-path method.
1. Bellman-Ford Algorithm Time Complexity:
Understanding the time complexity of the algorithm helps evaluate its performance across different graph sizes.
Given a graph with V vertices and E edges:
- It performs edge investigations V-1 times.
- Checking all edges once takes E time.
So, the total time = (V – 1) x E = O(V x E)
2. Space Complexity:
There is no extra memory used apart from edges, in which we store the distance from the source to reach a vertex in an array of size V.
So, space complexity is: O(V)
Get 100% Hike!
Master Most in Demand Skills Now!
Difference Between Bellman-Ford and Dijkstra’s Algorithm
| Feature |
Bellman-Ford Algorithm |
Dijkstra’s Algorithm |
| Negative Weights |
Bellman-Ford can handle negative-weight edges. |
It cannot handle negative weights. |
| Time Complexity |
The Time Complexity of the Bellman-Ford algorithm is O(V × E). |
The Time Complexity of Dijkstra’s algorithm is O((V + E) log V) with a priority queue. |
| Algorithm Type |
It is based on dynamic programming. |
It is based on the greedy algorithm. |
| Detect Negative Cycles |
Yes, it can detect the negative cycles. |
No, it cannot detect the negative cycles. |
| Use Case |
Networking, graphs with negative edges. |
Graphs with non-negative weights. |
Applications of Bellman-Ford Algorithm
Let us explore some important applications where this algorithm is used:
1. Networking and Routing
- It is one of the most popular uses of this algorithm in computer networks. It helps in finding the shortest path for packets of data from one computer to another. This algorithm in networking helps routers determine the shortest path to transfer data efficiently.
2. GPS Navigation
- The GPS systems also use shortest path algorithms in the search for the best route.
- This algorithm is used to calculate the paths when roads have negative costs(toll discounts or decreased time rate).
3. Robotics and Urban Planning
- In the field of robotics, robots may need to traverse a grid or a map with obstacles.
- Bellman-Ford helps in calculating the shortest and safest path to reach a destination.
- Planning for the roads and transportation routes frequently involves planners using Bellman-Ford in an efficient manner.
Best Practices for Implementing the Bellman-Ford Algorithm
- Proper Data Structures: Use arrays or lists to store edges and distances. This allows you to easily iterate and update.
- Handle Negative Weights Carefully: Always check for negative weight cycles after running the algorithm. This is important, as skipping this step can lead to the incorrect shortest path.
- Early Termination: If no distances have been updated while iterating, completing all V-1 iterations is unnecessary. This saves time while working with large graphs.
- 5. Evaluation on Various Graphs: Test your implementation against positive, negative, and mixed weight graphs, as well as graphs that include cycles, to check for correctness.
- 6. Unreachable Node: Make sure that your code handles unreachable nodes by setting their distance to infinity.
Start Coding in Python for Free: No Experience Needed
Begin writing actual Python code through interactive, beginner-friendly modules completely for free.
Conclusion
The Bellman-Ford algorithm is a reliable method for finding shortest paths, even in graphs with negative weights. It will relax the edges to ensure that the shortest distances are computed correctly, and it will detect negative weight cycles, which many other algorithms, such as Dijkstra’s algorithm, cannot. The algorithm runs in time complexity of O(V × E) and space complexity of O(V), so it is suitable only for small to medium-sized graphs. The applications of this algorithm vary from networking, GPS navigation, robotics, logistics, and many more, which makes it an important algorithm for computer networks and real-life cases.
Check out the other related blogs:
Bellman-Ford Algorithm – FAQs
Q1. How does the Bellman-Ford Algorithm work?
Bellman-Ford Algorithm works by making all the edges relax (V-1 times) and updating the shortest path from the source to every vertex.
Q2. What is the space complexity of the Bellman-Ford Algorithm?
The space complexity is O(V) because it stores distances for each vertex in an array.
Q3. Can Bellman-Ford handle negative weights?
Yes, Bellman-Ford can handle the negative weights correctly.
Q4. What is the time complexity of the Bellman-Ford Algorithm?
The time complexity is O(V × E), where V is the number of vertices and E is the number of edges.
Q5. Can Bellman-Ford detect negative cycles?
Yes, it detects them by checking if any edge can still be relaxed after V-1 iterations.