Dynamic Programming21 sections · 916 units
Open in Course

Matrix Chain Multiplication - The Setup

Understanding dimensions

The input is an array of dimensions. If dims = [1010, 2020, 3030, 4040], you have 33 matrices: A1A_1 is 10×2010 \times 20, A2A_2 is 20×3020 \times 30, A3A_3 is 30×4030 \times 40.

Matrix i has dimensions dims[i1]×dims[i]dims[i-1] \times dims[i]. The number of matrices is n = dims.length - 11. Multiplying matrices i through j (after optimal parenthesization) produces a matrix of size dims[i1]×dims[j]dims[i-1] \times dims[j]. This fact is key for computing dp[i][j] from smaller ranges. Understanding the problem structure is half the battle.