The Bellman-Ford Algorithm is a dynamic programming algorithm used to find the shortest path from a single source to all other vertices in a weighted graph. Unlike Dijkstra’s Algorithm, it can handle negative weight edges and can also detect negative weight cycles.
0.(V - 1) times (where V is the number of vertices):
(u, v, w), update the distance to v if a shorter path via u is found (dist[v] = min(dist[v], dist[u] + w)).Here’s the complete C++ implementation using an edge list representation.
#include <iostream>
#include <vector>
#include <climits>
using namespace std;
// Structure to represent an edge
struct Edge {
int src, dest, weight;
};
// Bellman-Ford Algorithm Function
void bellmanFord(int V, int E, vector<Edge>& edges, int src) {
// Distance array initialized to "infinity"
vector<int> dist(V, INT_MAX);
// Set the source distance to 0
dist[src] = 0;
// Step 1: Relax all edges V-1 times
for (int i = 0; i < V - 1; i++) {
for (const auto& edge : edges) {
if (dist[edge.src] != INT_MAX && dist[edge.src] + edge.weight < dist[edge.dest]) {
dist[edge.dest] = dist[edge.src] + edge.weight;
}
}
}
// Step 2: Check for negative weight cycles
for (const auto& edge : edges) {
if (dist[edge.src] != INT_MAX && dist[edge.src] + edge.weight < dist[edge.dest]) {
cout << "Graph contains a negative weight cycle!\n";
return;
}
}
// Output shortest distances
cout << "Shortest distances from source node " << src << ":\n";
for (int i = 0; i < V; i++) {
cout << "Node " << i << " : " << (dist[i] == INT_MAX ? "INF" : to_string(dist[i])) << endl;
}
}
int main() {
int V = 5; // Number of vertices
int E = 7; // Number of edges
vector<Edge> edges = {
{0, 1, -1},
{0, 2, 4},
{1, 2, 3},
{1, 3, 2},
{1, 4, 2},
{3, 2, 5},
{3, 1, 1},
{4, 3, -3}
};
int source = 0; // Start node
bellmanFord(V, E, edges, source);
return 0;
}
{source, destination, weight}.V²).INT_MAX (infinity).0.V-1 times):
(u, v, w) is checked, and if a shorter path is found (dist[u] + w < dist[v]), we update dist[v].V-1 edges.(u, v, w) can still be relaxed, it means a negative cycle exists.| Operation | Complexity |
|---|---|
Edge relaxation (V-1 times) | O(V × E) |
| Negative cycle check | O(E) |
| Total Complexity | O(V × E) |
E ≈ V), it runs in O(V²), which is slower than Dijkstra (O((V+E) log V)).E ≈ V²), it runs in O(V³), making it inefficient for large graphs.For the given graph, running the program produces:
Shortest distances from source node 0:
Node 0 : 0
Node 1 : -1
Node 2 : 2
Node 3 : -2
Node 4 : 1
Explanation:
0 → 1 (-1), 0 → 2 (4), 1 → 2 (3), 1 → 3 (2), 1 → 4 (2), etc.4 → 3 (-3) makes dist[3] smaller than expected.