SlideShare a Scribd company logo
1 of 15
0-1 KNAPSACK PROBLEM
Statement of the problem:
Given n items, each with corresponding value p and weight w,
find which items to place in the knapsack such that sum of all p
is maximum, and the sum of all w does not exceed the maximum
weight capacity c of the knapsack.
We can also express the problem as follows:
1
n
i i
i
p x

 is maximum and
1
n
i i
i
w x c


where
0 item is not taken
1 item is taken
ix

 

Solution #1 : Brute force
 We take all possible item combinations.
 For any n items, the total number of combinations is
 
0
,
n
i
C i n

 = 2n
 We pick the combinations that satisfy the constraint and sort
each p and get the maximum.
 This approach has complexity  2n
O .
Solution #2:
Dynamic Programming (Bottom-Top Computation)
Construct an n  c value matrix V to compute a value in each cell
for every row in the matrix.
The last cell V[n, c] will give the solution to the maximum total
value.
Bottom-Top computation pseudocode:
for i = 0 to c:
V[0, i]  0
for i = 0 to n:
for k = 0 to c:
V[i, k]  Max(V[i - 1, k], pi + V[i - 1, k - wi])
Example:
i 1 2 3 4 5
p 30 20 40 70 60
w 4 1 2 5 3
n = 5
c = 10
Solution:
 The value matrix can be viewed as bottom-top, with the first
row (i = 0) the bottom, and moving up to the succeeding
rows up to the top (i = n).
 Row 0 of the value matrix all start with 0.
 The column k starts at 0 and ends at c (the constraint).
k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
 Value at V[i, k] = Max(V[i - 1, k], pi + V[i- 1, k - wi])
where
ip - the value of the item at row i
iw - the weight of the item at row i
 For each row, we search each column where 0ik w  , i.e.,
the maximum is V[i - 1, k] (the cell above the current cell).
 If 0ik w  , compare  1,V i k and  1,i ip V i k w   . The
maximum of the two is the value of  ,V i k .
k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 30 30 30 30 30 30 30
at i = 1, k = 4:
   1, 0,4 0V i k V  
1 130, 4p w  , 14 4 4 0ik w w     
   11, 0,0 30 0 30i ip V i k w p V       
Completing the value matrix:
k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 30 30 30 30 30 30 30
2 0 20 20 20 30 50 50 50 50 50 50
3 0 20 40 60 60 60 70 90 90 90 90
4 0 20 40 60 60 70 90 110 130 130 130
5 0 20 40 60 80 100 120 120 130 150 170
The last cell at  ,V n c is the solution to the maximum value.
 The value matrix only showed the solution to the maximum
value, but not the individual items chosen.
 Modify the last pseudocode to mark the cells where the
maximum is  1,i ip V i k w   , where 0ik w  .
k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 30 30 30 30 30 30 30
2 0 20 20 20 30 50 50 50 50 50 50
3 0 20 40 60 60 60 70 90 90 90 90
4 0 20 40 60 60 70 90 110 130 130 130
5 0 20 40 60 80 100 120 120 130 150 170
Pseudocode to find the items selected:
k = c
for i = n down to 0:
if  ,V i k is marked:
output item i
ik k w 
From the last example:
k
i
0 1 2 3 4 5 6 7 8 9 10
0 0 0 0 0 0 0 0 0 0 0 0
1 0 0 0 0 30 30 30 30 30 30 30
2 0 20 20 20 30 50 50 50 50 50 50
3 0 20 40 60 60 60 70 90 90 90 90
4 0 20 40 60 60 70 90 110 130 130 130
5 0 20 40 60 80 100 120 120 130 150 170
i k Marked
5 10 Yes
4 10 - 5w = 10 - 3 = 7 Yes
3 7 - 4w = 7 - 5 = 2 Yes
2 32 w = 2 - 2 = 0 No
1 0 - 2w = 0 - 1 = INVALID No
The items selected are 3, 4, 5.
 Bottom-top computation has complexity  O nc .
 For large n, a vast improvement to  2n
O .
 Any problem involving maximizing a total value while
satisfying a constraint can use this method, as long as the
items can only be either chosen or not, i.e., the item cannot
be broken into smaller parts.

More Related Content

What's hot

Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13Kumar
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmHoneyChintal
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back trackingTech_MX
 
5.5 back track
5.5 back track5.5 back track
5.5 back trackKrish_ver2
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingrowntu
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programingrupali_2bonde
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cyclevarun arora
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithmRezwan Siam
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 

What's hot (20)

Fractional knapsack class 13
Fractional knapsack class 13Fractional knapsack class 13
Fractional knapsack class 13
 
NP completeness
NP completenessNP completeness
NP completeness
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
5.1 greedy
5.1 greedy5.1 greedy
5.1 greedy
 
8 queens problem using back tracking
8 queens problem using back tracking8 queens problem using back tracking
8 queens problem using back tracking
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
5.5 back track
5.5 back track5.5 back track
5.5 back track
 
Primality
PrimalityPrimality
Primality
 
Data Structure and Algorithm - Divide and Conquer
Data Structure and Algorithm - Divide and ConquerData Structure and Algorithm - Divide and Conquer
Data Structure and Algorithm - Divide and Conquer
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
 
Daa:Dynamic Programing
Daa:Dynamic ProgramingDaa:Dynamic Programing
Daa:Dynamic Programing
 
Np complete
Np completeNp complete
Np complete
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Heap sort
Heap sortHeap sort
Heap sort
 
Recursive algorithms
Recursive algorithmsRecursive algorithms
Recursive algorithms
 
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycleBacktracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
Backtracking-N Queens Problem-Graph Coloring-Hamiltonian cycle
 
Greedy algorithm
Greedy algorithmGreedy algorithm
Greedy algorithm
 
Bruteforce algorithm
Bruteforce algorithmBruteforce algorithm
Bruteforce algorithm
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 

Similar to 0-1 KNAPSACK PROBLEM

Perceptron 2015.ppt
Perceptron 2015.pptPerceptron 2015.ppt
Perceptron 2015.pptSadafAyesha9
 
Quantum Computing Notes Ver 1.2
Quantum Computing Notes Ver 1.2Quantum Computing Notes Ver 1.2
Quantum Computing Notes Ver 1.2Vijayananda Mohire
 
Supporting Vector Machine
Supporting Vector MachineSupporting Vector Machine
Supporting Vector MachineSumit Singh
 
Data structure notes
Data structure notesData structure notes
Data structure notesanujab5
 
Nodal & Mesh Analysis
Nodal & Mesh AnalysisNodal & Mesh Analysis
Nodal & Mesh AnalysisQuaziRossi
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandboundhodcsencet
 
The Perceptron and its Learning Rule
The Perceptron and its Learning RuleThe Perceptron and its Learning Rule
The Perceptron and its Learning RuleNoor Ul Hudda Memon
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack DynamicParas Patel
 
Newton raphson method
Newton raphson methodNewton raphson method
Newton raphson methodNazrul Kabir
 
A Stochastic Limit Approach To The SAT Problem
A Stochastic Limit Approach To The SAT ProblemA Stochastic Limit Approach To The SAT Problem
A Stochastic Limit Approach To The SAT ProblemValerie Felton
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Techglyphs
 
Stability with analysis and psa and load flow.ppt
Stability with analysis and psa and load flow.pptStability with analysis and psa and load flow.ppt
Stability with analysis and psa and load flow.pptZahid Yousaf
 
Schrodinger equation in quantum mechanics
Schrodinger equation in quantum mechanicsSchrodinger equation in quantum mechanics
Schrodinger equation in quantum mechanicsRakeshPatil2528
 
Lecture (3) _transportation problem31-10-2020.pdf
Lecture (3) _transportation problem31-10-2020.pdfLecture (3) _transportation problem31-10-2020.pdf
Lecture (3) _transportation problem31-10-2020.pdfamrhebafamily
 

Similar to 0-1 KNAPSACK PROBLEM (20)

Perceptron 2015.ppt
Perceptron 2015.pptPerceptron 2015.ppt
Perceptron 2015.ppt
 
Quantum Computing Notes Ver 1.2
Quantum Computing Notes Ver 1.2Quantum Computing Notes Ver 1.2
Quantum Computing Notes Ver 1.2
 
Supporting Vector Machine
Supporting Vector MachineSupporting Vector Machine
Supporting Vector Machine
 
Data structure notes
Data structure notesData structure notes
Data structure notes
 
Nodal & Mesh Analysis
Nodal & Mesh AnalysisNodal & Mesh Analysis
Nodal & Mesh Analysis
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
 
The Perceptron and its Learning Rule
The Perceptron and its Learning RuleThe Perceptron and its Learning Rule
The Perceptron and its Learning Rule
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
 
Lop1
Lop1Lop1
Lop1
 
solution for 2D truss1
solution for 2D truss1solution for 2D truss1
solution for 2D truss1
 
Knapsack dp
Knapsack dpKnapsack dp
Knapsack dp
 
2-Perceptrons.pdf
2-Perceptrons.pdf2-Perceptrons.pdf
2-Perceptrons.pdf
 
Assignmentl3 solutions
Assignmentl3 solutionsAssignmentl3 solutions
Assignmentl3 solutions
 
CostFunctions.pdf
CostFunctions.pdfCostFunctions.pdf
CostFunctions.pdf
 
Newton raphson method
Newton raphson methodNewton raphson method
Newton raphson method
 
A Stochastic Limit Approach To The SAT Problem
A Stochastic Limit Approach To The SAT ProblemA Stochastic Limit Approach To The SAT Problem
A Stochastic Limit Approach To The SAT Problem
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2
 
Stability with analysis and psa and load flow.ppt
Stability with analysis and psa and load flow.pptStability with analysis and psa and load flow.ppt
Stability with analysis and psa and load flow.ppt
 
Schrodinger equation in quantum mechanics
Schrodinger equation in quantum mechanicsSchrodinger equation in quantum mechanics
Schrodinger equation in quantum mechanics
 
Lecture (3) _transportation problem31-10-2020.pdf
Lecture (3) _transportation problem31-10-2020.pdfLecture (3) _transportation problem31-10-2020.pdf
Lecture (3) _transportation problem31-10-2020.pdf
 

More from i i

Bouncing circle
Bouncing circleBouncing circle
Bouncing circlei i
 
sequential and combinational circuits exam
sequential and combinational circuits examsequential and combinational circuits exam
sequential and combinational circuits exami i
 
hypothesis testing overview
hypothesis testing overviewhypothesis testing overview
hypothesis testing overviewi i
 
x86 architecture
x86 architecturex86 architecture
x86 architecturei i
 
boolean algebra exercises
boolean algebra exercisesboolean algebra exercises
boolean algebra exercisesi i
 
database normalization case study
database normalization case studydatabase normalization case study
database normalization case studyi i
 
cpbricks context diagram
cpbricks context diagramcpbricks context diagram
cpbricks context diagrami i
 
cpbricks project document
cpbricks project documentcpbricks project document
cpbricks project documenti i
 
cpbricks manual
cpbricks manualcpbricks manual
cpbricks manuali i
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, androidi i
 
shortest job first
shortest job firstshortest job first
shortest job firsti i
 
designing reports
designing reportsdesigning reports
designing reportsi i
 
bnf of c switch statement
bnf of c switch statementbnf of c switch statement
bnf of c switch statementi i
 
shell and merge sort
shell and merge sortshell and merge sort
shell and merge sorti i
 
adders/subtractors, multiplexers, intro to ISA
adders/subtractors, multiplexers, intro to ISAadders/subtractors, multiplexers, intro to ISA
adders/subtractors, multiplexers, intro to ISAi i
 

More from i i (15)

Bouncing circle
Bouncing circleBouncing circle
Bouncing circle
 
sequential and combinational circuits exam
sequential and combinational circuits examsequential and combinational circuits exam
sequential and combinational circuits exam
 
hypothesis testing overview
hypothesis testing overviewhypothesis testing overview
hypothesis testing overview
 
x86 architecture
x86 architecturex86 architecture
x86 architecture
 
boolean algebra exercises
boolean algebra exercisesboolean algebra exercises
boolean algebra exercises
 
database normalization case study
database normalization case studydatabase normalization case study
database normalization case study
 
cpbricks context diagram
cpbricks context diagramcpbricks context diagram
cpbricks context diagram
 
cpbricks project document
cpbricks project documentcpbricks project document
cpbricks project document
 
cpbricks manual
cpbricks manualcpbricks manual
cpbricks manual
 
imperative programming language, java, android
imperative programming language, java, androidimperative programming language, java, android
imperative programming language, java, android
 
shortest job first
shortest job firstshortest job first
shortest job first
 
designing reports
designing reportsdesigning reports
designing reports
 
bnf of c switch statement
bnf of c switch statementbnf of c switch statement
bnf of c switch statement
 
shell and merge sort
shell and merge sortshell and merge sort
shell and merge sort
 
adders/subtractors, multiplexers, intro to ISA
adders/subtractors, multiplexers, intro to ISAadders/subtractors, multiplexers, intro to ISA
adders/subtractors, multiplexers, intro to ISA
 

Recently uploaded

Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...MyIntelliSource, Inc.
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantAxelRicardoTrocheRiq
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsAlberto González Trastoy
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Modelsaagamshah0812
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationkaushalgiri8080
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfkalichargn70th171
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptkotipi9215
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfkalichargn70th171
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...MyIntelliSource, Inc.
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEOrtus Solutions, Corp
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...Christina Lin
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - InfographicHr365.us smith
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataBradBedford3
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityNeo4j
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...stazi3110
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...OnePlan Solutions
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about usDynamic Netsoft
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software DevelopersVinodh Ram
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number SystemsJheuzeDellosa
 

Recently uploaded (20)

Exploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the ProcessExploring iOS App Development: Simplifying the Process
Exploring iOS App Development: Simplifying the Process
 
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
Steps To Getting Up And Running Quickly With MyTimeClock Employee Scheduling ...
 
Salesforce Certified Field Service Consultant
Salesforce Certified Field Service ConsultantSalesforce Certified Field Service Consultant
Salesforce Certified Field Service Consultant
 
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time ApplicationsUnveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
Unveiling the Tech Salsa of LAMs with Janus in Real-Time Applications
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
Project Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanationProject Based Learning (A.I).pptx detail explanation
Project Based Learning (A.I).pptx detail explanation
 
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
chapter--4-software-project-planning.ppt
chapter--4-software-project-planning.pptchapter--4-software-project-planning.ppt
chapter--4-software-project-planning.ppt
 
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdfThe Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
The Essentials of Digital Experience Monitoring_ A Comprehensive Guide.pdf
 
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
Try MyIntelliAccount Cloud Accounting Software As A Service Solution Risk Fre...
 
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASEBATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
BATTLEFIELD ORM: TIPS, TACTICS AND STRATEGIES FOR CONQUERING YOUR DATABASE
 
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
ODSC - Batch to Stream workshop - integration of Apache Spark, Cassandra, Pos...
 
Asset Management Software - Infographic
Asset Management Software - InfographicAsset Management Software - Infographic
Asset Management Software - Infographic
 
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer DataAdobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
Adobe Marketo Engage Deep Dives: Using Webhooks to Transfer Data
 
EY_Graph Database Powered Sustainability
EY_Graph Database Powered SustainabilityEY_Graph Database Powered Sustainability
EY_Graph Database Powered Sustainability
 
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
Building a General PDE Solving Framework with Symbolic-Numeric Scientific Mac...
 
Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...Advancing Engineering with AI through the Next Generation of Strategic Projec...
Advancing Engineering with AI through the Next Generation of Strategic Projec...
 
DNT_Corporate presentation know about us
DNT_Corporate presentation know about usDNT_Corporate presentation know about us
DNT_Corporate presentation know about us
 
Professional Resume Template for Software Developers
Professional Resume Template for Software DevelopersProfessional Resume Template for Software Developers
Professional Resume Template for Software Developers
 
What is Binary Language? Computer Number Systems
What is Binary Language?  Computer Number SystemsWhat is Binary Language?  Computer Number Systems
What is Binary Language? Computer Number Systems
 

0-1 KNAPSACK PROBLEM

  • 2. Statement of the problem: Given n items, each with corresponding value p and weight w, find which items to place in the knapsack such that sum of all p is maximum, and the sum of all w does not exceed the maximum weight capacity c of the knapsack.
  • 3. We can also express the problem as follows: 1 n i i i p x   is maximum and 1 n i i i w x c   where 0 item is not taken 1 item is taken ix    
  • 4. Solution #1 : Brute force  We take all possible item combinations.  For any n items, the total number of combinations is   0 , n i C i n   = 2n  We pick the combinations that satisfy the constraint and sort each p and get the maximum.  This approach has complexity  2n O .
  • 5. Solution #2: Dynamic Programming (Bottom-Top Computation) Construct an n  c value matrix V to compute a value in each cell for every row in the matrix. The last cell V[n, c] will give the solution to the maximum total value.
  • 6. Bottom-Top computation pseudocode: for i = 0 to c: V[0, i]  0 for i = 0 to n: for k = 0 to c: V[i, k]  Max(V[i - 1, k], pi + V[i - 1, k - wi])
  • 7. Example: i 1 2 3 4 5 p 30 20 40 70 60 w 4 1 2 5 3 n = 5 c = 10
  • 8. Solution:  The value matrix can be viewed as bottom-top, with the first row (i = 0) the bottom, and moving up to the succeeding rows up to the top (i = n).  Row 0 of the value matrix all start with 0.  The column k starts at 0 and ends at c (the constraint). k i 0 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0
  • 9.  Value at V[i, k] = Max(V[i - 1, k], pi + V[i- 1, k - wi]) where ip - the value of the item at row i iw - the weight of the item at row i  For each row, we search each column where 0ik w  , i.e., the maximum is V[i - 1, k] (the cell above the current cell).  If 0ik w  , compare  1,V i k and  1,i ip V i k w   . The maximum of the two is the value of  ,V i k .
  • 10. k i 0 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 30 30 30 30 30 30 30 at i = 1, k = 4:    1, 0,4 0V i k V   1 130, 4p w  , 14 4 4 0ik w w         11, 0,0 30 0 30i ip V i k w p V       
  • 11. Completing the value matrix: k i 0 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 30 30 30 30 30 30 30 2 0 20 20 20 30 50 50 50 50 50 50 3 0 20 40 60 60 60 70 90 90 90 90 4 0 20 40 60 60 70 90 110 130 130 130 5 0 20 40 60 80 100 120 120 130 150 170 The last cell at  ,V n c is the solution to the maximum value.
  • 12.  The value matrix only showed the solution to the maximum value, but not the individual items chosen.  Modify the last pseudocode to mark the cells where the maximum is  1,i ip V i k w   , where 0ik w  . k i 0 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 30 30 30 30 30 30 30 2 0 20 20 20 30 50 50 50 50 50 50 3 0 20 40 60 60 60 70 90 90 90 90 4 0 20 40 60 60 70 90 110 130 130 130 5 0 20 40 60 80 100 120 120 130 150 170
  • 13. Pseudocode to find the items selected: k = c for i = n down to 0: if  ,V i k is marked: output item i ik k w 
  • 14. From the last example: k i 0 1 2 3 4 5 6 7 8 9 10 0 0 0 0 0 0 0 0 0 0 0 0 1 0 0 0 0 30 30 30 30 30 30 30 2 0 20 20 20 30 50 50 50 50 50 50 3 0 20 40 60 60 60 70 90 90 90 90 4 0 20 40 60 60 70 90 110 130 130 130 5 0 20 40 60 80 100 120 120 130 150 170 i k Marked 5 10 Yes 4 10 - 5w = 10 - 3 = 7 Yes 3 7 - 4w = 7 - 5 = 2 Yes 2 32 w = 2 - 2 = 0 No 1 0 - 2w = 0 - 1 = INVALID No The items selected are 3, 4, 5.
  • 15.  Bottom-top computation has complexity  O nc .  For large n, a vast improvement to  2n O .  Any problem involving maximizing a total value while satisfying a constraint can use this method, as long as the items can only be either chosen or not, i.e., the item cannot be broken into smaller parts.