Dijkstra’s Algorithm in Depth
The proof, heap choices, universal optimality and breaking the sorting barrier.
Since Fibonacci heaps arrived in 1987, Dijkstra's algorithm in looked like the last word on shortest paths with non-negative weights, because it outputs the vertices sorted by distance and sorting costs . In 2024 it was proved universally optimal for exactly that task. In 2025, an algorithm that refuses to sort beat it on sparse directed graphs.
What you’ll learn
- Prove Dijkstra's algorithm correct with a two-part invariant, and locate the single place where non-negative weights are needed.
- Analyze Dijkstra's algorithm with arrays and with binary, -ary, Fibonacci, bucket and radix heaps, stating each bound with its model.
- Prove the lower bound for ordering vertices by distance, and state the universal optimality theorem of Haeupler, Hladík, Rozhoň, Tarjan and Tětek (2024).
- Explain how Duan, Mao, Mao, Shu and Yin (2025) beat on sparse directed graphs without contradicting that lower bound, and how bidirectional search and preprocessing speed up road-network queries.
The invariant, precisely
The minimum is exact. Let minimize , and let be any path from to . Its first vertex outside is some ; the prefix up to has all earlier vertices in , so by (b) it is at least . The rest of , from to , has non-negative length: this is the only place where is used.
Every path to is at least , and is the length of a real path, so (a) holds once joins :
(b) is restored by relaxing 's edges. A shortest path to with earlier vertices in either avoids , with length , or can be taken to end with the edge : if it went on from to a settled vertex , its prefix up to could be replaced by a shortest path to inside , which is no longer. So
Settling order. After the step, every unsettled estimate is still at least , since relaxing from adds . So the vertices are settled in nondecreasing order of distance: Dijkstra's algorithm also sorts the vertices by distance.
Zero-weight edges are fine, since the proof needs only , and ties may be broken arbitrarily. The last step matters later: an algorithm that settles vertices one at a time in distance order solves the distance order problem, and that problem has a lower bound.
Where exactly does the correctness proof use the fact that all weights are non-negative?
- To bound the number of heap entries by
- To show that the part of a path after it first leaves the settled set has length at least
- To guarantee that every vertex is reachable from
- To make the result independent of how ties are broken
Show the answer
B. To show that the part of a path after it first leaves the settled set has length at least
The minimum is exact because any path to first leaves at some with , and what follows cannot be negative. A negative edge after breaks exactly this step, which is how the counterexamples in the Optimized lesson work.
Heaps: the price of a priority queue
Dijkstra's algorithm makes insertions and extract-mins and up to decrease-keys (or extra insertions with lazy deletion). Its running time is therefore , and the choice of priority queue decides everything:
| Queue | Dijkstra's running time | Notes |
|---|---|---|
| unsorted array | Dijkstra's original 1959 version; best on dense graphs | |
| binary heap | lazy deletion instead of decrease-key keeps the same bound | |
| -ary heap, | Johnson (1975); linear once | |
| Fibonacci heap | amortized decrease-key (Fredman & Tarjan, 1987) | |
| bucket queue, integer weights | Dial (1969) | |
| radix heap, integer weights | ; combined with a Fibonacci heap | Ahuja, Mehlhorn, Orlin & Tarjan (1990) |
| Thorup's hierarchy, undirected graphs, integer weights | Thorup (1999), on the word RAM; it avoids visiting vertices in distance order |
In a -ary heap, sift-up climbs levels, while sift-down compares up to children per level:
So Dijkstra's algorithm costs
Choosing balances the two terms. If , then and the algorithm is linear for fixed :
A graph has vertices and edges. With a -ary heap and , how many levels can a decrease-key climb at most, that is, what is ?
Show the answer
2
, against about levels in a binary heap. Each extract-min now scans up to children on each of levels, but there are only of them: about heap steps in total, against roughly with a binary heap.
In practice, a binary heap with lazy deletion is hard to beat: Fibonacci heaps pay large constant factors for their decrease-key, and on integer weights, bucket and radix structures are popular. The Fibonacci bound matters for theory, where became the benchmark to beat.
The sorting barrier and universal optimality
Take a star: edges of weight for . Then , and the distance order is the sorted order of the weights:
With distinct weights, all orders are possible answers. An algorithm whose tests have two outcomes is a binary decision tree with a leaf for every answer it can give:
By Stirling's formula:
For a star with leaves, at least how many comparisons must a comparison-based algorithm make in the worst case to output the vertices in distance order?
Show the answer
8770
, since . Stirling's first two terms give , and the term supplies the rest. So is optimal for distance ordering on the worst graphs.
The intuition: on a path, each vertex is inserted and extracted before the next one arrives, so the working-set bound charges per vertex, which is linear time, as it must be since the order is forced. On a star, items wait in the heap together and each costs , matching sorting. The theorem says that Dijkstra's algorithm adapts this way on every graph.
Breaking the sorting barrier. Duan, Mao, Mao, Shu and Yin (STOC 2025, best paper) gave a deterministic -time algorithm for single-source shortest paths in directed graphs with real non-negative weights, in the comparison-addition model. It beats whenever , that is, on sparse graphs. Instead of extracting every vertex from a priority queue, it splits the problem recursively and runs a few rounds of Bellman–Ford-style relaxation to single out a small set of "pivot" vertices; only those need to be ordered, so the algorithm never sorts all vertices by distance. For undirected graphs, Duan, Mao, Shu and Yin (FOCS 2023) had already broken the bound with a randomized algorithm. Follow-up work has improved these results further.
Dijkstra's algorithm is universally optimal (2024), yet a 2025 algorithm runs faster on sparse directed graphs. Why is there no contradiction?
- The 2025 algorithm works only on undirected graphs
- The 2025 algorithm is randomized and fast only in expectation
- Universal optimality concerns outputting the vertices in distance order, which needs on some graphs; the 2025 algorithm computes the distances without sorting them
- Universal optimality holds only for integer weights
Show the answer
C. Universal optimality concerns outputting the vertices in distance order, which needs on some graphs; the 2025 algorithm computes the distances without sorting them
The two results solve different problems. Ordering the vertices by distance is as hard as sorting on a star, and Dijkstra's algorithm is optimal for it on every graph. Computing the distances themselves does not require sorting them, and that is the gap the 2025 algorithm exploits.
Road networks: bidirectional search and preprocessing
A navigation query asks for one pair on a graph with tens of millions of vertices. Plain Dijkstra settles every vertex closer to than is, roughly a disk around . Bidirectional Dijkstra grows two smaller disks instead, one forward from and one backward from along reversed edges, and must stop at the right moment.
Suppose a shortest path were shorter than . Every vertex on has , so one of the two terms is below its queue's top, and has been settled by that search:
So every vertex of is settled by one search or the other, and some edge of joins a vertex settled forward to one settled backward (if one search covers all of , its last edge into or first edge out of does). Scanning that edge set
Real routing engines go much further by preprocessing the graph once. ALT (Goldberg & Harrelson, 2005) runs A* with lower bounds derived, through the triangle inequality, from precomputed distances to and from a few landmark vertices. Contraction hierarchies (Geisberger, Sanders, Schultes & Delling, 2008) rank the vertices by importance and contract them one by one, adding a shortcut edge whenever a removed vertex was the only shortest connection between two neighbors; a query then runs a bidirectional Dijkstra that only climbs to more important vertices, and it settles a tiny fraction of the graph. Such techniques answer continental road queries orders of magnitude faster than plain Dijkstra.
Key takeaways
- Invariant: settled distances are exact, and every unsettled estimate is the best path through settled vertices. Non-negativity is used once, to bound the part of a path after it leaves the settled set, and vertices are settled in nondecreasing distance.
- Running time is extract-mins plus decrease-keys: with an array, with a binary heap, with a -ary heap, with a Fibonacci heap; integer weights allow buckets, radix heaps and, for undirected graphs, Thorup's .
- Ordering vertices by distance needs comparisons on a star, and Dijkstra's algorithm with a working-set heap is universally optimal for that task (Haeupler et al., FOCS 2024).
- Distances alone can be computed faster than sorting: deterministically for directed graphs with real non-negative weights (Duan et al., STOC 2025).
- Bidirectional Dijkstra stops when the two smallest keys add up to at least the best path seen; road-network engines add landmarks and contraction hierarchies on top.
Check yourself
In the interactive lesson these are scored with your confidence, and what you miss comes back for review.
Does Dijkstra's algorithm stay correct when some edges have weight exactly ?
- Yes: the proof needs only that every weight is at least
- No: zero-weight edges create ties that break the invariant
- Only if the zero-weight edges form no cycle
- Only with a Fibonacci heap
Show the answer
A. Yes: the proof needs only that every weight is at least
The single use of the weights is "the rest of a path has length ", which zero-weight edges satisfy. Ties may be broken arbitrarily, and a cycle of zero-weight edges is harmless.
Which arity makes Dijkstra's algorithm with a -ary heap run in ?
Show the answer
C.
The cost is : extract-mins at and decrease-keys at . Taking makes and gives , which is linear when .
Why must every comparison-based algorithm that outputs the vertices in order of distance make comparisons on some graphs?
- Because every vertex must be inserted into a heap
- Because on a star whose edges from have arbitrary weights, the distance order is the sorted order of those weights, and sorting needs comparisons
- Because the graph has edges
- Because relaxing an edge costs time
Show the answer
B. Because on a star whose edges from have arbitrary weights, the distance order is the sorted order of those weights, and sorting needs comparisons
On the star, , so the output is a sorted list of the . A decision tree that distinguishes all orders has depth at least , whatever two-outcome tests it uses.
Bidirectional Dijkstra searches forward from and backward from , and keeps , the shortest – path seen so far through an edge between the two searches. Which stopping rule is correct while still stopping early?
- Stop when some vertex has been settled by both searches, and return the sum of its two distances
- Stop when the backward search first reaches
- Stop when the smallest keys of the two queues add up to at least , and return
- Alternate the two searches and stop after steps each
Show the answer
C. Stop when the smallest keys of the two queues add up to at least , and return
Any – path shorter than would consist of vertices settled by one search or the other, joined by an edge between the two settled regions, and scanning that edge would already have made that small. The rule "stop at the first doubly settled vertex" fails: with 4, 4 and 7, both searches settle first, giving 8 instead of 7.
Further reading
- M. L. Fredman & R. E. Tarjan, "Fibonacci heaps and their uses in improved network optimization algorithms," Journal of the ACM 34(3) (1987), 596–615.
- M. Thorup, "Undirected single-source shortest paths with positive integer weights in linear time," Journal of the ACM 46(3) (1999), 362–394.
- B. Haeupler, R. Hladík, V. Rozhoň, R. E. Tarjan & J. Tětek, "Universal Optimality of Dijkstra via Beyond-Worst-Case Heaps," FOCS (2024).
- R. Duan, J. Mao, X. Mao, X. Shu & L. Yin, "Breaking the Sorting Barrier for Directed Single-Source Shortest Paths," STOC (2025).
- R. Geisberger, P. Sanders, D. Schultes & D. Delling, "Contraction Hierarchies: Faster and Simpler Hierarchical Routing in Road Networks," WEA 2008, LNCS 5038, 319–333.