Algorithm Search⌘K

Proofs: Wavelet matrix

These are the human proofs of Wavelet matrix, at full length.

Access, rank, select and quantile on the wavelet matrix are correct full

This proof shows: correctness.

Notation as on the page. Symbols are LL-bit integers; cℓc_\ell is bit ℓ\ell of cc counted from the most significant, and preℓ(c)=⌊c/2L−ℓ⌋\text{pre}_\ell(c) = \lfloor c / 2^{L - \ell} \rfloor is the integer formed by its first ℓ\ell bits. An occurrence is a position of SS; we follow each occurrence through the levels.

Fact 1 (the move). Let ii be a position of SℓS_\ell. The occurrence at Sℓ[i]S_\ell[i] is at position downℓ(i)\text{down}_\ell(i) in Sℓ+1S_{\ell+1}, with downℓ(i)=rank0(Bℓ,i)\text{down}_\ell(i) = \text{rank}_0(B_\ell, i) if Bℓ[i]=0B_\ell[i] = 0 and zℓ+rank1(Bℓ,i)z_\ell + \text{rank}_1(B_\ell, i) if Bℓ[i]=1B_\ell[i] = 1. Moreover, for 0≤i≤n0 \le i \le n, the occurrences of Sℓ[0…i−1]S_\ell[0 \ldots i-1] with bit 0 are exactly the first rank0(Bℓ,i)\text{rank}_0(B_\ell, i) positions of Sℓ+1S_{\ell+1}, and those with bit 1 are the positions zℓ,…,zℓ+rank1(Bℓ,i)−1z_\ell, \ldots, z_\ell + \text{rank}_1(B_\ell, i) - 1.

Proof. Sℓ+1S_{\ell+1} lists the occurrences with bit 0 in their order in SℓS_\ell, in positions 0,…,zℓ−10, \ldots, z_\ell - 1, then those with bit 1 in their order, in positions zℓ,…,n−1z_\ell, \ldots, n-1. An occurrence with bit 0 at position ii is preceded among the 0-occurrences by those at the positions below ii with bit 0, which are rank0(Bℓ,i)\text{rank}_0(B_\ell, i) many; symmetrically for 1. The second part is the same count for a prefix. □\square

For b∈{0,1}b \in \{0, 1\} write rankb′(i)=rank0(Bℓ,i)\text{rank}'_b(i) = \text{rank}_0(B_\ell, i) if b=0b = 0 and zℓ+rank1(Bℓ,i)z_\ell + \text{rank}_1(B_\ell, i) if b=1b = 1; by Fact 1, downℓ(i)=rankBℓ[i]′(i)\text{down}_\ell(i) = \text{rank}'_{B_\ell[i]}(i).

Fact 2 (blocks). For each ℓ\ell and each ℓ\ell-bit prefix xx, the occurrences whose symbol has preℓ=x\text{pre}_\ell = x form a contiguous block [sℓ(x),eℓ(x))[s_\ell(x), e_\ell(x)) of SℓS_\ell, listed in their order in SS. Moreover, for sℓ(x)≤i≤eℓ(x)s_\ell(x) \le i \le e_\ell(x) and b∈{0,1}b \in \{0, 1\}, the occurrences of the block at positions below ii whose bit ℓ\ell is bb are exactly those at the positions [rankb′(sℓ(x)),rankb′(i))[\text{rank}'_b(s_\ell(x)), \text{rank}'_b(i)) of Sℓ+1S_{\ell+1}; in particular sℓ+1(2x+b)=rankb′(sℓ(x))s_{\ell+1}(2x + b) = \text{rank}'_b(s_\ell(x)).

Proof. By induction on ℓ\ell. For ℓ=0\ell = 0 the only prefix is empty and the block is all of S0=SS_0 = S. Assume the first claim for ℓ\ell and take the block [s,e)[s, e) of xx. The positions in [s,i)[s, i) with bit bb are, in increasing order, i1<i2<⋯<iti_1 < i_2 < \cdots < i_t. By Fact 1, the positions of SℓS_\ell with bit bb, taken in increasing order, go to consecutive positions of Sℓ+1S_{\ell+1} starting at rankb′(0)\text{rank}'_b(0), and those below a position yy go exactly to the positions below rankb′(y)\text{rank}'_b(y). So downℓ(i1),…,downℓ(it)\text{down}_\ell(i_1), \ldots, \text{down}_\ell(i_t) are the positions rankb′(s),…,rankb′(i)−1\text{rank}'_b(s), \ldots, \text{rank}'_b(i) - 1, in this order. With i=ei = e, this shows that the occurrences with prefix 2x+b2x + b, which are those of the block of xx with bit bb, form a contiguous block of Sℓ+1S_{\ell+1} starting at rankb′(s)\text{rank}'_b(s), still in the order of SS. This proves the first claim for ℓ+1\ell + 1 and the second claim for ℓ\ell. □\square

Access. By Fact 1, lines L3 and L4 move ii to the position of the same occurrence one level down, and append its bit ℓ\ell to cc. After LL levels, cc has all LL bits of the symbol, from the most significant.

Rank. Let i0i_0 be the input, and xℓ=preℓ(c)x_\ell = \text{pre}_\ell(c). Invariant before level ℓ\ell: p=sℓ(xℓ)p = s_\ell(x_\ell), p≤i≤eℓ(xℓ)p \le i \le e_\ell(x_\ell), and the occurrences of the block of xℓx_\ell at positions [p,i)[p, i) of SℓS_\ell are exactly the occurrences in S[0…i0−1]S[0 \ldots i_0 - 1] whose symbol has prefix xℓx_\ell. At ℓ=0\ell = 0, p=0p = 0, i=i0i = i_0 and the block is all of SS. Lines L8 and L9 map pp and ii with rankcℓ′\text{rank}'_{c_\ell}; by Fact 2, the new [p,i)[p, i) holds exactly those occurrences of [p,i)[p, i) whose bit ℓ\ell is cℓc_\ell, and p=sℓ+1(xℓ+1)p = s_{\ell+1}(x_{\ell+1}). After level L−1L - 1, xL=cx_L = c, so [p,i)[p, i) holds exactly the occurrences of cc in S[0…i0−1]S[0 \ldots i_0 - 1], and line L10 returns their number.

Select. By the rank argument with i0=0i_0 = 0, line L11 gives p=sL(c)p = s_L(c). By Fact 2 the block of cc in SLS_L lists the occurrences of cc in the order of SS, so the jj-th is at q=p+j−1q = p + j - 1 (line L12); it is in the block because j≤ncj \le n_c. Going up, if cℓ=0c_\ell = 0 the occurrence at position qq of Sℓ+1S_{\ell+1} came from a position ii of SℓS_\ell with Bℓ[i]=0B_\ell[i] = 0 and rank0(Bℓ,i)=q\text{rank}_0(B_\ell, i) = q, which is the (q+1)(q+1)-th 0 of BℓB_\ell (line L14). If cℓ=1c_\ell = 1, it came from ii with Bℓ[i]=1B_\ell[i] = 1 and zℓ+rank1(Bℓ,i)=qz_\ell + \text{rank}_1(B_\ell, i) = q, the (q−zℓ+1)(q - z_\ell + 1)-th 1 (line L15). After level 0, qq is the position in S0=SS_0 = S.

Quantile. Invariant before level ℓ\ell: c=xc = x is a prefix of ℓ\ell bits, [l,r)[l, r) is a range inside the block of xx in SℓS_\ell, the multiset of the symbols in [l,r)[l, r) is the multiset of the symbols of S[l0…r0−1]S[l_0 \ldots r_0 - 1] with prefix xx, and the answer is the kk-th smallest of it. Initially xx is empty and the block is SS. Among the symbols with prefix xx, those with bit ℓ\ell equal to 0 are all smaller than those with bit 1, and there are t=rank0(Bℓ,r)−rank0(Bℓ,l)t = \text{rank}_0(B_\ell, r) - \text{rank}_0(B_\ell, l) of them in [l,r)[l, r). If k≤tk \le t the answer has bit 0 and is the kk-th smallest of them; line L20 maps the range by Fact 2. Otherwise it has bit 1 and is the (k−t)(k - t)-th smallest of the others, line L21. After LL levels, cc is the answer. ■\blacksquare

Back to Wavelet matrix