Matrix chain multiplication
Download
1 / 49

Matrix Chain Multiplication - PowerPoint PPT Presentation


  • 150 Views
  • Uploaded on

Matrix Chain Multiplication. matrix multiplication. c. b. b. =. a. A = a x b matrix B = b x c matrix. Multiplying the Matrix. c. b. b. =. a. Time used = Θ (abc). Naïve Method. for (i = 1; i <= a;i++) { for (j = 1; i <= c;j++) { sum = 0; for (k = 1;k <= b;k++) {

loader
I am the owner, or an agent authorized to act on behalf of the owner, of the copyrighted work described.
capcha
Download Presentation

PowerPoint Slideshow about ' Matrix Chain Multiplication' - keahi


An Image/Link below is provided (as is) to download presentation

Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author.While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server.


- - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

Matrix multiplication
matrix multiplication

c

b

b

=

a

A = a x b matrix

B = b x c matrix


Multiplying the matrix
Multiplying the Matrix

c

b

b

=

a

Time used = Θ(abc)


Na ve method
Naïve Method

for (i = 1; i <= a;i++) {

for (j = 1; i <= c;j++) {

sum = 0;

for (k = 1;k <= b;k++) {

sum += A[i][k] * B[k][j];

}

C[i][j] = sum;

}

}

O(abc)


Matrix chain multiplication1
Matrix Chain Multiplication

N x N

matrix

N x N

matrix

N x 1 matrix

B

C

A

How to compute ABC ?


Matrix multiplication1
Matrix Multiplication

  • ABC = (AB)C = A(BC)

  • (AB)C differs from A(BC)?

    • Same result, different efficiency

  • What is the cost of (AB)C?

  • What is the cost of A(BC)?


(AB)C

N x N

N x N

N*N*N

N x N

N

x

1

N*N*1

N

x

1


A(BC)

N x N

N

x

1

N*N*1

N x N

N

x

1

N*N*1

N

x

1


The problem
The Problem

  • Input: a1,a2,a3,….,an

    • n-1 matrices of sizes

    • a1 x a2 B1

    • a2 x a3 B2

    • a3 x a4 B3

    • ….

    • an-1 x an Bn-1

  • Output

    • The order of multiplication

    • How to parenthesize the chain


Example
Example

INPUT

  • a1 a2 a3 a4 a5 a6

  • 10 x 5 x 1 x 5 x 10 x 2

B1 B2 B3 B4 B5

Possible Output

((B1B2)(B3B4))B5

(B1B2)((B3B4)B5)

(B1((B2B3)B4))B5

And much more…


Consider the output
Consider the Output

What do

(B1B2)((B3B4)B5)

(B1B2)(B3(B4B5))

What do

have in common?

((B1B2)(B3B4))B5

(((B1B2)B3)B4))B5

have in common?


Solving b 1 b 2 b 3 b 4 b n 1
Solving B1 B2 B3 B4 … Bn-1

Min cost of

B1 (B2 B3 B4 … Bn-1 )

(B1 B2) (B3 B4 … Bn-1 )

(B1 B2 B3) (B4 … Bn-1 )

(B1 B2 B3 B4 …) Bn-1


Solving b 1 b 2 b 3 b 4 b n 11
Solving B1 B2 B3 B4 … Bn-1

Min cost of

B1(B2 B3 B4 … Bn-1 )

(B1 B2)(B3 B4 … Bn-1 )

(B1 B2 B3) (B4 … Bn-1 )

(B1 B2 B3 B4 …) Bn-1

Sub problem

Sub problem

Sub problem

Sub problem

Sub problem

Sub problem


Matrix chain multiplication2
Matrix Chain Multiplication

B1…BN-1

(B1)(B2…BN-1)

(B1B2)(B3…BN-1)

(B1…)(BN-1)

(B…)(B2…BN-2)

… … … …

(B2…BN-2) (BN-1)


Deriving the recurrent
Deriving the Recurrent

  • mcm(l,r)

    • The least cost to multiply Bl … Br

  • The solution is mcm(1,n-1)


The recurrence
The Recurrence

  • Initial Case

    • mcm(x,x) = 0

    • mcm(x,x+1) = a[x] * a[x+1] * a[x+2]


The recurrence1
The Recurrence

min cost of Bl(Bl+1 Bl+2 Bl+3… Br ) + al•al+1•ar+1

min cost of (Bl Bl+1)(Bl+2 Bl+3… Br ) + al•al+2•ar+1

min cost of (Bl Bl+1 Bl+2) (Bl+3… Br ) + al•al+3•ar+1

min cost of (Bl Bl+1 Bl+2 Bl+3…)Br + al•ar•ar+1

mcm(l,r) = min of


The recurrence2
The Recurrence

0

mcm(l+1,r)

+

Bl(Bl+1 Bl+2 Bl+3… Br ) + al•al+1•ar+1

(Bl Bl+1)(Bl+2 Bl+3… Br ) + al•al+2•ar+1

(Bl Bl+1 Bl+2) (Bl+3… Br ) + al•al+3•ar+1

(Bl Bl+1 Bl+2 Bl+3…)Br + al•ar•ar+1

+

mcm(l,l+1)

mcm(l+2,r)

+

+

mcm(l,r) = min of

mcm(l,l+2)

mcm(l+3,r)

+

+

mcm(l,r-1)

0

+

+


Matrix chain multiplication3
Matrix Chain Multiplication

intmcm(intl,int r) {

if (l < r) {

minCost = MAX_INT;

for (inti = l;i < r;i++) {

my_cost = mcm(l,i) + mcm(i+1,r) + (a[l] * a[i+1] * a[r+1]);

minCost = min(my_cost,minCost);

}

returnminCost;

} else {

return 0;

}

}


Using bottom up dp
Using bottom-up DP

  • Design the table

  • M[i,j] = the best solution (min cost) for multiplying Bi…Bj

  • The solution is at M[i,n-1]


What is m i j
What is M[i,j]?

  • Trivial case

    • What is m[x,x] ?

    • No multiplication, m[x,x] = 0


What is m i j1
What is M[i,j]?

  • Simple case

    • What is m[x,x+1] ?

    • BxBx+1

    • Only one solution = ax* ax+1* ax+2


What is m i j2
What is M[i,j]?

  • General case

    • What is m[x,x+k] ?

    • BxBx+1Bx+2…Bx+k

Bx(Bx+1 Bx+2 Bx+3 … Bx-k) + ax•ax+1•ax+k+1

(Bx Bx+1)(Bx+2 Bx+3 … Bx+k) + ax•ax+2•ax-k+1

(Bx Bx+1 Bx+2) (Bx+3 … Bx+k) + ax•ax+3•ax+k+1

(Bx Bx+1 Bx+2 Bx+3 …)Bx+k + ax•ax+k•ax+k+1

min of


Filling the table
Filling the Table

M[1,1]

M[1,6] (our solution)


Filling the table1
Filling the Table

Trivial case


Filling the table2
Filling the Table

Arbitrary case


Filling the table3
Filling the Table

Arbitrary case

Plus a1•a2•a6


Filling the table4
Filling the Table

Arbitrary case

Plus a1•a3•a6


Filling the table5
Filling the Table

Arbitrary case

Plus a1•a4•a6


Filling the table6
Filling the Table

Arbitrary case

Plus a1•a5•a6




Example1
Example

  • a1 a2 a3 a4 a5 a6

  • 10 x 5 x 1 x 5 x 10 x 2

B1 B2 B3 B4 B5


Example2
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example3
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example4
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example5
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example6
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example7
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example8
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2

Option 1 = 0 + 25 + 10 x 5 x 5 = 275


Example9
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2

Option 2 = 50 + 25 + 10 x 1 x 5 = 100 minimal


Example10
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2

(2) means that the minimal solution is by dividing at B2

Option 2 = 50 + 25 + 10 x 1 x 5 = 100 minimal


Example11
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2

Option 1 = 0+ 50 + 5x 1 x 10 = 100


Example12
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2

Option 1 = 25+ 0 + 5x 5 x 10 = 275


Example13
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2

Option 1 is better


Example14
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example15
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example16
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


Example17
Example

a1 a2 a3 a4 a5 a6

10 x 5 x 1 x 5 x 10 x 2


ad