Algorithm Search⌘K

Proofs: Fischer-Heun RMQ

These are the human proofs of Fischer-Heun RMQ, at full length.

The Fischer-Heun query formula returns the leftmost minimum full

This proof shows: correctness, complexity.

Let A[1…n]A[1 \ldots n] with n≥1n \ge 1 and a virtual A[0]=−∞A[0] = -\infty. The 2d-Min-heap has the nodes 0,…,n0, \ldots, n, and parent(u)\mathrm{parent}(u) for u≥1u \ge 1 is the largest p<up < u with A[p]≤A[u]A[p] \le A[u]. The children of a node are ordered by position. The DFUDS sequence is E=( desc(0) desc(1)⋯desc(n)E = \texttt{(} \, \mathrm{desc}(0) \, \mathrm{desc}(1) \cdots \mathrm{desc}(n) with desc(v)=(deg⁡(v))\mathrm{desc}(v) = \texttt{(}^{\deg(v)} \texttt{)}. Positions in EE start at 1, D[p]D[p] is the number of "(" minus the number of ")" in E[1…p]E[1 \ldots p], with D[0]=0D[0] = 0, and end(v)\mathrm{end}(v) is the position of the last symbol of desc(v)\mathrm{desc}(v).

Lemma 1 (stack build). Line L1 computes the parents in O(n)O(n) time.

Proof. Keep a stack of positions, with 0 at the bottom. Before position uu is pushed, pop every top t≥1t \ge 1 with A[t]>A[u]A[t] > A[u]; the top that remains is the parent. By induction the stack holds, from the bottom, the positions t<ut < u with A[t]<A[s]A[t] < A[s] for no s∈(t,u)s \in (t, u), that is, the positions with A[t]≤A[s]A[t] \le A[s] for all s∈(t,u)s \in (t, u), in increasing order of position and non-decreasing order of value. The largest p<up < u with A[p]≤A[u]A[p] \le A[u] has A[s]>A[u]≥A[p]A[s] > A[u] \ge A[p] for all s∈(p,u)s \in (p, u), so it is on the stack, and every stack entry above it has a value greater than A[u]A[u], else it would be a larger candidate. So the pops stop exactly at pp. The popped entries tt have a later uu with A[u]<A[t]A[u] < A[t], so they leave the stack for good. Each position is pushed and popped at most once. □\square

Lemma 2 (preorder). For u>vu > v, the node uu is in the subtree of vv exactly when A[s]≥A[v]A[s] \ge A[v] for all s∈(v,u]s \in (v, u]. So each subtree is a range [v,v+sv)[v, v + s_v) of positions, and the preorder of the 2d-Min-heap is 0,1,…,n0, 1, \ldots, n.

Proof. By induction on uu. If A[s]≥A[v]A[s] \ge A[v] for all s∈(v,u]s \in (v, u], then vv is a candidate for the parent of uu, so p′=parent(u)∈[v,u)p' = \mathrm{parent}(u) \in [v, u), and p′p' is vv or, by induction, in the subtree of vv; so is uu. Conversely, if uu is in the subtree of vv, then p′=parent(u)p' = \mathrm{parent}(u) is vv or in the subtree of vv, so A[s]≥A[v]A[s] \ge A[v] for s∈(v,p′]s \in (v, p'] by induction, and A[p′]≥A[v]A[p'] \ge A[v]. The items in (p′,u)(p', u) are greater than A[u]A[u], by the choice of the parent, and A[u]≥A[p′]≥A[v]A[u] \ge A[p'] \ge A[v], so the condition holds up to uu. The condition is closed under smaller uu, so the subtree of vv is a range starting at vv. It is {v}\{v\} together with the subtrees of the children of vv, which are disjoint ranges ordered by position, so by induction on the size the preorder that visits vv and then its children in order lists the range in increasing order. □\square

Lemma 3 (excess at the ends). D[end(v)]=P(v)D[\mathrm{end}(v)] = P(v), where P(v)=#{u>v:parent(u)≤v}P(v) = \#\{u > v : \mathrm{parent}(u) \le v\}. Also, u>vu > v has parent(u)≤v\mathrm{parent}(u) \le v exactly when A[u]<A[s]A[u] < A[s] for all s∈(v,u)s \in (v, u), that is, when uu is a strict prefix minimum of A[v+1…n]A[v + 1 \ldots n].

Proof. E[1…end(v)]E[1 \ldots \mathrm{end}(v)] has 1+∑t≤vdeg⁡(t)1 + \sum_{t \le v} \deg(t) symbols "(" and v+1v + 1 symbols ")". Every node u≥1u \ge 1 has its parent below uu, so ∑t≤vdeg⁡(t)=#{u≥1:parent(u)≤v}=v+P(v)\sum_{t \le v} \deg(t) = \#\{u \ge 1 : \mathrm{parent}(u) \le v\} = v + P(v). So D[end(v)]=1+v+P(v)−(v+1)=P(v)D[\mathrm{end}(v)] = 1 + v + P(v) - (v + 1) = P(v). The second part is the definition of the parent: the largest p<up < u with A[p]≤A[u]A[p] \le A[u] is at most vv exactly when no s∈(v,u)s \in (v, u) has A[s]≤A[u]A[s] \le A[u]. □\square

Lemma 4 (inside a description). For v≥1v \ge 1, every position pp of desc(v)\mathrm{desc}(v) other than end(v)\mathrm{end}(v) has D[p]>D[end(v−1)]D[p] > D[\mathrm{end}(v - 1)].

Proof. The symbols before end(v)\mathrm{end}(v) in desc(v)\mathrm{desc}(v) are all "(", so D[p]=D[end(v−1)]+kD[p] = D[\mathrm{end}(v - 1)] + k for some k≥1k \ge 1. □\square

Lemma 5 (matching). For u≥1u \ge 1, the ")" at end(u−1)\mathrm{end}(u - 1) matches a "(" inside desc(parent(u))\mathrm{desc}(\mathrm{parent}(u)).

Proof. The ")" that matches a "(" at qq is at the first position q′>qq' > q with D[q′]=D[q]−1D[q'] = D[q] - 1. Let p=parent(u)p = \mathrm{parent}(u) with d=deg⁡(p)d = \deg(p), let uu be the kk-th child of pp, and let qq be the position of the (d−k+1)(d - k + 1)-st "(" of desc(p)\mathrm{desc}(p). Set end(−1)=1\mathrm{end}(-1) = 1, so D[end(−1)]=1D[\mathrm{end}(-1)] = 1. Let Q=#{t>p:parent(t)<p}Q = \#\{t > p : \mathrm{parent}(t) < p\}; by Lemma 2 these tt lie outside the subtree of pp, so t>u−1t > u - 1 for all of them. By Lemma 3, D[end(p−1)]=P(p−1)=Q+1D[\mathrm{end}(p - 1)] = P(p - 1) = Q + 1, counting pp itself, so D[q]=Q+d−k+2D[q] = Q + d - k + 2.

For v=u−1v = u - 1, the nodes t>u−1t > u - 1 with parent(t)≤u−1\mathrm{parent}(t) \le u - 1 are the QQ nodes above and the children u,…u, \ldots of pp from the kk-th on; no node after u−1u - 1 has its parent in (p,u−1](p, u - 1], because those parents are in the subtrees of the first k−1k - 1 children, which end at u−1u - 1 by Lemma 2. So D[end(u−1)]=Q+d−k+1=D[q]−1D[\mathrm{end}(u - 1)] = Q + d - k + 1 = D[q] - 1.

It remains to show that the excess stays at least D[q]D[q] on (q,end(u−1))(q, \mathrm{end}(u - 1)). The positions after qq inside desc(p)\mathrm{desc}(p), other than its end, are "(" and so above D[q]D[q]. For p≤v≤u−2p \le v \le u - 2, the count P(v)P(v) includes the QQ nodes, the d−k+1d - k + 1 children of pp from uu on, and the node v+1≤u−1v + 1 \le u - 1, whose parent is at most vv; so D[end(v)]=P(v)≥D[q]D[\mathrm{end}(v)] = P(v) \ge D[q]. By Lemma 4, the positions inside desc(v)\mathrm{desc}(v) for p<v≤u−1p < v \le u - 1 are above D[end(v−1)]≥D[q]D[\mathrm{end}(v - 1)] \ge D[q]. So end(u−1)\mathrm{end}(u - 1) is the first position after qq at the excess D[q]−1D[q] - 1. □\square

Theorem (Eq. (2) of the papers). For 1≤i<j≤n1 \le i < j \le n, let x=select0(i+1)=end(i)x = \mathrm{select}_0(i + 1) = \mathrm{end}(i), y=select0(j)=end(j−1)y = \mathrm{select}_0(j) = \mathrm{end}(j - 1), and ww the leftmost minimum of D[x…y]D[x \ldots y]. If rank0(open(w))=i\mathrm{rank}_0(\mathrm{open}(w)) = i, then ii is the leftmost minimum of A[i…j]A[i \ldots j]; otherwise rank0(w)\mathrm{rank}_0(w) is. For i=ji = j the answer is ii.

Proof. The range E[x…y]E[x \ldots y] is end(i)\mathrm{end}(i) followed by desc(i+1),…,desc(j−1)\mathrm{desc}(i + 1), \ldots, \mathrm{desc}(j - 1). By Lemma 4 no position inside a description is a leftmost minimum, because the end of the previous description, which is in the range and earlier, is lower. So w=end(v)w = \mathrm{end}(v) for the least v∈[i,j−1]v \in [i, j - 1] that minimises P(v)P(v).

Let c′c' be the leftmost minimum of A[i+1…j]A[i + 1 \ldots j], and RR the set of strict prefix minima of A[c′…n]A[c' \ldots n] that lie after jj. Since A[c′]≤A[s]A[c'] \le A[s] for all s∈[c′,j]s \in [c', j], the prefix minima of A[c′…n]A[c' \ldots n] are c′c' and RR, so P(c′−1)=1+∣R∣P(c' - 1) = 1 + |R| by Lemma 3.

  • For i≤v≤c′−2i \le v \le c' - 2: A[v+1]A[v + 1] is a prefix minimum of A[v+1…n]A[v + 1 \ldots n], and c′c' is one too, since A[c′]<A[s]A[c'] < A[s] for s∈[i+1,c′)s \in [i + 1, c'). Each u∈Ru \in R is one too, since A[u]A[u] is below every item of A[c′…u−1]A[c' \ldots u - 1] and so below A[c′]A[c'], which is below every item of A[v+1…c′−1]A[v + 1 \ldots c' - 1]. These are ∣R∣+2|R| + 2 distinct nodes, so P(v)≥P(c′−1)+1P(v) \ge P(c' - 1) + 1.
  • For c′≤v≤j−1c' \le v \le j - 1: A[v+1]A[v + 1] with v+1≤jv + 1 \le j is a prefix minimum of A[v+1…n]A[v + 1 \ldots n], and each u∈Ru \in R is one too, as A[u]<A[c′]≤A[s]A[u] < A[c'] \le A[s] for s∈[v+1,j]s \in [v + 1, j] and A[u]A[u] is below the items between jj and uu. So P(v)≥1+∣R∣=P(c′−1)P(v) \ge 1 + |R| = P(c' - 1).

So w=end(c′−1)w = \mathrm{end}(c' - 1) and rank0(w)=c′\mathrm{rank}_0(w) = c'. The items A[i+1…c′−1]A[i + 1 \ldots c' - 1] are greater than A[c′]A[c'], so parent(c′)≤i\mathrm{parent}(c') \le i. By Lemma 5, open(w)\mathrm{open}(w) lies in desc(parent(c′))\mathrm{desc}(\mathrm{parent}(c')), and rank0\mathrm{rank}_0 of a position inside desc(t)\mathrm{desc}(t), before its end, is tt. So the test of line L7 holds exactly when parent(c′)=i\mathrm{parent}(c') = i, that is, when A[i]≤A[c′]A[i] \le A[c'], and then ii is the leftmost minimum of A[i…j]A[i \ldots j]. Otherwise parent(c′)<i\mathrm{parent}(c') < i, so A[i]>A[c′]A[i] > A[c'], and c′c' is the leftmost minimum of A[i…j]A[i \ldots j]. □\square

Complexity. The build is O(n)O(n): Lemma 1, one pass to write EE, and directories built in one pass each. EE has 2n+22n + 2 bits. With the o(n)o(n)-bit directories for rank, select, the leftmost ±1 range minimum on DD and parenthesis matching described by Fischer and Heun, each of the O(1)O(1) steps of a query is O(1)O(1) on the word RAM, so a query is O(1)O(1) and the structure has 2n+o(n)2n + o(n) bits. □\square

Back to Fischer-Heun RMQ