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

Knapsack Problem Explained

The document discusses the knapsack problem, which involves selecting items to place in a knapsack of limited capacity in order to maximize the total value of items without exceeding the weight limit. There are two types of knapsack problems - the fractional knapsack problem, where items can be broken into fractions, and the 0-1 knapsack problem, where items must be taken in full or not at all. The fractional knapsack problem can be solved using a greedy approach by sorting items by density and selecting items until the weight limit is reached. The 0-1 knapsack problem is more difficult and is solved using dynamic programming by building up and storing solutions to subproblems in a table.

Uploaded by

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

Knapsack Problem Explained

The document discusses the knapsack problem, which involves selecting items to place in a knapsack of limited capacity in order to maximize the total value of items without exceeding the weight limit. There are two types of knapsack problems - the fractional knapsack problem, where items can be broken into fractions, and the 0-1 knapsack problem, where items must be taken in full or not at all. The fractional knapsack problem can be solved using a greedy approach by sorting items by density and selecting items until the weight limit is reached. The 0-1 knapsack problem is more difficult and is solved using dynamic programming by building up and storing solutions to subproblems in a table.

Uploaded by

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

KNAPSACK PROBLEM

SUBMIT TED BY : DEEPSIKHA(51810090)


S U N N Y KU M A R JA I S WA L
(51810091)
CONTENTS :
1. Understanding the problem
2. Approaches to Solve.
3. Their Procedures
4. Examples
5. Algorithms

NIT, Kurukshetra 2
KNAPSACK PROBLEM
C o n s i d e r, y o u a r e g i v e n a c o n t a i n e r w i t h l i m i t e d c a p a c i t y ‘ W ’ .
a n d s o m e i t e m s I 0, I 1, . . . . I n w i t h v a l u e s v 0, v 1, … . v n a n d w i t h
t h e i r w e i g h t s W 0 , W 1 , … . W n r e s p e c t i v e l y.

GOAL :- We have to put each item in knapsack so that the total weight
is less than or equal to ‘W’ and total value of items is as
large as possible.

“ THIS IS AN OPTIMIZATION PROBLEM”

NIT, Kurukshetra 3
Each item has 2 attributes : -
• Va l u e : i . e . , v i f o r i t e m I i.

• W e i g h t : i . e . , W i f o r i t e m I i.

Ty p e s o f K n a p s a c k p r o b l e m s : -
• Fractional Knapsack problem :- Can take fraction of an item.

(Using Greedy method).

• 0-1 Knapsack problem :- Either can take or leave an item.

(Using Dynamic programming)

No distribution of a single item.

NIT, Kurukshetra 4
(FRACTIONAL KNAPSACK PROBLEM)
In this case,items can be broken into smaller pieces:
Given problem :- Consider Capacity of knapsack ‘W’ = 60 kgs.

Items Weight(kg.) Value


1 40 280
2 10 100
3 20 120
4 24 120

STEP :- 1. C a l c u l a t e D e n s i t y ( v a l u e / w e i g h t ) o f e a c h i t e m :

D e n s i t y = Va l u e ( v i)
W e i g h t ( W i)

and place them in Descending order According to Density of items;

NIT, Kurukshetra 5
(FRACTIONAL KNAPSACK PROBLEM)
After Sorting Place the items in different table :

Items Weight(kg.) Value Density


2 10 100 10
REMEMBER
1 40 280 7
‘W’=60
3 20 120 6
4 24 120 5

STEP:-2. Now,We will pick items such that our value is Max. and total Weight is
less than Capacity of knapsack(W=60).

Condition :- i s _ w e i g h t [ I i] + t o t a l w e i g h t < = W (initially total weight=0),


if yes
take whole item
else
take fractional part of item.

NIT, Kurukshetra 6
(FRACTIONAL KNAPSACK PROBLEM)
STEP :-3. Place items in new table according to condition;

Items Weight(kg. Value Density Total Total


) weight Value
2 10 100 10 10+0<=60 100 REMEMBER
‘W’=60
1 40 280 7 40+10<=60 380
3 20 120 6 50+20*1/2<=60 440
4 24 120 5 - -
i s _ w e i g h t [ I 2] + 0 < = 6 0 i s _ w e i g h t [ I 1] + 1 0 < = 6 0 i s _ w e i g h t [ I 3] + 5 0 < = 6 0 So,as total weight
10 + 0 <= 60 40 + 10 <= 60 20 + 50 <= 60 inside knapsack is
if yes if yes if yes r e a c h e d i t s c a p a c i t y,
take whole item take whole item …………… Hence,
F i l l v a l u e o f I 2. F i l l v a l u e o f I 1. else
20*1/2+50<=60 Max. value = 440.
To t a l w e i g h t = 1 0 To t a l w e i g h t = 5 0
To t a l v a l u e = 1 0 0 . To t a l v a l u e = 3 8 0 . To t a l w e i g h t = 6 0 (optimized solution)
To t a l v a l u e = 4 4 0 .

NIT, Kurukshetra 7
(FRACTIONAL KNAPSACK ALGORITHM)

W Time
complexity:
O(nlog(n))

NIT, Kurukshetra 8
0-1 KNAPSACK PROBLEM
In 0-1 Knapsack, items cannot be broken which means the thief should take the
item as a whole or should leave it. This is reason behind calling it as
“0-1 Knapsack”.
Hence, in case of 0-1 Knapsack, the value of xi can be either 0 or 1,
where other constraints remain the same.

xi : amount of object is selected, where 0< xi <1.

Satisfying condition :-
∑ x iW i < C a p a c i t y o f K n a p s a c k .

Objective :-
∑ x iV i s h o u l d b e o p t i m i z e d . ( m a x i m u m )

“ T H I S I S S O LV E D B Y D Y N A M I C P R O G R A M M I N G . ”

NIT, Kurukshetra 9
(0-1 KNAPSACK PROBLEM)
In this case,items can’t be broken into smaller pieces:
Given problem :- Consider Capacity of knapsack ‘W’ = 5 kgs.

Items Weight(kg.) Value


1 3 100
2 2 20
3 4 60
4 1 40

Approach :- Compute the solutions to the sub-problems once and store the
solutions in a table, So that they can be reused repeatedly using
bottom-up approach.

“ Our objective is here to fill the knapsack in such a manner that value is
maximum.”

NIT, Kurukshetra 10
(0-1 KNAPSACK PROBLEM)
STEP :-1. Create a table V(i,w), Where i = item no.
w = Weight of item.

Here, I = 0 to 4;
and, w = 0 to 5; because max. capacity W = 5.

V(i,w) w=0 1 2 3 4 5
i=0 0 0 0 0 0 0
1 0
2 0
3 0
4 0

Fill the row i = 0 with 0. This means when 0 item is considered weight is 0.
Fill the column w = 0 with 0. This means when weight is 0 then items considered is 0.
NIT, Kurukshetra 11
(0-1 KNAPSACK PROBLEM)
Procedure to fill the V[i,w] table.
Items Weight(kg.) Value
if weight[i] > w then 1 3 100
V[i,w] = V[i-1,w] n=4 & W=5
2 2 20

else if weight[i] <= w then 3 4 60


V[i,w] = max( V[i-1,w], val[i] + V[i-1, w - weight[i]] ) 4 1 40

V(i,w) w = 0 1 2 3 4 5 For i=1 and w=1, For i=1 and w=2,


i=0 0 0 0 0 0 0 If weight[1]>w If weight[1]>w
3 > 1 3 > 2
1 0 0 0
So, V[1,1]= V[0,1] So, V[1,2]= V[0,2]
2 0 V[1,1]= 0 V[1,2]= 0
3 0
4 0

NIT, Kurukshetra 12
(0-1 KNAPSACK PROBLEM)
if weight[i] > w then Items Weight(kg.) Value
V[i,w] = V[i-1,w]
1 3 100
else if weight[i] <= w then
2 2 20
V[i,w] = max( V[i-1,w], val[i] + V[i-1, w - weight[i]] )
3 4 60
4 1 40

V(i,w w = 0 1 2 3 4 5 For i=1 and w=3,


If weight[1]>w
) 3 > 3 No,
i=0 0 0 0 0 0 0 V[1,3]=max(V[0,3], val[1]+V[i-1,w-weight[i]])
=max(0,100+V[0,3-3])
1 0 0 0 100 100 =max(0,100+V[0,0])
=100.
2 0
For i=1 and w=4,
3 0 If weight[1]>w
3 > 4 No,
4 0 V[1,4]=max(V[0,4], val[1]+V[i-1,w-weight[i]])
=max(0,100+V[0,4-3])
=max(0,100+V[0,1])
=100.
NIT, Kurukshetra 13
(0-1 KNAPSACK PROBLEM)
A f t e r, f i l l i n g o u t t h e t a b l e w e h a v e t o f i n d t h e Items Weight(kg.) Value
maximum value V[n,w].
1 3 100
max V[n,w]=140. 2 2 20
3 4 60
4 1 40

V(i,w w = 0 1 2 3 4 5
)
i=0 0 0 0 0 0 0
1 0 0 0 100 100 100
2 0 0 20 100 100 120
3 0 0 20 100 100 120
4 0 40 40 100 140 140

NIT, Kurukshetra 14
(0-1 KNAPSACK PROBLEM)
Rule for back tracking the items that we have put Items Weight(kg.) Value
inside into knapsack.
set i=n and w=W 1 3 100
while I and w > 0 do
if V[i,w] != V[i-1,w] 2 2 20
then mark ith item 3 4 60
w= w-weight[i]
i= i-1 4 1 40
else i= i-1
end if i=3 and w=4 ;
V[3,4] !=V[2,4]
end while
i =3-1=2.
V(i,w w = 0 1 2 3 4 5 i=4 and w=5 ;
) V[4,5] !=V[3,5] i=2 and w=4 ;
m a r k 4 th i t e m V[2,4] !=V[1,4]
i=0 0 0 0 0 0 0 w=5-weight[4] i =2-1=1.
1 0 0 0 100 100 100 w=5-1=4.
i =4-1=3. i=1 and w=4 ;
2 0 0 20 100 100 120 V[1,4] !=V[0,4]
So ,item 1 & 4 were m a r k 1 st i t e m
3 0 0 20 100 100 120 w=4-weight[1]
put inside the
4 0 40 40 100 140 140 knapsack. w=4-3=1.
NIT, Kurukshetra i =1-1=0. 15
(0-1 KNAPSACK ALGORITHM)

100

// bi is value of ith item

Time
complexity:
O(n.W)

NIT, Kurukshetra 16
THANK YOU

NIT, Kurukshetra 17
REFERENCES
https://www.dyclassroom.com/dynamic-programming/0-1-knapsack-problem

https://www.tutorialspoint.com/design_and_analysis_of_algorithms/design_and_analysis_of_algorithm
s_01_knapsack.htm

https://www.sanfoundry.com/dynamic-programming-solutions-0-1-knapsack-problem/

18

You might also like