90 likes | 114 Views
Given a chain of matrices to be multiplied, this algorithm aims to minimize the number of steps needed for the multiplication by efficient parenthesizing. It computes the product of matrices A and B of specific sizes. Heart of the solution lies in calculating the minimum number of steps required to multiply matrices in a given range.
E N D
Matrix Chain Multiplication Input: Output: Objective: a chain of matrices to be multiplied a parenthesizing of the chain minimize number of steps needed for the multiplication
Matrix Chain Multiplication Input: Output: Objective: a chain of matrices to be multiplied a parenthesizing of the chain minimize number of steps needed for the multiplication
Matrix Chain Multiplication Input: Output: Objective: a chain of matrices to be multiplied a parenthesizing of the chain minimize number of steps needed for the multiplication Matrix multiplication: A of size m x n, B of size n x p How many steps to compute A.B ?
Matrix Chain Multiplication Input: Output: Objective: a chain of matrices to be multiplied a parenthesizing of the chain minimize number of steps needed for the multiplication Example: 4 2 5 1 2 3
Matrix Chain Multiplication Input: Output: Objective: a chain of matrices to be multiplied a parenthesizing of the chain minimize number of steps needed for the multiplication Heart of the solution:
Matrix Chain Multiplication Input: Output: Objective: a chain of matrices to be multiplied a parenthesizing of the chain minimize number of steps needed for the multiplication Heart of the solution: S[L,R] = the minimum number of steps required to multiply matrices from the L-th to the R-th
Matrix Chain Multiplication Input: Output: Objective: a chain of matrices to be multiplied a parenthesizing of the chain minimize number of steps needed for the multiplication Heart of the solution: S[L,R] = the minimum number of steps required to multiply matrices from the L-th to the R-th
Matrix Chain Multiplication • MATRIX-CHAIN-MULTIPLICATION (a0,…,an) • for L=1 to n do S[L,L] = 0 • for d=1 to n do • for L=1 to n-d do • R = L+d • S[L,R] = 1 • for k=L to R-1 • tmp = S[L,k]+S[k+1,R]+aL-1.ak.aR • if S[L,R] > tmp then S[L,R] = tmp • return S[1,n]