Algorithms · Subset Sum and Traveling Salesman
Subset Sum: Problem Definition and Greedy Failure
Overview
Subset Sum asks whether some subset of a given set of numbers adds up exactly to a target . It is closely related to 0-1 knapsack, and the two central lessons are that greedy strategies fail and that brute force costs time.
Key concepts
- Subset Sum problem: given a set of numbers and a target number , decide whether there exists any subset of this set whose sum (the sum of all the numbers chosen in that subset) is equal to .
- Relation to 0-1 knapsack: the problem is similar to (not exactly the same as) 0-1 knapsack.
It can be characterized the same way: the subproblems can be characterized, the recursive equations can be written, and the problem can be solved the same way 0-1 knapsack was solved.
There is a correlation between the two problems. - Greedy is not applicable: there is no obvious greedy criterion — you do not know whether to take the smallest number or the highest number.
- Brute force: each element has two choices — it may be present in a subset or it may not be present — so for elements there are subsets. Examine the sum of each subset and check whether it equals .
How it works
- State the instance: a set of numbers and a target .
- Decide whether some subset sums exactly to .
- Greedy attempt: pick the smallest number first, subtract it from , and look for an element equal to the remainder — this fails (see the worked example).
- Brute force: enumerate all subsets, sum each one, and check it against .
Formulas
- Number of subsets of a set of elements: (each element is either included or excluded: 2 choices per element).
- Brute force time complexity: (each subset's sum must be examined).
- Greedy check in the example: after picking 1, remaining required sum .
Worked examples
Set , target .
| Step | Action | Result |
|---|---|---|
| 1 | Greedy on the smaller numbers: take 1 | Remaining sum needed |
| 2 | Look for an element equal to 4 | No number in the set equals 4 → greedy fails |
| 3 | Check the correct subset | → answer is yes, a subset exists |
Comparison
| Method | Idea | Result / Cost |
|---|---|---|
| Greedy | Pick smallest (or largest) numbers first | Not applicable — no correct greedy choice; fails on , |
| Brute force | Enumerate all subsets, check each sum against | Correct but takes time |
Revision summary
- Subset Sum: given numbers and a target , decide whether some subset sums exactly to .
- The problem is closely related to 0-1 knapsack; it can be characterized with subproblems and recursive equations and solved the same way (leading toward dynamic programming).
- Greedy does not work: there is no rule for whether to take the smallest or the largest element.
- The greedy pick of 1 in with leaves a remainder of 4 that no element can fill.
- Brute force enumerates subsets (each element present or absent) and takes time.
Previous-year exam insights
- Subset Sum is solved by dynamic programming with a 2-D Boolean table X[i][j] (TRUE iff some subset of the first i elements sums to j), using the recurrence X[i][j] = X[i-1][j] OR (j ≥ AND X[i-1][j-]), with base case X[i][0] = TRUE and X[0][j] = FALSE for j > 0;
the answer is X[n][W].
Asked inGATE 2008GATE 2008 - The DP table for subset sum has n+1 rows (row 0 for no elements) and W+1 columns and runs in time and space (space can be reduced to by keeping one row); this is pseudo-polynomial, not polynomial, because W is part of the input value.
Asked inGATE 2008 - Subset Sum is NP-complete in general (it is a special case of 0-1 knapsack), so no polynomial-time algorithm is known; the DP is pseudo-polynomial and brute force is .
Asked inISRO 2018 - Among the standard paradigms, dynamic programming (not divide and conquer, greedy, or branch and bound) is the paradigm that gives the minimum-time exact solution for subset sum in the usual exam sense.
Asked inISRO 2018 - Subset Sum is a decision problem (yes/no); the related optimization version asks for a subset with sum as close as possible to W (or exactly W if it exists), and the DP table can be used to reconstruct the actual subset by backtracking.
- Greedy fails for subset sum because the choice of which element to include is not greedily safe: a locally optimal pick can make the exact target unreachable even when a solution exists, so all subsets may need to be considered.
Asked inISRO 2018