7 routes42 sections324 units

Unit 5.7 · about 4 h

125 / 324

Knapsack, subset and bitmask dynamic programming

One table shape answers min-coins, counting and feasibility. The loop direction decides whether an item is reusable.

  • derive
  • implement

You can now

  • Derive take-or-skip recurrences with the correct inner-loop direction for \(0/1\) versus unbounded choice.
  • Reduce subset-sum and counting variants to one knapsack table.
  • Encode small-set state as bitmasks and bound the \(2^n\) state cost.

Covers

  • Knapsack, subset and bitmask state

Study, in this order

4 steps

  1. \(0/1\) knapsack and the backward inner loop
    DSA Handbook - dynamic programming

    The 1D table fill; Why the inner loop sweeps right-to-left; Worked example: LC 416 on [1, 5, 11, 5]; Reformulations: when the problem doesn't say "knapsack" (LC 494, 1049, 474)

  2. Unbounded knapsack and the reversed loop
    DSA Handbook - dynamic programming

    What changes from \(0/1\) knapsack; The mechanics: filling \(\text{dp}[a]\) left to right; Counting unordered combinations: LC 518; Counting ordered sequences: LC 377

  3. Bitmask state for small sets
    DSA Handbook - dynamic programming

    The pattern; Worked example: LC 847 on a 3-node line; What it actually costs; Permutation / assignment: position is popcount(mask) (LC 526)

  4. Subset Sum and the Nadiria change problem
    Algorithms - dynamic programming

    Section 3.8 (pp. 116-117): \(\operatorname{SS}(i, t)\) recurrence with the \(O(nT)\) table, and Exercise 1 Nadiria change (p. 123): greedy failure first, then recurrence, then DP

Focus on

  • \(0/1\) sweeps capacity right to left so item \(i\) cannot fund itself in the same pass. Unbounded sweeps left to right so \(\text{dp}[a-c]\) already includes item \(i\).
  • Subset Sum \(O(nT)\) is pseudopolynomial: \(T\) is a value, not the input size. Tabulation loses to backtracking when \(T\) dwarfs \(2^n\).
  • Bitmask state costs \(2^n\) subsets times \(n\) transitions. It fits only small ground sets; that boundary is the content, not a footnote.

Reproduce closed-book

Closed-book in Python 3.13: coin_change([1,2,5], 11) = 3, coin_change_ii(5, [1,2,5]) = 4, can_partition([1,5,11,5]) = True and can_partition([1,2,3,5]) = False. Also show largest-first greedy on ([1,3,4], 6) gives 3 coins versus optimal 2. State the amounts and all five expected outputs.

Traps

  • Sweeping \(0/1\) capacity left to right. Item \(i\) then reuses its own update and the \(0/1\) table silently becomes unbounded.
  • Reading min-coins -1 as zero ways. Coin Change returns -1 when unreachable; Coin Change II returns 0 ways. Different families, different sentinels.
  • Applying bitmask DP past \(n = 20\) without counting. \(2^{20}\) rows times \(n\) columns already exceeds ten million cells; state the bound first.

Practice

LeetCode 322 - Coin Change

Attempt cold. Check ([1,2,5], 11) = 3, ([2], 3) = -1 and ([1], 0) = 0, then submit.

Attempt cold: write the reuse direction before coding. After submitting, read the AlgoMaster Coin Change lesson (https://algomaster.io/learn/dsa/coin-change).

LeetCode 416 - Partition Equal Subset Sum

Check [1,5,11,5] = True and [1,2,3,5] = False, then submit.

Reduce to subset sum with target \(\text{total}/2\). After submitting, compare with the DSA Handbook knapsack-01 reformulation section.

Self-test

  1. Why does the \(0/1\) inner loop run right to left?

  2. Coins [1,3,4], amount 6: what does largest-first greedy give and why is it wrong?

  3. Subset Sum runs \(O(nT)\). Why is that not polynomial?

  4. When is bitmask state the only state that fits?

Completion criterion

Stop when

The five reproduce numbers come out of a blank file, and you can state each loop direction and why.

Mark passed, then open 5.8 Only mark this when you can do it without notes.

Leads to

Comes after

Resources for this unit

  • DSA Handbook - dynamic programming
    Handbook Academyprimarycourse
    Study Part 9 in order from state and recurrence through sequence, grid, tree and bitmask families. For every example, close the page and reconstruct the state, transition and evaluation order.
  • Algorithms - dynamic programming
    Jeff Ericksonrequiredtheory
    Use the dynamic-programming chapter to derive states and recurrences from first principles and to separate DP from greedy reasoning and shortest paths.

Also in this section

  • AlgoMaster DSA - dynamic programming slice
    AlgoMasterrequiredpractice
    Attempt Coin Change, Longest Common Subsequence and House Robber III cold. Write the state, transition, base case and evaluation order first; compare against brute force on small inputs and reconstruct a witness when available.
  • Hypothesis - exhaustive and differential DP tests
    Hypothesissupportpractice
    Generate small valid instances, compare memoized, tabulated and exhaustive results, and retain shrunk failures for wrong state, base case, order and compression defects.
  • NeetCode - unseen mixed problems
    NeetCodeoptionalassessment
    Use two tag-hidden mixed problems as the final transfer test. One must require rejecting a tempting greedy or insufficient-state solution with a counterexample.
  • Python sys: recursion limit
    Python Software Foundationreferencereference
    Read sys.getrecursionlimit and sys.setrecursionlimit: the limit guards the C stack and its safe maximum is platform-dependent. Check the default with getrecursionlimit (1000 on CPython 3.13), raise it once for memoized DP, or switch to tabulation for deep inputs.
  • Erickson lecture note D - Advanced Dynamic Programming
    Jeff Ericksonreferencetheory
    Read Section D.1 (Saving Space: Divide and Conquer) for the Hirschberg Half-table split that recovers an optimal edit sequence in \(O(m + n)\) space.
  • Erickson lecture note J - Approximation Algorithms
    Jeff Ericksonreferencetheory
    Read Section J.1 (Load Balancing) for the Partition reduction that makes exact scheduling NP-hard, the GreedyLoadBalance heuristic, and Theorem J.1 with its factor-2 proof.

Quick find

Search Michi

Type to search every route, section, unit, topic and resource.

Enter opens the first result Esc closes

Reference

Keyboard shortcuts

n
Continue to the first unit not passed
j / k
Next / previous unit
1 2 3
Toggle learned, practised, passed
/
Open search
?
Show this help
Esc
Close open panels