Algorithm Search⌘K

Proofs: Valiant's parallel merge

These are the human proofs of Valiant's parallel merge, at full length.

Valiant's merge computes the stable merge full

This proof shows: correctness.

Let CC be the stable merge of the sorted lists A[0…m−1]A[0 \ldots m-1] and B[0…n−1]B[0 \ldots n-1]: an item aa of AA comes before an item bb of BB if and only if b<ab < a is false, and items of one list keep their order. So A[i]A[i] is at position i+#{j:B[j]<A[i]}i + \#\{j : B[j] < A[i]\} and B[j]B[j] at position j+#{i:not B[j]<A[i]}j + \#\{i : \text{not } B[j] < A[i]\}, which is jj plus the number of items of AA at most B[j]B[j]. A pair (i,j)(i, j) is a split if the first i+ji + j items of CC are A[0…i−1]A[0 \ldots i-1] and B[0…j−1]B[0 \ldots j-1].

We prove by induction on m+nm + n that VALIANT-MERGE writes CC, where a call on parts of AA and BB writes into CC starting at a given position.

Lemma 1. Lines L1 and L2

Proof. Line L1 copies the only non-empty list. Line L2 compares every pair, so for every item it knows the number of items of the other list before it, and the position formulas above give its place. □\square

Lemma 2. Each sample gets its rank

On lines L4 and L5, each A-sample learns the number of items of BB less than it, and each B-sample the number of items of AA at most it.

Proof. Let aa be an A-sample, and let kk be the number of B-samples bb with b<ab < a, from round 1. B-samples are at the indices t−1,2t−1,…t - 1, 2t - 1, \ldots, and BB is sorted, so the items B[0…kt−1]B[0 \ldots kt - 1] are all less than aa (each is at most the kk-th B-sample, which is less than aa), and the items from index (k+1)t−1(k+1)t - 1 on are not less than aa, if that index exists. The items in between are B[kt…min⁡((k+1)t−1,n)−1]B[kt \ldots \min((k+1)t - 1, n) - 1], at most t−1t - 1 of them, and round 2 compares aa with each. So the rank of aa is ktkt plus the number of those items less than aa. For a B-sample the same argument holds with "at most" in place of "less than", since "not b<ab < a" means a≤ba \le b. □\square

Lemma 3. The samples give a chain of splits

Each sample at index ii of AA with rank jj gives a split (i,j)(i, j), and A[i]A[i] is at position i+ji + j. The same holds for B-samples. Splits with i+j≤i′+j′i + j \le i' + j' have i≤i′i \le i' and j≤j′j \le j', and different splits have different sums.

Proof. The items before A[i]A[i] in CC are A[0…i−1]A[0 \ldots i-1] and the items of BB less than A[i]A[i], which are B[0…j−1]B[0 \ldots j-1], so (i,j)(i, j) is a split. The same argument works for B-samples. For the chain property, the first i+ji + j items are a prefix of the first i′+j′i' + j' items, and two splits with equal sums describe the same set. □\square

Lemma 4. The pieces

Let (i,j)(i, j) and (i′,j′)(i', j') be splits next to each other in the sorted list of line L7. The positions i+j,…,i′+j′−1i + j, \ldots, i' + j' - 1 of CC hold the stable merge of A[i…i′−1]A[i \ldots i'-1] and B[j…j′−1]B[j \ldots j'-1]. If (i,j)(i, j) comes from an A-sample, the first of these items is A[i]A[i], and if it comes from a B-sample, it is B[j]B[j]. Each piece, after removing that item, has at most s−1s - 1 items of AA and at most t−1t - 1 items of BB.

Proof. The first part follows from Lemma 3 as in a merge by splits: the items between the two prefixes are the two parts, in the order of CC. By Lemma 3 the sample that gives the split (i,j)(i, j) is at position i+ji + j, the first position of the piece. If the remaining part of AA had ss or more items, it would contain an index i′′i'' with i<i′′<i′i < i'' < i' that is an A-sample index, because A-sample indices are ss apart. Its split would lie strictly between the two splits, by Lemma 3, which contradicts that they are next to each other. The same holds for BB. □\square

Theorem

VALIANT-MERGE writes the stable merge of AA and BB into CC.

Proof. By induction on m+nm + n. If line L1 or L2 applies, use Lemma 1. Otherwise mn>m+nm n > m + n, so m,n≥2m, n \ge 2 and s,t≥2s, t \ge 2, and there is at least one sample in each list. By Lemmas 2 and 3, line L6 writes each sample to its position in CC. By Lemma 4 the remaining positions split into pieces, each the stable merge of two parts with fewer items than m+nm + n, which line L8 writes correctly by the induction hypothesis. The pieces and the samples cover all positions of CC once. □\square

Back to Valiant's parallel merge