Approximate Distance Oracles
Mikkel Thorup
AT&T Labs – Research
180 Park Avenue
Florham Park, NJ 07932, USA
.com
Uri Zwick
�
School of Computer Science
Tel Aviv University
Tel Aviv 69978, Israel
.ac.il
ABSTRACT
LetG = (V;E) be an undirected weighted graph with jV j = n and
jEj = m. Let k � 1 be an integer. We show that G = (V;E) can
be preprocessed in O(kmn1=k) expected time, constructing a data
structure of size O(kn1+1=k), such that any subsequent distance
query can be answered, approximately, in O(k) time. The approxi-
mate distance returned is of stretch at most 2k�1, i.e., the quotient
obtained by dividing the estimated distance by the actual distance
lies between 1 and 2k � 1. We show that a 1963 girth conjecture
of Erdős, implies that
(n1+1=k) space is needed in the worst case
for any real stretch strictly smaller than 2k+1. The space require-
ment of our algorithm is, therefore, essentially optimal. The most
impressive feature of our data structure is its constant query time,
hence the name “oracle”. Previously, data structures that used only
O(n1+1=k) space had a query time of
(n1=k) and a slightly larger,
non-optimal, stretch. Our algorithms are extremely simple and easy
to implement efficiently. They also provide faster constructions of
sparse spanners of weighted graphs, and improved tree covers and
distance labelings of weighted or unweighted graphs.
1. INTRODUCTION
Consider the following interesting problem which is, perhaps,
the most natural formulation of the classical all-pairs shortest paths
problem (APSP). We are given a description of a large network,
such as the Internet, or a large road network, such as the US road
network1, with n nodes and m connections. Each connection has
a length, or weight, associated with it. Usually m � n2. We are
to preprocess the network, so that subsequent distance queries or
shortest path queries could be answered quickly, on-line.
This formulation seems to capture more accurately the real na-
ture of the all-pairs shortest paths problem, as in most applications
we are not really interested in all distances, we just want the ability
to retrieve them quickly, if needed. For example, there are probably
many pairs of addresses in the US whose distance is of interest to
�Work supported in part by the Israel Science Foundation
founded by The Israel Academy of Sciences and Humanities.
1The US road network is a planar network. To get a more interest-
ing non-planar network, assume that the weights attach to the edges
represent travel time, and add flight connections between airports.
Permission to make digital or hard copies of all or part of this work for
personal or classroom use is granted without fee provided that copies are
not made or distributed for profit or commercial advantage and that copies
bear this notice and the full citation on the first page. To copy otherwise, to
republish, to post on servers or to redistribute to lists, requires prior specific
permission and/or a fee.
STOC’01, July 6-8, 2001, Hersonissos, Crete, Greece.
Copyright 2001 ACM 1-58113-349-9/01/0007 …$5.00.
no one. This is precisely the case with “sublinear” algorithms for
static metric space problems. The input to such a problem is often
the shortest paths metric of a graph. A “sublinear” algorithm at-
tempts to solve such problem while querying only some of the dis-
tances. For more details, see Indyk [34] (Note that for the location
problems considered in [34], we now have faster direct solutions
for sparse graphs [53]).
Using an APSP algorithm, we can preprocess the graph in
~O(mn) time, and produce a data structure of size O(n2), an n�n
matrix holding the distances, and perhaps a succinct representation
of shortest paths between all pairs of vertices of the graph. Any
distance query can then be answered in O(1) time.
There are, however, several serious objections to this solution.
First, a preprocessing time of ~O(mn) may be too long. Second,
even if we are willing to wait that long, the n� n matrix produced
may be too large to store efficiently (typically m � n2, and then
this table is much larger than the network itself).
Here, we explore alternative solutions to this problem. We show
that better solutions exist, if the network is undirected, and if we are
willing to settle for approximate distances, instead of exact ones.
The approximate distances produced by our algorithms are of a fi-
nite stretch. An estimate Æ̂(u; v) to the distance Æ(u; v) from u
to v is said to be of stretch t if and only if Æ(u; v) � Æ̂(u; v) �
t � Æ(u; v). Stretched distances may be acceptable under some sce-
narios, while unacceptable in others. Many recent algorithms deal-
ing with finite metric spaces produce only approximate answers,
even if exact distances are used. In particular, this is the case with
the above mentioned sublinear metric space algorithms of Indyk
[34]. Adapting these algorithms to exploit our approximate dis-
tance oracles is therefore a straightforward task.
As stated in the abstract, we describe, for any integer k � 1, a
preprocessing algorithm that runs in O(kmn1=k) time, producing
a data structure of size O(kn1+1=k). Note that the preprocessing
time is almost linear in the size of the network, if k is a large con-
stant, while the size of the data structure produced is almost linear
in the number of nodes. In particular, for dense enough graphs, the
data structure produced is much more compact than the network
itself. Subsequent queries can then be answered, approximately, in
O(k) time. i.e., constant time. The stretch of the approximations
returned is at most 2k � 1. More formally:
THEOREM 1.1. Let G = (V;E) be a weighted undirected
graph with non-negative edge weights with jV j = n, jEj = m.
Let k � 1 be an integer. Then, the graph G can be preprocessed
in O(kmn1=k) expected time, producing a data structure of size
O(kn1+1=k), such that subsequent distance queries can be an-
swered, approximately, in O(k) time. The stretch of the produced
estimates is at most 2k � 1. Paths no longer than the estimates
returned can be produced in constant time per edge.
For k = 1, we simply get the APSP solution. When k = 2,
we get a preprocessing time of O(mn1=2), space O(n3=2), query
time O(1), and stretch at most 3. When k = blog nc, we get a
preprocessing time of O(m log n), space O(n log n), query time
O(log n), and stretch O(log n). Higher values of k do not improve
the space or preprocessing time.
The most interesting feature of our algorithms, we believe, is the
fact that for every fixed k we get a constant query time, hence the
name distance oracles.
The space requirements of our oracles are essentially optimal.
We show that a 1963 girth conjecture of Erdős, and others, im-
plies that
(n1+1=k) bits of storage are needed, in the worst case,
by any oracle, however slow, that gives estimated distances with
stretch strictly less than 2k + 1. This girth conjecture is known to
hold for k = 1; 2; 3; 5. Thus, in particular, any oracle giving stretch
2:99 answers must use, on some graphs, at least
(n2) bits of stor-
age, and any oracle giving stretch 4:99 answers must use, on some
graphs, at least
(n3=2) bits of storage, almost the same amount of
storage used by our stretch 3 oracle.
The oracle model of the shortest paths problem was considered
before, at least implicitly, by Awerbuch et al. [7], Cohen [18], and
by Dor et al. [25]. (See discussion in the next section.) Our re-
sults significantly improve, however, the previously available re-
sults. Most strikingly, using slightly less space, we reduce the query
time from ~O(kn1=k) to O(k), while giving, at the same time, more
accurate distance estimates.
Theorem 1.1 is proved in Section 4. Before that, in Section 3, we
present a simplified version of our algorithm for the the special case
where the input is the complete distance matrix of a finite metric
space. This version of the algorithm is faster (O(n2) time) and
particularly suited for external memory implementation.
As a byproduct of our oracle construction for graphs, we also
get faster algorithms for constructing sparse spanners and compact
tree covers of weighted graphs (see Section 4.4), and near-optimal
distance labelings of graphs (see Section 3.5).
As mentioned in the abstract, all our algorithms are extremely
simple and easy to implement efficiently.
The rest of the paper is organized as follows. In the next section,
we compare our results with previously available results. In Sec-
tion 3 we construct approximate distance oracles for finite metric
spaces. The input in this setting is an n� n matrix giving the dis-
tance between any two points in the space. In Section 4 we adapt
this construction to work on the shortest paths metric of a given
input graph. The input this time is the graph, and not an explicit
representation of all the distances in it. Breaking the description
of our distance oracles in this way allows us to separate the metric
aspects of our constructions from the algorithmic graph techniques
needed for efficient implementation. In Section 5 we describe al-
most matching lower bounds on the space requirements of approx-
imate distance oracles. We also show that essentially no non-trivial
distance oracles are possible for directed graphs. We end, in Sec-
tion 6, with some concluding remarks and open problems.
2. PREVIOUS RESULTS
A summary of previously obtained algorithms for computing
exact or approximate distances in general weighted undirected
graphs, cast in our framework, is given in Table 1.
In more detail, the fastest solution for APSP for directed and
undirected weighted graphs with non-negative weights from an ar-
bitrary (comparison based) domain is to run a single-source shortest
paths (SSSP) algorithm from each node. This takesO(m+n log n)
time using the classical algorithm of Dijkstra [23], implemented
using Fibonacci heaps [31] (see also Cormen et al. [20, Chapter
21]). In this paper, we are only interested in undirected graphs, and
then an improved running time of O(m) can be obtained when the
weights are integer [49] (or floating point [51]). Consequently, the
time bound for APSP is O(mn). The O(m) time bound for SSSP
has been incorporated in the other time bounds below, so an ~O(�)
time bound indicates the presence of logarithmic factors not stem-
ming from Dijkstra’s algorithm.
For completeness, we note that improved time bounds may be
obtained if not all edges are part of shortest paths [35, 43], or if the
graph is dense and all weights are small integers [59, 48].
Zwick [59] has shown that if a stretch of 1 + �, for some fixed
� > 0, is allowed, then APSP can be solved in ~O(n!) time, where
! < 2:376 is the exponent of matrix multiplication. For stretches
2, 7=3, and 3, Cohen and Zwick [19] have shown that APSP can be
solved in time ~O(m1=2n3=2), ~O(n7=3), and ~O(n2), respectively.
The above ~O(n2) time bound is clearly near-optimal, if we insist
on producing a complete table of distances. It is interesting to note,
however, that stretch 3 is also the smallest stretch for which one
can hope to produce distance oracles that use o(n2) space for all
graphs. Indeed, Dor et al. [25] describe a stretch 3 oracle that
uses only ~O(m1=3n+n2=m1=3) space, which is always ~O(n5=3),
has a preprocessing time of ~O(m2=3n), and O(1) query time. The
preprocessing time of this algorithm is o(n2) if m = o(n3=2). The
ideas of Dor et al. [25] do not extend, however, to larger stretches.
Our new stretch 3 oracle (Theorem 1.1 with k = 2) has a pre-
processing time of O(mn1=2), optimal space O(n3=2), and O(1)
query time. The new preprocessing time is faster than all the other
preprocessing times when m = O(n3=2), in which case it achieves
a preprocessing time of O(n2).
For general stretch, Awerbuch et al. [7] gave, for every integer
k � 1, a stretch 64k oracle with space ~O(kn1+1=k), preprocessing
time ~O(mn1=k), and ~O(kn1=k) query time. Cohen [18] signifi-
cantly improved this result, reducing the stretch to 2k + � while
leaving the other parameters unchanged. In this paper, we reduce
the stretch a bit further to 2k�1, which is probably optimal for the
amount of space used, and much more importantly, dramatically
reduce the query time from ~O(kn1=k) to O(k).
Distance oracles are closely related to spanners. A t-spanner of
a weighted undirected graph G is a subgraph H of G such that the
distances in H are stretch t estimates of the distances in G (see Pe-
leg and Schäffer [46]). Clearly, a stretch t oracle, like ours, capable
of producing paths witnessing the estimated distances, must explic-
itly or implicitly contain a t-spanner. Hence, t-spanners provide
a clean mathematical view of compact distance oracles. Indeed,
all of the above mentioned results providing o(n2) space bounds
[7],[18],[25] can be viewed as producing spanners.
The sizes of spanners are closely related to the girth of a graph,
which is the size of its smallest simple cycle. Clearly, the girth
of a graph is at least t + 2 if and only if no proper subgraph of
it is a t-spanner. A classical result in extremal graph theory (see
discussion references in Section 5) states that an n-vertex graph
with at least n1+1=k edges is of girth at most 2k. As pointed out
by Althöfer et al. [5], this implies that every weighted undirected
graph on n vertices has a (2k�1)-spanner with O(n1+1=k) edges.
Such a spanner can be constructed using an algorithm similar to
Kruskal’s algorithm (see [36] or Cormen et al. [20, Chapter 24]) for
constructing minimum spanning trees: Building the spanner from
scratch, consider the edges of the graph in a non-decreasing order
of weight, adding each edge to the spanner if its endpoints are not
already connected, in the spanner, by a path using at most 2k � 1
edges. At any stage, the spanner is a (2k� 1)-spanner of the edges
already considered, and its unweighted girth is at least 2k + 1, so
it has only O(n1+1=k) edges. The fastest implementation of this
Stretch Query time Space Preproc. time Reference Space lower-bound
1 O(1) O(n2) O(mn) [49]
O(m) O(m) 0 [49]
1 + � O(1) O(n2) ~O(n!) [59]
(m) bits
2 O(1) O(n2) ~O(m1=2n3=2) [19]
7=3 O(1) O(n2) ~O(n7=3) [19]
O(1) O(n2) ~O(n2) [19]
3 O(1) ~O(m1=3n+ n2=m1=3) ~O(m2=3n) [25]
(minfm;n3=2g) bits
[47]
O(1) O(n3=2) O(mn1=2) This paper
2k � 1 O(n1+1=k) O(n1+1=k) O(mn1+1=k) [5]
O(k) O(kn1+1=k) O(kmn1=k) This paper
(minfm;n1+1=kg) bits
Conjecture [28]
2k + � ~O(kn1=k) ~O(kn1+1=k) ~O(kmn1=k) [18]
64k ~O(kn1=k) ~O(kn1+1=k) ~O(kmn1=k) [7]
(minfm;n
1+1=32kg) bits
Conjecture [28]
Table 1: All available exact and approximate distance oracles for weighted undirected graphs
algorithm that we are aware of runs in O(mn1+1=k) time. Our pre-
processing algorithm constructs a similar spanner much faster. The
construction time is now O(kmn1=k), instead of O(mn1+1=k).
It is conjectured by many (e.g., Erdős [28], equation 7 on p. 33,
Bondy and Simonovits [12], remark 1 on p. 98 and Bollobás [11],
item 13 on p. 164), that there are n-vertex graphs with
(n1+1=k)
edges that are of girth 2k + 2. This conjecture is proved for
k = 1; 2; 3; 5 (see Section 5). Since these graphs have no proper t-
spanners, for t < 2k+1, the conjecture would imply that the above
mentioned upper bounds are best possible. We show in Section 5
that the conjecture also implies that
(n1+1=k) bits are needed, in
the worst case, by any oracle giving estimates of stretch smaller
than 2k + 1, even if it not required to construct appropriate paths.
Some distance oracles were constructed for special classes of
graphs. Efficient distance oracles for graphs of small treewidth
were obtained by Chaudhuri and Zaroliagis [16]. Exact and ap-
proximate distance oracles for planar and Euclidean graphs were
considered by Arikati et al. [6], Chiang and Mitchell [17], Djidjev
[24], and recently by Thorup [52].
Finally, we mention that there has been some work on approx-
imating distances, in unweighted undirected graphs, with additive
rather than multiplicative errors (see Aingwoth et al. [1] and Dor
et al. [25]), and very recently, on approximating distances with
multiplicative and additive errors (see Elkin and Peleg [27] and
Elkin [26]). There has also been work on low distortion embed-
dings of general metric spaces into some low-dimensional metrics
(see Bourgain [13], Linial at al. [40] and Bartal [9]), but these em-
beddings lead to stretches of at least
(log n), as compared with
our 2k � 1.
Techniques. Our construction technique is most closely related
to the techniques employed by Awerbuch et al. [7] and Cohen [18].
A common feature of these previously used techniques is the con-
struction of a family of balls with the property that each vertex
is contained in at most of ~O(kn1=k) balls. The returned distance
between two vertices is then the smallest diameter of a ball contain-
ing them both. To find this ball, they inspect each of the ~O(kn1=k)
balls containing the first vertex, and check, in constant time per
ball, whether it also contains the second. Though conceptually sim-
ple, the use of balls leads to several technical complications. One
of them, for example, is an added logarithmic factor paid for the
construction of balls with exponentially increasing diameters. The
main drawback of this approach, however, is the lack of a quick
way of finding the smallest diameter ball containing two given ver-
tices.
In our construction, we relax the rigid notion of balls with limited
diameter, and use instead collections of induced trees that form a
tree cover of the graph. Each vertex is contained in only in a small
number of trees, and for any pair of vertices, there is a tree in the
cover containing a small-stretch path between them. Furthermore,
we can identify the appropriate tree in constant time.
3. APPROXIMATE DISTANCE ORACLES
FOR METRIC SPACES
We begin by presenting approximate distance oracles for general
metric spaces.
THEOREM 3.1. Let (V; Æ) be a finite metric space represented
as an n � n distance matrix. Let k � 1 be an integer. The metric
space (V; Æ) can be preprocessed in O(n2) expected time, produc-
ing a data structure of O(kn1+1=k) size, such that subsequent dis-
tance queries can be answered, approximately, in O(k) time. The
stretch of the produced estimates is at most 2k � 1.
Our preprocessing and query answering algorithms are given, re-
spectively, in Figure 1 and Figure 2. Both are extremely simple
and easy to implement. The algorithms and their implementation
details are discussed in more detail in the next two subsections.
The following two subsections are then devoted to the analysis of
the algorithms, showing that they satisfy the requirements of The-
orem 3.1. In Section 3.5 we show that our approximate distance
oracles also produce, as a byproduct, almost optimal distance la-
bels. In Section 3.6 we show that our randomized preprocessing al-
gorithm may be derandomized with only a small loss of efficiency.
algorithm preprok(V; Æ)
A0 V ; Ak ;
for i 1 to k � 1
let Ai contain each element of Ai�1,
independently, with probability n�1=k .
for every v 2 V
for i 0 to k � 1,
let Æ(Ai; v) minf Æ(w; v) j w 2 Aig,
and pi(v) 2 Ai be nearest to v, i.e. Æ(pi(v); v) = Æ(Ai; v).
Æ(Ak; v) 1
letB(v) [k�1i=0 fw 2 Ai�Ai+1 j Æ(w; v) < Æ(Ai+1; v)g.
Figure 1: Preprocessing a finite metric space
algorithm distk(u; v)
w u ; i 0
while w 62 B(v)
i i+ 1
(u; v) (v; u)
w pi(u)
return Æ(w; u) + Æ(w; v)
Figure 2: Answering a distance query
Finally, in the last two subsections of this section we consider more
practical issues.
3.1 Preprocessing a finite metric space
A description of the preprocessing algorithm preprok(V; Æ) is
given in Figure 1. The missing implementation details are ex-
plained below. The algorithm receives an n�n matrix representing
a finite metric Æ(u; v) on a set V containing n points referred to as
vertices. (In the next section, we consider the case in which the
input to the preprocessing algorithm is not an explicit n�n matrix
that describes the metric on V , but rather a weighted undirected
graph G = (V;E) that induces a shortest paths metric on V .)
The preprocessing algorithm starts by constructing a non-
increasing sequence of sets A0 � A1 � � � � � Ak�1 by a process
of repeated sampling. The sequence begins with A0 = V . Each
set Ai, where 1 � i < k, is then obtained by taking, roughly,
an n�1=k fraction of the elements of Ai�1. More precisely, each
element of Ai�1 is placed in Ai, independently, with probability
n�1=k . Finally, Ak = ;. The expected size of Ai, for 0 � i � k,
is clearly n1�i=k .
In the following, for simplicity, we assume that Ak�1 6= ;. This
is the case with extremely high probability, and if not, we can just
rerun the algorithm. Now, for each vertex v and index i = 0; :::; k�
1, the algorithm computes Æ(Ai; v), the smallest distance from an
element of Ai to v. It also lets pi(v) 2 Ai be nearest possible to v,
that is, Æ(pi(v); v) = Æ(Ai; v). Note that A0 = V , so Æ(A0; v) =
0, and p0(v) = v, for every v 2 V . Next, the algorithm sets
Æ(Ak; v) Æ(;; v) =1, with pk(v) undefined.
Finally, for each vertex v 2 V , the algorithm computes a bunch
p2(v)
v
p1(v)
Figure 3: The construction of the bunch B(v).
B(v) � V as follows. A vertex w 2 Ai�Ai+1 is put in the bunch
B(v) if and only if Æ(w; v) < Æ(Ai+1; v), i.e., ifw is strictly closer
to v than all the vertices of Ai+1. Note that as Æ(Ak; v) =1, we
get that Ak�1 � B(v), for every v 2 V . We show in Section 3.3,
that the expected size of the bunch B(v), for every v 2 V , is at
most kn1=k .
A schematic description of the construction of the bunch B(v),
for some v 2 V , is given in Figure 3. It is assumed there that
k = 3. The black vertices are the vertices of A2, the grey ones are
the vertices of A1�A2, while the white ones are those ofA0�A1.
The bunch B(v) is composed of the the vertices pointed to by an
arrow from v. Also shown in the figure are p1(v), the vertex of A1
closest to v, and p2(v), the vertex of A2 closest to v.
This almost completes the operation of the preprocessing algo-
rithm. For each bunch B(v), the preprocessing algorithm con-
structs a hash table (see Carter and Wegman [15], or [20, Chap-
ter 12]) of size O(jB(v)j) that holds, for every w 2 B(v), the
distance Æ(w; v). Using this hash table it can be checked for every
w 2 V , in expectedO(1) time, whetherw 2 B(v) and if so what is
Æ(w; v). Alternatively, it constructs a 2-level hash table (see Fred-
man, Komlos and Szemeredi [30]), again of size O(jB(v)j), using
which it is possible to check whetherw 2 B(v), and return Æ(w; v)
if so, in O(1) worst case time. For the sake of implementations, we
note here that multiplicative hashing has recently been proved uni-
versal by Dietzfelbinger et al. [21], and Dietzfelbinger and Hüne
[22], and Thorup [50] have found it to be an order of magnitude
faster than the standard universal hashing schemes from text books
using arithmitic modulo a prime number.
The data structure constructed by the preprocessing algorithm
stores for each vertex v 2 V ,
� for 0 � i � k � 1, the witness pi(v) and the corresponding
distance Æ(pi(v); v) = Æ(Ai; v).
� the (2-level) hash table for the bunch B(v), holding Æ(v; w),
for every w 2 B(v).
The total size of the data structure is O(kn +
P
v2V
jB(v)j). In
Section 3.3, we show that the expected size of B(v), for every
v 2 V , is at most kn1=k . The total size of the data structure is
therefore O(kn1+1=k). The time complexity of preprok(V; Æ) is
clearly O(n2).
3.2 Answering a distance query
A description of the very simple query answering algorithm
distk(u; v) is given in Figure 2. It uses only four variables: u and v,
the two vertices whose distance is to be estimated, a third vertex w
and an index i. The algorithm repeatedly swaps u and v. This
clearly does not affect their distance. Initially, w = u = p0(u)
and i = 0. If w 2 B(v), a condition checked by accessing the (2-
level) hash table of B(v), we are done. Otherwise, the algorithm
increments i, swaps u and v, and lets w pi(u) 2 Ai. It con-
tinues in this way until w 2 B(v). This condition is guaranteed
to hold when i = k � 1, if not before, as then w 2 Ak�1 and
Ak�1 � B(v) for every v 2 V .
When w 2 B(v), the algorithm returns Æ(w;u) + Æ(w; v) as
an upper bound on Æ(u; v). The distance Æ(w; u) = Æ(pi(u); u) is
read directly from the data structure constructed during the prepro-
cessing stage. The distance Æ(w; v) = Æ(v; w) is returned by the
(2-level) hash table of B(v) together with the answer to the query
w 2 B(v).
The complexity of distk(u; v) is clearly O(k). The most time
consuming operations are the at most k accesses to the hash tables
to test whether w 2 B(v), returning Æ(w; v) if so. The stretch of
the estimate produced by distk(u; v) is analyzed in Section 3.4.
3.3 Analysis of the preprocessing algorithm
We have shown already, in Section 3.1, that the running time
of the preprocessing algorithm is O(n2) and that the size of the
data structure produced by it is O(kn +
P
v2V
jB(v)j). All that
remains, therefore, is to analyze the expected sizes of the bunches
B(v), for v 2 V .
LEMMA 3.2. For every v 2 V , we have E[jB(v)j] � kn1=k .
PROOF. Let v 2 V . We prove the lemma by showing, for every
0 � i � k�1, that the expected size of B(v)\Ai is at most n
1=k .
For i = k � 1, the statement is trivial as E[jAk�1j] = n
1=k .
Assume, therefore, that i < k � 1.
We show that the expected size of B(v) \ Ai, for i < k � 1,
is stochastically dominated by a geometric random variable with
parameter p = n�1=k. Let w1; w2; : : : ; w` be the elements of Ai,
arranged in any non-decreasing order of distance from v. If wi 2
B(v), then Æ(wi; v) < Æ(Ai+1; v), and thus w1; w2; : : : ; wi�1 62
Ai+1. Note that p = Pr[w 2 Ai+1 j w 2 Ai] = n
�1=k , for
i < k � 1. Thus Pr[wi 2 B(v)] � (1 � p)
i�1 and the expected
size of B(v) \Ai is at most
X̀
i=1
Pr[wi 2 B(v)] �
X̀
i=1
(1� p)
i�1
< p
�1
= n
1=k
:
This completes the proof of the lemma.
As described, the preprocessing algorithm has, therefore, a worst
case running time of O(n2) and the data structure produced has
an expected size of O(kn1+1=k). We can get a data structure of
sizeO(kn1+1=k) by rerunning the algorithm until the data structure
produced is small enough. By Markov’s inequality, the expected
number of repetitions required is constant, so the expected running
time of this version of the algorithm is still O(n2), as stated in
Theorem 3.1.
3.4 Analysis of the query answering algorithm
We next obtain an upper bound of 2k � 1 on the stretch of the
estimated distance returned by distk(u; v).
LEMMA 3.3. distk(u; v) � (2k � 1)Æ(u; v).
PROOF. Clearly, the swapping of u and v does not change their
distance � = Æ(u; v). Before the while loop starts, w = u so
Æ(w; u) = 0. We want to show that each iteration increases Æ(w; u)
by at most �. Since Ak�1 � B(v), there are at most k � 1 iter-
ations, so we will then end up with Æ(w; u) � (k � 1)�. Now,
Æ(w; v) � Æ(w; u) + Æ(u; v) � (k � 1)� + � � k�, so the
estimated distance returned is at most (2k � 1)�.
All that remains, therefore, is to show that Æ(w; u) increases,
in each iteration, by at most � = Æ(u; v). Let ui; vi and wi be
the values of the variables u; v and w assigned with a given value
of i. Then v0 and u0 are the original values of u and v, and then
w0 = u0, so Æ(w0; u0) = 0.
We want to show that Æ(wi; ui) � Æ(wi�1; ui�1)+� if the i-th
iteration passes the test of the while-loop. Then wi�1 62 B(vi�1),
so Æ(wi�1; vi�1) � Æ(Ai; vi�1) = Æ(pi(vi�1); vi�1). How-
ever, vi�1 = ui and wi = pi(ui), so we get Æ(wi; ui) =
Æ(pi(ui); ui) = Æ(pi(vi�1); vi�1) � Æ(wi�1; vi�1) �
Æ(wi�1; ui�1) +�, as required.
This completes the proof of Theorem 3.1.
3.5 Distance labels
Let G = (V;E) be a weighted undirected graph on n-vertices
with integer edge weights. Let � be the diameter of G, and let
k � 1 be an integer. Peleg [45] describes a way of assigning each
vertex v 2 V of the graph G = (V;E) an O(kn1=k log n log �)-
bit label, denoted label(v), such that for any u; v 2 V , a stretch 8k
estimate of the distance Æ(u; v) may be obtained just by looking at
label(u) and label(v). Computing this estimate, in Peleg’s scheme,
may take
(n1=k) time.
We obtain the following improvement to Peleg’s result:
THEOREM 3.4. Let (V; Æ) be a metric space on n points with
integral distances with diameter �. Let 1 � k � log n be
an integer. Then, it is possible to assign to each point v 2 V
an O(n1=k log1�1=k n log(n�))-bit label, denoted label(v), such
given label(u) and label(v), for any two points u; v 2 V , it is pos-
sible to compute, in O(k) time, an approximation to the distance
Æ(u; v) with a stretch of at most 2k � 1.
As would follow from the results of Section 5, this result is es-
sentially optimal. Lower bounds on the size of labels in various
kinds of labeling schemes are also obtained by Gavoille et al. [32].
In our labeling scheme, label(v), for each v 2 V , is composed
of the the witnesses pi(v) and the distances Æ(Ai; v), for 1 � i <
k, as well as the (2-level) hash table that holds, for every w 2
B(v), the distance Æ(w; v). It is easy check that all the information
needed by the query answering algorithm distk(u; v) is contained
in label(u) or in label(v). Thus, a stretch 2k � 1 estimate of the
distance Æ(u; v) may be obtained in O(k) time just by examining
label(u) and label(v).
It follows from Lemma 3.2 that the expected size of label(v), for
any v 2 V , is O(kn1=k) words, where each word holds either a
name of a vertex or a distance. As there are n vertices in the graph,
and as the diameter of the graph is �, each word contains at most
log(n�) bits. We are interested, here, however, in the maximum
size of a label, not its expected size.
It is not difficult to show, using arguments similar to arguments
used below, that with high probability, the size of every bunch
B(v), for v 2 V , is O(n1=k log n). This yields, therefore, a dis-
tance labeling scheme with O(n1=k log n log(n�))-bit labels. A
factor of about log1=k n may be gained by slightly changing the
sampling probability used by the preprocessing algorithm:
s n1=k(lnn+ 1)1�1=k
A0 V ; Ak ;
for i 1 to k � 1
for every v 2 V ,
let Ni(v) contain the s vertices of Ai�1 closest to v.
let Ai be a subset of Ai�1 of size at most
jAi�1j
s
(lnn+ 1)
that hits Ni(v), for every v 2 V .
Figure 4: Deterministic construction of the samples.
LEMMA 3.5. If the sampling probability used by preprok(V; Æ)
is changed from n�1=k to (n= lnn)�1=k , then with high prob-
ability, the size of every bunch B(v), for v 2 V , is
O(n1=k log1�1=k n).
The proof of the lemma would appear in the full version.
3.6 Derandomization
The preprocessing algorithm prepro(V; Æ) given in Section 3.1
is randomized. In it not difficult, however, to derandomize it, with
only a small loss in efficiency. Randomization is only used by
prepro(V; Æ) in the selection of the samples A0 � A1 � � � � �
Ak, and in the construction of the (2-level) hash tables.
A deterministic way of constructing a sequence of samples with
all the desired properties is given in Figure 4. The sets Ai are con-
structed one by one. The set A0 is simply V . Suppose that Ai�1,
for 1 � i < k, was already constructed. The algorithm lets Ni(v),
for every v 2 V , be the set of the n1=k ln1�1=k n vertices of Ai�1
that are closest to v. Ties are broken arbitrarily. Then, the algo-
rithm chooses a set Ai of size at most n
1�i=k(lnn+1)i=k that hits
all the neighborhoods Ni(v), for v 2 V . To construct the set Ai,
the algorithm relies on the following well known lemma, which is
a slight modification of Theorem 2.2 of Alon and Spencer [4, p. 6]:
LEMMA 3.6. Let N1; : : : ; Nn � U be a collection of sets with
jU j = u and jNij � s, for 1 � i � n. Then, a setA of size at most
u
s
(ln ns
u
+1) � u
s
(lnn+1) such that Ni\A 6= ;, for 1 � i � n,
can be found, deterministically, in O(u+
Pn
i=1
jNij) time.
The setA, whose existence is claimed in Lemma 3.6, is obtained
by repeatedly adding to A elements of U that hit as many unhit sets
as possible, until only u
s
sets are unhit. The construction of A is
then completed by adding an element from each one of the unhit
sets. For more details, see Alon and Spencer [4, p. 6]. (Lemma 3.6
is slightly more general than Theorem 2.2 of [4] that assumes u =
n.) We now claim:
THEOREM 3.7. If the random sampling used by preprok(V; Æ)
is replaced by the deterministic sampling procedure described in
Figure 4, then the size of each bunch B(v), for v 2 V , is at most
kn1=k(lnn+ 1)1�1=k .
PROOF. Let v 2 V . Note that B(v) = [k�1i=0 Bi(v), where
Bi(v) = fw 2 Ai � Ai+1 j Æ(w; v) < Æ(Ai+1; v)g, for
1 � i � k. We claim that jBi(v)j � s = n
1=k(lnn + 1)1�1=k ,
for 0 � i < k � 1, otherwise Ni(v) � Bi(v), and by the con-
struction of Ai+1, we have Ai+1 \ Bi(v) 6= ;, a contradiction.
Finally, Bk�1(v) = Ak�1, and it is easy to show by induction that
jAk�1j � n
1=k(lnn+ 1)1�1=k .
We have thus lost only a factor of about log1�1=k n with respect
to the expected bunch size of the randomized algorithm, and only
algorithm preprok(V;E)
A0 V ; Ak ;
for i 1 to k � 1
let Ai contain each element of Ai�1,
independently, with probability n�1=k.
Æ(Ak; v) 1
for i k � 1 downto 0
for every v 2 V ,
compute Æ(Ai; v) and find pi(v) 2 Ai
such that Æ(pi(v); v) = Æ(Ai; v).
if Æ(Ai; v) = Æ(Ai+1; v) then pi(v) pi+1(v) (*)
for every w 2 Ai �Ai+1,
grow a shortest path tree T (w) from w
spanning C(w) = f v 2 V j Æ(w; v) < Æ(Ai+1; v) g.
for every v 2 V ,
let B(v) fw 2 V j v 2 C(w)g.
Figure 5: Preprocessing a graph
a factor of about k with respect to the maximum bunch size of the
randomized algorithm with the slightly modified sampling.
The preprocessing algorithm preprok(V;E) also has to con-
struct a 2-level hash table for each bunch B(v), where v 2 V .
(This step is not explicit in the description of the algorithm given
in Figure 1.) The linear time algorithm given by Fredman et al.
[30] for the construction of such tables is randomized. Their algo-
rithm is derandomized, however, by Alon and Naor [3]. To con-
struct a perfect hash table over q = ~O(n1=k) elements from a
universe of size n, without assuming that k is constant, they use
O(q log q log n) = ~O(n1=k) time. Hence constructing the hash
table for all B(v) takes ~O(n1+1=k) time, so this derandomization
does not affect the overall running time of O(n2) for the prepro-
cessing algorithm.
4. APPROXIMATE DISTANCE ORACLES
FOR GRAPHS
In the previous section, we assumed that metric Æ(u; v) is given
to us explicitly. Here, we consider the more realistic situation in
which the metric that we are supposed to process is the shortest
paths metric of a weighted undirected graph. The graph, and not
the metric, is given to us this time.
We can, of course, begin by solving the APSP problem for the
input graph and then use the algorithms of the previous section to
preprocess the metric obtained. This solution is wasteful, however,
both in terms of running time and in terms of space. It is much
more efficient to directly process the graph that induces the metric.
The new preprocessing algorithm is described next, in Sec-
tion 4.1. A modification to the query answering algorithm that al-
lows it to return paths, and not just approximate distances, is then
described in Section 4.2. The analysis of the modified preprocess-
ing algorithm is given in Section 4.3. Finally, in Section 4.4 we
show that our preprocessing algorithm is also a very efficient algo-
rithm for constructing sparse spanners and compact tree covers.
4.1 Preprocessing a graph
A description of the preprocessing algorithm preprok(V;E) is
given in Figure 5. It receives as input a weighted undirected graph
Figure 6: Constructing the clusters.
G = (V;E). The preprocessing algorithm is similar to the pre-
processing algorithm given in Figure 1. In particular, the sets Ai
and the bunches B(v) would be exactly the same. The implemen-
tation details, this time, are less trivial, as distances Æ(u; v) have
to be computed, instead of just being read from an input matrix.
This is why we introduce the new sets C(�) before computing the
bunches B(�).
The algorithm starts again by constructing the samples A0 �
A1 � � � � � Ak�1 � Ak, where A0 = V and Ak = ;. The
elements of Ai are sometimes referred to as i-centers.
The operation of the algorithm is then composed of k iterations.
The i-th iteration starts by computing the distances Æ(Ai; v), for
every v 2 V , where Æ(Ai; v) = minf Æ(w; v) j w 2 Aig. This is
done by adding to G = (V;E) a new source vertex s, and edges
(s; w) of weight 0, for every w 2 Ai, and by computing the dis-
tances from the new source s to all the other vertices of the graph.
The distances are found in O(m) time by running the single-source
shortest paths algorithm of Thorup [51]. It is easy to check that for
every v 2 V , the distance from s to v in the new graph is indeed
Æ(Ai; v). Furthermore, the shortest paths tree constructed by the al-
gorithm supplies, for every v 2 V , a witness pi(v) 2 Ai such that
Æ(pi(v); v) = Æ(Ai; v). Indeed, if v is in the branch of the short-
est paths tree that starts with the edge (s; w), where w 2 Ai, then
Æ(Ai; v) = Æ(w; v) and we can set pi(v) w. All the witnesses
are easily found, therefore, in O(m) time.
The minimum distance Æ(Ai; v) may be attained by several ver-
tices of Ai. The preprocessing algorithm of the previous section
lets pi(v) be an arbitrary vertex of Ai satisfying Æ(pi(v); v) =
Æ(Ai; v). The new statement (*) trivially preserves this property.
In addition, it also ensures that the following property, that plays a
crucial role in the construction of small-stretch paths, as described
in Section 4.2, also holds:
LEMMA 4.1. For any v 2 V and 0 � i � k � 1, we have
pi(v) 2 B(v).
PROOF. We prove the claim by induction on i from above. The
claim trivially holds when i = k � 1, as then pk�1(v) 2 Ak�1 �
B(v), for every v 2 V . Suppose therefore that i < k � 1, and that
pi+1(v) 2 B(v). If the test of (*) fails, we get pi(v) = pi+1(v) 2
B(v). Otherwise, Æ(pi(v); v) = Æ(Ai; v) < Æ(Ai+1; v), and then
pi(v) 2 B(v) by definition of B(v).
Next, the algorithm constructs a cluster C(w) around each i-
center w 2 Ai � Ai+1. The cluster C(w) is composed of all the
vertices that are closer to w than to any (i � 1)-center. In other
words, C(w) = f v 2 V j Æ(w; v) < Æ(Ai+1; v) g. Note that for
every w 2 Ak�1 we have C(w) = V , as Æ(Ak; v) =1, for every
v 2 V .
It is easy to see that the bunches of the previous section and the
clusters of this section are ‘inverses’ of each other, in that w 2
B(v) if and only if v 2 C(w) for any v; w 2 V . Thus, the bunches
constructed by the final loop of the preprocessing algorithm are
identical to the bunches that would have been constructed by the
preprocessing algorithm of the previous section.
The construction of clusters is reminiscent of the construction
of Voronoi diagrams. An important difference here, however, is
that each i-center w 2 Ai � Ai+1 captures all vertices that are
closer to it than to all the (i+1)-centers, and not to all the i-centers
as the definition of Voronoi diagrams would suggest. In particu-
lar, the clusters at a particular iteration are not necessarily disjoint.
A schematic description of the clustering construction process is
given in Figure 6. The filled vertices there are (i + 1)-centers.
The two large unfilled vertices are i-centers and the two polygons
depict the clusters associated with them. (In Figure 6, it is implic-
itly assumed that the distances between the vertices are Euclidean.
This is done for illustration purposes only. Our algorithms work on
general weighted graphs.)
Each cluster C(w) is computed by running a slightly modified
version of Thorup’s SSSP algorithm from w [49]. Since this algo-
rithm is rather complicated, we describe instead a modified version
of Dijkstra’s classical SSSP algorithm [23] (see also [20, Chapter
25]). The changes to Thorup’s algorithm are very similar.
Modifying Dijkstra’s algorithm. Dijkstra’s algorithm with
source w maintains for each vertex v an upper bound d(v) on
the distance Æ(w; v). If d(v) has not been assigned yet, it is in-
terpreted as infinite. Initially, we just set d(w) = 0, and we
have no visited vertices. At each iteration, we select an unvis-
ited vertex u with the smallest finite d(u), visit it, and relax all
its edges, that is, for each incident edge (u; v) 2 E, we set
d(v) minf d(v); d(u) + `(u; v)g. We continue in this way
until no unvisited vertex v has a finite d(v).
Our simple modification of Dijkstra’s algorithm is that we relax
the edge (u; v) only if d(u) + `(u; v) < Æ(Ai+1; v). Note that
Æ(Ai+1; v) was computed in the previous iteration so the test takes
constant time.
LEMMA 4.2. The modified Dijkstra’s algorithm visits exactly
the vertices of C(w), assigning each the correct distance from w.
PROOF. The proof is similar to correctness proof of Dijkstra’s
original algorithm. Suppose w 2 Ai � Ai+1. The essential new
point is the following easily verified claim: if v 2 C(w) and v0
lies on a shortest path from w to v, then Æ(w; v0) < Æ(Ai+1; v
0),
so v0 2 C(w). By definition, v 2 C(w) if and only if Æ(w; v) <
Æ(Ai+1; v), but then Æ(w; v
0) = Æ(w; v)�Æ(v; v0) < Æ(Ai+1; v)�
Æ(v; v0) � Æ(Ai+1; v
0), as desired.
Note that we only relax an edge (u; v) if d(u) + `(u; v) <
Æ(Ai+1; v). Consequently, a vertex v 62 C(w) is never assigned
a finite distance, and hence it is never visited.
We now show that all vertices visited are assigned correct dis-
tances from w. The proof is by induction. Suppose that v 2
C(w) is about to be visited and that all previously visited ver-
tices were assigned the correct distance. Let p be a shortest path
from w to v. Let u0 be the last visited vertex on this path,
and let v0 be the next vertex on the path. By the above claim,
Æ(w; v0) < Æ(Ai+1; v
0). Moreover, by the induction hypothe-
sis, when u0 was visited d(u0) = Æ(w; u0), and then Æ(w; v0) =
Æ(w; u0)+`(u0; v0) = d(u0)+`(u0; v0). Hence d(u0)+`(u0; v0) <
Æ(Ai+1; v
0), so the edge (u0; v0) was relaxed, setting d(v0)
Æ(u; v0). As v is about to be visited before v0, we must have
d(v) � d(v0) = Æ(w; v0) � Æ(w; v) � d(v), so d(v) = Æ(w; v).
Finally, we want to show that all v 2 C(w) are visited. Suppose
for a contradiction that v 2 C(w) is not visited. Let (u0; v0) be the
last edge on a shortest path to v with u0 visited. From above, we
know that u0 got assigned the correct distance, and the same anal-
ysis as above implies that (u0; v0) got relaxed when u0 was visited,
but then v0 will be visited eventually, contradicting the choice of
(u0; v0).
Thus, the modified version of Dijkstra’s algorithm that we de-
scribed does construct C(w). It is easy to arrange that it would
also produce a shortest path tree T (w) spanning the cluster C(w).
This would not affect the running time of the algorithm.
For a simple, yet relatively efficient, implementation of Dijk-
stra’s algorithm, we can just use William’s heap [57] (see also
[20, Chapter 7]) to store the finite distances d(v) of the unvisited
vertices. We can then both find the v minimizing d(v) and de-
crease some d(v) in O(log n) time. The former is done at most
n � 1 times, and the latter is done at most m times, so the to-
tal running time of the unmodified algorithm, from a given source
vertex, is O((m + n) log n). It is easy to see that in the modi-
fied version of the algorithm, all the edges relaxed are edges that
touch vertices of C(w). Thus, the time spent on the construc-
tion of C(w) is O(jE(C(w))j log n), where E(C(w)) is the set
of edges touching vertices of C(w). The complexity is reduced to
O(jE(C(w))j+ jC(w)j log n) if the more sophisticated Fibonacci
heaps of Fredman and Tarjan [31] (see also [20, Chapter 20]) are
used.
The same conditional relaxation can be applied to Thorup’s
SSSP algorithm [49]. We first spend, once and for all, O(m) time
on constructing a so-called component hierachy. Afterwards, each
cluster C(w), for w 2 V , can be computed in O(jE(C(w))j) time.
When all the clusters C(w), for w 2 V are constructed, they
are used to generate the bunches B(v), for v 2 V . Recall that, by
definition, w 2 B(v) if and only if v 2 C(w). The conversion can
clearly be done in O(
P
w2V
jC(w)j) = O(
P
v2V
jB(v)j) time.
Finally, the algorithm constructs (2-level) hash tables for the
bunches B(v), for v 2 V , and outputs the witnesses pi(v), the
distances Æ(pi(v); v) = Æ(Ai; v), and the hash tables of B(v), for
every 1 � i � k and v 2 V . In addition to that, the preprocess-
ing algorithm also outputs, for every w 2 V , the shortest paths
tree T (w) that spans the cluster C(w).
The sum of the sizes of all the trees T (w), for w 2 W , is the
same as the sum of the sizes of all the clusters, which is also the
sum of the sizes of all the bunches. Thus, the size of the data struc-
ture produced is, asymptotically, the same as the size of the data
structure that would have been produced, had the preprocessing al-
gorithm of the previous section been applied to the shortest paths
metric of the graph. Thus, the expected size of the produced data
structure is O(kn1+1=k). All that remains, therefore, is to analyze
the running time of preprok(V;E). This is done in Section 4.3.
As a final remark, we note that instead of constructing a sepa-
rate hash table for each bunch B(v), for v 2 V , we can construct
a single (2-level) hash table of size O(
P
v2V
jB(v)j) that holds
Æ(w; v), for every w; v 2 V such that w 2 B(v). The access time
would still be O(1).
4.2 Answering a path query
As all the data structures returned by the metric preprocessing
algorithm of Section 3.1 are also returned by the graph preprocess-
ing algorithm of Section 4.1, the query answering algorithm from
Figure 2, detailed in Section 3.2, may be used, without any modifi-
cation, to answer approximate distance queries.
We next describe how to augment the distance query algo-
rithm if it is to return not just an estimated distance distk(u; v)
of stretch at most 2k � 1, but also a path from u to v of length at
most distk(u; v).
When the distance query algorithm terminates, w 2 B(v) so
v 2 C(w). Moreover, by Lemma 4.1, w = pi(u) 2 B(u), so we
also have u 2 C(w). Hence, the path between u and v in T (w), the
shortest paths tree of C(w), is of length at most Æ(w; u)+ Æ(w; v).
To report the edges on this path in constant time per edge, we move
in parallel from u and v towards the root w, stopping as soon as we
reach, from one of u and v, a vertex w0 that was already reached
from the other. (This vertex is the least common ancestor of u and v
in the tree.) We then output the edges on the path from u to w0 and,
in reversed order, the edges on the path from v to w0.
The above solution constructs the small-stretch path from u to v
in amortized constant time per edge. Using techniques from [54]
it is possible to construct the path in worst case constant time per
edge. We do not elaborate on this here.
4.3 Analysis of graph preprocessing
As mentioned, the complexity of constructing the cluster C(w)
isO(jE(C(w))j) (orO((jE(C(w))j+ jC(w)j) log n) if the simple
modification of Dijkstra’s algorithm is used). Recall that E(C(w))
is the set of edges that touch vertices of C(w). Let E(v) be the
set of edges that touch the vertex v. The total cost of construct-
ing all clusters is asymptotically bounded by
P
w2V
jE(C(w))j �P
w2V;v2C(w)
jE(v)j =
P
v2V;w2B(v)
jE(v)j =
P
v2V
(jB(v)j �
jE(v)j). By Lemma 3.2, the expected size of jB(v)j is at most
kn1=k , for any v 2 V , so by linearity of expectation, the expected
total cost is asymptotically bounded by
X
v2V
(n
1=k
jE(v)j) = 2kmn
1=k
:
Since all other operations in preprok(V;E) take onlyO(km) time,
its total complexity is O(kmn1=k).
As in the last section we note that it is only the expected size
of the data structure constructed which is O(kn1+1=k). To ob-
tain a data structure of size O(kn1+1=k), we may have to run
preprok(V; E) several times, but the expected number of repeti-
tions is constant, so the total expected preprocessing time is still
O(kmn1=k), as specified in Theorem 1.1.
4.4 Sparse spanners and tree covers
As described in Section 4.2, the query answering algorithm may
actually find a stretched path between u and v in some tree T (w).
We get, therefore, the following interesting corollary:
COROLLARY 4.3. The collection of shortest paths trees T (w),
for w 2 V , constructed by algorithm preprok(V;E), forms a
(2k � 1)-spanner of the graph G = (V;E). The expected size
of this (2k � 1)-spanner is O(kn1+1=k) and it can be constructed
in O(kmn1=k) time.
As mentioned, the fact that every weighted graph on n-vertices
has a (2k � 1)-spanner with O(n1+1=k) edges is not new. The
corollary gives, however, a much faster algorithm for construct-
ing such spanners. The fastest running time known before, for
weighted graph, was O(mn1+1=k) [5]. For unweighted graphs,
there is linear time algorithm for constructing such spanners (see
also Exercise 3 on page 188 of Peleg [44], attributed to [33]).
Combining Corollary 4.3 with Lemma 3.5 we also get the fol-
lowing corollary:
girth number of edges lower-bound references
4 �(n2) complete bipartite graphs
6 �(n3=2) [47],[29],[14],[56]
8 �(n4=3) [55],[10],[56]
10
(n6=5) ; O(n5=4) [55],[10],[37]
12 �(n6=5) [55],[10],[56],[37]
14
(n9=8) ; O(n7=6) [38],[39]
16
(n10=9) ; O(n8=7) [58],[38]
4r ; r � 5
(n
1+ 1
3(r�1) ) ; O(n
1+ 1
2r�1 ) [38],[39]
4r + 2 ; r � 4
(n
1+ 1
3r�1 ) ; O(n
1+ 1
2r ) [38],[39]
Table 2: Best known bounds on the maximum number of edges
in an n-vertex graph with a given girth.
COROLLARY 4.4. The collection of shortest paths trees T (w),
forw 2 V , constructed by algorithm preprok(V;E), with the sam-
pling probability changed from n�1=k to (n= lnn)�1=k , forms a
tree cover of the graph G = (V;E) with the following proper-
ties: (i) With high probability, every vertex is contained in only
O(n1=k log1�1=k n) trees. (ii) For every two vertices u; v 2 V ,
there is a tree T (w) in this collection that contains a path between
u and v that is of stretch at most 2k � 1. Furthermore, the corre-
sponding tree can be identified in O(k) time.
A deterministic algorithm for constructing such tree covers may
be obtained using the technique of Section 3.6. Our tree cover con-
struction improves a construction implicit in Awerbuch and Peleg
[8] (see also Peleg [44, Chapter 15]). In [54], we use our tree cover
construction, together with other ideas, to obtain routing schemes
for weighted undirected networks that exhibit an essentially opti-
mal tradeoff between the size of the routing tables used and the
stretch of the resulting routes.
5. SPACE LOWER BOUND
A simple argument shows that for any integer k, any graph on n
vertices with at least n1+1=k edges contains a cycle of size at
most 2k. (For a proof that 1
2
n1+1=k edges are in fact enough, see
Alon et al. [2].) This result is conjectured by Erdős [28], Bondy
and Simonovits [12] and Bollobás [11] to be tight. Namely, it is
conjectured that for any k � 1, there are graphs with
(n1+1=k)
edges and girth greater than 2k. As any graph contains a bipar-
tite subgraph with at least half the edges, the conjecture actually
implies the existence of graphs with
(n1+1=k) edges and girth
at least 2k + 2. This conjecture was proved, however, only for
k = 1; 2; 3; 5 (see references below).
Let mg(n) be the maximal number of edges in an n-vertex
graph with girth g. The girth conjecture says that m2k+2(n) =
(n1+1=k). Note, as mentioned above, that m2k+2(n) =
�(m2k+1(n)). The best bounds on mg(n), for even girth g, are
given in Table 2. (Several references are given for each result. This
is either because the result was independently discovered by sev-
eral authors, or because there are several variants of the construc-
tion. Some of the references, e.g., Wenger [56], were added as
they are more accessible than the older references.) The results for
g = 6 follow from constructions of finite projective geometries.
The constructions of Lazebnik et al. [38],[39] slightly improve re-
sults obtained by Margulis [42] and the results obtained using the
Ramanujan graphs of Lubotzky et al. [41].
PROPOSITION 5.1. Let k be an integer, and let t < 2k + 1.
Then, any stretch t distance oracle for graphs with n vertices and
m edges must use at least minfm;m2k+2(n)g bits of storage on
at least one input graph.
PROOF. Let O be a stretch t distance oracle for graphs with n
vertices and m edges. For any graph H of this size, let OH be the
data structure produced by O by preprocessing H . Let OH(v; w)
be the approximate distance returned by the oracle for the query
(v; w). Note that ÆH(v; w) � OH(v; w) � t ÆH(v; w).
Let G be a girth 2k + 2 unweighted graph on n vertices with
m0 = minfm;m2k+2(n)g edges. (If m < m2k+2(n) we can
simply pick an m-edge subgraph of a girth 2k + 2 graph with
m2k+2(n) edges.)
Let H be any subgraph of G. Consider any edge (v; w) of G. If
(v; w) is in H , then OH(v; w) � t < 2k + 1. But, if (v; w) is not
in H , the shortest path from v to w in H has at least 2k + 1 edges,
so OH(v; w) � 2k + 1. Consequently, all the 2
m0 subgraphs of G
have different tables, and hence at least one requires m0 bits.
Proposition 5.1 holds even if the oracle is only required to pro-
duce estimated distances, without being required to produce corre-
sponding paths. We point out, however, that there is still a loga-
rithmic gap of �(k log n) between this lower bound and our upper
bound, even if the girth conjecture holds, as our algorithms use
O(kn1+1=k) words while the lower bound is
(n1+1=k) bits.
Finally, we point out that no space efficient approximate distance
oracles are possible for directed graphs:
PROPOSITION 5.2. For any finite stretch distance oracle for di-
rected graphs must use at least
(n2) bits of storage on at least one
n-vertex graph.
The simple proof would appear in the full version of the paper.
6. CONCLUDING REMARKS
We presented approximate distance oracles with fast preprocess-
ing times, essentially optimal space requirements, and constant
query time. Our construction is extremely simple. It yields, as
byproducts, improved algorithms for constructing sparse spanners,
more compact tree covers, and more concise distance labelings.
Due to their basic nature, we expect our ideas to prove useful in
many other contexts.
Some interesting open questions remain. First, our basic prepro-
cessing algorithm is randomized. While it was easy to derandomize
it when the full distance matrix was available, it is not clear how to
do it in o(mn) time in the graph setting. It seems that new ideas
would be needed to achieve that.
Our oracles are almost optimal, in all respects, when the param-
eter k is large. It remains an interesting open problem, however,
to reduce the preprocessing times of small stretch oracles. The sit-
uation for stretch 3 is especially intriguing. We show here that a
stretch 3 oracle with a space requirement of O(n3=2) can be con-
structed in O(mn1=2) time. Cohen and Zwick [19] have shown
that a stretch 3 oracle that uses O(n2) space can be constructed in
O(n2 log n) time. Could these results be combined, i.e., is it pos-
sible to construct a stretch 3 oracle that uses only O(n3=2) space in
~O(n2) time?
As mentioned in Section 4.4, the results of this paper, com-
bined with some other ingredients, yield essentially optimal rout-
ing schemes for weighted undirected networks. More on this can
be found in [54].
7. ACKNOWLEDGMENTS
We would like to thank Edith Cohen for her results that inspired
this research and for making the cooperation between the authors
possible, Felix Lazebnik for his help in compiling Table 2, and
Michael Elkin for pointing out the connection between distance or-
acles and distance labels.
8. REFERENCES
[1] D. Aingworth, C. Chekuri, P. Indyk, and R. Motwani. Fast estimation
of diameter and shortest paths (without matrix multiplication). SIAM
J. Computing, 28:1167–1181, 1999.
[2] N. Alon, S. Hoory, and N. Linial. The Moore bound for irregular
graphs. Submitted for publication, 2000.
[3] N. Alon and M. Naor. Derandomization, witnesses for boolean
matrix multiplication, and construction of perfect hash functions.
Algorithmica, 16:434–449, 1996.
[4] N. Alon and J. Spencer. The probabilistic method. Wiley, 1992.
[5] I. Althöfer, G. Das, D. Dobkin, D. Joseph, and J. Soares. On sparse
spanners of weighted graphs. Discrete & Computational Geometry,
9:81–100, 1993.
[6] S. Arikati, D. Chen, L. Chew, G. Das, M. Smid, and C. Zaroliagis.
Planar spanners and approximate shortest path queries among
obstacles in the plane. In Proc. 4th ESA, pages 514–528, 1996.
[7] B. Awerbuch, B. Berger, L. Cowen, and D. Peleg. Near-linear time
construction of sparse neighborhood covers. SIAM J. Computing,
28:263–277, 1999.
[8] B. Awerbuch and D. Peleg. Routing with polynomial
communication-space trade-off. SIAM J. Discrete Mathematics,
5(2):151–162, 1992.
[9] Y. Bartal. On approximating arbitrary metrices by tree metrics. In
Proc. 30th STOC, pages 161–168, 1999.
[10] C. Benson. Minimal regular graphs of girth eight and twelve.
Canadian Journal of Mathematics, 18:1091–1094, 1966.
[11] B. Bollobás. Extremal graph theory. Academic Press, 1978.
[12] J. Bondy and M. Simonovits. Cycles of even length in graphs.
Journal of Combinatorial Theory, Series B, 16:97–105, 1974.
[13] J. Bourgain. On libschitz embedding of finite metric spaces in hilbert
space. Israel J. Math., 52:46–52, 1985.
[14] W. Brown. On graphs that do not contain a Thomsen graph. Canad.
Math. Bull., 9:281–285, 1966.
[15] J. L. Carter and M. N. Wegman. Universal classes of hash functions.
J. Comput. Syst. Sc., 18:143–154, 1979.
[16] S. Chaudhuri and C. Zaroliagis. Shortest paths in digraphs of small
treewidth. Part I: Sequential algorithms. Algorithmica, 27:212–226,
2000.
[17] Y. Chiang and J. Mitchell. Two-point Euclidean shortest path queries
in the plane. In Proc. 10th SODA, pages 215–224, 1999.
[18] E. Cohen. Fast algorithms for constructing t-spanners and paths with
stretch t. SIAM J. Computing, 28:210–236, 1999.
[19] E. Cohen and U. Zwick. All-pairs small-stretch paths. J. Algorithms,
38:335–353, 2001.
[20] T. Cormen, C. Leiserson, and R. Rivest. Introduction to algorithms.
The MIT Press, 1990.
[21] M. Dietzfelbinger, T. Hagerup, J. Katajainen, and M. Penttonen. A
reliable randomized algorithm for the closest-pair problem. J.
Algorithms, 25:19–51, 1997.
[22] M. Dietzfelbinger and M. Hüne. A dictionary implementaion based
on dynamic perfect hashing, 1996. DIMACS 1996 Implementation
Challenge. To appear in AMS-DIMACS Series in Discrete
Mathematics and Theoretical Computer Science.
[23] E. Dijkstra. A note on two problems in connexion with graphs.
Numerische Mathematik, 1:269–271, 1959.
[24] H. Djidjev. Efficient algorithms for shortest path queries in planar
digraphs. In Proc. 22nd WG, pages 151–165, 1996.
[25] D. Dor, S. Halperin, and U. Zwick. All pairs almost shortest paths.
SIAM J. Computing, 29:1740–1759, 2000.
[26] M. Elkin. Computing almost shortest paths. Technical Report
MCS01-03, Faculty of Mathematics and Computer Science, The
Weizmann Institute of Science, Rehovot, Israel, 2001.
[27] M. Elkin and D. Peleg. (1 + �; �)-Spanner constructions for general
graphs. In Proc. 33th STOC, 2001. To appear.
[28] P. Erdős. Extremal problems in graph theory. In Theory of Graphs
and its Applications (Proc. Sympos. Smolenice, 1963), pages 29–36.
Publ. House Czechoslovak Acad. Sci., Prague, 1964.
[29] P. Erdős, A. Rényi, and V. Sós. On a problem of graph theory. Studia
Sci. Math. Hungar., 1:215–235, 1966.
[30] M. Fredman, J. Komlós, and E. Szemerédi. Storing a sparse table
with O(1) worst case access time. J. ACM, 31:538–544, 1984.
[31] M. Fredman and R. Tarjan. Fibonacci heaps and their uses in
improved network optimization algorithms. J. ACM, 34:596–615,
1987.
[32] C. Gavoille, D. Peleg, S. Pérennes, and R. Raz. Distance labeling in
graphs. In Proc. 12th SODA, pages 210–219, 2001.
[33] S. Halperin and U. Zwick. Unpublished result, 1996.
[34] P. Indyk. Sublinear time algorithms for metric space problems. In
Proc. 31th STOC, pages 428–434, 1999.
[35] D. Karger, D. Koller, and S. Phillips. Finding the hidden path: time
bounds for all-pairs shortest paths. SIAM J. Computing,
22:1199–1217, 1993.
[36] J. Kruskal. On the shortest spanning subtree of a graph and the
traveling salesman problem. Proceedings of the American
Mathematical Society, 7:48–50, 1956.
[37] F. Lazebnik and V. Ustimenko. New examples of graphs without
small cycles and of large size. European Journal of Combinatorics,
14(5):445–460, 1993. Algebraic combinatorics (Vladimir, 1991).
[38] F. Lazebnik, V. Ustimenko, and A. Woldar. A new series of dense
graphs of high girth. Bulletin of the American Mathematical Society
(New Series), 32(1):73–79, 1995.
[39] F. Lazebnik, V. Ustimenko, and A. Woldar. A characterization of the
components of the graphs D(k; q). Discrete Mathematics,
157(1-3):271–283, 1996.
[40] N. Linial, E. London, and Y. Rabinovich. The geometry of graphs
and some of its algorithmic applications. Combinatorica,
15:215–245, 1995.
[41] A. Lubotzky, R. Phillips, and P. Sarnak. Ramanujan graphs.
Combinatorica, 8:261–277, 1988.
[42] G. Margulis. Explicit group–theoretical construction of
combinatorial schemes and their application to the design of
expanders and concentrators. Problems of Information Transmission,
24:39–46, 1988.
[43] C. McGeoch. All-pairs shortest paths and the essential subgraph.
Algorithmica, 13:426–461, 1995.
[44] D. Peleg. Distributed computing – A locality-sensitive approach.
Society for Industrial and Applied Mathematics (SIAM),
Philadelphia, PA, 2000.
[45] D. Peleg. Proximity-preserving labeling schemes. J. Graph Theory,
33:167–176, 2000.
[46] D. Peleg and A. Schäffer. Graph spanners. J. Graph Theory,
13:99–116, 1989.
[47] I. Reiman. Über ein Problem von K. Zarankiewicz. Acta. Math.
Acad. Sci. Hungar., 9:269–273, 1958.
[48] A. Shoshan and U. Zwick. All pairs shortest paths in undirected
graphs with integer weights. In Proc. 40th FOCS, pages 605–614,
1999.
[49] M. Thorup. Undirected single-source shortest paths with positive
integer weights in linear time. J. ACM, 46:362–394, 1999.
[50] M. Thorup. Even strongly universal hashing is pretty fast. In Proc.
11th SODA, pages 496–497, 2000.
[51] M. Thorup. Floats, integers, and single source shortest paths. J.
Algorithms, 35:189–201, 2000.
[52] M. Thorup. Compact oracles for reachability and approximate
distances in planar digraphs, 2001. Submitted.
[53] M. Thorup. Quick k-median, k-center, and facility location for sparse
graphs. In Proc. 28th ICALP, 2001. To appear.
[54] M. Thorup and U. Zwick. Compact routing schemes. In Proc. 13th
SPAA, 2001. To appear.
[55] J. Tits. Sur la trialité et certains groupes qui s’en déduisent. Publ.
Math. I.H.E.S., 2:14–20, 1959.
[56] R. Wenger. Extremal graphs with no C4’s, C6’s and C10’s. Journal
of Combinatorial Theory, Series B, 52:113–116, 1991.
[57] J. Williams. Heapsort. Comm. ACM, 7(5):347–348, 1964.
[58] A. Woldar and V. Ustimenko. An application of group theory to
extremal graph theory. In Group theory, Proceedings of the Ohio
State-Denison Conference, pages 293–298. World Sci. Publishing,
River Edge, NJ, 1993.
[59] U. Zwick. All pairs shortest paths in weighted directed graphs –
exact and almost exact algorithms. In Proc. 39th FOCS, pages
310–319, 1998.