2. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
2
Learning Outcome
By the end of this lesson, students will be
able to:
Understand dynamic programming
approach for solving problems.
Understand the effectiveness of dynamic
Algorithms for solving problems.
Implement dynamic Algorithms for
solving the Knapsack problem.
3. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
3
The Knapsack Problem
Set of n items, where each item i is specified
by a size si and a value vi.
We are also given a size bound S (the size of
our knapsack).
The goal is to find the sub set of items of
maximum total value such that sum of their
sizes is at most S (they all fit into the
knapsack).
5. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
5
Imagine you have an assignment with different
parts labeled A through G.
Each part has a “value” (in points) and a “size”
(time in hours to complete).
For example, say the values and times for our
assignment are:
6. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
6
You have a total of 15 hours:
Which parts should you do?
If there was partial credit that was proportional to
the amount of work done (e.g., one hour spent on
problem C earns you 2.5 points) then the best
approach is to work on problems in order of
points/ hour (a greedy strategy).
But, what if there is no partial credit? In that case,
which parts should you do, and what is the best
total value possible?
7. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
7
We can solve the knapsack problem in exponential
time by trying all possible subsets.
With Dynamic Programming, we can reduce this
to time O(nS).
Using dynamic programming to solve this integer
programming problem is now straightforward.
We use an (n+1) x (C+1) table to evaluate the
values of V[i,j].
11. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
11
Example 2:
Weights : 2, 3, 3, 4, 6
Values : 1, 2, 5, 9, 4
Knapsack Capacity (W) = 10
From the above input, the capacity of the
knapsack is 10 kgs and there are 5 items to choose
from.
A very simple idea is to evaluate each item and
find out if it should be included in the knapsack or
not in order to maximize the value.
14. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
14
static int knapSack(int W, int wt[], int val[], int n){
if (n == 0 || W == 0)
return 0;
if (wt[n-1] > W)
return knapSack(W, wt, val, n-1);
else return max( val[n-1] + knapSack(W-wt[n-1], wt, val, n-
1),
knapSack(W, wt, val, n-1));
}
15. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
15
public static void main(String args[]){
int val[] = new int[]{7,9,5,12,14,6,12};
int wt[] = new int[]{3,4,2,6,7,3,5};
int W = 15;
int n = val.length;
System.out.println(knapSack(W, wt, val, n));
}
}
16. Dynamic Programming-III
Algorithm
Analysis
&
Design June 2020
16
Summary
In this presentation, we studied the
effectiveness of dynamic Algorithms for
solving problems. We implemented dynamic
Algorithms for solving the Knapsack problem.
In the Knapsack problem, we have a set of n
items, & we are also given a size bound S. The
goal is to find the sub set of items of
maximum total value such that sum of their
sizes is at most S.