SlideShare a Scribd company logo
1 of 19
Design and Analysis of Algorithms
Lecture # 15
0/1 Knapsack Problem
0/1 Knapsack Problem
• A thief goes into a jewelry store to steal jewelry items. He
has a knapsack (a bag) that he would like to fill up.
• The bag has a limit on the total weight of the objects
placed in it. If the total weight exceeds the limit, the bag
would tear open.
• The value of the jewelry items varies for cheap to
expensive.
• The thief’s goal is to put items in the bag such that the
value of the items is maximized and the weight of the
items does not exceed the weight limit of the bag.
• Another limitation is that an item can either be put in the
bag or not - fractional items are not allowed.
0/1 Knapsack Problem
• The problem is: what jewelry should the thief choose that
satisfy the constraints?
• Formally, the problem can be stated as follows: Given a
knapsack with maximum capacity W, and a set S consisting
of n items.
• Each item i has some weight wi and value value vi (all wi ,
vi and W are integer values).
• How to pack the knapsack to achieve maximum total value
of packed items?
0/1 Knapsack Problem
For example, consider the following scenario:
0/1 Knapsack Problem
• The knapsack problem belongs to the domain of
optimization problems. Mathematically, the problem is:
• The problem is called a “0-1” problem, because each
item must be entirely accepted or rejected. How do
we solve the problem.
0/1 Knapsack Problem
• We could try the brute-force solution:
-Since there are n items, there are 2n possible
combinations of the items (an item either chosen or
not).
-We go through all combinations and find the one
with the most total value and with total weight
less or equal to W.
• Clearly, the running time of such a brute-force
algorithm will be O(2n). Can we do better? The answer
is “yes”, with an algorithm based on dynamic
programming.
0/1 Knapsack Problem-DP Approach
• For each i ≤ n and each w ≤ W, solve the knapsack problem
for the first i objects when the capacity is w.
• Why will this work? Because solutions to larger sub
problems can be built up easily from solutions to smaller
ones. We construct a matrix V [0 . . . n, 0 . . . W].
• For 1 ≤ i ≤ n, and 0 ≤ j ≤ W, V[i, j] will store the maximum
value of any set of objects {1, 2, . . . , i} that can fit into a
knapsack of weight j.
• V[n, W] will contain the maximum value of all n objects
that can fit into the entire knapsack of weight W.
• To compute entries of V we will imply an inductive
approach. As a basis, V[0, j] = 0 for 0 ≤ j ≤ W
0/1 Knapsack Problem
• since if we have no items then we have no value. We
consider two cases:
Leave object i: If we choose to not take object i, then the
optimal value will come about by considering how to fill a
knapsack of size j with the remaining objects {1, 2, . . . , i -
1}. This is just V[i - 1, j].
Take object i: If we take object i, then we gain a value of vi.
But we use up wi of our capacity. With the remaining j - wi
capacity in the knapsack, we can fill it in the best possible
way with objects {1, 2, . . . , i - 1}. This is vi + V[i - 1, j - wi].
This is only possible if wi ≤ j.
0/1 Knapsack Problem
•This leads to the following recursive formulation:
A naive evaluation of this recursive definition is
exponential. So, as usual, we avoid re-computation by
making a table .
•
0/1 Knapsack Problem
• Example: The maximum weight the knapsack can hold
is W is 11. There are five items to choose from.
Their weights and values are presented in the following
table:
• The [i, j] entry here will be V[i, j], the best value
obtainable using the first i rows of items if the
maximum capacity were j. We begin by initializating and
first row.
0/1 Knapsack Problem
• Recall that we take V[i, j] to be 0 if either i or j is ≤ 0. We
then proceed to fill in top-down, left-to-right
always using
0/1 Knapsack Problem
• V[i, j] = max{V[i - 1, j], vi + V[i - 1, j - wi] }
0/1 Knapsack Problem
0/1 Knapsack Problem
0/1 Knapsack Problem
• Finally, we have
0/1 Knapsack Problem-DP Algorithm
The time complexity is clearly O(n · W). It must be
cautioned that as n and W get large, both time and space
complexity become significant.
0/1 Knapsack Problem-DP Algorithm
Constructing the Optimal Solution
The algorithm for computing V[i, j] does not keep record
of which subset of items gives the optimal solution. To
compute the actual subset, we can add an auxiliary
boolean array keep[i, j] which is 1 if we decide to take the
ith item and 0 otherwise. We will use all the values keep[i,
j] to determine the optimal subset T of items to put in
the knapsack as follows:
0/1 Knapsack Problem-DP Algorithm
0/1 Knapsack Problem
• Here is the keep matrix for the example problem.
• When the item selection algorithm is applied, the
selected items are 4 and 3. This is indicated by the
boxed entries in the table above.

More Related Content

Similar to Design and analysis of Algorithms - Lecture 15.ppt

Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Ridhima Chowdhury
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approachpadmeshagrekar
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnewabhinav108
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmHoneyChintal
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Techglyphs
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptxAkswant
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEMMrunal Patil
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and boundAbhishek Singh
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandboundhodcsencet
 
Approximation alogrithms
Approximation alogrithmsApproximation alogrithms
Approximation alogrithmsMohsen Fatemi
 

Similar to Design and analysis of Algorithms - Lecture 15.ppt (20)

Knapsack dp
Knapsack dpKnapsack dp
Knapsack dp
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)Knapsack Problem (DP & GREEDY)
Knapsack Problem (DP & GREEDY)
 
Lop1
Lop1Lop1
Lop1
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
UNIT V.pptx
UNIT V.pptxUNIT V.pptx
UNIT V.pptx
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
 
12 Greeddy Method
12 Greeddy Method12 Greeddy Method
12 Greeddy Method
 
knapsack.pptx
knapsack.pptxknapsack.pptx
knapsack.pptx
 
AOA ppt.ppt
AOA ppt.pptAOA ppt.ppt
AOA ppt.ppt
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2Bt0080 fundamentals of algorithms2
Bt0080 fundamentals of algorithms2
 
Greedy+Day-3.pptx
Greedy+Day-3.pptxGreedy+Day-3.pptx
Greedy+Day-3.pptx
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 
Dynamic Programming
Dynamic ProgrammingDynamic Programming
Dynamic Programming
 
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
 
0 1 knapsack using branch and bound
0 1 knapsack using branch and bound0 1 knapsack using branch and bound
0 1 knapsack using branch and bound
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Approximation alogrithms
Approximation alogrithmsApproximation alogrithms
Approximation alogrithms
 

Recently uploaded

Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfmuntazimhurra
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxkessiyaTpeter
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfSumit Kumar yadav
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...jana861314
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhousejana861314
 
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPirithiRaju
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptxRajatChauhan518211
 
Botany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsBotany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsSumit Kumar yadav
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfSumit Kumar yadav
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoSérgio Sacani
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSarthak Sekhar Mondal
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Sérgio Sacani
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxUmerFayaz5
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Lokesh Kothari
 
Nanoparticles synthesis and characterization​ ​
Nanoparticles synthesis and characterization​  ​Nanoparticles synthesis and characterization​  ​
Nanoparticles synthesis and characterization​ ​kaibalyasahoo82800
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsAArockiyaNisha
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCEPRINCE C P
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PPRINCE C P
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real timeSatoshi NAKAHIRA
 

Recently uploaded (20)

Biological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdfBiological Classification BioHack (3).pdf
Biological Classification BioHack (3).pdf
 
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptxSOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
SOLUBLE PATTERN RECOGNITION RECEPTORS.pptx
 
Botany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdfBotany 4th semester file By Sumit Kumar yadav.pdf
Botany 4th semester file By Sumit Kumar yadav.pdf
 
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
Traditional Agroforestry System in India- Shifting Cultivation, Taungya, Home...
 
Orientation, design and principles of polyhouse
Orientation, design and principles of polyhouseOrientation, design and principles of polyhouse
Orientation, design and principles of polyhouse
 
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdfPests of cotton_Sucking_Pests_Dr.UPR.pdf
Pests of cotton_Sucking_Pests_Dr.UPR.pdf
 
Green chemistry and Sustainable development.pptx
Green chemistry  and Sustainable development.pptxGreen chemistry  and Sustainable development.pptx
Green chemistry and Sustainable development.pptx
 
Botany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questionsBotany krishna series 2nd semester Only Mcq type questions
Botany krishna series 2nd semester Only Mcq type questions
 
Chemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdfChemistry 4th semester series (krishna).pdf
Chemistry 4th semester series (krishna).pdf
 
Isotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on IoIsotopic evidence of long-lived volcanism on Io
Isotopic evidence of long-lived volcanism on Io
 
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatidSpermiogenesis or Spermateleosis or metamorphosis of spermatid
Spermiogenesis or Spermateleosis or metamorphosis of spermatid
 
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
Discovery of an Accretion Streamer and a Slow Wide-angle Outflow around FUOri...
 
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
9953056974 Young Call Girls In Mahavir enclave Indian Quality Escort service
 
Animal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptxAnimal Communication- Auditory and Visual.pptx
Animal Communication- Auditory and Visual.pptx
 
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
Labelling Requirements and Label Claims for Dietary Supplements and Recommend...
 
Nanoparticles synthesis and characterization​ ​
Nanoparticles synthesis and characterization​  ​Nanoparticles synthesis and characterization​  ​
Nanoparticles synthesis and characterization​ ​
 
Natural Polymer Based Nanomaterials
Natural Polymer Based NanomaterialsNatural Polymer Based Nanomaterials
Natural Polymer Based Nanomaterials
 
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCESTERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
STERILITY TESTING OF PHARMACEUTICALS ppt by DR.C.P.PRINCE
 
Artificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C PArtificial Intelligence In Microbiology by Dr. Prince C P
Artificial Intelligence In Microbiology by Dr. Prince C P
 
Grafana in space: Monitoring Japan's SLIM moon lander in real time
Grafana in space: Monitoring Japan's SLIM moon lander  in real timeGrafana in space: Monitoring Japan's SLIM moon lander  in real time
Grafana in space: Monitoring Japan's SLIM moon lander in real time
 

Design and analysis of Algorithms - Lecture 15.ppt

  • 1. Design and Analysis of Algorithms Lecture # 15 0/1 Knapsack Problem
  • 2. 0/1 Knapsack Problem • A thief goes into a jewelry store to steal jewelry items. He has a knapsack (a bag) that he would like to fill up. • The bag has a limit on the total weight of the objects placed in it. If the total weight exceeds the limit, the bag would tear open. • The value of the jewelry items varies for cheap to expensive. • The thief’s goal is to put items in the bag such that the value of the items is maximized and the weight of the items does not exceed the weight limit of the bag. • Another limitation is that an item can either be put in the bag or not - fractional items are not allowed.
  • 3. 0/1 Knapsack Problem • The problem is: what jewelry should the thief choose that satisfy the constraints? • Formally, the problem can be stated as follows: Given a knapsack with maximum capacity W, and a set S consisting of n items. • Each item i has some weight wi and value value vi (all wi , vi and W are integer values). • How to pack the knapsack to achieve maximum total value of packed items?
  • 4. 0/1 Knapsack Problem For example, consider the following scenario:
  • 5. 0/1 Knapsack Problem • The knapsack problem belongs to the domain of optimization problems. Mathematically, the problem is: • The problem is called a “0-1” problem, because each item must be entirely accepted or rejected. How do we solve the problem.
  • 6. 0/1 Knapsack Problem • We could try the brute-force solution: -Since there are n items, there are 2n possible combinations of the items (an item either chosen or not). -We go through all combinations and find the one with the most total value and with total weight less or equal to W. • Clearly, the running time of such a brute-force algorithm will be O(2n). Can we do better? The answer is “yes”, with an algorithm based on dynamic programming.
  • 7. 0/1 Knapsack Problem-DP Approach • For each i ≤ n and each w ≤ W, solve the knapsack problem for the first i objects when the capacity is w. • Why will this work? Because solutions to larger sub problems can be built up easily from solutions to smaller ones. We construct a matrix V [0 . . . n, 0 . . . W]. • For 1 ≤ i ≤ n, and 0 ≤ j ≤ W, V[i, j] will store the maximum value of any set of objects {1, 2, . . . , i} that can fit into a knapsack of weight j. • V[n, W] will contain the maximum value of all n objects that can fit into the entire knapsack of weight W. • To compute entries of V we will imply an inductive approach. As a basis, V[0, j] = 0 for 0 ≤ j ≤ W
  • 8. 0/1 Knapsack Problem • since if we have no items then we have no value. We consider two cases: Leave object i: If we choose to not take object i, then the optimal value will come about by considering how to fill a knapsack of size j with the remaining objects {1, 2, . . . , i - 1}. This is just V[i - 1, j]. Take object i: If we take object i, then we gain a value of vi. But we use up wi of our capacity. With the remaining j - wi capacity in the knapsack, we can fill it in the best possible way with objects {1, 2, . . . , i - 1}. This is vi + V[i - 1, j - wi]. This is only possible if wi ≤ j.
  • 9. 0/1 Knapsack Problem •This leads to the following recursive formulation: A naive evaluation of this recursive definition is exponential. So, as usual, we avoid re-computation by making a table . •
  • 10. 0/1 Knapsack Problem • Example: The maximum weight the knapsack can hold is W is 11. There are five items to choose from. Their weights and values are presented in the following table: • The [i, j] entry here will be V[i, j], the best value obtainable using the first i rows of items if the maximum capacity were j. We begin by initializating and first row.
  • 11. 0/1 Knapsack Problem • Recall that we take V[i, j] to be 0 if either i or j is ≤ 0. We then proceed to fill in top-down, left-to-right always using
  • 12. 0/1 Knapsack Problem • V[i, j] = max{V[i - 1, j], vi + V[i - 1, j - wi] }
  • 15. 0/1 Knapsack Problem • Finally, we have
  • 16. 0/1 Knapsack Problem-DP Algorithm The time complexity is clearly O(n · W). It must be cautioned that as n and W get large, both time and space complexity become significant.
  • 17. 0/1 Knapsack Problem-DP Algorithm Constructing the Optimal Solution The algorithm for computing V[i, j] does not keep record of which subset of items gives the optimal solution. To compute the actual subset, we can add an auxiliary boolean array keep[i, j] which is 1 if we decide to take the ith item and 0 otherwise. We will use all the values keep[i, j] to determine the optimal subset T of items to put in the knapsack as follows:
  • 19. 0/1 Knapsack Problem • Here is the keep matrix for the example problem. • When the item selection algorithm is applied, the selected items are 4 and 3. This is indicated by the boxed entries in the table above.