Belman-Ford algorithm in 2d Array
Master System Design with Codemia
Enhance your system design skills with over 120 practice problems, detailed solutions, and hands-on exercises.
Introduction
The Bellman-Ford algorithm is a classical algorithm for computing shortest paths from a single source vertex to all other vertices in a weighted graph. Unlike Dijkstra's algorithm, Bellman-Ford can handle graphs with negative weight edges, making it more versatile at the cost of higher time complexity. This article explores how the Bellman-Ford algorithm works when the graph is represented as a 2D array (adjacency matrix), with a complete code example and analysis of edge cases.
Graph Representation with a 2D Array
When using a 2D array to represent a graph for Bellman-Ford, the array is the adjacency matrix of the graph:
- Each cell
graph[i][j]holds the weight of the edge from vertex to vertex . - If there is no edge between vertices and , the value is set to infinity (or a sentinel value like
float('inf')in Python). - A value of on the diagonal (
graph[i][i]) means the distance from a vertex to itself is zero.
For a graph with vertices, the adjacency matrix has dimensions .
Algorithm Steps
1. Initialization
Create a distance array dist[] of size :
- Set
- Set for all other vertices
This means the source is reachable with zero cost, and all other vertices are initially considered unreachable.
2. Relaxation
Iterate times over all edges. For each edge from vertex to vertex with weight :
When using an adjacency matrix, "iterating over all edges" means scanning every cell graph[u][v] that is not infinity. Each of the rounds guarantees that shortest paths using at most edges are found by round .
3. Negative Cycle Detection
After completing the iterations, perform one additional pass over all edges. If any distance can still be reduced, a negative weight cycle exists in the graph, and shortest paths are not well-defined.
Implementation
Here is a complete Python implementation using a 2D adjacency matrix:
Worked Example
Using the graph above with 4 vertices and source vertex 0:
Initial distances: [0, inf, inf, inf]
Round 1:
- Edge (weight 4):
- Edge (weight 3):
- Edge (weight 2):
Round 2:
- Edge (weight -5): (no change)
Round 3: No further updates.
Final distances: [0, 4, 7, 9]
Complexity Analysis
- Time Complexity: when using an adjacency matrix, because scanning all edges requires per round and there are rounds. With an edge list representation, this becomes .
- Space Complexity: for the distance array (the adjacency matrix is the input and is not counted as extra space).
By comparison, Dijkstra's algorithm with a priority queue achieves but cannot handle negative edge weights.
When to Use Bellman-Ford
- The graph contains negative weight edges (Dijkstra fails here).
- You need to detect negative weight cycles.
- The graph is dense enough that the adjacency matrix representation is natural.
For sparse graphs without negative weights, Dijkstra with a min-heap is the better choice.
Common Pitfalls
- Forgetting to initialize the source distance to zero.
- Using an adjacency matrix but not skipping infinity entries during relaxation, which leads to integer overflow issues.
- Running only one pass instead of passes, which misses shortest paths that use many edges.
- Confusing "no path exists" (distance remains infinity) with "negative cycle detected" (distance keeps decreasing).
Summary
The Bellman-Ford algorithm finds shortest paths from a single source in graphs that may contain negative weights. When the graph is stored as a 2D adjacency matrix, the triple-nested loop structure is straightforward but runs in time. The algorithm's ability to detect negative cycles makes it uniquely valuable compared to Dijkstra's algorithm, at the cost of higher time complexity.

