Algorithm Search⌘K

Proofs: Merge path

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

Merge path computes the stable merge full

This proof shows: correctness.

Let CC be the stable merge of the sorted arrays A[0…m−1]A[0 \ldots m-1] and B[0…n−1]B[0 \ldots n-1], in which uu comes before vv if uu has a smaller key, or the keys are equal and uu is in AA and vv in BB, or both are in the same array and uu has the smaller index.

Lemma 1. Co-ranks exist and are unique

For each kk with 0≤k≤m+n0 \le k \le m + n, there is exactly one pair (i,j)(i, j) with i+j=ki + j = k such that the first kk items of CC are A[0…i−1]A[0 \ldots i-1] and B[0…j−1]B[0 \ldots j-1]. Moreover max⁡(0,k−n)≤i≤min⁡(k,m)\max(0, k - n) \le i \le \min(k, m).

Proof. The stable order lists the items of AA in index order, so the items of AA among the first kk items of CC are a prefix A[0…i−1]A[0 \ldots i-1], and the same holds for BB with some jj. Since there are kk items in all, i+j=ki + j = k, and the pair is fixed by CC. From 0≤i≤m0 \le i \le m, 0≤j≤n0 \le j \le n and j=k−ij = k - i follows the range of ii. □\square

Lemma 2. The test on line L4

Let max⁡(0,k−n)≤i<min⁡(k,m)\max(0, k - n) \le i < \min(k, m). Then B[k−i−1]<A[i]B[k - i - 1] < A[i] holds exactly when A[i]A[i] is not among the first kk items of CC.

Proof. The bounds give 0≤k−i−1≤n−10 \le k - i - 1 \le n - 1, so B[k−i−1]B[k-i-1] exists. The items before A[i]A[i] in CC are A[0],…,A[i−1]A[0], \ldots, A[i-1] and the items of BB with key less than A[i]A[i], as items of BB with equal key come after A[i]A[i]. So A[i]A[i] is at position i+qi + q, where qq is the number of items of BB less than A[i]A[i]. It is among the first kk items when i+q<ki + q < k, that is, q≤k−i−1q \le k - i - 1. Since BB is sorted, q≥k−iq \ge k - i holds exactly when B[k−i−1]<A[i]B[k - i - 1] < A[i]. □\square

Lemma 3. CO-RANK returns the co-rank

Proof. Let (i∗,j∗)(i^*, j^*) be the co-rank of kk. By Lemma 1, A[i]A[i] is among the first kk items of CC exactly when i<i∗i < i^*. So by Lemma 2, for ii in the search range with i<min⁡(k,m)i < \min(k, m), the test on line L4 is false for i<i∗i < i^* and true for i≥i∗i \ge i^*. The loop keeps the invariant lo≤i∗≤hilo \le i^* \le hi: it holds at the start by Lemma 1; if the test is true at ii then i∗≤ii^* \le i and setting hi=ihi = i keeps it; if it is false then i∗>ii^* > i and setting lo=i+1lo = i + 1 keeps it. Each round makes hi−lohi - lo smaller, and the loop ends with lo=hi=i∗lo = hi = i^*. So line L5 returns (i∗,k−i∗)=(i∗,j∗)(i^*, k - i^*) = (i^*, j^*). □\square

Theorem. MERGE-PATH writes C

Proof. Let kt=⌊t(m+n)/p⌋k_t = \lfloor t(m+n)/p \rfloor, so 0=k0≤k1≤⋯≤kp=m+n0 = k_0 \le k_1 \le \cdots \le k_p = m + n, and let (it,jt)(i_t, j_t) be the co-rank of ktk_t. By Lemma 1, it≤it+1i_t \le i_{t+1} and jt≤jt+1j_t \le j_{t+1}, because the first ktk_t items of CC are among the first kt+1k_{t+1}. The items of C[kt…kt+1−1]C[k_t \ldots k_{t+1} - 1] are the first kt+1k_{t+1} items without the first ktk_t, that is, A[it…it+1−1]A[i_t \ldots i_{t+1} - 1] and B[jt…jt+1−1]B[j_t \ldots j_{t+1} - 1]. They appear in CC in the stable order, and the stable order of a subset is the stable order restricted to it, so they appear as the stable merge of these two pieces. Line L8 computes exactly this merge and writes it to C[it+jt…it+1+jt+1−1]=C[kt…kt+1−1]C[i_t + j_t \ldots i_{t+1} + j_{t+1} - 1] = C[k_t \ldots k_{t+1} - 1]. The ranges for different tt are disjoint and cover CC, so the processors write CC without conflicts. □\square

Back to Merge path