0% found this document useful (0 votes)
10 views

Matrix Chain Multiplication

Uploaded by

pavithra.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views

Matrix Chain Multiplication

Uploaded by

pavithra.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 4

Matrix Chain Multiplication is a classic optimization problem that arises in the context of

multiplying a sequence of matrices. The goal is to determine the most efficient way to
multiply a given set of matrices together. The efficiency is measured in terms of the number
of scalar multiplications required, which can be significant depending on the order of
multiplication.

Problem Definition

Given a sequence of matrices A1,A2,…,AnA_1, A_2, \ldots, A_nA1,A2,…,An, where the


dimensions of matrix AiA_iAi are pi−1×pip_{i-1} \times p_ipi−1×pi, the task is to find the
optimal way to parenthesize the product A1A2…AnA_1 A_2 \ldots A_nA1A2…An so that
the total number of scalar multiplications is minimized.

Matrix Multiplication Cost

When multiplying two matrices AAA and BBB:

 If AAA is of size p×qp \times qp×q and BBB is of size q×rq \times rq×r, the number
of scalar multiplications required is p×q×rp \times q \times rp×q×r.

Example

Consider the following matrices:

 A1A_1A1 (10 × 30)


 A2A_2A2 (30 × 5)
 A3A_3A3 (5 × 60)

The dimensions can be represented as:

 p0=10,p1=30,p2=5,p3=60p_0 = 10, p_1 = 30, p_2 = 5, p_3 = 60p0=10,p1=30,p2


=5,p3=60

The goal is to compute the product A1A2A3A_1 A_2 A_3A1A2A3 with the minimum
number of multiplications.

Approach: Dynamic Programming

The Matrix Chain Multiplication problem can be efficiently solved using a dynamic
programming approach. Here’s a step-by-step breakdown of the algorithm:

1. Define the Cost Matrix:


o Create a 2D array m[i][j]m[i][j]m[i][j] that will store the minimum number of
scalar multiplications required to compute the product of matrices from
AiA_iAi to AjA_jAj.
2. Define the Split Point:
o For every possible split point kkk between iii and jjj, the cost of computing the
product can be expressed as: m[i][j]=min⁡i≤k<j{m[i][k]+m[k+1][j]
+pi−1×pk×pj}m[i][j] = \min_{i \leq k < j} \{ m[i][k] + m[k+1][j] + p_{i-1} \
times p_k \times p_j \}m[i][j]=i≤k<jmin{m[i][k]+m[k+1][j]+pi−1×pk×pj}
o Here, pi−1×pk×pjp_{i-1} \times p_k \times p_jpi−1×pk×pj is the cost of
multiplying the two resulting matrices.
3. Base Case:
o If i=ji = ji=j (only one matrix), then m[i][j]=0m[i][j] = 0m[i][j]=0 because no
multiplication is needed.
4. Fill the Cost Matrix:
o Iterate through chain lengths (from 2 to nnn) and compute the minimum costs
for each possible subchain.
5. Reconstruct the Solution:
o Optionally, maintain another 2D array s[i][j]s[i][j]s[i][j] to record the split
points, allowing you to reconstruct the optimal parenthesization.

Example of Matrix Chain Multiplication

Let's solve the problem step-by-step with matrices A1,A2,A3A_1, A_2, A_3A1,A2,A3:

1. Dimensions:
o p=[10,30,5,60]p = [10, 30, 5, 60]p=[10,30,5,60]
2. Initialization:
o Create a 3x3 cost matrix mmm:

css
Copy code
m = [ [0, ∞, ∞],
[∞, 0, ∞],
[∞, ∞, 0]
]

3. Calculating Costs:
o For chain length=2chain\ length = 2chain length=2:
 For (i,j)=(1,2)(i, j) = (1, 2)(i,j)=(1,2):
 Calculate m[1][2]m[1][2]m[1][2]:

m[1][2]=m[1][1]+m[2][2]+p0×p1×p2=0+0+10×30×5=1500m[1][2] =
m[1][1] + m[2][2] + p_0 \times p_1 \times p_2 = 0 + 0 + 10 \times 30 \
times 5 = 1500m[1][2]=m[1][1]+m[2][2]+p0×p1×p2
=0+0+10×30×5=1500

 For (i,j)=(2,3)(i, j) = (2, 3)(i,j)=(2,3):


 Calculate m[2][3]m[2][3]m[2][3]:

m[2][3]=m[2][2]+m[3][3]+p1×p2×p3=0+0+30×5×60=9000m[2][3] =
m[2][2] + m[3][3] + p_1 \times p_2 \times p_3 = 0 + 0 + 30 \times 5 \
times 60 = 9000m[2][3]=m[2][2]+m[3][3]+p1×p2×p3
=0+0+30×5×60=9000

o Now mmm looks like:

css
Copy code
m = [ [0, 1500, ∞],
[∞, 0, 9000],
[∞, ∞, 0]
]

4. For chain length=3chain\ length = 3chain length=3:


o Calculate m[1][3]m[1][3]m[1][3]:
o Split at k=1k=1k=1: m[1][3]=m[1][1]+m[2]
[3]+p0×p1×p3=0+9000+10×30×60=18000m[1][3] = m[1][1] + m[2][3] +
p_0 \times p_1 \times p_3 = 0 + 9000 + 10 \times 30 \times 60 = 18000m[1]
[3]=m[1][1]+m[2][3]+p0×p1×p3=0+9000+10×30×60=18000
o Split at k=2k=2k=2: m[1][3]=m[1][2]+m[3]
[3]+p0×p2×p3=1500+0+10×5×60=4500m[1][3] = m[1][2] + m[3][3] + p_0 \
times p_2 \times p_3 = 1500 + 0 + 10 \times 5 \times 60 = 4500m[1][3]=m[1]
[2]+m[3][3]+p0×p2×p3=1500+0+10×5×60=4500
o The minimum cost is:

m[1][3]=4500m[1][3] = 4500m[1][3]=4500

5. Final Cost Matrix:

css
Copy code
m = [ [0, 1500, 4500],
[∞, 0, 9000],
[∞, ∞, 0]
]

Complexity Analysis

 Time Complexity: The time complexity of the algorithm is O(n3)O(n^3)O(n3),


where nnn is the number of matrices. This arises from the three nested loops:
o One for the chain length,
o One for the starting matrix,
o One for the split point.
 Space Complexity: The space complexity is O(n2)O(n^2)O(n2) for storing the cost
matrix.

Applications

1. Compiler Optimization: Matrix chain multiplication is analogous to optimizing the


order of operations in expressions to minimize computation time.
2. Image Processing: Efficient matrix operations in computer graphics and image
transformations.
3. Numerical Methods: In numerical algorithms, matrix operations are often central,
and optimizing their computation can yield significant performance improvements.

Conclusion
Matrix Chain Multiplication is a fundamental problem in algorithm design that showcases the
power of dynamic programming. By breaking the problem into smaller subproblems and
utilizing optimal substructure, it provides an efficient method to determine the best order of
operations for matrix multiplication.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy