This slide contains 0-1 knapsack problem using naive recursive approach and using top-down dynamic programming approach. Here we have used memorization to optimize the recursive approach.
Introduction to ArtificiaI Intelligence in Higher Education
Maximum Value Knapsack Problem Solved with Dynamic Programming
1. Presented By :- Abhishek Kumar Singh
2015UGCS018
B-Tech ( 6th Semester)
NIT Jamshedpur
0-1 Knapsack problem
2. Overview
• 0-1 knapsack , its types and recursive
implementation
• Optimization of recursive solution using
Memorization
3. 0-1 Knapsack problem
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?
4. Knapsack problem
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
5. 0-1 Knapsack problem: Naïve
recursive approach
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)
6. Approach
To consider all subsets of items, there
can be two cases for every item:
(1) the item is included in the optimal
subset,
(2) not included in the optimal set.
7. Approach
Therefore, the maximum value that can be
obtained from n items is max of following two
values.
1) Maximum value obtained by n-1 items and
W weight (excluding nth item).
2) Value of nth item plus maximum value
obtained by n-1 items and W minus weight of
the nth item (including nth item).
If weight of nth item is greater than W, then
the nth item cannot be included and case 1 is
the only possibility.
8. Following is knapsack function
int knapSack(int W, int wt[], int val[], int n)
{
// Base Case
if (n == 0 || W == 0)
return 0;
// If weight of the nth item is more than Knapsack capacity W, then
// this item cannot be included in the optimal solution
if (wt[n-1] > W)
return knapSack(W, wt, val, n-1);
// Return the maximum of two cases:
// (1) nth item included
// (2) not included
else return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-1),
knapSack(W, wt, val, n-1) );
}
9. STATE SPACE TREE
In the following recursion tree, K() refers to knapSack().
The two parameters indicated in the following recursion
tree are n and W. The recursion tree is for following sample
inputs. wt[] = {1, 1, 1}, W = 2, val[] = {10, 20, 30}
10. Problem in the previous
approach
It should be noted that the above
function computes the same
subproblems again and again. See the
above recursion tree, K(1, 1) is being
evaluated twice. Time complexity of
this naive recursive solution is
exponential (2^n).
To overcome this problem we can use
the dynamic
programming technique, which
basically involved storing the solutions
11. Recursive Top-Down Dynamic programming
approach memorization
Top-down dynamic programming
means that we’ll use same recursive
algorithm to solve the problem, but instead
of simply returning the computed values
from our function we’ll first store it in an
auxiliary table. Then every time we call the
recursion we first check the table to see if
the solution was computed already. If it
was we use it, else we compute and store
it for future use.
12. int matrix[100][100] = {0};
int knapsack( int index, int size, int weights[], int values[] )
{
int take,dontTake;
take = dontTake = 0;
if (matrix[index][size]!=0) //checking whether the subproblem is
return matrix[index][size]; // already calculated or not
if (index==0) // If the given subproblem is at leave node
{ if (weights[0]<=size)
{ matrix[index][size] = values[0];
return values[0]; }
else
{ matrix[index][size] = 0;
return 0; }
}
if (weights[index]<=size)
take = values[index] + knapsack(index-1, size-weights[index], weights,
values);
dontTake = knapsack(index-1, size, weights, values);
matrix[index][size] = max (take, dontTake);
return matrix[index][size];
13. Time Complexity
The time complexity of the previous algorithm is
O(w*n) where ‘w’ is the maximum weight of the
knapsack and ‘n’ is the no. of elements .
In the previous algorithm what we are using is
called Memorization.
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
14. Advantages & Disadvantages
Advantage:
It is used to find all the existing solution if
there exist for any problem.
It is used for the problems where path
through which the solution is achieved is
important rather than the solution itself.
classic example of this is N-Queen
problem or Knight problem.
Disadvantage:
Multiple function calls are expensive
Requires large amount of space as the
each function state needs to be stored on