Algorithm Search⌘K

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 A[0…n−1]A[0 \ldots n-1] with n≥1n \ge 1, and let CC be its Cartesian tree with leftmost minima: the root of the tree of a range [a,b][a, b] is the least position p∈[a,b]p \in [a, b] with A[p]=min⁡A[a…b]A[p] = \min A[a \ldots b], its left subtree is the tree of [a,p−1][a, p - 1] and its right subtree the tree of [p+1,b][p + 1, b]. The subtree of each node vv is a range [av,bv][a_v, b_v] of positions with v∈[av,bv]v \in [a_v, b_v].

Lemma 1 (stack build). Line L1 builds CC.

Proof. After step ii the stack holds the right spine of the Cartesian tree of A[0…i]A[0 \ldots i], from the root down, with values non-decreasing and, for equal values, earlier positions higher. When ii arrives, the popped nodes are those with values greater than A[i]A[i]; they form the bottom of the spine, and they belong in the left subtree of ii, since ii is the leftmost minimum of the range they and their left subtrees cover, together with ii. The node left on top, if any, has a value at most A[i]A[i], and it is earlier, so it is the parent of ii with ii as its right child. This gives the Cartesian tree of A[0…i]A[0 \ldots i]. Each position is pushed and popped at most once, so the build takes O(n)O(n) time. □\square

Lemma 2 (RMQ to LCA). For l≤rl \le r, the lowest common ancestor ww of ll and rr in CC is the leftmost minimum of A[l…r]A[l \ldots r].

Proof. The nodes ll and rr are in the subtree of ww, so [l,r]⊆[aw,bw][l, r] \subseteq [a_w, b_w]. If w<lw < l, both ll and rr are in the right subtree of ww, and if w>rw > r both are in the left subtree; either way a child of ww would be a common ancestor, which contradicts the choice of ww. So w∈[l,r]w \in [l, r]. As ww is the leftmost minimum of [aw,bw]⊇[l,r][a_w, b_w] \supseteq [l, r], it is a minimum of A[l…r]A[l \ldots r], and no position of [l,w)[l, w) has an equal value, since such a position would be a minimum of [aw,bw][a_w, b_w] further left. □\square

Lemma 3 (LCA to ±1 RMQ). Let E,D,FE, D, F be the tour, the depths and the first indices of line L2, and let x=min⁡(F[u],F[v])x = \min(F[u], F[v]) and y=max⁡(F[u],F[v])y = \max(F[u], F[v]). Then ∣D[k+1]−D[k]∣=1|D[k+1] - D[k]| = 1 for all kk, and the lowest common ancestor ww of uu and vv is the only node of least depth among E[x…y]E[x \ldots y].

Proof. Consecutive tour entries are a parent and a child, so their depths differ by one. The walk enters the subtree of ww at F[w]≤xF[w] \le x and leaves it after all of its entries, so E[x…y]E[x \ldots y] lies in the subtree of ww, and every node there has depth at least depth(w)\mathrm{depth}(w). If u=wu = w or v=wv = w, then E[x]=wE[x] = w. Otherwise uu and vv are in different child subtrees of ww, and the walk returns to ww between finishing the first one and entering the second, so ww occurs in E[x…y]E[x \ldots y]. Every other node of the subtree is deeper. □\square

Lemma 4 (normalized blocks). If two blocks D[s…s+t−1]D[s \ldots s + t - 1] and D[s′…s′+t−1]D[s' \ldots s' + t - 1] have the same length and the same signature, then D[s+i]−D[s]=D[s′+i]−D[s′]D[s + i] - D[s] = D[s' + i] - D[s'] for all i<ti < t, so for all i≤k<ti \le k < t the leftmost minimum of the offsets i…ki \ldots k is the same in both. There are at most 2b−12^{b-1} signatures of full blocks.

Proof. D[s+i]−D[s]D[s + i] - D[s] is the sum of the first ii steps, which the signature fixes. Adding a constant to all items does not change where the leftmost minimum is. A full block has b−1b - 1 steps, each one bit. □\square

Theorem 1 (correctness). QUERY(l,r)(l, r) returns the leftmost minimum of A[l…r]A[l \ldots r].

Proof. By Lemmas 2 and 3 it is enough that kk is a position of least depth in D[x…y]D[x \ldots y], because then E[k]=wE[k] = w. If xx and yy are in one block, line L9 reads the table of its signature, which by Lemma 4 holds the minimum of the offsets x−jb…y−jbx - jb \ldots y - jb of the block. Otherwise [x,y][x, y] is the disjoint union of the end of block bxbx, the whole blocks bx+1,…,by−1bx + 1, \ldots, by - 1 and the start of block byby. 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 D[x…y]D[x \ldots y]. □\square

Theorem 2 (cost). Preprocessing takes O(n)O(n) time and words, and a query takes O(1)O(1) time on the word RAM.

Proof. Lines L1 and L2 take O(n)O(n) by Lemma 1 and because the tour has m=2n−1m = 2n - 1 entries. Line L4 reads each depth once. With b=max⁡(1,⌊12lg⁡m⌋)b = \max(1, \lfloor \frac12 \lg m \rfloor), the number of blocks is B=⌈m/b⌉=O(m/log⁡m)B = \lceil m / b \rceil = O(m / \log m) for m≥4m \ge 4, so the sparse table of line L5 has O(Blog⁡B)=O(m)O(B \log B) = O(m) entries and takes as long to build. By Lemma 4 at most 2b−1+1≤m+12^{b-1} + 1 \le \sqrt m + 1 tables are built at line L6, each of b2=O(log⁡2m)b^2 = O(\log^2 m) entries in O(b2)O(b^2) time, which is O(mlog⁡2m)=O(m)O(\sqrt m \log^2 m) = O(m). A query does a constant number of array reads, one division, one bit length of a word, and at most three comparisons. □\square

Back to Bender-Farach-Colton RMQ