0% found this document useful (0 votes)
49 views

Dynamic Programming 1 Compu Comb Knapsack

This document discusses dynamic programming and provides examples of its applications. It describes using dynamic programming to compute combinations and solve the knapsack problem. Dynamic programming is characterized as divide and conquer with a table to store solutions to subproblems to avoid recomputing them. It provides pseudocode for dynamic programming algorithms to compute combinations and solve the 0-1 knapsack problem in polynomial time by storing the subproblem solutions in a table.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
49 views

Dynamic Programming 1 Compu Comb Knapsack

This document discusses dynamic programming and provides examples of its applications. It describes using dynamic programming to compute combinations and solve the knapsack problem. Dynamic programming is characterized as divide and conquer with a table to store solutions to subproblems to avoid recomputing them. It provides pseudocode for dynamic programming algorithms to compute combinations and solve the 0-1 knapsack problem in polynomial time by storing the subproblem solutions in a table.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 32

IT-3105

Design and Analysis of Algorithms


Dynamic Programming-1

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

n2
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.

Numbers show the


order in which the entry are filled.
Example
Analysis
How many table entries are filled in?

(n-r+1) (r+1)=nr + n - r2+1 ≤ n (r+1)+1

Each entry takes time O(1), so total time required


is O(n2).

This is much better than O(2n).

Space: naive , O(nr). Smart O(r).


Dynamic Programming Technique
When divide and conquer generates a large no. of identical sub

problems , recursion is too expensive.

Instead store solutions to sub problems in a table.

This technique is called dynamic programming.

To design a dynamic programming algorithm:

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 ]

return (T [n, r])


The knapsack problem

Given n items of length S1,S2….Sn, is there is a


subset of these items with total length exactly S.
S1 S2 S3 S4 S5 S6 S7

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

Hence , by standard techniques, T(n)= Θ(2n).


Dynamic Programming
Store knapsack(i, j) in the table t [i, j]

t[i,j] is set to true if either


• t[i-1,j] is true, or
• t[i-1,j-si] makes sense and is true.
Dynamic Programming
This is done with the following code:
t [ i ,j] := t [ i-1, j]
if j – s i ≥ 0 then

t [i ,j] := t [ i, j] or t [i-1, j-si]


j-si j

i-1

i
Filling in the table
The algorithm
function knapsack(s1,s2,…….sn,S)

1. t [0 0] := true

2. for j:=1 to S do t[j,0]:= false

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)

There fore the algorithm runs in time O(nS).This is


usable if S is small.
Example
S1=1, S2=2, S3=2, S4=4, S5=5, S6=2, S7=4

S=15

t[i , j] := t [i-1, j] or t [i-1, j-si]

t [3,3] := t [2,3] or t [2 , 3-s3]

t [3,3] := t [2 ,3] or t [2,1]


Example
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 , bi and W are
integer values)
Problem: How to pack the knapsack to
achieve maximum total value of packed
items? 0
 Problem,in other words, is to find
max subject to ≤ W
The problem is called a “0-1” problem,
because each item must be entirely
accepted or rejected.

You might also like