Download 620 KB 8th Feb 2015 Lecture4

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the work of artificial intelligence, which forms the content of this project

Document related concepts
no text concepts found
Transcript
Catalan Numbers
Multiplying n Numbers
Objective:
• Find C(n), the number
of ways to compute
product x1 . x2 …. xn.
n
multiplication order
2
3
(x1 · x2)
(x1 · (x2 · x3))
((x1 · x2) · x3)
(x1 · (x2 · (x3 · x4)))
(x1 · ((x2 · x3) · x4))
((x1 · x2) · (x3 · x4))
((x1 · (x2 · x3)) · x4)
(((x1 · x2) · x3) · x4)
4
Multiplying n Numbers – small n
n
C(n)
1
1
2
1
3
2
4
5
5
14
6
42
7
132
Chain-Matrix Multiplication
Dr Nazir A. Zafar
Advanced Algorithms Analysis and Design
Problem Statement: Chain Matrix Multiplication
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
Chain Matrix Multiplication
(Brute Force Approach)
Brute Force Chain Matrix Multiplication
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)
Kinds of problems solved by algorithms
10 x 100 x 20 = 20000
1
10 x 20
A1
10 x 100
100 x 5 x 20 = 10000
2
100 x 20
A2
100 x 5
A3
5 x 50
3
5 x 50 x 20 = 5000
5 x 20
Total Cost = 35000
A4
50 x 20
Second Chain : (A1 · ((A2 . A3). A4))
1
10 x100 x 20 = 20000
10 x 20
3
A1
10 x 100
2
100 x 20
A4
50 x 20
100 x 50
A2
100 x 5
100 x 50 x 20 = 100000
A3
5 x 50
100 x 5 x 50 = 25000
Total Cost = 145000
Third Chain : ((A1 · A2). (A3 . A4))
10 x 5 x 20 = 1000
2
10 x 100 x 5 = 5000
10x20
5 x 50 x 20 = 5000
1
3
10 x 5
A1
10 x 100
Total Cost = 11000
A2
100 x 5
5 x 20
A3
5 x 50
A4
50 x 20
Fourth Chain : ((A1 · (A2 . A3)). A4)
3
10 x 50 x 20 = 10000
10x20
10 x 100 x 50 = 50000
100 x 5 x 50 = 25000
Total Cost = 85000
1
A4
50x20
10x50
A1
10x100
2
100x50
A2
100x5
Dr Nazir A. Zafar
A3
5x50
Advanced Algorithms Analysis and Design
Fifth Chain : (((A1 · A2). A3). A4)
10 x 50 x 20 = 10000
3
10 x 20
10 x 5 x 50 = 2500
2
10 x 50
10 x 100 x 5 = 5000
1
Total Cost = 17500
A1
10 x 100
10 x 5
A2
100 x 5
A3
5 x 50
A4
50 x 20
Chain Matrix Cost
First Chain
Second Chain
Third Chain
Fourth Chain
Fifth Chain
35,000
145,000
11,000
85,000
17,500
((A1 · A2). (A3 . A4))
2
10x20
1
3
10 x 5
A1
10 x 100
A2
100 x 5
5 x 20
A3
5 x 50
A4
50 x 20
Related documents