← Wiki Index / Computation & Algorithms / Thorup–Zwick Approximate Distance Oracle
💻 Computation & Algorithms

Thorup–Zwick Approximate Distance Oracle

The Thorup–Zwick approximate distance oracle is a randomized graph data structure invented by Mikkel Thorup and Uri Zwick that preprocesses an undirected weighted graph of n vertices into O(k n^(1 + 1/k)) space and answers any pairwise shortest-path distance query in O(k) time within a multiplicative stretch factor of at most 2k − 1.

Thorup–Zwick approximate distance oracle is a landmark randomized graph data structure and preprocessing algorithm in computation and algorithms introduced by Mikkel Thorup and Uri Zwick in 2001 (journal publication 2005 in the Journal of the ACM) for answering all-pairs shortest-path distance queries on an undirected weighted graph G = (V, E, w) with n = |V| vertices and m = |E| non-negatively weighted edges. Given any integer trade-off parameter k ≥ 1, the Thorup–Zwick algorithm preprocesses G in O(k m n1/k) expected time to construct a compact data structure—termed an approximate distance oracle—occupying O(k n1+1/k) words of memory. Subsequent queries for the shortest-path distance δ(u, v) between any pair of vertices u, v ∈ V are answered in worst-case O(k) time (after O(1)-time hash lookups) by returning an upper-bound estimate δ̂(u, v) whose multiplicative stretch never exceeds 2k − 1:

δ(u, v) ≤ δ̂(u, v) ≤ (2k − 1) · δ(u, v)

Moreover, conditioned on the 1963 Erdős girth conjecture from extremal graph theory, both the O(n1+1/k) space complexity and the 2k − 1 stretch bound of the Thorup–Zwick oracle are simultaneously optimal up to logarithmic or k-dependent constant factors for every integer k ≥ 1.

The All-Pairs Distance Storage Bottleneck

Full Distance Matrices vs. On-Demand Shortest-Path Search

In network routing, geographic information systems, computational biology, and social network analysis, applications routinely query the shortest-path distance δ(u, v) between arbitrary pairs of vertices in a large sparse graph where m = O(n). Prior to Thorup and Zwick's work, algorithm designers faced a trade-off between two classical regimes:

  1. Full All-Pairs Precomputation (O(n2) Space, O(1) Query Time): Running the Floyd–Warshall algorithm in O(n3) time, Johnson's algorithm in O(n m + n2 log n) time, or n invocations of Thorup's shortest-paths algorithm in O(n m) time produces the n × n distance matrix D[u, v] = δ(u, v). While any distance query is then answered in O(1) array lookup time, storing Θ(n2) entries is infeasible when n ≥ 106.
  2. Zero Precomputation (O(m + n) Space, Ω(m) Query Time): Storing only the adjacency list of G requires O(m + n) linear space, but answering a single point-to-point distance query (u, v) via Dijkstra's algorithm takes O(m + n log n) time.

A central question in graph algorithms throughout the 1990s (studied by Awerbuch, Berger, Cowen, and Peleg via sparse graph spanners and routing labeling schemes) was whether a data structure could store subquadratic space o(n2) while answering arbitrary pairwise distance queries in constant O(1) time within a small constant multiplicative approximation factor.

Why Graph Spanners Alone Do Not Give Constant Query Time

A t-spanner of a graph G = (V, E) is a sparse subgraph H = (V, E′) with E′ ⊆ E such that for all u, v ∈ V, the shortest-path distance in H satisfies δH(u, v) ≤ t · δG(u, v). Althöfer et al. (1993) proved that every weighted graph admits a (2k − 1)-spanner with O(n1+1/k) edges. However, compressing the graph into a sparse spanner H only reduces the number of edges; querying the distance between u and v inside H still requires running Dijkstra's algorithm across H in Ω(n1+1/k) time. Thorup and Zwick coined the term approximate distance oracle for a preprocessed data structure that achieves the same O(k n1+1/k) subquadratic memory footprint as a (2k − 1)-spanner while reducing the query time from Ω(n1+1/k) down to O(k) = O(1).

Construction of the Thorup–Zwick Hierarchy

Randomized Vertex Sampling Hierarchy

Let G = (V, E, w) be an undirected graph with non-negative edge weights w(e) ≥ 0, and let k ≥ 1 be a fixed integer parameter governing the space–stretch trade-off. The preprocessing phase constructs a k-level hierarchy of decreasing vertex subsets (landmark layers):

V = A0 ⊇ A1 ⊇ A2 ⊇ … ⊇ Ak−1 ⊇ Ak = ∅

Starting from A0 = V, for each level i = 1, 2, …, k − 1, the subset Ai is formed by sampling each vertex of Ai−1 independently at random with probability p = n−1/k, while the top sentinel layer is fixed to Ak = ∅. Because each vertex of V survives i independent sampling rounds with probability pi = n−i/k, the expected cardinality of level i is:

E[ |Ai| ] = n · (n−1/k)i = n1 − i/k for i = 0, 1, …, k − 1

In particular, the highest non-empty landmark layer Ak−1 has expected size n1/k.

Pivots, Distances to Levels, and Vertex Bunches

For every vertex v ∈ V and every level i ∈ {0, 1, …, k − 1}, let δ(Ai, v) = minw ∈ Ai δ(w, v) denote the shortest-path distance from v to the nearest landmark vertex in Ai. The level-i pivot (or witness) of v, denoted pi(v) ∈ Ai, is a vertex in Ai that achieves this minimum distance, so δ(pi(v), v) = δ(Ai, v). For the empty top level Ak = ∅, we define δ(Ak, v) = +∞ and pk(v) = null. Because A0 ⊇ A1 ⊇ … ⊇ Ak, the distances from v to successive landmark levels are monotonically non-decreasing:

0 = δ(A0, v) ≤ δ(A1, v) ≤ … ≤ δ(Ak−1, v) < δ(Ak, v) = +∞

Using these level thresholds, the level-i bunch of vertex v, denoted Bi(v), is defined as the set of all landmarks in Ai \ Ai+1 that are closer to v than the nearest landmark in the next higher level Ai+1:

Bi(v) = { w ∈ Ai \ Ai+1 : δ(w, v) < δ(Ai+1, v) }

The total bunch B(v) of vertex v is the disjoint union of its bunches across all k levels: B(v) = ⋃i=0k−1 Bi(v). For the top non-empty level i = k − 1, since Ak = ∅ and δ(Ak, v) = +∞, the top bunch is the entire top layer: Bk−1(v) = Ak−1.

Expected Bunch Size and Total Oracle Space Analysis

The key probabilistic insight of Thorup and Zwick is that for every vertex v ∈ V and every level i ∈ {0, 1, …, k − 2}, the expected size of Bi(v) is bounded by n1/k.

Proof via Geometric Coin Flips: Fix a vertex v ∈ V and condition on the subset Ai. Sort all vertices of Ai in non-decreasing order of their distance from v: w1, w2, …, w|Ai|, so δ(w1, v) ≤ δ(w2, v) ≤ …. Each vertex wr ∈ Ai is promoted into Ai+1 independently with probability p = n−1/k. A vertex wr can belong to Bi(v) only if none of the r closest vertices w1, …, wr was sampled into Ai+1 (since the first wj promoted to Ai+1 sets the cutoff threshold δ(Ai+1, v) = δ(wj, v), excluding wj and all farther vertices). Therefore, the number of vertices in Bi(v) is stochastically dominated by the number of failures before the first success in a sequence of independent Bernoulli trials with success probability p = n−1/k:

E[ |Bi(v)| ] ≤ (1 − p) / p < 1 / p = n1/k

For the top level i = k − 1, E[|Bk−1(v)|] = E[|Ak−1|] = n1/k. Summing over all k levels i = 0, 1, …, k − 1 by linearity of expectation gives E[|B(v)|] ≤ k · n1/k.

The Thorup–Zwick oracle stores, for every vertex v ∈ V:

  1. Its k pivots p0(v), p1(v), …, pk−1(v) and their distances δ(pi(v), v), and
  2. A 2-level perfect hash table (Fredman, Komlós, and Szemerédi, 1984) representing the bunch B(v), mapping each vertex w ∈ B(v) to δ(w, v) in O(1) worst-case lookup time and O(|B(v)|) space.

Summing over all n vertices v ∈ V yields a total expected space of O(k n1+1/k) words. By defining dual clusters C(w) = {v ∈ V : w ∈ B(v)} for each landmark w and growing modified Dijkstra trees out of each w ∈ Ai \ Ai+1 that prune any branch as soon as the tentative distance from w to v reaches the threshold δ(Ai+1, v), the preprocessing stage visits each vertex's adjacency list O(k n1/k) expected times, running in O(k m n1/k) expected time.

Query Algorithm and Proof of the (2k − 1) Stretch Bound

The O(k)-Step Alternating Pivot Search

Given any two query vertices u, v ∈ V, the Thorup–Zwick query algorithm finds an intermediate landmark w that belongs to both bunches simultaneously (w ∈ B(u) ∩ B(v)) by testing successive pivots while alternating the roles of u and v:

  1. Initialize w ← u and level index i ← 0.
  2. While w ∉ B(v) (tested in O(1) worst-case time using the hash table for B(v)):
    • Increment i ← i + 1, swap (u, v) ← (v, u), and advance w ← pi(u).
  3. Return the triangle-inequality distance estimate δ̂(u, v) = δ(w, u) + δ(w, v).

Here δ(w, u) = δ(pi(u), u) is retrieved from u's pivot array, and δ(w, v) is retrieved in O(1) time from the hash table of B(v). Because Bk−1(v) = Ak−1 for every vertex v and pk−1(u) ∈ Ak−1, the loop always terminates at or before iteration i = k − 1. Thus, the query performs at most k − 1 hash table lookups and executes in worst-case O(k) time.

Inductive Proof of Stretch at Most 2k − 1

First, because w is a vertex in G and δ(w, u) and δ(w, v) are true shortest-path distances in G, the triangle inequality on undirected graphs ensures that the returned estimate never underestimates the true distance: δ(u, v) ≤ δ(w, u) + δ(w, v) = δ̂(u, v).

Second, to prove the upper bound δ̂(u, v) ≤ (2k − 1) δ(u, v), trace the growth of δ(w, u) across the failed iterations j = 0, 1, …, i − 1 before the loop terminates at step i ≤ k − 1:

  • Base Case (j = 0): At the start of step 0, w = p0(u) = u, so δ(p0(u), u) = 0 = 0 · δ(u, v).
  • Inductive Step: Suppose at the start of iteration j (for some 0 ≤ j < i), we have δ(w, u) = δ(pj(u), u) ≤ j · δ(u, v), and the membership test w ∈ B(v) fails (w ∉ B(v)). Since w = pj(u) ∈ Aj, the only way w can fail to belong to B(v) is if v's distance to the next higher landmark level Aj+1 is no larger than v's distance to w:
δ(Aj+1, v) = δ(pj+1(v), v) ≤ δ(w, v)

By the triangle inequality in the undirected graph G, combined with the induction hypothesis δ(w, u) ≤ j · δ(u, v):

δ(pj+1(v), v) ≤ δ(w, v) ≤ δ(w, u) + δ(u, v) ≤ (j + 1) · δ(u, v)

After swapping (u, v) ← (v, u) and updating w ← pj+1(u), the invariant δ(w, u) ≤ (j + 1) · δ(u, v) holds for step j + 1.

When the loop terminates at step i ≤ k − 1 with w ∈ B(v), we have δ(w, u) ≤ i · δ(u, v) and δ(w, v) ≤ δ(w, u) + δ(u, v) ≤ (i + 1) · δ(u, v). Adding the two distances gives the returned estimate:

δ̂(u, v) = δ(w, u) + δ(w, v) ≤ (2i + 1) · δ(u, v) ≤ (2k − 1) · δ(u, v)

This completes the proof that the multiplicative stretch is at most 2k − 1 for every vertex pair (u, v).

Parameter kMultiplicative Stretch (2k − 1)Oracle Space ComplexityPreprocessing Time (Expected)Worst-Case Query TimeEquivalent Girth Lower Bound Graph
k = 11 (Unstretched distances)O(n2) wordsO(m n)O(1)Complete bipartite graph Kn/2, n/2 (girth 4)
k = 23-stretchO(n3/2) wordsO(m n1/2)O(1)Incidence graph of projective plane (girth 6)
k = 35-stretchO(n4/3) wordsO(m n1/3)O(1)Generalized quadrangle incidence graph (girth 8)
k = 59-stretchO(n6/5) wordsO(m n1/5)O(1)Extremal graph of girth ≥ 12
k = ⌈log2 n⌉O(log n)-stretchO(n log n) wordsO(m log n)O(log n)High-girth expander graphs (girth Ω(log n))

Space Lower Bound via the Erdős Girth Conjecture

Extremal High-Girth Graphs and Information-Theoretic Compression

A notable feature of the Thorup–Zwick paper is its matching space lower bound showing that no data structure of any kind (regardless of preprocessing time or query algorithm) can achieve stretch less than 2k + 1 using o(n1+1/k) bits of memory.

The lower bound connects distance approximation to the girth of a graph (the length of its shortest cycle). In 1963, Paul Erdős conjectured that for every integer k ≥ 1, there exist unweighted graphs on n vertices with girth at least 2k + 2 and Ω(n1+1/k) edges (proven unconditionally for k = 1, 2, 3, 5 via projective planes, generalized quadrangles, and generalized hexagons).

Suppose G = (V, E) is an unweighted graph with girth ≥ 2k + 2 and M = Θ(n1+1/k) edges. Consider the family of 2M subgraphs G′ ⊆ G obtained by independently retaining or deleting each edge e ∈ E. For any edge e = (u, v) ∈ E:

  • If e is present in G′, then the shortest-path distance in G′ is δG′(u, v) = 1.
  • If e is deleted from G′, any path between u and v in G′ would together with e form a cycle in G of length at most δG′(u, v) + 1. Because G has girth at least 2k + 2, any alternative path in G′ must have length δG′(u, v) ≥ 2k + 1.

Consequently, any distance oracle whose multiplicative stretch is less than 2k + 1 (such as 2k − 1) must return δ̂(u, v) ≤ 2k when e ∈ G′ and δ̂(u, v) ≥ 2k + 1 when e ∉ G′. Thus, querying the oracle on all pairs (u, v) ∈ E uniquely reconstructs which of the 2M subgraphs G′ was preprocessed! By the pigeonhole principle of information theory, the oracle data structure must occupy at least M = Ω(n1+1/k) bits of storage.

Refinements, Derandomization, and Constant-Time Queries

Following Thorup and Zwick's 2001/2005 theorem, researchers refined several aspects of approximate distance oracles:

  • Deterministic Construction: Roditty, Thorup, and Zwick (2005) derandomized the landmark sampling hierarchy using the method of conditional expectations and neighborhood sizing, achieving a deterministic O(k m n1/k) preprocessing time with the same O(k n1+1/k) space and 2k − 1 stretch.
  • Reducing Query Time from O(k) to O(1): Wulff-Nilsen (2013) and Chechik (2014, 2015) demonstrated that by storing precomputed witness pointers and applying binary search or constant-step jumping over the monotone distance sequence δ(Ai, v), the query time of a (2k − 1)-stretch distance oracle of size O(n1+1/k) can be reduced from O(k) down to O(1) worst-case time independent of k.
  • Shortest-Path Routing and Spanner Extraction: Beyond returning the scalar distance estimate δ̂(u, v), storing the shortest-path tree parent pointer alongside each entry in B(v) allows the oracle to report the edges of a path of length ≤ (2k − 1) δ(u, v) in O(1) time per edge, and taking the union of the shortest-path tree paths from each v to its bunch B(v) yields a sparse (2k − 1)-spanner with O(k n1+1/k) edges.

Frequently Asked Questions

Why does the Thorup–Zwick oracle fail on directed graphs?

The inductive stretch proof relies on the symmetry of undirected shortest-path distances: δ(u, v) = δ(v, u) and δ(w, v) ≤ δ(w, u) + δ(u, v). In a directed graph, a landmark w that can reach u via a short directed path may have no path to v even when u has a direct edge to v. Thorup and Zwick proved that in directed graphs, no non-trivial subquadratic-space approximate distance oracle with finite multiplicative stretch exists, because an arbitrary n/2 × n/2 bipartite reachability matrix can be embedded into a directed graph.

How do I choose the parameter k in practice?

Choosing k = 2 gives stretch at most 3 with O(n1.5) space and 2 hash lookups per query; k = 3 gives stretch at most 5 with O(n1.33) space and 3 lookups; and k = ⌈log2 n⌉ yields O(n log n) space with O(log n) stretch. In road-network and social-graph benchmarks, the average observed stretch is typically between 1.02 and 1.15—far below the worst-case 2k − 1 bound.

Can the oracle return the approximate shortest path, not just its length?

Yes. During preprocessing, when building the pruned Dijkstra tree rooted at each landmark w ∈ Ai \ Ai+1, each vertex x in C(w) records its parent edge toward w. Once the query finds the common landmark w ∈ B(u) ∩ B(v), following parent pointers from u to w and from v to w outputs a path of weight δ̂(u, v) ≤ (2k − 1) δ(u, v) in O(1) time per edge.

References

  1. Thorup, M., & Zwick, U. (2005). Approximate distance oracles. Journal of the ACM, 52(1), 1–24. https://doi.org/10.1145/1044731.1044732
  2. Roditty, L., Thorup, M., & Zwick, U. (2005). Deterministic constructions of approximate distance oracles and spanners. In Automata, Languages and Programming (ICALP 2005), Lecture Notes in Computer Science, Vol. 3580, pp. 261–272. Springer. https://doi.org/10.1007/11523468_22
  3. Chechik, S. (2014). Approximate distance oracles with constant query time. In Proceedings of the 46th Annual ACM Symposium on Theory of Computing (STOC 2014), pp. 654–663. https://doi.org/10.1145/2591796.2591801
  4. Wulff-Nilsen, C. (2013). Approximate distance oracles with improved query time. In Proceedings of the 24th Annual ACM-SIAM Symposium on Discrete Algorithms (SODA 2013), pp. 539–549. https://doi.org/10.1137/1.9781611973105.39
  5. Althöfer, I., Das, G., Dobkin, D., Joseph, D., & Soares, J. (1993). On sparse spanners of weighted graphs. Discrete & Computational Geometry, 9(1), 81–100. https://doi.org/10.1007/BF02189308
🧬 Support Open Science
Explore interactive science games on LowKey Games or browse scientific apparel & accessories in the CRV Science Shop.
💡 Spot an inaccuracy or have primary literature to expand this article?
Submit a citation or revision note using your shared CRV Science / LowKey Games profile ID.
Suggest Revision