Question

Let A1,A2,A3,A_1, A_2, A_3, and A4A_4 be four matrices of dimensions 10×510 \times 5, 5×205 \times 20, 20×1020 \times 10, and 10×510 \times 5, respectively. The minimum number of scalar multiplications required to find the product A1A2A3A4A_1 A_2 A_3 A_4 using the basic matrix multiplication method is ________.

Official answer and solution
Official answer
1500
From
Official GATE 2016 answer key

Solution

Apply dynamic programming to solve the Matrix Chain Multiplication problem for 4 matrices.

Given matrix dimensions:

  • A1:10×5A_1: 10 \times 5 (p0=10,p1=5p_0 = 10, p_1 = 5)
  • A2:5×20A_2: 5 \times 20 (p2=20p_2 = 20)
  • A3:20×10A_3: 20 \times 10 (p3=10p_3 = 10)
  • A4:10×5A_4: 10 \times 5 (p4=5p_4 = 5)
    Dimension array: p=[10,5,20,10,5]p = [10, 5, 20, 10, 5].

Let m[i,j]m[i, j] denote the minimum number of scalar multiplications required to compute Ai…AjA_i \dots A_j.

  1. Chains of length 2:
  • m[1,2]=p0p1p2=10×5×20=1000m[1, 2] = p_0 p_1 p_2 = 10 \times 5 \times 20 = 1000
  • m[2,3]=p1p2p3=5×20×10=1000m[2, 3] = p_1 p_2 p_3 = 5 \times 20 \times 10 = 1000
  • m[3,4]=p2p3p4=20×10×5=1000m[3, 4] = p_2 p_3 p_4 = 20 \times 10 \times 5 = 1000
  1. Chains of length 3:
  • m[1,3]=min⁡{m[1,1]+m[2,3]+p0p1p3=0+1000+10×5×10=1500,m[1,2]+m[3,3]+p0p2p3=1000+0+10×20×10=3000}=1500m[1, 3] = \min\{ m[1, 1] + m[2, 3] + p_0 p_1 p_3 = 0 + 1000 + 10 \times 5 \times 10 = 1500, m[1, 2] + m[3, 3] + p_0 p_2 p_3 = 1000 + 0 + 10 \times 20 \times 10 = 3000 \} = 1500

  • m[2,4]=min⁡{m[2,2]+m[3,4]+p1p2p4=0+1000+5×20×5=1500,m[2,3]+m[4,4]+p1p3p4=1000+0+5×10×5=1250}=1250m[2, 4] = \min\{ m[2, 2] + m[3, 4] + p_1 p_2 p_4 = 0 + 1000 + 5 \times 20 \times 5 = 1500, m[2, 3] + m[4, 4] + p_1 p_3 p_4 = 1000 + 0 + 5 \times 10 \times 5 = 1250 \} = 1250

  1. Chain of length 4 (m[1,4]m[1, 4]):
  • Split at k=1k = 1: m[1,1]+m[2,4]+p0p1p4=0+1250+10×5×5=1250+250=1500m[1, 1] + m[2, 4] + p_0 p_1 p_4 = 0 + 1250 + 10 \times 5 \times 5 = 1250 + 250 = 1500
  • Split at k=2k = 2: m[1,2]+m[3,4]+p0p2p4=1000+1000+10×20×5=2000+1000=3000m[1, 2] + m[3, 4] + p_0 p_2 p_4 = 1000 + 1000 + 10 \times 20 \times 5 = 2000 + 1000 = 3000
  • Split at k=3k = 3: m[1,3]+m[4,4]+p0p3p4=1500+0+10×10×5=1500+500=2000m[1, 3] + m[4, 4] + p_0 p_3 p_4 = 1500 + 0 + 10 \times 10 \times 5 = 1500 + 500 = 2000

The minimum scalar multiplications required is:

m[1,4]=min⁡(1500,3000,2000)=1500m[1, 4] = \min(1500, 3000, 2000) = 1500

This optimal cost corresponds to the parenthesization A1((A2A3)A4)A_1 ((A_2 A_3) A_4).

Final answer

1500

Common trap

Multiplying left-to-right ((A1A2)A3)A4=1000+2000+500=3500((A_1 A_2) A_3) A_4 = 1000 + 2000 + 500 = 3500, which is sub-optimal.

The answer is the one in the official GATE answer key. The solution is this site's own explanation.

Source

Paper
GATE 2016 Computer Science and Information Technology (CS) Session 2
Question
48 of 65, printed as CS Q.38
Section
Computer Science and Information Technology

Report an error in this question

GATE questions are the work of the organising institutes. This site is independent and is not affiliated with them.