Question

In: Computer Science

There are many variations on the Knapsack problem. In this question, consider the same set up...

There are many variations on the Knapsack problem. In this question, consider the same set up as the 0-1 Knapsack problem except suppose there are as many copies of each item as you would like. Thus, if one of the items is small but very valuable, it is possible to put many exact duplicates of that item into the knapsack. To maximize the value of items in the knapsack, the thief may decide to put in as many of the high value, low weight items as possible into the Knapsack

Giving up on the greedy approach and not seeing how to approach this problem using divide and conquer, you decide to try dynamic programming.

  1. Specify the function that represents the quantity to be optimized.
  2. Give the recurrence relation that describes the optimal substructure of the problem using
  3. Give the specification of the table that you would use in a bottom up programmatic solution.   Specify the dimensions of the table and what each entry in the table represents.
  4. Write the pseudo code of the algorithm for filling in the table that you would use in a bottom up programmatic solution. That is convert the recurrence relation (part B.) to an iterative algorithm.
  5. Write the pseudo code of the algorithm for tracing back through the table to find the set of items that gives the maximum total value.
  6. Write the asymptotic complexity of filling in the table.

Solutions

Expert Solution

For A and B: Following function can be used

OPT(i, w) = max(OPT(i-1, w) or vi + OPT(i, w-wi))
                     ^                   ^
          removed the element      not removing the element

// dp[i][j] -> maximum weight j  that we can have after processing i elements.
// the size of the table will be O(W)after optimisation
dp[i] = 0
dp[i] = max(dp[i], dp[i-wt[j]] + val[j] 
                   where j varies from 0 
                   to n-1 such that:
                   wt[j] <= i

result = d[W]
int unboundedKnapsack(int W, int n, 
                       int val[], int wt[])
{
    // dp[i] is going to store maximum value
    // with knapsack capacity i.
    int dp[W+1];
    memset(dp, 0, sizeof dp);
 
    // Fill dp[] using above recursive formula
    for (int i=0; i<=W; i++)
      for (int j=0; j<n; j++)
         if (wt[j] <= i)
            dp[i] = max(dp[i], dp[i-wt[j]] + val[j]);
 
    return dp[W];
}

For tracing back

// stores the result of Knapsack 
    int res = dp[n][W];     
    printf("%d\n", res); 
      
    w = W; 
    for (i = n; i > 0 && res > 0; i--) { 
          
        // either the result comes from the top 
        // (dp[i-1][w]) or from (val[i-1] + dp[i-1] 
        // [w-wt[i-1]]) as in Knapsack table. If 
        // it comes from the latter one/ it means  
        // the item is included. 
        if (res == dp[i - 1][w])  
            continue;         
        else { 
  
            // This item is included. 
            printf("%d ", wt[i - 1]); 
              
            // Since this weight is included its  
            // value is deducted 
            res = res - val[i - 1]; 
            w = w - wt[i - 1]; 
        } 
    } 

Since we are traversing the whole table only once, the complexity will be


Related Solutions

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...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible...
The Knapsack problem is an optimization problem that asks to fill a knapsack with maximum possible value. Using greedy paradigm, we can solve this problem easily. Your task is the following: (a) Write the pseudo-code of a greedy solution for knapsack problem. (b) Give is the time complexity of your solution in part (a). (c) Implement part (a) in C programming language.
Consider the following items in the Knapsack Problem: Item                weight             value &nbsp
Consider the following items in the Knapsack Problem: Item                weight             value            Knapsack capacity W = 9. 1                      3                   $6 2 4 $12 3                      2                  $10 4                      5                  $20 Determine the maximum value in the knapsack if we allow repetitions; i.e., if there are an unlimited number of each item so that more than one such item can be chosen. Find the missing value in the following linear array. P(w) is the maximum profit obtainable for a knapsack of capacity w. w-->             0       1      2       3       4       5       6       7       8       9 P(w):    0 0 10 10     20     20     30    30 40 ?   P(9)...
Consider the knapsack problem with the capacity C = 8 and 5 items with weights 6,...
Consider the knapsack problem with the capacity C = 8 and 5 items with weights 6, 4, 3, 7, 1. Find which items will exactly fill the knapsack using dynamic programming solution introduced in class. Show all your work. use I/O knapsack
Consider the isatin yield experiment below. Set up the 2^4 experiment in this problem in two...
Consider the isatin yield experiment below. Set up the 2^4 experiment in this problem in two blocks with ABCD confounded. Analyze the data from this design. Is the block effect large?. Show the steps to perform this in Minitab. factor low high A:acid strength (%) 87 93 B:Reaction time (min) 15 30 C:Amount of acid (ml) 35 45 D:reaction temperature (c) 60 70 A B C D yield -1 -1 -1 -1 6.08 1 -1 -1 -1 6.04 -1 1...
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack...
Urgent! How do you do solve a knapsack problem recursively in JAVA. for an arbitrary knapsack capacity and series of weights. Assume the weights are sorted in an array. **The arguments to the knapsack function are target weight and the array index where the remaining items start. For example if you want your knapsack to weigh exactly 20 pounds and you have five items with weights of 11,8,7,6 and 5 pounds. In this case only combination that works is 8,...
how to write a genetic algorithm for a knapsack problem .
how to write a genetic algorithm for a knapsack problem .
Marketers increasingly emphasize a two-tier, “Tiffany/Walmart” strategy. Many firms now offer different variations of the same...
Marketers increasingly emphasize a two-tier, “Tiffany/Walmart” strategy. Many firms now offer different variations of the same basic offering to high-end and low-end segments. Gap’s Banana Republic chain sells blue jeans for $58, whereas Old Navy stores sell a slightly different version for $22. What do you all think of this marketing strategy as it pertains to targeting different markets? Are you aware of other examples of this strategy in place? How does it - or would it - impact your...
In this experiment, you will set things up again the same way you did in the...
In this experiment, you will set things up again the same way you did in the very first, only this time you will add Mycobacterium tuberculosis instead of yeast. Graph the data as above, and answer the questions below. Time (minutes) no sugar glucose sucrose maltose lactose galactose 0 0 0 0 0 0 0 5 0 3 0 0 2.8 3.1 10 0 6 0 0 5.6 6.2 15 0 9 0 0 8.8 9.3 20 0 12 0...
Carefully work through the sample calculations for this lab ON PAPER. Then, set up the same...
Carefully work through the sample calculations for this lab ON PAPER. Then, set up the same equations on a separate sheet of paper, but replace the values in the ‘Sample Calculations’ folder with the ones provided below and complete the calculations: Mass of Zinc = 0.0928 g H2 volume reading from the eudiometer tube = 35.98 mL Water bath temperature = 19.9 °C Barometric pressure = 768.2 mm Hg Level (pressure) difference from the meter stick = 41.20 cm H2O...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT