Algorithm Search⌘K

Proofs: Wavelet tree

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

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

This proof shows: correctness.

Notation as on the page: node vv covers [av,bv)[a_v, b_v), SvS_v is the subsequence of the symbols of SS in that range, in order, and for an inner node mv=⌊(av+bv)/2⌋m_v = \lfloor (a_v + b_v)/2 \rfloor, Bv[i]=[Sv[i]≥mv]B_v[i] = [S_v[i] \ge m_v], with left child ℓ(v)\ell(v) covering [av,mv)[a_v, m_v) and right child r(v)r(v) covering [mv,bv)[m_v, b_v). Since bv−av≥2b_v - a_v \ge 2 at an inner node, av<mv<bva_v < m_v < b_v, so both children cover nonempty ranges and the descent ends at a leaf after at most ⌈lg⁡σ⌉\lceil \lg \sigma \rceil steps.

Lemma (mapping). Let vv be an inner node and 0≤i≤∣Sv∣0 \le i \le |S_v|. The symbols of Sv[0…i−1]S_v[0 \ldots i-1] that are less than mvm_v, in order, are Sℓ(v)[0…rank0(Bv,i)−1]S_{\ell(v)}[0 \ldots \text{rank}_0(B_v, i) - 1], and those that are at least mvm_v are Sr(v)[0…rank1(Bv,i)−1]S_{r(v)}[0 \ldots \text{rank}_1(B_v, i) - 1]. In particular, if i<∣Sv∣i < |S_v| and Bv[i]=0B_v[i] = 0, then Sv[i]=Sℓ(v)[rank0(Bv,i)]S_v[i] = S_{\ell(v)}[\text{rank}_0(B_v, i)], and symmetrically for 1.

Proof. Sℓ(v)S_{\ell(v)} is the subsequence of SS of the symbols in [av,mv)[a_v, m_v), which is also the subsequence of SvS_v of the symbols less than mvm_v, because [av,mv)⊆[av,bv)[a_v, m_v) \subseteq [a_v, b_v) and taking subsequences keeps order. The symbols of SvS_v less than mvm_v are those at the positions with Bv=0B_v = 0. So the first rank0(Bv,i)\text{rank}_0(B_v, i) of them are those at the positions with Bv=0B_v = 0 below ii. For the second claim, apply the first with i+1i + 1: the symbol Sv[i]S_v[i] is the last one of the prefix of length rank0(Bv,i+1)=rank0(Bv,i)+1\text{rank}_0(B_v, i+1) = \text{rank}_0(B_v, i) + 1. The right side is symmetric. □\square

Access. Invariant before each test of line L2: vv is the node covering [a,b)[a, b), 0≤i<∣Sv∣0 \le i < |S_v| and Sv[i]=S[i0]S_v[i] = S[i_0], where i0i_0 is the input. It holds at the root. At an inner node, if Bv[i]=0B_v[i] = 0 then Sv[i]<mS_v[i] < m and, by the lemma, Sv[i]=Sℓ(v)[rank0(Bv,i)]S_v[i] = S_{\ell(v)}[\text{rank}_0(B_v, i)]; line L4 moves to exactly that node and position. Line L5 is symmetric. At a leaf, [a,b)={a}[a, b) = \{a\}, and every symbol of SvS_v is aa, so S[i0]=aS[i_0] = a, which line L6 returns.

Rank. Let c∈[0,σ)c \in [0, \sigma) and 0≤i0≤n0 \le i_0 \le n. Invariant: c∈[a,b)c \in [a, b), vv covers [a,b)[a, b), and the number of cc in S[0…i0−1]S[0 \ldots i_0 - 1] equals the number of cc in Sv[0…i−1]S_v[0 \ldots i - 1]. At the root it is trivial. At an inner node with c<mc < m, all copies of cc in Sv[0…i−1]S_v[0 \ldots i-1] are less than mm, so by the lemma they are exactly the copies of cc in Sℓ(v)[0…rank0(Bv,i)−1]S_{\ell(v)}[0 \ldots \text{rank}_0(B_v, i) - 1]; line L10 keeps the invariant, and line L11 is symmetric. At a leaf, every symbol of SvS_v is cc, so the count is ii, which line L12 returns.

Select. Claim: for a node vv with c∈[av,bv)c \in [a_v, b_v) and 1≤j≤1 \le j \le the number of cc in SvS_v, SELECT(v,av,bv,c,j)(v, a_v, b_v, c, j) returns the position in SvS_v of the jj-th cc of SvS_v. By induction on the height of vv. At a leaf, all symbols are cc and the jj-th is at j−1j - 1 (line L13). At an inner node with c<mvc < m_v, the copies of cc in SvS_v all lie in Sℓ(v)S_{\ell(v)} in the same order, so by induction $p = $ SELECT(ℓ(v),…)(\ell(v), \ldots) is the position in Sℓ(v)S_{\ell(v)} of the jj-th cc. By the lemma, position pp of Sℓ(v)S_{\ell(v)} is the position qq of SvS_v with Bv[q]=0B_v[q] = 0 and rank0(Bv,q)=p\text{rank}_0(B_v, q) = p, which is the (p+1)(p+1)-th 0 of BvB_v, that is select0(Bv,p+1)\text{select}_0(B_v, p + 1), line L15. Line L16 is symmetric. At the root, Sv=SS_v = S.

Quantile. Invariant: vv covers [a,b)[a, b), the multiset MM of the symbols of Sv[l…r−1]S_v[l \ldots r-1] is the multiset of the symbols of S[l0…r0−1]S[l_0 \ldots r_0 - 1] that lie in [a,b)[a, b), and the answer, the k0k_0-th smallest of S[l0…r0−1]S[l_0 \ldots r_0 - 1], is the kk-th smallest of MM (in particular 1≤k≤∣M∣1 \le k \le |M|). Initially it holds. At an inner node, by the lemma applied to rr and to ll, the symbols of Sv[l…r−1]S_v[l \ldots r - 1] less than mm are those of Sℓ(v)[rank0(Bv,l)…rank0(Bv,r)−1]S_{\ell(v)}[\text{rank}_0(B_v, l) \ldots \text{rank}_0(B_v, r) - 1], and there are z=rank0(Bv,r)−rank0(Bv,l)z = \text{rank}_0(B_v, r) - \text{rank}_0(B_v, l) of them; the others are in the corresponding range of Sr(v)S_{r(v)}. In sorted order, the zz symbols less than mm come first. If k≤zk \le z, the kk-th smallest of MM is the kk-th smallest of the left part, line L20. Otherwise it is the (k−z)(k - z)-th smallest of the right part, line L21. At a leaf MM consists of copies of aa, and k≤∣M∣k \le |M|, so the answer is aa, line L22. ■\blacksquare

Back to Wavelet tree