Dynamic Programming - Algorithm
Dynamic Programming - Algorithm
Dynamic Programming - Algorithm
Dynamic Programming
Mashiour Rahman
Assistant Professor
[email protected]
American International University Bangladesh
Algorithm design techniques
Iterative (brute-force) algorithms
Example: insertion sort
Algorithms that use efficient data structures
Example: heap sort
Divide-and-conquer algorithms
Example: Binary search, merge sort, quick sort
Dynamic programming
Example: Fibonacci numbers, Matrix multiplication optimization, Longest
Common Subsequence
Greedy algorithms
Example: Huffman coding, Minimum Spanning Tree, Dijkstara
Graph Algorithms
Example: BFS (Robot moving, Path Finding), DFS( Topological Sorting,
Strongly Connected Component)
F(5) F(4)
F(1) F(0)
F(5) F(4)
F(1) F(0)
p1 p2 p3 p4
A4
A2
A3
p0 A1
A2 A4 A2
A3 A 3 A4
A1 A1
p0 p0
A2(A3A4)
p0 A1
A1( A2(A3A4)) p0
(A1(A2(A3 A4)))
p0p1p4 multiplications
((A1 A2)A3)
(A1 A2)
p0p2p3 multiplications
p0p1p2 multiplications
Total: p0p1p2 + p0p2p3 + p0p3p4
p4
p3 p4
A4
((A1A2)A3)A4
p0 (A1A2)A3
p0
(((A1 A2)A3)A4)
p0p3p4 multiplications
(((AB)C)D)=30x25
Costs:
T1 = d1-1 x d1 = d0 x d1 = 30 x 1
(((AB)
AB C)D)
T2 = d2-1 x d2 = d1 x d2 = 1 x 40
1200 + 12000 + 7500 = 20700 T3 = d3-1 x d3 = d2 x d3 = 40 x 10
((AB)(
AB CD)
CD T4 = d4-1 x d4 = d3 x d4 = 10 x 25
1200 + 10000 + 30000 = 41200 Costs:
(A((BC)
BC D)) ((T1 T2) T3) T4 = 20700
400 + 250 + 750 = 1400 (T1 T2) (T3 T4) = 41200
T1 ((T2 T3) T4) = 1400
Since Ti..j can be obtained by breaking it into Ti..k & Tk+1..j , we have
0 if i j
M(i,j)
min
i kj
M(i,k) M(k 1,j) di1 dk dj if i j
But there are only few different subproblems (i, j): one solution for each
choice of i and j (i < j).
j
Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming31
MCM::Step 2: Recursive (Recurrence) Formulation
Recursive-Matrix-Chain(d, i, j)
1 if i = j then
2 return 0;
3 M[i, j] = ∞;
4 for k = i to j-1 do
5 q = Recursive-Matrix-Chain(d, i, k) +
Recursive-Matrix-Chain(d, k+1, j) +
di-1dkdj
6 if q < M[i, j] then
7 M[i, j] = q ;
S[i, j] = k ;
8 return M[i, j] ;
Overlapping Subproblems
Let T(n) be the time complexity of
Recursive-Matrix-Chain(d, 1, n)
For n > 1, we have
n 1
T2..2 T3..4
Length = 2 T1..2 T2..3 T3..4 T2..4 =
T2..3 T4..4
Matrix-Chain-Order( d )
[i, j]=[ , ] to [ , ] 4 4
5 5
k= 6
MemoMCM(i,j)
MemoMCM(i,j)
1.
1. if
if ii == jj then
then return
return 00
2.
2. else
else if M[i,j] << then
if M[i,j] then return
return M[i,j]
M[i,j]
3.
3. else
else for
for kk :=:= ii to
to j-1
j-1 do
do
4.
4. qq :=
:= MemoMCM(i,k)+
MemoMCM(i,k)+
MemoMCM(k+1,j)
MemoMCM(k+1,j) ++ ddi-1 dkdj
i-1dkdj
5.
5. if
if qq << M[i,j]
M[i,j] then
then
6.
6. M[i,j]
M[i,j] :=:= qq
7.
7. return
return M[i,j]
M[i,j]
Initialize all elements to and call MemoMCM(i,j)
Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming44
Memoization
Memoization:
Solve the problem in a top-down fashion, but
record the solutions to subproblems in a table.
Pros and cons:
Recursion is usually slower than loops and uses
stack space
Easier to understand
If not all subproblems need to be solved, you are
sure that only the necessary ones are solved
Z is a subsequence of X if
zj = xi( j ) for all j = 1, … , k
where <i(1),i(2),…,i(k)> is strictly increasing (but not required
to be consecutive)
Examples:
Let X = < A, B, C, B, D, A, B >
< A >, < B >, < C >, and < D > are subsequences.
< C, A >, < C, B >, < C, B, A, B > are subsequences.
How many possible subsequences for a n-element sequence?
Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming46
Longest Common Subsequence Problem
Z is a common subsequence of sequences X and Y if Z is a
subsequence of both X and Y.
Example:
Let X = < A, B, C, B, D, A, B > and Y = < B, D, C, A, B, A >
< A >, <B>, <C>, and <D> are common subsequences.
< C, A > is, but < A, C > is not.
< B, C, A> is a common subsequence
< B, C, B, A > is the longest common subsequence.
Example:
x = “sariempiolcewe”
y = “westigmupsalrte”
Mashiour Rahman AIUB::CSC2105::Algorithms Dynamic Programming47
Longest Common Subsequence Problem
LCS:
LCS(x,y))
Brute-force algorithm:
X a b b c a a c
Y 0 0 0 0 0 0 0 0
a 0
c 0
c 0
b 0
c 0
c 0
a 0
a b b c a a c
0 0 0 0 0 0 0 0
a 0
c 0
c 0
b 0
c 0
c 0
a 0
a b b c a a c
0 0 0 0 0 0 0 0
a 0
c 0
c 0
b 0
c 0
c 0
a 0
a b b c a a c
0 0 0 0 0 0 0 0
a 0 1 1 1 1 1 1 1
c 0 1 1 1 2 2 2 2
c 0 1 1 1 2 2 2 3
b 0 1 2 2 2 2 2 3
c 0 1 2 2 3 3 3 3
c 0 1 2 2 3 3 3 4
a 0 1 2 2 3 4 4 4
length[X] = m, length[Y] = n,
Call Print-LCS(b, X, n, m) to construct LCS
Time complexity: O(m+n).
a b b c a a c
0 0 0 0 0 0 0 0
a 0 1 1 1 1 1 1 1
c 0 1 1 1 2 2 2 2
c 0 1 1 1 2 2 2 3
b 0 1 2 2 2 2 2 3
c 0 1 2 2 3 3 3 3
c 0 1 2 2 3 3 3 4
a 0 1 2 2 3 4 4 4