0% found this document useful (0 votes)
36 views18 pages

Knapsack Problem

It a topic on operations research

Uploaded by

iemterm4.1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views18 pages

Knapsack Problem

It a topic on operations research

Uploaded by

iemterm4.1
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Knapsack Problem

1
Knapsack Problem
The knapsack problem or rucksack problem is a
problem in combinatorial optimization: Given a set of
items, each with a weight and a value, determine the
number of each item to include in a collection so that
the total weight is less than or equal to a given limit
and the total value is as large as possible.
which boxes should be chosen to maximize the
amount of money while still keeping the overall
weight under or equal to 15 kg?
➢ Answer: 3 yellow boxes and 3 grey boxes .
Knapsack problem
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

1. “Fractional knapsack problem”


 Items are divisible: you can take any fraction of an item
0-1 Knapsack problem
 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
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?
0-1 Knapsack problem
 Problem, in other words, is to find
max  bi subject to  wi  W
iT iT

The problem is called a “0-1” problem,


because each item must be entirely
accepted or rejected.
0-1 Knapsack problem:
 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
0-1 Knapsack Algorithm
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for 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]
else
V[i,w] = V[i-1,w]

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


Example:
n = 4 (number of elements)
W = 5 (max weight)

Elements (weight, benefit):


1:(2,3)
2:(3,4)
3:(4,5)
4:(5,6)
Example
i\W 0 1 2 3 4 5
0 0 0 0 0 0 0
1 0
2 0
3 0
4 0

for i = 1 to n
V[i,0] = 0
Items:
1: (2,3)
Example 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=1 4: (5,6)
0 0 0 0 0 0 0
bi=3
1 0 0 3 3 3 3
wi=2
2 0
3 0 w=5
4 0 w-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
Items:
1: (2,3)
Example 2: (3,4)
3: (4,5)
i\ W 0 1 2 3 4 5 4: (5,6)
i=2
0 0 0 0 0 0 0
bi=4
1 0 0 3 3 3 3
wi=3
2 0 0 3 4 4 7
3 0 w=5
4 0 w-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
Items:
1: (2,3)
Example 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=3 4: (5,6)
0 0 0 0 0 0 0
bi=5
1 0 0 3 3 3 3
7 wi=4
2 0 0 3 4 4
3 0 0 3 4 5 7 w= 5
4 0 w- wi=1
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
Items:
1: (2,3)
Example 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0
bi=6
1 0 0 3 3 3 3
wi=5
2 0 0 3 4 4 7
3 0 0 3 4 5 7 w= 5
4 0 0 3 4 5 7 w- wi=0
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
How to find actual Knapsack items
 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=W
if V[i,k]  V[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
Items:
1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 i=4 4: (5,6)
0 0 0 0 0 0 0 k= 5
1 0 0 3 3 3 3 bi=6
2 0 0 3 4 4 7 wi=5
3 0 0 3 4 5 7 V[i,k] = 7
4 0 0 3 4 5 7 V[i−1,k] =7
i=n, k=W
while i,k > 0
if V[i,k]  V[i−1,k] then
mark the ith item as in the knapsack
i = i−1, k = k-wi
else
i = i−1
Items:
1: (2,3)
Finding the Items 2: (3,4)
3: (4,5)
i\W 0 1 2 3 4 5 4: (5,6)
0 0 0 0 0 0 0
1 0 0 3 3 3 3
2 0 0 3 4 4 7
3 0 0 3 4 5 7 The optimal
knapsack
4 0 0 3 4 5 7
should contain
i=n, k=W {1, 2}
while i,k > 0
if V[i,k]  V[i−1,k] then
mark the nth item as in the knapsack
i = i−1, k = k-wi
else
i = i−1

You might also like