Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
15 views

Lec 12 - Dynamic Programming - Chain Matrix Problem

The document describes the matrix chain multiplication problem and different approaches to solve it, including a brute force enumeration approach. The problem is to determine the optimal order of multiplying a sequence of matrices to minimize the number of operations. It can be solved using dynamic programming to store solutions to subproblems in a table for lookup. The document provides examples of calculating the number of operations for different parenthesization orders of multiplying the matrices using a brute force approach.

Uploaded by

f2021266018
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views

Lec 12 - Dynamic Programming - Chain Matrix Problem

The document describes the matrix chain multiplication problem and different approaches to solve it, including a brute force enumeration approach. The problem is to determine the optimal order of multiplying a sequence of matrices to minimize the number of operations. It can be solved using dynamic programming to store solutions to subproblems in a table for lookup. The document provides examples of calculating the number of operations for different parenthesization orders of multiplying the matrices using a brute force approach.

Uploaded by

f2021266018
Copyright
© © All Rights Reserved
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 147

Matrix Chain 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

A. Enumeration Approach (Brute Force)

B. Divide and Conquer (Recursive Approach)

C. Dynamic Programming (Table Driven Approach)


Matrix Chain Multiplication

O(4n) - Brute Force


Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)

Given a sequence of matrices A1 A2…A4 , and dimensions p0 p1…p4 where Ai


is of dimension pi-1 x pi ,
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3
(A4)3 x 4
we have five possible ways to compute the product.
1. (A1(A2(A3A4)))
It should be noted that matrix multiplication is associative,
2. (A1((A2A3)A4)) but not commutative. We always have:
3. ((A1A2)(A3A4)) ((AB)(CD)) = (A(B(CD))) or
any other grouping as long as the matrices are in the same
4. ((A1(A2A3))A4) consecutive order.
5. (((A1A2)A3)A4) BUT NOT: ((AB)(CD)) = ((BA)(DC))
Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)

Given a sequence of matrices A1 A2…A4 , and dimensions p0 p1…p4 where Ai


is of dimension pi-1 x pi ,
(A1)3 x 5
(A2)5 x 8
(A3)8 x 3
(A4)3 x 4
we have five possible ways to compute the product.
1. (A1(A2(A3A4)))
 Try all possible ways to parenthesize A = A1*A2*A3*A4
2. (A1((A2A3)A4))
3. ((A1A2)(A3A4))  Calculate number of operations for each one
4. ((A1(A2A3))A4)
 Pick the one that is best
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)

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

( A1 (3 x 5) . ( A2 (A3 A4)) (5 x 4) ) = 3*5*4 = 60


3x4
----------------------
1. (A1(A2(A3A4))) = 316 = 316
2. (A1((A2A3)A4))
3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)

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

( A1 (3 x 5) . ( (A2 A3)A4 ) (5 x 4) ) = 3*5*4 = 60


3x4
----------------------
1. (A1(A2(A3A4))) = 316 = 240
2. (A1((A2A3)A4)) = 240
3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)

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

( (A1 A2) (3 x 8) . (A3 A4) (8 x 4) ) = 3*8*4 = 96


3x4
----------------------
1. (A1(A2(A3A4))) = 316 = 312
2. (A1((A2A3)A4)) = 240
3. ((A1A2)(A3A4)) = 312
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)

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

( (A1 (A2 A3) (3 x 3) . A4 (3 x 4) ) = 3*3*4 = 36


3x4
----------------------
1. (A1(A2(A3A4))) = 316 = 201
2. (A1((A2A3)A4)) = 240
3. ((A1A2)(A3A4)) = 312
4. ((A1(A2A3))A4) = 201
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)

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

( ( (A1 A2)A3 ) (5 x 3) . A4 (3 x 4) ) = 5*3*4 = 60


5x4
----------------------
1. (A1(A2(A3A4))) = 316 = 300
2. (A1((A2A3)A4)) = 240
3. ((A1A2)(A3A4)) = 312
4. ((A1(A2A3))A4) = 201 4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4) = 300
Matrix Chain Multiplication
Matrix Chain Problem (Brute-Force)

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)

Let P(n) be the number of ways to compute multiplication of A = A0*A1*…*An-1

n P(n)
1 1
2 1
3 2
4 5
5 14
6 42
7 132

It is almost 4n
Matrix Chain Multiplication

O(2n) – Divide and Conquer


Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Define Subproblems:
A1 A2 A3 A4 A5 - - - - - - An k=1

A1 A2 A3 A4 A5 - - - - - - An k=2 We need to cut the chain A1,, An


somewhere in the middle, i.e.,pick
A1 A2 A3 A4 A5 An k=3
- - - - - -
some k, where 1  k < n
-- -- -- -- -- -- -- --
All Possible Cuttings
-- -- -- -- -- -- -- --

A1 A2 A3 A4 - - - - - -
An-1 An k = n-1

A1..n = ( A1..k )( Ak+1..n ) 1 ≤ k < n


Generalization:
A1 A2 A3 A4 A5 - - - - - - An
Ai..j = ( Ai..k )( Ak+1..j ) i ≤ k < j p0xp1 p1xp2 p2xp3 p3xp4 p4xp5 - - - - - Pn-1xpn
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4

A2..2 A3..4 A2..3 A4..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

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4

A2..2 A3..4 A2..3 A4..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

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4

A2..2 A3..4 A2..3 A4..4

A3..3 A4..4 A2..2 A3..3

A2 A3 A2 A3
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4

A2..2 A3..4 A2..3 A4..4 A1..1 A2..2

A3..3 A4..4 A2..2 A3..3

A1 A2 A1 A2
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4

A2..2 A3..4 A2..3 A4..4 A1..1 A2..2 A3..3 A4..4

A3..3 A4..4 A2..2 A3..3

A3 A4 A3 A4
A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..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

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2

A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4


0

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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2


0 0

(A1)3 x 5 A3..4 = mul(A3..3) + mul(A4..4 ) + mul [ (A3)8x3 (A4)3x4 ]


(A2)5 x 8 + 8*3*4
= 0 + 0
(A3)8 x 3 (A3..4) = 96
(A4)3 x 4 8x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4


0

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

A2..2 A3..3 A2..2 A3..3 A1..1 A2..2

(A1)3 x 5 There are two ways to compute multiplication of A2..4


(A2)5 x 8 1. A2 A3 A4 Solve both and
(A3)8 x 3 select minimum of the two for A2..4 result
2. A2 A3 A4
(A4)3 x 4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4


0 ( 256 , ? )

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

A2..2 A3..3 A2..2 A3..3 A1..1 A2..2

(A1)3 x 5 A2..4 = mul(A2..2) + mul(A3..4 ) + mul [(A2..2)5x8 . (A3..4)8x4]


(A2)5 x 8 = 0 + 96 + 5*8*4
(A3)8 x 3 (A2..4) = 96 + 160 = 256
(A4)3 x 4 5x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4


0 ( 256 , ? )

A2..3 A4..4 A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
120

A2..2 A3..3 A2..2 A3..3 A1..1 A2..2


0 0

(A1)3 x 5 A2..3 = mul(A2..2) + mul(A3..3 ) + mul [(A2..2)5x8 . (A3..3)8x3]


(A2)5 x 8 = 0 + 0 + 5*8*3
(A3)8 x 3 (A2..3) = 120
(A4)3 x 4 5x3
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4


0 ( 256 , ? )

A2..3 A4..4 A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
120 0

A2..2 A3..3 A1..1 A2..2

(A1)3 x 5 A2..4 = mul(A2..3) + mul(A4..4 ) + mul [(A2..3)5x3 . (A4..4)3x4]


(A2)5 x 8 = 120 + 0 + 5*3*4
(A3)8 x 3 (A2..4) = 120 + 60 = 180
(A4)3 x 4 5x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4


0 ( 256 , 180 )

A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3

A2..2 A3..3 A1..1 A2..2

(A1)3 x 5 A2..4 = mul(A2..3) + mul(A4..4 ) + mul [(A2..3)5x3 . (A4..4)3x4]


(A2)5 x 8 = 120 + 0 + 5*3*4
(A3)8 x 3 (A2..4) = 120 + 60 = 180
(A4)3 x 4 5x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..4


0 180
(A2A3)(A4)
A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3
Keep Track

A2..2 A3..3 A1..1 A2..2

(A1)3 x 5 A2..4 = mul(A2..3) + mul(A4..4 ) + mul [(A2..3)5x3 . (A4..4)3x4]


(A2)5 x 8 = 120 + 0 + 5*3*4
(A3)8 x 3 (A2..4) = 120 + 60 = 180
(A4)3 x 4 5x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4
( 240 , ? , ?)
A1..1 A2..4 A1..2 A3..4 A1..3 A4..4
0 180

A1..1 A2..2 A3..3 A4..4 A1..1 A2..3 A1..2 A3..3

A2..2 A3..3 A1..1 A2..2

(A1)3 x 5 A1..4 = mul(A1..1) + mul(A2..4 ) + mul [(A1..1)3x5 . (A2..4)5x4]


(A2)5 x 8 = 0 + 180 + 3*5*4
(A3)8 x 3 (A1..4) = 180 + 60 = 240
(A4)3 x 4 3x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

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

A2..2 A3..3 A1..1 A2..2

(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

A2..2 A3..3 A1..1 A2..2

(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

A2..2 A3..3 A1..1 A2..2

(A1)3 x 5 A1..4 = mul(A1..2) + mul(A3..4 ) + mul [(A1..2)3x8 . (A3..4)8x4]


(A2)5 x 8 = 120 + 96 + 3*8*4
(A3)8 x 3 (A1..4) = 120 + 96 + 96 = 312
(A4)3 x 4 3x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4
( 240 , 312 , ?)
A1..2 A3..4 A1..3 A4..4
120 96
A1..1 A2..3 A1..2 A3..3

A2..2 A3..3 A1..1 A2..2

(A1)3 x 5 A1..4 = mul(A1..2) + mul(A3..4 ) + mul [(A1..2)3x8 . (A3..4)8x4]


(A2)5 x 8 = 120 + 96 + 3*8*4
(A3)8 x 3 (A1..4) = 120 + 96 + 96 = 312
(A4)3 x 4 3x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4
( 240 , 312 , ?)
A1..3 A4..4

A1..1 A2..3 A1..2 A3..3


0 120

A2..2 A3..3 A1..1 A2..2


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 , 312 , ?)
A1..3 A4..4
(165 , ? )

A1..1 A2..3 A1..2 A3..3


0 120

A1..1 A2..2

(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

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

(A1)3 x 5 A1..3 = mul(A1..2) + mul(A3..3 ) + mul [(A1..2)3x8 . (A3..3)8x3]


(A2)5 x 8 = 120 + 0 + 3*8*3
(A3)8 x 3 (A1..3) = 0 + 120 + 72 = 192
(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 , 192 )

A1..2 A3..3
120 0

(A1)3 x 5 A1..3 = mul(A1..2) + mul(A3..3 ) + mul [(A1..2)3x8 . (A3..3)8x3]


(A2)5 x 8 = 120 + 0 + 3*8*3
(A3)8 x 3 (A1..3) = 0 + 120 + 72 = 192
(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 , 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

(A1)3 x 5 A1..4 = mul(A1..3) + mul(A4..4 ) + mul [(A1..3)3x3 . (A4..4)3x4]


(A2)5 x 8 = 165 + 0 + 3*3*4
(A3)8 x 3 (A1..4) = 165 + 0 + 36 = 201
(A4)3 x 4 3x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

Combine Step
A1..4
( 240 , 312 , 201 )
A1..3 A4..4
165 0

(A1)3 x 5 A1..4 = mul(A1..3) + mul(A4..4 ) + mul [(A1..3)3x3 . (A4..4)3x4]


(A2)5 x 8 = 165 + 0 + 3*3*4
(A3)8 x 3 (A1..4) = 165 + 0 + 36 = 201
(A4)3 x 4 3x4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

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

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2

A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2


Brute Force
1. (A1(A2(A3A4)))
2. (A1((A2A3)A4))
3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2


Divide and Conquer Brute Force
1. (A1(A2(A3A4))) 1. (A1(A2(A3A4)))
2. (A1((A2A3)A4))
3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2


Divide and Conquer Brute Force
1.(A1(A2(A3A4))) 1. (A1(A2(A3A4)))
2.(A1((A2A3)A4)) 2. (A1((A2A3)A4))
3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2


Divide and Conquer Brute Force
1.(A1(A2(A3A4))) 1. (A1(A2(A3A4)))
2.(A1((A2A3)A4)) 2. (A1((A2A3)A4))
3. ((A1A2)(A3A4)) 3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2


Divide and Conquer Brute Force
1.(A1(A2(A3A4))) 1. (A1(A2(A3A4)))
2.(A1((A2A3)A4)) 2. (A1((A2A3)A4))
3. ((A1A2)(A3A4)) 3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4) 4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2


Divide and Conquer Brute Force
1.(A1(A2(A3A4))) 1. (A1(A2(A3A4)))
2.(A1((A2A3)A4)) 2. (A1((A2A3)A4))
3. ((A1A2)(A3A4)) 3. ((A1A2)(A3A4))
4. ((A1(A2A3))A4) 4. ((A1(A2A3))A4)
5. (((A1A2)A3)A4) 5. (((A1A2)A3)A4)
Matrix Chain Multiplication
Matrix Chain Problem (Divide and Conquer)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2

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)

Recursive Algorithm (Top Down Version):

Algorithm RecursiveMatrixChain(S, i, j):


Input: sequence S of n matrices to be multiplied
Output: number of operations in an optimal parenthesization of S
if i=j
then return 0
for k  i to j do
mi, j  min{mi,j , RecursiveMatrixChain(S, i ,k)+ RecursiveMatrixChain(S, k+1,j)
+
pi-1 pk pj}
return mi,j
Matrix Chain Multiplication
Time Complexity (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)

Time to compute m[ i,j] by RECURSIVE-MATRIX-CHAIN:


We assume that the execution of lines 1-2 and 6-7 take at least unit time.

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
n2
 2  2i  n
i 0
n 1
 2(2  1)  n
n 1
2

WE guess that T (n)  (2 n ).


n 1
Using the substitution method with T ( n)  2
Matrix Chain Multiplication

O(n3) – Dynamic Programming


Matrix Chain Multiplication
Matrix Chain Problem
A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2

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

For dynamic programming to be applicable, an optimization problem


must have:

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).

The subproblems are not independent, but instead they overlap


(hence, should be constructed bottom-up).

In contrast , a divide-and-conquer approach is suitable usually generates


brand new problems (i.e., subproblems are independent) at each step of
recursion.
Dynamic Programing – General Technique

Optimal Substructure

A problem exhibits optimal substructure if an optimal solution to the


problem contains within it optimal solutions to subproblems.

Whenever a problem exhibits optimal substructure, it is a good clue that


dynamic programming might apply.

Dynamic programming uses optimal substructure in a bottom-up fashion.

Dynamic programming algorithms take advantage of overlapping subproblems by


solving each subproblem once and then storing the solution in a table where it
can be looked up when needed. Since subproblems overlap, we don’t use
recursion. Instead, we construct optimal subproblems “bottom-up.”
Dynamic Programing – Principle of Optimality

Optimization Problems

Dynamic Problem typically applied to optimization problems that satisfy


the principle of optimality. Dynamic programming solves optimization
problems by combining solutions to subproblems.

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

• Dynamic Programming applies to optimization problems in which a set of


choices must be made in order to arrive at an optimal solution.
• Dynamic Programming is effective when a given problem may arise from
more than one partial set of choices.
• As choices are made, subproblems of the same form arise frequently.

• The key is to store the solutions of subproblems to be reused in the future.

• “Programming” refers to a tabular method with a series of choices, not


“coding”.
Matrix Chain Multiplication
Dynamic Programming (Dynamic Programming)

1. Establish a recursive property that gives the optimal


solution to an instance of the problem.

2. Compute the value of an optimal solution in a bottom-up


fashion.

3. Construct an optimal solution in a bottom-up fashion.


Matrix Chain Multiplication
1. Establish a Recursive Property (Dynamic Programming)

Solve it Optimally Solve it Optimally

A1 A2 A3 A4 A5 - - - - - - An k=1 A1..1 A2..n


(Left Subproblem) (Right Subproblem)

We need to cut the chain A1,, An somewhere in the middle,


i.e., pick some k, where 1  k < n
All Possible Cuttings

A1 A2 A3 A4 A5 - - - - - - An
p0xp1 p1xp2 p2xp3 p3xp4 p4xp5 - - - - - Pn-1xpn
Matrix Chain Multiplication
1. Establish a Recursive Property (Dynamic Programming)

Solve it Optimally Solve it Optimally

A1 A2 A3 A4 A5 - - - - - - An k=1 A1..2 A3..n


(Left Subproblem) (Right Subproblem)
A1 A2 A3 A4 A5 - - - - - - An k=2

A1 A2 A3 A4 A5 - - - - - - An
p0xp1 p1xp2 p2xp3 p3xp4 p4xp5 - - - - - Pn-1xpn
Matrix Chain Multiplication
1. Establish a Recursive Property (Dynamic Programming)

Solve it Optimally Solve it Optimally

A1 A2 A3 A4 A5 - - - - - - An k=1 A1..3 A4..n


(Left Subproblem) (Right Subproblem)
A1 A2 A3 A4 A5 - - - - - - An k=2

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)

Solve it Optimally Solve it Optimally

A1 A2 A3 A4 A5 - - - - - - An k=1 A1..n-1 An..n


(Left Subproblem) (Right Subproblem)
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

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

A1..n = ( A1..k )( Ak+1..n ) 1 ≤ k < n


A1 A2 A3 A4 A5 - - - - - - An
Generalization: p0xp1 p1xp2 p2xp3 p3xp4 p4xp5 - - - - - Pn-1xpn

Ai..j = ( Ai..k )( Ak+1..j ) i ≤ k < j


Matrix Chain Multiplication
1. Establish a Recursive Property (Dynamic Programming)

Ai..j = ( Ai..k ) ( Ak+1..j ) i ≤ k < j

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

mul ( Ai..k ) · ( Ak+1..j ) = pi-1 x pk x pj


pi-1xpk pkxpj
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)

Ai..j = ( Ai..k ) ( Ak+1..j ) i ≤ k < j

opt. mul (Ai..j) = min ( mul (Ai..k) + mul (Ak+1..j) + # of mul ( Ai..k ) · ( Ak+1..j ) )
i≤k<j

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
mul ( Ai..k ) · ( Ak+1..j ) = pi-1 x pk x pj
pi-1xpk pkxpj
Recurrence Relation
m[i, j] = min ( m[i,k] + m[k+1,j] + pi-1 x pk x pj )
i>j
i≤k<j
Recursive Definition
m[i, j] = 0 i=j
Matrix Chain Multiplication
1. Establish a Recursive Property (Dynamic Programming)

m[i, j] = min ( m[i,k] + m[k+1,j] + pi-1 x pk x pj ) i > j


i≤k<j
m[i, j] = 0 i = j : (contains 1 matrix) and (i>j invalid)

To help us keep track of how to constrct an optimal solution, we define

s[ i,j] = value of k at which we can split the product Ai...j to obtain

an optimal paranthesization.

s[i, j] = k
Matrix Chain Multiplication
Subproblem Graph – Top Down (Dynamic Programming)

A1..4

A1..1 A2..4 A1..2 A3..4 A1..3 A4..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

A3..3 A4..4 A2..2 A3..3 A2..2 A3..3 A1..1 A2..2

A1 A2 A3 A4
p0xp1 p1xp2 p2xp3 p3xp4
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)

A1..4

A1..3 A2..4

A1..2 A2..3 A3..4

A1..1 A2..2 A3..3 A4..4


Dynamic Programming Matrix Multiplications Brute Force/D&C
(A1 . A2) (A2 . A3)(A3 . A4) 15 1. (A1(A2(A3A4)))
Scalar Multiplications 2. (A1((A2A3)A4))
A1..3 = (A1) . (A2 A3) 3. ((A1A2)(A3A4))
= (A1 A2) . (A3) ≈ 4n 4. ((A1(A2A3))A4)
Depends on Order of Matrices 5. (((A1A2)A3)A4)
A2..4 = (A2) . (A3 A4)
= (A2 A3) . (A4) A1..4

A1..4 = (A1) . (A2..4)


= (A1 A2) . (A3 A4)
= (A1..3) . (A4) A1..3 A2..4
Matrix Multiplications
10
Scalar Multiplications A1..2 A2..3 A3..4
n3
Depends on Order of Matrices
A1..1 A2..2 A3..3 A4..4
Note: Difference becomes significant in case of large # matrices
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)

Input: Array p[0…n] containing matrix dimensions and n


Result: Minimum-cost table m and split table s
MATRIX-CHAIN-ORDER(p[ ], n)
for i ← 1 to n
m[i, i] ← 0
Takes O(n3) time
for l ← 2 to n
for i ← 1 to n-l+1 Requires O(n2) space
j ← i+l-1
m[i, j] ← 
for k ← i to j-1
q ← m[i, k] + m[k+1, j] + p[i-1] p[k] p[j]
if q < m[i, j]
m[i, j] ← q
s[i, j] ← k
return m and s
78
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)

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[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0


i≤k<j 0
5
m[1,2] 6 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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0


i≤k<j 0
5
m[1,2] = min { m[1, 1] + m[2, 2] + p0 p1 p2 } 6 0
1≤k<2
= min { 0 + 0 + 30x35x15} 1 2 3 4 5 6
S 1
= min ( 15750 ) = 15750 S[1,2] = 1
2
m[2, 3] = min { m[2, 2] + m[3, 3] + p1 p2 p3 } 3
2≤k<3
4
= min { 0 + 0 + 35x15x5} 5
S[2,3] = 2 6
= min ( 2625 ) = 2625
83
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750
0 1 2 3 4 5 6
P 30 35 15 5 10 20 25 2 0 2625

3 0

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0


i≤k<j 0
5
m[1,2] = min { m[1, 1] + m[2, 2] + p0 p1 p2 } 6 0
1≤k<2
= min { 0 + 0 + 30x35x15} 1 2 3 4 5 6
S 1 1
= min ( 15750 ) = 15750 S[1,2] = 1
2 2
m[2, 3] = min { m[2, 2] + m[3, 3] + p1 p2 p3 } 3
2≤k<3
4
= min { 0 + 0 + 35x15x5} 5
= min ( 2625 ) = 2625 S[2,3] = 2 6
84
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750
0 1 2 3 4 5 6
P 30 35 15 5 10 20 25 2 0 2625

3 0

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0


i≤k<j 0
5
m[3, 4] = min { m[3, 3] +m[4, 4] + p2 p3 p4 } 6 0
3≤k<4
= min { 0 + 0 + 15x5x10 } 1 2 3 4 5 6
= min ( 750 ) = 750 S[3,4] = 3 1
S 1
m[4, 5] = min { m[4, 4] + m[5, 5] + p3 p4 p5} 2 2
4≤k<5
= min { 0 + 0 + 5x10x20} 3
= min ( 1000 ) = 1000 S[4,5] = 4
4
m[5, 6] = min { m[5, 5] + m[6, 6] + p4 p5 p6} 5
5≤k<6
= min { 0 + 0 + 10x20x25} 6
= min ( 5000 ) = 5000 S[5,6] = 5 85
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750
0 1 2 3 4 5 6
2 0 2625
P 30 35 15 5 10 20 25
3 0 750

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000

i≤k<j 0
5 5000

l = 24] = min { m[3, 3] +m[4, 4] + p p p }


m[3, 2 3 4 6 0
3≤k<4
= min { 0 + 0 + 15x5x10 } 1 2 3 4 5 6
= min ( 750 ) = 750 S[3,4] = 3 1
S 1
m[4, 5] = min { m[4, 4] + m[5, 5] + p3 p4 p5} 2 2
4≤k<5
= min { 0 + 0 + 5x10x20} 3 3
= min ( 1000 ) = 1000 S[4,5] = 4 4
4
m[5, 6] = min { m[5, 5] + m[6, 6] + p4 p5 p6} 5 5
5≤k<6
= min { 0 + 0 + 10x20x25} 6
= min ( 5000 ) = 5000 S[5,6] = 4 86
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)
1 2 3 4 5 6
m 1 0 15750
0 1 2 3 4 5 6
2625
P 30 35 15 5 10 20 25 2 0

3 0 750

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000

i≤k<j 0
5 5000

m[1, 3] = min { m[1, 1] +m[2, 3] + p0 p1 p3 , 6 0


1≤k<3 m[1, 2] +m[3, 3] + p0 p2 p3 }
1 2 3 4 5 6
= min { 0 + 2625 + 30x35x5 ,
15750 + 0 + 30x15x5 } S 1 1

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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000

i≤k<j 0
5 5000

m[1, 3] = min { m[1, 1] +m[2, 3] + p0 p1 p3 , 6 0


1≤k<3 m[1, 2] +m[3, 3] + p0 p2 p3 }
1 2 3 4 5 6
= min { 0 + 2625 + 30x35x5 ,
15750 + 0 + 30x15x5 } S 1 1 1

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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000

i≤k<j 0
5 5000

m[3, 5]=min { m[3, 3] +m[4, 5] + p2 p3 p5 , 6 0


3≤k<5 m[3, 4] +m[5, 5] + p2 p4 p5 }
1 2 3 4 5 6
= min { 0 + 1000 + 15x5x20 , 1 1
750 + 0 +15x10x20 } S 1
= min ( 2500 , 3750 ) = 2500 S[3,5] = 3 2 2 3

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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[3, 5]=min { m[3, 3] +m[4, 5] + p2 p3 p5 , 6 0


3≤k<5 m[3, 4] +m[5, 5] + p2 p4 p5 }
1 2 3 4 5 6
= min { 0 + 1000 + 15x5x20 , 1 1
750 + 0 +15x10x20 } S 1
= min ( 2500 , 3750 ) = 2500 S[3,5] = 3 2 2 3

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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[1, 4] = min { m[1, 1] + m[2, 4] + p0 p1 p4 , 6 0


1 ≤ k < 4 m[1, 2] + m[3, 4] + p0 p2 p4 ,
m[1,3] + m[4,4] + p0 p3 p4 } 1 2 3 4 5 6
S 1 1 1
= min { 0 + 4375 + 30x35x10 , 3
15750 + 750 + 30x15x10 , 2 2
7875 + 0 + 30x5x10 } 3 3 3

= min { 14875 , 21000 , 9375} = 9375 4 4 5


S[1,4] = 3 5 5

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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[1, 4] = min { m[1, 1] + m[2, 4] + p0 p1 p4 , 6 0


1 ≤ k < 4 m[1, 2] + m[3, 4] + p0 p2 p4 ,
m[1,3] + m[4,4] + p0 p3 p4 } 1 2 3 4 5 6
S 1 1 1 3
= min { 0 + 4375 + 30x35x10 , 3
15750 + 750 + 30x15x10 , 2 2
7875 + 0 + 30x5x10 } 3 3 3

= min { 14875 , 21000 , 9375} = 9375 4 4 5


S[1,4] = 3 5 5

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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[2, 5]=min { m[2, 2] + m[3, 5] + p1 p2 p5 , 6 0


2 ≤ k < 5 m[2, 3] + m[4, 5] + p1 p3 p5 ,
m[2,4] + m[5,5] + p1 p4 p5 } 1 2 3 4 5 6
S 1 1 1 3
=min { 0 + 2500 + 35x15x20 , 3
2625+ 1000 + 35x5x20 , 2 2
4375 + 0 + 35x10x20 } 3 3 3

=min { 13000 , 7125 , 11375 } = 7125 4 4 5


S[2,5] = 3 5 5

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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[2, 5]=min { m[2, 2] + m[3, 5] + p1 p2 p5 , 6 0


2 ≤ k < 5 m[2, 3] + m[4, 5] + p1 p3 p5 ,
m[2,4] + m[5,5] + p1 p4 p5 } 1 2 3 4 5 6
S 1 1 1 3
=min { 0 + 2500 + 35x15x20 , 3 3
2625+ 1000 + 35x5x20 , 2 2
4375 + 0 + 35x10x20 } 3 3 3

=min { 13000 , 7125 , 11375 } = 7125 4 4 5


S[2,5] = 3 5 5

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

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[3, 6] = min { m[3, 3] + m[4, 6] + p2 p3 p6 , 6 0


3≤k<6 m[3, 4] + m[5, 6] + p2 p4 p6 ,
m[3,5] + m[6,6] + p2 p5 p6 v } 1 2 3 4 5 6
S 1 1 1 3
= min { 0 + 3500 + 15x5x25 , 3 3
750+ 5000 + 15x10x25 , 2 2
2500 + 0 + 15x20x25 } 3 3 3

= min { 5375 , 9500 , 10000 } = 5375 4 4 5


S[3,6] = 3 5 5

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

3 0 750 2500 5375

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[3, 6] = min { m[3, 3] + m[4, 6] + p2 p3 p6 , 6 0


3≤k<6 m[3, 4] + m[5, 6] + p2 p4 p6 ,
m[3,5] + m[6,6] + p2 p5 p6 v } 1 2 3 4 5 6
S 1 1 1 3
= min { 0 + 3500 + 15x5x25 , 3 3
750+ 5000 + 15x10x25 , 2 2
2500 + 0 + 15x20x25 } 3 3 3 3

= min { 5375 , 9500 , 10000 } = 5375 4 4 5


S[3,6] = 3 5 5

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

3 0 750 2500 5375

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[1, 5] = min { m[1, 1] + m[2, 5] + p0 p1 p5 , 6 0


1 ≤ k < 5 m[1, 2] + m[3, 5] + p0 p2 p5 ,
m[1,3] + m[4,5] + p0 p3 p5 , 1 2 3 4 5 6
m[1, 4] + m[5, 5] + p0 p4 p5 }S 1 1 1 3
= min { 0 + 7175 + 30x35x20 , 2 2 3 3
15750 + 2500 + 30x15x20 ,
3 3 3 3
7875 + 1000 + 30x5x20 ,
9375 + 0 + 30x10x20 } 4 4 5

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

3 0 750 2500 5375

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[1, 5] = min { m[1, 1] + m[2, 5] + p0 p1 p5 , 6 0


1 ≤ k < 5 m[1, 2] + m[3, 5] + p0 p2 p5 ,
m[1,3] + m[4,5] + p0 p3 p5 , 1 2 3 4 5 6
m[1, 4] + m[5, 5] + p0 p4 p5 }S 1 1 1 3 3
= min { 0 + 7175 + 30x35x20 , 2 2 3 3
15750 + 2500 + 30x15x20 ,
3 3 3 3
7875 + 1000 + 30x5x20 ,
9375 + 0 + 30x10x20 } 4 4 5

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

3 0 750 2500 5375

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[2, 6] = min { m[2, 2] + m[3, 6] + p1 p2 p6 , 6 0


2 ≤ k < 6 m[2, 3] + m[4, 6] + p1 p3 p6 ,
m[2,4] + m[5,6] + p1 p4 p6 , 1 2 3 4 5 6
m[2, 5] + m[6, 6] + p1 p5 p6 }S 1 1 1 3 3
= min { 0 + 5375 + 35x15x25 , 2 2 3 3
2625+ 3500 + 35x5x25 ,
3 3 3 3
4375 + 5000 + 35x10x25 ,
5725 + 0 + 35x20x25 } 4 4 5
5
= min { 18500 , 10500 , 18125 , 23225} 5
= 10500 S[2,6] = 3 6
99
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 10500

3 0 750 2500 5375

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[2, 6] = min { m[2, 2] + m[3, 6] + p1 p2 p6 , 6 0


2 ≤ k < 6 m[2, 3] + m[4, 6] + p1 p3 p6 ,
m[2,4] + m[5,6] + p1 p4 p6 , 1 2 3 4 5 6
m[2, 5] + m[6, 6] + p1 p5 p6 }S 1 1 1 3 3
= min { 0 + 5375 + 35x15x25 , 2 2 3 3 3
2625+ 3500 + 35x5x25 ,
3 3 3 3
4375 + 5000 + 35x10x25 ,
5725 + 0 + 35x20x25 } 4 4 5
5
= min { 18500 , 10500 , 18125 , 23225} 5
= 10500 S[2,6] = 3 6
100
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 10500

3 0 750 2500 5375

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[1, 6] = min { m[1, 1] + m[2, 6] + p0 p1 p6 , 6 0


1 ≤ k < 6 m[1, 2] + m[3, 6] + p0 p2 p6 ,
m[1,3] + m[4,6] + p0 p3 p6 , 1 2 3 4 5 6
m[1, 4] + m[5, 6] + p0 p4Sp6 ,1 1 1 3 3
m[1, 5] + m[6, 6] + p0 p5 p6 }
2 2 3 3 3
=min { 0 + 10500 + 30x35x25 , 3 3 3 3
15750+ 5375 + 30x15x25 ,
7875 + 3500 + 30x5x25 , 4 4 5
9375+ 5000 + 30x10x25 , 5
11875+ 0 + 30x20x25 } 5
6
=min { 36750 , 32375 , 15125S[1,6]
, 21875,
= 326875} 101
= 15125
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

3 0 750 2500 5375

m[i, j] = min { m[i, k] + m[k+1, j] + pi-1 pk pj } 4 0 1000 3500

i≤k<j 0
5 5000

m[1, 6] = min { m[1, 1] + m[2, 6] + p0 p1 p6 , 6 0


1 ≤ k < 6 m[1, 2] + m[3, 6] + p0 p2 p6 ,
m[1,3] + m[4,6] + p0 p3 p6 , 1 2 3 4 5 6
m[1, 4] + m[5, 6] + p0 p4Sp6 ,1 1 1 3 3 3
m[1, 5] + m[6, 6] + p0 p5 p6 }
2 2 3 3 3
=min { 0 + 10500 + 30x35x25 , 3 3 3 3
15750+ 5375 + 30x15x25 ,
7875 + 3500 + 30x5x25 , 4 4 5
9375+ 5000 + 30x10x25 , 5
11875+ 0 + 30x20x25 } 5
6
=min { 36750 , 32375 , 15125S[1,6]
, 21875,
= 326875} 102
= 15125
Matrix Chain Multiplication
2. Compute Optimal Value (DP-Bottom Up)

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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Top-Down Approach

Memoization

There is a variation of dynamic programming that often offers the


efficiency of the usual dynamic-programming approach while
maintaining a top-down strategy.

The idea is to memoize the the natural, but inefficient, recursive


algorithm.

We maintain a table with subproblem solutions, but the control


structure for filling in the table is more like the recursive algorithm.

108
Matrix-Chain multiplication

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Top-Down Approach

Memoization (cont.)

•An entry in a table for the solution to each subproblem is maintained.


•Eech table entry initially contains a special value to indicate that the
entry has yet to be filled.
•When the subproblem is first encountered during the execution of the
recursive algorithm, its solution is computed and then stored in the
table.
•Each subsequent time that the problem is encountered, the value
stored in the table is simply looked up and returned.

109
Matrix-Chain multiplication

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Top-Down Approach

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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Top-Down Approach

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)

• To keep track of how to construct an optimal solution, we use a split table s


• s[i, j ] = k (value of k at which Ai Ai+1 … Aj is split for optimal parenthesization)

An optimal solution can be constructed from the computed information


stored in the table s[1...n, 1...n].

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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach


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 ) ) ( ( 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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach


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 ) ) ( ( 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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach

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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach

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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach

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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach

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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach

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

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach

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, ….

• Some famous dynamic programming algorithms.


– Viterbi for hidden Markov models.
– Unix diff for comparing two files.
– Smith-Waterman for sequence alignment.
– Bellman-Ford for shortest path routing in networks.
– Cocke-Kasami-Younger for parsing context free grammars.

146
Matrix-Chain multiplication

Matrix-Chain multiplication Dynamic Programming

Dynamic Programming – Bottom Up Approach

Step 1: Characterize the structure of an optimal paranthesization


Find the optimal substructure and then use it to construct an optimal solution
to the main problem from optimal solutions to subproblems .

32

You might also like