Dynamic Programming
Dynamic Programming
Src: http://www.ics.uci.edu/~eppstein/161/960109.html
CSE211: Design and Amrita School of Engineering
Analysis of Algorithms Amrita Vishwa Vidyapeetham
Memoized Solution
●
Memoization: Store intermediate results
so as to reuse
●
Memoized Algorithm
memo = {};
mfib(n):
if n in memo: return memo[n]
if n<=2: f=1
else: f=mfib(n-1)+mfib(n-2)
memo[n]=f
return f
Src: http://www.geeksforgeeks.org/dynamic-programming-set-8-matrix-chain-
multiplication/
CSE211: Design and Amrita School of Engineering
Analysis of Algorithms Amrita Vishwa Vidyapeetham
DP Strategy
●
Memoize
● Create a 2d matrix m to store intermediate
results
● N[i,j] represents the cost of multiplying the
maxtrices Ai..Aj
●
Solve final problem
●
Entry N[0,n-1] gives the total cost of multiplying
the matrices
●
Optimal parenthesization can be determined by
backtracking through table
{ }
N 2,2 +N 3,5 +d 1 d 2 d 5 =0+ 2500+35 . 15 . 20=13,000
N 2,5 =min N 2,3 +N 4,5 +d 1 d 3 d 5 =2625+ 1000+35 . 5x . 20=7125
N 2,4 +N 5,5 +d 1 d 4 d 5 =4375+ 0+35 . 10 . 20=11,375
CSE211: Design and Amrita School of Engineering
Analysis of Algorithms Amrita Vishwa Vidyapeetham
Bottom-up solution outline
● First compute Ni,i = 0 for i = 1 , 2 , . . . . , n
● Then compute Ni,i+1 for i = 1 , 2 , . . . . , n-1 and so on
● The Ni,j computed depends on Ni,k and Nk+1,j already
computed
● Using this layout, the minimum cost Ni,j for
multiplying a subchain AiAi+1. . . Aj of matrices
● Use intersection of lines running northeast from Ai and
northwest from Aj
● Each entry si,j records a value of k such that an
optimal parenthesization of AiAi+1. . . Aj splits the
product between Ak and Ak+1
https://s3.amazo
naws.com/hr-
challenge-
images/8677/14
33916387-
bae1920043-
LCS.png
Determine which items to take so that it fits the sack, and total
utility is maximized (an item must be taken completely or dropped)
●
Objective function to maximize
KP(4,10)
KP(3,10) KP(3,5)
KP(2,10) KP(2,6)
KP(1,10) KP(1,8)
d
N= ∑ x k c k
k=1
CSE211: Design and Amrita School of Engineering
Analysis of Algorithms Amrita Vishwa Vidyapeetham
Dynamic Programming Solution
● Subproblem: C[p] is the minimum
number of coins of denominations c1, c2,
. . . , cd needed for some value p
● Guess: The solution must start with
some coin ci
● remaining coins in the optimal solution
must themselves be the optimal solution to
making change for p−ci
● C[p] = 1+C[p−ci];
C [ p ]=
{ 0 ifp=0
mini:c ≼ p 1+c [ p−c i ] ifp>0
i
}
●
Find minimum coins for n=1,n=2, etc till final solution
reached
●
e.g consider denominations 1,2,5, and choose the
denominations in order
●
C[1] =1; C[2] = min(1+C[1], 1); C[5]= min(1+C[4], 1+C[3], 1)
CSE211: Design and Amrita School of Engineering
Analysis of Algorithms Amrita Vishwa Vidyapeetham
Recursion Tree
Src: http://ice-
web.cc.gatech.edu/ce21/1/static/audio/static/pythonds/_images/callTre
e.png
CSE211: Design and Amrita School of Engineering
Analysis of Algorithms Amrita Vishwa Vidyapeetham
Bottom-up DP Solution
Change(C, d, N)
C[0] = 0
for p = 1 to n
min = 1
for i = 1 to d
if c[i] ⩽ p then
if 1 + C[p − c[i]] < min then
min = 1 + C[p − c[i]]
coin = i
C[p] = min
S[p] = coin
return C and S