This preview shows page 1. Sign up to view the full content.
Unformatted text preview: lid subproblem deﬁnition, but it is not at all clear how to deﬁne an optimal
solution for index k in terms of optimal subproblem solutions. Our hope would be
that we would be able to derive an equation that takes the best solution using items
from Sk−1 and considers how to add the item k to that. Unfortunately, if we stick
with this deﬁnition for subproblems, then this approach is fatally ﬂawed. For, as we
show in Figure 5.7, if we use this characterization for subproblems, then an optimal
solution to the global problem may actually contain a suboptimal subproblem. (a) (3,2) (5,4) (8,5) (b) (3,2) (5,4) (8,5) (4,3) (10,9) 20
Figure 5.7: An example showing that our ﬁrst approach to deﬁning a knapsack subproblem does not work. The set S consists of ﬁve items denoted by the the
(weight, beneﬁt) pairs (3, 2), (5, 4), (8, 5), (4, 3), and (10, 9). The maximum total
weight is W = 20: (a) best solution with the ﬁrst four items; (b) best solution with
the ﬁrst ﬁve items. We shade each item in proportion to its beneﬁt. Chapter 5. Fundamental Techniques 280 A Better Subproblem Characterization
One of the reasons that deﬁning subproblems only in terms of an index k is fatally ﬂawed is that there is not enough information represented in a subproblem
to provide much help for solving the global optimization problem. We can correct
this difﬁculty, however, by adding a second parameter w. Let us therefore formulate
each subproblem as that of computing B[k, w], which is deﬁned as the maximum total value of a subset of Sk from among all those subsets having total weight exactly
w. We have B[0, w] = 0 for each w ≤ W , and we derive the following relationship
for the general case
B[k, w] = B[k − 1, w]
if wk > w
max{B[k − 1, w], B[k − 1, w − wk ] + bk } else. That is, the best subset of Sk that has total weight w is either the best subset of Sk−1
that has total weight w or the best subset of Sk−1 that has total weight w − wk plus
the item k. Since the best subset of Sk that has total weight w must either contain
item k or not, one of these two choices must be the right choice. Thus, we have
a subproblem deﬁnition that is simple (it involves just two parameters, k and w)
and satisﬁes the subproblem optimization condition. Moreover, it has subproblem
overlap, for the optimal way of summing exactly w to weight may be used by many
future subproblems.
In deriving an algorithm from this deﬁnition, we can make one additional observation, namely, that the deﬁnition of B[k, w] is built from B[k − 1, w] and possibly
B[k − 1, w − wk ]. Thus, we can implement this algorithm using only a single array B,
which we update in each of a series of iterations indexed by a parameter k so that at
the end of each iteration B[w] = B[k, w]. This gives us Algorithm 5.8 (01Knapsack).
Algorithm 01Knapsack(S, W ):
Input: Set S of n items, such that item i has positive beneﬁt bi and positive
integer weight wi ; positive integer maximum total weight W
Output: For w = 0, . . . , W , maximum beneﬁt B[w] of a subset of S with total
weight w
for w ← 0 to W do
B[w] ← 0
for k ← 1 to n do
for w ← W downto wk do
if B[w − wk ] + bk > B[w] then
B[w] ← B[w − wk ] + bk
Algorithm 5.8: Dynamic programming algorithm for solving the 01 knapsack problem. 5.3. Dynamic Programming 281 Analyzing the 01 Knapsack Dynamic Programming Algorithm
The running time of the 01Knapsack algorithm is dominated by the two nested
forloops, where the outer one iterates n times and the inner one iterates at most W
times. After it completes we can ﬁnd the optimal value by locating the value B[w]
that is greatest among all w ≤ W . Thus, we have the following:
Theorem 5.16: Given an integer W and a set S of n items, each of which has a
positive beneﬁt and a positive integer weight, we can ﬁnd the highest beneﬁt subset
of S with total weight at most W in O(nW ) time.
Proof: We have given Algorithm 5.8 (01Knapsack) for constructing the value of
the maximumbeneﬁt subset of S that has total weight at most W using an array B of
beneﬁt values. We can easily convert our algorithm into one that outputs the items
in a best subset, however. We leave the details of this conversion as an exercise. PseudoPolynomialTime Algorithms
In addition to being another useful application of the dynamic programming technique, Theorem 5.16 states something very interesting. Namely, it states that the
running time of our algorithm depends on a parameter W that, strictly speaking, is
not proportional to the size of the input (the n items, together with their weights and
beneﬁts, plus the number W ). Assuming that W is encoded in some standard way
(such as a binary number), then it takes only O(log W ) bits to encode W . Moreover,
if W is very large (say W = 2n ), then this dynamic programming algorithm would
actually be asymptotically slower than the brute force method. Thus, technically
speaking, this algorithm is not a polynomialtime algorithm, for its running time is
not actually a fu...
View
Full
Document
This document was uploaded on 03/26/2014.
 Spring '14

Click to edit the document details