Algorithm Search⌘K

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 TT of indices, write σ(T)=∑k∈Tak\sigma(T) = \sum_{k \in T} a_k. A list is sorted when its sums are in non-decreasing order.

Lemma 1 (SORTED-SUMS). Let lo≤hi+1lo \le hi + 1. SORTED-SUMS(a,lo,hi)(a, lo, hi) returns a sorted list that holds the entry (σ(T),T)(\sigma(T), T) exactly once for each subset T⊆{lo,…,hi}T \subseteq \{lo, \ldots, hi\}, and no other entry.

Proof. We show by induction on the number of iterations of line L12 this invariant: before the iteration with kk, the list SS is sorted and holds (σ(T),T)(\sigma(T), T) exactly once for each T⊆{lo,…,k−1}T \subseteq \{lo, \ldots, k-1\}, and nothing else.

Base. Before the first iteration, line L11 gives S=[(0,∅)]S = [(0, \emptyset)]. The only subset of the empty set is ∅\emptyset, with σ(∅)=0\sigma(\emptyset) = 0. A list with one entry is sorted.

Step. Assume the invariant before the iteration with kk. Line L13 maps each entry (s,T)(s, T) of SS to (s+ak,T∪{k})(s + a_k, T \cup \{k\}). Since k∉Tk \notin T, the new entry is (σ(T∪{k}),T∪{k})(\sigma(T \cup \{k\}), T \cup \{k\}). The map adds the same number aka_k to each sum, so it keeps the order of the sums, and S′S' is sorted. The subsets of {lo,…,k}\{lo, \ldots, k\} are of two kinds: those without kk, which are the sets TT of SS, and those with kk, which are the sets T∪{k}T \cup \{k\} of S′S'. Each subset is of exactly one kind and is made from exactly one TT. 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 k+1k + 1.

End. The loop runs for k=lo,…,hik = lo, \ldots, hi, a finite range, so it stops. After the last iteration, the invariant with k=hi+1k = hi + 1 is the claim. □\square

Lemma 2 (the split). Let h=⌊n/2⌋h = \lfloor n/2 \rfloor, L={1,…,h}L = \{1, \ldots, h\} and U={h+1,…,n}U = \{h+1, \ldots, n\}. For each I⊆{1,…,n}I \subseteq \{1, \ldots, n\} there is exactly one pair (TA,TB)(T_A, T_B) with TA⊆LT_A \subseteq L, TB⊆UT_B \subseteq U and I=TA∪TBI = T_A \cup T_B, namely TA=I∩LT_A = I \cap L and TB=I∩UT_B = I \cap U. Then σ(I)=σ(TA)+σ(TB)\sigma(I) = \sigma(T_A) + \sigma(T_B).

Proof. LL and UU are disjoint and their union is {1,…,n}\{1, \ldots, n\}. So I=(I∩L)∪(I∩U)I = (I \cap L) \cup (I \cap U), and any pair with the two conditions must have TA=I∩LT_A = I \cap L and TB=I∩UT_B = I \cap U. The sum over a disjoint union is the sum of the two sums. □\square

By Lemma 1 and Lemma 2, there is a set II with σ(I)=t\sigma(I) = t if and only if there are positions pp and qq with A[p].s+B[q].s=tA[p].s + B[q].s = t. 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 (p,q)(p, q) has p<ip < i or q>jq > j.

Initialization. Line L4 sets i=0i = 0 and j=∣B∣−1j = |B| - 1. No position is less than 0 and no position of BB is more than ∣B∣−1|B| - 1, so (P) holds.

Maintenance. Assume (P) and that the loop body runs, so 0≤i<∣A∣0 \le i < |A| and 0≤j<∣B∣0 \le j < |B|. Let s=A[i].s+B[j].ss = A[i].s + B[j].s (line L6). If s=ts = t, line L7 returns, and there is no next test. If s<ts < t, take any qq. If q>jq > j, then (i,q)(i, q) is not a solution pair by (P). If q≤jq \le j, then B[q].s≤B[j].sB[q].s \le B[j].s because BB is sorted, so A[i].s+B[q].s≤s<tA[i].s + B[q].s \le s < t. So no solution pair has p=ip = i, and line L8 sets i←i+1i \leftarrow i + 1 with (P) still true. If s>ts > t, take any pp. If p<ip < i, then (p,j)(p, j) is not a solution pair by (P). If p≥ip \ge i, then A[p].s≥A[i].sA[p].s \ge A[i].s because AA is sorted, so A[p].s+B[j].s≥s>tA[p].s + B[j].s \ge s > t. So no solution pair has q=jq = j, and line L9 sets j←j−1j \leftarrow j - 1 with (P) still true. □\square

Termination. Let Φ=i+(∣B∣−1−j)\Phi = i + (|B| - 1 - j). At line L4, Φ=0\Phi = 0. Each iteration that does not return increases Φ\Phi by exactly 1. While the loop runs, i≤∣A∣−1i \le |A| - 1 and j≥0j \ge 0, so Φ≤∣A∣+∣B∣−2\Phi \le |A| + |B| - 2 at each test that enters the loop. So the loop runs at most ∣A∣+∣B∣−1|A| + |B| - 1 times.

Theorem. HOROWITZ-SAHNI(a,t)(a, t) stops. It returns a set I⊆{1,…,n}I \subseteq \{1, \ldots, n\} with σ(I)=t\sigma(I) = t if such a set exists, and NONE otherwise.

Proof. Termination is shown above. Lemma 1 holds for lines L2 and L3, so AA and BB are sorted and hold the subsets of the two halves.

Case 1: line L7 returns. Then A[i].s+B[j].s=tA[i].s + B[j].s = t. By Lemma 1, A[i]=(σ(TA),TA)A[i] = (\sigma(T_A), T_A) with TA⊆LT_A \subseteq L and B[j]=(σ(TB),TB)B[j] = (\sigma(T_B), T_B) with TB⊆UT_B \subseteq U. By Lemma 2, I=TA∪TBI = T_A \cup T_B has σ(I)=t\sigma(I) = t.

Case 2: line L10 returns NONE. The loop stopped, so i=∣A∣i = |A| or j=−1j = -1. By (P), no solution pair has p<∣A∣p < |A| in the first case, or q>−1q > -1 in the second case. Each position of AA is less than ∣A∣|A|, and each position of BB is more than −1-1. So there is no solution pair, and by Lemma 2 there is no set II with σ(I)=t\sigma(I) = t.

In both cases the result is right. If a set with sum tt exists, Case 2 is impossible, so the algorithm returns such a set. ■\blacksquare

Back to Horowitz-Sahni algorithm