Dynamic Programming 1 Compu Comb Knapsack
Dynamic Programming 1 Compu Comb Knapsack
By
Dr. K. V. Arya
ABV-IIITM, Gwalior, India
Summary
• Dynamic Programming -Divide and
conquer with a table
• Application to -:
Computing combinations
Knapsack Problem
Computing Combinations
To choose r things out of n, either
• Choose the first item. Then we choose the
remaining r-1items from the other n-1 items.
Or
• Don’t choose the first item. Then we must
choose the r items from the other n-1 items.
Therefore ,
n n 1 n 1
r
r 1
r
Divide and Conquer
This gives the simple divide and conquer algorithm for
finding number of combinations of n things chosen r at a
time.
function choose( n , r )
If r=0 or n=r then return(1) else
return (choose(n-1,r-1) +choose(n-1,r))
Divide and Conquer
Correctness Proof : - A simple induction of n.
Analysis :- Let T(n) be the worst case running
time of choose (n,r) over all possible values of
r.
Then, c if n 1
T ( n)
2T (n 1) d otherwise
For some constant c , d.
Correctness Contd…
Hence,
T ( n) 2T ( n 1) d
2( 2T ( n 2) d ) d
4T ( n 2) 2d d
4( 2T ( n 3) d ) 2 d
8T ( n 3) 4d 2d d
i 1
2i T (n i ) d 2 j
j 0
n2
2j
2 n 1 T (1) d
j 0
(c d ) 2 n 1 d
Hence, T(n)= Θ(2n)
Example
Repeated computation
A Better Algorithm
Pascal’s triangle. Use a table T[ 0..n,0..r].
n
T[i, j] holds
i
function choose (n, r)
for i := 0 to n-r do T(i , 0) :=1;
for i := 0 to r do T(i , i) :=1;
for j := 1 to r do
for i :=j+1 to n-r + j do
T[ i , j] := T[i-1 ,j-1] + T[i-1 ,j];
return( T[ n ,r ])
Initialization
T 0 r
0
Required Answer
n-r
n
General rule
To fill in T[i ,j], we need T[i-1,j-1] and T[i-1,j] to
be already filled in
j-1 j
i-1
i
Filling in the table
Fill in the columns from left to right fill in each of the
columns from top to bottom.
Identification:
• Devise divide and conquer algorithm
• Analyze- running time is exponential.
• Same sub problems solved many times.
Construction
Take part of divide and conquer algorithm that does
the “conquer” part and replace the recursive calls
with table lookups.
Instead returning a value, record it in a table entry.
Use base of divide and conquer to fill in start of
table.
Devise “look-up template”.
Devise for loops that fill the table using “look-up
template”.
Construction cond…
Divide and conquer
function choose(n,r)
If r=0 or r=n then return(1) else
return(choose(n-1,r-1)+choose(n-1,r))
Dynamic Programming
function choose(n,r)
for i:=0 to n-r do T[ i, 0 ] :=1
for i:=0 to r do T [ i , i ] := 1
for j : = 1 to r do
for i := j + 1 to n - r +j do
T [ i, j ]: = T [ i-1 , j -1 ] + T [ i -1 , j ]
S7
S1 S3 S6
S2 S4 S5 S7
The Knapsack Problem
Want knapsack(i ,j) to return true if there is subset of the first i
items that has total length exactly j.
S1 S2 S3 S4 Si-1 Si
When can knapsack(i ,j) return true? Either the ith item is user or
it is not.
Divide and Conquer
If the ith item is not used, and knapsack(i-1,j)
returns true.
S1 S2 S3 S4 . . . . S i-1
j
If the ith item is used, and knapsack(i-1 ,j-si) returns
true. S 1 S2 S 3 S 4 S i-1
j-si j si
The Code
Call knapsack(n,s)
function knapsack(i ,j)
comment return true if s1,s2…si can fill j
if i=0 then return(j=0)
else if knapsack(i-1,j) then return(true)
else if si ≤ j then
return(knapsack(i-1,j-si))
Let T(n) be the running knapsack(n,S)
c if n 1
T ( n)
2T ( n 1) d otherwise
i-1
i
Filling in the table
The algorithm
function knapsack(s1,s2,…….sn,S)
1. t [0 0] := true
3. for i:=1 to n do
4. for j:=0 to S do
5. t [i , j]:=t [i-1,j]
6. if j-si ≥0 then
7. t [i , j] := t [i , j] or t [i-1, j-si]
Analysis
Line 1 to 8 cost O(1)
The for loop on line 2 costs O(S)
The line 5-7 cost O(1)
The for loop on lines 4-7 costs O(S)
The for loop on lines 3-7 costs O(nS)
S=15