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 -bit integers; is bit of counted from the most significant, and is the integer formed by its first bits. An occurrence is a position of ; we follow each occurrence through the levels.
Fact 1 (the move). Let be a position of . The occurrence at is at position in , with if and if . Moreover, for , the occurrences of with bit 0 are exactly the first positions of , and those with bit 1 are the positions .
Proof. lists the occurrences with bit 0 in their order in , in positions , then those with bit 1 in their order, in positions . An occurrence with bit 0 at position is preceded among the 0-occurrences by those at the positions below with bit 0, which are many; symmetrically for 1. The second part is the same count for a prefix.
For write if and if ; by Fact 1, .
Fact 2 (blocks). For each and each -bit prefix , the occurrences whose symbol has form a contiguous block of , listed in their order in . Moreover, for and , the occurrences of the block at positions below whose bit is are exactly those at the positions of ; in particular .
Proof. By induction on . For the only prefix is empty and the block is all of . Assume the first claim for and take the block of . The positions in with bit are, in increasing order, . By Fact 1, the positions of with bit , taken in increasing order, go to consecutive positions of starting at , and those below a position go exactly to the positions below . So are the positions , in this order. With , this shows that the occurrences with prefix , which are those of the block of with bit , form a contiguous block of starting at , still in the order of . This proves the first claim for and the second claim for .
Access. By Fact 1, lines L3 and L4 move to the position of the same occurrence one level down, and append its bit to . After levels, has all bits of the symbol, from the most significant.
Rank. Let be the input, and . Invariant before level : , , and the occurrences of the block of at positions of are exactly the occurrences in whose symbol has prefix . At , , and the block is all of . Lines L8 and L9 map and with ; by Fact 2, the new holds exactly those occurrences of whose bit is , and . After level , , so holds exactly the occurrences of in , and line L10 returns their number.
Select. By the rank argument with , line L11 gives . By Fact 2 the block of in lists the occurrences of in the order of , so the -th is at (line L12); it is in the block because . Going up, if the occurrence at position of came from a position of with and , which is the -th 0 of (line L14). If , it came from with and , the -th 1 (line L15). After level 0, is the position in .
Quantile. Invariant before level : is a prefix of bits, is a range inside the block of in , the multiset of the symbols in is the multiset of the symbols of with prefix , and the answer is the -th smallest of it. Initially is empty and the block is . Among the symbols with prefix , those with bit equal to 0 are all smaller than those with bit 1, and there are of them in . If the answer has bit 0 and is the -th smallest of them; line L20 maps the range by Fact 2. Otherwise it has bit 1 and is the -th smallest of the others, line L21. After levels, is the answer.