section 1.9: the knapsack problem

29
Section 1.9: The Knapsack Problem Math for Liberal Studies

Upload: loman

Post on 22-Feb-2016

56 views

Category:

Documents


0 download

DESCRIPTION

Math for Liberal Studies. Section 1.9: The Knapsack Problem. Variation on a Theme. 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 - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: Section 1.9: The Knapsack Problem

Section 1.9: The Knapsack Problem

Math for Liberal Studies

Page 2: Section 1.9: The Knapsack Problem

Variation on a Theme

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

Page 3: Section 1.9: The Knapsack Problem

An Example

In this example, we have 4 weights, and our bin (“knapsack”) can hold 10 pounds

We label the weights to make it easier

Page 4: Section 1.9: The Knapsack Problem

Possible Solutions

How should we pack our knapsack?

There are many possible solutions

This one has a total value of $15

Page 5: Section 1.9: The Knapsack Problem

Possible Solutions

How should we pack our knapsack?

There are many possible solutions

This one also has a total value of $15

Page 6: Section 1.9: The Knapsack Problem

Possible Solutions

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

Page 7: Section 1.9: The Knapsack Problem

Finding the Best Solution

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

Page 8: Section 1.9: The Knapsack Problem

Finding the Best Solution

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?

Page 9: Section 1.9: The Knapsack Problem

Recursion

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

Page 10: Section 1.9: The Knapsack Problem

Recursion

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

Page 11: Section 1.9: The Knapsack Problem

Recursion

If a knapsack can hold zero pounds, then it has to be empty!

This empty knapsack becomes our starting point

Page 12: Section 1.9: The Knapsack Problem

Recursive Packing Algorithm

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

Page 13: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

We start off by making our chart

The 0-capacity knapsack must be empty

Capacity Contents Value

0 empty $0

1

2

3

4

5

6

7

8

9

10

Page 14: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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

Page 15: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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

Page 16: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

At capacity 3, we have a choice: pack A with 1 lb left over pack B

Since B is worth $4, this is the better option

Capacity Contents Value

0 empty $0

1 empty $0

2 A $3

3 B $4

4

5

6

7

8

9

10

Page 17: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

At capacity 4, we start having more options: optimal 2-lb knapsack + A optimal 1-lb knapsack + B optimal 0-lb knapsack + C

Note we still can’tfit a D

Capacity Contents Value

0 empty $0

1 empty $0

2 A $3

3 B $4

4 ? ?

5

6

7

8

9

10

Page 18: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

optimal 2-lb knapsack + A This is two A’s, worth $6

optimal 1-lb knapsack + B This is just one B, worth $4

optimal 0-lb knapsack + C This is one C, worth $7

The third option is best

Capacity Contents Value

0 empty $0

1 empty $0

2 A $3

3 B $4

4 C $7

5

6

7

8

9

10

Page 19: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

Capacity 5 options: optimal 3 + A

optimal 2 + B

optimal 1 + C

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

Page 20: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

Capacity 5 options: optimal 3 + A▪ This is B & A, worth $7

optimal 2 + B

optimal 1 + C

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

Page 21: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

Capacity 5 options: optimal 3 + A▪ This is B & A, worth $7

optimal 2 + B▪ This is A & B, worth $7

optimal 1 + C

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

Page 22: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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

Page 23: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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

Page 24: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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

Page 25: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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

Page 26: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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

Page 27: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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

Page 28: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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 ? ?

Page 29: Section 1.9: The Knapsack Problem

Using the Recursive Algorithm

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