Algorithm Search⌘K

Proofs: Range min-max tree

These are the human proofs of Range min-max tree, at full length.

Correctness of the range min-max tree searches full

This proof shows: correctness.

Let EE be the excess of PP, with E(−1)=0E(-1) = 0. For a tree node vv with range [s,t][s, t], the stored values are e(v)=E(t)−E(s−1)e(v) = E(t) - E(s-1), m(v)=min⁡s≤k≤t(E(k)−E(s−1))m(v) = \min_{s \le k \le t} (E(k) - E(s-1)) and M(v)=max⁡s≤k≤t(E(k)−E(s−1))M(v) = \max_{s \le k \le t} (E(k) - E(s-1)); padding leaves have empty ranges, m=+∞m = +\infty and M=−∞M = -\infty.

Lemma 1 (the build). If vv has children ℓ\ell over [s,q][s, q] and rr over [q+1,t][q+1, t], then e(v)=e(ℓ)+e(r)e(v) = e(\ell) + e(r), m(v)=min⁡(m(ℓ),e(ℓ)+m(r))m(v) = \min(m(\ell), e(\ell) + m(r)) and M(v)=max⁡(M(ℓ),e(ℓ)+M(r))M(v) = \max(M(\ell), e(\ell) + M(r)). For kk in [q+1,t][q+1, t], E(k)−E(s−1)=(E(q)−E(s−1))+(E(k)−E(q))=e(ℓ)+(E(k)−E(q))E(k) - E(s-1) = (E(q) - E(s-1)) + (E(k) - E(q)) = e(\ell) + (E(k) - E(q)), so the minimum over the right half, relative to E(s−1)E(s-1), is e(ℓ)+m(r)e(\ell) + m(r); the rest is immediate. Padding values are neutral for min⁡\min and max⁡\max.

Lemma 2 (discrete continuity). Let c=E(s−1)c = E(s-1). An integer xx equals E(k)E(k) for some kk in [s,t][s, t] if and only if c+m(v)≤x≤c+M(v)c + m(v) \le x \le c + M(v). Necessity is the definition of mm and MM. For sufficiency, the values E(s),…,E(t)E(s), \ldots, E(t) change by exactly ±1\pm 1 from one to the next, and they include c+m(v)c + m(v) and c+M(v)c + M(v); the walk between these two indices passes through every integer between them.

FWDSEARCH. Let t∗=E(i)+dt^* = E(i) + d and let k0k_0 be the block of ii. Line L2 checks every position after ii in block k0k_0, in order, so it returns the first occurrence if it lies there.

Otherwise, consider the climb. Let v0v_0 be the leaf of k0k_0 and v1,v2,…v_1, v_2, \ldots its ancestors. Claim: at the start of each iteration with current node vv, (a) c=E(end of the range of v)c = E(\text{end of the range of } v), and (b) no position after ii and up to the end of the range of vv has excess t∗t^*. Initially cc is set to the excess at the end of block k0k_0, and (b) is what line L2 established. If vv is a right child, the range of its parent ends where the range of vv ends, so moving up keeps (a) and (b). If vv is a left child with right sibling uu, the range of uu starts right after the range of vv, so cc is the excess just before uu. By Lemma 2, uu contains a position with excess t∗t^* if and only if the test of line L5 succeeds. If it does, every position from after ii to the end of vv is excluded by (b), so the first occurrence after ii lies in uu, and DESCEND is called with the correct cc. If it does not, the range of the parent ends where uu ends, line L6 adds e(u)e(u) to make cc the excess there, and (b) extends to the range of the parent. If the root is reached, (b) covers all of PP after ii, so there is no occurrence, and line L8 returns none.

DESCEND. Claim: at each step, cc is the excess just before the range of vv, and the first occurrence of t∗t^* in the range of the node first passed to DESCEND lies in the range of vv. Initially this holds by the above. At an internal vv with children ℓ\ell and rr, Lemma 2 tells whether ℓ\ell contains an occurrence; if it does, the first occurrence in vv is in ℓ\ell, which starts with the same cc. If not, it is in rr, and c+e(ℓ)c + e(\ell) is the excess just before rr. At a leaf, line L12 scans in order and returns the first occurrence, which exists by the claim.

BWDSEARCH. The mirror argument: scanning the block of ii leftward finds the last occurrence before ii there; on the climb, cc is the excess at the end of the stretch to the left of what has been excluded, the left sibling uu starts with excess c−e(u)c - e(u), and by Lemma 2 the test c−e(u)+m(u)≤t∗≤c−e(u)+M(u)c - e(u) + m(u) \le t^* \le c - e(u) + M(u) decides whether uu contains an occurrence; the descent prefers right children and scans the leaf from its end, keeping cc equal to the excess at the end of the current range. If the root is reached with no occurrence in [0,i−1][0, i-1], the only remaining candidate is the virtual position −1-1, whose excess is 0.

FINDCLOSE, FINDOPEN, ENCLOSE. For an opening parenthesis at ii, every position strictly inside its pair has excess at least E(i)E(i), and its matching close is the first position after ii with excess E(i)−1E(i) - 1, which is fwdsearch(i,−1)\text{fwdsearch}(i, -1). For a closing parenthesis at jj with match ii, E(i−1)=E(j)E(i - 1) = E(j) and every kk with i≤k<ji \le k < j has E(k)>E(j)E(k) > E(j), so i−1=bwdsearch(j,0)i - 1 = \text{bwdsearch}(j, 0). For an opening parenthesis at ii with E(i)=h≥2E(i) = h \ge 2, the enclosing pair opens at the greatest k<ik < i with E(k)=h−1E(k) = h - 1 and P[k]=(P[k] = (; the position k−1k - 1 then has excess h−2h - 2, and no position in [k,i−1][k, i-1] does, because the pair opened at kk is still open there, so k−1=bwdsearch(i,−2)k - 1 = \text{bwdsearch}(i, -2). If h=1h = 1, no position before ii has excess −1-1, and none is returned.

RMQ. The partial blocks are scanned exactly, and the canonical nodes of the full blocks are folded left to right with cc equal to the excess before each, so by the definition of mm the fold gives the minimum μ\mu over [i,j][i, j]. If E(i)>μE(i) > \mu, every position in [i,j][i, j] before the first one with excess μ\mu has excess above μ\mu, and the first such position after ii is fwdsearch(i,μ−E(i))\text{fwdsearch}(i, \mu - E(i)), which lies in [i,j][i, j] because μ\mu is reached there. ■\blacksquare

Back to Range min-max tree