Algorithm Search⌘K

Proofs: Shiloach-Vishkin merging

These are the human proofs of Shiloach-Vishkin merging, at full length.

Shiloach-Vishkin merging 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]: the items in the strict order where uu comes before vv if uu has the 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. 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].

Lemma 1. The cuts are splits

Proof. Let (i,j)(i, j) be an A-cut, with jj the number of items of BB less than A[i]A[i]. The items before A[i]A[i] in CC are A[0…i−1]A[0 \ldots i-1], and the items B[ℓ]B[\ell] with B[ℓ]<A[i]B[\ell] < A[i], which are B[0…j−1]B[0 \ldots j-1] because BB is sorted. So A[i]A[i] is at position i+ji + j, and the first i+ji + j items are those. A B-cut (i,j)(i, j), with ii the number of items of AA at most B[j]B[j], is the same with the roles swapped: the items before B[j]B[j] are B[0…j−1]B[0 \ldots j-1] and the items of AA at most B[j]B[j], which are A[0…i−1]A[0 \ldots i-1]. The pairs (0,0)(0, 0) and (m,n)(m, n) are splits too. □\square

Lemma 2. Splits form a chain

If (i,j)(i, j) and (i′,j′)(i', j') are splits with i+j≤i′+j′i + j \le i' + j', then i≤i′i \le i' and j≤j′j \le j'. Two different splits have different sums i+ji + j.

Proof. The first i+ji + j items of CC are a prefix of the first i′+j′i' + j' items, so A[0…i−1]A[0 \ldots i-1] is part of A[0…i′−1]A[0 \ldots i'-1] and B[0…j−1]B[0 \ldots j-1] of B[0…j′−1]B[0 \ldots j'-1]. If i+j=i′+j′i + j = i' + j', the two prefixes are the same set, so i=i′i = i' and j=j′j = j'. □\square

Lemma 3. Line L4 gives the order

Let a=⌈m/p⌉a = \lceil m/p \rceil and b=⌈n/p⌉b = \lceil n/p \rceil. The number of B-cuts that come before the A-cut (i,j)(i, j) in the order of Lemma 2 is max⁡(0,⌈j/b⌉−1)\max(0, \lceil j/b \rceil - 1), and the number of A-cuts that come before the B-cut (i,j)(i, j) is max⁡(0,⌈i/a⌉−1)\max(0, \lceil i/a \rceil - 1). No A-cut equals a B-cut.

Proof. A B-cut with index tbt b comes before the A-cut (i,j)(i, j) if and only if B[tb]B[t b] is among the first i+ji + j items of CC, that is, if and only if tb<jt b < j by Lemma 1. The number of integers t≥1t \ge 1 with tb<jt b < j is max⁡(0,⌈j/b⌉−1)\max(0, \lceil j/b \rceil - 1), and all of them satisfy tb<nt b < n and t≤p−1t \le p - 1, because j≤n≤pbj \le n \le p b. The count for a B-cut is the same with the roles swapped. If an A-cut and a B-cut were the same pair (i,j)(i, j), then B[j]B[j] would not be less than A[i]A[i] (because only jj items of BB are less than A[i]A[i]) and A[i]A[i] would not be at most B[j]B[j] (because only ii items of AA are at most B[j]B[j]), which is impossible. □\square

So the place that line L4 gives to a cut is its index among the cuts of its own list plus the number of cuts of the other list before it, which is its index in the sorted order of all cuts. The places are distinct, and SS is the list of all splits in increasing order.

Lemma 4. Each piece is a merge of two parts

Let (i,j)(i, j) and (i′,j′)(i', j') be consecutive in SS. Then C[i+j…i′+j′−1]C[i + j \ldots i' + j' - 1] is the stable merge of A[i…i′−1]A[i \ldots i'-1] and B[j…j′−1]B[j \ldots j'-1], and it holds at most aa items of AA and at most bb items of BB.

Proof. By Lemma 2, i≤i′i \le i' and j≤j′j \le j'. The first i′+j′i' + j' items of CC are A[0…i′−1]A[0 \ldots i'-1] and B[0…j′−1]B[0 \ldots j'-1], and the first i+ji + j are A[0…i−1]A[0 \ldots i-1] and B[0…j−1]B[0 \ldots j-1]. So the items in between are A[i…i′−1]A[i \ldots i'-1] and B[j…j′−1]B[j \ldots j'-1], in the order of CC, which on these items is their stable merge. If the part of AA had more than aa items, it would contain an index tat a with i<ta<i′i < t a < i' and ta<mt a < m, and then the A-cut tt would be a split strictly between the two consecutive splits, by Lemma 2. The same holds for BB. □\square

Theorem

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

Proof. By Lemma 3, SS lists (0,0)(0, 0), all cuts and (m,n)(m, n) in increasing order of i+ji + j. The pieces between consecutive splits cover the positions 0,…,m+n−10, \ldots, m + n - 1 of CC exactly once, and by Lemma 4 line L7 writes the correct items to each piece. □\square

Back to Shiloach-Vishkin merging