Question

In: Computer Science

Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for...

Recall the dynamic programming algorithm we saw in class for solving the 0/1 knapsack problem for n objects with a knapsack capacity of K. In particular, we characterized our recurrence OPT(j, W) to be following quantity:

OPT(j, W) := The maximum profit that can be obtained when selecting from objects 1, 2, . . . , j with a knapsack capacity of W , where (after filling in our dynamic programming table), we return the value stored at OPT(n, K) as our final answer.

In this problem, we’ll consider an alternate dynamic programming approach for solving 0/1 knapsack.

a/ Suppose we now define our recurrence in the following way: OPT(j, R) := The smallest capacity that permits us to earn a total revenue of exactly R when selecting from objects 1, 2, . . . , j. Give a formal definition for OPT(j, R), defined as a recurrence that characterizes the quantity in terms of smaller subproblems. You may assume all weights and prices are positive integers. Be sure to define all necessary base cases, and give a brief justification for why your recurrence is correct.

b/ Using your recurrence from part (a), describe both (i) the order in which the algorithm should fill the dynamic programming table, and (ii) how, once filled in, the table can be used to return the final answer. Remember, we are still looking to return the maximum profit that can be obtained when selecting from all items (in other words, only the algorithm has changed, not the problem definition)

c/ What is the run time of your algorithm? For what kind of inputs might it be better to use this algorithm instead of our original approach? Briefly justify your answer.

Solutions

Expert Solution

(a) Let OPT(j, R) represent the smallest capacity of the knapsack to earn a total revenue of exactly R when selecting from objects from 1 to j.

Let Vj represent value of the jth object and Wj the weight of the jth object.

So now let us suppose we know all answers till j-1. Now for finding answer for j,R we have two choices-

  1. Do not take the jth object. In this case answer is same as answer of j-1,R
  2. Take the jth object. Now the answer will be Wj more than the ans for R-Vj since we want R as the revenue after taking this object of value Vj. Also note that if R < Vj we cannot take this object.

So here is the final recurrence

So if R >= Vj

OPT(j, R) = min(OPT(j - 1, R), OPT(j - 1, R - Vj) + Wj)

otherwise if R < Vj

  OPT(j, R) = OPT(j - 1, R)

and OPT(j, 0) = 0 for all j = 0 to n. This is beacuse 0 value requires 0 capacity.

and OPT(0, R) = infinite for all R > 0. This is because without selecting any element it's impossible to generate a non 0 revenue. For practical purposes we will take infinite as a very large number like INT_MAX in cpp.

(b) Now we need to create a table with rows indexed from 0 to n representing j and columns labelled from 0 to MAXR representing revenue R. But what is MAXR? What is the maximum revenue we can generate ignoring the constraint? It is the sum of all values of all objects because we cannot generate more than that for sure.

(i) Now to fill the table we will first fill in the first column, R = 0 with 0s and the first row, j = 0, R > 0 with INF(a very large value representing infinity). Now we will start filling the table in top down manner taking the minimum of value just above it and the value Vj cell left of the cell immidiately above it + Wj.

(ii) Now once the table is filled we can get the answer by iterating in the last row j = n and finding the largest R where the cell contains a value <= K (the given capacity). This R is the max revenue that can be obtained.

(c) Runtime of this lagorithm is O(n*(MAXR)) where MAXR is the sum of all values of the objects. This is because we fill the table one cell at a time and the table is of size n*MAXR. We can use this algorithm if the weights are very large but the values of the objects are small.


Related Solutions

In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a)...
In C++ or Java Write the Greedy programming Algorithm for the 0-1 Knapsack problem.                    (a) No fraction allowed Max Weight W= 9 item 1: profit $20, weight 2, prof/weight=$10 item 2: profit $30, weight 5, prof/weight=$6 item 3: profit $35, weight 7, prof/weight=$5 item 4: profit $12, weight 3, prof/weight=$4 item 5: profit $3, weight 1, prof/weight=$3
Implement the dynamic algorithm of 0-1 knapsack problem in Java. The following table shows 10 items,...
Implement the dynamic algorithm of 0-1 knapsack problem in Java. The following table shows 10 items, their values and weights. The maximum weight capacity of the knapsack is 113. What could be the maximum value of the knapsack and the most valuable set of items that fit in the knapsack? Item Weight Value 1 32 727 2 40 763 3 44 60 4 20 606 5 1 45 6 29 370 7 3 414 8 13 880 9 6 133...
Provide the dynamic-programming recurrence for a function that is used to solve 0-1 Knapsack. Clearly define...
Provide the dynamic-programming recurrence for a function that is used to solve 0-1 Knapsack. Clearly define what the function is computing.
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, ....
5. Design a dynamic programming algorithm to solve the following problem. Input: An array A[1, . . . , n] of positive integers, an integer K. Decide: Are there integers in A such that their sum is K. (Return T RUE or F ALSE) Example: The answer is TRUE for the array A = [1, 2, 3] and 5, since 2 + 3 = 5. The answer is FALSE for A = [2, 3, 4] and 8. Note that you...
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of...
Knapsack algorithm problem: Consider the following variation of the Knapsack problem. There are n types of items, let call them 1,2,3,4,...,n. There are exactly c_i copies of item i, and each such copy has value v_i and weight w_i. As before, the knapsack capacity is W, and the other constraint is that you can only take at most c_i copies of item i ( since no more are available). Show how to compute the optimal value that can be achieved...
how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is...
Problem 2. Purpose: practice algorithm design using dynamic programming. A subsequence is palindromic if it is the same whether read left to right or right to left. For instance, the sequence A,C,G,T,G,T,C,A,A,A,A,T,C,G has many palindromic subsequences, including A,C,G,C,A and A,A,A,A (on the other hand, the subsequence A,C,T is not palindromic). Assume you are given a sequence x[1...n] of characters. Denote L(i,j) the length of the longest palindrome in the substring x[i,...,j]. The goal of the Maximum Palindromic Subsequence Problem (MPSP)...
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels,...
Build a Dynamic Programming algorithm in Python for the following problem. Given two sequences of vowels, find the length of longest subsequence present in both of them. NOTE: A subsequence is a sequence that appears in the same order, but not necessarily contiguous. For example, “aei”, “aeo”, “eio”, “aiu”, ... are subsequences of “aeiou”. Sample Input 1: aeiou aiu Sample Output 1: 3 Sample Input 2: aeaiueoiuaeeoeooaauoi aeuioauuuoaeieeaueuiouaiieuiuuuaoueueauaeiauuo Sample Output 2: 16 Sample Input 3: iioioieiaiauaoeoiioiiue iuuueauiaieooaoaaouaaaae Sample Output 3:...
What are the different algorithms we may use to solve the 0/1 Knapsack problem? Compare and...
What are the different algorithms we may use to solve the 0/1 Knapsack problem? Compare and contrast the different methods.
For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory...
For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory throughout the computation, in order to compute the value of the best packing of the knapsack? For the 0-1 Knapsack algorithm, is it necessary to maintain the entire 2-dimensional table in memory throughout the computation, in order to identify the items that are included in an optimal packing of the knapsack? For the Longest Common Subsequence algorithm, is it necessary to maintain the entire...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT