Hello! Let's continue our journey into shortest path algorithms.
Introduction
In our last lesson, we explored Dijkstra's algorithm, a fast and efficient method for finding the shortest paths from a single source. However, we also discovered its crucial limitation: it fails when a graph contains edges with negative weights. The greedy approach of finalizing the "closest" node breaks down when a future path could offer a "shortcut" via a negative-cost edge.
This lesson directly addresses that limitation, fulfilling the learning outcome: Apply the Bellman-Ford algorithm for graphs with negative edge weights and to detect negative cycles.
The Bellman-Ford algorithm is slower than Dijkstra's, but it's more robust. It can handle graphs with negative edge weights, and it has a built-in mechanism to detect the presence of negative-weight cycles—a situation where shortest paths can become undefined (infinitely small). This makes it an indispensable tool for a wider range of graph problems, particularly in areas like finance where negative costs or losses are common.
1. The Core Idea: Systematic Relaxation
Unlike Dijkstra's greedy approach, Bellman-Ford is fundamentally based on dynamic programming. It solves the problem by systematically building up the solution. The core idea is that the shortest path from a source s to any vertex v can have at most edges (where is the number of vertices), assuming no negative cycles exist. If a path had more edges, it would have to contain a cycle, which wouldn't be optimal unless the cycle had a negative weight.
Based on this insight, the Bellman-Ford algorithm works by repeatedly relaxing every edge in the graph. It performs this relaxation process for passes.
- After 1 pass, it has found all shortest paths that are at most 1 edge long.
- After 2 passes, it has found all shortest paths that are at most 2 edges long.
- ...
- After passes, it has found all shortest paths that are at most edges long.
Therefore, after passes, the algorithm is guaranteed to have found all shortest simple paths.
To understand this foundational concept and how it differs from Dijkstra's, let's start with a clear overview.
Bellman Ford Algorithm | Shortest path & Negative cycles | Graph Theory
The video 'Bellman Ford Algorithm' by WilliamFiset provides an excellent introduction. It explains what the algorithm is, why it's needed, and the critical concept of negative cycles.
Please watch from the beginning to 04:00. Pay close attention to the distinction between negative edges (which Bellman-Ford can handle) and negative cycles (which make shortest paths undefined).
The process described in the video—initializing distances and then repeatedly relaxing all edges—is the heart of the algorithm. Let's look at the formal steps.
Bellman-Ford - finding shortest paths with negative weights
The article 'Bellman-Ford - finding shortest paths with negative weights' on cp-algorithms.com gives a concise, formal description of the algorithm and its implementation logic. This will solidify the concepts from the video.
Read the sections 'Description of the algorithm' and 'Implementation'. The key takeaway is the structure: an outer loop that runs V-1 times and an inner loop that iterates through all edges to perform relaxation. Notice the C++ code reflects this simple, nested-loop structure.
2. A Step-by-Step Walkthrough
Now that you have the high-level idea and the formal structure, let's walk through a complete example. Seeing the distance values update with each pass is the best way to understand how the algorithm converges to the correct answer.
The following image provides a static, step-by-step trace of the algorithm.

To see this process in a more dynamic fashion, let's return to the WilliamFiset video.
Bellman Ford Algorithm | Shortest path & Negative cycles | Graph Theory
This segment of the video provides a full, animated walkthrough of the algorithm on a sample graph. It's a great way to see the relaxation process in action.
Watch from 05:55 to 10:20. Follow the distance array on the right as the algorithm iterates. Note that the order in which edges are relaxed within a pass doesn't matter for the final correctness, though it can affect how quickly the values converge.
3. Detecting Negative Cycles
One of Bellman-Ford's most powerful features is its ability to detect negative-weight cycles. The logic is elegant:
- We've established that any shortest path in a graph with no negative cycles can have at most edges.
- After passes, the algorithm has found these paths.
- What happens if we run the algorithm for one more pass (the -th pass)? If any distance value can still be improved, it means we've found a shorter path that must involve at least edges. A path with edges through vertices must contain a cycle.
- Since the path's weight was reduced by traversing this cycle, the cycle's total weight must be negative.
So, the detection method is simple: After the main passes, perform one final pass. If any edge can still be relaxed, a negative cycle exists.

The following video demonstrates this detection process.
4.4 Bellman Ford Algorithm - Single Source Shortest Path - Dynamic Programming
In this video, Abdul Bari provides a fantastic explanation of how Bellman-Ford detects negative cycles. He runs the standard V-1 passes and then performs an extra pass to see if any values change.
Please watch from 10:14 to 17:02. The first part (until 13:00) shows a graph with negative edges but no negative cycles, where the V-th pass produces no changes. The second part introduces a negative cycle and shows how the V-th pass continues to relax edges, signaling the cycle's existence.
Once a negative cycle is detected, the shortest paths to any nodes that are part of the cycle or are reachable from it are undefined (or can be considered negative infinity), as you can traverse the cycle endlessly to reduce the path cost.
4. Proof of Correctness and Complexity
Your preference for understanding the "why" behind algorithms is key here. The proof of Bellman-Ford's correctness is a classic inductive argument that's quite intuitive.
This MIT OpenCourseWare lecture transcript provides a formal but clear proof of the algorithm's correctness, both for finding shortest paths and for detecting negative cycles.
Read the sections that correspond to the proofs (you can search for 'prove this theorem' and 'the argument with respect to the corollary'). The core idea is that after k passes, the algorithm correctly computes all shortest paths with at most k edges. Since any simple path has at most V-1 edges, the algorithm is correct after V-1 passes. The second part explains why a successful relaxation in the V-th pass implies a negative cycle.
Complexity Analysis
The complexity of Bellman-Ford is straightforward to analyze, which your software engineering background will make immediately apparent.
- The algorithm consists of a main loop that runs times (for the path finding) plus potentially one more time (for cycle detection). Let's say this is passes.
- Inside this loop, the algorithm iterates through all edges and performs a constant-time relaxation operation for each.
Therefore, the total time complexity is .
- In a sparse graph (where is close to ), the complexity is about .
- In a dense graph (where is close to ), the complexity becomes .
This is significantly slower than Dijkstra's , which is why Dijkstra's is preferred whenever you can guarantee non-negative edge weights.
The space complexity is to store the distances and predecessors.
Conclusion
In this lesson, we've added a powerful and robust tool to our algorithmic toolbox. While slower than Dijkstra's, Bellman-Ford's ability to handle negative weights and detect negative cycles makes it applicable to a much broader class of problems.
Key Takeaways:
- The Bellman-Ford algorithm solves the single-source shortest path problem for graphs that may contain negative edge weights.
- It works by relaxing all edges in the graph for passes.
- It can detect a negative-weight cycle by running one additional pass (-th pass); if any edge can still be relaxed, a negative cycle reachable from the source exists.
- The time complexity is , making it slower than Dijkstra's but more general.
Preview of the Next Lesson:
We will now shift our focus from finding shortest paths to another fundamental graph problem: finding a Minimum Spanning Tree (MST). An MST is a subset of edges that connects all vertices together with the minimum possible total edge weight, without forming any cycles. To tackle this, we'll first need to learn about a specialized data structure perfect for detecting cycles: the Union-Find data structure. We'll explore its implementation with optimizations like path compression and union by rank.