You probably learned matrix multiplication in school: multiply the rows of one matrix against the columns of the next. If A is 10×30 and B is 30×5, computing A·B costs 10 × 30 × 5 = 1,500 multiplications.
Now suppose you have a chain of matrices: A·B·C. Matrix multiplication is associative — (A·B)·C gives exactly the same answer as A·(B·C). The math is the same; the work is not. Depending on the dimensions, one parenthesization can cost thousands of times more than the other.
With just three matrices the choice is obvious. But with ten matrices there are 4,862 different parenthesizations to consider, and with twenty there are over six billion. Brute force is hopeless.
This is where dynamic programming earns its keep. By breaking the problem into overlapping subproblems and storing partial results, it finds the cheapest multiplication order for any chain of n matrices in time — an enormous speedup over exponential brute force. The algorithm was first described by Godbole (1973) and independently by Yao (1975) and is now a cornerstone example of dynamic programming alongside sequence alignment and coin change.
Comments
Loading comments...