Proofs: Bender-Farach-Colton RMQ
These are the human proofs of Bender-Farach-Colton RMQ, at full length.
The Bender-Farach-Colton RMQ returns the leftmost minimum in O(1) after O(n) preprocessing full
This proof shows: correctness, complexity.
Let with , and let be its Cartesian tree with leftmost minima: the root of the tree of a range is the least position with , its left subtree is the tree of and its right subtree the tree of . The subtree of each node is a range of positions with .
Lemma 1 (stack build). Line L1 builds .
Proof. After step the stack holds the right spine of the Cartesian tree of , from the root down, with values non-decreasing and, for equal values, earlier positions higher. When arrives, the popped nodes are those with values greater than ; they form the bottom of the spine, and they belong in the left subtree of , since is the leftmost minimum of the range they and their left subtrees cover, together with . The node left on top, if any, has a value at most , and it is earlier, so it is the parent of with as its right child. This gives the Cartesian tree of . Each position is pushed and popped at most once, so the build takes time.
Lemma 2 (RMQ to LCA). For , the lowest common ancestor of and in is the leftmost minimum of .
Proof. The nodes and are in the subtree of , so . If , both and are in the right subtree of , and if both are in the left subtree; either way a child of would be a common ancestor, which contradicts the choice of . So . As is the leftmost minimum of , it is a minimum of , and no position of has an equal value, since such a position would be a minimum of further left.
Lemma 3 (LCA to ±1 RMQ). Let be the tour, the depths and the first indices of line L2, and let and . Then for all , and the lowest common ancestor of and is the only node of least depth among .
Proof. Consecutive tour entries are a parent and a child, so their depths differ by one. The walk enters the subtree of at and leaves it after all of its entries, so lies in the subtree of , and every node there has depth at least . If or , then . Otherwise and are in different child subtrees of , and the walk returns to between finishing the first one and entering the second, so occurs in . Every other node of the subtree is deeper.
Lemma 4 (normalized blocks). If two blocks and have the same length and the same signature, then for all , so for all the leftmost minimum of the offsets is the same in both. There are at most signatures of full blocks.
Proof. is the sum of the first steps, which the signature fixes. Adding a constant to all items does not change where the leftmost minimum is. A full block has steps, each one bit.
Theorem 1 (correctness). QUERY returns the leftmost minimum of .
Proof. By Lemmas 2 and 3 it is enough that is a position of least depth in , because then . If and are in one block, line L9 reads the table of its signature, which by Lemma 4 holds the minimum of the offsets of the block. Otherwise is the disjoint union of the end of block , the whole blocks and the start of block . The two ends come from the tables, and the middle from the sparse table, whose two overlapping ranges have the minimum of all the middle blocks because the minimum is idempotent. The least of the candidates is a minimum of .
Theorem 2 (cost). Preprocessing takes time and words, and a query takes time on the word RAM.
Proof. Lines L1 and L2 take by Lemma 1 and because the tour has entries. Line L4 reads each depth once. With , the number of blocks is for , so the sparse table of line L5 has entries and takes as long to build. By Lemma 4 at most tables are built at line L6, each of entries in time, which is . A query does a constant number of array reads, one division, one bit length of a word, and at most three comparisons.