18.04.2013 Views

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

The.Algorithm.Design.Manual.Springer-Verlag.1998

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Knapsack Problem<br />

most expensive item, until the knapsack is full. Unfortunately, this 0/1 constraint is usually inherent in<br />

most applications.<br />

Issues that arise in selecting the best algorithm include:<br />

● Does every item have the same cost/value or the same size? - If each item is worth the same<br />

amount to us as any other item, say $1, I maximize my value by taking the greatest number of<br />

items. In this case the optimal solution is to sort the items in order of increasing size and insert<br />

them into the knapsack in this order until nothing fits. <strong>The</strong> problem is solved similarly when<br />

each object has the same size but the costs are different. <strong>The</strong>se are the easy cases of knapsack.<br />

● Does each item have the same ``price per pound''? - In this case, our problem is equivalent to<br />

ignoring the price and just trying to minimize the amount of empty space left in the knapsack.<br />

Unfortunately, even this restricted version of the problem is NP-complete, and so we cannot<br />

expect an efficient algorithm that always solves the problem. Don't lose hope, however, because<br />

knapsack proves to be an ``easy'' hard problem, one that can usually be handled with the<br />

algorithms described below.<br />

An important special case of constant ``price-per-pound'' knapsack is the integer partition<br />

problem, presented in cartoon form in Figure .<br />

Figure: Integer partition is a variant of the Knapsack problem<br />

Here, we seek to partition the elements of S into two sets A and B such that , or<br />

alternately make the difference as small as possible. Integer partition can be thought of as bin<br />

packing with two equal-sized bins or knapsack with a capacity of half the total weight, so all three<br />

problems are closely related and NP-complete.<br />

<strong>The</strong> constant `price-per-pound' knapsack problem is often called the subset sum problem, because<br />

given a set of numbers, we seek a subset that adds up to a specific target number, i.e. the<br />

capacity of our knapsack.<br />

● Are all the sizes relatively small integers? - When the sizes of the items and the knapsack capacity<br />

C are all integers, there is an efficient dynamic programming algorithm that finds the optimal<br />

solution in time O(n C) and O(C) space. Whether this is good for you depends upon how big C is.<br />

For 1,000, this might be great, but not for 10,000,000.<br />

<strong>The</strong> algorithm works as follows: Let S' be a set of items, and let C[i,S'] be true if and only if there<br />

file:///E|/BOOK/BOOK4/NODE145.HTM (2 of 4) [19/1/2003 1:30:33]

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!