Matrix-Chain Multiplication as an optimization problem
Matrix-Chain Multiplication as an optimization problem
1
1. Introduction
i. What is Dynamic Programming?
ii. Elements of Dynamic Programming
2. Matrix-Chain Multiplication
i. Matrix-Chain Multiplication Problem
ii. Algorithms
iii. Examples
2
Introduction
3
What is Dynamic Programming?
4
What is Dynamic Programming?
5
Elements of Dynamic Programming
6
Matrix-Chain
Multiplication
7
Matrix-Chain Multiplication
8
Multiplication of Two Matrices
9
Example
10
Matrix-Chain Multiplication Problem
11
Dynamic Programming Approach
12
Dynamic Programming Approach (Contd.)
13
Dynamic Programming Approach (Contd.)
14
Dynamic Programming Approach (Contd.)
Input: Array p[0…n] containing matrix dimensions
and n
Result: Minimum-cost table m and split table s
Algorithm MatrixChainOrder(p[ ], n)
{
for i := 1 to n do
m[i, i] := 0;
for l := 2 to n do
for i := 1 to n-l+1 do
{ j := 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;
}
}
return m and s
}
15
Example
Matrix A1 A2 A3 A4 A5 A6
Dimensions 10x20 20x5 5x15 15x50 50x10 10x15
i\j 1 2 3 4 5 6
1 0
2 0
3 0
4 0
5 0
6 0
16
Example (Contd.)
i\j 1 2 3 4 5 6
1 0 1000
2 0 1500
3 0 3750
4 0 7500
5 0 7500
6 0
17
Example (Contd.)
Chains of length 3 require some minimization –
but only one each.
m[1,3]=min{(m[1,1]+m[2,3]+p0p1p3),(m[1,2]+m[3,3]+p0p2p3)}
= min{(0+1500+10x20x15), (1000+0+10x5x15)}
= min { 4500, 1750 } = 1750
m[2,4]=min{(m[2,2]+m[3,4]+p1p2p4),(m[2,3]+m[4,4]+p1p3p4)}
= min{(0+3750+20x5x50), (1500+0+20x15x50)}
= min { 8750, 16500 } = 8750
m[3,5]=min{(m[3,3]+m[4,5]+p2p3p5),(m[3,4]+m[5,5]+p2p4p5)}
= min{(0+7500+5x15x10), (3750+0+5x50x10)}
= min { 8250, 6250 } = 6250
m[4,6]=min{(m[4,4]+m[5,6]+p3p4p6),(m[4,5]+m[6,6]+p3p5p6)}
= min{(0+7500+15x50x15), (7500+0+15x10x15)}
= min { 18750, 9750 } = 9750
i\j 1 2 3 4 5 6
1 0 1000 1750
2 0 1500 8750
3 0 3750 6250
4 0 7500 9750
5 0 7500
6 0
18
Example (Contd.)
m[1,4]=min{(m[1,1]+m[2,4]+p0p1p4),(m[1,2]+m[3,4]+p0p2p4),
(m[1,3]+m[4,4]+p0p3p4)}
= min{(0+8750+10x20x50), (1000+3750+10x5x50),
(1750+0+10x15x50)}
= min { 18750, 7250, 9250 } = 7250
m[2,5]=min{(m[2,2]+m[3,5]+p1p2p5),(m[2,3]+m[4,5]+p1p3p5),
(m[2,4]+m[5,5]+p1p4p5)}
= min{(0+6250+20x5x10), (1500+7500+20x15x10),
(8750+0+20x50x10)}
= min { 7250, 12000, 18750 } = 7250
m[3,6]=min{(m[3,3]+m[4,6]+p2p3p6),(m[3,4]+m[5,6]+p2p4p6),
(m[3,5]+m[6,6]+p2p5p6)}
= min{(0+9750+5x15x15), (3750+7500+5x50x15),
(6250+0+5x10x15)}
= min { 10875, 15000, 7000 } = 7000
i\j 1 2 3 4 5 6
1 0 1000 1750 7250
2 0 1500 8750 7250
3 0 3750 6250 7000
4 0 7500 9750
5 0 7500
6 0
19
Example (Contd.)
m[1,5]=min{(m[1,1]+m[2,5]+p0p1p5),(m[1,2]+m[3,5]+p0p2p5),
(m[1,3]+m[4,5]+p0p3p5),(m[1,4]+m[5,5]+p0p4p5)}
= min{(0+7250+10x20x10), (1000+6250+10x5x10),
(1750+7500+10x15x10), (7250+0+10x50x10)}
= min { 9250, 7750, 10750, 12250 } = 7750
m[2,6]=min{(m[2,2]+m[3,6]+p1p2p6),(m[2,3]+m[4,6]+p1p3p6),
(m[2,4]+m[5,6]+p1p4p6),(m[2,5]+m[6,6]+p1p5p6)}
= min{(0+7000+20x5x15), (1500+9750+20x15x15),
(8750+7500+20x50x15), (7250+0+20x10x15)}
= min { 8500, 15750, 31,250, 10250 } = 8500
m[1,6]=min{(m[1,1]+m[2,6]+p0p1p6),(m[1,2]+m[3,6]+p0p2p6),
(m[1,3]+m[4,6]+p0p3p6),(m[1,4]+m[5,6]+p0p4p6),
(m[1,5]+m[6,6]+p0p5p6)}
= min{(11500, 8750, 13750, 22250, 9250 } = 8750
i\j 1 2 3 4 5 6
1 0 1000 1750 7250 7750 8750
2 0 1500 8750 7250 8500
3 0 3750 6250 7000
4 0 7500 9750
5 0 7500
6 0
20
Dynamic Programming Approach (Contd.)
The algorithm takes O(n3) time and requires O(n2)
space.
Step 4: Constructing an optimal solution
Our algorithm computes the minimum-cost
table m and the split table s.
The optimal solution can be constructed from
the split table s.
Each entry s[i, j ]=k shows where to split the
product Ai Ai+1 … Aj for the minimum cost.
The following recursive procedure prints an
optimal parenthesization.
Algorithm PrintOptimalPerens(s, i, j)
{ if (i=j) then
Print “A”i;
else
{ Print “(“;
PrintOptimalPerens(s, i, s[i,j]);
PrintOptimalPerens(s, s[i,j]+1, j);
Print “)”;
}
21
}
Example (Contd.)
So far we have decided that the best way to
parenthesize the expression results in 8750
multiplication.
But we have not addressed how we should
actually DO the multiplication to achieve the
value.
However, look at the last computation we did –
the minimum value came from computing
A = (A1A2)(A3A4A5A6)
then we set
s[i, j] = k.
22
Example (Contd.)
If we do this then we find that the s array looks
like this:
i\j 1 2 3 4 5 6
1 1 1 2 2 2 2
2 2 2 2 2 2
3 3 3 4 5
4 4 4 5
5 5 5
6 6
We already know that we must compute A1..2
and A3..6.
By looking at s[3,6] = 5, we discover that we
should compute A3..6 as A3..5A6..6 and then by
seeing that s[3,5] = 4, we get the final
parenthesization
A = ((A1A2)(((A3A4)A5)A6)).
23