Section 1.9: The Knapsack Problem

advertisement
Math for Liberal Studies

The knapsack problem is a variation of the bin
packing problems we have been discussing

This time, there is just one bin

The weights have values, and you can use
each weight as many times as you want

The goal is to pack the bin so that it has the
highest possible value

In this example, we have 4 weights, and our
bin (“knapsack”) can hold 10 pounds

We label the weights to make it easier

How should we pack our knapsack?

There are many possible solutions

This one has a total value of $15

How should we pack our knapsack?

There are many possible solutions

This one also has a total value of $15

This one also has a total value of $15

Notice that we have 1 lb of space left over

Since none of our weights weigh 1 lb, this
space must remain empty

We will use a recursive algorithm to find the
best solution to this problem

Think about the last weight that we would
pack into the best possible 10 lb knapsack

The optimal 10 lb knapsack will look like one
of these:

But how do we determine, for example, what
the optimal 8 lb knapsack is?

To find the optimal 8 lb knapsack, we will use
the same idea

The best 8 lb knapsack is one of these:
 The best 6 lb knapsack + A
 The best 5 lb knapsack + B
 The best 4 lb knapsack + C
 The best 3 lb knapsack + D

If we keep going backwards like this, our
knapsacks can’t keep getting smaller forever

Eventually we will be asking ourselves what
the optimal 0 lb knapsack is

If a knapsack can hold zero pounds, then it
has to be empty!

This empty knapsack becomes our starting
point

Make a chart that lists capacities from 0 up
through the capacity of the knapsack

Starting with 0, 1, 2, etc., determine the
optimal knapsack of each capacity until you
reach the capacity of the original knapsack

We start off by making our
chart
Capacity
Contents
Value
0
empty
$0
1
2

The 0-capacity knapsack
must be empty
3
4
5
6
7
8
9
10

Since our smallest weight is
2 lbs, the 1-lb knapsack
must also be empty
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
3
4
5
6
7
8
9
10

Once we reach a capacity
of 2, the only way to pack
our knapsack is to use a
single “A”, worth $3
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
4
5
6
7
8
9
10

At capacity 3, we have a
choice:
Capacity
Contents
Value
0
empty
$0
1
empty
$0
 pack A with 1 lb left over
2
A
$3
 pack B
3
B
$4
4

Since B is worth $4, this is
the better option
5
6
7
8
9
10

Capacity
Contents
Value
0
empty
$0
1
empty
$0
 optimal 2-lb knapsack + A
2
A
$3
 optimal 1-lb knapsack + B
3
B
$4
4
?
?
At capacity 4, we start
having more options:
 optimal 0-lb knapsack + C
5
6

Note we
still can’t
fit a D
7
8
9
10


Capacity
Contents
Value
 This is two A’s, worth $6
0
empty
$0
1
empty
$0
optimal 1-lb knapsack + B
2
A
$3
3
B
$4
4
C
$7
optimal 2-lb knapsack + A
 This is just one B, worth $4

optimal 0-lb knapsack + C
 This is one C, worth $7
5
6
7
8

The third option is best
9
10

Capacity 5 options:
 optimal 3 + A
 optimal 2 + B
 optimal 1 + C
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
?
?
6
7
 optimal 0 + D
8
9
10

Capacity 5 options:
 optimal 3 + A
▪ This is B & A, worth $7
 optimal 2 + B
 optimal 1 + C
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
?
?
6
7
 optimal 0 + D
8
9
10

Capacity 5 options:
 optimal 3 + A
▪ This is B & A, worth $7
 optimal 2 + B
▪ This is A & B, worth $7
 optimal 1 + C
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
?
?
6
7
 optimal 0 + D
8
9
10

Capacity 5 options:
 optimal 3 + A
▪ This is B & A, worth $7
 optimal 2 + B
▪ This is A & B, worth $7
 optimal 1 + C
▪ This is C, worth $7
 optimal 0 + D
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
?
?
6
7
8
9
10

Capacity 5 options:
 optimal 3 + A
▪ This is B & A, worth $7
 optimal 2 + B
▪ This is A & B, worth $7
 optimal 1 + C
▪ This is C, worth $7
 optimal 0 + D
▪ This is D, worth $8
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
?
?
6
7
8
9
10

Capacity 6 options:
 optimal 4 + A
▪ This is C & A, worth $10
 optimal 3 + B
▪ This is two B’s, worth $8
 optimal 2 + C
▪ This is A & C, worth $10
 optimal 1 + D
▪ This is D, worth $8
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
D
$8
6
?
?
7
8
9
10

Capacity 7 options:
 optimal 5 + A
▪ This is D & A, worth $11
 optimal 4 + B
▪ This is C & B, worth $11
 optimal 3 + C
▪ This is B & C, worth $11
 optimal 2 + D
▪ This is A & D, worth $11
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
D
$8
6
A, C
$10
7
?
?
8
9
10


Since all of our options at
capacity 7 had the same
value, we can put any of
them in our table
We’ll use A & D, but it
would be equally correct to
use B & C
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
D
$8
6
A, C
$10
7
A, D
$11
8
9
10

We determine the best 8-lb
and 9-lb knapsacks in the
same way
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
D
$8
6
A, C
$10
7
A, D
$11
8
C, C
$14
9
C, D
$15
10

Capacity 10 options:
 optimal 8 + A
▪ This is A and two C’s, worth $17
 optimal 7 + B
▪ This is A, B, and D, worth $15
 optimal 6 + C
▪ This is A and two C’s, worth $17
 optimal 5 + D
▪ This is two D’s, worth $16
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
D
$8
6
A, C
$10
7
A, D
$11
8
C, C
$14
9
C, D
$15
10
?
?


We have now completed
our problem
Using this information, it
would be easy to figure out
the optimal 11-lb, 12-lb,
etc. knapsacks for this
problem
Capacity
Contents
Value
0
empty
$0
1
empty
$0
2
A
$3
3
B
$4
4
C
$7
5
D
$8
6
A, C
$10
7
A, D
$11
8
C, C
$14
9
C, D
$15
10
A, C, C
$17
Download