Dynamic programming example: subset sum [00R9]
- November 6, 2026
As a slightly more involved example of dynamic programming, consider the following problem.
The subset sum problem [00RA]
- November 6, 2026
Given a set X = \{x_1, x_2, \dots , x_n\} of positive integers and a target value S, is there is a subset of X whose sum is exactly S?
Brute force subset sum solution [00RB]
- November 6, 2026
A brute force solution to the subset sum problem is to list every possible subset of X, sum each, and check whether any of them add to S. Naïvely, this takes \Theta (2^n) time to list every subset, and \Theta {n} time to sum each one, for a total of \Theta (n 2^n). (If we list the subsets in a clever order, we can arrange things so that only a single element is added or removed at each step, allowing us to update a running sum in only \Theta (1) per subset, for a total time of \Theta (2^n), but that's not all that much better.)
Recurrence for subset sum [00RC]
- November 6, 2026
The key to solving this problem is to generalize it along two dimensions: we consider both summing to any s \leq S, and we also consider trying to use only the x_i up to x_k, that is, \{x_1, \dots , x_k\}, instead of the full set X. That is, \mathit {canAddTo}(k,s) will be \mathsf {T} if there is a subset of \{x_1, \dots , x_k\} which adds to exactly s, and \mathsf {F} otherwise. We have the following recurrence: \begin {align*} \mathit {canAddTo}(k, 0) &= \mathsf {T} \qquad (\text {for all } k \geq 0) \\ \mathit {canAddTo}(0, s) &= \mathsf {F} \qquad (\text {for all } s > 0) \\ \mathit {canAddTo}(k,s) &= \begin {cases} \mathit {canAddTo}(k-1, s) & \text {if } x_k > s \\ \mathit {canAddTo}(k-1, s) \lor \mathit {canAddTo}(k-1, s - x_k) & \text {otherwise} \end {cases} \end {align*}
That is,
- We can always add to the sum 0 by picking the empty subset.
- We can never add up to a nonzero sum if we aren’t allowed to use any of the x_i.
- If x_k > s, then it can’t be used in a subset that sums to s, so whether we can make the sum s using a subset of x_1 \dots x_k has the same answer as whether we can make s using a subset of x_1 \dots x_{k-1}.
- Otherwise, we can try both omitting x_k (in which case we have to make s using elements up to x_{k-1}, as before), or using it (in which case we have to make the remaining s - x_k using the elements up to x_{k-1}).
Note that the above is essentially a proof by induction of the recurrence relation's correctness. We explained why the base cases are correct, and then explained why the recursive cases will compute the right thing, given the assumption that the recursive calls will return the correct answer when given smaller inputs.
Memoization for subset sum [00RD]
- November 6, 2026
This most definitely has overlapping subproblems. One simple approach is to use a 2D array c of size (n+1) \times (S + 1), so c[k][s] will store the output of \mathit {canAddTo}(k,s). It is also possible to use a dictionary with (k,s) pairs as keys; however, since 0 \leq k \leq n and 0 \leq s \leq S, using a 2D array is simpler and accomplishes exactly the same thing. (An array can be thought of as a special-purpose dictionary where the keys are consecutive integers starting from 0.)
Each entry depends only on entries either above it ( k is smaller), or above it and to the left ( k and s are both smaller), so we can fill it in row order or column order. In pseudocode:
Alternatively, we could use the technique of having a recursive function which checks first to see whether the required output is already in the array.
Since we fill in an (n+1) \times (S + 1) array, with \Theta (1) work required to fill in each cell, the algorithm takes \Theta (nS) time overall.
Remembering choices to reconstruct subset sum solutions [00RE]
- November 6, 2026
The above algorithm tells us whether it is possible to add to S via some subset, but it does not tell us what the actual subset is. To compute the actual optimal solution instead of just the optimal value, we can save the choices made at each step.
What information does \mathit {canAddTo} discard? It is precisely the choice of whether to use x_k or not. The Boolean “or” operation will be \mathsf {T} if either one of its inputs is; it does not care which. Therefore we will make another (n+1)\times (S+1) array of booleans called \mathit {use}, where \mathit {use}[k][s] is \mathsf {T} if and only if we should use x_k in a subset to make s. We initialize it to all \mathsf {F} values, and fill it in as shown in the pseudocode below.
If c[n][S] is , then there is some subset of X which adds to S. To reconstruct such an actual subset we can work our way backwards as follows: