SlideShare a Scribd company logo
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

PPTX
01 Knapsack using Dynamic Programming
PPTX
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
PPT
Knapsack problem and Memory Function
PPT
Knapsack problem
PPTX
Knapsack problem
PPT
DynProg_Knapsack.ppt
PDF
A Survey- Knapsack Problem Using Dynamic Programming
PPTX
Fractional Knapsack Problem
01 Knapsack using Dynamic Programming
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
Knapsack problem and Memory Function
Knapsack problem
Knapsack problem
DynProg_Knapsack.ppt
A Survey- Knapsack Problem Using Dynamic Programming
Fractional Knapsack Problem

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

PPTX
knapsack problem
PPTX
Dynamic Programming-Knapsack Problem
PDF
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
PPTX
Knapsack
PPT
AOA ppt.ppt
PPTX
Knapsack Dynamic
PPTX
Greedy Algorithm - Knapsack Problem
PPT
Knapsack Algorithm www.geekssay.com
PPT
4 greedy methodnew
PPTX
Module 3_Greedy Technique_2021 Scheme.pptx
PPTX
Dynamic programming (dp) in Algorithm
PPTX
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
PPTX
Daa:Dynamic Programing
PDF
PPTX
THE GREEDY METHOD notes related to education.pptx
PPT
Knapsack Problem Analysis of Algorithm.ppt
PDF
306_0_1_Knapsack .pdf
PPTX
knapsack.pptx
PPTX
Lecture 23(OBST,knapsack) from DAA for students of CSE
PPT
0-1 knapsack.ppt
knapsack problem
Dynamic Programming-Knapsack Problem
Comparative analysis-of-dynamic-and-greedy-approaches-for-dynamic-programming
Knapsack
AOA ppt.ppt
Knapsack Dynamic
Greedy Algorithm - Knapsack Problem
Knapsack Algorithm www.geekssay.com
4 greedy methodnew
Module 3_Greedy Technique_2021 Scheme.pptx
Dynamic programming (dp) in Algorithm
0/1 DYNAMIC PROGRAMMING KNAPSACK PROBLEM
Daa:Dynamic Programing
THE GREEDY METHOD notes related to education.pptx
Knapsack Problem Analysis of Algorithm.ppt
306_0_1_Knapsack .pdf
knapsack.pptx
Lecture 23(OBST,knapsack) from DAA for students of CSE
0-1 knapsack.ppt
Ad

Recently uploaded (20)

PDF
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
PDF
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
PDF
The scientific heritage No 166 (166) (2025)
PPTX
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
PPTX
7. General Toxicologyfor clinical phrmacy.pptx
PPTX
Classification Systems_TAXONOMY_SCIENCE8.pptx
PDF
An interstellar mission to test astrophysical black holes
PPTX
Cell Membrane: Structure, Composition & Functions
PPTX
Taita Taveta Laboratory Technician Workshop Presentation.pptx
PPT
POSITIONING IN OPERATION THEATRE ROOM.ppt
PPTX
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
PPTX
ECG_Course_Presentation د.محمد صقران ppt
PPTX
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
PDF
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
PDF
Sciences of Europe No 170 (2025)
PPTX
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
PPTX
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
PPTX
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
PDF
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
PPTX
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
SEHH2274 Organic Chemistry Notes 1 Structure and Bonding.pdf
CAPERS-LRD-z9:AGas-enshroudedLittleRedDotHostingaBroad-lineActive GalacticNuc...
The scientific heritage No 166 (166) (2025)
G5Q1W8 PPT SCIENCE.pptx 2025-2026 GRADE 5
7. General Toxicologyfor clinical phrmacy.pptx
Classification Systems_TAXONOMY_SCIENCE8.pptx
An interstellar mission to test astrophysical black holes
Cell Membrane: Structure, Composition & Functions
Taita Taveta Laboratory Technician Workshop Presentation.pptx
POSITIONING IN OPERATION THEATRE ROOM.ppt
Protein & Amino Acid Structures Levels of protein structure (primary, seconda...
ECG_Course_Presentation د.محمد صقران ppt
DRUG THERAPY FOR SHOCK gjjjgfhhhhh.pptx.
Mastering Bioreactors and Media Sterilization: A Complete Guide to Sterile Fe...
Sciences of Europe No 170 (2025)
Vitamins & Minerals: Complete Guide to Functions, Food Sources, Deficiency Si...
ANEMIA WITH LEUKOPENIA MDS 07_25.pptx htggtftgt fredrctvg
ognitive-behavioral therapy, mindfulness-based approaches, coping skills trai...
Unveiling a 36 billion solar mass black hole at the centre of the Cosmic Hors...
cpcsea ppt.pptxssssssssssssssjjdjdndndddd
Ad

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.