SlideShare a Scribd company logo
1 of 15
0/1 KNAPSACK
PROBLEM
-RACKSAVI.R,
I M.Sc Information Technology,
V.V.Vanniaperumal college for women,
Virudhunagar.
OBJECTIVES
 Dynamic Programming
 0/1 Knapsack Problem
 Algorithm
 Example Problem
DYNAMIC PROGRAMMING
Dynamic Programming
is used where we have
problems, which can be
divided into similar sub-
problems.
0/1 KNAPSACK PROBLEM
 The 0/1 Knapsack Problem is a
classical dynamic programming
problem.
 The goal is to maximize the value
of a Knapsack that can hold at most W
units.
Contd…
The objective is to
maximize the profit:-
• The capacity of the
bag is C.
• We have n items.
• The ith item has Value
vi and Weight wi.
ALGORITHM
for w = 0 to w
V[0,w] = 0 O(W)
for i = 1 to n
V[i ,0] = 0 O(n)
for i = 1 to n // Repeat n times
for w = 1 to w
if wi <= w O(W)
//item i can be part of
the solution
If bi + V[i-1,w-wi] > V[i-1,w]
V[i,w] = bi + V[i-1,w-wi]
Else
V[i,w] = V[i-1,w]
Else
V[i,w] = V[i-1,w] //wi > w
EXAMPLE PROBLEM
GIVEN:-
Weight = {3,4,6,5}
Profits = {2,3,1,4}
W = 8
N = 4
p
iw 0 1 2 3 4 5 6 7 8
0 0 0 0 0 0 0 0 0 0
1 0
2 0
3 0
4 0
Pi
____
2
3
4
1
Wi
_____
3
4
5
6
iw 0 1 2 3 4 5 6 7 8
Pi Wi 0 0 0 0 0 0 0 0 0 0
2 3 1 0 0 0 2
3 4 2 0
4 5 3 0
1 6 4 0
The bag of capacity
W = 4
Value of wi = 3
i.e., Solution is 2
iW 0 1 2 3 4 5 6 7 8
Pi Wi 0 0 0 0 0 0 0 0 0 0
2 3 1 0 0 0 2 2 2 2 2 2
3 4 2 0 0 0 2 3 3 3 5 5
4 5 3 0
1 6 4 0
= Max( 3+0,2)
= Max(3,2)
= 3
= Max (3+2,2)
=Max (5,2)
= 5
iw 0 1 2 3 4 5 6 7 8
Pi Wi 0 0 0 0 0 0 0 0 0 0
2 3 1 0 0 0 2 2 2 2 2 2
3 4 2 0 0 0 2 3 3 3 5 5
4 5 3 0 0 0 2 3 4 4 5 6
1 6 4 0
= Max(4+0,3) = Max(4+0,5)
= Max(4+0,3) = Max(4+2,5)
iw 0 1 2 3 4 5 6 7 8
Pi Wi 0 0 0 0 0 0 0 0 0 0
2 3 1 0 0 0 2 2 2 2 2 2
3 4 2 0 0 0 2 3 3 3 5 5
4 5 3 0 0 0 2 3 4 4 5 6
1 6 4 0 0 0 2 3 4 4 5 6
= Max(1+0,4)
= Max(1+0,5)
= Max(1+0,6)
The Maximum profit is 6.
The formula for filling a
particular value of the cell:
“m[i,w] = max( m[i-1,w], m[i-
1,w-w[i] ] + p[i] ) “
Eg:
m(4,7) = max( m[3,7] , m[3,
7-6]+ 1)
m(4,7) = max(5,0+1)
m(4,7) = max(5,1)
=> 5
Knapsack problem

More Related Content

Similar to Knapsack problem

Building a cutting-edge data processing environment on a budget
Building a cutting-edge data processing environment on a budgetBuilding a cutting-edge data processing environment on a budget
Building a cutting-edge data processing environment on a budgetGael Varoquaux
 
Deep Neural Networks for Computer Vision
Deep Neural Networks for Computer VisionDeep Neural Networks for Computer Vision
Deep Neural Networks for Computer VisionAlex Conway
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingComparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingEditor IJMTER
 
From grep to BERT
From grep to BERTFrom grep to BERT
From grep to BERTQAware GmbH
 
Tears for quantum fears
Tears for quantum fearsTears for quantum fears
Tears for quantum fearsMark Carney
 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesTalha Shaikh
 
A practical Introduction to Machine(s) Learning
A practical Introduction to Machine(s) LearningA practical Introduction to Machine(s) Learning
A practical Introduction to Machine(s) LearningBruno Gonçalves
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptxAkswant
 
Deep learning Malaysia presentation 12/4/2017
Deep learning Malaysia presentation 12/4/2017Deep learning Malaysia presentation 12/4/2017
Deep learning Malaysia presentation 12/4/2017Brian Ho
 
An approach to solve the N-Queens Problem using Artificial Intelligence algor...
An approach to solve the N-Queens Problem using Artificial Intelligence algor...An approach to solve the N-Queens Problem using Artificial Intelligence algor...
An approach to solve the N-Queens Problem using Artificial Intelligence algor...IRJET Journal
 
Deep Learning with Apache MXNet (September 2017)
Deep Learning with Apache MXNet (September 2017)Deep Learning with Apache MXNet (September 2017)
Deep Learning with Apache MXNet (September 2017)Julien SIMON
 
Simple big data, in Python
Simple big data, in PythonSimple big data, in Python
Simple big data, in PythonGael Varoquaux
 
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...Herman Wu
 
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)Tech in Asia ID
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.pptRuchika Sinha
 
module3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfmodule3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfShiwani Gupta
 
Support Vector Machine and Implementation using Weka
Support Vector Machine and Implementation using WekaSupport Vector Machine and Implementation using Weka
Support Vector Machine and Implementation using WekaMacha Pujitha
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory FunctionBarani Tharan
 
Lunch session: Quantum Computing
Lunch session: Quantum ComputingLunch session: Quantum Computing
Lunch session: Quantum ComputingRolf Huisman
 

Similar to Knapsack problem (20)

Building a cutting-edge data processing environment on a budget
Building a cutting-edge data processing environment on a budgetBuilding a cutting-edge data processing environment on a budget
Building a cutting-edge data processing environment on a budget
 
Deep Neural Networks for Computer Vision
Deep Neural Networks for Computer VisionDeep Neural Networks for Computer Vision
Deep Neural Networks for Computer Vision
 
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programmingComparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
 
From grep to BERT
From grep to BERTFrom grep to BERT
From grep to BERT
 
Tears for quantum fears
Tears for quantum fearsTears for quantum fears
Tears for quantum fears
 
Comparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategiesComparitive Analysis of Algorithm strategies
Comparitive Analysis of Algorithm strategies
 
A practical Introduction to Machine(s) Learning
A practical Introduction to Machine(s) LearningA practical Introduction to Machine(s) Learning
A practical Introduction to Machine(s) Learning
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptx
 
Deep learning Malaysia presentation 12/4/2017
Deep learning Malaysia presentation 12/4/2017Deep learning Malaysia presentation 12/4/2017
Deep learning Malaysia presentation 12/4/2017
 
An approach to solve the N-Queens Problem using Artificial Intelligence algor...
An approach to solve the N-Queens Problem using Artificial Intelligence algor...An approach to solve the N-Queens Problem using Artificial Intelligence algor...
An approach to solve the N-Queens Problem using Artificial Intelligence algor...
 
Deep Learning with Apache MXNet (September 2017)
Deep Learning with Apache MXNet (September 2017)Deep Learning with Apache MXNet (September 2017)
Deep Learning with Apache MXNet (September 2017)
 
Simple big data, in Python
Simple big data, in PythonSimple big data, in Python
Simple big data, in Python
 
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
運用CNTK 實作深度學習物件辨識 Deep Learning based Object Detection with Microsoft Cogniti...
 
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
"Practical Machine Learning With Ruby" by Iqbal Farabi (ID Ruby Community)
 
DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
 
module3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdfmodule3_Greedymethod_2022.pdf
module3_Greedymethod_2022.pdf
 
Support Vector Machine and Implementation using Weka
Support Vector Machine and Implementation using WekaSupport Vector Machine and Implementation using Weka
Support Vector Machine and Implementation using Weka
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Knapsack problem and Memory Function
Knapsack problem and Memory FunctionKnapsack problem and Memory Function
Knapsack problem and Memory Function
 
Lunch session: Quantum Computing
Lunch session: Quantum ComputingLunch session: Quantum Computing
Lunch session: Quantum Computing
 

Recently uploaded

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxmanuelaromero2013
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptxVS Mahajan Coaching Centre
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfJayanti Pande
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfchloefrazer622
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationnomboosow
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxiammrhaywood
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3JemimahLaneBuaron
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Educationpboyjonauth
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxGaneshChakor2
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfsanyamsingh5019
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxSayali Powar
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Sapana Sha
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 

Recently uploaded (20)

How to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptxHow to Make a Pirate ship Primary Education.pptx
How to Make a Pirate ship Primary Education.pptx
 
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
Mattingly "AI & Prompt Design: Structured Data, Assistants, & RAG"
 
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions  for the students and aspirants of Chemistry12th.pptxOrganic Name Reactions  for the students and aspirants of Chemistry12th.pptx
Organic Name Reactions for the students and aspirants of Chemistry12th.pptx
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 
Web & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdfWeb & Social Media Analytics Previous Year Question Paper.pdf
Web & Social Media Analytics Previous Year Question Paper.pdf
 
Arihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdfArihant handbook biology for class 11 .pdf
Arihant handbook biology for class 11 .pdf
 
Interactive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communicationInteractive Powerpoint_How to Master effective communication
Interactive Powerpoint_How to Master effective communication
 
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptxSOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
SOCIAL AND HISTORICAL CONTEXT - LFTVD.pptx
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3Q4-W6-Restating Informational Text Grade 3
Q4-W6-Restating Informational Text Grade 3
 
Introduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher EducationIntroduction to ArtificiaI Intelligence in Higher Education
Introduction to ArtificiaI Intelligence in Higher Education
 
CARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptxCARE OF CHILD IN INCUBATOR..........pptx
CARE OF CHILD IN INCUBATOR..........pptx
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1Código Creativo y Arte de Software | Unidad 1
Código Creativo y Arte de Software | Unidad 1
 
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdfTataKelola dan KamSiber Kecerdasan Buatan v022.pdf
TataKelola dan KamSiber Kecerdasan Buatan v022.pdf
 
Sanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdfSanyam Choudhary Chemistry practical.pdf
Sanyam Choudhary Chemistry practical.pdf
 
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptxPOINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
POINT- BIOCHEMISTRY SEM 2 ENZYMES UNIT 5.pptx
 
Staff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSDStaff of Color (SOC) Retention Efforts DDSD
Staff of Color (SOC) Retention Efforts DDSD
 
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111Call Girls in Dwarka Mor Delhi Contact Us 9654467111
Call Girls in Dwarka Mor Delhi Contact Us 9654467111
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 

Knapsack problem

  • 1. 0/1 KNAPSACK PROBLEM -RACKSAVI.R, I M.Sc Information Technology, V.V.Vanniaperumal college for women, Virudhunagar.
  • 2. OBJECTIVES  Dynamic Programming  0/1 Knapsack Problem  Algorithm  Example Problem
  • 3. DYNAMIC PROGRAMMING Dynamic Programming is used where we have problems, which can be divided into similar sub- problems.
  • 4. 0/1 KNAPSACK PROBLEM  The 0/1 Knapsack Problem is a classical dynamic programming problem.  The goal is to maximize the value of a Knapsack that can hold at most W units.
  • 5. Contd… The objective is to maximize the profit:- • The capacity of the bag is C. • We have n items. • The ith item has Value vi and Weight wi.
  • 6. ALGORITHM for w = 0 to w V[0,w] = 0 O(W) for i = 1 to n V[i ,0] = 0 O(n) for i = 1 to n // Repeat n times for w = 1 to w if wi <= w O(W) //item i can be part of the solution
  • 7. If bi + V[i-1,w-wi] > V[i-1,w] V[i,w] = bi + V[i-1,w-wi] Else V[i,w] = V[i-1,w] Else V[i,w] = V[i-1,w] //wi > w
  • 8. EXAMPLE PROBLEM GIVEN:- Weight = {3,4,6,5} Profits = {2,3,1,4} W = 8 N = 4
  • 9. p iw 0 1 2 3 4 5 6 7 8 0 0 0 0 0 0 0 0 0 0 1 0 2 0 3 0 4 0 Pi ____ 2 3 4 1 Wi _____ 3 4 5 6
  • 10. iw 0 1 2 3 4 5 6 7 8 Pi Wi 0 0 0 0 0 0 0 0 0 0 2 3 1 0 0 0 2 3 4 2 0 4 5 3 0 1 6 4 0 The bag of capacity W = 4 Value of wi = 3 i.e., Solution is 2
  • 11. iW 0 1 2 3 4 5 6 7 8 Pi Wi 0 0 0 0 0 0 0 0 0 0 2 3 1 0 0 0 2 2 2 2 2 2 3 4 2 0 0 0 2 3 3 3 5 5 4 5 3 0 1 6 4 0 = Max( 3+0,2) = Max(3,2) = 3 = Max (3+2,2) =Max (5,2) = 5
  • 12. iw 0 1 2 3 4 5 6 7 8 Pi Wi 0 0 0 0 0 0 0 0 0 0 2 3 1 0 0 0 2 2 2 2 2 2 3 4 2 0 0 0 2 3 3 3 5 5 4 5 3 0 0 0 2 3 4 4 5 6 1 6 4 0 = Max(4+0,3) = Max(4+0,5) = Max(4+0,3) = Max(4+2,5)
  • 13. iw 0 1 2 3 4 5 6 7 8 Pi Wi 0 0 0 0 0 0 0 0 0 0 2 3 1 0 0 0 2 2 2 2 2 2 3 4 2 0 0 0 2 3 3 3 5 5 4 5 3 0 0 0 2 3 4 4 5 6 1 6 4 0 0 0 2 3 4 4 5 6 = Max(1+0,4) = Max(1+0,5) = Max(1+0,6)
  • 14. The Maximum profit is 6. The formula for filling a particular value of the cell: “m[i,w] = max( m[i-1,w], m[i- 1,w-w[i] ] + p[i] ) “ Eg: m(4,7) = max( m[3,7] , m[3, 7-6]+ 1) m(4,7) = max(5,0+1) m(4,7) = max(5,1) => 5