Dynamic Programming21 sections · 916 units
Open in Course

Matrix Chain Multiplication - Transition

Try all split points

To compute dp[i][j]dp[i][j], try every split point kk from ii to j1j-1. Splitting at kk means you first compute (MiMk)(M_i \ldots M_k), then (Mk+1Mj)(M_{k+1} \ldots M_j), then multiply the results.

Cost = dp[i][k]+dp[k+1][j]+dims[i1]×dims[k]×dims[j]dp[i][k] + dp[k+1][j] + dims[i-1] \times dims[k] \times dims[j]. The last term is the cost of multiplying the two resulting matrices.

Take the minimum over all kk:

dp[i][j]=minik<j(dp[i][k]+dp[k+1][j]+dims[i1]×dims[k]×dims[j])dp[i][j] = \min_{i \le k < j} \left( dp[i][k] + dp[k+1][j] + dims[i-1] \times dims[k] \times dims[j] \right)