Proofs: Schroeppel-Shamir algorithm
These are the human proofs of Schroeppel-Shamir algorithm, at full length.
Schroeppel-Shamir returns a subset with sum t exactly when one exists full
This proof shows: correctness, termination.
Notation. Let be the four quarters of the positions, and let be the lists of line L1, with and sorted by sum after line L2. We write also for the sum of the -th entry. A left entry is a pair with value . A right entry is a pair with value . Let and be the sets of all left and right entries. An entry is used when it has been popped from its heap, and unused otherwise. Let and be the unused entries.
Lemma 1 (subsets and pairs). The map is a bijection from the subsets of to , and for the image .
Proof. The quarters partition the positions, so is the disjoint union of its four parts, and four subsets, one of each quarter, have exactly one union. The sum over is the sum of the sums over the four parts.
Lemma 2 (invariant H). Before each test of line L5:
- For each , there is such that the used left entries of row are exactly , and holds the entry with its value if , and no entry of row otherwise.
- If , then , and its top has the smallest value in .
The mirror statements hold for : the used right entries of row are , holds if , and its top has the largest value in .
Proof. Part 1, initialization: line L3 puts in for each , and nothing is used, so . Maintenance: changes only in line L8. It pops the top , which by part 1 is . This entry becomes used, so grows by 1, and line L8 pushes when . Other rows do not change.
Part 2: by part 1, the unused entries of row are . Since is sorted, for each . So the smallest value in is the smallest over the rows with , which is the smallest value in , which is its top. If , some row has , and holds its entry.
The proof for is the same, with sorted, the cursor moving down from in line L9, and "largest" for "smallest".
Lemma 3 (invariant S). Before each test of line L5: for each and with , both and .
Proof. Initialization: nothing is used. Maintenance: suppose the invariant holds before a pass that does not return. Then and are not empty, and by Lemma 2 and .
- Case (line L8). The pass uses only the top of , with . Suppose for some . By the invariant, , so and , a contradiction. So is in no pair with total , and the invariant still holds after is used.
- Case (line L9). The pass uses only the top of , with . If , then by the invariant, so and , a contradiction. The invariant still holds.
Lemma 4 (termination). The loop of line L5 runs at most times.
Proof. Each pass that does not return pops one entry from a heap. By Lemma 2, part 1, an entry is pushed only when it is the next unused entry of its row, so each entry is pushed at most once and popped at most once. Each pass therefore reduces by exactly 1, and this quantity starts at and is never negative.
Theorem. SCHROEPPEL-SHAMIR stops. It returns a set with if such a set exists, and NONE otherwise.
Proof. The loop stops by Lemma 4, and lines L1 to L4 and each pass take finite time.
A returned set is correct. Line L7 returns only when , where and . The returned set is the union of the subsets of entries , and by Lemma 1 its sum is .
NONE is correct. Line L10 runs only when or is empty. By Lemma 2, part 2, and its mirror, or . By Lemma 3, every pair with total has and , so no such pair exists. By Lemma 1, no subset has sum .
If a solution exists, the algorithm returns one. The algorithm leaves the loop either by line L7, which returns a correct set, or by line L10. Line L10 is reached only when no pair has total , as shown above. So when a solution exists, line L10 is not reached, and by termination line L7 returns.