Two parameters

One Bellman–Ford buys every Dijkstra

A directed graph of 256 vertices with a third of its arcs negative needs shortest paths between every pair. Running Bellman–Ford from every source costs 25.8 million counted operations on the densest graph drawn; running it once, repricing every arc by what it found, and then running Dijkstra from every source costs 13.1 million, and the one Bellman–Ford is under one per cent of that. Floyd–Warshall's 16.8 million is never the cheapest count on the plate. On the sparsest graphs the repeated Bellman–Ford wins, because its early exit makes nine passes rather than 255.

An estimate is a reweighting showed that A* is not a different algorithm from Dijkstra’s. Give every vertex a potential — the estimate of its distance to the goal — and reprice each arc from uu to vv by its cost plus the potential at vv minus the potential at uu. Along any route the potentials telescope, so every route between two fixed vertices changes cost by the same constant and the shortest route does not move. Dijkstra on the repriced arcs expands the cells A* expands, in the order A* expands them. And the repriced arcs are non-negative exactly when the estimate is consistent, which is exactly Dijkstra’s precondition.

That essay pointed out that the construction does not need the potential to be an estimate. It needs a function whose change across every arc is no more than the arc’s cost. If a graph has negative arcs, Dijkstra’s precondition fails outright — the bound with a precondition showed it returning a wrong answer on four vertices with one negative arc — but a potential that makes every repriced arc non-negative would restore it, for every source at once.

Johnson’s algorithm computes such a potential. This page measures what it costs and what it buys against the two methods it competes with for all-pairs shortest paths on graphs with negative arcs.

The potential that is already a shortest path

The construction is short. Add one new vertex joined to every vertex by an arc of cost zero, and run Bellman–Ford from it. Call the distance it finds to each vertex h(v)h(v). Every h(v)h(v) is at most zero, and for every arc from uu to vv of cost ww,

h(v)h(u)+w,h(v) \le h(u) + w,

because a route to vv through uu is a route to vv, and h(v)h(v) is the shortest. Rearranged, w+h(u)h(v)0w + h(u) - h(v) \ge 0. So repricing each arc by the potential hh makes every arc non-negative, and it is exactly A*'s repricing with h-h in the role of the estimate. The triangle inequality that makes an estimate consistent is here a theorem about shortest paths, and it holds for every arc automatically.

After repricing, Dijkstra runs from each vertex in turn, and each distance it finds is converted back by adding hh at the far end and subtracting it at the near end. One Bellman–Ford and VV Dijkstras, where the alternative is VV Bellman–Fords.

3 searches on one grid: from 1,572 cells expanded down to 1,550One 50 × 50 grid with 28% of its cells removed, every step costing between one and nine, searched from corner to corner. The shortest path costs 356. No estimate: 1,572 expanded · path 356. Straight-line estimate: 1,550 expanded · path 356. Dijkstra on costs reduced by the manhattan estimate: 1,550 expanded · path 356 · 0 arcs priced below zero.cells expandedNo estimate1,572 expanded · path 356Straight-line estimate1,550 expanded · path 356Reduced, manhattan estimate1,550 expanded · path 356 · 0 arcs priced below zeroV = 2,500, steps cost one to nine, seed 20260910shortest path 356
Fig. 1 The same identity on a search grid, where it was first measured: one 50 by 50 weighted grid searched from corner to corner, shortest path 356. Dijkstra expands 1,572 cells. A* with the straight-line estimate expands 1,550. Dijkstra on arcs repriced by that estimate expands the same 1,550, and prices no arc below zero.

The plate is the reweighting with a supplied potential, and the two searches it compares are one search. Johnson’s algorithm is the same construction on a graph where the potential has to be found first, because no one can write down a consistent estimate for a graph whose arcs are negative.

The graphs, and what is counted

The graphs are directed, with 256 vertices, and each vertex draws a stated number of out-arcs to vertices chosen at random. Each arc costs a whole number from one to nine plus p(u)p(v)p(u) - p(v) for a random hidden potential pp between 0 and 39. That second term makes a third or more of the arcs negative — 189 of 507 on the sparsest graph, 15,173 of 41,088 on the densest — while every cycle keeps its original cost, since the hidden potentials telescope around it. So the graphs have many negative arcs and no negative cycle, by construction rather than by luck.

Three methods compute every distance:

  • Bellman–Ford from every source, each run with the early exit that stops when a pass over the arcs changes nothing;
  • Johnson’s reweighting: one Bellman–Ford from the added vertex, then Dijkstra with a binary heap from every source on the repriced arcs;
  • Floyd–Warshall: for every intermediate vertex, for every pair, keep the smaller of the direct distance and the distance through that vertex.

The count is the same unit throughout the field where it applies — arc relaxations tried — plus the comparisons the heap makes, and Floyd–Warshall’s count is its V3V^3 comparisons of a distance with a distance through an intermediate. All three methods’ answers are compared entry by entry, 65,536 of them per graph, and they agree on every graph drawn.

The count against density

All pairs on 256 vertices with negative arcs: Bellman–Ford from every source is cheapest at out-degree 2, Johnson's reweighting from 8, and Floyd–Warshall at noneCounted work — arc relaxations tried plus heap comparisons, and Floyd–Warshall's min operations — for all-pairs shortest paths on directed graphs of 256 vertices whose arcs cost one to nine plus the difference of a random potential, so that a third or more are negative and no cycle is. All three answers agree entry by entry. Out-degree 2 (507 arcs): Bellman–Ford 594,269, Johnson 647,489, Floyd–Warshall 16,777,216. Out-degree 4 (1,009 arcs): Bellman–Ford 1,357,716, Johnson 1,402,193, Floyd–Warshall 16,777,216. Out-degree 8 (2,009 arcs): Bellman–Ford 2,527,435, Johnson 2,081,266, Floyd–Warshall 16,777,216. Out-degree 16 (3,958 arcs): Bellman–Ford 4,793,705, Johnson 2,954,556, Floyd–Warshall 16,777,216. Out-degree 32 (7,666 arcs): Bellman–Ford 7,713,288, Johnson 4,141,186, Floyd–Warshall 16,777,216. Out-degree 64 (14,430 arcs): Bellman–Ford 12,042,527, Johnson 6,041,691, Floyd–Warshall 16,777,216. Out-degree 128 (25,704 arcs): Bellman–Ford 18,962,204, Johnson 9,040,397, Floyd–Warshall 16,777,216. Out-degree 255 (41,088 arcs): Bellman–Ford 25,754,326, Johnson 13,106,436, Floyd–Warshall 16,777,216.24816326412825510⁶10⁷average out-degreecounted workBellman–Ford from every sourceJohnson's reweightingFloyd–Warshall256 vertices, every answer comparedwork: relaxations + heap comparisons
Fig. 2 Counted work for all-pairs shortest paths on directed graphs of 256 vertices with negative arcs, against average out-degree, on logarithmic axes. Out-degree 2 (507 arcs): Bellman–Ford from every source 594,269, Johnson 647,489, Floyd–Warshall 16,777,216. Out-degree 4: 1,357,716 and 1,402,193. Out-degree 8: 2,527,435 and 2,081,266. Out-degree 16: 4,793,705 and 2,954,556. Out-degree 32: 7,713,288 and 4,141,186. Out-degree 64: 12,042,527 and 6,041,691. Out-degree 128: 18,962,204 and 9,040,397. Out-degree 255 (41,088 arcs): 25,754,326 and 13,106,436. Floyd–Warshall is 16,777,216 at every density.

Three lines and two crossings, one of which is the surprise.

Floyd–Warshall is flat and never the cheapest. Its count is V3V^3 whatever the graph, 16.8 million at 256 vertices, because its three loops run over every triple regardless of which arcs exist. Johnson’s reweighting is below it at every density drawn, and repeated Bellman–Ford is below it until an out-degree of 128.

Johnson’s reweighting wins from an out-degree of eight. At 255 out-arcs a vertex it is half the cost of repeated Bellman–Ford, 13.1 million against 25.8 million, and the ratio stays near a half from 64 upwards.

Repeated Bellman–Ford wins on the sparsest graphs. At out-degree two it costs 594,269 against Johnson’s 647,489, and at four, 1,357,716 against 1,402,193. Running the “slow” algorithm 256 times beats running the “fast” one 256 times after a one-off preparation.

Why the early exit wins sparse

The textbook comparison says Bellman–Ford costs VEV \cdot E per source and Dijkstra with a heap ElogVE \log V, so Johnson’s reweighting should win everywhere by a factor of about V/logVV / \log V. The measurement disagrees at the sparse end because the textbook Bellman–Ford is not the one anybody runs.

Bellman–Ford's passes per source fall from 9.1 to 2.9 as the graph fills in, and the reweighting stays under 1% of Johnson's workOn directed graphs of 256 vertices with negative arcs and no negative cycle, the mean number of passes Bellman–Ford makes from each source before a pass changes nothing, and the share of Johnson's counted work spent on its one Bellman–Ford run from the virtual source. Out-degree 2: 189 of 507 arcs negative; 9.12 passes; reweighting 0.63% of Johnson's 647,489. Out-degree 4: 352 of 1,009 arcs negative; 7.04 passes; reweighting 0.38% of Johnson's 1,402,193. Out-degree 8: 736 of 2,009 arcs negative; 5.83 passes; reweighting 0.56% of Johnson's 2,081,266. Out-degree 16: 1,425 of 3,958 arcs negative; 5.41 passes; reweighting 0.58% of Johnson's 2,954,556. Out-degree 32: 2,856 of 7,666 arcs negative; 4.50 passes; reweighting 0.77% of Johnson's 4,141,186. Out-degree 64: 5,331 of 14,430 arcs negative; 3.79 passes; reweighting 0.73% of Johnson's 6,041,691. Out-degree 128: 9,457 of 25,704 arcs negative; 3.39 passes; reweighting 0.86% of Johnson's 9,040,397. Out-degree 255: 15,173 of 41,088 arcs negative; 2.95 passes; reweighting 0.95% of Johnson's 13,106,436.248163264128255110average out-degreepasses per source · reweighting share, %Bellman–Ford passesreweighting, % of Johnson256 vertices, costs 1–9 plus a potentialno negative cycle by construction
Fig. 3 On the same graphs: the mean number of passes Bellman–Ford makes from each source before a pass changes nothing, and the share of Johnson’s counted work spent on its one Bellman–Ford run. Out-degree 2: 9.12 passes, reweighting 0.63% of Johnson’s work. Out-degree 4: 7.04 and 0.38%. Out-degree 8: 5.83 and 0.56%. Out-degree 16: 5.41 and 0.58%. Out-degree 32: 4.50 and 0.77%. Out-degree 64: 3.79 and 0.73%. Out-degree 128: 3.39 and 0.86%. Out-degree 255: 2.95 and 0.95%.

The textbook bound allows 255 passes per source. The measured runs make between three and nine. The bound with a precondition found the same thing on a single source — seven passes of the 2,047 allowed on a graph of 2,048 vertices — and the reason is that Bellman–Ford with an early exit needs only as many passes as the shortest paths have arcs. On a random graph most shortest paths are short in arcs, and the denser the graph the shorter they get: 2.95 passes on the densest graph means most distances are settled within three arcs of the source.

So repeated Bellman–Ford costs about VEPV \cdot E \cdot P, where PP is that small and falling number of passes, and Johnson’s reweighting costs about VEV \cdot E relaxations — one per arc per source — plus the heap’s comparisons. On the sparsest graph the arcs are so few that the heap is most of Johnson’s cost: 544,096 of its 647,489 at out-degree two. Nine passes over 507 arcs from each source is cheaper than a heap that has to order every vertex a source reaches. As the graph fills in, EE grows by a factor of eighty while the heap’s work grows by a factor of about four and a half, and Johnson’s count comes to be dominated by relaxations, one per arc, where Bellman–Ford’s is three per arc.

The counts can be read against that account directly. On the densest graph repeated Bellman–Ford makes 25,754,326 relaxations against VEV \cdot E of 10,518,528, a ratio of 2.45, while its passes average 2.95. The ratio sits below the passes for two reasons, both visible in how the method is written. The first pass relaxes arcs only out of vertices already reached, and on the first pass from a source that set starts at one vertex and grows as the pass proceeds, so the first pass touches only part of the graph. And the last pass is the one that changes nothing — it relaxes every arc and learns only that it can stop. On a graph where distances settle in two passes, the method pays for a third to find that out.

Johnson’s Dijkstra runs have no such final pass. Each one relaxes the arcs out of each vertex exactly once, when that vertex leaves the queue, and stops when the queue is empty, so its relaxations are exactly EE per source. What it pays instead is the heap: each vertex reached is pushed and popped, at a comparison or two per level of the heap on the way in and on the way out. That is about 2,100 comparisons per source on the sparsest graph and about 9,600 on the densest, and on the sparsest graph those comparisons are 84% of Johnson’s whole count — which is exactly why the method that uses no queue at all wins there.

The preparation is the part of Johnson’s algorithm that sounds expensive and is not. Its one Bellman–Ford run is under one per cent of the total at every density drawn. Paying a whole Bellman–Ford once, to make every one of 256 later searches cheaper, is an easy trade when the thing being bought is 256 searches.

Floyd–Warshall, and what the count does not see

Floyd–Warshall losing the count at every density is worth being careful about, because its reputation for dense graphs is not wrong. It is about something this count does not charge.

Its V3V^3 is exact: the three loops always run. Johnson’s count at the densest graph is 10.6 million relaxations and 2.5 million heap comparisons, and the relaxations are exactly one per arc per source plus the preparation’s — 256 times 41,088, plus 124,288. The generator draws 255 random out-arcs per vertex with repeats, which leaves 41,088 distinct arcs of the 65,280 a complete graph would have: 63%. If every arc were present, Johnson’s relaxations alone would be VV(V1)V \cdot V(V-1), close to V3V^3, and with its heap comparisons added it would pass Floyd–Warshall. Extrapolating from the measured heap cost, the crossing falls at about 85% of all possible arcs, a density the plate does not reach.

The other thing the count does not see is where the operations land in memory. Floyd–Warshall’s inner loop compares one row of the distance matrix with another, element by element, in order — a sequential sweep of two arrays, which is the access pattern a cache handles best. Johnson’s algorithm follows adjacency lists and sifts a binary heap, both of which jump across memory. A list and a block of memory measured a factor of 3.6 in modelled cache misses between two layouts of the same traversal, and the count is not the time is the site’s standing warning that a gap in operations can be reordered by a gap in misses. So the counts on this page say Floyd–Warshall is never the cheapest in operations up to 63% density; they do not say it is slower, and on a real machine at high density it may well not be.

The same comparison at twice the size

All pairs on 512 vertices with negative arcs: Bellman–Ford from every source is cheapest at out-degree 2, Johnson's reweighting from 8, and Floyd–Warshall at noneCounted work — arc relaxations tried plus heap comparisons, and Floyd–Warshall's min operations — for all-pairs shortest paths on directed graphs of 512 vertices whose arcs cost one to nine plus the difference of a random potential, so that a third or more are negative and no cycle is. All three answers agree entry by entry. Out-degree 2 (1,022 arcs): Bellman–Ford 2,825,119, Johnson 3,253,208, Floyd–Warshall 134,217,728. Out-degree 8 (4,058 arcs): Bellman–Ford 11,204,018, Johnson 9,106,663, Floyd–Warshall 134,217,728. Out-degree 64 (30,746 arcs): Bellman–Ford 56,204,414, Johnson 26,541,306, Floyd–Warshall 134,217,728. Out-degree 511 (165,535 arcs): Bellman–Ford 209,183,080, Johnson 96,845,871, Floyd–Warshall 134,217,728.286451110⁷10⁸average out-degreecounted workBellman–Ford from every sourceJohnson's reweightingFloyd–Warshall512 vertices, every answer comparedwork: relaxations + heap comparisons
Fig. 4 The comparison at 512 vertices, at four densities. Out-degree 2 (1,022 arcs): Bellman–Ford from every source 2,825,119, Johnson 3,253,208, Floyd–Warshall 134,217,728. Out-degree 8: 11,204,018 and 9,106,663. Out-degree 64: 56,204,414 and 26,541,306. Out-degree 511 (165,535 arcs): 209,183,080 and 96,845,871. Floyd–Warshall is 134,217,728 at every density.

Doubling the vertices keeps both crossings where they were — repeated Bellman–Ford still wins at out-degree two and loses by eight — and makes Floyd–Warshall’s position worse at the sparser densities, since its count grew by a factor of eight while the other two grew by between four and five there; at the densest graph all three grew by between seven and eight. Two parameters, one bound, no order is the reason no single ranking survives: the costs are functions of VV and EE together, and which is cheaper is a question about the graph’s density, not about the algorithms.

Choosing by density, and what the choice depends on

The plates make a rule that can be stated in one line, and it is worth stating along with what it rests on. On these graphs, below an average of about eight out-arcs a vertex, run Bellman–Ford from every source; from eight to well past sixty per cent of all possible arcs, reweight once and run Dijkstra; and only near a complete graph does Floyd–Warshall’s fixed V3V^3 come into contention on the count.

Every boundary in that rule is a property of the graphs rather than of the algorithms. The first boundary depends on how many passes Bellman–Ford needs, which depends on how many arcs shortest paths use, which on a random graph is small and on a road network is not. The second depends on how many arcs exist against how many could. And neither boundary depends on the negative arcs at all: they make Dijkstra unusable without the preparation, but once the preparation is done, the counts are the counts of Dijkstra on a graph with non-negative costs. The negative arcs decide that a preparation is needed; the density decides whether it pays.

That separation is the same one two parameters, one bound, no order insists on for every graph bound: a cost in VV and EE is not ranked against another until a regime is named. The textbook ranking of these three methods — Floyd–Warshall for dense graphs, Johnson for sparse ones, Bellman–Ford for neither — names no regime and is wrong at both ends of the plate.

Where the reweighting sits among the field’s other repairs

A graph with negative arcs has now been handled three ways, and each repair removes a different obstacle.

The precondition that removes the queue handled a graph with no cycles at all: relax the arcs in topological order, once each, and negative costs do not matter. A graph is as hard as its largest cycle handled a graph with cycles by breaking it into strongly connected components, running Bellman–Ford inside each, and relaxing the arcs between components in topological order.

Components first does 4.2× less work than Bellman–Ford's early exit at 64 componentsA directed graph built from strongly connected components, arcs inside a component costing zero to five and arcs between components costing between minus four and five, with V = 1,024 and the number of components swept from 2 to 64. The vertices are relabelled at random, so no ordering of the input helps. Every method returns identical distances. Components first: 34,115, 41,911, 38,549, 28,086, 20,273, 15,104. Bellman–Ford, early exit: 50,085, 80,689, 78,496, 74,398, 47,439, 63,660. Bellman–Ford, every pass: 3,134,373, 3,131,185, 3,132,064, 3,134,110, 3,134,799, 3,135,660.24816326410⁵10⁶components the graph is built fromcounted workcomponents firstBellman–Ford, early exitBellman–Ford, every passV = 1,024, negative arcs between componentsvertices relabelled at random
Fig. 5 That repair, measured: a directed graph of 1,024 vertices built from strongly connected components, with negative arcs between components, as the number of components runs from 2 to 64. Components first costs 34,115, 41,911, 38,549, 28,086, 20,273 and 15,104; Bellman–Ford with its early exit costs 50,085 to 80,689; Bellman–Ford with every pass costs about 3.13 million throughout. At 64 components, components first does 4.2 times less work than the early exit.

The three repairs answer different questions. The topological order and the components-first method are single-source methods that make Bellman–Ford cheaper by giving it less to do. Johnson’s reweighting does not make Bellman–Ford cheaper at all; it runs it once, in full, and uses the result to make negative arcs disappear, so that every later query can use a method that could not have handled them. It is a preparation that turns a precondition from something the graph must satisfy into something the algorithm arranges.

That is the same move the site’s estimate essays made, seen from the other side. An estimate is supplied and makes one query faster; Johnson’s potential is computed and makes every query possible. Both are functions on vertices whose differences across arcs are bounded by the arcs’ costs, and the property that makes one kind of function useful makes the other correct.

What the model leaves out

Graphs with negative cycles. The generator cannot produce one. Johnson’s single Bellman–Ford detects a negative cycle — a pass on the last round still changes something — and the method stops, as it must, since shortest paths are then undefined. Repeated Bellman–Ford would detect it from every source that can reach it, which is more work to learn the same fact.

A heap is not the only queue. The queue decides the class measured three priority queues for Dijkstra and found a factor of 37 between them on one graph. On the sparsest graphs here the heap is most of Johnson’s cost, and a cheaper queue would move the crossing with repeated Bellman–Ford towards sparser graphs still.

Random graphs only. Bellman–Ford’s early exit is cheap because shortest paths on a random graph are short in arcs. On a graph whose shortest paths are long in arcs — a road network, a long chain with shortcuts — the passes per source rise towards the number of vertices, and repeated Bellman–Ford loses everywhere.

Still open: when the queries are not every pair

Johnson’s preparation costs under one per cent of its total because it is divided over 256 sources. A system that answers shortest-path queries one at a time, as they arrive, on a graph with negative arcs faces a different trade: one Bellman–Ford run against however many queries eventually come. If the queries are few, a Bellman–Ford per query is cheaper; if they are many, the one-off reweighting is. And a graph that changes — an arc’s cost updated — can invalidate the potential, so the preparation may have to be repaid.

The measurement that follows prices a stream of single-source queries on these graphs, with and without a stored potential, and finds the number of queries at which the preparation pays for itself — and then updates a stated share of arc costs between queries and asks how often the stored potential stays valid, since an update that keeps every repriced arc non-negative does not require recomputing it.

What this makes readable

Essays that name this one as a prerequisite.

Named alongside this one

Essays reaching for the same objects. Nobody chose these; they are what the concept index makes visible.

What links here

Every essay whose body links to this one.

The objects this essay names

Each one links to every other essay that touches it.

Bellman–FordBinary heapDensityDijkstra's algorithmEarly exitNegative weightPotential functionRelaxationReweightingShortest pathTriangle inequalityTwo parameter bound