GATE | GATE-CS-2003 | Question 90
Consider the following dynamic programming code snippet for solving the 0/1 Knapsack problem:
Given the values [60, 100, 120] and weights [10, 20, 30], what would be the output of calling knapsack(values, weights, 50, 3)?
The output is 220 because the knapsack function, using dynamic programming, selects items from the given values and weights that maximize the total value while not exceeding the knapsack’s weight capacity of 50. Among the available items, the optimal selection would be to include the items with values 60 and 120, which sum up to 180, and the sum of their corresponding weights up to 40, leaving room to include the item with a value of 100. Hence, the total value obtained is 220, which is the highest achievable value given the constraints.
Quiz of this Question
Please comment below if you find anything wrong in the above post
Please Login to comment...