Summary: Matrix chain multiplication is an optimization problem that can be solved using dynamic programming.
Given a sequence of matrices, we want to find the most efficient way to multiply these matrices together. The
problem is not actually to perform the multiplications, but merely to decide in what order to perform the
We have many options because matrix multiplication is associative. In other words, no matter how we
parenthesize the product, the result will be the same. For example, if we had four matrices A, B, C, and D, we
(ABC)D = (AB)(CD) = A(BCD) = A(BC)D = ..
We could go through each possible parenthesization (brute force), but this would require time O(2n
), which is very slow
and impractical for large n.
One simple solution is called memoization: each time we compute the minimum cost needed to multiply out a specific
subsequence, we save it. If we are ever asked to compute it again, we simply give the saved answer, and do not
recompute it. Since there are about n2
/2 different subsequences, where n is the number of matrices, the space required
to do this is reasonable. It can be shown that this simple trick brings the runtime down from O(2n
) to O(n3
), which is
more than efficient enough for real applications. This is topdown dynamic programming.