Algorithm Search⌘K

Proofs: Thorup-Zwick approximate distance oracle

These are the human proofs of Thorup-Zwick approximate distance oracle, at full length.

TZ-QUERY returns an estimate with stretch at most 2k-1, and infinity exactly for disconnected pairs full

This proof shows: correctness, termination.

Let G=(V,E)G = (V, E) be undirected with weights w:E→R≥0w : E \to \mathbb{R}_{\ge 0}, and let dd be its distance function, with d(u,v)=+∞d(u, v) = +\infty when uu and vv are in different components. Then d(u,v)=d(v,u)d(u, v) = d(v, u) and d(u,x)≤d(u,y)+d(y,x)d(u, x) \le d(u, y) + d(y, x) for all u,x,yu, x, y. Let V=A0⊇A1⊇⋯⊇Ak−1⊇Ak=∅V = A_0 \supseteq A_1 \supseteq \cdots \supseteq A_{k-1} \supseteq A_k = \emptyset be any nested sets; the proof does not use how they are sampled. Write d(A,v)=min⁡a∈Ad(a,v)d(A, v) = \min_{a \in A} d(a, v), with min⁡∅=+∞\min \emptyset = +\infty.

Fact 1 (pivots). The Dijkstra run of line L3 from all of AiA_i at once is Dijkstra on GG plus a new source joined to each vertex of AiA_i by an edge of weight 0, so it gives d(Ai,v)d(A_i, v), and pi(v)p_i(v) is a vertex of AiA_i with d(pi(v),v)=d(Ai,v)d(p_i(v), v) = d(A_i, v) whenever this is finite. Also p0(v)=vp_0(v) = v and d(A0,v)=0d(A_0, v) = 0.

Preprocessing

Fix ii and w∈Ai∖Ai+1w \in A_i \setminus A_{i+1}, and let b(x)=d(Ai+1,x)b(x) = d(A_{i+1}, x) and C(w)={x:d(w,x)<b(x)}C(w) = \{x : d(w, x) < b(x)\}.

Lemma 2 (clusters are closed under shortest paths). If x∈C(w)x \in C(w) and yy is on a shortest path from ww to xx, then y∈C(w)y \in C(w).

Proof. d(w,x)<b(x)<∞d(w, x) < b(x) < \infty, so the path exists, and d(w,y)+d(y,x)=d(w,x)d(w, y) + d(y, x) = d(w, x). Also b(x)≤b(y)+d(y,x)b(x) \le b(y) + d(y, x), by the triangle inequality for the nearest vertex of Ai+1A_{i+1} to yy. So d(w,y)=d(w,x)−d(y,x)<b(x)−d(y,x)≤b(y)d(w, y) = d(w, x) - d(y, x) < b(x) - d(y, x) \le b(y). □\square

Lemma 3 (line L6 settles exactly C(w)C(w), with exact distances). The search of line L6 settles xx with key DD if and only if x∈C(w)x \in C(w), and then D=d(w,x)D = d(w, x).

Proof. Every key pushed is the weight of a walk from ww, so every key of xx is at least d(w,x)d(w, x). A key DD of xx is pushed only if D<b(x)D < b(x), so a settled xx has d(w,x)≤D<b(x)d(w, x) \le D < b(x), and x∈C(w)x \in C(w).

Conversely, let x∈C(w)x \in C(w) and let w=y0,y1,…,yr=xw = y_0, y_1, \ldots, y_r = x be a shortest path; by Lemma 2 every yj∈C(w)y_j \in C(w), so d(w,yj)<b(yj)d(w, y_j) < b(y_j). The search is Dijkstra on GG in which some pushes are not done, and every push not done has a key D≥b(y)>d(w,y)D \ge b(y) > d(w, y) for its vertex yy. We show by induction on jj that yjy_j is settled with key d(w,yj)d(w, y_j). For j=0j = 0: d(w,w)=0<b(w)d(w, w) = 0 < b(w), so the search starts with ww at key 0, which is the least possible key, and settles it with key 0. For j≥1j \ge 1: by induction yj−1y_{j-1} is settled with key d(w,yj−1)d(w, y_{j-1}), and it then relaxes the edge to yjy_j with the key Dj=d(w,yj−1)+w(yj−1,yj)=d(w,yj)<b(yj)D_j = d(w, y_{j-1}) + w(y_{j-1}, y_j) = d(w, y_j) < b(y_j). If yjy_j was settled before yj−1y_{j-1}, its key is at most d(w,yj−1)≤Djd(w, y_{j-1}) \le D_j, as keys are extracted in nondecreasing order. Otherwise the entry DjD_j is pushed, or an entry of yjy_j with a key at most DjD_j is already in the heap, so yjy_j is settled later with a key at most DjD_j, as its first extracted entry has the least key among its entries in the heap. In both cases its key is at most d(w,yj)d(w, y_j), and at least d(w,yj)d(w, y_j) by the first paragraph. □\square

Corollary 4. After line L7, for every vv, B(v)B(v) holds exactly the vertices ww with v∈C(w)v \in C(w), for the ii with w∈Ai∖Ai+1w \in A_i \setminus A_{i+1}, each with the value d(w,v)d(w, v).

Each search ends, since it settles each vertex at most once and pushes at most once per edge scan; so preprocessing terminates.

Queries

Fix a query (u0,v0)(u_0, v_0) and let δ=d(u0,v0)\delta = d(u_0, v_0). The pair {u,v}\{u, v\} is always {u0,v0}\{u_0, v_0\}, so d(u,v)=δd(u, v) = \delta at all times.

Lemma 5 (a value returned is a walk). If line L15 returns a value, it is d(u,w)+d(w,v)≥δd(u, w) + d(w, v) \ge \delta.

Proof. Line L15 runs with w=pi(u)w = p_i(u), where p0(u)=up_0(u) = u, so d(Ai,u)=d(w,u)=d(u,w)d(A_i, u) = d(w, u) = d(u, w) by Fact 1. By Corollary 4, B(v)[w]=d(w,v)B(v)[w] = d(w, v). The sum is at least d(u,v)=δd(u, v) = \delta by the triangle inequality. □\square

Lemma 6 (the key step). If w∈Aiw \in A_i and ww is not a key of B(v)B(v), then d(Ai+1,v)≤d(w,v)d(A_{i+1}, v) \le d(w, v).

Proof. If w∈Ai+1w \in A_{i+1} this is the definition of d(Ai+1,v)d(A_{i+1}, v). Otherwise w∈Ai∖Ai+1w \in A_i \setminus A_{i+1}, and by Corollary 4, v∉C(w)v \notin C(w), that is, d(w,v)≥d(Ai+1,v)d(w, v) \ge d(A_{i+1}, v). □\square

Lemma 7 (invariant). Each time line L10 tests ww, w=pi(u)w = p_i(u), w∈Aiw \in A_i, and d(w,u)≤iδd(w, u) \le i \delta.

Proof. At the first test, w=u=p0(u)w = u = p_0(u) and d(w,u)=0d(w, u) = 0. Suppose the invariant holds at a test that fails, with values u,v,w,iu, v, w, i. By Lemma 6 and the triangle inequality,

d(Ai+1,v)≤d(w,v)≤d(w,u)+d(u,v)≤iδ+δ=(i+1)δ.d(A_{i+1}, v) \le d(w, v) \le d(w, u) + d(u, v) \le i\delta + \delta = (i + 1)\delta.

Lines L11 and L12 set i′=i+1i' = i + 1, u′=vu' = v and v′=uv' = u. If line L13 does not return, line L14 sets w′=pi′(u′)=pi+1(v)∈Ai+1w' = p_{i'}(u') = p_{i+1}(v) \in A_{i+1}, and by Fact 1, d(w′,u′)=d(Ai+1,v)≤(i+1)δ=i′δd(w', u') = d(A_{i+1}, v) \le (i + 1)\delta = i' \delta. □\square

Theorem. TZ-QUERY terminates. If δ<∞\delta < \infty it returns d^\hat d with δ≤d^≤(2k−1)δ\delta \le \hat d \le (2k - 1)\delta; if δ=∞\delta = \infty it returns ∞\infty. If A1=∅A_1 = \emptyset, in particular if k=1k = 1, it returns δ\delta.

Proof. Termination: each iteration of the loop increases ii, and line L13 returns when i=ki = k, so there are at most kk iterations.

Case δ<∞\delta < \infty. Let KK be the component of u0u_0 and v0v_0, and let jj be the largest index with Aj∩K≠∅A_j \cap K \ne \emptyset; j≥0j \ge 0 as A0=VA_0 = V, and j≤k−1j \le k - 1 as Ak=∅A_k = \emptyset. Suppose the test of line L10 fails at level ii. In the proof of Lemma 7, d(Ai+1,v)≤(i+1)δ<∞d(A_{i+1}, v) \le (i + 1)\delta < \infty with v∈Kv \in K, so Ai+1A_{i+1} meets KK, and i+1≤j≤k−1i + 1 \le j \le k - 1. Hence line L13 sees i+1<ki + 1 < k and d(Ai+1,v)<∞d(A_{i+1}, v) < \infty, so pi+1(v)p_{i+1}(v) is defined and line L13 does not return. So the loop ends only at line L15, with i≤ji \le j. By Lemmas 5 and 7, the value is

d^=d(u,w)+d(w,v)≤d(u,w)+d(w,u)+d(u,v)≤2iδ+δ=(2i+1)δ≤(2k−1)δ,\hat d = d(u, w) + d(w, v) \le d(u, w) + d(w, u) + d(u, v) \le 2i\delta + \delta = (2i + 1)\delta \le (2k - 1)\delta,

and d^≥δ\hat d \ge \delta. If A1=∅A_1 = \emptyset, then j=0j = 0, so i=0i = 0 and d^=δ\hat d = \delta.

Case δ=∞\delta = \infty. If line L15 returned a value, by Lemma 5 it would be d(u,w)+d(w,v)d(u, w) + d(w, v) with ww a key of B(v)B(v), so d(w,v)<∞d(w, v) < \infty, and w=pi(u)w = p_i(u) with pi(u)p_i(u) defined, so d(w,u)<∞d(w, u) < \infty; then uu and vv would be connected through ww. So the loop ends at line L13, which returns ∞\infty. ■\blacksquare

Back to Thorup-Zwick approximate distance oracle