Algo VC Lecture24
Algo VC Lecture24
Algo VC Lecture24
Dynamic programming
1
Recap
2
Dynamic Programming
4
The development of a dynamic programming
5
Rod cutting
6
Ex: a rod of length 4
7
Example: Fibonacci numbers
• Recall definition of Fibonacci numbers:
F(n-1) + F(n-2)
Computing the nth Fibonacci number using bottom-up iteration and recording
results:
F(0) = 0
F(1) = 1
F(2) = 1+0 = 1
…
F(n-2) =
F(n-1) =
F(n) = F(n-1)
0 + F(n1-2) 1 . . . F(n-2) F(n-1) F(n)
What if we solve it
Efficiency: n recursively?
- time n
9 - space
Examples of DP algorithms
• Computing a binomial coefficient
14
Algorithm to Multiply 2 Matrices
Input: Matrices Ap×q and Bq×r (with dimensions p×q and q×r)
Result: Matrix Cp×r resulting from the product A·B
MATRIX-MULTIPLY(Ap×q , Bq×r)
1. for i ← 1 to p
2. for j ← 1 to r
3. C[i, j] ← 0
4. for k ← 1 to q
5. C[i, j] ← C[i, j] + A[i, k] · B[k, j]
6. return C
15
Strassens’s Matrix Multiplication
16
Divide-and-Conquer
17
Divide and Conquer Matrix Multiply
A B = R
A0 A1 B0 B1 A0B0+A1B2 A0B1+A1B3
=
A2 A3 B2 B3 A2B0+A3B2 A2B1+A3B3
18
Divide and Conquer Matrix Multiply
A B = R
a0 b0 = a0 b0
19
Strassens’s Matrix Multiplication
20
Comparison
C11 = P1 + P4 - P5 + P7
= (A11+ A22)(B11+B22) + A22 * (B21 - B11) - (A11 + A12) * B22+
(A12 - A22) * (B21 + B22)
= A11 B11 + A11 B22 + A22 B11 + A22 B22 + A22 B21 – A22 B11 -
A11 B22 -A12 B22 + A12 B21 + A12 B22 – A22 B21 – A22 B22
= A11 B11 + A12 B21
21
Strassen Algorithm
void matmul(int *A, int *B, int *R, int n) {
if (n == 1) {
(*R) += (*A) * (*B);
} else {
matmul(A, B, R, n/4);
matmul(A, B+(n/4), R+(n/4), n/4);
matmul(A+2*(n/4), B, R+2*(n/4), n/4);
matmul(A+2*(n/4), B+(n/4), R+3*(n/4), n/4);
matmul(A+(n/4), B+2*(n/4), R, n/4);
matmul(A+(n/4), B+3*(n/4), R+(n/4), n/4);
matmul(A+3*(n/4), B+2*(n/4), R+2*(n/4), n/4);
matmul(A+3*(n/4), B+3*(n/4), R+3*(n/4), n/4);
Divide matrices in
}
sub-matrices and
recursively multiply
22 sub-matrices
Time Analysis
23
Matrix-chain Multiplication
24
Matrix-chain Multiplication
25
Matrix-chain Multiplication
To compute the number of scalar multiplications necessary, we
must know:
– Algorithm to multiply two matrices
– Matrix dimensions
26
Matrix-chain Multiplication
Total:
27 75,000
Matrix-chain Multiplication
Matrix-chain multiplication problem
– Given a chain A1, A2, …, An of n matrices, where for i=1, 2, …, n,
matrix Ai has dimension pi-1pi
– Parenthesize the product A1A2…An such that the total number of scalar
multiplications is minimized
Brute force method of exhaustive search takes time exponential
in n
28
Dynamic Programming Approach
The structure of an optimal solution
– Let us use the notation Ai..j for the matrix that results from the product
Ai Ai+1 … Aj
– An optimal parenthesization of the product A1A2…An splits the product
between Ak and Ak+1 for some integer k where1 ≤ k < n
– First compute matrices A1..k and Ak+1..n ; then multiply them to get the
final matrix A1..n
29
Dynamic Programming Approach
– That is, the optimal solution to the problem contains within it the
optimal solution to subproblems
30
Summary
32