SlideShare a Scribd company logo
Knapsack Problem Notes

    V.S. Subrahmanian
   University of Maryland



      © 5/7/2002 V.S.       1
Knapsack Problem
• You have a knapsack that has capacity (weight) C.
• You have several items I1,…,In.
• Each item Ij has a weight wj and a benefit bj.
• You want to place a certain number of copies of
  each item Ij in the knapsack so that:
   – The knapsack weight capacity is not exceeded and
   – The total benefit is maximal.


                  © 5/7/2002 V.S.           2
Example
Item     Weight           Benefit

A        2                60

B        3                75

C        4                90


           Capacity = 5
       © 5/7/2002 V.S.          3
Key question
• Suppose f(w) represents the maximal
  possible benefit of a knapsack with weight
  w.
• We want to find (in the example) f(5).
• Is there anything we can say about f(w) for
  arbitrary w?


               © 5/7/2002 V.S.     4
Key observation
• To fill a knapsack with items of weight w, we
  must have added items into the knapsack in some
  order.
• Suppose the last such item was Ij with weight wi
  and benefit bi.
• Consider the knapsack with weight (w- wi).
  Clearly, we chose to add Ij to this knapsack
  because of all items with weight wi or less, Ij had
  the max benefit bi.
                  © 5/7/2002 V.S.          5
Key observation
• Thus, f(w) = MAX { bj + f(w-wj) | Ij is an
  item}.
• This gives rise to an immediate recursive
  algorithm to determine how to fill a
  knapsack.




               © 5/7/2002 V.S.      6
Example
Item   Weight            Benefit


A      2                 60

B      3                 75

C      4                 90


       © 5/7/2002 V.S.        7
f(0), f(1)
• f(0) = 0. Why? The knapsack with capacity
  0 can have nothing in it.
• f(1) = 0. There is no item with weight 1.




              © 5/7/2002 V.S.    8
f(2)
• f(2) = 60. There is only one item with
  weight 60.
• Choose A.




               © 5/7/2002 V.S.     9
f(3)
• f(3) = MAX { bj + f(w-wj) | Ij is an item}.
= MAX { 60+f(3-2), 75 + f(3-3)}
= MAX { 60 + 0, 75 + 0 }
= 75.
Choose B.



                © 5/7/2002 V.S.      10
f(4)
• f(4) = MAX { bj + f(w-wj) | Ij is an item}.
= MAX { 60 + f(4-2), 75 + f(4-3), 90+f(4-4)}
= MAX { 60 + 60, 75 + f(1), 90 + f(0)}
= MAX { 120, 75, 90}
=120.
Choose A.

                © 5/7/2002 V.S.      11
f(5)
• f(5) = MAX { bj + f(w-wj) | Ij is an item}.
= MAX { 60 + f(5-2), 75 + f(5-3), 90+f(5-4)}
= MAX { 60 + f(3), 75 + f(2), 90 + f(1)}
= MAX { 60 + 75, 75 + 60, 90+0}
= 135.
Choose A or B.

                © 5/7/2002 V.S.      12
Result
• Optimal knapsack weight is 135.
• Two possible optimal solutions:
  – Choose A during computation of f(5). Choose
    B in computation of f(3).
  – Choose B during computation of f(5). Choose
    A in computation of f(2).
• Both solutions coincide. Take A and B.

               © 5/7/2002 V.S.       13
Another example
• Knapsack of capacity 50.
• 3 items
  – Item 1 has weight 10, benefit 60
  – Item 2 has weight 20,benefit 100
  – Item 3 has weight 30, benefit 120.




                © 5/7/2002 V.S.          14
f(0),..,f(9)
• All have value 0.




               © 5/7/2002 V.S.   15
f(10),..,f(19)
• All have value 10.
• Choose Item 1.




               © 5/7/2002 V.S.   16
f(20),..,f(29)
• F(20) = MAX { 60 + f(10), 100 + f(0) }
= MAX { 60+60, 100+0}
=120.
Choose Item 1.




              © 5/7/2002 V.S.     17
f(30),…,f(39)
• f(30) = MAX { 60 + f(20), 100 + f(10), 120
  + f(0) }
= MAX { 60 + 120, 100+60, 120+0}
= 180
Choose item 1.



              © 5/7/2002 V.S.     18
f(40),…,f(49)
• F(40) = MAX { 60 + f(30), 100 + f(20), 120
  + f(10)}
= MAX { 60 + 180, 100+120, 120 + 60}
= 240.
Choose item 1.



              © 5/7/2002 V.S.     19
f(50)
• f(50) = MAX { 60 + f(40), 100 + f(30), 120
  + f(20) }
= MAX { 60 + 240, 100+180, 120 + 120}
= 300.
Choose item 1.



              © 5/7/2002 V.S.     20
Knapsack Problem Variants
• 0/1 Knapsack problem: Similar to the
  knapsack problem except that for each item,
  only 1 copy is available (not an unlimited
  number as we have been assuming so far).
• Fractional knapsack problem: You can take
  a fractional number of items. Has the same
  constraint as 0/1 knapsack. Can solve using
  a greedy algorithm.
               © 5/7/2002 V.S.     21
0/1 Knapsack
• f[j,w] = best solution having weight exactly
  w after considering j elements
• If j >= C: f[j,w] = f[j-1,w]
• Otherwise: f[j,w] =
                         Best solution after adding k-1 items
• MAXremaining items {
   – f[j-1,w],            Replace an item with the k’th item

   – f[j-1,w-wj]+bj }

                  © 5/7/2002 V.S.          22
0/1 Knapsack Algorithm
For w = 0 to C do f[w]=0; (* initialize *)
For j=1 to n do
  for w=C downto wj do
     if f[w-wj] + bj > f[w] then
           f[w] = f[w-wj] + bj


O(n.C) algorithm
               © 5/7/2002 V.S.       23
Fractional knapsack
• Much easier
• For item Ij, let rj = bj/wj. This gives you the
  benefit per measure of weight.
• Sort the items in descending order of rj
• Pack the knapsack by putting as many of
  each item as you can walking down the
  sorted list.
                 © 5/7/2002 V.S.       24

More Related Content

What's hot

Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
Jenny Galino
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
padmeshagrekar
 
Huffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm Presentation
Akm Monir
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
i i
 
Knapsack
KnapsackKnapsack
Knapsack
Karthik Chetla
 
Graph algorithms
Graph algorithmsGraph algorithms
Graph algorithms
University of Haripur
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
Adnan Malak
 
Dss digital signature standard and dsa algorithm
Dss  digital signature standard and dsa algorithmDss  digital signature standard and dsa algorithm
Dss digital signature standard and dsa algorithm
Abhishek Kesharwani
 
Rsa and diffie hellman algorithms
Rsa and diffie hellman algorithmsRsa and diffie hellman algorithms
Rsa and diffie hellman algorithms
daxesh chauhan
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
harsh kothari
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
rowntu
 
2-Approximation Vertex Cover
2-Approximation Vertex Cover2-Approximation Vertex Cover
2-Approximation Vertex Cover
Kowshik Roy
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
HoneyChintal
 
Huffman coding
Huffman coding Huffman coding
Huffman coding
Nazmul Hyder
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
Madhu Bala
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
Amit Kumar Rathi
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
Prof. Dr. K. Adisesha
 
Backtracking
BacktrackingBacktracking
Backtracking
subhradeep mitra
 
Np completeness
Np completenessNp completeness
Np completeness
Rajendran
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
Sikder Tahsin Al-Amin
 

What's hot (20)

Knapsack Problem
Knapsack ProblemKnapsack Problem
Knapsack Problem
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
Huffman Coding Algorithm Presentation
Huffman Coding Algorithm PresentationHuffman Coding Algorithm Presentation
Huffman Coding Algorithm Presentation
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 
Knapsack
KnapsackKnapsack
Knapsack
 
Graph algorithms
Graph algorithmsGraph algorithms
Graph algorithms
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
 
Dss digital signature standard and dsa algorithm
Dss  digital signature standard and dsa algorithmDss  digital signature standard and dsa algorithm
Dss digital signature standard and dsa algorithm
 
Rsa and diffie hellman algorithms
Rsa and diffie hellman algorithmsRsa and diffie hellman algorithms
Rsa and diffie hellman algorithms
 
Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
Knapsack problem dynamicprogramming
Knapsack problem dynamicprogrammingKnapsack problem dynamicprogramming
Knapsack problem dynamicprogramming
 
2-Approximation Vertex Cover
2-Approximation Vertex Cover2-Approximation Vertex Cover
2-Approximation Vertex Cover
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
Huffman coding
Huffman coding Huffman coding
Huffman coding
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Shortest path algorithms
Shortest path algorithmsShortest path algorithms
Shortest path algorithms
 
Analysis and Design of Algorithms notes
Analysis and Design of Algorithms  notesAnalysis and Design of Algorithms  notes
Analysis and Design of Algorithms notes
 
Backtracking
BacktrackingBacktracking
Backtracking
 
Np completeness
Np completenessNp completeness
Np completeness
 
Graphs - Discrete Math
Graphs - Discrete MathGraphs - Discrete Math
Graphs - Discrete Math
 

More from Hemant Gautam

Make a fool geekssay.com sharing
Make a fool geekssay.com sharingMake a fool geekssay.com sharing
Make a fool geekssay.com sharing
Hemant Gautam
 
1 men die-younger geekssay.com sharing
1 men die-younger geekssay.com sharing1 men die-younger geekssay.com sharing
1 men die-younger geekssay.com sharing
Hemant Gautam
 
Shopping cart project geekssay.com
Shopping cart project geekssay.comShopping cart project geekssay.com
Shopping cart project geekssay.com
Hemant Gautam
 
Segmentation geekssay.com
Segmentation  geekssay.comSegmentation  geekssay.com
Segmentation geekssay.com
Hemant Gautam
 
Atm traffic management geekssay.com
Atm traffic management  geekssay.comAtm traffic management  geekssay.com
Atm traffic management geekssay.com
Hemant Gautam
 
Disk scheduling geekssay.com
Disk scheduling geekssay.comDisk scheduling geekssay.com
Disk scheduling geekssay.com
Hemant Gautam
 
Kruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.comKruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.com
Hemant Gautam
 
College Monitoring system BY: Geekssay.com
College Monitoring system BY: Geekssay.comCollege Monitoring system BY: Geekssay.com
College Monitoring system BY: Geekssay.com
Hemant Gautam
 

More from Hemant Gautam (8)

Make a fool geekssay.com sharing
Make a fool geekssay.com sharingMake a fool geekssay.com sharing
Make a fool geekssay.com sharing
 
1 men die-younger geekssay.com sharing
1 men die-younger geekssay.com sharing1 men die-younger geekssay.com sharing
1 men die-younger geekssay.com sharing
 
Shopping cart project geekssay.com
Shopping cart project geekssay.comShopping cart project geekssay.com
Shopping cart project geekssay.com
 
Segmentation geekssay.com
Segmentation  geekssay.comSegmentation  geekssay.com
Segmentation geekssay.com
 
Atm traffic management geekssay.com
Atm traffic management  geekssay.comAtm traffic management  geekssay.com
Atm traffic management geekssay.com
 
Disk scheduling geekssay.com
Disk scheduling geekssay.comDisk scheduling geekssay.com
Disk scheduling geekssay.com
 
Kruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.comKruskals prims shared by: geekssay.com
Kruskals prims shared by: geekssay.com
 
College Monitoring system BY: Geekssay.com
College Monitoring system BY: Geekssay.comCollege Monitoring system BY: Geekssay.com
College Monitoring system BY: Geekssay.com
 

Knapsack Algorithm www.geekssay.com

  • 1. Knapsack Problem Notes V.S. Subrahmanian University of Maryland © 5/7/2002 V.S. 1
  • 2. Knapsack Problem • You have a knapsack that has capacity (weight) C. • You have several items I1,…,In. • Each item Ij has a weight wj and a benefit bj. • You want to place a certain number of copies of each item Ij in the knapsack so that: – The knapsack weight capacity is not exceeded and – The total benefit is maximal. © 5/7/2002 V.S. 2
  • 3. Example Item Weight Benefit A 2 60 B 3 75 C 4 90 Capacity = 5 © 5/7/2002 V.S. 3
  • 4. Key question • Suppose f(w) represents the maximal possible benefit of a knapsack with weight w. • We want to find (in the example) f(5). • Is there anything we can say about f(w) for arbitrary w? © 5/7/2002 V.S. 4
  • 5. Key observation • To fill a knapsack with items of weight w, we must have added items into the knapsack in some order. • Suppose the last such item was Ij with weight wi and benefit bi. • Consider the knapsack with weight (w- wi). Clearly, we chose to add Ij to this knapsack because of all items with weight wi or less, Ij had the max benefit bi. © 5/7/2002 V.S. 5
  • 6. Key observation • Thus, f(w) = MAX { bj + f(w-wj) | Ij is an item}. • This gives rise to an immediate recursive algorithm to determine how to fill a knapsack. © 5/7/2002 V.S. 6
  • 7. Example Item Weight Benefit A 2 60 B 3 75 C 4 90 © 5/7/2002 V.S. 7
  • 8. f(0), f(1) • f(0) = 0. Why? The knapsack with capacity 0 can have nothing in it. • f(1) = 0. There is no item with weight 1. © 5/7/2002 V.S. 8
  • 9. f(2) • f(2) = 60. There is only one item with weight 60. • Choose A. © 5/7/2002 V.S. 9
  • 10. f(3) • f(3) = MAX { bj + f(w-wj) | Ij is an item}. = MAX { 60+f(3-2), 75 + f(3-3)} = MAX { 60 + 0, 75 + 0 } = 75. Choose B. © 5/7/2002 V.S. 10
  • 11. f(4) • f(4) = MAX { bj + f(w-wj) | Ij is an item}. = MAX { 60 + f(4-2), 75 + f(4-3), 90+f(4-4)} = MAX { 60 + 60, 75 + f(1), 90 + f(0)} = MAX { 120, 75, 90} =120. Choose A. © 5/7/2002 V.S. 11
  • 12. f(5) • f(5) = MAX { bj + f(w-wj) | Ij is an item}. = MAX { 60 + f(5-2), 75 + f(5-3), 90+f(5-4)} = MAX { 60 + f(3), 75 + f(2), 90 + f(1)} = MAX { 60 + 75, 75 + 60, 90+0} = 135. Choose A or B. © 5/7/2002 V.S. 12
  • 13. Result • Optimal knapsack weight is 135. • Two possible optimal solutions: – Choose A during computation of f(5). Choose B in computation of f(3). – Choose B during computation of f(5). Choose A in computation of f(2). • Both solutions coincide. Take A and B. © 5/7/2002 V.S. 13
  • 14. Another example • Knapsack of capacity 50. • 3 items – Item 1 has weight 10, benefit 60 – Item 2 has weight 20,benefit 100 – Item 3 has weight 30, benefit 120. © 5/7/2002 V.S. 14
  • 15. f(0),..,f(9) • All have value 0. © 5/7/2002 V.S. 15
  • 16. f(10),..,f(19) • All have value 10. • Choose Item 1. © 5/7/2002 V.S. 16
  • 17. f(20),..,f(29) • F(20) = MAX { 60 + f(10), 100 + f(0) } = MAX { 60+60, 100+0} =120. Choose Item 1. © 5/7/2002 V.S. 17
  • 18. f(30),…,f(39) • f(30) = MAX { 60 + f(20), 100 + f(10), 120 + f(0) } = MAX { 60 + 120, 100+60, 120+0} = 180 Choose item 1. © 5/7/2002 V.S. 18
  • 19. f(40),…,f(49) • F(40) = MAX { 60 + f(30), 100 + f(20), 120 + f(10)} = MAX { 60 + 180, 100+120, 120 + 60} = 240. Choose item 1. © 5/7/2002 V.S. 19
  • 20. f(50) • f(50) = MAX { 60 + f(40), 100 + f(30), 120 + f(20) } = MAX { 60 + 240, 100+180, 120 + 120} = 300. Choose item 1. © 5/7/2002 V.S. 20
  • 21. Knapsack Problem Variants • 0/1 Knapsack problem: Similar to the knapsack problem except that for each item, only 1 copy is available (not an unlimited number as we have been assuming so far). • Fractional knapsack problem: You can take a fractional number of items. Has the same constraint as 0/1 knapsack. Can solve using a greedy algorithm. © 5/7/2002 V.S. 21
  • 22. 0/1 Knapsack • f[j,w] = best solution having weight exactly w after considering j elements • If j >= C: f[j,w] = f[j-1,w] • Otherwise: f[j,w] = Best solution after adding k-1 items • MAXremaining items { – f[j-1,w], Replace an item with the k’th item – f[j-1,w-wj]+bj } © 5/7/2002 V.S. 22
  • 23. 0/1 Knapsack Algorithm For w = 0 to C do f[w]=0; (* initialize *) For j=1 to n do for w=C downto wj do if f[w-wj] + bj > f[w] then f[w] = f[w-wj] + bj O(n.C) algorithm © 5/7/2002 V.S. 23
  • 24. Fractional knapsack • Much easier • For item Ij, let rj = bj/wj. This gives you the benefit per measure of weight. • Sort the items in descending order of rj • Pack the knapsack by putting as many of each item as you can walking down the sorted list. © 5/7/2002 V.S. 24