Knapsack 0 1
Knapsack 0 1
1
KNAPSACK
The knapsack problem or rucksack
problem is a problem in combinatorial
optimization.
It derives its name from the following
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
4
0/1 KNAPSACK PROBLEM (1)
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
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
set i = n and w = W