Proofs: Horowitz-Sahni algorithm
These are the human proofs of Horowitz-Sahni algorithm, at full length.
The Horowitz-Sahni algorithm finds a subset with the target sum when one exists full
This proof shows: correctness, termination.
We use the pseudocode of the page. For a set of indices, write . A list is sorted when its sums are in non-decreasing order.
Lemma 1 (SORTED-SUMS). Let . SORTED-SUMS returns a sorted list that holds the entry exactly once for each subset , and no other entry.
Proof. We show by induction on the number of iterations of line L12 this invariant: before the iteration with , the list is sorted and holds exactly once for each , and nothing else.
Base. Before the first iteration, line L11 gives . The only subset of the empty set is , with . A list with one entry is sorted.
Step. Assume the invariant before the iteration with . Line L13 maps each entry of to . Since , the new entry is . The map adds the same number to each sum, so it keeps the order of the sums, and is sorted. The subsets of are of two kinds: those without , which are the sets of , and those with , which are the sets of . Each subset is of exactly one kind and is made from exactly one . MERGE of two sorted lists returns a sorted list with the entries of both, each once (see merging sorted lists). So after line L14 the invariant holds for .
End. The loop runs for , a finite range, so it stops. After the last iteration, the invariant with is the claim.
Lemma 2 (the split). Let , and . For each there is exactly one pair with , and , namely and . Then .
Proof. and are disjoint and their union is . So , and any pair with the two conditions must have and . The sum over a disjoint union is the sum of the two sums.
By Lemma 1 and Lemma 2, there is a set with if and only if there are positions and with . Call such a pair of positions a solution pair.
Lemma 3 (the scan). At each test of line L5, the following invariant holds:
(P) No solution pair has or .
Initialization. Line L4 sets and . No position is less than 0 and no position of is more than , so (P) holds.
Maintenance. Assume (P) and that the loop body runs, so and . Let (line L6). If , line L7 returns, and there is no next test. If , take any . If , then is not a solution pair by (P). If , then because is sorted, so . So no solution pair has , and line L8 sets with (P) still true. If , take any . If , then is not a solution pair by (P). If , then because is sorted, so . So no solution pair has , and line L9 sets with (P) still true.
Termination. Let . At line L4, . Each iteration that does not return increases by exactly 1. While the loop runs, and , so at each test that enters the loop. So the loop runs at most times.
Theorem. HOROWITZ-SAHNI stops. It returns a set with if such a set exists, and NONE otherwise.
Proof. Termination is shown above. Lemma 1 holds for lines L2 and L3, so and are sorted and hold the subsets of the two halves.
Case 1: line L7 returns. Then . By Lemma 1, with and with . By Lemma 2, has .
Case 2: line L10 returns NONE. The loop stopped, so or . By (P), no solution pair has in the first case, or in the second case. Each position of is less than , and each position of is more than . So there is no solution pair, and by Lemma 2 there is no set with .
In both cases the result is right. If a set with sum exists, Case 2 is impossible, so the algorithm returns such a set.