Algorithm Search⌘K

Proofs: Fusion tree

These are the human proofs of Fusion tree, at full length.

Fusion node rank by sketches is exact full

This proof shows: correctness, invariant, termination.

Fix a node with keys x0<⋯<xk−1x_0 < \cdots < x_{k-1}, positions DD and sketches as in the page. For ww-bit numbers a≠ba \ne b, write δ(a,b)=msb(a⊕b)\delta(a, b) = \mathrm{msb}(a \oplus b), the highest position where they differ. Above δ(a,b)\delta(a, b) they agree, and a<ba < b exactly when aa has 0 at δ(a,b)\delta(a, b).

Lemma 1. For keys xa<xbx_a < x_b of the node, δ(xa,xb)∈D\delta(x_a, x_b) \in D.

Proof. Let t=max⁡a≤j<bδ(xj,xj+1)t = \max_{a \le j < b} \delta(x_j, x_{j+1}), which is in DD. Each consecutive pair agrees above tt, so xax_a and xbx_b agree above tt. At tt, the sequence xa,…,xbx_a, \ldots, x_b is increasing and agrees above tt, so its bits at tt are a run of 0 then a run of 1, and some consecutive pair differs at tt; so xax_a has 0 and xbx_b has 1 at tt, and δ(xa,xb)=t\delta(x_a, x_b) = t. □\square

Lemma 2. If zz and a key xx differ and δ(z,x)∈D\delta(z, x) \in D, then sketch(z)<sketch(x)\mathrm{sketch}(z) < \mathrm{sketch}(x) exactly when z<xz < x, and the sketches differ.

Proof. The sketch bits come from positions of DD in decreasing order. At the positions of DD above δ(z,x)\delta(z, x) both agree; at δ(z,x)\delta(z, x) they differ, in the same direction as zz and xx. □\square

By Lemmas 1 and 2, the sketches of the keys are strictly increasing.

Lemma 3 (packed rank). For 0≤s≤2r0 \le s \le 2^r, PACKED-RANK(v,s)=∣{j:sketch(xj)<s}∣(v, s) = |\{j : \mathrm{sketch}(x_j) < s\}|.

Proof. Q=sMQ = sM has ss in each field, since s<2r+1s < 2^{r+1}. Field jj of KK is 2r+sketch(xj)2^r + \mathrm{sketch}(x_j), and 0≤2r+sketch(xj)−s<2r+10 \le 2^r + \mathrm{sketch}(x_j) - s < 2^{r+1}, so K−QK - Q is the field-wise difference with no borrow between fields. Its bit rr is set exactly when 2r+sketch(xj)−s≥2r2^r + \mathrm{sketch}(x_j) - s \ge 2^r. So TT has one bit for each jj with sketch(xj)≥s\mathrm{sketch}(x_j) \ge s, and line L11 returns the count of the other keys. □\square

Now let qq be a query that is not a key of the node. Let ℓ=min⁡jδ(q,xj)\ell = \min_j \delta(q, x_j), so the keys with δ(q,xj)=ℓ\delta(q, x_j) = \ell are those with the longest common prefix with qq; call this set the block B\mathcal{B}. Write PP for the bits of qq above ℓ\ell.

Lemma 4. B\mathcal{B} is a nonempty set of consecutive keys, and for each key x∉Bx \notin \mathcal{B}, δ(q,x)∈D\delta(q, x) \in D and δ(q,x)>ℓ\delta(q, x) > \ell. With ii the value of line L2, at least one of xi−1x_{i-1}, xix_i (those that exist) is in B\mathcal{B}, so line L3 picks y∈By \in \mathcal{B}, and δ(q,y)=ℓ=p\delta(q, y) = \ell = p.

Proof. B\mathcal{B} is the set of keys whose bits above ℓ\ell are PP and whose bit at ℓ\ell is not that of qq; since no key agrees with qq at ℓ\ell and above (by the choice of ℓ\ell), B\mathcal{B} is the set of keys with prefix PP, an interval of the sorted order, and it is nonempty. Take x∉Bx \notin \mathcal{B} and y∈By \in \mathcal{B}. Then j=δ(q,x)>ℓj = \delta(q, x) > \ell, and yy agrees with qq at all positions ≥j\ge j, so δ(x,y)=j\delta(x, y) = j, which is in DD by Lemma 1. Since qq and yy agree at jj and above, Lemma 2 for z=qz = q shows that sketch(x)<sketch(q)\mathrm{sketch}(x) < \mathrm{sketch}(q) exactly when x<qx < q, which holds exactly when x<yx < y, that is, when xx is before the block. So the keys with sketch <sketch(q)< \mathrm{sketch}(q) are all keys before the block and some keys of the block, which by the order of sketches are a prefix of the block. Hence a≤i≤b+1a \le i \le b + 1 where xa,…,xbx_a, \ldots, x_b is the block. If i=ai = a then xi∈Bx_i \in \mathcal{B}; if i=b+1i = b + 1 then xi−1∈Bx_{i-1} \in \mathcal{B}; otherwise both are. Line L3 compares q⊕xi−1q \oplus x_{i-1} and q⊕xiq \oplus x_i as numbers; the smaller one has the lower top bit, so it picks a key with δ=ℓ\delta = \ell. □\square

If qq is a key, then qq itself has the longest common prefix, and the same argument with B={q}\mathcal{B} = \{q\} (and ℓ=−1\ell = -1) shows that q∈{xi−1,xi}q \in \{x_{i-1}, x_i\}. Its sketch is not less than itself, so q=xiq = x_i, which is what line L4 returns.

Lemma 5 (the corrected rank). Line L6 or L7 returns ∣{j:xj≤q}∣−1|\{j : x_j \le q\}| - 1.

Proof. Let p=ℓp = \ell from Lemma 4.

Case bit pp of qq is 1. The keys of B\mathcal{B} have prefix PP and bit 0 at pp, so they are all <q< q; each other key is <q< q exactly when it is <e< e, where e=P 0 1⋯1e = P\,0\,1\cdots1, because ee and qq agree above pp and the other keys differ from both above pp. So the number of keys ≤q\le q is the number of keys ≤e\le e. Now count by sketch. For x∉Bx \notin \mathcal{B}, δ(e,x)=δ(q,x)∈D\delta(e, x) = \delta(q, x) \in D, so by Lemma 2 the sketch comparison with ee is strict and correct. For x∈Bx \in \mathcal{B}, x≤ex \le e (it has prefix P 0P\,0, and ee is the largest number with that prefix), and sketch(x)≤sketch(e)\mathrm{sketch}(x) \le \mathrm{sketch}(e): both agree at the positions of DD at pp and above, and below pp, ee has only ones. So ∣{j:sketch(xj)≤sketch(e)}∣=∣{j:xj≤e}∣|\{j : \mathrm{sketch}(x_j) \le \mathrm{sketch}(e)\}| = |\{j : x_j \le e\}|, and by Lemma 3 with s=sketch(e)+1≤2rs = \mathrm{sketch}(e) + 1 \le 2^r this is what line L6 computes.

Case bit pp of qq is 0. The keys of B\mathcal{B} have bit 1 at pp, so they are all >q> q; with e=P 1 0⋯0e = P\,1\,0\cdots0, each other key is <q< q exactly when it is <e< e. So the number of keys ≤q\le q (equal to the number <q< q, as qq is not a key) is the number of keys <e< e. For x∉Bx \notin \mathcal{B} the sketch comparison is strict and correct as before. For x∈Bx \in \mathcal{B}, x≥ex \ge e and sketch(x)≥sketch(e)\mathrm{sketch}(x) \ge \mathrm{sketch}(e), since ee has only zeros below pp. So ∣{j:sketch(xj)<sketch(e)}∣=∣{j:xj<e}∣|\{j : \mathrm{sketch}(x_j) < \mathrm{sketch}(e)\}| = |\{j : x_j < e\}|, which line L7 computes by Lemma 3. □\square

So NODE-PRED returns the index of the largest node key ≤q\le q, or −1-1.

Theorem. PRED(T,q)(T, q) returns max⁡{x∈S:x≤q}\max\{x \in S : x \le q\}, or NIL.

Proof. Invariant of the loop of line L13: the answer is the larger of bestbest and the largest key ≤q\le q in the subtree of vv (with NIL below every key), and every key of that subtree is larger than bestbest. At the root it holds with $best = $ NIL. In a node, by Lemma 5, xj≤q<xj+1x_j \le q < x_{j+1} (with the obvious meaning at the ends). If xj=qx_j = q, the answer is qq (line L15). Otherwise, by (U1), the keys of the subtree of vv that are ≤q\le q are x0,…,xjx_0, \ldots, x_j, the subtrees c0,…,cjc_0, \ldots, c_j, and the part of cj+1c_{j+1} that is ≤q\le q, and all the keys before cj+1c_{j+1} are ≤xj\le x_j. So the best candidate outside cj+1c_{j+1} is xjx_j (line L16), every key of cj+1c_{j+1} is larger than xjx_j, and moving to cj+1c_{j+1} keeps the invariant. The loop ends because the depth grows, and at $v = $ NIL the answer is bestbest. ■\blacksquare

Back to Fusion tree