The Fischer-Heun query formula returns the leftmost minimum full
This proof shows: correctness, complexity.
Let A[1…n] with n≥1 and a virtual A[0]=−∞. The 2d-Min-heap has the nodes 0,…,n, and parent(u) for u≥1 is the largest p<u with A[p]≤A[u]. The children of a node are ordered by position. The DFUDS sequence is E=(desc(0)desc(1)⋯desc(n) with desc(v)=(deg(v)). Positions in E start at 1, D[p] is the number of "(" minus the number of ")" in E[1…p], with D[0]=0, and end(v) is the position of the last symbol of desc(v).
Lemma 1 (stack build). Line L1 computes the parents in O(n) time.
Proof. Keep a stack of positions, with 0 at the bottom. Before position u is pushed, pop every top t≥1 with A[t]>A[u]; the top that remains is the parent. By induction the stack holds, from the bottom, the positions t<u with A[t]<A[s] for no s∈(t,u), that is, the positions with A[t]≤A[s] for all s∈(t,u), in increasing order of position and non-decreasing order of value. The largest p<u with A[p]≤A[u] has A[s]>A[u]≥A[p] for all s∈(p,u), so it is on the stack, and every stack entry above it has a value greater than A[u], else it would be a larger candidate. So the pops stop exactly at p. The popped entries t have a later u with A[u]<A[t], so they leave the stack for good. Each position is pushed and popped at most once. □
Lemma 2 (preorder). For u>v, the node u is in the subtree of v exactly when A[s]≥A[v] for all s∈(v,u]. So each subtree is a range [v,v+sv) of positions, and the preorder of the 2d-Min-heap is 0,1,…,n.
Proof. By induction on u. If A[s]≥A[v] for all s∈(v,u], then v is a candidate for the parent of u, so p′=parent(u)∈[v,u), and p′ is v or, by induction, in the subtree of v; so is u. Conversely, if u is in the subtree of v, then p′=parent(u) is v or in the subtree of v, so A[s]≥A[v] for s∈(v,p′] by induction, and A[p′]≥A[v]. The items in (p′,u) are greater than A[u], by the choice of the parent, and A[u]≥A[p′]≥A[v], so the condition holds up to u. The condition is closed under smaller u, so the subtree of v is a range starting at v. It is {v} together with the subtrees of the children of v, which are disjoint ranges ordered by position, so by induction on the size the preorder that visits v and then its children in order lists the range in increasing order. □
Lemma 3 (excess at the ends). D[end(v)]=P(v), where P(v)=#{u>v:parent(u)≤v}. Also, u>v has parent(u)≤v exactly when A[u]<A[s] for all s∈(v,u), that is, when u is a strict prefix minimum of A[v+1…n].
Proof. E[1…end(v)] has 1+∑t≤vdeg(t) symbols "(" and v+1 symbols ")". Every node u≥1 has its parent below u, so ∑t≤vdeg(t)=#{u≥1:parent(u)≤v}=v+P(v). So D[end(v)]=1+v+P(v)−(v+1)=P(v). The second part is the definition of the parent: the largest p<u with A[p]≤A[u] is at most v exactly when no s∈(v,u) has A[s]≤A[u]. □
Lemma 4 (inside a description). For v≥1, every position p of desc(v) other than end(v) has D[p]>D[end(v−1)].
Proof. The symbols before end(v) in desc(v) are all "(", so D[p]=D[end(v−1)]+k for some k≥1. □
Lemma 5 (matching). For u≥1, the ")" at end(u−1) matches a "(" inside desc(parent(u)).
Proof. The ")" that matches a "(" at q is at the first position q′>q with D[q′]=D[q]−1. Let p=parent(u) with d=deg(p), let u be the k-th child of p, and let q be the position of the (d−k+1)-st "(" of desc(p). Set end(−1)=1, so D[end(−1)]=1. Let Q=#{t>p:parent(t)<p}; by Lemma 2 these t lie outside the subtree of p, so t>u−1 for all of them. By Lemma 3, D[end(p−1)]=P(p−1)=Q+1, counting p itself, so D[q]=Q+d−k+2.
For v=u−1, the nodes t>u−1 with parent(t)≤u−1 are the Q nodes above and the children u,… of p from the k-th on; no node after u−1 has its parent in (p,u−1], because those parents are in the subtrees of the first k−1 children, which end at u−1 by Lemma 2. So D[end(u−1)]=Q+d−k+1=D[q]−1.
It remains to show that the excess stays at least D[q] on (q,end(u−1)). The positions after q inside desc(p), other than its end, are "(" and so above D[q]. For p≤v≤u−2, the count P(v) includes the Q nodes, the d−k+1 children of p from u on, and the node v+1≤u−1, whose parent is at most v; so D[end(v)]=P(v)≥D[q]. By Lemma 4, the positions inside desc(v) for p<v≤u−1 are above D[end(v−1)]≥D[q]. So end(u−1) is the first position after q at the excess D[q]−1. □
Theorem (Eq. (2) of the papers). For 1≤i<j≤n, let x=select0(i+1)=end(i), y=select0(j)=end(j−1), and w the leftmost minimum of D[x…y]. If rank0(open(w))=i, then i is the leftmost minimum of A[i…j]; otherwise rank0(w) is. For i=j the answer is i.
Proof. The range E[x…y] is end(i) followed by desc(i+1),…,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) for the least v∈[i,j−1] that minimises P(v).
Let c′ be the leftmost minimum of A[i+1…j], and R the set of strict prefix minima of A[c′…n] that lie after j. Since A[c′]≤A[s] for all s∈[c′,j], the prefix minima of A[c′…n] are c′ and R, so P(c′−1)=1+∣R∣ by Lemma 3.
- For i≤v≤c′−2: A[v+1] is a prefix minimum of A[v+1…n], and c′ is one too, since A[c′]<A[s] for s∈[i+1,c′). Each u∈R is one too, since A[u] is below every item of A[c′…u−1] and so below A[c′], which is below every item of A[v+1…c′−1]. These are ∣R∣+2 distinct nodes, so P(v)≥P(c′−1)+1.
- For c′≤v≤j−1: A[v+1] with v+1≤j is a prefix minimum of A[v+1…n], and each u∈R is one too, as A[u]<A[c′]≤A[s] for s∈[v+1,j] and A[u] is below the items between j and u. So P(v)≥1+∣R∣=P(c′−1).
So w=end(c′−1) and rank0(w)=c′. The items A[i+1…c′−1] are greater than A[c′], so parent(c′)≤i. By Lemma 5, open(w) lies in desc(parent(c′)), and rank0 of a position inside desc(t), before its end, is t. So the test of line L7 holds exactly when parent(c′)=i, that is, when A[i]≤A[c′], and then i is the leftmost minimum of A[i…j]. Otherwise parent(c′)<i, so A[i]>A[c′], and c′ is the leftmost minimum of A[i…j]. □
Complexity. The build is O(n): Lemma 1, one pass to write E, and directories built in one pass each. E has 2n+2 bits. With the o(n)-bit directories for rank, select, the leftmost ±1 range minimum on D and parenthesis matching described by Fischer and Heun, each of the O(1) steps of a query is O(1) on the word RAM, so a query is O(1) and the structure has 2n+o(n) bits. □