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 be the stable merge of the sorted lists and : an item of comes before an item of if and only if is false, and items of one list keep their order. So is at position and at position , which is plus the number of items of at most . A pair is a split if the first items of are and .
We prove by induction on that VALIANT-MERGE writes , where a call on parts of and writes into 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.
Lemma 2. Each sample gets its rank
On lines L4 and L5, each A-sample learns the number of items of less than it, and each B-sample the number of items of at most it.
Proof. Let be an A-sample, and let be the number of B-samples with , from round 1. B-samples are at the indices , and is sorted, so the items are all less than (each is at most the -th B-sample, which is less than ), and the items from index on are not less than , if that index exists. The items in between are , at most of them, and round 2 compares with each. So the rank of is plus the number of those items less than . For a B-sample the same argument holds with "at most" in place of "less than", since "not " means .
Lemma 3. The samples give a chain of splits
Each sample at index of with rank gives a split , and is at position . The same holds for B-samples. Splits with have and , and different splits have different sums.
Proof. The items before in are and the items of less than , which are , so is a split. The same argument works for B-samples. For the chain property, the first items are a prefix of the first items, and two splits with equal sums describe the same set.
Lemma 4. The pieces
Let and be splits next to each other in the sorted list of line L7. The positions of hold the stable merge of and . If comes from an A-sample, the first of these items is , and if it comes from a B-sample, it is . Each piece, after removing that item, has at most items of and at most items of .
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 . By Lemma 3 the sample that gives the split is at position , the first position of the piece. If the remaining part of had or more items, it would contain an index with that is an A-sample index, because A-sample indices are 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 .
Theorem
VALIANT-MERGE writes the stable merge of and into .
Proof. By induction on . If line L1 or L2 applies, use Lemma 1. Otherwise , so and , and there is at least one sample in each list. By Lemmas 2 and 3, line L6 writes each sample to its position in . By Lemma 4 the remaining positions split into pieces, each the stable merge of two parts with fewer items than , which line L8 writes correctly by the induction hypothesis. The pieces and the samples cover all positions of once.