Given a set of positive numbers, find the subsets that add up to a target. This is a backtracking problem with an elegant pruning rule.
The Problem
Given n positive integers w₁, ..., wₙ and a target M, find all subsets whose sum is exactly M. For example, with weights {3, 34, 4, 12, 5, 2} and M = 9 the subsets {4, 5} and {3, 4, 2} work. There are 2ⁿ subsets, so brute force is exponential. The problem models budgeting, change-making and partitioning, and it is NP-complete.
The State-Space Tree
First sort the weights in increasing order. Each level i decides whether wᵢ is included (left branch) or excluded (right branch). A node at level i stores the sum s of the included weights so far and r, the sum of the weights not yet decided. The tree has up to 2ⁿ⁺¹ − 1 nodes.
The Pruning Conditions
A node is promising only if it can still reach M. Two tests are applied. Too large: if s + wᵢ₊₁ > M, then adding the next (smallest remaining) weight overshoots, and no completion works, so prune. Too small: if s + r < M, even taking all the remaining weights cannot reach M, so prune. When s = M, a solution is found and recorded. These two bounds eliminate large parts of the tree. The worst-case time remains O(2ⁿ), but typical instances are solved quickly.
Related Problems and Extensions
A decision version asks only whether some subset has sum M and can be solved by dynamic programming in Θ(nM) time (pseudo-polynomial) with a table like that of the knapsack; the backtracking version lists all solutions. Partition, deciding whether a set can be split into two halves of equal sum, is the special case M = (total)/2.
Definition: Sum of subsets. The problem of finding the subsets of a set of positive integers whose elements add up to a given target M.
Table 4.3: Pruning rules
| Test | Condition | Action |
|---|---|---|
| Solution | s = M | Record the subset |
| Too large | s + next weight > M | Prune |
| Too small | s + remaining total < M | Prune |
| Otherwise | Promising | Branch: include or exclude |
Example 4.3: Sum of subsets with M = 9
Problem: Weights sorted: 2, 3, 4, 5, 12, 34; M = 9. Find all subsets with sum 9.
Solution:
- Include 2 (s = 2), include 3 (s = 5), include 4 (s = 9): solution {2, 3, 4}.
- Backtrack: 2, 3, then 5 gives s = 10 > 9 pruned; 2, 3 with 12 or 34 pruned (too large).
- Exclude 3: 2 with 4 gives 6, then 5 gives 11 > 9 pruned; 2 with 5 gives 7, remaining 12, 34 too large, and 7 < 9 with nothing small left: prune.
- Exclude 2: include 3, 4 → 7, then 5 → 12 pruned; include 4, 5 → s = 9: solution {4, 5}.
Result: Two solutions: {2, 3, 4} and {4, 5}.
def subsets(w, M):
w, out = sorted(w), []
def go(i, chosen, s, rest):
if s == M:
out.append(chosen)
return
if i == len(w) or s + w[i] > M or s + rest < M:
return
go(i + 1, chosen + [w[i]], s + w[i], rest - w[i])
go(i + 1, chosen, s, rest - w[i])
go(0, [], 0, sum(w))
return out
print(subsets([3, 34, 4, 12, 5, 2], 9))
print(subsets([5, 10, 12, 13, 15, 18], 30))
Output
[[2, 3, 4], [4, 5]]
[[5, 10, 15], [5, 12, 13], [12, 18]]
Common mistakes
- Not sorting the weights, which weakens the "too large" test.
- Forgetting to subtract the weight from the remaining sum when it is excluded.
- Reporting only one subset when all are required.
- Using the greedy approach, which fails to find sums such as 9 with {2, 3, 4}.
Key takeaways
- Sort weights; each level includes or excludes one weight.
- Prune if s + next > M (too large) or s + remaining < M (too small).
- Record a solution when s = M.
- Worst case O(2ⁿ); DP decision version is Θ(nM).
Practice questions
- State the two pruning conditions in sum of subsets.
- Why sort the weights first?
- What is the worst-case time of backtracking for sum of subsets?
Answers to the practice questions
- Prune when s + next weight > M, or when s + sum of remaining weights < M.
- So that the next weight is the smallest remaining, making the too-large test sharp.
- O(2ⁿ).
Sources
- Official BCA syllabus Design and Analysis of Algorithms, course code 26BCA404DS03, Unit IV
