SlideShare a Scribd company logo
1 of 36
KNAPSACK PROBLEM AND MEMORY
FUNCTION
PREPARED BY
M. Baranitharan
Kings College of Engineering
Given some items, pack the knapsack to get
the maximum total value. Each item has some
weight and some value. Total weight that we can
carry is no more than some fixed number W.
So we must consider weights of items as well as
their values.
Item # Weight Value
1 1 8
2 3 6
3 5 5
There are two versions of the problem:
1. “0-1 knapsack problem”
 Items are indivisible; you either take an item or not.
Some special instances can be solved with dynamic
programming
1. “Fractional knapsack problem”
 Items are divisible: you can take any fraction of an
item
 Given a knapsack with maximum capacity W, and
a set S consisting of n items
 Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?
 Problem, in other words, is to find
∑∑ ∈∈
≤
Ti
i
Ti
i Wwb subject tomax
The problem is called a “0-1” problem,
because each item must be entirely
accepted or rejected.
Let’s first solve this problem with a
straightforward algorithm
 Since there are n items, there are 2n
possible
combinations of items.
 We go through all combinations and find the
one with maximum value and with total weight
less or equal to W
 Running time will be O(2n
)
 We can do better with an algorithm based on
dynamic programming
 We need to carefully identify the subproblems
 Given a knapsack with maximum capacity W, and
a set S consisting of n items
 Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?
 We can do better with an algorithm based on
dynamic programming
 We need to carefully identify the subproblems
Let’s try this:
If items are labeled 1..n, then a subproblem
would be to find an optimal solution for
Sk = {items labeled 1, 2, .. k}
If items are labeled 1..n, then a subproblem
would be to find an optimal solution for Sk =
{items labeled 1, 2, .. k}
 This is a reasonable subproblem definition.
 The question is: can we describe the final
solution (Sn ) in terms of subproblems (Sk)?
 Unfortunately, we can’t do that.
Max weight: W = 20
For S4:
Total weight: 14
Maximum benefit: 20
w1 =2
b1 =3
w2 =4
b2 =5
w3 =5
b3 =8
w4 =3
b4 =4 wi bi
10
85
54
43
32
Weight Benefit
9
Item
#
4
3
2
1
5
S4
S5
w1 =2
b1 =3
w2 =4
b2 =5
w3 =5
b3 =8
w5 =9
b5 =10
For S5:
Total weight: 20
Maximum benefit: 26
Solution for S4 is
not part of the
solution for S !!!
?
 As we have seen, the solution for S4 is not part of
the solution for S5
 So our definition of a subproblem is flawed and we
need another one!
 Given a knapsack with maximum capacity W, and
a set S consisting of n items
 Each item i has some weight wi and benefit value
bi (all wi and W are integer values)
 Problem: How to pack the knapsack to achieve
maximum total value of packed items?
 Let’s add another parameter: w, which will
represent the maximum weight for each subset of
items
 The subproblem then will be to compute V[k,w],
i.e., to find an optimal solution for Sk = {items
labeled 1, 2, .. k} in a knapsack of size w
 The subproblem will then be to compute V[k,w],
i.e., to find an optimal solution for Sk = {items
labeled 1, 2, .. k} in a knapsack of size w
 Assuming knowing V[i, j], where i=0,1, 2, … k-1,
j=0,1,2, …w, how to derive V[k,w]?
It means, that the best subset of Sk that has total
weight w is:
1) the best subset of Sk-1 that has total weight ≤ w, or
2) the best subset of Sk-1 that has total weight ≤ w-wk plus
the item k



+−−−
>−
=
else}],1[],,1[max{
if],1[
],[
kk
k
bwwkVwkV
wwwkV
wkV
Recursive formula for subproblems:
 The best subset of Sk that has the total weight ≤ w,
either contains item k or not.
 First case: wk>w. Item k can’t be part of the solution,
since if it was, the total weight would be > w, which is
unacceptable.
 Second case: wk ≤ w. Then the item k can be in the
solution, and we choose the case with greater value.



+−−−
>−
=
else}],1[],,1[max{
if],1[
],[
kk
k
bwwkVwkV
wwwkV
wkV
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
if wi <= 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
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
for i = 1 to n
for w = 0 to W
< the rest of the code >
What is the running time of this
algorithm?
O(W)
O(W)
Repeat n times
O(n*W)
Remember that the brute-force algorithm
takes O(2n
)
Let’s run our algorithm on the
following data:
n = 4 (# of elements)
W = 5 (max weight)
Elements (weight, benefit):
(2,3), (3,4), (4,5), (5,6)
for w = 0 to W
V[0,w] = 0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
for i = 1 to n
V[i,0] = 0
0
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
if wi <= 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
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
0
i=1
bi=3
wi=2
w=1
w-wi =-1
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
0
0
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=4
w-wi =2
if wi <= 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
3 3
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
300
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=1
bi=3
wi=2
w=5
w-wi =3
if wi <= 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
3 3 3
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=2
w-wi =-1
3 3 3 3
3
if wi <= 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
0
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=2
bi=4
wi=3
w=3
w-wi =0
3 3 3 3
0
if wi <= 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
43
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=3
bi=5
wi=4
w= 1..3
3 3 3 3
0 3 4 4
if wi <= 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
7
3 40
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=4
bi=6
wi=5
w= 1..4
3 3 3 3
0 3 4 4
if wi <= 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
7
3 40
70 3 4 5
5
Items:
1: (2,3)
2: (3,4)
3: (4,5)
4: (5,6)
00
0
0
0
0 0 0 0 000
1
2
3
4 50 1 2 3
4
iW
i=4
bi=6
wi=5
w= 5
w- wi=0
3 3 3 3
0 3 4 4 7
0 3 4
if wi <= 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
5
7
7
0 3 4 5
 This algorithm only finds the max possible value
that can be carried in the knapsack
◦ i.e., the value in V[n,W]
 To know the items that make this maximum value,
an addition to this algorithm is necessary
 All of the information we need is in the table.
 V[n,W] is the maximal value of items that can be
placed in the Knapsack.
 Let i=n and k=W
if V[i,k] ≠ V[i−1,k] then
mark the ith
item as in the knapsack
i = i−1, k = k-wi
else
i = i−1 // Assume the ith
item is not in the knapsack
// Could it be in the optimally packed
knapsack?
 Goal:
◦ Solve only subproblems that are necessary and solve it only once
 Memorization is another way to deal with overlapping subproblems
in dynamic programming
 With memorization, we implement the algorithm recursively:
◦ If we encounter a new subproblem, we compute and store the solution.
◦ If we encounter a subproblem we have seen, we look up the answer
 Most useful when the algorithm is easiest to implement recursively
◦ Especially if we do not need solutions to all subproblems.
for i = 1 to n
for w = 1 to W
V[i,w] = -1
for w = 0 to W
V[0,w] = 0
for i = 1 to n
V[i,0] = 0
MFKnapsack(i, w)
if V[i,w] < 0
if w < wi
value = MFKnapsack(i-1, w)
else
value = max(MFKnapsack(i-1, w),
bi + MFKnapsack(i-1, w-wi))
V[i,w] = value
return V[i,w]
 Dynamic programming is a useful technique of
solving certain kind of problems
 When the solution can be recursively described
in terms of partial solutions, we can store these
partial solutions and re-use them as necessary
(memorization)
 Running time of dynamic programming
algorithm vs. naïve algorithm:
◦ 0-1 Knapsack problem: O(W*n) vs. O(2n
)
Design and Analysis of Algorithms - Chapter 8 36

More Related Content

What's hot

Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problemharsh kothari
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemMadhu Bala
 
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
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approachpadmeshagrekar
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programmingkhush_boo31
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data StructureAnuj Modi
 
knapsack problem
knapsack problemknapsack problem
knapsack problemAdnan Malak
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesFahim Ferdous
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignmentKarthi Keyan
 
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
 
Problem Formulation in Artificial Inteligence Projects
Problem Formulation in Artificial Inteligence ProjectsProblem Formulation in Artificial Inteligence Projects
Problem Formulation in Artificial Inteligence ProjectsDr. C.V. Suresh Babu
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEMi i
 

What's hot (20)

Fractional Knapsack Problem
Fractional Knapsack ProblemFractional Knapsack Problem
Fractional Knapsack Problem
 
8 queens problem using ga
8 queens problem using ga8 queens problem using ga
8 queens problem using ga
 
Greedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack ProblemGreedy Algorithm - Knapsack Problem
Greedy Algorithm - Knapsack Problem
 
Knapsack Problem
Knapsack ProblemKnapsack Problem
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
 
Knapsack problem using greedy approach
Knapsack problem using greedy approachKnapsack problem using greedy approach
Knapsack problem using greedy approach
 
Knapsack problem using dynamic programming
Knapsack problem using dynamic programmingKnapsack problem using dynamic programming
Knapsack problem using dynamic programming
 
B trees in Data Structure
B trees in Data StructureB trees in Data Structure
B trees in Data Structure
 
knapsack problem
knapsack problemknapsack problem
knapsack problem
 
SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS SINGLE-SOURCE SHORTEST PATHS
SINGLE-SOURCE SHORTEST PATHS
 
Branch and bound
Branch and boundBranch and bound
Branch and bound
 
BackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and ExamplesBackTracking Algorithm: Technique and Examples
BackTracking Algorithm: Technique and Examples
 
Np cooks theorem
Np cooks theoremNp cooks theorem
Np cooks theorem
 
Register allocation and assignment
Register allocation and assignmentRegister allocation and assignment
Register allocation and assignment
 
4 greedy methodnew
4 greedy methodnew4 greedy methodnew
4 greedy methodnew
 
Sum of subset problem.pptx
Sum of subset problem.pptxSum of subset problem.pptx
Sum of subset problem.pptx
 
Knapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithmKnapsack problem algorithm, greedy algorithm
Knapsack problem algorithm, greedy algorithm
 
Problem Formulation in Artificial Inteligence Projects
Problem Formulation in Artificial Inteligence ProjectsProblem Formulation in Artificial Inteligence Projects
Problem Formulation in Artificial Inteligence Projects
 
8 queen problem
8 queen problem8 queen problem
8 queen problem
 
0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM0-1 KNAPSACK PROBLEM
0-1 KNAPSACK PROBLEM
 

Similar to Knapsack problem and Memory Function

DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.pptRuchika Sinha
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack DynamicParas Patel
 
lecture 25
lecture 25lecture 25
lecture 25sajinsc
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsackGaurav Dubey
 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptQurbanAli72
 
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 naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...Abhishek Singh
 
Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Amit Kumar Rathi
 
Greedy algo revision 2
Greedy algo revision 2Greedy algo revision 2
Greedy algo revision 2maamir farooq
 
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptxINNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptxAvilosErgelaKram
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDeepakGowda357858
 
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
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandboundhodcsencet
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamicchidabdu
 

Similar to Knapsack problem and Memory Function (20)

DynProg_Knapsack.ppt
DynProg_Knapsack.pptDynProg_Knapsack.ppt
DynProg_Knapsack.ppt
 
Knapsack Dynamic
Knapsack DynamicKnapsack Dynamic
Knapsack Dynamic
 
lecture 25
lecture 25lecture 25
lecture 25
 
Presentation of knapsack
Presentation of knapsackPresentation of knapsack
Presentation of knapsack
 
Design and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.pptDesign and analysis of Algorithms - Lecture 15.ppt
Design and analysis of Algorithms - Lecture 15.ppt
 
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 naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...0 1 knapsack using naive recursive approach and top-down dynamic programming ...
0 1 knapsack using naive recursive approach and top-down dynamic programming ...
 
0-1 knapsack.ppt
0-1 knapsack.ppt0-1 knapsack.ppt
0-1 knapsack.ppt
 
AOA ppt.ppt
AOA ppt.pptAOA ppt.ppt
AOA ppt.ppt
 
Knapsack problem
Knapsack problemKnapsack problem
Knapsack problem
 
Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1Dynamic Programming knapsack 0 1
Dynamic Programming knapsack 0 1
 
Greedy algo revision 2
Greedy algo revision 2Greedy algo revision 2
Greedy algo revision 2
 
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptxINNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
INNER_SPACE_PRODUCT-EUCLIDEAN_PLANE.pptx
 
Dynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse studentsDynamic Programming for 4th sem cse students
Dynamic Programming for 4th sem cse students
 
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
 
Dynamic programming
Dynamic programmingDynamic programming
Dynamic programming
 
Module 3_DAA (2).pptx
Module 3_DAA (2).pptxModule 3_DAA (2).pptx
Module 3_DAA (2).pptx
 
knapsackusingbranchandbound
knapsackusingbranchandboundknapsackusingbranchandbound
knapsackusingbranchandbound
 
Unit 3-Greedy Method
Unit 3-Greedy MethodUnit 3-Greedy Method
Unit 3-Greedy Method
 
Sienna 10 dynamic
Sienna 10 dynamicSienna 10 dynamic
Sienna 10 dynamic
 

More from Barani Tharan

Elliptical curve cryptography
Elliptical curve cryptographyElliptical curve cryptography
Elliptical curve cryptographyBarani Tharan
 
Water pollution parameter
Water pollution parameterWater pollution parameter
Water pollution parameterBarani Tharan
 
Realism in Computer Graphics
Realism in Computer GraphicsRealism in Computer Graphics
Realism in Computer GraphicsBarani Tharan
 
Networking in cloud computing
Networking in cloud computingNetworking in cloud computing
Networking in cloud computingBarani Tharan
 
E book management system
E book management systemE book management system
E book management systemBarani Tharan
 
Energy band theory of solids
Energy band theory of solidsEnergy band theory of solids
Energy band theory of solidsBarani Tharan
 
Course registration system
Course registration systemCourse registration system
Course registration systemBarani Tharan
 
Clipping in Computer Graphics
Clipping in Computer Graphics Clipping in Computer Graphics
Clipping in Computer Graphics Barani Tharan
 
Water indicator Circuit to measure the level of any liquid
Water indicator Circuit to measure the level of any liquidWater indicator Circuit to measure the level of any liquid
Water indicator Circuit to measure the level of any liquidBarani Tharan
 
Cloud computing in medical field
Cloud computing in medical fieldCloud computing in medical field
Cloud computing in medical fieldBarani Tharan
 
Application of fourier transform
Application of fourier transformApplication of fourier transform
Application of fourier transformBarani Tharan
 

More from Barani Tharan (14)

Graph coloring
Graph coloringGraph coloring
Graph coloring
 
Elliptical curve cryptography
Elliptical curve cryptographyElliptical curve cryptography
Elliptical curve cryptography
 
Water pollution parameter
Water pollution parameterWater pollution parameter
Water pollution parameter
 
Realism in Computer Graphics
Realism in Computer GraphicsRealism in Computer Graphics
Realism in Computer Graphics
 
Conjestion control
Conjestion controlConjestion control
Conjestion control
 
Networking in cloud computing
Networking in cloud computingNetworking in cloud computing
Networking in cloud computing
 
E book management system
E book management systemE book management system
E book management system
 
Energy band theory of solids
Energy band theory of solidsEnergy band theory of solids
Energy band theory of solids
 
Course registration system
Course registration systemCourse registration system
Course registration system
 
Clipping in Computer Graphics
Clipping in Computer Graphics Clipping in Computer Graphics
Clipping in Computer Graphics
 
Water indicator Circuit to measure the level of any liquid
Water indicator Circuit to measure the level of any liquidWater indicator Circuit to measure the level of any liquid
Water indicator Circuit to measure the level of any liquid
 
Cloud computing in medical field
Cloud computing in medical fieldCloud computing in medical field
Cloud computing in medical field
 
Application of fourier transform
Application of fourier transformApplication of fourier transform
Application of fourier transform
 
4G technology
4G technology4G technology
4G technology
 

Recently uploaded

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escortsranjana rawat
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLDeelipZope
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2RajaP95
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionDr.Costas Sachpazis
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024hassan khalil
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacingjaychoudhary37
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerAnamika Sarkar
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSCAESB
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsCall Girls in Nagpur High Profile
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...ranjana rawat
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSKurinjimalarL3
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidNikhilNagaraju
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxbritheesh05
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxvipinkmenon1
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile servicerehmti665
 

Recently uploaded (20)

High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Isha Call 7001035870 Meet With Nagpur Escorts
 
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptxExploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
Exploring_Network_Security_with_JA3_by_Rakesh Seal.pptx
 
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Serviceyoung call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
young call girls in Rajiv Chowk🔝 9953056974 🔝 Delhi escort Service
 
Current Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCLCurrent Transformer Drawing and GTP for MSETCL
Current Transformer Drawing and GTP for MSETCL
 
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2HARMONY IN THE HUMAN BEING - Unit-II UHV-2
HARMONY IN THE HUMAN BEING - Unit-II UHV-2
 
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective IntroductionSachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
Sachpazis Costas: Geotechnical Engineering: A student's Perspective Introduction
 
Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024Architect Hassan Khalil Portfolio for 2024
Architect Hassan Khalil Portfolio for 2024
 
microprocessor 8085 and its interfacing
microprocessor 8085  and its interfacingmicroprocessor 8085  and its interfacing
microprocessor 8085 and its interfacing
 
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube ExchangerStudy on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
Study on Air-Water & Water-Water Heat Exchange in a Finned Tube Exchanger
 
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
★ CALL US 9953330565 ( HOT Young Call Girls In Badarpur delhi NCR
 
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
9953056974 Call Girls In South Ex, Escorts (Delhi) NCR.pdf
 
GDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentationGDSC ASEB Gen AI study jams presentation
GDSC ASEB Gen AI study jams presentation
 
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur EscortsHigh Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
High Profile Call Girls Nagpur Meera Call 7001035870 Meet With Nagpur Escorts
 
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
(ANVI) Koregaon Park Call Girls Just Call 7001035870 [ Cash on Delivery ] Pun...
 
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICSAPPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
APPLICATIONS-AC/DC DRIVES-OPERATING CHARACTERISTICS
 
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
🔝9953056974🔝!!-YOUNG call girls in Rajendra Nagar Escort rvice Shot 2000 nigh...
 
main PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfidmain PPT.pptx of girls hostel security using rfid
main PPT.pptx of girls hostel security using rfid
 
Artificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptxArtificial-Intelligence-in-Electronics (K).pptx
Artificial-Intelligence-in-Electronics (K).pptx
 
Introduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptxIntroduction to Microprocesso programming and interfacing.pptx
Introduction to Microprocesso programming and interfacing.pptx
 
Call Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile serviceCall Girls Delhi {Jodhpur} 9711199012 high profile service
Call Girls Delhi {Jodhpur} 9711199012 high profile service
 

Knapsack problem and Memory Function

  • 1. KNAPSACK PROBLEM AND MEMORY FUNCTION PREPARED BY M. Baranitharan Kings College of Engineering
  • 2. Given some items, pack the knapsack to get the maximum total value. Each item has some weight and some value. Total weight that we can carry is no more than some fixed number W. So we must consider weights of items as well as their values. Item # Weight Value 1 1 8 2 3 6 3 5 5
  • 3. There are two versions of the problem: 1. “0-1 knapsack problem”  Items are indivisible; you either take an item or not. Some special instances can be solved with dynamic programming 1. “Fractional knapsack problem”  Items are divisible: you can take any fraction of an item
  • 4.  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight wi and benefit value bi (all wi and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items?
  • 5.  Problem, in other words, is to find ∑∑ ∈∈ ≤ Ti i Ti i Wwb subject tomax The problem is called a “0-1” problem, because each item must be entirely accepted or rejected.
  • 6. Let’s first solve this problem with a straightforward algorithm  Since there are n items, there are 2n possible combinations of items.  We go through all combinations and find the one with maximum value and with total weight less or equal to W  Running time will be O(2n )
  • 7.  We can do better with an algorithm based on dynamic programming  We need to carefully identify the subproblems
  • 8.  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight wi and benefit value bi (all wi and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items?
  • 9.  We can do better with an algorithm based on dynamic programming  We need to carefully identify the subproblems Let’s try this: If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k}
  • 10. If items are labeled 1..n, then a subproblem would be to find an optimal solution for Sk = {items labeled 1, 2, .. k}  This is a reasonable subproblem definition.  The question is: can we describe the final solution (Sn ) in terms of subproblems (Sk)?  Unfortunately, we can’t do that.
  • 11. Max weight: W = 20 For S4: Total weight: 14 Maximum benefit: 20 w1 =2 b1 =3 w2 =4 b2 =5 w3 =5 b3 =8 w4 =3 b4 =4 wi bi 10 85 54 43 32 Weight Benefit 9 Item # 4 3 2 1 5 S4 S5 w1 =2 b1 =3 w2 =4 b2 =5 w3 =5 b3 =8 w5 =9 b5 =10 For S5: Total weight: 20 Maximum benefit: 26 Solution for S4 is not part of the solution for S !!! ?
  • 12.  As we have seen, the solution for S4 is not part of the solution for S5  So our definition of a subproblem is flawed and we need another one!
  • 13.  Given a knapsack with maximum capacity W, and a set S consisting of n items  Each item i has some weight wi and benefit value bi (all wi and W are integer values)  Problem: How to pack the knapsack to achieve maximum total value of packed items?
  • 14.  Let’s add another parameter: w, which will represent the maximum weight for each subset of items  The subproblem then will be to compute V[k,w], i.e., to find an optimal solution for Sk = {items labeled 1, 2, .. k} in a knapsack of size w
  • 15.  The subproblem will then be to compute V[k,w], i.e., to find an optimal solution for Sk = {items labeled 1, 2, .. k} in a knapsack of size w  Assuming knowing V[i, j], where i=0,1, 2, … k-1, j=0,1,2, …w, how to derive V[k,w]?
  • 16. It means, that the best subset of Sk that has total weight w is: 1) the best subset of Sk-1 that has total weight ≤ w, or 2) the best subset of Sk-1 that has total weight ≤ w-wk plus the item k    +−−− >− = else}],1[],,1[max{ if],1[ ],[ kk k bwwkVwkV wwwkV wkV Recursive formula for subproblems:
  • 17.  The best subset of Sk that has the total weight ≤ w, either contains item k or not.  First case: wk>w. Item k can’t be part of the solution, since if it was, the total weight would be > w, which is unacceptable.  Second case: wk ≤ w. Then the item k can be in the solution, and we choose the case with greater value.    +−−− >− = else}],1[],,1[max{ if],1[ ],[ kk k bwwkVwkV wwwkV wkV
  • 18. for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for i = 1 to n for w = 0 to W if wi <= 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
  • 19. for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 for i = 1 to n for w = 0 to W < the rest of the code > What is the running time of this algorithm? O(W) O(W) Repeat n times O(n*W) Remember that the brute-force algorithm takes O(2n )
  • 20. Let’s run our algorithm on the following data: n = 4 (# of elements) W = 5 (max weight) Elements (weight, benefit): (2,3), (3,4), (4,5), (5,6)
  • 21. for w = 0 to W V[0,w] = 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW
  • 22. for i = 1 to n V[i,0] = 0 0 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW
  • 23. if wi <= 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 0 Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 0 i=1 bi=3 wi=2 w=1 w-wi =-1 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW 0 0 0
  • 24. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=4 w-wi =2 if wi <= 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 3 3
  • 25. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 300 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=1 bi=3 wi=2 w=5 w-wi =3 if wi <= 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 3 3 3
  • 26. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=2 w-wi =-1 3 3 3 3 3 if wi <= 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 0
  • 27. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=2 bi=4 wi=3 w=3 w-wi =0 3 3 3 3 0 if wi <= 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 43
  • 28. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=3 bi=5 wi=4 w= 1..3 3 3 3 3 0 3 4 4 if wi <= 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 7 3 40
  • 29. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 bi=6 wi=5 w= 1..4 3 3 3 3 0 3 4 4 if wi <= 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 7 3 40 70 3 4 5 5
  • 30. Items: 1: (2,3) 2: (3,4) 3: (4,5) 4: (5,6) 00 0 0 0 0 0 0 0 000 1 2 3 4 50 1 2 3 4 iW i=4 bi=6 wi=5 w= 5 w- wi=0 3 3 3 3 0 3 4 4 7 0 3 4 if wi <= 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 5 7 7 0 3 4 5
  • 31.  This algorithm only finds the max possible value that can be carried in the knapsack ◦ i.e., the value in V[n,W]  To know the items that make this maximum value, an addition to this algorithm is necessary
  • 32.  All of the information we need is in the table.  V[n,W] is the maximal value of items that can be placed in the Knapsack.  Let i=n and k=W if V[i,k] ≠ V[i−1,k] then mark the ith item as in the knapsack i = i−1, k = k-wi else i = i−1 // Assume the ith item is not in the knapsack // Could it be in the optimally packed knapsack?
  • 33.  Goal: ◦ Solve only subproblems that are necessary and solve it only once  Memorization is another way to deal with overlapping subproblems in dynamic programming  With memorization, we implement the algorithm recursively: ◦ If we encounter a new subproblem, we compute and store the solution. ◦ If we encounter a subproblem we have seen, we look up the answer  Most useful when the algorithm is easiest to implement recursively ◦ Especially if we do not need solutions to all subproblems.
  • 34. for i = 1 to n for w = 1 to W V[i,w] = -1 for w = 0 to W V[0,w] = 0 for i = 1 to n V[i,0] = 0 MFKnapsack(i, w) if V[i,w] < 0 if w < wi value = MFKnapsack(i-1, w) else value = max(MFKnapsack(i-1, w), bi + MFKnapsack(i-1, w-wi)) V[i,w] = value return V[i,w]
  • 35.  Dynamic programming is a useful technique of solving certain kind of problems  When the solution can be recursively described in terms of partial solutions, we can store these partial solutions and re-use them as necessary (memorization)  Running time of dynamic programming algorithm vs. naïve algorithm: ◦ 0-1 Knapsack problem: O(W*n) vs. O(2n )
  • 36. Design and Analysis of Algorithms - Chapter 8 36