Lecture # 14 - New
Lecture # 14 - New
Decide on : algorithm
design techniques etc.
Design an algorithm
Prove correctness
Read Chapter 8
Dynamic Programming
Number of possibilities
to parenthesize n 1 if n 1
matrices
P (=>
n) n 1
P ( k ) P ( n k ) if n 2
k 1
=> P( n ) C ( n 1) 1 2( n 1) ( 4 )
n
n n 1 n 3/ 2
C(n) - Catalan number
Department of Computer Science 8
Chain Matrix Multiplication: Motivation
2( 2 n 1 1) n
2n 2 n
2n
0 if i j
minm[i, k ] m[k 1, j ] pi 1 pk p j if i j
m[i, j ]
ik j
Analysis:
(n i )(n i 1)
n
T ( n) n
i 1 2
1 n 2
T (n) n (n 2ni i 2 n i )
2 i 1
1 n 2 n n n n
T (n) n ( n 2ni i 2 n i )
2 i 1 i 1 i 1 i 1 i 1
1 n 2 n n n n
T (n) n ( n 2ni i 2 n i )
2 i 1 i 1 i 1 i 1 i 1
1 2 n n n n n
T ( n) n ( n 1 2n i i 2 n 1 i )
2 i 1 i 1 i 1 i 1 i 1
1
T (n) n (6n 3 6n 3 6n 2 2n 3 3n 2 n 6n 2 3n 2 3n)
12
1 1 1
T (n) (12n 2n 2n) (10n 2n ) (5n n 3 )
3 3
12 12 6
Department of Computer Science
Obtaining the optimal multiplication order
j
i 2 3 4 A1 30×1
1 1 1 1
A2 1×40
2 2 3 A3 40×10
3 3 A4 10×25
PRINT(s, 1, 4)
PRINT (s, 1, 1) PRINT (s, 2, 4)
m[i, i ] 0, i 1,...,4
m[i, j ] min (m[i, k ] m[k 1, j ] pi 1. pk . p j )
ik j
Main Diagonal 0
m[1, 1] = 0 0
m[2, 2] = 0 0
m[3, 3] = 0 0
m[4, 4] = 0
m[1,2] m[3,3] p0 . p2 . p3 ))
m[1, 3] = min(0+25000+10.100.50, 5000+0+10.5.50)
= min(75000, 2500) = 2500
s[1, 3] = k = 2
Department of Computer Science
Computing m[2, 4]
1 5 8 10
Order of Computation 2 6 9
3 7
4
0 1 2 2
0 2 2
0 3
0
For, m(1, 4) 2
k=2
10x20
((A1 . A2) . (A3 . A4)) 1
3
10 x 5 5 x 20
A1 A2 A3 A4
10 x 100 100 x 5 5 x 50 50 x 20
Department of Computer Science
Representing Order using Binary Tree
The above computation shows that the minimum
cost for multiplying those four matrices is 11000.
The optimal order for multiplication is
((A1 . A2) . (A3 . A4))
2
For, m(1, 4) 10x20
k=2 1
3
10 x 5 5 x 20
A1 A2 A3 A4
10 x 100 100 x 5 5 x 50 50 x 20
MCM OBST
0 if i j
minm[i, k ] m[k 1, j ] pi 1 pk p j if i j
m[i, j ]
ik j
OBST
MCM
Department of Computer Science 51
CONCLUSION