chain matrix multiplication - hkustversion of october 26, 2016 chain matrix multiplication 12 / 27...

27
Chain Matrix Multiplication Version of October 26, 2016 Version of October 26, 2016 Chain Matrix Multiplication 1 / 27

Upload: others

Post on 27-May-2020

6 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Chain Matrix Multiplication

Version of October 26, 2016

Version of October 26, 2016 Chain Matrix Multiplication 1 / 27

Page 2: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Outline

Outline

Review of matrix multiplication.

The chain matrix multiplication problem.

A dynamic programming algorithm for chain matrixmultiplication.

Version of October 26, 2016 Chain Matrix Multiplication 2 / 27

Page 3: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Review of Matrix Multiplication

Matrix: An n ×m matrix A = [a[i , j ]] is a two-dimensional array

A =

a[1, 1] a[1, 2] · · · a[1,m − 1] a[1,m]a[2, 1] a[2, 2] · · · a[2,m − 1] a[2,m]...

......

...a[n, 1] a[n, 2] · · · a[n,m − 1] a[n,m]

,

which has n rows and m columns.

Example

A 4× 5 matrix: 12 8 9 7 6

7 6 89 56 25 5 6 9 108 6 0 −8 −1

.

Version of October 26, 2016 Chain Matrix Multiplication 3 / 27

Page 4: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Review of Matrix Multiplication

The product C = AB of a p × q matrix A and a q × r matrix B isa p × r matrix C given by

c[i , j ] =

q∑k=1

a[i , k]b[k , j ], for 1 ≤ i ≤ p and 1 ≤ j ≤ r

Complexity of Matrix multiplication: Note that C has pr entriesand each entry takes Θ(q) time to compute so the total proceduretakes Θ(pqr) time.

Example

A =

1 8 97 6 −15 5 6

, B =

1 87 65 5

, C = AB =

102 10144 8770 100

.

Version of October 26, 2016 Chain Matrix Multiplication 4 / 27

Page 5: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Remarks on Matrix Multiplication

Matrix multiplication is associative, e.g.,

A1A2A3 = (A1A2)A3 = A1(A2A3),

so parenthesization does not change result.

Matrix multiplication is NOT commutative, e.g.,

A1A2 6= A2A1

Version of October 26, 2016 Chain Matrix Multiplication 5 / 27

Page 6: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Matrix Multiplication of ABC

Given p × q matrix A, q × r matrix B and r × s matrix C ,ABC can be computed in two ways: (AB)C and A(BC )

The number of multiplications needed are:

mult[(AB)C ] = pqr + prs,

mult[A(BC )] = qrs + pqs.

Example

For p = 5, q = 4, r = 6 and s = 2,

mult[(AB)C ] = 180,

mult[A(BC )] = 88.

A big difference!

Implication: Multiplication “sequence” (parenthesization) isimportant!!

Version of October 26, 2016 Chain Matrix Multiplication 6 / 27

Page 7: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Outline

Review of matrix multiplication.

The chain matrix multiplication problem.

A dynamic programming algorithm for chain matrixmultiplication.

Version of October 26, 2016 Chain Matrix Multiplication 7 / 27

Page 8: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

The Chain Matrix Multiplication Problem

Definition (Chain matrix multiplication problem)

Given dimensions p0, p1, . . . , pn, corresponding to matrix sequenceA1, A2, . . ., An in which Ai has dimension pi−1 × pi , determine the“multiplication sequence” that minimizes the number of scalarmultiplications in computing A1A2 · · ·An.

i.e.,, determine how to parenthesize the multiplications.

Example

A1A2A3A4 = (A1A2)(A3A4) = A1(A2(A3A4)) = A1((A2A3)A4)

= ((A1A2)A3)(A4) = (A1(A2A3))(A4)

Exhaustive search: Ω(4n/n3/2).

Question

Is there a better approach?

Yes – DPVersion of October 26, 2016 Chain Matrix Multiplication 8 / 27

Page 9: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Outline

Review of matrix multiplication.

The chain matrix multiplication problem.

A dynamic programming algorithm.

Version of October 26, 2016 Chain Matrix Multiplication 9 / 27

Page 10: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Developing a Dynamic Programming Algorithm

Step 1: Define Space of Subproblems

Original Problem:Determine minimal cost multiplication sequence for A1..n.

Subproblems: For every pair 1 ≤ i ≤ j ≤ n:Determine minimal cost multiplication sequence forAi ..j = AiAi+1 · · ·Aj .

Note that Ai..j is a pi−1 × pj matrix.

There are(n2

)= Θ(n2) such subproblems. (Why?)

How can we solve larger problems using subproblem solutions?

Version of October 26, 2016 Chain Matrix Multiplication 10 / 27

Page 11: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Relationships among subproblems

At the last step of any optimal multiplication sequence (for asubbroblem), there is some k such that the two matrices Ai ..k andAk+1..j are multipled together. That is,

Ai ..j = (Ai · · ·Ak) (Ak+1 · · ·Aj) = Ai ..kAk+1..j .

Question

How do we decide where to split the chain (what is k)?

ANS: Can be any k . Need to check all possible values.

Question

How do we parenthesize the two subchains Ai ..k and Ak+1..j?

ANS: Ai ..k and Ak+1..j must be computed optimally, so we canapply the same procedure recursively.

Version of October 26, 2016 Chain Matrix Multiplication 11 / 27

Page 12: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Optimal Structure Property

If the “optimal” solution of Ai ..j involves splitting into Ai ..k andAk+1..j at the final step, then parenthesization of Ai ..k and Ak+1..j

in the optimal solution must also be optimal

If parenthesization of Ai ..k was not optimal, it could bereplaced by a cheaper parenthesization, yielding a cheaperfinal solution, constradicting optimality

Similarly, if parenthesization of Ak+1..j was not optimal, itcould be replaced by a cheaper parenthesization, againyielding contradiction of cheaper final solution.

Version of October 26, 2016 Chain Matrix Multiplication 12 / 27

Page 13: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Relationships among subproblems

Step 2: Constructing optimal solutions from optimal subproblem solutions.

For 1 ≤ i ≤ j ≤ n, let m[i , j ] denote the minimum number ofmultiplications needed to compute Ai ..j . This optimum costmust satisify the following recursive definition.

m[i , j ] =

0 i = j ,mini≤k<j (m[i , k] + m[k + 1, j ] + pi−1pkpj) i < j

Ai ..j = Ai ..kAk+1..j

Version of October 26, 2016 Chain Matrix Multiplication 13 / 27

Page 14: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Proof of Recurrence

Proof.

If j = i , then m[i , j ] = 0 because, no mutiplications are required.

If i < j , note that, for every k , calculating Ai ..k and Ak+1..j

optimally and then finishing by multiplying Ai ..kAk+1..j to get Ai ..j

uses (m[i , k] + m[k + 1, j ] + pi−1pkpj multiplications.

The optimal way of calculating Ai ..j uses no more than the worstof these j − i ways so

m[i , j ]≤ mini≤k<j

(m[i , k] + m[k + 1, j ] + pi−1pkpj) .

Ai ..j = Ai ..kAk+1..j

Version of October 26, 2016 Chain Matrix Multiplication 14 / 27

Page 15: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Proof of Recurrence (II)

Proof.

For the other direction, note that an optimal sequence ofmultiplications for Ai ..j is equivalent to splitting Ai ..j = Ai ..kAk+1..j

for some k, where the sequences of multiplications to calculateAi ..k and Ak+1..j are also optimal. Hence, for that special k ,

m[i , j ] = m[i , k] + m[k + 1, j ] + pi−1pkpj .

Combining with the previous page, we have just proven

m[i , j ] =

0 i = j ,mini≤k<j (m[i , k] + m[k + 1, j ] + pi−1pkpj) i < j .

Version of October 26, 2016 Chain Matrix Multiplication 15 / 27

Page 16: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Developing a Dynamic Programming Algorithm

Step 3: Bottom-up computation of m[i , j ].

Recurrence:

m[i , j ] = mini≤k<j

(m[i , k] + m[k + 1, j ] + pi−1pkpj)

Fill in the m[i , j ] table in an order, such that when it is time tocalculate m[i , j ], the values of m[i , k] and m[k + 1, j ] for all k arealready available.

An easy way to ensure this is to compute them in increasing orderof the size (j − i) of the matrix-chain Ai ..j :

m[1, 2], m[2, 3], m[3, 4], . . . , m[n− 3, n− 2], m[n− 2, n− 1], m[n− 1, n]m[1, 3], m[2, 4], m[3, 5], . . . , m[n − 3, n − 1], m[n − 2, n]m[1, 4], m[2, 5], m[3, 6], . . . , m[n − 3, n]. . .m[1, n − 1], m[2, n]

m[1, n]

Version of October 26, 2016 Chain Matrix Multiplication 16 / 27

Page 17: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Example for the Bottom-Up Computation

Example

A chain of four matrices A1, A2, A3 and A4, withp0 = 5, p1 = 4, p2 = 6, p3 = 2 and p4 = 7. Find m[1, 4].

S0: Initialization

Version of October 26, 2016 Chain Matrix Multiplication 17 / 27

Page 18: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Example – Continued

Step 1: Computing m[1, 2]By definition

m[1, 2] = min1≤k<2

(m[1, k] + m[k + 1, 2] + p0pkp2)

= m[1, 1] + m[2, 2] + p0p1p2 = 120.

Version of October 26, 2016 Chain Matrix Multiplication 18 / 27

Page 19: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Example – Continued

Step 2: Computing m[2, 3]By definition

m[2, 3] = min2≤k<3

(m[2, k] + pm[k + 1, 3] + p1pkp3)

= m[2, 2] + m[3, 3] + p1p2p3 = 48.

Version of October 26, 2016 Chain Matrix Multiplication 19 / 27

Page 20: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Example – Continued

Step 3: Computing m[3, 4]By definition

m[3, 4] = min3≤k<4

(m[3, k] + m[k + 1, 4] + p2pkp4)

= m[3, 3] + m[4, 4] + p2p3p4 = 84.

Version of October 26, 2016 Chain Matrix Multiplication 20 / 27

Page 21: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Example – Continued

Step 4: Computing m[1, 3]By definition

m[1, 3] = min1≤k<3

(m[1, k] + m[k + 1, 3] + p0pkp3)

= min

m[1, 1] + m[2, 3] + p0p1p3m[1, 2] + m[3, 3] + p0p2p3

= 88.

Version of October 26, 2016 Chain Matrix Multiplication 21 / 27

Page 22: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Example – Continued

Step 5: Computing m[2, 4]By definition

m[2, 4] = min2≤k<4

(m[2, k] + m[k + 1, 4] + p1pkp4)

= min

m[2, 2] + m[3, 4] + p1p2p4m[2, 3] + m[4, 4] + p1p3p4

= 104.

Version of October 26, 2016 Chain Matrix Multiplication 22 / 27

Page 23: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Example – Continued

Step 6: Computing m[1, 4]By definition

m[1, 4] = min1≤k<4

(m[1, k] + m[k + 1, 4] + p0pkp4)

= min

m[1, 1] + m[2, 4] + p0p1p4m[1, 2] + m[3, 4] + p0p2p4m[1, 3] + m[4, 4] + p0p3p4

= 158.

Version of October 26, 2016 Chain Matrix Multiplication 23 / 27

Page 24: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Constructing a Solution

m[i , j ] only keeps costs but not actual multiplication sequence.

To solve problem, need to reconstruct multiplication sequencethat yields m[1, n].

Solution: similar to previous DP algorithm(s) keep an auxillaryarray s[∗, ∗].s[i , j ] = k where k is the index that achieves minimum in

m[i , j ] = mini≤k<j

(m[i , k] + m[k + 1, j ] + pi−1pkpj) .

Version of October 26, 2016 Chain Matrix Multiplication 24 / 27

Page 25: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Developing a Dynamic Programming Algorithm

Step 4: Constructing optimal solution

Idea: Maintain an array s[1..n, 1..n], where s[i , j ] denotes k for theoptimal splitting in computing Ai ..j = Ai ..kAk+1..j .

Question

How to Recover the Multiplication Sequence using s[i , j ]?

s[1, n] (A1 · · ·As[1,n]) (As[1,n]+1 · · ·An)s[1, s[1, n]] (A1 · · ·As[1,s[1,n]]) (As[1,s[1,n]]+1 · · ·As[1,n])s[s[1, n] + 1, n] (As[1,n]+1 · · ·As[s[1,n]+1,n]) (As[s[1,n]+1,n]+1 · · ·An)...

...

Apply recursively until multiplication sequence is completelydetermined.

Version of October 26, 2016 Chain Matrix Multiplication 25 / 27

Page 26: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

Step 4...

Example (Finding the Multiplication Sequence)

Consider n = 6. Assume array s[1..6, 1..6] has been properlyconstructed. The multiplication sequence is recovered as follows.

s[1, 6] = 3 (A1A2A3)(A4A5A6)s[1, 3] = 1 (A1(A2A3))s[4, 6] = 5 ((A4A5)A6)

Hence the final multiplication sequence is

(A1(A2A3))((A4A5)A6).

Version of October 26, 2016 Chain Matrix Multiplication 26 / 27

Page 27: Chain Matrix Multiplication - HKUSTVersion of October 26, 2016 Chain Matrix Multiplication 12 / 27 Relationships among subproblems Step 2: Constructing optimal solutions from optimal

The Dynamic Programming Algorithm

Matrix-Chain(p, n): // l is length of sub-chain

for i = 1 to n do m[i , i ] = 0;;for l = 2 to n do

for i = 1 to n − l + 1 doj = i + l − 1;m[i , j ] =∞;for k = i to j − 1 do

q = m[i , k] + m[k + 1, j ] + p[i − 1] ∗ p[k] ∗ p[j ];if q < m[i , j ] then

m[i , j ] = q;s[i , j ] = k;

end

end

end

endreturn m and s; (Optimum in m[1, n])

Complexity: The loops are nested three levels deep. Each loop index takes on

≤ n values. Hence the time complexity is O(n3). Space complexity is Θ(n2).Version of October 26, 2016 Chain Matrix Multiplication 27 / 27