You are on page 1of 32

Example:

0/1 Knapsack
Max weight: 7

What is the maximum total


Items:
value you can get in the bag?
Item # val weight

0 2 1

1 5 3

2 4 4

3 6 5

Solution: ?
Bottom Up Approach
Max Weight

val weight 0 1 2 3 4 5 6 7

0 0

2 1
Items

5 3

4 4

6 5
Bottom Up Approach
Max Weight

val weight 0 1 2 3 4 5 6 7

0 0

2 1
Items

5 3

4 4

6 5

We will fill in these spots with the knapsack’s best value at


that max weight with items at or above that row
Bottom Up Approach
Max Weight

val weight 0 1 2 3 4 5 6 7

0 0 0

2 1 0
Items

5 3 0

4 4 0

6 5 0

Fill in the base cases


Bottom Up Approach
Max Weight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0
Items

5 3 0

4 4 0

6 5 0

Fill in the base cases


Bottom Up Approach
Max Weight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2
Items

5 3 0

4 4 0

6 5 0
Bottom Up Approach
Max Weight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2
Items

5 3 0

4 4 0

6 5 0
Bottom Up Approach
Max Weight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0

4 4 0

6 5 0
Bottom Up Approach
Max Weight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0

4 4 0

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 ?

4 4 0

6 5 0

Best solution when I use this item


? = max
Best solution when I do not use this item
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 ?

4 4 0

6 5 0

this item + best val at remaining weight = 5 + v(0) = 5 + 0 = 5 5


? = max = max = 5
best val so far at this maxWeight (ie exclude this item) = 2 2
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5

4 4 0

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 ?

4 4 0

6 5 0

this item + best val at remaining weight = 5 + v(1) = 5 + 2 = 7 7


? = max = max = 7
best val so far at this maxWeight (ie exclude this item) = 2 2
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7

4 4 0

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 ?

6 5 0

this item + best val at remaining weight = 4 + v(0) = 4 + 0 = 4 4


? = max = max = 7
best val so far at this maxWeight (ie exclude this item) = 7 7
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 ?

6 5 0

this item + best val at remaining weight = 4 + v(1) = 4 + 2 = 6 6


? = max = max = 7
best val so far at this maxWeight (ie exclude this item) = 7 7
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 ?

6 5 0

this item + best val at remaining weight = 4 + v(3) = 4 + 5 = 9 9


? = max = max = 9
best val so far at this maxWeight (ie exclude this item) = 7 7
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0 2 2 5 7 ?
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0 2 2 5 7 ?

this item + best val at remaining weight = 6 + v(0) = 6 + 0 = 6 6


? = max = max = 7
best val so far at this maxWeight (ie exclude this item) = 7 7
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0 2 2 5 7 7
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0 2 2 5 7 7 ?

this item + best val at remaining weight = 6 + v(1) = 6 + 2 = 8 8


? = max = max = 8
best val so far at this maxWeight (ie exclude this item) = 7 7
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0 2 2 5 7 7 8
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0 2 2 5 7 7 8 ?

this item + best val at remaining weight = 6 + v(2) = 6 + 2 = 8 8


? = max = max = 9
best val so far at this maxWeight (ie exclude this item) = 9 9
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0 2 2 5 7 7 8 9
Bottom Up Approach
maxWeight

val weight 0 1 2 3 4 5 6 7

0 0 0 0 0 0 0 0 0 0

2 1 0 2 2 2 2 2 2 2
Items

5 3 0 2 2 5 7 7 7 7

4 4 0 2 2 5 7 7 7 9

6 5 0 2 2 5 7 7 8 9

Final answer is 9

You might also like