[prev] 57 [next]

Example: Subset Sum (cont)

Generate and test approach:

subsetsum(A,k):
|  Input  set A of n integers, target sum k
|  Output true if Σb∈Bb=k for some B⊆A
|         false otherwise
|
|  for each subset S⊆A do
|  |  if sum(S)=k then
|  |     return true
|  |  end if
|  end for
|  return false

  • How many subsets are there of n elements?
  • How could we generate them?