Professional Documents
Culture Documents
CSC 302
SK Hafizul Islam
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 1 / 43
Agenda I
1 Dynamic Programming
Matrix-chain multiplication
Basics
Statement of the problem
Naive approach
Dynamic Programming approach
Optimal Substructure
Recursive definition of optimal solution
Find optimal cost
Construct an optimal solution
An Example
Dynamic Programming Algorithm
Recursive Algorithm
Memoization to avoid exponential time complexity
Naive Approach
Dynamic Programming Approach
Recursive Algorithm
3 Suggested Readings
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 3 / 43
Matrix-chain multiplication
T = A1 A2 · · · An (1)
How we parenthesize a chain of matrices can have a dramatic impact on the cost
of evaluating the product.
We can multiply two matrices A and B only if they are compatible: the number of columns
of A must equal the number of rows of B.
The time to compute C = A ∗ B, where C = [cij ]p×r , A = [aij ]p×q , and B = [bij ]q×r , is
dominated by the number of scalar multiplications in cij = aik ∗ bkj , which is pqr .
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 6 / 43
Matrix-chain multiplication
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 7 / 43
Matrix-chain multiplication: Naive approach
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 8 / 43
Matrix-chain multiplication: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 9 / 43
Matrix-chain multiplication: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 10 / 43
Matrix-chain multiplication: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 10 / 43
Matrix-chain multiplication: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 10 / 43
Matrix-chain multiplication: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 11 / 43
Matrix-chain multiplication: Dynamic Programming
There are only j − i possible values of k, i.e., k = i, i + 1, · · · j − 1, so we can check them all
and find the one which returns a smallest cost.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 12 / 43
Matrix-chain multiplication: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 13 / 43
Matrix-chain multiplication: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 14 / 43
Matrix-chain multiplication: Dynamic Programming
An Example
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 15 / 43
Matrix-chain multiplication: Dynamic Programming
An Example
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j
m[i, j] 1 2 3 4
1
2
3
4
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 15 / 43
Matrix-chain multiplication: Dynamic Programming
An Example
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 1
2 2
3
3
4
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 15 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 16 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j
m[i, j] 1 2 3 4
1 0
2 0
3 0
4 0
m[i, i] := 0 for i = 1, 2, 3, 4
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 16 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 1
2 0 2
3 0
3
4 0
m[i, i] := 0 for i = 1, 2, 3, 4
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 16 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 17 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j
m[i, j] 1 2 3 4
1 0 120
2 0
3 0
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 1 1
2 0 2
3 0
3
4 0
4
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 18 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j
m[i, j] 1 2 3 4
1 0 120
2 0 48
3 0
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 1 1
2 0 48 2 2
3 0
3
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 19 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j
m[i, j] 1 2 3 4
1 0 120
2 0 48
3 0 84
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 1 1
2 0 48 2 2
3 0 84
3 3
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 20 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j
m[i, j] 1 2 3 4
1 0 120 88
2 0 48
3 0 84
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 88 1 1 1
2 0 48 2 2
3 0 84
3 3
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 21 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j
m[i, j] 1 2 3 4
1 0 120 88
2 0 48 104
3 0 84
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 88 1 1 1
2 0 48 104 2 2 2
3 0 84
3 3
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 22 / 43
Matrix-chain multiplication: Dynamic Programming
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j
m[i, j] 1 2 3 4
1 0 120 88 158
2 0 48 104
3 0 84
4 0
Matrix A1 A2 A3 A4
Dimension p0 × p1 p1 × p2 p2 × p3 p3 × p4
Dimension 5×4 4×6 6×2 2×7
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 88 158 1 1 1 3
2 0 48 104 2 2 2
3 0 84
3 3
4 0
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 23 / 43
Matrix-chain multiplication: Dynamic Programming
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 88 158 1 1 1 3
2 0 48 104 2 2 2
3 0 84
3 3
4 0
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 23 / 43
Matrix-chain multiplication: Dynamic Programming
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 24 / 43
Matrix-chain multiplication: Dynamic Programming
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 88 158 1 1 1 3
2 0 48 104 2 2 2
3 0 84
3 3
4 0
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 24 / 43
MCM: Dynamic Programming-An Example
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 25 / 43
MCM: Dynamic Programming-An Example
We can compute m[i, j] for i < j We can compute s[i, j] for i < j
m[i, j] 1 2 3 4 s[i, j] 2 3 4
1 0 120 88 158 1 1 1 3
2 0 48 104 2 2 2
3 0 84
3 3
4 0
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 25 / 43
MCM: Dynamic Programming-Algorithm
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 27 / 43
MCM: Dynamic Programming-Algorithm
Algorithm: Recursive-Matrix-Chain(p, i, j)
1 if (i = j) then
2 Return 0
3 end
4 m[i, j] := ∞
5 for k ← i to j − 1 do
6 q :=Recursive-Matrix-Chain(p, i, k)+Recursive-Matrix-Chain(p, k + 1, j)+pi−1 pk pj
7 end
8 if (q < m[i, j]) then
9 m[i, j] := q
10 end
11 Return(m[1, j])
( Pn−1
1 + k=1 T (k) + T ((n − k) + 1) if n > 1
T (n) =
1 if n = 1
T (n) = Ω(2n )
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 28 / 43
MCM: Overlapping subproblems
15.3 Elements of dynamic programming 385
1..4
2..2 3..4 2..3 4..4 1..1 2..2 3..3 4..4 1..1 2..3 1..2 3..3
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 29 / 43
MCM: Dynamic Programming-Algorithm
Algorithm: Memoized-Matrix-Chain(p)
1 Let m[1 · · · n, 1 · · · n] be anew table
2 for i ← 1 to n do
3 for j ← i to n do
4 m[i, j] := ∞
5 end
6 end
7 Return Lookup-Chain(m, p, 1, n)
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 30 / 43
MCM: Dynamic Programming-Algorithm
Algorithm: Lookup-Chain(m, p, i, j)
1 if (m[i, j] < ∞) then
2 Return m[i, j]
3 end
4 if (i = j) then
5 m[i, j] := 0
6 end
7 else
8 for k ← i to j − 1 do
9 q :=Lookup-Chain(m, p, i, k)+Lookup-
Chain(m, p, k + 1, j)+pi−1 pk pj
10 if (q < m[i, j]) then
11 m[i, j] := q
12 end
13 end
14 end
15 Return m[i, j]
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 31 / 43
MCM: Dynamic Programming-Algorithm
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 32 / 43
Longest common subsequence
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 33 / 43
Longest common subsequence
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 34 / 43
LCS: Naive Approach
In a brute-force approach to solving the LCS problem, we would enumerate all subsequences
of X and check each subsequence to see whether it is also a subsequence of Y , keeping
track of the longest subsequence we find.
Each subsequence of X corresponds to a subset of the indices {1, 2, 3, · · · , m}.
X has 2m subsequences, this approach requires exponential time.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 35 / 43
LCS: Dynamic Programming Approach
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 36 / 43
LCS: Dynamic Programming Approach
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 37 / 43
LCS: Dynamic Programming Approach
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 37 / 43
Longest common subsequence
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 38 / 43
Longest common subsequence
T (n) = (m + n)
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 39 / 43
Longest common subsequence
15.4 4:
Step Longest common subsequence
Constructing an LCS 395
j 0 1 2 3 4 5 6
i yj B D C A B A
0 xi 0 0 0 0 0 0 0
1 A 0 0 0 0 1 1 1
2 B 0 1 1 1 1 2 2
3 C 0 1 1 2 2 2 2
4 B 0 1 1 2 2 3 3
5 D 0 1 2 2 2 3 3
6 A 0 1 2 2 3 3 4
7 B 0 1 2 2 3 4 4
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 40 / 43
Longest common subsequence
15.4 4:
Step Longest common subsequence
Constructing an LCS 395
j 0 1 2 3 4 5 6
X = ⟨A, B, C , B, D, A, B⟩
i yj B D C A B A
Y = ⟨B, D, C , A, B, A⟩
LCS of X and Y is
0 xi 0 0 0 0 0 0 0
Z = ⟨B, C , B, A⟩
1 A 0 0 0 0 1 1 1
2 B 0 1 1 1 1 2 2
3 C 0 1 1 2 2 2 2
4 B 0 1 1 2 2 3 3
5 D 0 1 2 2 2 3 3
6 A 0 1 2 2 3 3 4
7 B 0 1 2 2 3 4 4
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 40 / 43
Longest common subsequence: Recursive Algorithm
Algorithm: LCS-Length-Recur(X , Y , m, n)
1 if m = 0||n = 0 then
2 Return 0
3 end
4 else if X [m] = y [n] then
5 Return 1+LCS-Length-Recur(X , Y , m − 1, n − 1)
6 end
7 else
8 Return max LCS-Length-Recur(X , Y , m, n − 1),LCS-Length-Recur(X , Y , m − 1, n)
9 end
1
https://www.enjoyalgorithms.com/blog/longest-common-subsequence
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 41 / 43
Longest common subsequence: Recursive Algorithm
Algorithm: LCS-Length-Recur(X , Y , m, n)
1 if m = 0||n = 0 then
2 Return 0
3 end
4 else if X [m] = y [n] then
5 Return 1+LCS-Length-Recur(X , Y , m − 1, n − 1)
6 end
7 else
8 Return max LCS-Length-Recur(X , Y , m, n − 1),LCS-Length-Recur(X , Y , m − 1, n)
9 end
Chapter 15, Thomas H. Cormen, Charles E. Lieserson, Ronald L. Rivest and Clifford Stein,
Introduction to Algorithms, 3rd Edition, MIT Press/McGraw-Hill, 2009.
Chapter 05, E. Harowitz, S. Sahani, and S. Rajasekaran, Fundamentals of Computer
Algorithms, 2nd Edition, University Press, 2008.
Chapter 12, Michael T. Goodrich and Roberto Tamassia, Algorithm Design and Appli-
cations, Wiley, 2014.
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 42 / 43
Thank You
SK Hafizul Islam (Department of CSE, IIIT Kalyani) Algorithms - I October 19, 2022 43 / 43