Question: Solution in Python: We are given a set of whole numbers : {1,,}S: {n1,,nk} and a number N. Our goal is to choose a subset

Solution in Python:

We are given a set of whole numbers : {1,,}S: {n1,,nk} and a number N. Our goal is to choose a subset of numbers : {1,,}T: {ni1,,nij}S such that

(a) =1l=1jnilN, the sum of chosen numbers is less than or equal to N,

(b) The difference =1Nl=1jnil is made as small as possible.

For example, ={1,2,3,4,5,10}S={1,2,3,4,5,10} and =20N=20 then by choosing ={1,2,3,4,5}T={1,2,3,4,5}, we have 1+2+3+4+5=15201+2+3+4+5=1520, achieving a difference of 55. However, if we chose ={2,3,5,10}T={2,3,5,10} we obtain a sum of 2+3+5+10=202+3+5+10=20 achieving the smallest possible difference of 00.

Therefore the problem is as follows:

  • Inputs: list :[1,,]S:[n1,,nk] and number N.
  • Output: a list T of elements from S such that sum of elements of T is N and NeTe is the smallest possible.

The subsequent parts to this problem ask you to derive a dynamic programming solution to this problem.

Note: Because S and T are viewed as sets, each element in the set may occur exactly once.

1a. Write a recursive function for calculating the minimum value of the difference possible... def minSubsetDifference_recursive(N, s_list)

1b. Memoize the recurrence above... def minSubsetDifference_Memoize(N, s_list):

1c.Write code to recover the solution... def minSubsetDifference(N, s_list):

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!