Catalan Numbers
Multiplying n Numbers 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) Objective: Find C(n), the number of ways to compute product x1 . x2 …. xn.
Multiplying n Numbers – small n C(n) 1 2 3 4 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
Second Chain : (A1 · ((A2 . A3). A4))
Third Chain : ((A1 · A2). (A3 . A4))
Fourth Chain : ((A1 · (A2 . A3)). A4) Dr Nazir A. Zafar Advanced Algorithms Analysis and Design
Fifth Chain : (((A1 · A2). A3). A4)
Chain Matrix Cost ((A1 · A2). (A3 . A4)) First Chain 35,000 Second Chain 145,000 Third Chain 11,000 Fourth Chain 85,000 Fifth Chain 17,500 ((A1 · A2). (A3 . A4))