Algorithm Search⌘K

Proofs: Elias-Fano encoding

These are the human proofs of Elias-Fano encoding, at full length.

Elias-Fano access and next-geq are correct, and the space bound full

This proof shows: correctness, space.

Let 0≤x0≤⋯≤xn−1<u0 \le x_0 \le \cdots \le x_{n-1} < u, let ℓ\ell be as in line L1, and write xi=hi2ℓ+lix_i = h_i 2^\ell + l_i with 0≤li<2ℓ0 \le l_i < 2^\ell. Then hi=⌊xi/2ℓ⌋≤⌊(u−1)/2ℓ⌋h_i = \lfloor x_i / 2^\ell \rfloor \le \lfloor (u-1)/2^\ell \rfloor, and h0≤h1≤⋯≤hn−1h_0 \le h_1 \le \cdots \le h_{n-1} because ⌊⋅/2ℓ⌋\lfloor \cdot / 2^\ell \rfloor is monotone.

Lemma 1. The 1s of HH are exactly at the positions qi=hi+iq_i = h_i + i, these are strictly increasing in ii, and they lie inside HH.

Proof. Line L4 sets the bits qiq_i and no others. qi+1−qi=hi+1−hi+1≥1q_{i+1} - q_i = h_{i+1} - h_i + 1 \ge 1. The largest is qn−1≤⌊(u−1)/2ℓ⌋+n−1q_{n-1} \le \lfloor (u-1)/2^\ell \rfloor + n - 1, less than the length of HH. □\square

Access. By Lemma 1 the (i+1)(i+1)-th 1 of HH, in increasing order, is at qiq_i, so SELECT1H(i+1)−i=hi_H(i+1) - i = h_i. Line L5 returns hi2ℓ+Lo[i]=hi2ℓ+li=xih_i 2^\ell + Lo[i] = h_i 2^\ell + l_i = x_i.

Lemma 2. For h≥0h \ge 0 with h≤⌊(u−1)/2ℓ⌋h \le \lfloor (u-1)/2^\ell \rfloor, let ph=0p_h = 0 if h=0h = 0 and ph=SELECT0H(h)+1p_h = \text{SELECT0}_H(h) + 1 otherwise. Then ph−h=∣{i:hi<h}∣p_h - h = |\{i : h_i < h\}|.

Proof. For h=0h = 0 both sides are 0. Let h≥1h \ge 1. HH has ⌊(u−1)/2ℓ⌋+1≥h+1\lfloor (u-1)/2^\ell \rfloor + 1 \ge h + 1 zeros, since it has nn ones and length n+⌊(u−1)/2ℓ⌋+1n + \lfloor (u-1)/2^\ell \rfloor + 1, so the hh-th 0 exists. Before position qiq_i there are exactly qi−i=hiq_i - i = h_i zeros, because the other ii positions before qiq_i hold the 1s q0,…,qi−1q_0, \ldots, q_{i-1}. So qiq_i comes before the hh-th 0 if and only if hi<hh_i < h: if hi<hh_i < h, at most h−1h - 1 zeros precede qiq_i; if hi≥hh_i \ge h, the hh-th 0 precedes qiq_i. The positions 0,…,ph−10, \ldots, p_h - 1 contain exactly hh zeros, and the ones among them are the qiq_i with hi<hh_i < h. Hence ph=h+∣{i:hi<h}∣p_h = h + |\{i : h_i < h\}|. □\square

Next-geq. Let 0≤x<u0 \le x < u, h=⌊x/2ℓ⌋h = \lfloor x / 2^\ell \rfloor, and let i∗=min⁡{i:xi≥x}i^* = \min\{i : x_i \ge x\}, or nn if no such ii exists. Line L8 sets i=ph−hi = p_h - h, which by Lemma 2 is the number of elements with hi<hh_i < h; by monotonicity these are x0,…,xi−1x_0, \ldots, x_{i-1}. Each of them satisfies xj<h2ℓ≤xx_j < h 2^\ell \le x, so i≤i∗i \le i^*. The loop of line L9 increases ii while xi<xx_i < x; since the sequence is nondecreasing, the elements xi,xi+1,…,xi∗−1x_i, x_{i+1}, \ldots, x_{i^*-1} are all less than xx, and xi∗≥xx_{i^*} \ge x or i∗=ni^* = n. So the loop stops exactly at i=i∗i = i^*, which line L10 returns.

Space. The low bits take nℓn \ell bits, and HH has nn ones and Z=⌊(u−1)/2ℓ⌋+1Z = \lfloor (u-1)/2^\ell \rfloor + 1 zeros. If u≥n≥1u \ge n \ge 1 and ℓ=⌊lg⁡(u/n)⌋\ell = \lfloor \lg(u/n) \rfloor, then ℓ+1>lg⁡(u/n)\ell + 1 > \lg(u/n), so 2ℓ>u/(2n)2^\ell > u/(2n) and Z≤u/2ℓ+1<2n+1Z \le u / 2^\ell + 1 < 2n + 1. The total is less than nℓ+3n+1n \ell + 3n + 1 bits. If instead ℓ=⌈lg⁡(u/n)⌉\ell = \lceil \lg(u/n) \rceil, then 2ℓ≥u/n2^\ell \ge u/n, Z≤n+1Z \le n + 1, and the total is at most n⌈lg⁡(u/n)⌉+2n+1n \lceil \lg(u/n) \rceil + 2n + 1. ■\blacksquare

Back to Elias-Fano encoding