Knapsack Problem Intro
Knapsack Problem Intro
KNAPSACK PROBLEM
Knapsack
A 1998 study of the Stony Brook University Algorithm
Repository showed that, out of 75 algorithmic problems,
the knapsack problem was the 18th most popular and the
4th most needed after kd-trees, suffix trees, and the bin
packing problem
Knapsack problem
Given a set of items, each with a mass and a value,
determine the number of each item to include in a
collection so that the total weight is less than or equal to a
given limit and the total value is as large as possible.
1-Ring 1 15
2-Candelabra 5 10
3-Radio 3 9
4-Elvis 4 5
The problem is, which items should the thief take? If the
knapsack were large enough, the thief could take all of the
items and run. But, that is not the case (the problem states
that the knapsack cannot hold all of the items).
The foolish and slow thief climbs in the window, and sees the
items. This thief was a programmer, downsized as part of the
"dot-bomb" blowout. Possessing a solid background in Boolean
logic, he figures that he can simply compute all combinations of
the objects and choose the best. So, he starts going through the
binary combinations of objects - all 2^5 of them. While he is still
drawing the truth table, the police show up, and arrest him.
Although his solution would certainly have given him the best
answer, it just took long to compute.
The wise thief appears, and observes the items. He
notes that an empty knapsack has a value of 0. Further,
he notes that a knapsack can either contain each item,
or not. Further, his decision to include an item will be
based on a quick calculation - either the knapsack with
some combination of the previous items will be worth
more, or else the knapsack of a size that will fit the
current item was worth more. So, he does this quick
computation, and figures out that the best knapsack
he can take is made up of items 1,3, and 4, for a total
value of 29.
Knapsack problem
There are two versions of the problem:
1. “0-1 knapsack problem”
Items are indivisible; you either take an item or not.
Some special instances can be solved with dynamic
programming
Exhibit No greedy choice property.
No greedy algorithm exists.
Exhibit optimal substructure property.
Only dynamic programming algorithm exists.
.
1. “Fractional knapsack problem”
Items are divisible: you can take any fraction of an item
Exhibit greedy choice property.
Greedy algorithm exists.
Exhibit optimal substructure property
0-1 Knapsack Problem
The thief wants to steal n items
The ith item weighs wi and has value vi
Take the most valuable load, limit of W pounds
This is called the 0-1 version of the problem because
there are no fractions.
The thief must take the whole item or leave it behind.
Both the 0-1 Knapsack Problem and the Fractional
Knapsack Problem have optimal substructure. If we
remove item j from the optimal load, the remaining
problem must be optimal for the remaining W -
wj pounds for the overall solution to be optimal.
The greedy solution for the Fractional Knapsack
Problem does not work here
Item Value Weight
Example: 1 60 10
2 100 20
3 120 30
W = 50
The greedy solution would select 1, leaving 40 pounds.
At this point if we pick items 1 and 2 the total value is
160.
If we pick items 1 and 3 the total value is 180.
The optimal solution is to pick items 2 and 3. The total
value of this solution is 220
Example: Fractional Knapsack
Problem