1. Gyanastra
  2. Algorithms
  3. Subset Sum and Traveling Salesman
  4. Subset Sum: Problem Definition and Greedy Failure
Free lesson, no account needed 5 min read 8 practice questions after it
Asked in past papers3 times
  • GATE CSE20081990
  • ISRO2018

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 WW. It is closely related to 0-1 knapsack, and the two central lessons are that greedy strategies fail and that brute force costs 2n2^n time.

Key concepts

  • Subset Sum problem: given a set of NN numbers A1,A2,…,ANA_1, A_2, \dots, A_N and a target number WW, decide whether there exists any subset of this set whose sum (the sum of all the numbers chosen in that subset) is equal to WW.
  • 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 nn elements there are 2n2^n subsets. Examine the sum of each subset and check whether it equals WW.

How it works

  1. State the instance: a set of NN numbers and a target WW.
  2. Decide whether some subset sums exactly to WW.
  3. Greedy attempt: pick the smallest number first, subtract it from WW, and look for an element equal to the remainder — this fails (see the worked example).
  4. Brute force: enumerate all 2n2^n subsets, sum each one, and check it against WW.

Formulas

  • Number of subsets of a set of nn elements: 2n2^n (each element is either included or excluded: 2 choices per element).
  • Brute force time complexity: O(2n)O(2^n) (each subset's sum must be examined).
  • Greedy check in the example: after picking 1, remaining required sum =W−1=5−1=4= W - 1 = 5 - 1 = 4.

Worked examples

Set A={6,2,3,1}A = \{6, 2, 3, 1\}, target W=5W = 5.

StepActionResult
1Greedy on the smaller numbers: take 1Remaining sum needed =5−1=4= 5 - 1 = 4
2Look for an element equal to 4No number in the set equals 4 → greedy fails
3Check the correct subset {2,3}\{2, 3\}2+3=52 + 3 = 5 → answer is yes, a subset exists

Comparison

MethodIdeaResult / Cost
GreedyPick smallest (or largest) numbers firstNot applicable — no correct greedy choice; fails on {6,2,3,1}\{6, 2, 3, 1\}, W=5W = 5
Brute forceEnumerate all 2n2^n subsets, check each sum against WWCorrect but takes 2n2^n time

Revision summary

  • Subset Sum: given NN numbers and a target WW, decide whether some subset sums exactly to WW.
  • 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 {6,2,3,1}\{6, 2, 3, 1\} with W=5W = 5 leaves a remainder of 4 that no element can fill.
  • Brute force enumerates 2n2^n subsets (each element present or absent) and takes 2n2^n 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 ≥ aia_{i} AND X[i-1][j-aia_{i}]), 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 O(nW)O(nW) time and O(nW)O(nW) space (space can be reduced to O(W)O(W) 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 O(2n)O(2^{n}).
    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
Pages 1–2 of 2