★ Matrix chain multiplication [00RF]
- November 7, 2026
Matrix multiplication cost [00RG]
- November 7, 2026
Suppose we have matrices A and B, where A is p \times q and B is q \times r. (To be able to multiply them, the q has to match.) How many operations are needed to compute the matrix product AB?
- The result AB will be a p \times r matrix, so it has pr entries.
- To compute each entry of AB, we take a row of q entries from A and a column of q entries from B and multiply them, then add the results. So we do about q multiplications and q additions to compute each element of AB.
Thus, the total time to compute AB is O(pqr). Note that we can assume p, q, and r are small enough that fancy matrix multiplication algorithms like Strassen’s algorithm don’t really help.
Motivation [00RH]
- November 7, 2026
Matrix multiplication is associative, (AB)C = A(BC), but these may not take the same time to compute! Let’s try an example. Say
- A is 2 \times 10,
- B is 10 \times 3,
- and C is 3 \times 20.
It takes 10 \times 3 \times 20 = 600 operations to compute BC, resulting in a 10 \times 20 matrix. It would then take another 2 \times 10 \times 20 = 400 to compute A(BC), for a total of 1000. On the other hand, if we associate the product as (AB)C, it takes 2 \times 10 \times 3 = 60 operations to compute the 2 \times 3 matrix AB, and then only another 2 \times 3 \times 20 = 120 operations to compute (AB)C, for a total of 180—a big difference!
With just three matrices the situation is simple: we can easily just do these computations, compare, and choose the cheaper order. However, if we have a sequence of n matrices we wish to multiply, the situation is more difficult. For example, suppose we have six matrices A_1 \dots A_6 and want to compute their product. We could associate them, for example, as (A_1((A_2(A_3A_4))A_5))A_6, or as A_1(A_2(A_3(A_4(A_5A_6)))), or in fact in any of 42 distinct parenthesizations. We really don’t want to check all of them to see which would be the cheapest. In general, the number of parenthesizations for n matrices is the (n-1)st Catalan number C_n = \frac {1}{n+1}\binom {2n}{n} which for large n is approximately equal to C_n \sim \frac {4^n}{n^{3/2} \sqrt {\pi }}, so the number of possibilities becomes very large, very fast. The brute force algorithm—i.e. checking all parenthesizations to see which is best—is completely out of the question!
Matrix chain multiplication problem [00RI]
- November 7, 2026
Formally, we have a sequence of n matrices A_1, A_2, …, A_n, and a sequence of n+1 positive integers p_1, …, p_{n+1} such that matrix A_i has size p_i \times p_{i+1}. We want to compute the parenthesization of the A_i which minimizes the number of operations needed to compute the product A_1 \dots A_n. (Note that this generalizes readily to any situation where we have a sequence of n things and an associative binary operation where the cost of the operation varies depending on the arguments it is applied to.)
Recurrence for matrix chain multiplication [00RJ]
- November 7, 2026
The key observation is that ultimately, there will be some final two matrices that get multiplied to produce the final answer. These have to come from some splitting of the sequence of matrices into two subproducts (A_1 \dots A_k)(A_{k+1} \dots A_n). The total cost to compute the product if we split at index k is then given by the sum of the optimal cost to compute A_1 \dots A_k, the optimal cost to compute A_{k+1} \dots A_n, and the cost to do the final matrix multiplication, which will take p_1 p_{k+1} p_{n+1} operations (because (A_1 \dots A_k) is a p_1 \times p_{k+1} matrix, and (A_{k+1} \dots A_n) is a p_{k+1} \times p_{n+1} matrix). The best possible cost for the product A_1 \dots A_n will then be the minimum cost over all such splitting points k. In general, if we use m[i,j] to denote the minimum cost for computing the product A_i \dots A_j, with i \leq j, then we have the recurrence \begin {align*} m[i,i] &= 0 \\ m[i,j] &= \min _{i \leq k < j} (m[i,k] + m[k+1,j] + p_i p_{k+1} p_{j+1}) \end {align*} Note that m[i,i] = 0 since this corresponds to the base case of already having a single matrix A_i, so we do not need to do any work.
Memoization for matrix chain multiplication [00RK]
- November 7, 2026
We can store the m[i,j] values in an n \times n matrix (actually, we just need the part above the main diagonal since we assume i \leq j). Note that each m[i,j] depends on values to its left (m[i,k] where k < j) and below it (m[k+1,j] where k \geq i). So we can fill in the matrix by diagonals, beginning by filling in zeros along the entire main diagonal, then filling in values just above the main diagonal, then the second diagonal above the main diagonal, and so on. Note that the dth diagonal above the main diagonal consists of values of the form m[i,i+d], which corresponds to subproducts A_i \dots A_{i+d} of exactly d+1 matrices. So this seems intuitively sensible: first we compute the optimal way to multiply any two adjacent matrices; then we compute the optimal way to multiply any three adjacent matrices; then any four, and so on.
(Alternatively, we could arrange the matrix a bit differently, so that m[i,d] would denote the optimal cost for multiplying A_i \dots A_{i+d}. Then we would simply fill in the matrix by columns.)
Runtime analysis for matrix chain multiplication [00RL]
- November 7, 2026
The matrix m has size n \times n, and we need to fill in half its entries, giving a total of \Theta (n^2) entries to fill in. Each entry is computed as a minimum over at most n costs, each of which takes O(1) to compute (just a few lookups, additions, and multiplications). Thus the whole algorithm is O(n^3).
Reconstructing optimal matrix parenthesization [00RM]
- November 7, 2026
As usual, this recurrence gives us the optimal cost but doesn’t tell us what the actual best parenthesization is. This is a common issue with dynamic programming; the problem is that when we take the minimum over all k for each entry in the matrix m, we forget which k was best, recording only the best cost itself. The solution, therefore, is to maintain another n \times n matrix b which records the information that is otherwise being forgotten: b[i,j] records the “best split”, that is, the value of k (where i \leq k < j) which results in the minimum cost for the product A_i \dots A_j. After filling in the matrices m and b, we can reconstruct the best parenthesization (which is really just a binary tree) by recursively splitting starting from the top: we start by looking up b[1,n] = k which tells us where to do the top-level split (A_1 \dots A_k)(A_{k+1} \dots A_n). We then recursively look up b[1,k] and b[k+1,n] to find out where to make the next splits, and so on, until there are just single matrices left at the leaves of the tree.