GATE Computer ScienceModerateGATE CS (PYQ pattern)

A recursive matrix-chain function without memoisation, cost(i, j), returns 0 when i = j and otherwise calls cost(i, k) and cost(k+1, j) for every k from i to j − 1. How many calls to cost (including the first one) are made by cost(1, 6)?

  1. A. 243
  2. B. 42
  3. C. 81
  4. D. 729

Correct answer: A. 243

Let R(n) be the calls for a chain of n matrices: R(1) = 1 and R(n) = 1 + Σ_{k=1}^{n−1} (R(k) + R(n−k)). This gives 1, 3, 9, 27, 81, 243, i.e. R(n) = 3^(n−1), so R(6) = 243. 42 is the number of parenthesisations of 6 matrices; with memoisation only 21 subchains are distinct.

More Algorithms questions

Exams that ask this topic