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_Dynamic Programming.pptx (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
Introduction to Computational Complexity Theory pptx
PPTX
Different Searching and Sorting Methods.pptx
PPTX
Analysis of Algorithms (1).pptx, asymptotic
PPTX
0-1_knapsack_using_DP, types of knapsack
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
Introduction to Computational Complexity Theory pptx
Different Searching and Sorting Methods.pptx
Analysis of Algorithms (1).pptx, asymptotic
0-1_knapsack_using_DP, types of knapsack
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)

PDF
Well-logging-methods_new................
PPTX
Current and future trends in Computer Vision.pptx
PPTX
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
PPTX
CYBER-CRIMES AND SECURITY A guide to understanding
PDF
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
PPTX
Foundation to blockchain - A guide to Blockchain Tech
PPTX
Artificial Intelligence
PPT
Project quality management in manufacturing
PDF
composite construction of structures.pdf
PDF
Digital Logic Computer Design lecture notes
PPTX
Construction Project Organization Group 2.pptx
PDF
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PDF
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
PDF
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
DOCX
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
PPTX
Geodesy 1.pptx...............................................
PPT
introduction to datamining and warehousing
PDF
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
PPT
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
PDF
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf
Well-logging-methods_new................
Current and future trends in Computer Vision.pptx
M Tech Sem 1 Civil Engineering Environmental Sciences.pptx
CYBER-CRIMES AND SECURITY A guide to understanding
Mitigating Risks through Effective Management for Enhancing Organizational Pe...
Foundation to blockchain - A guide to Blockchain Tech
Artificial Intelligence
Project quality management in manufacturing
composite construction of structures.pdf
Digital Logic Computer Design lecture notes
Construction Project Organization Group 2.pptx
BMEC211 - INTRODUCTION TO MECHATRONICS-1.pdf
PRIZ Academy - 9 Windows Thinking Where to Invest Today to Win Tomorrow.pdf
Enhancing Cyber Defense Against Zero-Day Attacks using Ensemble Neural Networks
ASol_English-Language-Literature-Set-1-27-02-2023-converted.docx
Geodesy 1.pptx...............................................
introduction to datamining and warehousing
Mohammad Mahdi Farshadian CV - Prospective PhD Student 2026
Introduction, IoT Design Methodology, Case Study on IoT System for Weather Mo...
TFEC-4-2020-Design-Guide-for-Timber-Roof-Trusses.pdf

0-1_knapsack_using_Dynamic Programming.pptx

  • 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