Skip to content
Official PYQ · 2018

GATE CS 2018 Q31 — Algorithm Design: Dynamic Programming

Explain with AI

Assume that multiplying a matrix G1G_1 of dimension p×qp \times q with another matrix G2G_2 of dimension q×rq \times r requires pqrpqr scalar multiplications. Computing the product of nn matrices G1G2G3…GnG_1G_2G_3 \dots G_n can be done by parenthesizing in different ways. Define GiGi+1G_i G_{i+1} as an explicitly computed pair for a given paranthesization if they are directly multiplied. For example, in the matrix multiplication chain G1G2G3G4G5G6G_1G_2G_3G_4G_5G_6 using parenthesization (G1(G2G3))(G4(G5G6)),G2G3(G_1(G_2G_3))(G_4(G_5G_6)), G_2G_3 and G5G6G_5G_6 are only explicitly computed pairs.

Consider a matrix multiplication chain F1F2F3F4F5F_1F_2F_3F_4F_5, where matrices F1,F2,F3,F4F_1, F_2, F_3, F_4 and F5F_5 are of dimensions 2×25,25×3,3×16,16×12 \times 25, 25 \times 3, 3 \times 16, 16 \times 1 and 1×10001 \times 1000, respectively. In the parenthesization of F1F2F3F4F5F_1F_2F_3F_4F_5 that minimizes the total number of scalar multiplications, the explicitly computed pairs is/are

Choose one option
View worked solution

Answer: C

To find the optimal parenthesization of the matrix chain F1F2F3F4F5F_1 F_2 F_3 F_4 F_5, we use dynamic programming. The dimensions of the matrices are:

  • F1:2×25F_1: 2 \times 25
  • F2:25×3F_2: 25 \times 3
  • F3:3×16F_3: 3 \times 16
  • F4:16×1F_4: 16 \times 1
  • F5:1×1000F_5: 1 \times 1000

Let m[i,j]m[i, j] denote the minimum number of scalar multiplications needed to compute the product Fi…FjF_i \dots F_j. We compute the values for chains of increasing length:

  • Length 2:
    m[1,2]=2×25×3=150m[1, 2] = 2 \times 25 \times 3 = 150
    m[2,3]=25×3×16=1200m[2, 3] = 25 \times 3 \times 16 = 1200
    m[3,4]=3×16×1=48m[3, 4] = 3 \times 16 \times 1 = 48
    m[4,5]=16×1×1000=16000m[4, 5] = 16 \times 1 \times 1000 = 16000
  • Length 3:
    m[1,3]=min⁡(m[1,2]+2×3×16,m[2,3]+2×25×16)=min⁡(150+96,1200+800)=246m[1, 3] = \min(m[1, 2] + 2 \times 3 \times 16, m[2, 3] + 2 \times 25 \times 16) = \min(150 + 96, 1200 + 800) = 246   →(F1F2)F3\rightarrow (F_1 F_2) F_3
    m[2,4]=min⁡(m[2,3]+25×16×1,m[3,4]+25×3×1)=min⁡(1200+400,48+75)=123m[2, 4] = \min(m[2, 3] + 25 \times 16 \times 1, m[3, 4] + 25 \times 3 \times 1) = \min(1200 + 400, 48 + 75) = 123   →F2(F3F4)\rightarrow F_2 (F_3 F_4)
    m[3,5]=min⁡(m[3,4]+3×1×1000,m[4,5]+3×16×1000)=min⁡(48+3000,16000+48000)=3048m[3, 5] = \min(m[3, 4] + 3 \times 1 \times 1000, m[4, 5] + 3 \times 16 \times 1000) = \min(48 + 3000, 16000 + 48000) = 3048   →(F3F4)F5\rightarrow (F_3 F_4) F_5
  • Length 4:
    m[1,4]=min⁡(m[2,4]+2×25×1,m[1,2]+m[3,4]+2×3×1,m[1,3]+2×16×1)=min⁡(123+50,150+48+6,246+32)=173m[1, 4] = \min(m[2, 4] + 2 \times 25 \times 1, m[1, 2] + m[3, 4] + 2 \times 3 \times 1, m[1, 3] + 2 \times 16 \times 1) = \min(123 + 50, 150 + 48 + 6, 246 + 32) = 173   →F1(F2(F3F4))\rightarrow F_1 (F_2 (F_3 F_4))
    m[2,5]=min⁡(m[2,4]+25×1×1000,… )=123+25000=25123m[2, 5] = \min(m[2, 4] + 25 \times 1 \times 1000, \dots) = 123 + 25000 = 25123   →(F2(F3F4))F5\rightarrow (F_2 (F_3 F_4)) F_5
  • Length 5:
    m[1,5]=min⁡(m[1,4]+2×1×1000,… )=173+2000=2173m[1, 5] = \min(m[1, 4] + 2 \times 1 \times 1000, \dots) = 173 + 2000 = 2173   →((F1(F2(F3F4)))F5)\rightarrow ((F_1 (F_2 (F_3 F_4))) F_5)

The optimal parenthesization is (((F1(F2(F3F4)))F5)(((F_1 (F_2 (F_3 F_4))) F_5). In this structure, the only pair of adjacent matrices from the original chain that is directly multiplied is F3F4F_3 F_4. Thus, the only explicitly computed pair is F3F4F_3 F_4.

Free practice · Your answers on this page are not saved.

Published
Report a correction
Keep exploringMore Algorithm Design: Dynamic Programming questions