SlideShare a Scribd company logo
0/1 knapsack using DP
0/1 Knapsack Problem
• We are given a knapsack of capacity m and a set of n objects numbered
1,2,…,n. Each object i has weight wi and profit pi.
• Let x = [x1, x2,…, xn] be a solution vector in which xi = 0 if object i is not in
the knapsack, and xi = 1 if it is in the knapsack.
• The goal is to find a subset of objects to put into the knapsack so that
(that is, the objects fit into the knapsack) and
is maximized (that is, the profit is maximized).
0/1 Knapsack Problem
• The naive method is to consider all 2n
possible subsets of
the n objects and choose the one that fits into the
knapsack and maximizes the profit.
• Let F[i,x] be the maximum profit for a knapsack of capacity
x using only objects {1,2,…,i}. The DP formulation is:
S1
i
={(P,W)|(P-pi+1 ,W- wi+1 ) € Si }
we can compute Si+1
by merging and purging
the pairs in Si
and S1
i
Note :S0
={(0,0)}
Purging -
• If Si+1
contains 2 pairs (Pj,Wj) and (Pk,Wk)
with property that
Pj <= Pk and
Wj >= Wk
• and weight exceeds capacity of bag
Example
• n=3 m=6
(w1,w2,w3)=(2,3,4)
(p1,p2,p3)= (1,2,5)
Object x1 x2 x3
Pi 1 2 5
Wi 2 3 4
S0
={ ……….}
S1
={ ……….}
S2
={ ……….}
S3
={ ……….}
S1
0
={ ……….}
S1
1
={ ……….}
S1
2
={ ……….}
Object x1 x2 x3
Pi 1 2 5
Wi 2 3 4
S0
={ ……….}
S0
={ (0,0) } S1
0
={ (1,2)}
S1
={ ……….}
S1
={ (0,0)(1,2)} S1
1
={ (2,3),(3,5)}
x2
2
3
x1
1
2
Object x1 x2 x3
Pi 1 2 5
Wi 2 3 4
S0
={ ……….}
S1
={ (0,0)(1,2)} S1
1
={ (2,3),(3,5)}
x2
2
3
x1
1
2
S2
={ ……….}
S2
={(0,0)(1,2), (2,3),(3,5) }
x3
5
4
S1
2
={(5,4)(6,6), (7,7),(8,9) }
Object x1 x2 x3
Pi 1 2 5
Wi 2 3 4
S0
={ ……….}
x2
2
3
x1
1
2
S2
= {(0,0)(1,2), (2,3),(3,5) }
x3
5
4
S1
2
={(5,4)(6,6), (7,7),(8,9) }
S3
={ ……….}
S3
= { (0,0)(1,2), (2,3),(3,5),(5,4),(6,6),
(7,7),(8,9) }
PURGING
S3
= { (0,0)(1,2),(2,3),(3,5),(5,4),(6,6),
(7,7),(8,9) }
Profit is less and weight
is less
Weight is greater than
m
AFTER PURGING
S3
= { (0,0)(1,2),(2,3),(5,4),(6,6) }
Trace back
S0
={ (0,0) } S1
0
={ (1,2)}
S1
={ (0,0)(1,2)} S1
1
={ (2,3),(3,5)}
S2
={(0,0)(1,2), (2,3),(3,5) } S1
2
={(5,4)(6,6), (7,7),(8,9) }
S3
= { (0,0)(1,2),(2,3),(5,4),(6,6) }
X1=0
X1=1
X2=0
X2=1
X3=0 X3=1
(6,6) gives
maximum Profit It came from S1
2
So x3=1
i.e (6,6) obtained
from (6-5,6-4)=(1,2)
(1,2) €S2
It came from S1 So x2=0
(1,2) came from S1
0
So x1=1
Solution is
(x1,x2,x3) = (1,0,1)
Algorithm

More Related Content

PPTX
01 Knapsack using Dynamic Programming
PDF
Knapsack Problems in Algorithms and Design
PPT
Design and analysis of Algorithms - Lecture 15.ppt
PPTX
Knapsack problem
PPTX
Dynamic programming (dp) in Algorithm
PPT
Knapsack problem and Memory Function
PDF
A Survey- Knapsack Problem Using Dynamic Programming
PPT
4 greedy methodnew
01 Knapsack using Dynamic Programming
Knapsack Problems in Algorithms and Design
Design and analysis of Algorithms - Lecture 15.ppt
Knapsack problem
Dynamic programming (dp) in Algorithm
Knapsack problem and Memory Function
A Survey- Knapsack Problem Using Dynamic Programming
4 greedy methodnew

Similar to 0-1_knapsack_using_DP, types of knapsack (20)

PPT
Knapsack problem
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
PPTX
Knapsack
PDF
0-1 knapsack problem
PPTX
Daa:Dynamic Programing
PDF
306_0_1_Knapsack .pdf
PPTX
Presentation of knapsack
PPT
DynProg_Knapsack.ppt
PPTX
Dynamic Programming-Knapsack Problem
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
PPT
AOA ppt.ppt
PDF
7f44bdd880a385b7c1338293ea4183f930ea
PPTX
Knapsack problem dynamicprogramming
PDF
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
PDF
PPT
Knapsack Problem Analysis of Algorithm.ppt
PPT
0-1 knapsack.ppt
PDF
Dynamic Programming knapsack 0 1
DOCX
PPTX
Fractional knapsack problem
Knapsack problem
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Knapsack
0-1 knapsack problem
Daa:Dynamic Programing
306_0_1_Knapsack .pdf
Presentation of knapsack
DynProg_Knapsack.ppt
Dynamic Programming-Knapsack Problem
Module 3_Greedy Technique_2021 Scheme.pptx
AOA ppt.ppt
7f44bdd880a385b7c1338293ea4183f930ea
Knapsack problem dynamicprogramming
ADA Unit — 3 Dynamic Programming and Its Applications.pdf
Knapsack Problem Analysis of Algorithm.ppt
0-1 knapsack.ppt
Dynamic Programming knapsack 0 1
Fractional knapsack problem
Ad

More from Minakshee Patil (18)

PPTX
Introduction, characteristics, Pseudocode.pptx
PPTX
0-1_knapsack_using_Dynamic Programming.pptx
PPTX
Introduction to Computational Complexity Theory pptx
PPTX
Different Searching and Sorting Methods.pptx
PPTX
Analysis of Algorithms (1).pptx, asymptotic
PPT
Linear Data Structures, array, stack, queue
PPTX
Unit 5-BACKTRACKING- n queens, sum of subset, graph coloring problems
PPT
stack, opeartions on stack, applications of stack
PPTX
Algorithm Design Techiques, divide and conquer
PPTX
Analysis of Algorithms, recurrence relation, solving recurrences
PPT
Lecture2 (9).ppt
PPTX
oracle.pptx
PPT
Lecture1.ppt
PPT
Unit 1.ppt
PPTX
Hierarchical clustering algorithm.pptx
PPT
Lecture2 (1).ppt
PPT
Lecture3 (3).ppt
PPT
Lecture4.ppt
Introduction, characteristics, Pseudocode.pptx
0-1_knapsack_using_Dynamic Programming.pptx
Introduction to Computational Complexity Theory pptx
Different Searching and Sorting Methods.pptx
Analysis of Algorithms (1).pptx, asymptotic
Linear Data Structures, array, stack, queue
Unit 5-BACKTRACKING- n queens, sum of subset, graph coloring problems
stack, opeartions on stack, applications of stack
Algorithm Design Techiques, divide and conquer
Analysis of Algorithms, recurrence relation, solving recurrences
Lecture2 (9).ppt
oracle.pptx
Lecture1.ppt
Unit 1.ppt
Hierarchical clustering algorithm.pptx
Lecture2 (1).ppt
Lecture3 (3).ppt
Lecture4.ppt
Ad

Recently uploaded (20)

PPTX
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
PDF
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
PDF
Well-logging-methods_new................
PPT
introduction to datamining and warehousing
PDF
R24 SURVEYING LAB MANUAL for civil enggi
PPTX
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
PPTX
OOP with Java - Java Introduction (Basics)
PPTX
CH1 Production IntroductoryConcepts.pptx
PDF
Model Code of Practice - Construction Work - 21102022 .pdf
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
PPTX
Geodesy 1.pptx...............................................
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
PPTX
bas. eng. economics group 4 presentation 1.pptx
PPTX
Lecture Notes Electrical Wiring System Components
PPT
Project quality management in manufacturing
PDF
PPT on Performance Review to get promotions
PPT
Mechanical Engineering MATERIALS Selection
PDF
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
PPTX
Foundation to blockchain - A guide to Blockchain Tech
Infosys Presentation by1.Riyan Bagwan 2.Samadhan Naiknavare 3.Gaurav Shinde 4...
SM_6th-Sem__Cse_Internet-of-Things.pdf IOT
Well-logging-methods_new................
introduction to datamining and warehousing
R24 SURVEYING LAB MANUAL for civil enggi
MET 305 2019 SCHEME MODULE 2 COMPLETE.pptx
OOP with Java - Java Introduction (Basics)
CH1 Production IntroductoryConcepts.pptx
Model Code of Practice - Construction Work - 21102022 .pdf
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Geodesy 1.pptx...............................................
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
bas. eng. economics group 4 presentation 1.pptx
Lecture Notes Electrical Wiring System Components
Project quality management in manufacturing
PPT on Performance Review to get promotions
Mechanical Engineering MATERIALS Selection
keyrequirementskkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkkk
Foundation to blockchain - A guide to Blockchain Tech

0-1_knapsack_using_DP, types of knapsack

  • 2. 0/1 Knapsack Problem • We are given a knapsack of capacity m and a set of n objects numbered 1,2,…,n. Each object i has weight wi and profit pi. • Let x = [x1, x2,…, xn] be a solution vector in which xi = 0 if object i is not in the knapsack, and xi = 1 if it is in the knapsack. • The goal is to find a subset of objects to put into the knapsack so that (that is, the objects fit into the knapsack) and is maximized (that is, the profit is maximized).
  • 3. 0/1 Knapsack Problem • The naive method is to consider all 2n possible subsets of the n objects and choose the one that fits into the knapsack and maximizes the profit. • Let F[i,x] be the maximum profit for a knapsack of capacity x using only objects {1,2,…,i}. The DP formulation is:
  • 4. S1 i ={(P,W)|(P-pi+1 ,W- wi+1 ) € Si } we can compute Si+1 by merging and purging the pairs in Si and S1 i Note :S0 ={(0,0)}
  • 5. Purging - • If Si+1 contains 2 pairs (Pj,Wj) and (Pk,Wk) with property that Pj <= Pk and Wj >= Wk • and weight exceeds capacity of bag
  • 7. Object x1 x2 x3 Pi 1 2 5 Wi 2 3 4 S0 ={ ……….} S1 ={ ……….} S2 ={ ……….} S3 ={ ……….} S1 0 ={ ……….} S1 1 ={ ……….} S1 2 ={ ……….}
  • 8. Object x1 x2 x3 Pi 1 2 5 Wi 2 3 4 S0 ={ ……….} S0 ={ (0,0) } S1 0 ={ (1,2)} S1 ={ ……….} S1 ={ (0,0)(1,2)} S1 1 ={ (2,3),(3,5)} x2 2 3 x1 1 2
  • 9. Object x1 x2 x3 Pi 1 2 5 Wi 2 3 4 S0 ={ ……….} S1 ={ (0,0)(1,2)} S1 1 ={ (2,3),(3,5)} x2 2 3 x1 1 2 S2 ={ ……….} S2 ={(0,0)(1,2), (2,3),(3,5) } x3 5 4 S1 2 ={(5,4)(6,6), (7,7),(8,9) }
  • 10. Object x1 x2 x3 Pi 1 2 5 Wi 2 3 4 S0 ={ ……….} x2 2 3 x1 1 2 S2 = {(0,0)(1,2), (2,3),(3,5) } x3 5 4 S1 2 ={(5,4)(6,6), (7,7),(8,9) } S3 ={ ……….} S3 = { (0,0)(1,2), (2,3),(3,5),(5,4),(6,6), (7,7),(8,9) }
  • 11. PURGING S3 = { (0,0)(1,2),(2,3),(3,5),(5,4),(6,6), (7,7),(8,9) } Profit is less and weight is less Weight is greater than m
  • 12. AFTER PURGING S3 = { (0,0)(1,2),(2,3),(5,4),(6,6) }
  • 13. Trace back S0 ={ (0,0) } S1 0 ={ (1,2)} S1 ={ (0,0)(1,2)} S1 1 ={ (2,3),(3,5)} S2 ={(0,0)(1,2), (2,3),(3,5) } S1 2 ={(5,4)(6,6), (7,7),(8,9) } S3 = { (0,0)(1,2),(2,3),(5,4),(6,6) } X1=0 X1=1 X2=0 X2=1 X3=0 X3=1 (6,6) gives maximum Profit It came from S1 2 So x3=1 i.e (6,6) obtained from (6-5,6-4)=(1,2) (1,2) €S2 It came from S1 So x2=0 (1,2) came from S1 0 So x1=1