Dynamic Programming Approach, Knapsack Problem - Design and Analysis - Study Notes, Study notes of Digital Systems Design

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

2011/2012

Uploaded on 11/03/2012

ankitay
ankitay 🇮🇳

4.4

(50)

106 documents

1 / 5

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Lecture No. 22
6.5.1 0/1 Knapsack Problem: Dynamic Programming Approach
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 vi. 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:
Docsity.com
pf3
pf4
pf5

Partial preview of the text

Download Dynamic Programming Approach, Knapsack Problem - Design and Analysis - Study Notes and more Study notes Digital Systems Design in PDF only on Docsity!

Lecture No. 22

6.5.1 0/1 Knapsack Problem: Dynamic Programming Approach

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:

  • If keep[n,W] is 1, then n 2 T. We can now repeat this argument for keep [n - 1,W - wn ].
  • If kee[n,W] is 0, the n 62 T and we repeat the argument for keep[n - 1,W].

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.