Question

In: Computer Science

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.

Solutions

Expert Solution

DYNAMIC PROGRAMMING RECURRENCE FOR 0-1 KNAPSACK PROBLEM

  • Let f(i,y) be the profit value of the optimal solution to the knapsack instance defined by the state (i,y). Items i through n are available. Available capacity is y.
  • For the time being assume that we wish to determine only the value of the best solution. Later we will worry about determining the xis that yield this maximum value.
  • Under this assumption, our task is to determine f(1,c).
  • f(n,y) is the value of the optimal solution to the knapsack instance defined by the state (n,y). Only item n is available. Available capacity is y.
  • If wn <= y, f(n,y) = pn .
  • If wn > y, f(n,y) = 0.
  • Suppose that i < n.
  • f(i,y) is the value of the optimal solution to the knapsack instance defined by the state (i,y). Items i through n are available. Available capacity is y.
  • Suppose that in the optimal solution for the state (i,y), the first decision is to set xi= 0.
  • From the principle of optimality (we have shown that this principle holds for the knapsack problem), it follows that f(i,y) = f(i+1,y).
  • The only other possibility for the first decision is xi= 1.
  • The case xi= 1 can arise only when y >= wi.
  • From the principle of optimality, it follows that f(i,y) = f(i+1,y-wi ) + pi.
  • Combining the two cases, we get
    • f(i,y) = f(i+1,y) whenever y < wi.
    • f(i,y) = max{f(i+1,y), f(i+1,y-wi ) + pi }, y >= wi

The function for knapsack using dynamic programming:

static int knapSack(int W, int wt[], int val[], int n) 
    { 
        int i, w; 
        int K[][] = new int[n + 1][W + 1]; 
  
        // Build table K[][] in bottom up manner 
        for (i = 0; i <= n; i++) { 
            for (w = 0; w <= W; w++) { 
                if (i == 0 || w == 0) 
                    K[i][w] = 0; 
                else if (wt[i - 1] <= w) 
                    K[i][w] = max( 
                        val[i - 1] + K[i - 1][w - wt[i - 1]], 
                        K[i - 1][w]); 
                else
                    K[i][w] = K[i - 1][w]; 
            } 
        } 
  
        return K[n][W]; 
    } 

Explanation of the above function:

Let weight elements = {1, 2, 3}
Let weight values = {10, 15, 40}
Capacity=6

   0   1   2   3   4   5   6

0  0   0   0   0   0   0   0

1  0  10  10  10  10  10  10

2  0  10  15  25  25  25  25

3  0
 
Explanation:
​
Let weight elements = {1, 2, 3}
Let weight values = {10, 15, 40}
Capacity=6

   0   1   2   3   4   5   6

0  0   0   0   0   0   0   0

1  0  10  10  10  10  10  10

2  0  10  15  25  25  25  25

3  0

Explanation:
For filling 'weight = 2' we come 
across 'j = 3' in which 
we take maximum of 
(10, 15 + DP[1][3-2]) = 25   
  |        |
'2'       '2 filled'
not filled  


   0   1   2   3   4   5   6

0  0   0   0   0   0   0   0

1  0  10  10  10  10  10  10

2  0  10  15  25  25  25  25

3  0  10  15  40  50  55  65

Explanation:
For filling 'weight=3', 
we come across 'j=4' in which 
we take maximum of (25, 40 + DP[2][4-3]) 
= 50

For filling 'weight=3' 
we come across 'j=5' in which 
we take maximum of (25, 40 + DP[2][5-3])
= 55

For filling 'weight=3' 
we come across 'j=6' in which 
we take maximum of (25, 40 + DP[2][6-3])
= 65

Related Solutions

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)...
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
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.
Choose a problem that lends to an implementation that uses dynamic programming. Clearly state the problem...
Choose a problem that lends to an implementation that uses dynamic programming. Clearly state the problem and then provide high-level pseudocode for the algorithm. Explain why this algorithm can benefit from dynamic programming.
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...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the...
Can you please solve this using recursion/ dynamic programming? Any programming language is fine. Wallace the Weightlifting Walrus is training for a contest where it will have to lift 1000 kg. Wallace has some weight plates lying around, possibly of different weights, and its goal is to add some of the plates to a bar so that it can train with a weight as close as possible to 1000 kg. In case there exist two such numbers which are equally...
Solve the recurrence relation with the given initial conditions. b0 = 0, b1 = 4, bn...
Solve the recurrence relation with the given initial conditions. b0 = 0, b1 = 4, bn = 2bn ? 1 + 2bn ? 2 for n ? 2
Solve the following problem by Dynamic Programming: Maximize z = (y1 + 2)^2 + y2 *...
Solve the following problem by Dynamic Programming: Maximize z = (y1 + 2)^2 + y2 * y3 + (y4 - 5)^2 subject to y1 + y2 + y3 + y4 <= 5 yi >= 0 and integer, i = 1, 2, 3, 4
for y(t) function ty'' - ty' + ty = 0, y(0)= 0 , y'(0)= 1 solve...
for y(t) function ty'' - ty' + ty = 0, y(0)= 0 , y'(0)= 1 solve this initial value problem by using Laplace Transform. (The equation could have been given such as "y'' - y' + y = 0" but it is not. Please, be careful and solve this question step by step.) )
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;...
Give a recursive algorithm to solve the following recursive function. f(0) = 0;    f(1) = 1;   f(2) = 4; f(n) = 2 f(n-1) - f(n-2) + 2; n > 2 b) Solve f(n) as a function of n using the methodology used in class for Homogenous Equations. Must solve for the constants as well as the initial conditions are given.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT