190k views
3 votes
Suppose you won a shopping spree and you can take any number of items from the Amazon store (assume that there is only one copy of each item). There are n items in the store each of value v[1..n] and weights w[1..n]. You are given a trolley that can only carry a total of W weight of items. Don't worry about the size of the items, they would somehow magically fit in the trolley. You want to find the maximum value of items that you can pick. Being a programmer you decide to use dynamic programming to solve this problem. Write the recurrence formula that would represent the approach of your solution. Mention both the base case and the recursive case of the recurrence formula.

1 Answer

7 votes

Final answer:

The recurrence formula for the given problem is defined by a base case and a recursive case. The base case handles the scenario when there are no items to choose from or the weight capacity of the trolley is 0. The recursive case considers the options of including or excluding the nth item in the selection, and recursively finding the maximum value of the remaining items.

Step-by-step explanation:

Recurrence Formula:

The recurrence formula for the given problem can be stated as follows:

Base Case:

If there are no items to choose from (n = 0) or the weight capacity of the trolley is 0 (W = 0), then the maximum value of items that can be picked is 0.

Recursive Case:

If there are items to choose from (n > 0) and the weight capacity of the trolley is greater than 0 (W > 0), then the maximum value of items that can be picked is the maximum of the following two options:

1. Include the nth item in the selection, reduce the weight capacity by the weight of the item, and recursively find the maximum value of the remaining items.
2. Exclude the nth item from the selection and recursively find the maximum value of the remaining items with the current weight capacity.

User Mina HE
by
7.6k points