Lec 12 - Dynamic Programming - Chain Matrix Problem
Lec 12 - Dynamic Programming - Chain Matrix Problem
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 Problem
Scalar Multiplications
8 11 2 3
9 10 2
x 4 9 0 1
2 x 3 x 4 = 24
10 8 1
2 1 4 2
3x4 multiplications
2x3
9*8+10*4+2*2 9*11+10*9+2*1 9*2+10*0+2*4 9*3+10*1+2*2
=
10*8+8*4+1*2 10*11+8*9+1*1 10*2+8*0+1*4 10*3+8*1+1*2
2x4
- - - -
- - - - - - -
x - - - - =
- - - - - - -
- - - -
pxq qxr pxr
pxqxr
multiplications
Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)
1. (A1(A2(A3A4)))
( A3 (8 x 3) . A4 (3 x 4) )
8x4
= 8*3*4 = 96
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3 ( A2 (5 x 8) . (A3 A4) (8 x 4) )
5x4
= 5*8*4 = 160
(A4)3 x 4
2. (A1((A2A3)A4))
( A2 (5 x 8) . A3 (8 x 3) )
5x3
= 5*8*3 = 120
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3 ( (A2 A3) (5x 3) . A4 (3 x 4) )
5x4
= 5*3*4 = 60
(A4)3 x 4
3. ((A1A2)(A3A4))
( A1 (3 x 5) . A2 (5 x 8) )
3x8
= 3*5*8 = 120
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3 ( A3 (8 x 3) . A4 (3 x 4) ) 8x4
= 8*3*4 = 96
(A4)3 x 4
4. ((A1(A2A3))A4)
( A2 (5 x 8) . A3 (8 x 3) )
5x3
= 5*8*3 = 120
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3 ( A1 (3 x 5) . (A2 A3) (5x 3) ) 3x3
= 3*5*3 = 45
(A4)3 x 4
5. (((A1A2)A3)A4)
( A1 (3 x 5) . A2 (5 x 8) )
5x3
= 3*5*8 = 120
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3 ( (A1 A2) (5x 3) . A3 (8 x 3) ) 5x3
= 5*8*3 = 120
(A4)3 x 4
Another Example:
A1 = 13 x 5
A2 = 5 x 89
A3 = 89 x 3
A4 = 3 x 34
Parenthesization Scalar multiplications
1. (A1(A2(A3A4))) 25528
2. (A1((A2A3)A4)) 4055
3. ((A1A2)(A3A4)) 54201
4. ((A1(A2A3))A4) 2856
5. (((A1A2)A3)A4) 10582
Matrix Chain Multiplication
Time Complexity (Brute-Force)
n P(n)
1 1
2 1
3 2
4 5
5 14
6 42
7 132
It is almost 4n
Matrix Chain Multiplication
Define Subproblems:
A1 A2 A3 A4 A5 - - - - - - An k=1
A1 A2 A3 A4 - - - - - -
An-1 An k = n-1
A1..4
A1 A2 A3 A4
A1 A2 A3 A4 A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
A1 A2 A3 A4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
A1..4
A2 A3 A4
A2 A3 A4
A1 A2 A3 A4
A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
A1..4
A3..3 A4..4
A3 A4 A3 A4
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
A1..4
A2 A3 A2 A3
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
A1..4
A1 A2 A1 A2
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
A1..4
A3 A4 A3 A4
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
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
A1 A2 A3
A1 A2 A3
A1 A2 A3 A4
A1 A2 A3
p0xp1 p1xp2 p2xp3 p3xp4
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
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
Combine Step
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
0 96
Combine Step
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
0 96
Combine Step
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
0 96
Combine Step
A1..4
A2..3 A4..4 A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
120
Combine Step
A1..4
A2..3 A4..4 A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
120 0
Combine Step
A1..4
Combine Step
A1..4
Combine Step
A1..4
( 240 , ? , ?)
A1..1 A2..4 A1..2 A3..4 A1..3 A4..4
0 180
Combine Step
A1..4
( 240 , ? , ?)
A1..2 A3..4 A1..3 A4..4
120
A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
0 0
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3
(A4)3 x 4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
Combine Step
A1..4
( 240 , ? , ?)
A1..2 A3..4 A1..3 A4..4
120 96
A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
0 0
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3
(A4)3 x 4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
Combine Step
A1..4
( 240 , ? , ?)
A1..2 A3..4 A1..3 A4..4
120 96
A1..1 A2..3 A1..2 A3..3
Combine Step
A1..4
( 240 , 312 , ?)
A1..2 A3..4 A1..3 A4..4
120 96
A1..1 A2..3 A1..2 A3..3
Combine Step
A1..4
( 240 , 312 , ?)
A1..3 A4..4
Combine Step
A1..4
( 240 , 312 , ?)
A1..3 A4..4
(165 , ? )
A1..1 A2..2
Combine Step
A1..4
( 240 , 312 , ?)
A1..3 A4..4
(165 , ? )
A1..2 A3..3
120
A1..1 A2..2
0 0
(A1)3 x 5 A1..3 = mul(A1..1) + mul(A2..3 ) + mul [(A1..1)3x5 . (A2..3)5x3]
(A2)5 x 8 = 0 + 120 + 3*5*3
(A3)8 x 3 (A1..3) = 0 + 120 + 45 = 165
(A4)3 x 4 3x3
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
Combine Step
A1..4
( 240 , 312 , ?)
A1..3 A4..4
(165 , ? )
A1..2 A3..3
120 0
Combine Step
A1..4
( 240 , 312 , ?)
A1..3 A4..4
(165 , 192 )
A1..2 A3..3
120 0
Combine Step
A1..4
( 240 , 312 , ?)
A1..3 A4..4
(165 , 192 )
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3
(A4)3 x 4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
Combine Step
A1..4
( 240 , 312 , ?)
A1..3 A4..4
165 0
Combine Step
A1..4
( 240 , 312 , 201 )
A1..3 A4..4
165 0
Combine Step
A1..4
( 240 , 312 , 201 )
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3
(A4)3 x 4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)
Combine Step
A1..4
201
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3
(A4)3 x 4
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
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
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
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..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..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..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..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..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
Matrix Chain Problem (Divide and Conquer)
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). The subproblems are not independent,
but instead they overlap (hence, should be constructed bottom-up).
Matrix Chain Multiplication
Time Complexity (Divide and Conquer)
T (1) 1,
n 1
T (n) 1 (T (k ) T (n k ) 1) for n 1
k 1
n 1
2 T (i ) n
i 1
Matrix Chain Multiplication
Time Complexity (Divide and Conquer)
n 1
i
T ( n) 2 2
i 1
n2
2 2i n
i 0
n 1
2(2 1) n
n 1
2
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), we don’t use recursion.
Dynamic Programming solves each problem just once (Saves its result in a table)
Dynamic Programing – General Technique
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
In an Optimization Problem :
There are many possible solutions (feasible solutions)
Want to find an optimal solution to the problem
Wrong to say “the” optimal solution to the problem
There may be several solutions with the same optimal value
Dynamic Programing – Principle of Optimality
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
Dynamic Programming - Introduction
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 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
79
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
80
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
81
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
82
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 87
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 88
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 89
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 90
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
6
91
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750 7875 9375
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
6
92
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750 7875 9375
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
6
93
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750 7875 9375
0 1 2 3 4 5 6
2625
P 30 35 15 5 10 20 25 2 0 4375 7125
3 0 750 2500
i≤k<j 0
5 5000
6
94
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750 7875 9375
0 1 2 3 4 5 6
2625
P 30 35 15 5 10 20 25 2 0 4375 7125
3 0 750 2500
i≤k<j 0
5 5000
6
95
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750 7875 9375
0 1 2 3 4 5 6
2625
P 30 35 15 5 10 20 25 2 0 4375 7125
i≤k<j 0
5 5000
6
96
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
P 30 35 15 5 10 20 25 2 0 2625
4375 7125
i≤k<j 0
5 5000
5 5
= min { 28175 , 27250 , 11875 , 15375 }
= 11875 6
S[1,5] = 3
97
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
P 30 35 15 5 10 20 25 2 0 2625
4375 7125
i≤k<j 0
5 5000
5 5
= min { 28175 , 27250 , 11875 , 15375 }
= 11875 6
S[1,5] = 3
98
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
P 30 35 15 5 10 20 25 2 0 2625
4375 7125
i≤k<j 0
5 5000
P 30 35 15 5 10 20 25 2 0 2625
4375 7125 10500
i≤k<j 0
5 5000
P 30 35 15 5 10 20 25 2 0 2625
4375 7125 10500
i≤k<j 0
5 5000
P 30 35 15 5 10 20 25 2 0 2625
4375 7125 10500
i≤k<j 0
5 5000
m s
1 1
6 6
15125 2 2
5
3 i
5 i
10500 3 j 3
j 11875 4 3 3
4
7125 575 4 3 4
9375 3 3
3
3 5
4375 2500 3500 5 5
7875 1 3 3
2 2
2625 750 1000 5000 6 4 5
15750 1 2 3
1
0 0 0
0 0 0
A1 A2 A3 A4 A5 A6
103
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 )
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
104
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
105
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
106
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
107
Matrix-Chain multiplication
Memoization
108
Matrix-Chain multiplication
Memoization (cont.)
109
Matrix-Chain multiplication
1 MEMOIZED-MATRIX-CHAIN(p)
2 n←length[p]-1
3 for i←1 to n
4 do for j←i to n
do m[i,j] ←∞
return LOOKUP-CHAIN(p,1,n)
110
Matrix-Chain multiplication
Memoization (cont.)
LOOKUP-CHAIN(p,1,n)
1 if m[i,j] < ∞
2 then return m[i,j]
3 if i=j
4 then m[i,j] ←0
5 else for k←1 to j-1
6 do q← LOOKUP-CHAIN(p,i,k)
+ LOOKUP-CHAIN(p,k+1,j) + pi-1 pk pj
7 if q < m[i,j]
8 then m[i,j] ←q
9 return m[i,j]
94
Matrix Chain Multiplication
3. Optimal Solution (DP-Bottom Up)
PRINT-OPTIMAL-PARENS (s, i, j)
1 if i=j
2 then print “Ai”
3 else print “ ( “
4 PRINT-OPTIMAL-PARENS (s, i, s[i,j])
5 PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
6 Print “ ) ”
112
Matrix Chain Multiplication
3. Optimal Solution (DP-Bottom Up)
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT: S 1 2 3 4 5 6
1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 113
Matrix Chain Multiplication
3. Optimal Solution (DP-Bottom Up)
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 114
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “
POP ( s, 1, s[1,3 ] )
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 )
Print “ ) ”
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 115
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “
POP ( s, 1, s[1,3 ] )
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 )
Print “ ) ”
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
(( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 116
POP ( s, 1 , 1 ) Matrix-Chain multiplication
i=1 j=1 Dynamic Programming
then Print “ Ai”
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “
POP ( s, 1, s[1,3 ] )
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 )
Print “ ) ”
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
(( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 117
POP ( s, 1 , 1 ) Matrix-Chain multiplication
i=1 j=1 Dynamic Programming
Print “ Ai”
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “
POP ( s, 1, s[1,3 ] )
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 )
Print “ ) ”
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 118
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “ POP ( s, 2 , 3 )
i=2 j=3
POP ( s, 1, s[1,3 ] )
print “ ( “
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 ) POP ( s, 2, s[ 2, 3 ] )
Print “ ) ” POP ( s, s[ 2, 3 ]+1, 3 )
i=1 j=6
Print “ ) ”
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 119
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “ POP ( s, 2 , 3 )
i=2 j=3
POP ( s, 1, s[1,3 ] )
print “ ( “
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 ) POP ( s, 2, s[ 2, 3 ] )
Print “ ) ” POP ( s, s[ 2, 3 ]+1, 3 )
i=1 j=6
Print “ ) ”
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 ( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 120
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 2 , 2 )
POP ( s, 1 , 3 )
i=2 j=2
i=1 j=3 Print “ Ai”
print “ ( “ POP ( s, 2 , 3 )
i=2 j=3
POP ( s, 1, s[1,3 ] )
print “ ( “
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 ) POP ( s, 2, s[ 2, 3 ] )
Print “ ) ” POP ( s, s[ 2, 3 ]+1, 3 )
i=1 j=6
Print “ ) ”
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 ( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 121
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 2 , 2 )
POP ( s, 1 , 3 )
i=2 j=2
i=1 j=3 Print “ Ai”
print “ ( “ POP ( s, 2 , 3 )
i=2 j=3
POP ( s, 1, s[1,3 ] )
print “ ( “
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 ) POP ( s, 2, s[ 2, 3 ] )
Print “ ) ” POP ( s, s[ 2, 3 ]+1, 3 )
i=1 j=6
Print “ ) ”
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 122
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “ POP ( s, 2 , 3 )
i=2 j=3
POP ( s, 1, s[1,3 ] )
print “ ( “
POP ( s, s[1,3]+1,3) POP ( s, 3 , 3 )
POP ( s, 1 , 6 ) POP ( s, 2, s[ 2, 3 ] )
Print “ ) ” POP ( s, s[ 2, 3 ]+1, 3 ) i=3 j=3
i=1 j=6
Print “ ) ” Print “ Ai”
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 123
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “ POP ( s, 2 , 3 )
i=2 j=3
POP ( s, 1, s[1,3 ] )
print “ ( “
POP ( s, s[1,3]+1,3) POP ( s, 3 , 3 )
POP ( s, 1 , 6 ) POP ( s, 2, s[ 2, 3 ] )
Print “ ) ” POP ( s, s[ 2, 3 ]+1, 3 ) i=3 j=3
i=1 j=6
Print “ ) ” Print “ Ai”
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 124
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “ POP ( s, 2 , 3 )
i=2 j=3
POP ( s, 1, s[1,3 ] )
print “ ( “
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 ) POP ( s, 2, s[ 2, 3 ] )
Print “ ) ” POP ( s, s[ 2, 3 ]+1, 3 )
i=1 j=6
Print “ ) ”
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 125
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “ POP ( s, 2 , 3 )
i=2 j=3
POP ( s, 1, s[1,3 ] )
print “ ( “
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 ) POP ( s, 2, s[ 2, 3 ] )
Print “ ) ” POP ( s, s[ 2, 3 ]+1, 3 )
i=1 j=6
Print “ ) ”
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 126
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “
POP ( s, 1, s[1,3 ] )
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 )
Print “ ) ”
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 127
Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 3 )
i=1 j=3
print “ ( “
POP ( s, 1, s[1,3 ] )
POP ( s, s[1,3]+1,3)
POP ( s, 1 , 6 )
Print “ ) ”
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 128
Matrix-Chain multiplication Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “ POP ( s, 4 , 6 )
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 129
Matrix-Chain multiplication Matrix-Chain multiplication
Dynamic Programming
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “ POP ( s, 4 , 6 )
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 130
Matrix-Chain multiplication Matrix-Chain multiplication
Dynamic Programming
POP ( s, 4 , 5 )
i=4 j=5
POP ( s, 1 , 6 ) print “ ( “
POP ( s, 4, s[ 4, 5 ] )
i=1 j=6
POP ( s, s[ 4, 5 ]+1,5)
print “ ( “ POP ( s, 4 , 6 ) Print “ ) ”
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 131
Matrix-Chain multiplication Matrix-Chain multiplication
Dynamic Programming
POP ( s, 4 , 5 )
i=4 j=5
POP ( s, 1 , 6 ) print “ ( “
POP ( s, 4, s[ 4, 5 ] )
i=1 j=6
POP ( s, s[ 4, 5 ]+1,5)
print “ ( “ POP ( s, 4 , 6 ) Print “ ) ”
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 132
Matrix-Chain multiplication Matrix-Chain multiplication
Dynamic Programming
POP ( s, 4 , 4 )
i=4 j=4
POP ( s, 4 , 5 )
Print “ Ai”
i=4 j=5
POP ( s, 1 , 6 ) print “ ( “
POP ( s, 4, s[ 4, 5 ] )
i=1 j=6
POP ( s, s[ 4, 5 ]+1,5)
print “ ( “ POP ( s, 4 , 6 ) Print “ ) ”
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 133
Matrix-Chain multiplication Matrix-Chain multiplication
Dynamic Programming
POP ( s, 4 , 4 )
i=4 j=4
POP ( s, 4 , 5 )
Print “ Ai”
i=4 j=5
POP ( s, 1 , 6 ) print “ ( “
POP ( s, 4, s[ 4, 5 ] )
i=1 j=6
POP ( s, s[ 4, 5 ]+1,5)
print “ ( “ POP ( s, 4 , 6 ) Print “ ) ”
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 134
Matrix-Chain multiplication Matrix-Chain multiplication
Dynamic Programming
POP ( s, 4 , 5 )
i=4 j=5
POP ( s, 1 , 6 ) print “ ( “
POP ( s, 4, s[ 4, 5 ] )
i=1 j=6 POP ( s, 5 , 5 )
POP ( s, s[ 4, 5 ]+1,5)
print “ ( “ POP ( s, 4 , 6 ) Print “ ) ” i=5 j=5
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6 Print “ Ai”
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 135
Matrix-Chain multiplication Matrix-Chain multiplication
Dynamic Programming
POP ( s, 4 , 5 )
i=4 j=5
POP ( s, 1 , 6 ) print “ ( “
POP ( s, 4, s[ 4, 5 ] )
i=1 j=6 POP ( s, 5 , 5 )
POP ( s, s[ 4, 5 ]+1,5)
print “ ( “ POP ( s, 4 , 6 ) Print “ ) ” i=5 j=5
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6 Print “ Ai”
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 136
Matrix-Chain multiplication
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 137
Matrix-Chain multiplication
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 138
Matrix-Chain multiplication
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “ POP ( s, 4 , 6 )
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6) POP ( s, 6 , 6 )
i=6 j=6
Print “ ) ”
Print “ Ai”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 139
Matrix-Chain multiplication
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “ POP ( s, 4 , 6 )
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6) POP ( s, 6 , 6 )
i=6 j=6
Print “ ) ”
Print “ Ai”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 ) A6 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 140
Matrix-Chain multiplication
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “ POP ( s, 4 , 6 )
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 ) A6 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 141
Matrix-Chain multiplication
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “ POP ( s, 4 , 6 )
POP ( s, 1, s[ 1, 6 ] ) i=4 j=6
POP ( s, s[ 1, 6 ]+1, 6 ) print “ ( “
Print “ ) ” POP ( s, 4, s[ 4, 6 ] )
POP ( s, s[ 4, 6 ]+1,6)
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 ) A6 ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 142
Matrix-Chain multiplication
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 ) A6 ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 143
Matrix-Chain multiplication
POP ( s, 1 , 6 )
i=1 j=6
print “ ( “
POP ( s, 1, s[ 1, 6 ] )
POP ( s, s[ 1, 6 ]+1, 6 )
Print “ ) ”
OUTPUT:
S 1 2 3 4 5 6
( ( A1 (A2 A3 ) ) ( ( A4 A5 ) A6 ) ) 1 1 1 3 3 3
PRINT-OPTIMAL-PARENS (s, i, j) 3
2 2 3 3
if i=j
3 3 3 3
then print “Ai”
else print “ ( “ 4 4 5
PRINT-OPTIMAL-PARENS (s, i, s[i,j]) 5
5
PRINT-OPTIMAL-PARENS (s, s[i,j]+1, j)
Print “ ) ” 6 144
Dynamic Programming - History
• Bellman.
Pioneered the systematic study of dynamic programming
in the 1950s.
• Etymology.
– Dynamic programming = planning over time.
– Secretary of Defense was hostile to mathematical research.
– Bellman sought an impressive name to avoid confrontation.
• "it's impossible to use dynamic in a pejorative sense"
• "something not even a Congressman could object to"
Dynamic Programming Applications
• Areas
– Bioinformatics.
– Control theory.
– Information theory.
– Operations research.
– Computer science: theory, graphics, AI, systems, ….
146
Matrix-Chain multiplication
32