Question: Question 4 : Recall the problem Subset Sum which we solved in class using a dynamic programming algorithm. The problem was as follows: INPUT: set

Question 4:
Recall the problem Subset Sum which we solved in class using a dynamic programming algorithm. The problem was as follows:
INPUT: set S={s0,dots,sn-1} of non-negative integers, positive integer B OUTPUT: subset S'subeS such that xinS'?x=B, or "no solution" if no such S' exists. In English, the problem takes in a set of non-negative integers and a positive integer B, and returns some subset of the set that sums to B(or "no solution" if this is impossible). We solved this problem with the following method:
The DP Steps for Subset Sum
(a) What values are we trying to compute in the table T :
T[i,j]= TRUE if there exists a subset S'sube{s0,dots,si} such that xinS'?x=j
(b) What is the DP-relation:
T[i,j] is TRUE if:
T[i-1,j]= TRUE (don't use si) OR
-j>si and T[i-1,j-si]=TRUE (use si and some other numbers) OR
si=j(use only si)
(c) How do we initialize the table:
T[0,s0]= TRUE
T[0.x]= FALSE for all xs0.
(d) What do we return at the end:
Return T[n-1,B]
Subset Sum Pseudocode
T[0,s0]= TRUE
T[0,x]= FALSE for xs0
For i=1 to n-1
For j=0 to B
**T[i,j]= TRUE if:
*T[i-1,j]= TRUE, OR
*si==j,OR
*j>si and T[i-1,j-si]= TRUE
** Else set T[i,j]= FALSE
Else set T[i,j]= FALSE
Return T[n-1,B]
We will now examine a related problem: Subset Sum of a specific size. The formal problem statement is as follows:
INPUT: set S={s0,dots,sn-1} of non-negative integers, positive integer B, target size C, where C is an integer in the range 1,n
OUTPUT: subset S'subS such that xinS'?x=B AND |S'|=C, or FALSE if no such S' exists.
Example: S={2,5,7,30,42,50},B=79
 Question 4: Recall the problem Subset Sum which we solved in

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!