knapsack problem and memory function

36
KNAPSACK PROBLEM AND MEMORY FUNCTION PREPARED BY M. Baranitharan Kings College of Engineering

Upload: barani-tharan

Post on 14-Jan-2017

110 views

Category:

Engineering


1 download

TRANSCRIPT

Page 1: Knapsack problem and Memory Function

KNAPSACK PROBLEM AND MEMORY FUNCTION

PREPARED BYM. Baranitharan

Kings College of Engineering

Page 2: Knapsack problem and Memory Function

Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number W.So we must consider weights of items as well as their values.

Item # Weight Value 1 1 8 2 3 6 3 5 5

Page 3: Knapsack problem and Memory Function

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

2. “Fractional knapsack problem” Items are divisible: you can take any fraction of

an item

Page 4: Knapsack problem and Memory Function

Given a knapsack with maximum capacity W, and a set S consisting of n items

Each item i has some weight wi and benefit value bi (all wi and W are integer values)

Problem: How to pack the knapsack to achieve maximum total value of packed items?

Page 5: Knapsack problem and Memory Function

Problem, in other words, is to find

Ti

iTi

i Wwb subject to max

The problem is called a “0-1” problem, because each item must be entirely accepted or rejected.

Page 6: Knapsack problem and Memory Function

Let’s first solve this problem with a straightforward algorithm

Since there are n items, there are 2n possible combinations of items.

We go through all combinations and find the one with maximum value and with total weight less or equal to W

Running time will be O(2n)

Page 7: Knapsack problem and Memory Function

We can do better with an algorithm based on dynamic programming

We need to carefully identify the subproblems

Page 8: Knapsack problem and Memory Function

Given a knapsack with maximum capacity W, and a set S consisting of n items

Each item i has some weight wi and benefit value bi (all wi and W are integer values)

Problem: How to pack the knapsack to achieve maximum total value of packed items?

Page 9: Knapsack problem and Memory Function

We can do better with an algorithm based on dynamic programming

We need to carefully identify the subproblemsLet’s try this:If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k}

Page 10: Knapsack problem and Memory Function

If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk

= {items labeled 1, 2, .. k}

This is a reasonable subproblem definition. The question is: can we describe the final

solution (Sn ) in terms of subproblems (Sk)? Unfortunately, we can’t do that.

Page 11: Knapsack problem and Memory Function

Max weight: W = 20For S4:Total weight: 14Maximum benefit: 20

w1 =2b1 =3

w2 =4b2 =5

w3 =5b3 =8

w4 =3b4 =4 wi bi

10

85

54

43

32

Weight Benefit

9

Item#

4

3

2

1

5

S4

S5

w1 =2b1 =3

w2 =4b2 =5

w3 =5b3 =8

w5 =9b5 =10

For S5:Total weight: 20Maximum benefit: 26

Solution for S4 is not part of the solution for S5!!!

?

Page 12: Knapsack problem and Memory Function

As we have seen, the solution for S4 is not part of the solution for S5

So our definition of a subproblem is flawed and we need another one!

Page 13: Knapsack problem and Memory Function

Given a knapsack with maximum capacity W, and a set S consisting of n items

Each item i has some weight wi and benefit value bi (all wi and W are integer values)

Problem: How to pack the knapsack to achieve maximum total value of packed items?

Page 14: Knapsack problem and Memory Function

Let’s add another parameter: w, which will represent the maximum weight for each subset of items

The subproblem then will be to compute V[k,w], i.e., to find an optimal solution for Sk = {items labeled 1, 2, .. k} in a knapsack of size w

Page 15: Knapsack problem and Memory Function

The subproblem will then be to compute V[k,w], i.e., to find an optimal solution for Sk = {items labeled 1, 2, .. k} in a knapsack of size w

Assuming knowing V[i, j], where i=0,1, 2, … k-1, j=0,1,2, …w, how to derive V[k,w]?

Page 16: Knapsack problem and Memory Function

It means, that the best subset of Sk that has total weight w is:1) the best subset of Sk-1 that has total weight

w, or2) the best subset of Sk-1 that has total weight

w-wk plus the item k

else }],1[],,1[max{

if ],1[],[

kk

kbwwkVwkV

wwwkVwkV

Recursive formula for subproblems:

Page 17: Knapsack problem and Memory Function

The best subset of Sk that has the total weight w, either contains item k or not.

First case: wk>w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable.

Second case: wk w. Then the item k can be in the solution, and we choose the case with greater value.

else }],1[],,1[max{

if ],1[],[

kk

kbwwkVwkV

wwwkVwkV

Page 18: Knapsack problem and Memory Function

for w = 0 to WV[0,w] = 0

for i = 1 to nV[i,0] = 0

for i = 1 to nfor w = 0 to W

if wi <= w // item i can be part of the solution

if bi + V[i-1,w-wi] > V[i-1,w]V[i,w] = bi + V[i-1,w- wi]

elseV[i,w] = V[i-1,w]

else V[i,w] = V[i-1,w] // wi > w

Page 19: Knapsack problem and Memory Function

for w = 0 to WV[0,w] = 0

for i = 1 to nV[i,0] = 0

for i = 1 to nfor w = 0 to W

< the rest of the code >

What is the running time of this algorithm?

O(W)

O(W)Repeat n times

O(n*W)Remember that the brute-force algorithm

takes O(2n)

Page 20: Knapsack problem and Memory Function

Let’s run our algorithm on the following data:

n = 4 (# of elements)W = 5 (max weight)Elements (weight, benefit):(2,3), (3,4), (4,5), (5,6)

Page 21: Knapsack problem and Memory Function

for w = 0 to WV[0,w] = 0

0 0 0 0 000123

4 50 1 2 3

4

i\W

Page 22: Knapsack problem and Memory Function

for i = 1 to nV[i,0] = 0

0000

0 0 0 0 000123

4 50 1 2 3

4

i\W

Page 23: Knapsack problem and Memory Function

if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w]else V[i,w] = V[i-1,w] // wi > w

0

Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)

0

i=1bi=3wi=2w=1w-wi =-1

0 0 0 0 000123

4 50 1 2 3

4

i\W

000

Page 24: Knapsack problem and Memory Function

Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)

300000

0 0 0 0 000123

4 50 1 2 3

4

i\W i=1bi=3wi=2w=4w-wi =2

if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w]else V[i,w] = V[i-1,w] // wi > w

3 3

Page 25: Knapsack problem and Memory Function

Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)

300000

0 0 0 0 000123

4 50 1 2 3

4

i\W i=1bi=3wi=2w=5w-wi =3

if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w]else V[i,w] = V[i-1,w] // wi > w

3 3 3

Page 26: Knapsack problem and Memory Function

Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)

00000

0 0 0 0 000123

4 50 1 2 3

4

i\W i=2bi=4wi=3w=2w-wi =-1

3 3 3 33

if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w]else V[i,w] = V[i-1,w] // wi > w

0

Page 27: Knapsack problem and Memory Function

Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)

00000

0 0 0 0 000123

4 50 1 2 3

4

i\W i=2bi=4wi=3w=3w-wi =0

3 3 3 30

if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w]else V[i,w] = V[i-1,w] // wi > w

43

Page 28: Knapsack problem and Memory Function

Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)

00000

0 0 0 0 000123

4 50 1 2 3

4

i\W i=3bi=5wi=4w= 1..3

3 3 3 30 3 4 4

if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w]else V[i,w] = V[i-1,w] // wi > w

73 40

Page 29: Knapsack problem and Memory Function

Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)

00000

0 0 0 0 000123

4 50 1 2 3

4

i\W i=4bi=6wi=5w= 1..4

3 3 3 30 3 4 4

if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w]else V[i,w] = V[i-1,w] // wi > w

7

3 4070 3 4 5

5

Page 30: Knapsack problem and Memory Function

Items:1: (2,3)2: (3,4)3: (4,5) 4: (5,6)

00000

0 0 0 0 000123

4 50 1 2 3

4

i\W i=4bi=6wi=5w= 5w- wi=0

3 3 3 30 3 4 4 70 3 4

if wi <= w // item i can be part of the solution if bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w- wi] else V[i,w] = V[i-1,w]else V[i,w] = V[i-1,w] // wi > w

577

0 3 4 5

Page 31: Knapsack problem and Memory Function

This algorithm only finds the max possible value that can be carried in the knapsack◦ i.e., the value in V[n,W]

To know the items that make this maximum value, an addition to this algorithm is necessary

Page 32: Knapsack problem and Memory Function

All of the information we need is in the table.

V[n,W] is the maximal value of items that can be placed in the Knapsack.

Let i=n and k=Wif V[i,k] V[i1,k] then

mark the ith item as in the knapsacki = i1, k = k-wi

else i = i1 // Assume the ith item is not in the knapsack

// Could it be in the optimally packed knapsack?

Page 33: Knapsack problem and Memory Function

Goal: ◦ Solve only subproblems that are necessary and solve it only once

Memorization is another way to deal with overlapping subproblems in dynamic programming

With memorization, we implement the algorithm recursively:◦ If we encounter a new subproblem, we compute and store the

solution.◦ If we encounter a subproblem we have seen, we look up the

answer Most useful when the algorithm is easiest to implement

recursively◦ Especially if we do not need solutions to all subproblems.

Page 34: Knapsack problem and Memory Function

for i = 1 to nfor w = 1 to WV[i,w] = -1

for w = 0 to WV[0,w] = 0

for i = 1 to nV[i,0] = 0

MFKnapsack(i, w)if V[i,w] < 0

if w < wi

value = MFKnapsack(i-1, w)

else value = max(MFKnapsack(i-1, w),

bi + MFKnapsack(i-1, w-wi))

V[i,w] = value

return V[i,w]

Page 35: Knapsack problem and Memory Function

Dynamic programming is a useful technique of solving certain kind of problems

When the solution can be recursively described in terms of partial solutions, we can store these partial solutions and re-use them as necessary (memorization)

Running time of dynamic programming algorithm vs. naïve algorithm:◦ 0-1 Knapsack problem: O(W*n) vs. O(2n)

Page 36: Knapsack problem and Memory Function

Design and Analysis of Algorithms - Chapter 8 36