15. Bellman-Ford
The problem can be found at the following link: Problem Link
Problem Description
Given a weighted graph with V vertices (numbered from 0 to V-1) and a list of E edges, where each edge is represented as a triplet [u, v, w] denoting a directed edge from u to v with weight w.
Also given a source vertex src.
Your task is to compute the shortest distance from the source to all other vertices using the Bellman-Ford algorithm.
If a vertex is unreachable from the source, mark its distance as 1e8.
If the graph contains a negative weight cycle, return
[-1].
📝 Note: Sorry for uploading late, my Final Sem exam is going on.
Examples
Example 1:
Input:
V = 5
edges = [[1, 3, 2], [4, 3, -1], [2, 4, 1], [1, 2, 1], [0, 1, 5]]
src = 0Output:
Explanation:
0 → 1: 5
0 → 1 → 2: 6
0 → 1 → 2 → 4 → 3: 6
0 → 1 → 2 → 4: 7
Example 2:
Input:
Output:
Explanation:
The cycle: 1 → 2 → 3 → 1 has a negative weight → Negative Cycle Detected
Constraints
$( 1 \leq V \leq 100 )$
$( 1 \leq E = \text{edges.size()} \leq V \times (V-1) )$
$( -1000 \leq w \leq 1000 )$
$( 0 \leq \text{src} < V )$
My Approach
Standard Bellman-Ford Algorithm
The Bellman-Ford algorithm is designed to find the shortest paths from a single source node to all other nodes, even when negative edge weights exist.
Algorithm Steps:
Initialize a distance array
dist[]of sizeVwith all elements as1e8, and setdist[src] = 0.Relax all edges
V - 1times.Check for negative weight cycles by attempting one more relaxation:
If any edge still updates, it means a negative cycle exists → return
[-1].
Time and Auxiliary Space Complexity
Expected Time Complexity: $( O(V \times E) )$, as we perform edge relaxation
V-1times over allEedges.Expected Auxiliary Space Complexity: $( O(V) )$, to store the distance array of size
V.
Code (C++)
⚡ Alternative Approaches
📊 2️⃣ Bellman-Ford with Early Termination Optimization
Algorithm Steps:
Initialize a distance array with a large value (here,
1e8) and set the source distance to 0.For each of the
V - 1iterations, update the distances of the vertices.If no update is made in an iteration, break out early to optimize runtime.
Check once more for any possible relaxation to detect negative cycles.
Return
{-1}if a negative cycle is detected, else return the distance array.
📝 Complexity Analysis
Time Complexity:
O(V * E)in worst-case, better with early break.Space Complexity:
O(V)
✅ Why This?
This can significantly reduce computation when the graph stabilizes early. A practical tweak that saves time in sparse or nearly optimal graphs.
🆚 Comparison of Approaches
Approach
⏱️ Time Complexity
🗂️ Space Complexity
✅ Pros
⚠️ Cons
Standard Bellman-Ford
🟢 O(V × E)
🟢 O(V)
Simple, classic, handles all cases
May do redundant passes
Optimized with Early Termination
🟡 Best: O(E), Worst: O(V × E)
🟢 O(V)
Faster on converging graphs, fewer passes
Still worst-case O(V × E)
Use Bellman-Ford when negative weights or cycles are possible.
Use Early Termination for performance optimization.
Code (Java)
Code (Python)
Contribution and Support:
For discussions, questions, or doubts related to this solution, feel free to connect on LinkedIn: Any Questions. Let’s make this learning journey more collaborative!
⭐ If you find this helpful, please give this repository a star! ⭐
📍Visitor Count
Last updated