



Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Dynamic Programming Approach, Knapsack Problem, Constructing the Optimal Solution, Keep matrix, Inductive approach, Leave object, Take object, Recursive formulation are the key points in this study notes.
Typology: Study notes
1 / 5
This page cannot be seen from the preview
Don't miss anything!




For each i ≤ n and each w ≤ W, solve the knapsack problem for the first i objects when the capacity is w. Why will this work? Because solutions to larger sub problems can be built up easily from solutions to smaller ones. We construct a matrix V[0... n, 0.. .W]. For 1 ≤ i ≤ n, and 0 ≤ j ≤ W, V[i, j] will store the maximum value of any set of objects {1, 2,... , i} that can fit into a knapsack of weight j. V[n,W] will contain the maximum value of all n objects that can fit into the entire knapsack of weight W.
To compute entries of V we will imply an inductive approach. As a basis, V[0, j] = 0 for 0 ≤ j ≤ W since if we have no items then we have no value. We consider two cases:
Leave object i : If we choose to not take object i, then the optimal value will come about by considering how to fill a knapsack of size j with the remaining objects {1, 2,... , i - 1}. This is just V[i - 1, j].
Take object i : If we take object i, then we gain a value of v i. But we use up wi of our capacity. With the remaining j - wi capacity in the knapsack, we can fill it in the best possible way with objects {1, 2,... , i - 1}. This is vi + V[i - 1, j - wi]. This is only possible if wi _ j. This leads to the following recursive formulation:
A naive evaluation of this recursive definition is exponential. So, as usual, we avoid re- computation by making a table.
Example: The maximum weight the knapsack can hold is W is 11. There are five items to choose from. Their weights and values are presented in the following table:
The [i, j] entry here will be V[i, j], the best value obtainable using the first i rows of items if the maximum capacity were j. We begin by initializating and first row.
Recall that we take V[i, j] to be 0 if either i or j is _ 0. We then proceed to fill in top- down, left-to-right always using
V[i, j] = max_V[i - 1, j], vi + V[i - 1, j - wi]
The time complexity is clearly O(n ·W). It must be cautioned that as n and W get large, both time and space complexity become significant.
Constructing the Optimal Solution The algorithm for computing V[i, j] does not keep record of which subset of items gives the optimal solution. To compute the actual subset, we can add an auxiliary boolean array keep[i, j] which is 1 if we decide to take the ith item and 0 otherwise. We will use all the values keep[i, j] to determine the optimal subset T of items to put in the knapsack as follows:
We will add this to the knapsack algorithm:
Here is the keep matrix for the example problem.
When the item selection algorithm is applied, the selected items are 4 and 3. This is indicated by the boxed entries in the table above.