Algorithm Search⌘K

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 A,B,C,DA, B, C, D be the four quarters of the positions, and let LA,LB,LC,LDL_A, L_B, L_C, L_D be the lists of line L1, with LBL_B and LDL_D sorted by sum after line L2. We write LX[i]L_X[i] also for the sum of the ii-th entry. A left entry is a pair (i,j)(i, j) with value λ(i,j)=LA[i]+LB[j]\lambda(i, j) = L_A[i] + L_B[j]. A right entry is a pair (k,l)(k, l) with value ρ(k,l)=LC[k]+LD[l]\rho(k, l) = L_C[k] + L_D[l]. Let L\mathcal{L} and R\mathcal{R} 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 U1⊆LU_1 \subseteq \mathcal{L} and U2⊆RU_2 \subseteq \mathcal{R} be the unused entries.

Lemma 1 (subsets and pairs). The map S↦((S∩A,S∩B),(S∩C,S∩D))S \mapsto \bigl((S \cap A, S \cap B), (S \cap C, S \cap D)\bigr) is a bijection from the subsets of {0,…,n−1}\{0, \ldots, n-1\} to L×R\mathcal{L} \times \mathcal{R}, and ∑p∈Sap=λ+ρ\sum_{p \in S} a_p = \lambda + \rho for the image (λ,ρ)(\lambda, \rho).

Proof. The quarters partition the positions, so SS is the disjoint union of its four parts, and four subsets, one of each quarter, have exactly one union. The sum over SS is the sum of the sums over the four parts. □\square

Lemma 2 (invariant H). Before each test of line L5:

  1. For each ii, there is ci∈{0,…,∣LB∣}c_i \in \{0, \ldots, |L_B|\} such that the used left entries of row ii are exactly (i,0),…,(i,ci−1)(i, 0), \ldots, (i, c_i - 1), and H1H_1 holds the entry (i,ci)(i, c_i) with its value if ci<∣LB∣c_i < |L_B|, and no entry of row ii otherwise.
  2. If U1≠∅U_1 \neq \emptyset, then H1≠∅H_1 \neq \emptyset, and its top has the smallest value in U1U_1.

The mirror statements hold for H2H_2: the used right entries of row kk are (k,∣LD∣−1),…,(k,dk+1)(k, |L_D| - 1), \ldots, (k, d_k + 1), H2H_2 holds (k,dk)(k, d_k) if dk≥0d_k \ge 0, and its top has the largest value in U2U_2.

Proof. Part 1, initialization: line L3 puts (i,0)(i, 0) in H1H_1 for each ii, and nothing is used, so ci=0c_i = 0. Maintenance: H1H_1 changes only in line L8. It pops the top (i,j)(i, j), which by part 1 is (i,ci)(i, c_i). This entry becomes used, so cic_i grows by 1, and line L8 pushes (i,ci)(i, c_i) when ci<∣LB∣c_i < |L_B|. Other rows do not change.

Part 2: by part 1, the unused entries of row ii are (i,ci),…,(i,∣LB∣−1)(i, c_i), \ldots, (i, |L_B| - 1). Since LBL_B is sorted, λ(i,ci)≤λ(i,j)\lambda(i, c_i) \le \lambda(i, j) for each j≥cij \ge c_i. So the smallest value in U1U_1 is the smallest λ(i,ci)\lambda(i, c_i) over the rows with ci<∣LB∣c_i < |L_B|, which is the smallest value in H1H_1, which is its top. If U1≠∅U_1 \neq \emptyset, some row has ci<∣LB∣c_i < |L_B|, and H1H_1 holds its entry.

The proof for H2H_2 is the same, with LDL_D sorted, the cursor moving down from ∣LD∣−1|L_D| - 1 in line L9, and "largest" for "smallest". □\square

Lemma 3 (invariant S). Before each test of line L5: for each u∈Lu \in \mathcal{L} and v∈Rv \in \mathcal{R} with λ(u)+ρ(v)=t\lambda(u) + \rho(v) = t, both u∈U1u \in U_1 and v∈U2v \in U_2.

Proof. Initialization: nothing is used. Maintenance: suppose the invariant holds before a pass that does not return. Then H1H_1 and H2H_2 are not empty, and by Lemma 2 s1=min⁡U1λs_1 = \min_{U_1} \lambda and s2=max⁡U2ρs_2 = \max_{U_2} \rho.

  • Case s1+s2<ts_1 + s_2 < t (line L8). The pass uses only the top u0u_0 of H1H_1, with λ(u0)=s1\lambda(u_0) = s_1. Suppose λ(u0)+ρ(v)=t\lambda(u_0) + \rho(v) = t for some v∈Rv \in \mathcal{R}. By the invariant, v∈U2v \in U_2, so ρ(v)≤s2\rho(v) \le s_2 and λ(u0)+ρ(v)≤s1+s2<t\lambda(u_0) + \rho(v) \le s_1 + s_2 < t, a contradiction. So u0u_0 is in no pair with total tt, and the invariant still holds after u0u_0 is used.
  • Case s1+s2>ts_1 + s_2 > t (line L9). The pass uses only the top v0v_0 of H2H_2, with ρ(v0)=s2\rho(v_0) = s_2. If λ(u)+ρ(v0)=t\lambda(u) + \rho(v_0) = t, then u∈U1u \in U_1 by the invariant, so λ(u)≥s1\lambda(u) \ge s_1 and λ(u)+ρ(v0)≥s1+s2>t\lambda(u) + \rho(v_0) \ge s_1 + s_2 > t, a contradiction. The invariant still holds. □\square

Lemma 4 (termination). The loop of line L5 runs at most ∣L∣+∣R∣=2∣A∣+∣B∣+2∣C∣+∣D∣|\mathcal{L}| + |\mathcal{R}| = 2^{|A| + |B|} + 2^{|C| + |D|} 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 ∣U1∣+∣U2∣|U_1| + |U_2| by exactly 1, and this quantity starts at ∣L∣+∣R∣|\mathcal{L}| + |\mathcal{R}| and is never negative. □\square

Theorem. SCHROEPPEL-SHAMIR(a,t)(a, t) stops. It returns a set SS with ∑p∈Sap=t\sum_{p \in S} a_p = t 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 s1+s2=ts_1 + s_2 = t, where s1=λ(i,j)s_1 = \lambda(i, j) and s2=ρ(k,l)s_2 = \rho(k, l). The returned set is the union of the subsets of entries LA[i],LB[j],LC[k],LD[l]L_A[i], L_B[j], L_C[k], L_D[l], and by Lemma 1 its sum is s1+s2=ts_1 + s_2 = t.

NONE is correct. Line L10 runs only when H1H_1 or H2H_2 is empty. By Lemma 2, part 2, and its mirror, U1=∅U_1 = \emptyset or U2=∅U_2 = \emptyset. By Lemma 3, every pair (u,v)(u, v) with total tt has u∈U1u \in U_1 and v∈U2v \in U_2, so no such pair exists. By Lemma 1, no subset has sum tt.

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 tt, as shown above. So when a solution exists, line L10 is not reached, and by termination line L7 returns. ■\blacksquare

Back to Schroeppel-Shamir algorithm