1. Gyanastra
  2. Algorithms
  3. Subset Sum and Traveling Salesman
  4. Subset Sum: Brute Force and Recursive Formulation
Free lesson, no account needed 6 min read 8 practice questions after it
Asked in past papers2 times
  • GATE CSE2008
  • ISRO2018

Algorithms · Subset Sum and Traveling Salesman

Subset Sum: Brute Force and Recursive Formulation

Overview

Subset Sum asks whether some subset of nn given elements adds up exactly to a target sum SS.
The brute-force method checks all 2n2^n subsets, while the recursive formulation SS(i,S)SS(i, S) 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 nn elements A1,A2,…,AnA_1, A_2, \ldots, A_n and a target sum SS, determine whether there exists a subset of these elements whose sum is exactly equal to SS.
  • Brute force: Enumerate all subsets and check each subset's sum against the target.
  • Recursive formulation: Define SS(i,S)SS(i, S) = "is a sum of SS possible using elements A1…AiA_1 \ldots A_i?", then look for repeated subproblems so that the tabular (dynamic programming) method can be applied.
  • Include case: The ii-th element is present in the final subset, so a sum of S−AiS - A_i must be possible from the remaining i−1i-1 elements.
  • Exclude case: The ii-th element is absent, so the sum SS must be possible from the remaining i−1i-1 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 SS is less than AiA_i, AiA_i cannot be included (including it would make the sum exceed SS), so only the exclude case applies.
  • Link to 0-1 knapsack: The structure is exactly the same as 0-1 knapsack — there the ii-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

  1. The number of subsets of nn elements is 2n2^n.
  2. Examine the sum of each subset.
  3. Check whether that sum equals WW (the required target).
  4. This takes 2n2^n time.

Recursive formulation

  1. Define SS(i,S)SS(i, S): using elements A1A_1 to AiA_i, is there a subset whose sum equals SS?
  2. The element at the ii-th location may or may not be present in the final subset, giving two cases.
  3. Apply the base conditions at i=0i = 0.
  4. If S≥AiS \geq A_i, combine the include case SS(i−1,S−Ai)SS(i-1, S - A_i) and the exclude case SS(i−1,S)SS(i-1, S) with OR.
  5. If S<AiS < A_i, use only the exclude case SS(i−1,S)SS(i-1, S).

Base conditions

  • i=0i = 0 and S≠0S \neq 0: there are no elements at all, so producing a sum of some number (e.g. 5) is never possible → answer is false (no).
  • i=0i = 0 and S=0S = 0: 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 SS(i,S)SS(i, S) = true if a subset of A1…AiA_1 \ldots A_i has sum exactly SS, else false; AiA_i = value of the ii-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: 2n2^n (number of subsets).

Worked examples

Example 1 — include case reasoning Array: 1, 2, 3, 4. Target sum S=10S = 10.

  • If the element 4 is included, the remaining elements (1, 2, 3) must be able to form a sum of 10−4=610 - 4 = 6.
  • 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

  • i=0i = 0, S=5S = 5: no elements exist, so a sum of 5 cannot be produced → false.
  • i=0i = 0, S=0S = 0: choose the null set → sum 0 → true.

Example 3 — third condition reasoning If S<AiS < A_i, including AiA_i would make the sum exceed SS, so AiA_i is left out and we check SS(i−1,S)SS(i-1, S) on the remaining elements.

Comparison

AspectBrute forceRecursive formulation
IdeaEnumerate all 2n2^n subsets, check each sum against WWDecide include/exclude of ii-th element, recurse on i−1i-1
Time2n2^nExponential recursion; repeated subproblems make it suitable for tabular (DP) method
OutputYes/No whether sum WW achievableYes/No whether sum SS achievable

Revision summary

  • Number of subsets of nn elements = 2n2^n; brute force checks each subset's sum against WW in 2n2^n time.
  • Base cases: SS(0,0)=SS(0, 0) = true (null set sums to 0); SS(0,S)=SS(0, S) = false for S≠0S \neq 0.
  • Include case: SS(i−1,S−Ai)SS(i-1, S - A_i); exclude case: SS(i−1,S)SS(i-1, S); combine with OR.
  • If S<AiS < A_i, 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 ≥ aia_{i} AND X[i-1][j-aia_{i}]);
    the answer is X[n][W].
    Asked inGATE 2008
  • Subset sum is solved in pseudo-polynomial time O(nW)O(nW) 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 O(nW)O(nW) to O(W)O(W).
  • 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 O(2n)O(2^{n}) time in the worst case; memoising on (i, S) gives O(nW)O(nW) 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 - aia_{i} can go out of range and the simple table recurrence fails.
    Asked inGATE 2008
Pages 1–2 of 2