The Knapsack Problem and Memory Functions
• Problem :
Given n items of known weights w1, . . . , wn and values
v1, . . . , vn and a knapsack of capacity W, find the most
valuable subset of the items that fit into the knapsack.
• We assume here that all the weights and the knapsack
capacity are positive integers.
• To design a dynamic programming algorithm, we need
to derive a recurrence relation that expresses a solution
to an instance of the knapsack problem in terms of
solutions to its smaller subinstances
The Knapsack Problem and Memory Functions
Let us consider an instance defined by the first i items, where
1≤ i ≤ n ,
with weights and
values and
knapsack capacity j, 1 ≤ j ≤ W.
Let F(i, j) be the value the most valuable subset of the first i
items that fit into the knapsack of capacity j
• We can divide all the subsets of the first i items that fit
the knapsack of capacity j into two categories: those that
do not include the ith item and those that do.
The Knapsack Problem and Memory Functions
• We observe the following:
1. For the subsets that do not include the ith item, the
value of most valuable subset is, F(i − 1, j).
2. For the subsets that do include the ith item , value of
most valuable subset is : the value of the ith item
plus the value of the most valuable subset of the
first i − 1 items that fits into the knapsack of
capacity
.
The value of such an optimal subset is
The Knapsack Problem and Memory Functions
• Thus, the value of the most valuable subset of the
first i items is the maximum of F(i − 1, j)
and
• Another observation is that if the ith item does not fit
into the knapsack, then the value of the most valuable
subset is equal to the value of the most valuable subset
selected from the first i − 1 items.
• These observations lead to the following recurrence:
The Knapsack Problem and Memory Functions
• Our goal is to find F(n, W) that fit into the knapsack
of capacity W, and an optimal subset itself.
• We use the following table for solving the knapsack
problem by dynamic programming
The Knapsack Problem and Memory Functions
Solution
The Knapsack Problem and Memory Functions
Time efficiency
• The time efficiency and space efficiency of this
algorithm are both in
• The time needed to find the composition of an
optimal solution is in O(n).
The Knapsack Problem and Memory Functions
• The classic dynamic programming approach, works
bottom up: it fills a table with solutions to all smaller
subproblems, but each of them is solved only once.
• However, some of these these smaller subproblems
are often not necessary for getting a solution to the
problem given.
• Since this drawback is not present in the top-down
approach, it is natural to try to combine the strengths
of the top-down and bottom-up approaches.
• The method that solves only subproblems that are
necessary and does so only once is based on using
memory functions.
The Knapsack Problem and Memory Functions
• Thus , memory function based method solves a given
problem in the top-down manner but, in addition,
maintains a table used by a bottom-up dynamic
programming algorithm.
• Initially, all the table’s entries are initialized with a
special “null” symbol to indicate that they have not yet
been calculated.
• whenever a new value needs to be calculated, the
method checks the corresponding entry in the table
first
• if this entry is not “null,” it is simply retrieved from the
table; otherwise, it is computed by the recursive call
The Knapsack Problem and Memory Functions
• After initializing the table, the recursive function
needs to be called with i = n (the number of items) and
j = W (the knapsack capacity)
The Knapsack Problem and Memory Functions
Example
The Knapsack Problem and Memory Functions
• An optimal subset by backtracing the computations
starting with the last entry ( ie. F(4, 5))
Since F(4, 5) > F(3, 5), include item 4 , remaining
capacity = 5 − 2 = 3
Since F(3, 3) = F(2, 3), item 3 need not be in an optimal
subset (see the table)
Since F(2, 3) > F(1, 3), include item2,
remaining capacity = 3 − 1 = 2
Since F(1, 2) > F(0, 2), include item 1, remaining
capacity = 2 − 2 = 0
Most valuable subset = {item 1, item 2, item 4}.
How to find the objects that are selected to get the optimal solution
Algorithm: print_optimal_solution(n,m,w,v,x)
//purpose : to obtain the objects selected to get the optimal solution
//i/p: n- Number of objects to be selected
m- Capacity of the knapsack
w- Weights of all the objects
v – the optimal solution for the number of objects selected with remaining capacity
//O/P: x- The information of objects selected and not selected
//Display the optimal Solution
Write ‘The optimal solution is v[n,m]
//Initialization indicates that no object has been selected
for i<-1 to n do
x[i] = 0
end for
i=n;
J=m;
While(i!=0 and j!=0)
if(v[i][j]!=v[i-1][j])
x[i]=1
j=j-w[i]
endif
i=i-1
end while
for i<-1 to n do
if(x[i]=1)
write ‘object selected is ‘, I
endif
endfor

knapsack.pptx

  • 1.
    The Knapsack Problemand Memory Functions • Problem : Given n items of known weights w1, . . . , wn and values v1, . . . , vn and a knapsack of capacity W, find the most valuable subset of the items that fit into the knapsack. • We assume here that all the weights and the knapsack capacity are positive integers. • To design a dynamic programming algorithm, we need to derive a recurrence relation that expresses a solution to an instance of the knapsack problem in terms of solutions to its smaller subinstances
  • 2.
    The Knapsack Problemand Memory Functions Let us consider an instance defined by the first i items, where 1≤ i ≤ n , with weights and values and knapsack capacity j, 1 ≤ j ≤ W. Let F(i, j) be the value the most valuable subset of the first i items that fit into the knapsack of capacity j • We can divide all the subsets of the first i items that fit the knapsack of capacity j into two categories: those that do not include the ith item and those that do.
  • 3.
    The Knapsack Problemand Memory Functions • We observe the following: 1. For the subsets that do not include the ith item, the value of most valuable subset is, F(i − 1, j). 2. For the subsets that do include the ith item , value of most valuable subset is : the value of the ith item plus the value of the most valuable subset of the first i − 1 items that fits into the knapsack of capacity . The value of such an optimal subset is
  • 4.
    The Knapsack Problemand Memory Functions • Thus, the value of the most valuable subset of the first i items is the maximum of F(i − 1, j) and • Another observation is that if the ith item does not fit into the knapsack, then the value of the most valuable subset is equal to the value of the most valuable subset selected from the first i − 1 items. • These observations lead to the following recurrence:
  • 5.
    The Knapsack Problemand Memory Functions • Our goal is to find F(n, W) that fit into the knapsack of capacity W, and an optimal subset itself. • We use the following table for solving the knapsack problem by dynamic programming
  • 6.
    The Knapsack Problemand Memory Functions Solution
  • 8.
    The Knapsack Problemand Memory Functions Time efficiency • The time efficiency and space efficiency of this algorithm are both in • The time needed to find the composition of an optimal solution is in O(n).
  • 9.
    The Knapsack Problemand Memory Functions • The classic dynamic programming approach, works bottom up: it fills a table with solutions to all smaller subproblems, but each of them is solved only once. • However, some of these these smaller subproblems are often not necessary for getting a solution to the problem given. • Since this drawback is not present in the top-down approach, it is natural to try to combine the strengths of the top-down and bottom-up approaches. • The method that solves only subproblems that are necessary and does so only once is based on using memory functions.
  • 10.
    The Knapsack Problemand Memory Functions • Thus , memory function based method solves a given problem in the top-down manner but, in addition, maintains a table used by a bottom-up dynamic programming algorithm. • Initially, all the table’s entries are initialized with a special “null” symbol to indicate that they have not yet been calculated. • whenever a new value needs to be calculated, the method checks the corresponding entry in the table first • if this entry is not “null,” it is simply retrieved from the table; otherwise, it is computed by the recursive call
  • 11.
    The Knapsack Problemand Memory Functions • After initializing the table, the recursive function needs to be called with i = n (the number of items) and j = W (the knapsack capacity)
  • 12.
    The Knapsack Problemand Memory Functions Example
  • 16.
    The Knapsack Problemand Memory Functions • An optimal subset by backtracing the computations starting with the last entry ( ie. F(4, 5)) Since F(4, 5) > F(3, 5), include item 4 , remaining capacity = 5 − 2 = 3 Since F(3, 3) = F(2, 3), item 3 need not be in an optimal subset (see the table) Since F(2, 3) > F(1, 3), include item2, remaining capacity = 3 − 1 = 2 Since F(1, 2) > F(0, 2), include item 1, remaining capacity = 2 − 2 = 0 Most valuable subset = {item 1, item 2, item 4}.
  • 17.
    How to findthe objects that are selected to get the optimal solution Algorithm: print_optimal_solution(n,m,w,v,x) //purpose : to obtain the objects selected to get the optimal solution //i/p: n- Number of objects to be selected m- Capacity of the knapsack w- Weights of all the objects v – the optimal solution for the number of objects selected with remaining capacity //O/P: x- The information of objects selected and not selected //Display the optimal Solution Write ‘The optimal solution is v[n,m] //Initialization indicates that no object has been selected for i<-1 to n do x[i] = 0 end for i=n; J=m; While(i!=0 and j!=0) if(v[i][j]!=v[i-1][j]) x[i]=1 j=j-w[i] endif i=i-1 end while for i<-1 to n do if(x[i]=1) write ‘object selected is ‘, I endif endfor