Algorithm Search⌘K

Proofs: Merge sort tree

These are the human proofs of Merge sort tree, at full length.

The merge sort tree answers counting and k-th queries correctly full

This proof shows: correctness, complexity.

Let A[0…n−1]A[0 \ldots n-1] be an array over a totally ordered set, NN the least power of two with N≥nN \ge n, and number the nodes 1…2N−11 \ldots 2N - 1 as in the page: node v<Nv < N has the children 2v2v and 2v+12v + 1, and the leaf N+iN + i is position ii. Node vv at depth dd covers Iv=[j2K−d,(j+1)2K−d)I_v = [j 2^{K-d}, (j+1) 2^{K-d}), where K=lg⁡NK = \lg N and v=2d+jv = 2^d + j.

Lemma 1 (build). After BUILD, each SvS_v is the sorted list of the multiset Mv={A[k]:k∈Iv,k<n}M_v = \{A[k] : k \in I_v, k < n\}.

Proof. By induction on vv from 2N−12N - 1 down to 11. A leaf N+iN + i holds [A[i]][A[i]] if i<ni < n (line L7) and the empty list otherwise, which is MN+iM_{N+i} sorted. For v<Nv < N, IvI_v is the disjoint union of I2vI_{2v} and I2v+1I_{2v+1}, so Mv=M2v⊎M2v+1M_v = M_{2v} \uplus M_{2v+1}, and the merge of two sorted lists (line L8) is the sorted list of the union of their multisets. □\square

Lemma 2 (canonical nodes). For 0≤l≤r≤N0 \le l \le r \le N, the nodes at which lines L3 and L4 call LOWER have pairwise disjoint ranges whose union is [l,r)[l, r), and there are at most two of them per level.

Proof. Write l′,r′l', r' for the variables after line L1. We keep the invariant: the nodes used so far cover [l,r)[l, r) minus the union UU of IuI_u for l′≤u<r′l' \le u < r', where uu ranges over the nodes of the current level, and the used nodes are disjoint from UU. At the start nothing is used and UU is the union of the leaves N+l,…,N+r−1N + l, \ldots, N + r - 1, which is [l,r)[l, r). In one round, if l′l' is odd, node l′l' is the right child of its parent, so the node l′l' is moved from UU to the used set and l′l' becomes even; the same holds for r′r' odd with r′−1r' - 1. Now l′l' and r′r' are even, so the nodes l′,…,r′−1l', \ldots, r' - 1 pair up into the children of l′/2,…,r′/2−1l'/2, \ldots, r'/2 - 1, and line L5 keeps UU the same. The loop stops when l′≥r′l' \ge r', so UU is empty, and each round uses at most two nodes of one level. □\square

Theorem 1 (count). COUNT(l,r,x)(l, r, x) returns #{k∈[l,r):A[k]<x}\#\{k \in [l, r) : A[k] < x\}.

Proof. By Lemma 2 the positions of [l,r)[l, r) with k<nk < n are split among the used nodes, each in exactly one, and by Lemma 1 LOWER(Sv,x)(S_v, x) is the number of those positions k∈Ivk \in I_v with A[k]<xA[k] < x. Summing gives the claim. The same proof with an upper-bound search gives the count of A[k]≤xA[k] \le x. □\square

Theorem 2 (k-th). For 1≤k≤r−l1 \le k \le r - l, KTH(l,r,k)(l, r, k) returns the kk-th smallest item of A[l…r−1]A[l \ldots r-1].

Proof. Let c(v)=#{j∈[l,r):A[j]≤v}c(v) = \#\{j \in [l, r) : A[j] \le v\}; it is non-decreasing in vv. Let yy be the kk-th smallest item of the range. Then c(y)≥kc(y) \ge k, and c(v)<kc(v) < k for every v<yv < y. The root list S1S_1 is AA sorted (Lemma 1), so it contains yy, and the predicate P(m)=[c(S1[m])≥k]P(m) = [c(S_1[m]) \ge k] is monotone in mm and true at the last index n−1n - 1, since c(S1[n−1])=r−l≥kc(S_1[n-1]) = r - l \ge k. Lines L9 to L12 are a binary search for the least mm with P(m)P(m), kept by the invariant that P(hi)P(hi) holds and P(m′)P(m') fails for all m′<lom' < lo. At the least such mm, S1[m]≥yS_1[m] \ge y since PP fails below yy, and S1[m]≤yS_1[m] \le y since yy occurs in S1S_1 at some index where PP holds. So S1[m]=yS_1[m] = y. □\square

Theorem 3 (cost). BUILD takes O(nlog⁡n)O(n \log n) time and the lists have n(K+1)n (K + 1) items. COUNT takes O(log⁡2n)O(\log^2 n) time and KTH takes O(log⁡3n)O(\log^3 n).

Proof. The lists of one level hold each of the nn items once, so the merges of one level take O(n+N)O(n + N) time, and there are K+1=O(log⁡n)K + 1 = O(\log n) levels. By Lemma 2 COUNT does at most 2K2K binary searches on lists of at most nn items, O(log⁡n)O(\log n) each. KTH does ⌈lg⁡n⌉\lceil \lg n \rceil rounds of the outer search, each one count. Termination: COUNT halves r′−l′r' - l' at least every round, so it runs at most K+1K + 1 rounds, and the outer search halves hi−lohi - lo. □\square

Back to Merge sort tree