The document discusses the Matrix-Chain Multiplication problem, which involves finding the optimal way to parenthesize a sequence of matrices to minimize the number of scalar multiplications needed for their multiplication. It outlines the process of dynamic programming to solve the problem, including characterizing the structure of an optimal solution, defining recursive relationships, and computing costs efficiently. The final solution is achieved through a bottom-up approach, resulting in a time complexity of O(n^3) and space complexity of θ(n^2).
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0 ratings0% found this document useful (0 votes)
6 views
M2-matrix_chain_multiplication
The document discusses the Matrix-Chain Multiplication problem, which involves finding the optimal way to parenthesize a sequence of matrices to minimize the number of scalar multiplications needed for their multiplication. It outlines the process of dynamic programming to solve the problem, including characterizing the structure of an optimal solution, defining recursive relationships, and computing costs efficiently. The final solution is achieved through a bottom-up approach, resulting in a time complexity of O(n^3) and space complexity of θ(n^2).
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 23
Matrix-Chain Multiplication
Problem Statement • Given a sequence (chain) <A1,A2,...,An> of n matrices to be multiplied, and we wish to compute the product
• A1A2...An
• Parenthesize it to resolve all ambiguities in how the
matrices are multiplied together • Matrix multiplication is associative, and so all parenthesizations yield the same product Problem Statement • A product of matrices is fully parenthesized if it is either a single matrix or the product of two fully parenthesized matrix products, surrounded by parentheses
• For example, if the chain of matrices is <A 1,A2,A3,A4>, then we
can fully parenthesize the product A1A2A3A4 in five distinct ways: Problem Statement Problem Statement • A1A2...A3
• 10 X 100, 100 X 5, and 5 X 50
• ((A1A2)A3)
• (A1(A2A3))
• Computing the product according to the first
parenthesization is 10 times faster Problem Statement • Given a chain <A1,A2,...,An> of n matrices, where for i=1,2,...,n matrix Ai has dimension pi-1 x pi , fully parenthesize the product A1A2...An in a way that minimizes the number of scalar multiplications • We are not actually multiplying matrices • determine an order for multiplying matrices that has the lowest cost Problem Statement • time invested in determining this optimal order is more than paid for by the time saved later on when actually performing the matrix multiplications • Such as performing only 7500 scalar multiplications instead of 75,000 Counting the number of parenthesizations • When n = 1, only one way to fully parenthesize the matrix product • When n ≥ 2, a fully parenthesized matrix product is product of two fully parenthesized matrix subproducts, and the split between the two subproducts may occur between the kth and (k + 1)st matrices for any k = 1,2,...,n-1 Counting the number of parenthesizations • Thus we obtain the recurrence
• recurrence is similar to sequence of Catalan numbers
which grows as • number of solutions is thus exponential in n • brute-force method of exhaustive search makes for a poor strategy Applying dynamic programming 1. Characterize the structure of an optimal solution. 2. Recursively define the value of an optimal solution. 3. Compute the value of an optimal solution. 4. Construct an optimal solution from computed information. Characterize structure of an optimal solution • To parenthesize the product AiAi+1...Aj, we split the product between Ak and Ak+1 for some integer k in the range i ≤ k < j
• Cost of parenthesizing this way =
cost of computing the matrix Ai..k +
cost of computing Ak+1..j +
cost of multiplying them together
Characterize structure of an optimal solution • then optimally parenthsize AiAi+1...Ak, and also Ak+1Ak+2...Aj
• ensure that when we search for the correct place to split
the product, we have considered all possible places, so that we are sure of having examined the optimal one Step 2: A recursive solution • m[i, j] - minimum number of scalar multiplications needed to compute the matrix Ai..j
• If i = j , the problem is trivial; the chain consists of just one
matrix Ai..i = Ai no scalar multiplications are necessary to compute the product • m[i, i] = 0 for i = 1, 2,...,n Step 2: A recursive solution • Compute m[i, j] when i < j • We take advantage of structure of an optimal solution from step 1 • Let us assume that to optimally parenthesize, we split product AiAi+1...Aj between Ak and Ak+1, where i ≤ k <j
• m[i, j] = m[i, k] + m[k+1, j] +pi-1pkpj
• We do not know value of k so recursive defition becomes
Step 2: A recursive solution
• But complete information required is not avaiable in m
• we define s[i,j] to be a value of k at which we split the product AiAi+1...Aj in an optimal parenthesization Step 3: Computing the optimal costs • We could easily write a recursive algorithm based on recurrence to compute the minimum cost m[1,n] • this recursive algorithm takes exponential time, which is no better than the brute-force method of checking each way of parenthesizing the product Step 3: Computing the optimal costs • We have relatively few distinct subproblems: • One subproblem for each choice of i and j satisfying 1≤i≤ j≤n,
• A recursive algorithm may encounter each subproblem many
times in different branches of its recursion tree • Property of overlapping subproblems is the second hallmark of when dynamic programming applies (the first hallmark Bottom-up DP • Ai has dimensions pi-1 x pi for i = 1, 2,...,n sequence p = <p0, p1,...,pn>, where p.length = n + 1 auxiliary table m[1..n; 1..n] for storing the m[i, j] costs and another auxiliary table s[1..n-1, 2..n] that records which index of k achieved the optimal cost in computing m[i,j] • We shall use the table s to construct an optimal solution Bottom-up DP Bottom-up DP Bottom-up DP Bottom-up DP • A simple inspection of the nested loop structure of MATRIX- CHAIN-ORDER yields a running time of O(n3) for the algorithm. • The loops are nested three deep, and each loop index (l, i, and k) takes on at most n-1 values • requires θ(n2) space to store the m and s tables • more efficient than the exponential-time method of enumerating all possible parenthesizations and checking each one Step 4: Constructing an optimal solution
• the call PRINT-OPTIMAL-PARENS (s, 1, 6) prints the