Bellman–Ford algorithm
The Bellman–Ford algorithm is a dynamic programming algorithm that computes the shortest paths from a single source vertex to all other vertices in a weighted directed graph, accommodating negative edge weights provided no negative-weight cycles are present, and it can also detect such cycles if they exist.[1][2]
Named after mathematicians Richard Bellman and Lester R. Ford Jr., the algorithm traces its origins to Bellman's 1958 paper on routing problems in networks and Ford's 1956 report on network flow theory, where foundational ideas for iterative relaxation in shortest-path computation were introduced.[3][4] It operates by initializing distances from the source to infinity except for the source itself (set to zero), then iteratively relaxing all edges in the graph |V| - 1 times, where |V| is the number of vertices; each relaxation updates a vertex's distance if a shorter path is found via an adjacent edge.[1][2] After these iterations, a final pass over all edges checks for further relaxations, indicating a negative-weight cycle reachable from the source if any occur.[1][2]
The algorithm's time complexity is O(|V| \times |E|), where |E| is the number of edges, making it suitable for sparse graphs but less efficient than alternatives like Dijkstra's algorithm for non-negative weights.[5][6] Despite its higher asymptotic cost, Bellman–Ford remains essential in applications involving potential negative weights, such as arbitrage detection in financial networks or certain routing protocols, and serves as a basis for distributed variants like the distance-vector routing protocol.[2][7] Its correctness relies on the principle that the shortest path to any vertex uses at most |V| - 1 edges, ensuring convergence after the specified iterations in acyclic paths.[1]
History and Background
Origins and Development
The Bellman–Ford algorithm emerged in the 1950s as a key application of dynamic programming to optimization problems in networks, particularly for finding efficient paths in complex systems.[3][8] Although first proposed by Alfonso Shimbel in 1955 in his paper "Structure of Complex Systems and the Search for Their Inherent Simplicities," the algorithm is named after later contributors Richard Bellman and Lester Ford Jr.[9] This period marked a surge in operations research, driven by military and logistical needs during the Cold War, where researchers at institutions like the RAND Corporation sought mathematical tools to model resource allocation, routing, and control in uncertain environments.[10][11]
Central to its formulation was Richard Bellman's principle of optimality, introduced in the late 1940s and early 1950s as a foundational concept in dynamic programming. This principle posits that an optimal policy for a multistage decision process has the property that, regardless of the initial state and decision, the remaining decisions must constitute an optimal policy for the subsequent subproblem.[12] Bellman developed this idea while working on multistage decision processes at RAND, coining the term "dynamic programming" to describe recursive optimization techniques that break down problems into overlapping subproblems.[12] The principle enabled systematic computation of optimal solutions by building from simpler stages, laying the groundwork for algorithms addressing shortest paths in graphs with potential negative weights.
Detailed descriptions of the algorithm were provided by Lester R. Ford Jr. in 1956, who described a relaxation-based method for computing shortest paths in his RAND Corporation report on network flow theory, motivated by practical routing challenges.[8] Independently, Richard Bellman formalized a similar dynamic programming approach in his 1958 paper "On a Routing Problem," applying the principle of optimality to derive recursive equations for minimal-time paths in queueing networks.[3] These works, rooted in the era's emphasis on network analysis for defense and logistics, established the core iterative relaxation procedure that detects negative cycles and computes distances from a source vertex.[3][8]
Key Contributors
The Bellman–Ford algorithm is named after two primary contributors, Richard Bellman and Lester Ford Jr., who independently developed key aspects of the method in the mid-1950s. Richard Bellman, an American applied mathematician born in 1920 and deceased in 1984, is widely recognized as the inventor of dynamic programming, a foundational technique he introduced in the early 1950s while working at the RAND Corporation, a think tank focused on operations research and systems analysis.[13] In 1958, Bellman applied dynamic programming principles to solve the shortest path problem in routing networks, describing an iterative approach to compute optimal paths in graphs with possible negative weights in his paper "On a Routing Problem," published in the Quarterly of Applied Mathematics.[3]
Lester Ford Jr., born in 1927 and deceased in 2017, was an American mathematician specializing in network flow problems and operations research, also affiliated with the RAND Corporation during the algorithm's formative years.[14] In 1956, Ford, collaborating with D. R. Fulkerson, introduced the relaxation method—a core mechanism of iteratively updating distance estimates—in his RAND report "Network Flow Theory," where he outlined its use for finding shortest paths in networks as part of broader flow optimization techniques.[8] Ford's work extended to linear programming, contributing foundational methods for solving transportation and assignment problems through network-based formulations that bridged combinatorial optimization and linear constraints.[15]
The algorithm also acknowledges independent contributions from Edward F. Moore, an American computer scientist and mathematician, who in 1959 published a variation emphasizing efficient path exploration in mazes and graphs, presented as "Algorithm D" in his paper "The Shortest Path through a Maze" at the International Symposium on the Theory of Switching.[16] Moore's formulation, while focused on unweighted cases, reinforced the relaxation-based paradigm and is sometimes referred to in extensions of the Bellman–Ford method.
Algorithm Description
Problem Setup and Assumptions
The Bellman–Ford algorithm solves the single-source shortest path problem in a weighted directed graph G = (V, E), where V is the finite set of vertices (also called nodes) and E \subseteq V \times V is the set of directed edges. Each edge (u, v) \in E is assigned a real-valued weight w(u, v), representing the cost or length of traversing from vertex u to vertex v. These weights are finite and may include positive, negative, or zero values, allowing the algorithm to handle scenarios where some paths become cheaper by including negative-cost edges.[17]
Given a designated source vertex s \in V, the objective is to compute the shortest path from s to every other vertex v \in V. A path from s to v is a sequence of edges forming a walk from s to v without cycles, and its total weight is the sum of the weights of its edges. The algorithm assumes the graph contains no negative-weight cycles—closed paths with total weight less than zero—as such cycles would render shortest paths undefined by allowing arbitrarily low costs through repeated traversal. This assumption ensures that shortest paths exist and are well-defined for all vertices reachable from s.[17]
Standard notation for the problem includes \delta(s, v), which denotes the shortest-path distance from s to v, defined as the minimum total weight over all paths from s to v (or \infty if no such path exists). Additionally, \pi(v) represents the predecessor of v in a shortest path from s, forming part of the shortest path tree rooted at s. These distances and predecessors enable reconstruction of the actual paths once computed.
Step-by-Step Procedure
The Bellman–Ford algorithm initializes distance estimates from a designated source vertex s to all other vertices in the graph. Specifically, the distance d(s) is set to 0, while d(v) = \infty for every vertex v \neq s. An accompanying predecessor array \pi is initialized such that \pi(v) = null for all v \neq s, enabling subsequent path reconstruction.[4][17]
The algorithm then performs relaxation across all edges for exactly |V| - 1 iterations, where |V| denotes the number of vertices. In each iteration, every edge (u, v) with weight w(u, v) is considered; if d(v) > d(u) + w(u, v), the estimate is updated as d(v) = d(u) + w(u, v) and \pi(v) = u. This process systematically refines the distance estimates by propagating improvements from already-reached vertices to their neighbors.[17]
The limit of |V| - 1 iterations arises because, assuming no negative-weight cycles, any shortest path from s to another vertex involves at most |V| - 1 edges, ensuring that all relevant path lengths are fully relaxed within this bound.[17]
At termination, the array d provides the shortest-path distances from s to all vertices (with \infty indicating unreachability), while the predecessor array \pi supports tracing the shortest paths by backtracking from any target vertex to s.[4][17]
Pseudocode and Example
The Bellman–Ford algorithm can be expressed in pseudocode as follows, where G = (V, E) is a weighted directed graph, s \in V is the source vertex, w(u, v) denotes the weight of edge (u, v) \in E, d represents the shortest-path distance estimate from s to vertex v, and \pi is the predecessor of v in the shortest path:
BELLMAN-FORD(G, w, s)
Initialize d[v] ← ∞ for all v ∈ V
d[s] ← 0
π[v] ← NIL for all v ∈ V
for i ← 1 to |V| - 1
for each edge (u, v) ∈ E
if d[v] > d[u] + w(u, v)
d[v] ← d[u] + w(u, v)
π[v] ← u
BELLMAN-FORD(G, w, s)
Initialize d[v] ← ∞ for all v ∈ V
d[s] ← 0
π[v] ← NIL for all v ∈ V
for i ← 1 to |V| - 1
for each edge (u, v) ∈ E
if d[v] > d[u] + w(u, v)
d[v] ← d[u] + w(u, v)
π[v] ← u
This pseudocode performs |V| - 1 iterations of edge relaxations, updating distance estimates and predecessors whenever a shorter path is found.
To illustrate the algorithm, consider a simple directed graph with four vertices s, a, b, t and the following edges with mixed positive and negative weights: s \to a with weight 3, s \to b with weight 5, a \to b with weight -1, a \to t with weight 8, and b \to t with weight 2. The graph can be visualized textually as:
- s connects to a (3) and b (5).
- a connects to b (-1) and t (8).
- b connects to t (2).
Starting from source s, the initial distance estimates are d = 0, d = \infty, d = \infty, d = \infty, with all predecessors \pi[\cdot] = \mathrm{NIL}.
In iteration 1, relaxing all edges updates:
- s \to a: d = 0 + 3 = 3, \pi = s.
- s \to b: d = 0 + 5 = 5, \pi = s.
- a \to b: d = \min(5, 3 + (-1)) = 2, \pi = a.
- a \to t: d = 3 + 8 = 11, \pi = a.
- b \to t: d = \min(11, 2 + 2) = 4, \pi = b.
Distances after iteration 1: d = 0, d = 3, d = 2, d = 4.
In iteration 2, relaxing all edges yields no further updates, as each potential relaxation does not improve the estimates (e.g., a \to b: $3 + (-1) = 2, already equal to d; b \to t: $2 + 2 = 4, already equal to d).
The final shortest-path distances are d = 0, d = 3, d = 2, d = 4. Since there are only three other vertices, two iterations suffice, and no changes occur afterward.[18]
To reconstruct the actual paths using the predecessor array \pi, start from the target vertex and trace back to s. For vertex t, \pi = b, \pi = a, \pi = s, yielding the path s \to a \to b \to t with total weight $3 + (-1) + 2 = 4. For a, the path is simply s \to a. For b, it is s \to a \to b with weight $3 + (-1) = 2. The path to s is trivial (empty). This reconstruction works because the \pi array records the last edge used in the shortest path during relaxations.
Theoretical Analysis
Proof of Correctness
The proof of correctness for the Bellman–Ford algorithm establishes that, assuming the input directed graph G = (V, E) with weight function w: E \to \mathbb{R} contains no negative-weight cycles reachable from the source vertex s, the algorithm computes the shortest-path distances \delta(s, v) for all vertices v \in V. This relies on the principle of optimality for shortest paths, which states that an optimal solution to the problem contains optimal solutions to subproblems, allowing dynamic programming to build solutions iteratively.
Consider the distance estimates d, initialized as d = 0 and d = \infty for v \neq s. The algorithm performs |V| - 1 iterations of relaxing all edges in E, where relaxation for edge (u, v) \in E updates d \leftarrow \min\{d, d + w(u, v)\}.
Lemma: After k iterations of relaxation (for $0 \leq k \leq |V| - 1), d is no greater than the weight of any path from s to v that contains at most k edges.
Proof of Lemma (by induction on k):
For the base case (k = 0), before any relaxations, d = 0 and d = \infty for all v \neq s. The only path from s to s with 0 edges has weight 0, so d \leq 0. For any v \neq s, no path with 0 edges exists, and \infty upper-bounds any nonexistent path weight by convention.
For the inductive hypothesis, assume the statement holds after k iterations for some $0 < k < |V| - 1: for every vertex u \in V, d \leq weight of any path from s to u with at most k edges.
Now consider the (k+1)-th iteration. Let P be any path from s to v with exactly k+1 edges, so P = s \to \cdots \to u \to v where the prefix path from s to u has exactly k edges and weight at least d by the inductive hypothesis. The weight of P is then at least d + w(u, v). During the (k+1)-th iteration, the algorithm relaxes (u, v), setting d \leftarrow \min\{d, d + w(u, v)\}. Since the previous d (after k iterations) was already no greater than the weight of any path to v with at most k edges (which is at most the weight of any path with at most k+1 edges), and the relaxation further ensures d \leq d + w(u, v) \leq weight of P, the inductive step holds. Paths with fewer than k+1 edges are covered by prior iterations. Thus, after k+1 iterations, d \leq weight of any path from s to v with at most k+1 edges.
We also observe that the estimates satisfy d \geq \delta(s, v) throughout the algorithm. Initially, this holds ($0 = \delta(s, s) and \infty \geq \delta(s, v) for v \neq s). Relaxation preserves this lower bound because if d \geq \delta(s, u), then d + w(u, v) \geq \delta(s, u) + w(u, v) \geq \delta(s, v) by the triangle inequality for shortest paths (subpaths of shortest paths are shortest). Thus, each update keeps d \geq \delta(s, v).
Theorem: After |V| - 1 iterations, d = \delta(s, v) for all v \in V.
Proof of Theorem: In the absence of negative-weight cycles reachable from s, every shortest path from s to any v is simple (contains no cycles) and thus has at most |V| - 1 edges. By the lemma with k = |V| - 1, d \leq weight of the shortest path from s to v (which has at most |V| - 1 edges), so d \leq \delta(s, v). Combined with d \geq \delta(s, v) from the relaxation invariant, equality follows.
Time and Space Complexity
The Bellman–Ford algorithm exhibits a time complexity of O(|V| \cdot |E|), where |V| denotes the number of vertices and |E| the number of edges in the graph. This bound stems from the core procedure, which executes |V|-1 iterations to ensure all shortest paths are found, with each iteration relaxing all |E| edges by checking and potentially updating the distance estimates for each edge's tail vertex.
In its original presentation, Ford outlined the algorithm as performing n-1 successive approximations—where n = |V|—each involving a complete scan of all arcs in the network to minimize path lengths iteratively.[8] Bellman similarly framed the approach as requiring n-1 steps of functional equation resolution, equivalent to edge relaxations across the graph structure. These foundational descriptions establish the linear factor per iteration, confirming the overall quadratic-to-cubic scaling depending on graph density.
The space complexity of the Bellman–Ford algorithm is O(|V|) for the primary data structures, namely the array tracking shortest-path distances from the source and an optional predecessor array for path reconstruction. If the input graph is represented explicitly, an adjacency list formulation adds O(|E|) space to store the edges and their weights.
Worst-case performance occurs in dense graphs, where |E| = \Theta(|V|^2), yielding O(|V|^3) total operations, particularly when the graph's topology and weights cause nearly every edge to be relaxed in each of the |V|-1 iterations. This scenario underscores the algorithm's efficiency limits for single-source shortest path computations in large, edge-heavy networks with negative weights.
Handling Negative Weights
Role of Negative Edges
The Bellman–Ford algorithm is particularly suitable for computing shortest paths in directed graphs that contain negative edge weights, as it systematically relaxes all edges in the graph multiple times without relying on the non-negativity assumption required by other methods. Unlike Dijkstra's algorithm, which uses a priority queue to greedily select the next vertex with the smallest tentative distance and fails to correctly handle negative weights because it does not revisit nodes once dequeued, Bellman–Ford iterates through every edge |V|-1 times, where |V| is the number of vertices, allowing updates to propagate regardless of edge signs. This repeated relaxation process ensures that negative weights can be incorporated into path computations without violating the algorithm's correctness, provided no negative cycles exist.[19][20][4]
Negative edge weights influence the algorithm by enabling distance estimates to decrease in subsequent iterations, as the relaxation step—updating the distance to a vertex u via an edge (v, u) with weight w if dist > dist + w—can produce smaller values when w is negative, even after initial paths have been considered. This propagation occurs because negative edges effectively "pull down" the shortest path distances, requiring multiple passes to fully account for all possible path combinations that include such edges. For instance, in a simple graph with source vertex A connected to B by an edge of weight 5 and to C by an edge of weight 3, and C connected to B by an edge of weight -2, the first relaxation sets dist[B] = 5 and dist[C] = 3, but the second relaxation via the negative edge updates dist[B] to 1, demonstrating how the negative weight reveals a shorter path (A → C → B) that is discovered only after intermediate updates. The algorithm's design, rooted in dynamic programming principles, accommodates this by ensuring that after |V|-1 iterations, all shortest paths without cycles are computed.[19][20]
A key assumption underlying the algorithm's handling of negative weights is the absence of negative-weight cycles, which would permit paths of arbitrarily small total weight by repeatedly traversing the cycle, rendering shortest paths undefined or unbounded. Without negative cycles, the finite number of iterations guarantees convergence to optimal distances, as each relaxation step brings the estimates closer to the true shortest paths in a graph where negative edges do not create loops of decreasing cost. This limitation distinguishes Bellman–Ford from algorithms assuming non-negative weights, emphasizing its utility in scenarios like network routing with potential cost reductions modeled as negatives.[19][4]
Detecting Negative Cycles
The Bellman–Ford algorithm extends its relaxation procedure to detect the presence of negative-weight cycles in a directed graph with edge weights that may include negatives. After performing the standard |V|-1 iterations of relaxing all |E| edges, where |V| is the number of vertices and |E| is the number of edges, the algorithm conducts one additional iteration over all edges. If any distance estimate d can still be updated via relaxation (i.e., d > d + w(u, v) for some edge (u, v)), this indicates the existence of a negative-weight cycle, as all shortest paths without cycles should have stabilized by the |V|-1th iteration.[1][21]
This detection mechanism identifies only negative cycles that are reachable from the designated source vertex s, because the relaxation process propagates updates solely along paths originating from s. Cycles in components of the graph disconnected from s or unreachable from s will not trigger updates in the nth iteration, even if they are negative. Thus, the algorithm assumes the graph may have multiple components but focuses on paths from s.[1][22]
To extract the actual negative cycle once detected, the algorithm maintains a predecessor array π during relaxations, which records the parent vertex for each updated distance estimate. Starting from any vertex v whose distance is updated in the nth iteration, one traces back through the π array: follow π, then π[π], and so on, until a cycle is encountered (i.e., a vertex repeats). This backward traversal yields the vertices in the cycle, which must have negative total weight due to the update. The cycle's edges can then be verified by summing their weights to confirm negativity.[23][22]
If a negative cycle reachable from s is found, the shortest-path distances from s to any vertex on a path intersecting this cycle are undefined, as one can loop indefinitely to reduce the path weight arbitrarily. In such cases, the algorithm terminates by reporting the presence of a negative cycle rather than returning finite distances, ensuring users are aware that no bounded shortest paths exist for affected vertices.[1][21]
Applications
Distance-Vector Routing
The Bellman–Ford algorithm forms the core of distance-vector routing protocols, including the Routing Information Protocol (RIP), introduced in the late 1980s as an interior gateway protocol for managing routing in small to medium-sized networks.[24] In these protocols, each router maintains a vector of minimum distances to all known destinations, initially populated with direct link costs and infinite distances elsewhere.[24]
Routers periodically broadcast their entire distance vector to directly connected neighbors using UDP packets, typically every 30 seconds in RIP. Upon receipt, the receiving router applies the Bellman–Ford relaxation principle: for each destination, it computes the potential new distance as the sender's reported distance plus the cost of the link to the sender, adopting this value if it improves the current estimate. This distributed relaxation propagates path information incrementally across the network, enabling routers to build and update their forwarding tables without centralized coordination.[24][25]
Convergence to the optimal shortest-path routing tables requires at most |V|-1 exchange iterations, where |V| represents the number of routers, aligning with RIP's imposition of a 15-hop maximum diameter to bound the network scope and define infinity as 16 hops. While standard implementations use positive integer costs—often simplified to unit hop counts—the algorithm inherently supports negative link weights, which can model policy preferences like administrative penalties or incentives in advanced routing scenarios.[24][26]
Despite these strengths, distance-vector protocols exhibit slow convergence, especially following topology changes like link failures, as updates diffuse gradually through the network. A prominent limitation is the count-to-infinity problem, where two or more routers mutually reinforce outdated high-distance estimates after a destination becomes unreachable, causing metrics to increment indefinitely until reaching the infinity threshold. This is alleviated in part by split horizon, a technique where routers omit routes learned from a neighbor when advertising back to that same neighbor, preventing immediate feedback loops and accelerating detection of failures.[24][27]
Other Graph Problems
The Bellman–Ford algorithm finds application in detecting currency exchange arbitrage opportunities by modeling the foreign exchange market as a directed graph, where nodes represent currencies and edges represent exchange rates with weights defined as the negative logarithm of the exchange rate to transform multiplication into addition. A negative-weight cycle in this graph corresponds to a sequence of exchanges that yields a profit greater than the initial amount, as the total weight around the cycle would be negative, indicating an arbitrage loop. This approach allows the algorithm to identify such cycles through its relaxation process and additional iteration for detection, enabling traders to exploit temporary inconsistencies in exchange rates.[28]
In electrical networks, the Bellman–Ford algorithm can be used to compute optimal restoration paths after faults or outages by representing the network as a graph with nodes as buses or junctions and edges weighted by resistance, reactance, line capacities, or other factors contributing to losses or stability constraints, where negative weights may arise from certain configurations like modeled potentials. The algorithm's ability to handle negative weights makes it suitable for finding shortest paths that minimize energy loss or provide alternative routes during outages or overloads. For instance, in power system networks, it identifies the minimal-cost path from a generation source to loads, considering such constraints translated into edge weights.[29]
The Bellman–Ford algorithm addresses constraint satisfaction problems, particularly systems of difference constraints, by interpreting them as a constraint graph where variables correspond to nodes and constraints of the form x_j - x_i \leq w_{ij} become edges from i to j with weight w_{ij}. Running the algorithm from an added source node yields shortest-path distances that satisfy all constraints if no negative cycle exists, providing a feasible solution to the system. This formulation serves as a relaxation for linear programming problems with such constraints, enabling efficient solving of feasibility or optimization tasks in scheduling, resource allocation, and other domains reducible to difference constraints, with the algorithm's O(VE) time complexity offering a practical bound for moderate-sized instances.[30]
Variants and Improvements
Shortest Path Faster Algorithm (SPFA)
The Shortest Path Faster Algorithm (SPFA) is a variant of the Bellman–Ford algorithm whose origins are unknown but was independently rediscovered and named by Chinese researcher Fanding Duan in 1994 in the Journal of Southwest Jiaotong University.[[31]][[32]] It aims to improve the efficiency of shortest path computations in graphs that may contain negative edge weights by incorporating queue-based optimizations.[33] Unlike the standard Bellman–Ford, which performs fixed iterations over all edges, SPFA dynamically selects vertices for relaxation using a first-in-first-out (FIFO) queue, mimicking aspects of Dijkstra's algorithm while accommodating negatives.[32]
The procedure of SPFA initializes the distance array with infinity for all vertices except the source, which is set to 0, and enqueues the source vertex.[34] A queue data structure and an in-queue flag array (to track vertices currently in the queue) are used to manage processing. While the queue is not empty, the algorithm dequeues a vertex u and iterates over its adjacent vertices v. For each edge (u, v) with weight w, if \text{dist} > \text{dist} + w, then \text{dist} is updated to \text{dist} + w; if v is not already in the queue, it is enqueued.[34] To mitigate potential infinite loops from negative cycles and bound the worst-case behavior, implementations often include a counter for each vertex's enqueue operations, limiting it to at most |V| - 1 times; exceeding this indicates a negative cycle reachable from the source.[34]
In terms of time complexity, SPFA exhibits an empirical average-case performance of O(|E|) on random or sparse graphs, as fewer relaxations are needed compared to the full |V|-1 passes of Bellman–Ford.[35] However, its worst-case time complexity remains O(|V| \times |E|), identical to Bellman–Ford, due to potential repeated enqueuing in adversarial graphs.[33] The space complexity is O(|V|) for the distance array, queue, and flags.[34]
SPFA offers advantages in practical scenarios, particularly for sparse graphs where its queue mechanism avoids unnecessary relaxations, often running significantly faster than the baseline Bellman–Ford—sometimes approaching linear time in edges.[35] It retains the ability to detect negative cycles similarly to Bellman–Ford by tracking excessive updates or enqueues, making it suitable for applications requiring negative weight handling without the overhead of full iterations.[34] Despite these benefits, for graphs with non-negative weights, Dijkstra's algorithm with a priority queue is generally preferred to guarantee better worst-case performance.[36]
Integration with Other Algorithms
The Bellman–Ford algorithm integrates with other shortest path methods to address limitations in handling negative edge weights while improving efficiency for broader problems, such as computing all-pairs shortest paths in directed graphs without negative cycles. A key example is Johnson's algorithm, which leverages Bellman–Ford for initial potential computation to enable faster subsequent runs of Dijkstra's algorithm on reweighted graphs.
In Johnson's algorithm, introduced in 1977, an auxiliary vertex s is added to the original graph G = (V, E) with edges from s to every vertex in V weighted at 0. Bellman–Ford is executed from s to calculate the shortest path distances h(v) = \delta(s, v) for all v \in V, serving as node potentials. These potentials reweight the original edges as w'(u, v) = w(u, v) + h(u) - h(v) for each (u, v) \in E, transforming all weights to non-negative values without altering relative shortest path lengths or introducing negative cycles. If Bellman–Ford detects a negative cycle during this phase, one exists in the original graph.
Following reweighting, Dijkstra's algorithm is run from each vertex in V on the modified graph with weights w', yielding shortest path distances \delta'(u, v) in the reweighted graph. The true original distances are then recovered via \delta(u, v) = \delta'(u, v) + h(v) - h(u). This hybrid approach exploits Bellman–Ford's ability to manage negative weights for preprocessing while harnessing Dijkstra's efficiency on non-negative weights for the bulk of the computation.
The overall time complexity is O(|V| |E|) for the Bellman–Ford step plus O(|V| (|E| + |V| \log |V|)) for the |V| Dijkstra runs (using Fibonacci heaps), commonly expressed as O(|V| |E| + |V|^2 \log |V|) for practical analyses. This makes Johnson's algorithm advantageous for graphs with negative weights, outperforming |V| independent Bellman–Ford runs (which would require O(|V|^2 |E|)) in sparse settings, and scaling well for dense graphs where Dijkstra dominates. It thus provides an effective all-pairs solution where pure Bellman–Ford would be prohibitive.
The potential reweighting technique from Johnson's algorithm, rooted in Bellman–Ford relaxations, extends to other contexts, such as verifying all-pairs results from Floyd–Warshall by checking consistency on subsets or providing heuristic reductions for A* search in environments with negative costs.