KNAPSACK PROBLEM
1
KNAPSACK
The knapsack problem or rucksack
problem is a problem in combinatorial
optimization.
It derives its name from the following
maximization problem of the best choice of
essentials that can fit into one bag to be
carried on a trip.
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 a given limit and the total
value is as large as possible. 2
THE ORIGINAL KNAPSACK
PROBLEM (1)
Problem Definition
Want to carry essential items in one bag
Given a set of items, each has
A cost (i.e., 12kg)
A value (i.e., 4$)
Goal
Todetermine the number of each item to include in
a collection so that
The total cost is less than some given cost
And the total value is as large as possible
3
THE ORIGINAL KNAPSACK PROBLEM (2)
Three Types
0/1 Knapsack Problem
restricts the number of each kind of item to zero or
one
Bounded Knapsack Problem
restricts the number of each item to a specific value
Unbounded Knapsack Problem
places no bounds on the number of each item
4
0/1 KNAPSACK PROBLEM (1)
Problem: John wishes to take n items on a trip
The weight of item i is wi & items are all different (0/1 Knapsack
Problem)
The items are to be carried in a knapsack whose weight
capacity is c
When sum of item weights ≤ c, all n items can be carried in
the knapsack
When sum of item weights > c, some items must be left
behind
Which items should be taken/left?
5
0/1 KNAPSACK PROBLEM (2)
John assigns a profit pi to item i
All weights and profits are positive numbers
John wants to select a subset of the n items to take and the
points to remember here are-
In this problem we have a Knapsack that has a weight limit
W.
There are items i1, i2, ..., in each having weight w1, w2, …
wn and some benefit (value or profit) associated with it v1,
v2, ... vn
Our objective is to maximize the benefit such that the total
weight inside the knapsack is at most W.
Since this is a 0-1 Knapsack problem so we can either take
an entire item or reject it completely. We can not break an
item and fill the knapsack.
6
GREEDY ATTEMPTS FOR 0/1 KNAPSACK
Applygreedy method:
Problem Statement-
## Assume that there is a knapsack with max weight capacity
W=5
Main objective is to fill the knapsack with items such that the
benefit (value or profit) is maximum.
7
GREEDY ATTEMPTS FOR 0/1 KNAPSACK
Following table contains the items along with their value and weight.
item i 1 2 3
value val 60 100 120
weight 10 20 30
wt
Total items n = 3
Total capacity of the knapsack W = 50
Now we create a value table V[i,w] where,
i =number of items and
w= the weight of the items.
Rows denote the items and columns denote the weight.
As there are 3 items so, we have 4 rows from 0 to 4.
And the weight limit of the knapsack is W = 50 so, we have 6 columns
from 0 to 50
V[i,w] w=0 10 20 30 40 50
i=0
1
2
3
4
We fill the first row i = 0 with 0. This means when 0 item is
considered weight is 0.
Then we fill the first column w = 0 with 0. This means when weight
is 0 then items considered is 0.
Rule to fill the V[i,w] table.
if wt[i] > w then
V[i,w] = V[i-1,w]
else
V[i,w] = max( V[i-1,w], val[i] + V[i-1, w - wt[i]] )
After calculation, the value table V
V[i,w] w=0 10 20 30 40 50
i=0 0 0 0 0 0 0
1 0 60 60 60 60 60
2 0 60 100 160 160 160
3 0 60 100 160 180 220
Maximum value earned
Max Value = V[n,w]
= V[3,50]
= 220
Items that were put inside the knapsack are found using the following
rule:
set i = n and w = W
while i and w > 0 do
if V[i,w] != V[i-1,w] then
mark the ith item
set w = w - wt[i]
set i = i - 1
else
set i = i - 1
endif
Endwhile
So, items we are putting inside the knapsack are 3 and 2