Sum of Subset Problem
Objective: Given a set of positive integers, and a value sum S, find out if there exist a subset in
array whose sum is equal to given sum S.
Example 1:
set[]={3, 4, 5, 2}
Sum=6
0 1 2 3 4 5 6
0 T F F F F F F
3 T F F T F F F
4 T F F T T F F
5 T F F T T T F
2 T F T T T T T
Sum of Subset = { 2 + 4 } = 6
Example 2:
int[] A = { 3, 2, 7, 1}, Sum = 6
Sum of Subset = { 1 + 2 + 3 } = 6
Algorithm
Bool SumSubset(w,n,m)
{
1. t[0, 0] = True for(j = 1 to m) t[0, j] = False
2. for(i = 1 to n)
3. for(j = 0 to m)
4. t[i, j]=t[i-1, j]
5. if(j - wi >= 0)
6. t[i, j] = t[i-1, j] || t[i-1, j-wi]
7. return t[n, m]
}

Sum of subset problem

  • 1.
    Sum of SubsetProblem Objective: Given a set of positive integers, and a value sum S, find out if there exist a subset in array whose sum is equal to given sum S. Example 1: set[]={3, 4, 5, 2} Sum=6 0 1 2 3 4 5 6 0 T F F F F F F 3 T F F T F F F 4 T F F T T F F 5 T F F T T T F 2 T F T T T T T Sum of Subset = { 2 + 4 } = 6 Example 2: int[] A = { 3, 2, 7, 1}, Sum = 6 Sum of Subset = { 1 + 2 + 3 } = 6
  • 2.
    Algorithm Bool SumSubset(w,n,m) { 1. t[0,0] = True for(j = 1 to m) t[0, j] = False 2. for(i = 1 to n) 3. for(j = 0 to m) 4. t[i, j]=t[i-1, j] 5. if(j - wi >= 0) 6. t[i, j] = t[i-1, j] || t[i-1, j-wi] 7. return t[n, m] }