Exercise

Verify and Solve a Subset Sum Instance

Use binary input length correctly and prove a dynamic-programming recurrence.

Assignment sources

Noy 2025 high-quality exam preparation and community solutionsCommunity solutionפתרון מטלה 4 - סיבוכיות.pdfAssignment 4High-quality preparation and community solution — not official

Problem

For positive integers a1,…,am,Ta_1,\ldots,a_m,T, define D(i,t)D(i,t) to mean that some subset of the first ii numbers sums to tt. Derive a recurrence, solve the instance {3,5,6,11},T=14\{3,5,6,11\},T=14, and explain why the algorithm is pseudopolynomial rather than polynomial in the binary input length.

Hint 1

A solution either excludes aia_i or includes it and previously formed t−ait-a_i.

Hint 2

The table has a column for every integer from 00 through TT, while binary encoding of TT uses only Θ(log⁡T)\Theta(\log T) bits.

Full solution

Initialize D(0,0)=trueD(0,0)=\text{true} and D(0,t)=falseD(0,t)=\text{false} for t>0t>0. For i≥1i\ge1,

D(i,t)=D(i−1,t)∨(t≥ai∧D(i−1,t−ai)).D(i,t)=D(i-1,t)\lor\bigl(t\ge a_i\land D(i-1,t-a_i)\bigr).

The two disjuncts exactly partition subsets by whether they contain aia_i, proving correctness by induction on ii.

For the instance, 3+5+6=143+5+6=14, so D(4,14)D(4,14) is true; backtracking yields the selected subset {3,5,6}\{3,5,6\}.

The table has (m+1)(T+1)(m+1)(T+1) entries and can be filled in O(mT)O(mT) arithmetic/Boolean steps. This is polynomial in the numeric value TT, but TT may be exponential in its binary length. Therefore this runtime does not establish that binary SUBSET-SUM is in P. Membership in NP follows separately from a selection-bit certificate and polynomial-time binary addition.

Checkpoint

“Pseudopolynomial” describes dependence on encoded numeric values; it is not the same as exponential on every instance.

Provenance note

Original exercise using the standard dynamic program. It does not rely on student submissions as sources.

Common mistakes

  • Skipping a direction of correctness or an input-format edge case.
  • Measuring resources against a numeric value rather than encoded input length.
  • Treating an informal intuition as a proof of the stated bound.

Sources and verification

  • 2026 transcript 15
  • Garey-Johnson, SP13
  • Corpus coverage note: No direct merged-summary coverage.
  • Catalog mapping: data/exercises.json; statements and solutions are independently written; personal submissions are non-authoritative.
Guided construction

Subset Sum dynamic programming

Step 1 of 6
A = {3, 5, 6, 11} · target T = 14
i / sum
0
1
2
3
4
5
6
7
8
9
10
11
12
13
14
0 items
✓
·
·
·
·
·
·
·
·
·
·
·
·
·
·