1 of 13

Multi-Way Choices: Matrix Chain Multiplication

Multiplying two matrices of order (m, n) and (n, p) requires mnp number of multiplications.

Consider the problem of multiplying a chain of matrices: A1, A2, A3

A1, : (2 x 3), A2, : (3 x 4), A : (4 x 5)

(A1, A2) A3 : (2 x 3 x 4 )+ (2 x 4 x 5) = 24+ 40= 64

A1 ( A2 , A3 ): (3 x 4 x 5)+ (2 x 3x 5) = 60 + 30 = 90

we see that by changing the sequence of evaluation, the cost of operation (number of multiplications) changes.

1

2 of 13

DYNAMIC PROGRAMMING

(Matrix Chain Multiplication)

Instructor

Neelima Gupta

University of Delhi, INDIA

ngupta@cs.du.ac.in

2

3 of 13

Multi-Way Choices: Matrix Chain Multiplication

Input: n matrices: A1, A2, ………, An

of order (d1, d2 ),(d2, d3 ),…… , (dn,dn+1) r.espectively

Aim: Determine the order in which the matrices should be multiplied so as to minimize the number of multiplications.

3

4 of 13

Order and the Choices to make

Order is implicit in the problem

Choices Optimal has for the split point k

(A1, A2, ……Ak ) ( Ak+1 …, An )

k could be 1 or 2 or …. n-1

k =1 means (A1) ( A2 …, An )

k =n-1 means (A1, A2, ……An-1 ) ( An )

4

5 of 13

Let Opt(1, n) denote the minimum the number of multiplications required to multiply the chain

A1, A2, ………, An

Let k be the split point then the minimum the number of multiplications required to multiply the chain is

m1 + m2 + d1 dk+1 dn+1

where m1= minimum the number of multiplications required to multiply the chain A1, A2, ………, Ak recursively and

m2= minimum the number of multiplications required to multiply the chain Ak+1, A2, ………, An recursively

5

6 of 13

Then,

Opt(1, n) = mink {m1 + m2 + d1 dk+1 dn+1}

= mink {Opt(1, k) + Opt(k+1, n) + d1 dk+1 dn+1}

Generalising,

Opt(1i, n j)

= mink {Opt(1i, k) + Opt(k+1, nj) + di dk+1 dj+1}

6

7 of 13

Example:

Matrix dimensions:

  • A1 : 3 × 5
  • A2 : 5 × 4
  • A3 : 4 × 2
  • A4 : 2 × 7
  • A5 : 7 × 3
  • A6 : 3 × 8

7

8 of 13

Problem of parenthesization

8

A1

A2

A3

A4

A5

A6

A1

0

60

A2

-

0

40

A3

-

-

0

56

A4

-

-

-

0

42

A5

-

-

-

-

0

168

A6

-

-

-

-

-

0

A1 *A2 = 3*5*4 = 60

A2*A3 = 5*4*2 = 40

A3*A4 = 4*2*7 = 56

A4*A5 = 2*7*3 = 42

A5*A6 = 7*3*8 = 168

A1 : 3 × 5

A2 : 5 × 4

A3 : 4 × 2

A4 : 2 × 7

A5 : 7 × 3

A6 : 3 × 8

9 of 13

Problem of parenthesization

9

A1

A2

A3

A4

A5

A6

A1

0

60

70

A2

-

0

40

110

A3

-

-

0

56

66

A4

-

-

-

0

42

90

A5

-

-

-

-

0

168

A6

-

-

-

-

-

0

A1 : 3 × 5

A2 : 5 × 4

A3 : 4 × 2

A4 : 2 × 7

A5 : 7 × 3

A6 : 3 × 8

  • A1 _A3= A1 *A2 *A3 = min( (A1.A2).A3 =60+ 3*4*2 =84 or A1.(A2 .A3)=40+ 3*5*2=70) =70
  • A2 _A4= A2 *A3 *A4 = min( (A2.A3).A4 =40+ 5*2*7=110 or A2.(A3.A4)=56+5*4*7=196)=110
  • A3 _A5= A3 *A4 *A5 = min( (A3.A4).A5 =56+4*7*3=140 or A3.(A4.A5)=42+ 4*2*3 =66) = 66
  • A4 _A6= A4 *A5 *A6 = min( (A4 *A5)*A6= 42+2*3*8=90 or A4 *(A5 *A6 )=168+ 2*7*8=312)=90

10 of 13

Problem of parenthesization

10

A1

A2

A3

A4

A5

A6

A1

0

60

70

K=1

112

K=3

130

K=3

202

K=5

A2

-

0

40

110

K=3

112

K=3

218

K=3

A3

-

-

0

56

66

K=3

154

K=3

A4

-

-

-

0

42

90

K=5

A5

-

-

-

-

0

168

A6

-

-

-

-

-

0

A1 : 3 × 5

A2 : 5 × 4

A3 : 4 × 2

A4 : 2 × 7

A5 : 7 × 3

A6 : 3 × 8

11 of 13

Overlapping Subproblems

m[1………4]

m[1],m[2,3,4] m[1,2],m[3,4] m[1,2,3],m[4]

m[2],m[3,4] m[2,3],m[4] m[1],m[2,3] m[1,2],m[3]

11

12 of 13

Number of sub-problems

Opt[i……………j] i<=j

n options n options

Number of subproblems= n2

12

13 of 13

Running Time

13

Ο(n2) entries in the table

each takes O(n) time to compute

Total running time : Ο(n3).