Matrix Chain Problem - DP
Matrix Chain Problem - DP
Problem:
Given a sequence of matrices A1 A2…An , and dimensions p0 p1…pn
where Ai is of dimension pi-1 x pi , determine multiplication
sequence that minimizes the number of operations.
Note:
This algorithm does not perform the multiplication, it just figures out
the best order in which to perform the multiplication.
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
A1..4
A2..2 A3..4 A2..3 A4..4 A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
A divide-and-conquer approach usually generates brand new problems (i.e., subproblems are
independent) at each step of recursion. In solving problems with overlapping subproblems, A
Divide And Conquer algorithm does redundant work (i.e., Repeatedly solves common
subproblems)
Since subproblems overlap (as shown in the following tree), we don’t use recursion.
Matrix Chain Multiplication
1. Overlapping Subproblems
2. Optimal substructure
Dynamic Programing – General Technique
Overlapping Subproblems
When a recursive algorithm revisits the same problem over and over again,
we say that the optimization problem has overlapping subproblems
(i.e., subproblems share subsubproblems).
Optimal Substructure
Optimization Problems
Principle of optimality
In an optimal sequence of decisions or choices, each subsequence must be
optimal
or
the optimal solution to the problem contains within it the optimal solution
to subproblems
Matrix Chain Multiplication
Dynamic Programming (Dynamic Programming)
A1 A2 A3 A4 A5 - - - - - - An
p0xp1 p1xp2 p2xp3 p3xp4 p4xp5 - - - - - Pn-1xpn
Matrix Chain Multiplication
1. Establish a Recursive Property (Dynamic Programming)
A1 A2 A3 A4 A5 - - - - - - An
p0xp1 p1xp2 p2xp3 p3xp4 p4xp5 - - - - - Pn-1xpn
Matrix Chain Multiplication
1. Establish a Recursive Property (Dynamic Programming)
A1 A2 A3 A4 A5 - - - - - - An k=3
-- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
A1 A2 A3 A4 - - - - - -
An-1 An k = n-1
A1 A2 A3 A4 A5 - - - - - - An
p0xp1 p1xp2 p2xp3 p3xp4 p4xp5 - - - - - Pn-1xpn
Matrix Chain Multiplication
1. Establish a Recursive Property (Dynamic Programming)
Min. of these
A1 A2 A3 A4 A5 - - - - - - An k=1
A1 A2 A3 A4 A5 - - - - - - An k=2
A1 A2 A3 A4 A5 - - - - - - An k=3
-- -- -- -- -- -- -- --
-- -- -- -- -- -- -- --
A1 A2 A3 A4 - - - - - -
An-1 An k = n-1
opt. mul (Ai..j) = min ( mul (Ai..k) + mul (Ak+1..j) + # of mul ( Ai..k ) · ( Ak+1..j ) )
i≤k<j
Let
m[i, j] = minimum # of multiplications needed to compute Ai..j (Ai Ai+1 Aj)
m[i,k] = minimum # of scalar multiplications needed to compute Ai..k
m[k+1,j] = minimum # scalar multiplications needed to compute Ak+1..j
opt. mul (Ai..j) = min ( mul (Ai..k) + mul (Ak+1..j) + # of mul ( Ai..k ) · ( Ak+1..j ) )
i≤k<j
an optimal paranthesization.
s[i, j] = k
Matrix Chain Multiplication
Subproblem Graph – Top Down (Dynamic Programming)
A1..4
A2..2 A3..4 A2..3 A4..4 A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
A1..4
A1..3 A2..4
Example:
matrix dimension
A1 30 x 35
A2 35 x 15
A3 15 x 5
A4 5 x 10
A5 10 x 20
A6 20 x 25
Order Matrix P :
0 1 2 3 4 5 6
P 30 35 15 5 10 20 25
19
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1
0 1 2 3 4 5 6
P 30 35 15 5 10 20 25 2
3
m[ i, j ] = 0 for i=j 4
5
m[1,1] = 0 6
m[2,2] = 0
m[3,3] = 0 1 2 3 4 5 6
m[4,4] = 0 S 1
m[5,5] = 0
m[6,6] = 0 2
3
4
5
6
20
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0
0 1 2 3 4 5 6
P 30 35 15 5 10 20 25 2 0
3 0
m[ i, j ] = 0 for i=j 4 0
5 0
m[1,1] = 0 6 0
m[2,2] = 0
m[3,3] = 0 1 2 3 4 5 6
m[4,4] = 0 S 1
m[5,5] = 0
m[6,6] = 0 2
3
4
5
6
21
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0
0 1 2 3 4 5 6
P 30 35 15 5 10 20 25 2 0
3 0
m[2,3] 1 2 3 4 5 6
m[3,4] S 1
m[4,5] 2
3
m[5,6]
4
5
6
22
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0
0 1 2 3 4 5 6
P 30 35 15 5 10 20 25 2 0
3 0
3 0
3 0
i≤k<j 0
5 5000
3 0 750
i≤k<j 0
5 5000
2
= min ( 7875 , 18000 ) = 7875 S[1,3] = 1 2
m[2, 4] = min { m[2, 2] + m[3, 4] + p1 p2 p4 , 3 3
2 ≤ k < 4 m [2, 3]+m[4, 4] + p1 p3 p4 } 4 4
= min { 0 + 750 + 35x15x10 , 5 5
2625 + 0 + 35x5x10 }
6
= min ( 6000, 4375 ) = 4375 S[1,3] = 3 27
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750 7875
0 1 2 3 4 5 6
2625
P 30 35 15 5 10 20 25 2 0 4375
3 0 750
i≤k<j 0
5 5000
2 3
= min ( 7875 , 18000 ) = 7875 S[1,3] = 1 2
m[2, 4] = min { m[2, 2] + m[3, 4] + p1 p2 p4 , 3 3
2 ≤ k < 4 m [2, 3]+m[4, 4] + p1 p3 p4 } 4 4
= min { 0 + 750 + 35x15x10 , 5 5
2625 + 0 + 35x5x10 }
6
= min ( 6000, 4375 ) = 4375 S[1,3] = 3 28
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750 7875
0 1 2 3 4 5 6
2625
P 30 35 15 5 10 20 25 2 0 4375
3 0 750
i≤k<j 0
5 5000
3 3
m[4, 6] = min { m[4, 4] + m[5, 6] + p3 p4 p6 ,
4 4
4 ≤ k < 6 m[4, 5]+m[6, 6] + p3 p5 p6 }
5 5
= min { 0 + 5000 + 5x10x25 ,
1000 + 0 + 5x20x25} 6
= min ( 6250, 3500) = 3500 S[4,6] = 5 29
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750 7875
0 1 2 3 4 5 6
2625
P 30 35 15 5 10 20 25 2 0 4375
3 0 750 2500
i≤k<j 0
5 5000
3 3 3
m[4, 6] = min { m[4, 4] + m[5, 6] + p3 p4 p6 ,
4 4 5
4 ≤ k < 6 m[4, 5]+m[6, 6] + p3 p5 p6 }
5 5
= min { 0 + 5000 + 5x10x25 ,
1000 + 0 + 5x20x25} 6
= min ( 6250, 3500) = 3500 S[4,6] = 5 30
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
0 1 2 3 4 5 6 m 1 0 15750 7875 9375 11875 15125
P 30 35 15 5 10 20 25 2 0 2625
4375 7125 10500
i≤k<j 0
5 5000
( A1 A 2 A 3 A4 A 5 A6 )
( A1 A 2 A 3 ) ( A4 A 5 A6 )
1 2 3 4 5 6
S 1 1 1 3 3 3
2 2 3 3 3
3 3 3 3
4 4 5
5 5
6
32
Matrix Chain Multiplication
3. Optimal Solution (DP-Bottom Up)
( A1 A 2 A 3 A4 A 5 A6 )
( A1 A 2 A 3 ) ( A4 A 5 A6 )
( ( A1 ( A 2 A 3 ) ) ( A4 A 5 A6 )
1 2 3 4 5 6
S 1 1 1 3 3 3
2 2 3 3 3
3 3
4 4 5
5 5
6
33
Matrix Chain Multiplication
3. Optimal Solution (DP-Bottom Up)
( A1 A 2 A 3 A4 A 5 A6 )
( A1 A 2 A 3 ) ( A4 A 5 A6 )
( ( A1 ( A 2 A 3 ) ) ( A4 A 5 A6 )
1 2 3 4 5 6
S 1 1 1 3 3 3
( ( A1 ( A 2 A 3 ) ) ( ( A4 A 5 ) A6 )
2 2 3 3 3
3 3
4 4 5
5 5
6
34
Matrix Chain Multiplication
3. Optimal Solution (DP-Bottom Up)
( A1 A 2 A 3 A4 A 5 A6 ) OUTPUT:
( ( A1 (A2 A3 ) ) ( ( A4 A5 ) A6 ) )
( A1 A 2 A 3 ) ( A4 A 5 A6 )
( ( A1 ( A 2 A 3 ) ) ( A4 A 5 A6 )
1 2 3 4 5 6
S 1 1 1 3 3 3
( ( A1 ( A 2 A 3 ) ) ( ( A4 A 5 ) A6 )
2 2 3 3 3
3 3
4 4 5
5 5
6
35