haechandev
Algorithms

[Baekjoon 11049] Matrix Chain Multiplication

Interval DP for finding the cheapest way to parenthesize a matrix product.

January 15, 2023

The order of the matrices is fixed, but the parentheses can change. This makes it an interval DP problem: store the minimum cost of multiplying matrices i through j.

I used dp[length][start] for the minimum operation count of a consecutive group of matrices and tried every split point k.

cost(i, j) = min(
  cost(i, k) + cost(k + 1, j)
  + row(i) * col(k) * col(j)
)

Starting with intervals of length two and expanding to longer ones ensures that the smaller answers are already available. The important shift was to accumulate the cost of the multiplication order, rather than multiply the actual matrix values.

Problem: Baekjoon · Code: View on GitHub