ArticleslgStudy

computer science

Distance oracle

Distance oracle is a computer science topic covered in the lgStudy science library. This page brings together a partial reference excerpt, illustrations, worked examples, real-world applications and a short study plan, so you can understand Distance oracle rather than just read about it. In short: In computing, a distance oracle (DO) is a data structure for calculating distances between vertices in a graph. Introduction Let G(V,E) be an undirected, weighted graph, with n = |V| nodes and m = |E| edges.

Key takeaways

  • Distance oracle belongs to computer science; place it in that map before memorising details.
  • Learn the definition first, then one example that makes the definition concrete.
  • Connect Distance oracle to a quantity you can measure, compute or draw — that is where exam questions come from.
  • Reproduce the core statement of Distance oracle from memory before moving on to harder problems.

Reference excerpt

In computing, a distance oracle (DO) is a data structure for calculating distances between vertices in a graph.

Introduction Let G(V,E) be an undirected, weighted graph, with n = |V| nodes and m = |E| edges. We would like to answer queries of the form "what is the distance between the nodes s and t?". One way to do this is just run the Dijkstra algorithm. This takes time O(m+nlog⁡n), and requires no extra space (besides the graph itself). In order to answer many queries more efficiently, we can spend some time in pre-processing the graph and creating an auxiliary data structure. A simple data structure that achieves this goal is a matrix which specifies, for each pair of nodes, the distance between them. This structure allows us to answer queries in constant time O(1), but requires O(n2) extra space. It can be initialized in time O(n3) using an all-pairs shortest paths algorithm, such as the Floyd–Warshall algorithm. A DO lies between these two extremes. It uses less than O(n2) space in order to answer queries in less than O(m+nlog⁡n) time. Most DOs have to compromise on accuracy, i.e. they don't return the accurate distance but rather a constant-factor approximation of it.

Approximate DO Thorup and Zwick describe more than 10 different DOs. They then suggest a new DO that, for every k, requires space O(kn1+1/k), such that any subsequent distance query can be approximately answered in time O(k). The approximate distance returned is of stretch at most 2k−1, that is, the quotient obtained by dividing the estimated distance by the actual distance lies between 1 and 2k−1. The initialization time is O(kmn1/k). Some special cases include:

For k=1 we get the simple distance matrix. For k=2 we get a structure using O(n1.5) space which answers each query in constant time and approximation factor at most 3. For k=⌊log⁡n⌋, we get a structure using O(nlog⁡n) space, query time O(log⁡n), and stretch O(log⁡n). Higher values of k do not improve the space or preprocessing time.

DO for general metric spaces The oracle is built of a decreasing collection of k+1 sets of vertices:

A0=V For every i=1,…,k−1: Ai contains each element of Ai−1, independently, with probability n−1/k. Note that the expected size of Ai is n1−i/k. The elements of Ai are called i-centers. Ak=∅︀ For every node v, calculate its distance from each of these sets:

For every i=0,…,k−1: d(Ai,v)=min⁡(d(w,v)|w∈Ai and pi(v)=arg⁡min⁡(d(w,v)|w∈Ai. I.e., pi(v) is the i-center nearest to v, and d(Ai,v) is the distance between them. Note that for a fixed v, this distance is weakly increasing with i. Also note that for every v, d(A0,v)=0 and p0(v)=v. d(Ak,v)=∞. For every node v, calculate:

For every i=0,…,k−1: Bi(v)={w∈Ai∖Ai+1|d(w,v)<d(Ai+1,v)}. Bi(v) contains all vertices in Ai which are strictly closer to v than all vertices in Ai+1. The partial unions of Bis are balls in increasing diameter, that contain vertices with distances up to the first vertex of the next level. For every v, compute its bunch:

B(v)=⋃i=0k−1Bi(v). It is possible to show that the expected size of B(v) is at most kn1/k. For every bunch B(v), construct a hash table that holds, for every w∈B(V), the distance d(w,v). The total size of the data structure is O(kn+Σ|B(v)|)=O(kn+nkn1/k)=O(kn1+1/k) Having this structure initialized, the following algorithm finds the distance between two nodes, u and v:

w:=u,i:=0 while w∉B(v): i:=i+1 (u,v):=(v,u) (swap the two input nodes; this does not change the distance between them since the graph is undirected). w:=pi(u) return d(w,u)+d(w,v) It is possible to show that, in each iteration, the distance d(w,u) grows by at most d(v,u). Since Ak−1⊆B(v), there are at most k-1 iterations, so finally d(w,u)≤(k−1)d(v,u). Now by the triangle inequality, d(w,v)≤d(w,u)+d(u,v)≤kd(v,u), so the distance returned is at most (2k−1)d(u,v).

Improvements The above result was later improved by Patrascu and Roditty who suggest a DO of size O(n4/3m1/3) which returns a factor 2 approximation.

Reduction from set intersection oracle If there is a DO with an approximation factor of at most 2, then it is possible to build a set intersection oracle (SIO) with query time O(1) and space requirements O(N+n), where n is the number of sets and N the sum of their sizes; see set intersection oracle#Reduction to approximate distance oracle. It is believed that the SIO problem does not have a non-trivial solution. I.e., it requires Ω(n2) space to answer queries in time O(1), e.g. using an n-by-n table with the intersection between each two sets. If this conjecture is true, this implies that there is no DO with an approximation factor of less than 2 and a constant query time.

References

Worked examples

Example 1 — a first encounter with Distance oracle

Start with the simplest possible case. Write down what Distance oracle claims or describes in one sentence, then invent the smallest concrete situation in which that sentence is true. In computer science, the smallest case is usually a single object, a single equation or a single measurement. Check that every symbol or term in your sentence has a meaning in that case.

Example 2 — changing one variable

Take the situation from Example 1 and change exactly one quantity: double it, halve it, or set it to zero. Predict what should happen to Distance oracle before you calculate. Comparing your prediction with the result is the fastest way to find out whether you understand the idea or only the words.

Example 3 — an exam-style question

Typical questions about Distance oracle ask you to (a) state it precisely, (b) apply it to given data, and (c) explain a limitation. Practise writing all three answers in under five minutes; the third part is what separates a full-mark answer from an average one.

Applications of Distance oracle

In research
Distance oracle appears in computer science research whenever the underlying quantities have to be modelled precisely. Papers usually cite it as a starting assumption and then explore where it breaks down.
In technology and industry
Engineering practice reuses Distance oracle in design rules, simulations and safety margins. Knowing the idea lets you read a specification sheet and understand why the numbers look the way they do.
In the classroom
Distance oracle is common in secondary-school and first-year university syllabi. It links to neighbouring topics Graph data structures, Graph theory, so understanding it makes those chapters shorter.
In everyday life
Look for Distance oracle outside the textbook — in sport, cooking, traffic, electronics or the sky above you. An example you found yourself is remembered far longer than one you were given.
Ask Teacher Smith questions about this articleOpens your AI tutor with a question about “Distance oracle” →

How to study Distance oracle in 20 minutes

  1. Read the reference excerpt below once, without taking notes.
  2. Close the page and write down what Distance oracle means in your own words.
  3. Compare your version with the excerpt and mark what you missed.
  4. Work through the three examples above with pen and paper.
  5. Explain Distance oracle out loud to somebody else — or to Teacher Smith in the lgStudy chat.

Frequently asked questions

What is Distance oracle in simple terms?

In computing, a distance oracle (DO) is a data structure for calculating distances between vertices in a graph. Introduction Let G(V,E) be an undirected, weighted graph, with n = |V| nodes and m = |E| edges.

Why does Distance oracle matter?

Because it connects several computer science ideas at once: it gives you a definition you can apply, a quantity you can calculate, and a way to check whether a result is plausible.

How should I study Distance oracle?

Read the excerpt, restate it from memory, then work through the examples and applications listed on this page. The five-step study plan above takes about twenty minutes.

What does this page cover?

It gives you a compact reference excerpt plus original lgStudy explanations, examples, applications and study material on Distance oracle.

Tags

  • Graph data structures
  • Graph theory

Keep exploring