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) be undirected with weights w:E→R≥0, and let d be its distance function, with d(u,v)=+∞ when u and v are in different components. Then d(u,v)=d(v,u) and d(u,x)≤d(u,y)+d(y,x) for all u,x,y. Let V=A0⊇A1⊇⋯⊇Ak−1⊇Ak=∅ be any nested sets; the proof does not use how they are sampled. Write d(A,v)=mina∈Ad(a,v), with min∅=+∞.
Fact 1 (pivots). The Dijkstra run of line L3 from all of Ai at once is Dijkstra on G plus a new source joined to each vertex of Ai by an edge of weight 0, so it gives d(Ai,v), and pi(v) is a vertex of Ai with d(pi(v),v)=d(Ai,v) whenever this is finite. Also p0(v)=v and d(A0,v)=0.
Preprocessing
Fix i and w∈Ai∖Ai+1, and let b(x)=d(Ai+1,x) and C(w)={x:d(w,x)<b(x)}.
Lemma 2 (clusters are closed under shortest paths). If x∈C(w) and y is on a shortest path from w to x, then y∈C(w).
Proof. d(w,x)<b(x)<∞, so the path exists, and d(w,y)+d(y,x)=d(w,x). Also b(x)≤b(y)+d(y,x), by the triangle inequality for the nearest vertex of Ai+1 to y. So d(w,y)=d(w,x)−d(y,x)<b(x)−d(y,x)≤b(y). □
Lemma 3 (line L6 settles exactly C(w), with exact distances). The search of line L6 settles x with key D if and only if x∈C(w), and then D=d(w,x).
Proof. Every key pushed is the weight of a walk from w, so every key of x is at least d(w,x). A key D of x is pushed only if D<b(x), so a settled x has d(w,x)≤D<b(x), and x∈C(w).
Conversely, let x∈C(w) and let w=y0,y1,…,yr=x be a shortest path; by Lemma 2 every yj∈C(w), so d(w,yj)<b(yj). The search is Dijkstra on G in which some pushes are not done, and every push not done has a key D≥b(y)>d(w,y) for its vertex y. We show by induction on j that yj is settled with key d(w,yj). For j=0: d(w,w)=0<b(w), so the search starts with w at key 0, which is the least possible key, and settles it with key 0. For j≥1: by induction yj−1 is settled with key d(w,yj−1), and it then relaxes the edge to yj with the key Dj=d(w,yj−1)+w(yj−1,yj)=d(w,yj)<b(yj). If yj was settled before yj−1, its key is at most d(w,yj−1)≤Dj, as keys are extracted in nondecreasing order. Otherwise the entry Dj is pushed, or an entry of yj with a key at most Dj is already in the heap, so yj is settled later with a key at most Dj, 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), and at least d(w,yj) by the first paragraph. □
Corollary 4. After line L7, for every v, B(v) holds exactly the vertices w with v∈C(w), for the i with w∈Ai∖Ai+1, each with the value 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) and let δ=d(u0,v0). The pair {u,v} is always {u0,v0}, so d(u,v)=δ 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)≥δ.
Proof. Line L15 runs with w=pi(u), where p0(u)=u, so d(Ai,u)=d(w,u)=d(u,w) by Fact 1. By Corollary 4, B(v)[w]=d(w,v). The sum is at least d(u,v)=δ by the triangle inequality. □
Lemma 6 (the key step). If w∈Ai and w is not a key of B(v), then d(Ai+1,v)≤d(w,v).
Proof. If w∈Ai+1 this is the definition of d(Ai+1,v). Otherwise w∈Ai∖Ai+1, and by Corollary 4, v∈/C(w), that is, d(w,v)≥d(Ai+1,v). □
Lemma 7 (invariant). Each time line L10 tests w, w=pi(u), w∈Ai, and d(w,u)≤iδ.
Proof. At the first test, w=u=p0(u) and d(w,u)=0. Suppose the invariant holds at a test that fails, with values u,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)δ. Lines L11 and L12 set i′=i+1, u′=v and v′=u. If line L13 does not return, line L14 sets w′=pi′(u′)=pi+1(v)∈Ai+1, and by Fact 1, d(w′,u′)=d(Ai+1,v)≤(i+1)δ=i′δ. □
Theorem. TZ-QUERY terminates. If δ<∞ it returns d^ with δ≤d^≤(2k−1)δ; if δ=∞ it returns ∞. If A1=∅, in particular if k=1, it returns δ.
Proof. Termination: each iteration of the loop increases i, and line L13 returns when i=k, so there are at most k iterations.
Case δ<∞. Let K be the component of u0 and v0, and let j be the largest index with Aj∩K=∅; j≥0 as A0=V, and j≤k−1 as Ak=∅. Suppose the test of line L10 fails at level i. In the proof of Lemma 7, d(Ai+1,v)≤(i+1)δ<∞ with v∈K, so Ai+1 meets K, and i+1≤j≤k−1. Hence line L13 sees i+1<k and d(Ai+1,v)<∞, so pi+1(v) is defined and line L13 does not return. So the loop ends only at line L15, with i≤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)δ, and d^≥δ. If A1=∅, then j=0, so i=0 and d^=δ.
Case δ=∞. If line L15 returned a value, by Lemma 5 it would be d(u,w)+d(w,v) with w a key of B(v), so d(w,v)<∞, and w=pi(u) with pi(u) defined, so d(w,u)<∞; then u and v would be connected through w. So the loop ends at line L13, which returns ∞. ■