Search code examples
algorithmdynamic-programmingcatalan

Calculating matrix chain mutlipication with Catalan numbers


I studied Matrix Chain Multiplication problem and I understand what the algorithm does. Lately, I came across Catalan numbers, which came in handy when solving the parenthesization problem. The problem appeared to me very similar to Matrix Chain Multiplication. In fact, in CLRS they mention the Catalan numbers in the Matrix Chain Multiplication chapter.

I am curious can you solve Matrix Chain Multiplication with Catalan numbers algorithm? My thoughts are: no, you can't solve because Catalan numbers describe number of ways to parenthesize matrices, whereas the original Matrix Chain problem asks a different question -- specific way to arrange parenthesis that would give the smallest cost.

Are my thoughts correct?


Solution

  • Matrix Chain Multiplication and Parenthesization Problem are equilvaent form of one another. One can be reduced into another.

    The Chain Matrix Multiplication Problem

    Given a sequence of n matrices A1, A2, ... An, and their dimensions p0, p1, p2, ..., pn, where where i = 1, 2, ..., n, matrix Ai has dimension pi − 1 × pi, determine the order of multiplication that minimizes the the number of scalar multiplications.

    Equivalent form: Parenthesization Problem

    Given n matrices, A1, A2, ... An, where for 1 ≤ i ≤ n, Ai is a pi − 1 × pi, matrix, parenthesize the product A1, A2, ... An so as to minimize the total cost, assuming that the cost of multiplying an pi − 1× pi matrix by a pi × pi + 1 matrix using the naive algorithm is pi − 1× pi × pi + 1

    When you try to write the recurrent relation of above problem, it turns out to be same as that of catalan numbers. Hence catalan number can be used to solve matrix chain multiplication problem.

    Matrix-chain Multiplication Problem