/
Knapsack Problem Knapsack Problem

Knapsack Problem - PowerPoint Presentation

luanne-stotts
luanne-stotts . @luanne-stotts
Follow
478 views
Uploaded On 2016-05-20

Knapsack Problem - PPT Presentation

A dynamic approach Knapsack Problem Given a sack able to hold K kg Given a list of objects Each has a weight and a value Try to pack the object in the sack so that the total value is maximized ID: 326941

problem solution knapsack item solution problem item knapsack weight optimal number total object items assume max price approach dynamic set size ith

Share:

Link:

Embed:

Download Presentation from below link

Download Presentation The PPT/PDF document "Knapsack Problem" is the property of its rightful owner. Permission is granted to download and print the materials on this web site for personal, non-commercial use only, and to display it on your personal computer provided you do not modify the materials and that you retain all copyright notices contained in the materials. By downloading content from our website, you accept the terms of this agreement.


Presentation Transcript

Slide1

Knapsack Problem

A dynamic approachSlide2

Knapsack Problem

Given a sack, able to hold K kg

Given a list of objects

Each has a weight and a value

Try to pack the object in the sack so that the total value is maximizedSlide3

Variation

Rational Knapsack

Object is like a gold bar, we can cut it in to piece with the same value/weight

0-1 Knapsack

Object cannot be broken, we have to choose to take (1) or leave (0) the object

E.g.

K = 50

Objects = (

60

,

10

) (

100

,

20

) (

120

,

30

)

Best solution =

second

and

thirdSlide4

The Problem

Input

A number

W

, the capacity of the sack

n pairs of weight and price

((w

1,p1),(w2,p2),…,(wn,pn)) wi = weight of the ith itemspi = price of the ith itemOutputA subset S of {1,2,3,…,n} such that is maximum Slide5

from http://en.wikipedia.org/wiki/File:Knapsack.svgSlide6

Naïve approach

Try every possible combination of {1,2,3,…n}

Test whether a combination satisfies the weight constraint

If so, remember the best one

This gives O(2

n

)Slide7

Dynamic Approach

Let us assume that

W

(the maximum weight) and

w

i

are integers

Let us assume that we just want to know “the best total price”, i.e.,(well, soon we will see that this also leads to the actual solutionThe problem can be solved by a dynamic programmingHow?What should be the subproblem?Is it overlapping?Slide8

The Sub Problem

What shall we divide?

The number of items?

Let’s try half of the items?

what about the weight?Slide9

The Optimal Solution

Assume that we know the actual optimal solution to the problem

The solution consist of item {2,5,6,7}

What if we takes the item number 7 out?

What can we say about the set of {2,5,6}

Is it an optimal solution of any particular problem?Slide10

The Optimal Solution

Let

K(w)

be the “best total value” when

W

equals to

w

If the ith item is in the best solutionK(W) = K(W – wi) + piBut, we don’t really know that the ith item is in the optimal solutionSo, we try everythingK(W) = max1≤i ≤ n(K(W – wi) + pi)Is this our algorithm?Yes, if and only if we allow each item to be selected multiple times (that is not true for this problem)Slide11

Solution

We need to keep track whether the item is used

What if we know the optimal solution when

i

th

items is not being used?

Also for every size of knapsack from 0 to WThen, with additional ith item, we have only two choices, use it or not use itSlide12

The Recurrence

K(

a,b

)

= the best total price when the knapsack is of size

a

and only item number

1 to number b is consideredK(W,j) = max( K(W – wi,j – 1) + pi , K(W,j – 1) )The solution is at K(W,n)Slide13

The Code

set all

K(0,j) = 0

and all

K(w,0) = 0

for

j = 1

to n for w = 1 to W if (wj > W) K(w,j

) = K(w,j

– 1);

else

K(

w,j

) = max( K(w –

w

i

,j

– 1) + p

i

,

K(

W,j

– 1) )

return

K(

W,n

);