Proofs: Rank/select dictionary
These are the human proofs of Rank/select dictionary, at full length.
The rank/select dictionary returns rank and select correctly full
This proof shows: correctness.
We use the pseudocode and the notation of the page. lists the positions of the 1s of in increasing order, so for . The parameters and are fixed. The correctness does not depend on their values; only the time and the space do.
Lemma 1 (BUILD-SELECT). After BUILD-SELECT, for each group , with : ; if then sparse is true and ; otherwise sparse is false and, for each , with : , and is the list if , and empty otherwise.
Proof. The loop of line L1 runs once for each with , so and is not empty. Lines L2 to L4 and L6 set the fields of group as stated, since is increasing and so and . Likewise, for a dense group, the loop of line L7 runs once for each with , so is not empty, and lines L8 to L11 set and as stated. No step writes the fields of another group.
Lemma 2 (SCAN). If and no 1 lies in positions , then SCAN returns for each with .
Proof. The 1s at positions are, in order, , so the -th of them is . SCAN masks the bits below in the word of , so the words it reads contain exactly the 1s at positions , in increasing order. The loop subtracts the full counts of words that hold fewer than the remaining number of wanted 1s, as in SELECT1 of the bit vector; by the same invariant, it stops at the word that holds the wanted 1, and the clearing of the lowest bits finds it. The loop stops because , so the wanted 1 exists.
Lemma 3 (SELECT1). For , SELECT1 returns .
Proof. Write with , as line L12 does. Then is element of : the group starts at index of , and because and .
Case 1: group is sparse. By Lemma 1, , so line L13 returns .
Case 2: group is dense. Write with , as line L14 does. Element of is element of the subgroup , and by the same argument as above.
- If is not empty, by Lemma 1 it holds at index , and , so line L15 returns .
- Otherwise, line L16 calls SCAN with . is for , and , so the hypothesis of Lemma 2 holds. SCAN returns .
Lemma 4 (SELECT0). The same argument, applied to the list of the positions of the 0s and to the masked complement of the words, shows that SELECT0 returns the position of the -th 0. The mask of the last word removes the padding bits, which the complement would turn into false 0s.
Lemma 5 (RANK1). For , RANK1 returns .
Proof. With and , the build stores and . The term POPCOUNT counts the 1s at positions to . The sum is , as in the proof for Jacobson's rank structure. The build stores one word past the last full word, so exists for .
The time bound. For the default parameters a dense subgroup has span less than , and its -th 1 lies in , so SCAN reads at most words. With and , this is words for large enough, and each other line does work.