2. 0/1 Knapsack Problem
• A thief goes into a jewelry store to steal jewelry items. He
has a knapsack (a bag) that he would like to fill up.
• The bag has a limit on the total weight of the objects
placed in it. If the total weight exceeds the limit, the bag
would tear open.
• The value of the jewelry items varies for cheap to
expensive.
• The thief’s goal is to put items in the bag such that the
value of the items is maximized and the weight of the
items does not exceed the weight limit of the bag.
• Another limitation is that an item can either be put in the
bag or not - fractional items are not allowed.
3. 0/1 Knapsack Problem
• The problem is: what jewelry should the thief choose that
satisfy the constraints?
• Formally, the problem can be stated as follows: Given a
knapsack with maximum capacity W, and a set S consisting
of n items.
• Each item i has some weight wi and value value vi (all wi ,
vi and W are integer values).
• How to pack the knapsack to achieve maximum total value
of packed items?
5. 0/1 Knapsack Problem
• The knapsack problem belongs to the domain of
optimization problems. Mathematically, the problem is:
• The problem is called a “0-1” problem, because each
item must be entirely accepted or rejected. How do
we solve the problem.
6. 0/1 Knapsack Problem
• We could try the brute-force solution:
-Since there are n items, there are 2n possible
combinations of the items (an item either chosen or
not).
-We go through all combinations and find the one
with the most total value and with total weight
less or equal to W.
• Clearly, the running time of such a brute-force
algorithm will be O(2n). Can we do better? The answer
is “yes”, with an algorithm based on dynamic
programming.
7. 0/1 Knapsack Problem-DP 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
8. 0/1 Knapsack Problem
• 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.
9. 0/1 Knapsack Problem
•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 .
•
10. 0/1 Knapsack Problem
• 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.
11. 0/1 Knapsack Problem
• 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
16. 0/1 Knapsack Problem-DP Algorithm
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.
17. 0/1 Knapsack Problem-DP Algorithm
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:
19. 0/1 Knapsack Problem
• 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.