Embed presentation
Download to read offline
![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](https://image.slidesharecdn.com/sumofsubsetproblem-210706145535/75/Sum-of-subset-problem-1-2048.jpg)
![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]
}](https://image.slidesharecdn.com/sumofsubsetproblem-210706145535/75/Sum-of-subset-problem-2-2048.jpg)

Given a set of positive integers and a target sum S, the sum of subset problem is to determine if there exists a subset of the integers whose sum equals S. The algorithm uses a two dimensional table t, where t[i,j] is true if there is a subset that sums to j using the first i elements of the set. It iterates through the elements, setting t[i,j] to true if t[i-1,j] is already true or t[i-1,j-w[i]] is true, where w[i] is the current element. It returns t[n,m] which indicates if a subset summing to S exists.
![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](https://image.slidesharecdn.com/sumofsubsetproblem-210706145535/75/Sum-of-subset-problem-1-2048.jpg)
![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]
}](https://image.slidesharecdn.com/sumofsubsetproblem-210706145535/75/Sum-of-subset-problem-2-2048.jpg)