Upload
gordon-silvester-chambers
View
218
Download
2
Embed Size (px)
Citation preview
Catalan Numbers
Objective: • Find C(n), the number
of ways to compute product x1 . x2 …. xn.
n multiplication order
2 (x1 · x2)
3 (x1 · (x2 · x3))
((x1 · x2) · x3)
4 (x1 · (x2 · (x3 · x4)))
(x1 · ((x2 · x3) · x4))
((x1 · x2) · (x3 · x4))
((x1 · (x2 · x3)) · x4)
(((x1 · x2) · x3) · x4)
Multiplying n Numbers
n C(n)
1 1
2 1
3 2
4 5
5 14
6 42
7 132
Multiplying n Numbers – small n
Chain-Matrix Multiplication
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Statement: The chain-matrix multiplication problem can be stated as below:
• Given a chain of [A1, A2, . . . , An] of n matrices where for i = 1, 2, . . . , n, matrix Ai has dimension pi-
1 x pi, find the order of multiplication which minimizes the number of scalar multiplications.
Note:
• Order of A1 is p0 x p1,
• Order of A2 is p1 x p2,
• Order of A3 is p2 x p3, etc.
• Order of A1 x A2 x A3 is p0 x p3,
• Order of A1 x A2 x . . . x An is p0 x pn
Problem Statement: Chain Matrix Multiplication
Chain Matrix Multiplication
(Brute Force Approach)
Example
• Given a sequence [A1, A2, A3, A4]
• Order of A1 = 10 x 100
• Order of A2 = 100 x 5
• Order of A3 = 5x 50
• Order of A4 = 50x 20
Compute the order of the product A1 . A2 . A3 . A4
in such a way that minimizes the total number of scalar multiplications.
Brute Force Chain Matrix Multiplication
• There are five ways to parenthesize this product• Cost of computing the matrix product may vary,
depending on order of parenthesis.• All possible ways of parenthesizing
(A1 · (A2 . (A3 . A4)))
(A1 · ((A2 . A3). A4))
((A1 · A2). (A3 . A4))
((A1 · (A2 . A3)). A4)
(((A1 · A2). A3). A4)
Brute Force Chain Matrix Multiplication
10 x 100 x 20 = 20000
100 x 5 x 20 = 10000
5 x 50 x 20 = 5000
10 x 20
100 x 20
5 x 20A3
5 x 50A4
50 x 20
A2100 x 5
A110 x 100
Total Cost = 35000
1
2
3
Kinds of problems solved by algorithms
10 x100 x 20 = 20000
100 x 50 x 20 = 100000
100 x 5 x 50 = 25000
10 x 20
100 x 20
100 x 50
A35 x 50
A450 x 20
A2100 x 5
A110 x 100
Total Cost = 145000
1
2
3
Second Chain : (A1 · ((A2 . A3). A4))
10 x 5 x 20 = 1000
10 x 100 x 5 = 5000 5 x 50 x 20 = 500010x20
5 x 2010 x 5
A35 x 50
A450 x 20
A110 x 100
A2100 x 5
Total Cost = 11000
1
2
3
Third Chain : ((A1 · A2). (A3 . A4))
10 x 50 x 20 = 10000
100 x 5 x 50 = 25000
10 x 100 x 50 = 50000
Total Cost = 85000
10x20
100x50
10x50
A35x50
A450x20
A2100x5
A110x100
1
2
3
Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Fourth Chain : ((A1 · (A2 . A3)). A4)
10 x 50 x 20 = 10000
10 x 100 x 5 = 5000
10 x 5 x 50 = 2500
10 x 20
10 x 5
10 x 50
A35 x 50
A450 x 20
A2100 x 5
A110 x 100
Total Cost = 17500
1
2
3
Fifth Chain : (((A1 · A2). A3). A4)
First Chain 35,000
Second Chain 145,000
Third Chain 11,000
Fourth Chain 85,000
Fifth Chain 17,500 10x20
5 x 2010 x 5
A35 x 50
A450 x 20
A110 x 100
A2100 x 5
1
2
3
((A1 · A2). (A3 . A4))
Chain Matrix Cost