Algorithms · Subset Sum and Traveling Salesman
Subset Sum: Brute Force and Recursive Formulation
Overview
Subset Sum asks whether some subset of given elements adds up exactly to a target sum .
The brute-force method checks all subsets, while the recursive formulation decides include/exclude for each element and exposes the repeated subproblems that make a tabular (dynamic programming) solution possible.
Key concepts
- Subset Sum problem: Given elements and a target sum , determine whether there exists a subset of these elements whose sum is exactly equal to .
- Brute force: Enumerate all subsets and check each subset's sum against the target.
- Recursive formulation: Define = "is a sum of possible using elements ?", then look for repeated subproblems so that the tabular (dynamic programming) method can be applied.
- Include case: The -th element is present in the final subset, so a sum of must be possible from the remaining elements.
- Exclude case: The -th element is absent, so the sum must be possible from the remaining elements.
- OR combination: The sum is possible if either case succeeds. OR truth table: true OR true = true; true OR false = true; false OR true = true; false OR false = false.
- Third condition: If the required sum is less than , cannot be included (including it would make the sum exceed ), so only the exclude case applies.
- Link to 0-1 knapsack: The structure is exactly the same as 0-1 knapsack — there the -th element is taken and, on including it, the knapsack capacity is reduced and profit added; here on including it the required sum is reduced and only the OR is taken.
How it works
Brute force method
- The number of subsets of elements is .
- Examine the sum of each subset.
- Check whether that sum equals (the required target).
- This takes time.
Recursive formulation
- Define : using elements to , is there a subset whose sum equals ?
- The element at the -th location may or may not be present in the final subset, giving two cases.
- Apply the base conditions at .
- If , combine the include case and the exclude case with OR.
- If , use only the exclude case .
Base conditions
- and : there are no elements at all, so producing a sum of some number (e.g. 5) is never possible → answer is false (no).
- and : a sum of 0 is always possible — the simplest way is to choose nothing; the null set always has sum 0 → answer is true.
Formulas
-
Let = true if a subset of has sum exactly , else false; = value of the -th element.
-
Base cases:
SS(0, S) = false for S != 0
SS(0, 0) = true
- Recursive cases:
If S < A_i: SS(i, S) = SS(i-1, S) (A_i cannot be included)
If S >= A_i: SS(i, S) = SS(i-1, S - A_i) OR SS(i-1, S) (include OR exclude)
- Brute force time: (number of subsets).
Worked examples
Example 1 — include case reasoning Array: 1, 2, 3, 4. Target sum .
- If the element 4 is included, the remaining elements (1, 2, 3) must be able to form a sum of .
- Only if a sum of 6 is possible from {1, 2, 3} is a sum of 10 possible from {1, 2, 3, 4}.
Example 2 — base case reasoning
- , : no elements exist, so a sum of 5 cannot be produced → false.
- , : choose the null set → sum 0 → true.
Example 3 — third condition reasoning If , including would make the sum exceed , so is left out and we check on the remaining elements.
Comparison
| Aspect | Brute force | Recursive formulation |
|---|---|---|
| Idea | Enumerate all subsets, check each sum against | Decide include/exclude of -th element, recurse on |
| Time | Exponential recursion; repeated subproblems make it suitable for tabular (DP) method | |
| Output | Yes/No whether sum achievable | Yes/No whether sum achievable |
Revision summary
- Number of subsets of elements = ; brute force checks each subset's sum against in time.
- Base cases: true (null set sums to 0); false for .
- Include case: ; exclude case: ; combine with OR.
- If , the element cannot be included — only the exclude case applies.
- The recurrence has repeating subproblems, which is what makes the tabular / dynamic programming method applicable.
- The recurrence is exactly the same structure as 0-1 knapsack (there: reduce capacity and add profit; here: reduce the sum and take OR).
Previous-year exam insights
- The standard DP table for subset sum is a Boolean array X[0..n][0..W] (n rows, W+1 columns) with X[0][0]=true, X[0][j]=false for j>0, and X[i][j] = X[i-1][j] OR (j ≥ AND X[i-1][j-]);
the answer is X[n][W].
Asked inGATE 2008 - Subset sum is solved in pseudo-polynomial time by dynamic programming; it is NP-complete in general, so no polynomial-time algorithm is known, and greedy or divide-and-conquer do not solve it correctly.
Asked inISRO 2018 - The DP table can be filled with only two rows (previous and current) or one row traversed right-to-left, reducing space from to .
- Subset sum is a special case of 0-1 knapsack where each item's value equals its weight and the capacity is W; the DP recurrence has the same include/exclude structure.
- The recursive formulation without memoisation takes time in the worst case; memoising on (i, S) gives distinct subproblems, which is why the tabular method works.
- For the DP to be valid the elements must be non-negative (positive in the standard statement); with negative numbers the index j - can go out of range and the simple table recurrence fails.
Asked inGATE 2008