Algorithm Search⌘K

Proofs: Sqrt tree

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

The sqrt tree stores the products it names and answers each query with at most two combines full

This proof shows: correctness, termination.

Let (S,⊕)(S, \oplus) be a semigroup and n≥1n \ge 1. For 0≤a≤b≤n−10 \le a \le b \le n - 1 write Π(a,b)=A[a]⊕⋯⊕A[b]\Pi(a, b) = A[a] \oplus \cdots \oplus A[b]. By associativity, for a≤m<ba \le m < b,

Π(a,b)=Π(a,m)⊕Π(m+1,b).(1)\Pi(a, b) = \Pi(a, m) \oplus \Pi(m + 1, b). \tag{1}

For a layer jj with block exponent bjb_j, the block of an index ii is βj(i)=⌊i/2bj⌋\beta_j(i) = \lfloor i / 2^{b_j} \rfloor, and block pp is the range [p2bj,min⁡(n,(p+1)2bj)−1][p 2^{b_j}, \min(n, (p + 1) 2^{b_j}) - 1]. Write stj(p)\mathrm{st}_j(p) and enj(p)\mathrm{en}_j(p) for its first and last index.

Termination. Every loop of BUILD runs over a finite range that does not change in the loop, and the list of layers is finite because kj+1=⌈kj/2⌉<kjk_{j+1} = \lceil k_j / 2 \rceil < k_j for kj≥2k_j \ge 2. QUERY has no loop.

Lemma 1 (the layer table). For each 1≤h<K1 \le h < K there is exactly one layer jj with bj≤h<kjb_j \le h < k_j, and line L2 sets LAYER[h]\mathit{LAYER}[h] to it.

Proof. The intervals [bj,kj)=[kj+1,kj)[b_j, k_j) = [k_{j+1}, k_j) are consecutive pieces of [kt,k0)[k_t, k_0), where layer tt is the last one. They are disjoint, and their union is [bt,K)[b_t, K). The last layer has kt≥2k_t \ge 2 and kt+1=⌈kt/2⌉<2k_{t+1} = \lceil k_t / 2 \rceil < 2, so kt=2k_t = 2, since ⌈3/2⌉=2\lceil 3/2 \rceil = 2, and bt=1b_t = 1. If K<2K < 2, there is no layer and no hh with 1≤h<K1 \le h < K. □\square

Lemma 2 (prefixes and suffixes). After lines L3 and L4, PREj[i]=Π(stj(βj(i)),i)\mathit{PRE}_j[i] = \Pi(\mathrm{st}_j(\beta_j(i)), i) and SUFj[i]=Π(i,enj(βj(i)))\mathit{SUF}_j[i] = \Pi(i, \mathrm{en}_j(\beta_j(i))) for each layer jj and each 0≤i≤n−10 \le i \le n - 1.

Proof. Fix a block [s,e][s, e]. By induction on ii from ss: PREj[s]=A[s]=Π(s,s)\mathit{PRE}_j[s] = A[s] = \Pi(s, s), and PREj[i]=Π(s,i−1)⊕A[i]=Π(s,i)\mathit{PRE}_j[i] = \Pi(s, i - 1) \oplus A[i] = \Pi(s, i) by (1). The suffixes are the mirror image, by induction on ii downward from ee, with A[i]⊕Π(i+1,e)=Π(i,e)A[i] \oplus \Pi(i + 1, e) = \Pi(i, e). The blocks of a layer are disjoint and cover [0,n−1][0, n - 1], so every entry is set once. □\square

Lemma 3 (the between table). After lines L5 and L6, for blocks p≤qp \le q of the same segment of layer jj, BTWj[p,q]=Π(stj(p),enj(q))\mathit{BTW}_j[p, q] = \Pi(\mathrm{st}_j(p), \mathrm{en}_j(q)).

Proof. By Lemma 2, SUFj[stj(p)]=Π(stj(p),enj(p))\mathit{SUF}_j[\mathrm{st}_j(p)] = \Pi(\mathrm{st}_j(p), \mathrm{en}_j(p)), the product of block pp. By induction on qq: BTWj[p,p]\mathit{BTW}_j[p, p] is this product, and BTWj[p,q]=Π(stj(p),enj(q−1))⊕Π(stj(q),enj(q))=Π(stj(p),enj(q))\mathit{BTW}_j[p, q] = \Pi(\mathrm{st}_j(p), \mathrm{en}_j(q - 1)) \oplus \Pi(\mathrm{st}_j(q), \mathrm{en}_j(q)) = \Pi(\mathrm{st}_j(p), \mathrm{en}_j(q)) by (1), since enj(q−1)+1=stj(q)\mathrm{en}_j(q - 1) + 1 = \mathrm{st}_j(q). The loop of line L6 stops at the last block of the segment that exists, so every entry it writes names a range inside [0,n−1][0, n - 1]. □\square

Lemma 4 (the layer separates ll and rr). Let 0≤l<r≤n−10 \le l < r \le n - 1, let h≥1h \ge 1 be the highest bit of ll xor rr, and let j=LAYER[h]j = \mathit{LAYER}[h]. Then p=βj(l)p = \beta_j(l) and q=βj(r)q = \beta_j(r) satisfy p<qp < q, and pp and qq lie in the same segment of layer jj.

Proof. Since r≤n−1<2Kr \le n - 1 < 2^K, the bits of ll and rr above K−1K - 1 are 0, so h<Kh < K and Lemma 1 applies: bj≤h<kjb_j \le h < k_j. The numbers ll and rr agree on all bits above hh, so ⌊l/2h+1⌋=⌊r/2h+1⌋\lfloor l / 2^{h+1} \rfloor = \lfloor r / 2^{h+1} \rfloor, and since h+1≤kjh + 1 \le k_j, also ⌊l/2kj⌋=⌊r/2kj⌋\lfloor l / 2^{k_j} \rfloor = \lfloor r / 2^{k_j} \rfloor: the same segment. They differ at bit h≥bjh \ge b_j, so ⌊l/2bj⌋≠⌊r/2bj⌋\lfloor l / 2^{b_j} \rfloor \ne \lfloor r / 2^{b_j} \rfloor, and l<rl < r gives p<qp < q. A block lies inside one segment, because 2bj2^{b_j} divides 2kj2^{k_j}. □\square

Lemma 5 (query). For 0≤l≤r≤n−10 \le l \le r \le n - 1, QUERY(l,r)(l, r) returns Π(l,r)\Pi(l, r) and applies ⊕\oplus at most twice.

Proof. If l=rl = r, line L7 returns A[l]=Π(l,l)A[l] = \Pi(l, l) with no combine. If h=0h = 0, then ll and rr differ only in bit 0 and l<rl < r, so r=l+1r = l + 1 and line L8 returns A[l]⊕A[r]=Π(l,l+1)A[l] \oplus A[r] = \Pi(l, l + 1) with one combine. Otherwise h≥1h \ge 1, and by Lemma 4 the blocks p<qp < q are in one segment of layer jj. By Lemma 2, SUFj[l]=Π(l,enj(p))\mathit{SUF}_j[l] = \Pi(l, \mathrm{en}_j(p)) and PREj[r]=Π(stj(q),r)\mathit{PRE}_j[r] = \Pi(\mathrm{st}_j(q), r); both blocks are whole blocks of 2bj2^{b_j} indices except possibly block qq, which may be clipped at n−1≥rn - 1 \ge r, so these ranges are as stated. If q=p+1q = p + 1, then enj(p)+1=stj(q)\mathrm{en}_j(p) + 1 = \mathrm{st}_j(q), and line L10 returns Π(l,r)\Pi(l, r) by (1), with one combine. If q>p+1q > p + 1, the blocks p+1p + 1 and q−1q - 1 lie in the same segment, between pp and qq, and by Lemma 3 BTWj[p+1,q−1]=Π(stj(p+1),enj(q−1))\mathit{BTW}_j[p + 1, q - 1] = \Pi(\mathrm{st}_j(p + 1), \mathrm{en}_j(q - 1)). The three ranges are consecutive and cover [l,r][l, r] in order, so line L11 returns Π(l,r)\Pi(l, r) by (1) twice, with two combines. No step exchanges two factors, so commutativity is not used. □\square

Theorem. After BUILD on A[0…n−1]A[0 \ldots n - 1] over any semigroup, QUERY(l,r)(l, r) returns A[l]⊕⋯⊕A[r]A[l] \oplus \cdots \oplus A[r] with at most two applications of ⊕\oplus, for all 0≤l≤r≤n−10 \le l \le r \le n - 1, and all procedures end.

Proof. Lemmas 1 to 5 and the termination argument. ■\blacksquare

Back to Sqrt tree