Analysis & Design of
Algorithms
Dynamic Programming I
Dynamic Programming (DP)
Dynamic programming is similar to
divide-and-conquer in that the problem is
broken down into smaller !" dependent
s#b$problems%
In this approach we solve the small
instances first& save the res#lts and look
them #p later& when we need them& rather
than re-compute them%
Dynamic Programming (DP)
Dynamic programming can sometimes
pro'ide a polynomial time sol#tion to a
problem for which divide-and-conquer
prod#ces an exponential r#n$time%
(ccasionally we find that we do not need
to sa'e all s#b$problem sol#tions%
Dynamic Programming (DP)
"his applied to problem where sol#tion to the
problem has s#b problems%
"he s#b problems are dependent on each other%
)
"hey are ('er *apping%
"he s#b$problems are repeated& so sol'ing each
time that s#b$problem is waste of time
)
+#nning "ime become ,-P(.,."IA*
Dynamic Programming (DP)
/o in dynamic programming we a'oid re$
comp#ting the same s#b$problems again and
again%
0inds the sol#tion to the s#b problem 1#st once
store it in a table 2or array3 (therefore Dynamic
Programming) and re#se the res#lt when the
s#b$problem is enco#ntered again%
Dynamic Programming (DP)
Difference between the Greedy Method and
Dynamic Programming is that in the 4reedy
5ethod we generate only one decision
se6#ence while in Dynamic Programming we
may generate many%
7hat differentiates Dynamic Programming
from brute-force en#meration is that& in
Dynamic Programming we attempt to generate
only the optimal decision se6#ences%
"wo key ingredients
"wo key ingredients for an optimi8ation problem to
be s#itable for a dynamic$programming sol#tion9
Each substructure is optimal.
(Principle of optimality)
1. optimal substructures 2. overlapping subproblems
Subproblems are dependent.
(otherwise, a divide-and-conquer
approach is the choice.)
: steps of Dynamic Programming
Algorithm
;% <haracteri8e the structure of an optimal
sol#tion%
2. ecursively define the 'al#e of an
optimal sol#tion%
=% <omp#te the 'al#e of an optimal
solution bottom-up%
:% 2<onstr#ct an optimal sol#tion from
comp#ted information 3
0ibonacci .#mbers
"he 0ibonacci n#mbers are a series of n#mbers
as follows9
fib(;) > ;
fib(?) > ;
fib(=) > ?
fib(:) > =
fib(@) > @
0ib(A)> B
%%%
fib(n) =
1, n <=
2
fib(n-1) + fib(n-2), n > 2
fib() = ! " ! = #
fib($) = # " ! =
fib(%) = # " = %
&.
0ibonacci .#mbers
"he 0ibonacci n#mbers Fn are defined as
follows9
0C > C
0; > ;
0n > 0n$; D 0n$?
0ibonacci .#mbers
"he rec#rsi'e definition of 0ibonacci
n#mbers gi'es #s a rec#rsi'e algorithm for
comp#ting them9
+ec#rsi'e calls to Fib(n)
+ec#rsi'e calls to Fib(n)
A single rec#rsi'e call to fib(n) res#lts in one
rec#rsi'e call to fib(n - 1),
two rec#rsi'e calls to fib(n - 2),
three rec#rsi'e calls to fib(n - 3),
five rec#rsi'e calls to fib(n - 4) and&
in general&
0or each call& weEre recomp#ting the same
0ibonacci n#mber from scratch%
!o" to avoid unnecessary
repetitions#
7e can a'oid this #nnecessary
repetitions by writing down the res#lts
of rec#rsi'e calls and looking them #p
again if we need them later%
"his process is called memoization%
+ec#rsi'e Algorithm9 0ibonacci
.#mbers
"akes ,Fponential time%
Act#al s#b problems are polynomial b#t they get
repeated
/#b problems are not I.D,P,.D,."%
/#b problems share s#b$s#b problems%
/o G% we can sol'e #sing Dynamic
programming%
Memoi$ation
Memoi$ation
7hen the s#b$problem is first
enco#ntered& itEs sol#tion is comp#ted and
stored%
,ach s#bse6#ent time the s#b$problem is
enco#ntered& its sol#tion is looked #p%
0ibonacci .#mbers ) #sing
5emoi8ation
5emoi8ation
Array F[ ] gets filled from bottom #p
i%e%& first F[2]& then F[3]& and so on& #p to F[n]%
<oin <hanging Problem
The Coin Changing Problem
/#ppose we need to make change for AHI% 7e want to
do this #sing the fewest n#mber of coins possible%
Pennies& nickels& dimes and 6#arters are a'ailable%
(ptimal sol#tion for AHIhas siF coins9 two 6#arters& one
dime& a nickel& and two pennies%
7e can #se a greedy algorithm to sol'e this problem9
repeatedly choose the largest coin less than or e6#al to
the remaining s#m& #ntil the desired s#m is obtained%
"his is how millions of people make change e'ery day%
The Coin-Changing problem:
formal description
*et D > Jd
;
& d
?
& %%%& d
k
K be a finite set of distinct coin
denominations% ,Fample9 d
;
> ?@I& d
?
> ;CI& d= > @I&
and d
:
> ;I%L
7e can ass#me each d
i
is an integer and d
;
M d
?
M %%% M d
k
%
,ach denomination is a'ailable in #nlimited 6#antity%
"he <oin$<hanging problem9
) 5ake change for n cents& #sing a minim#m total n#mber of
coins%
) Ass#me that d
k
> ; so that there is always a sol#tion%
The Greedy Method
0or the coin set J ?@I& ;CI& @I& ;IK& the greedy
method always finds the optimal sol#tion%
,Fercise9 pro'e it%
It may not work for other coin sets% 0or eFample
it stops working if we knock o#t the nickel%
,Fample9 D > J ?@I& ;CI& ;IK and n > =CI% "he
4reedy method wo#ld prod#ce a sol#tion9
)
?@ID @ N ;I& which is not as good as =N ;CI%
A Dynamic Programming Solution:
Step i!
A Dynamic Programming Solution:
Step ii!
An e"ample: coin set # $%&' ($&' )%&')&*
An e"ample
A Dynamic Programming Solution:
Step iii!
A Dynamic Programming Solution:
Step i+!
Step i+!: Print optimal solution