C Program
#include <stdio.h> #define INF 9999 int main() { int n = 5, e = 8; int edges[][3] = { {0, 1, 6}, {0, 2, 7}, {1, 2, 8}, {1, 3, -4}, {1, 4, 5}, {2, 3, 9}, {2, 4, -3}, {3, 1, -2} }; int d[5]; for (int i = 0; i < n; i++) d[i] = INF; d[0] = 0; for (int i = 0; i < n-1; i++) for (int j = 0; j < e; j++) { int u = edges[j][0], v = edges[j][1], w = edges[j][2]; if (d[u] != INF && d[u] + w < d[v]) d[v] = d[u] + w; } for (int i = 0; i < e; i++) if (d[edges[i][0]] + edges[i][2] < d[edges[i][1]]) { printf("Negative Cycle Detected"); return 0; } for (int i = 0; i < n; i++) printf("%d ", d[i]); }
C Output
Input:
Vertices: 5
Edges:
0→1 (6), 0→2 (7), 1→2 (8), 1→3 (-4), 1→4 (5), 2→3 (9), 2→4 (-3), 3→1 (-2)
Output:
Input: Source = 0
Output: 0 2 7 -2 4
C++ Program
#include <iostream> #include <vector> using namespace std; int main() { int n = 4; vector<tuple<int,int,int>> edges = { {0,1,1}, {1,2,3}, {2,3,2}, {3,1,-6} }; vector<int> d(n, 1e9); d[0] = 0; for (int i = 1; i < n; i++) for (auto [u,v,w] : edges) if (d[u] + w < d[v]) d[v] = d[u] + w; for (auto [u,v,w] : edges) if (d[u] + w < d[v]) { cout << "Negative Cycle Found"; return 0; } for (int x : d) cout << x << " "; }
C++ Output
Input:
Edges: 0→1(1), 1→2(3), 2→3(2), 3→1(-6)Output:
Input: Source = 0
Output: Negative Cycle Found
JAVA Program
import java.util.*; class Main { public static void main(String[] a) { int n = 3; int[][] e = {{0,1,4},{0,2,5},{1,2,-10}}; int[] d = new int[n]; Arrays.fill(d, Integer.MAX_VALUE); d[0] = 0; for (int i = 0; i < n-1; i++) for (int[] ed : e) if (d[ed[0]] != Integer.MAX_VALUE && d[ed[0]] + ed[2] < d[ed[1]]) d[ed[1]] = d[ed[0]] + ed[2]; for (int[] ed : e) if (d[ed[0]] + ed[2] < d[ed[1]]) { System.out.print("Negative Cycle"); return; } for (int x : d) System.out.print(x + " "); } }
JAVA Output
Input:
Edges: 0→1(4), 0→2(5), 1→2(-10)Output:
Input: Source = 0
Output: 0 4 -6
Python Program
n = 4 edges = [(0, 1, 2), (1, 2, -1), (2, 3, 2), (3, 1, -2)] d = [float('inf')] * n d[0] = 0 for _ in range(n - 1): for u, v, w in edges: if d[u] + w < d[v]: d[v] = d[u] + w for u, v, w in edges: if d[u] + w < d[v]: print("Negative Cycle Found") exit() print(*d)
Python Output
Input:
Edges: 0→1(2), 1→2(-1), 2→3(2), 3→1(-2)Output:
Input: Source = 0
Output: Negative Cycle Found
In-Depth Explanation
Example
In the example of C code, the graph has nodes 0 through 4 and edges with negative weight. Dijkstra would fail in this case, but Bellman-Ford keeps relaxing edges to compute shortest paths even if some edges are negative. If a path continues to decrease beyond (V–1) iterations, then there is a negative cycle.
Real-Life Analogy
Think about currency exchange across various nations. If you begin with $1, exchange it across currencies, and end up with something greater than $1, you've discovered a lucrative loop — a real life example of a negative weight cycle, only in terms of cost. Bellman-Ford enables us to identify such opportunity for arbitrage or errors in system planning.
Why It Matters
Bellman-Ford is:
Designed for use on graphs that contain negative edge weights
Able to identify cycles
Used in financial systems, routing protocols (such as RIP), and distributed systems
Unlike Dijkstra, which doesn't work with negative weights, Bellman-Ford is more capable, albeit slower (O(VE)).
Learning Insights
You learn:
Edge relaxation: checking if a shorter path is found
How to detect cycles by checking further updates
The distinction between Dijkstra (greedy) and Bellman-Ford (brute-force relaxation)
Real-world applications of negative weights
Bellman-Ford computes the shortest path even in unstable systems — which Dijkstra can't.
Interview & Real-World Use
This is often used to:
Test comprehension of shortest path algorithms
Check whether you can work with graphs containing negative weights
Detect negative cycles (advanced logic check)
Applied in:
Currency exchange programs (to find arbitrage)
Network routing (RIP protocol)
Constraint systems (travel planning, scheduling)
Inconsistency detection in cost-based systems
The Bellman-Ford algorithm is one of the strongest graph theory tools when working with graphs that have negative weights and cycle detection. In contrast to greedy algorithms, it has guaranteed correctness even in difficult scenarios. Whether used in money algorithms, routing algorithms, or scheduling, Bellman-Ford's repeated edge relaxation principle makes it both logically straightforward and useful in practice. The above examples in C, C++, Java, and Python serve to solidify your comprehension and demonstrate how it works across platforms and applications.
Social Plugin