14
Catalan Numbers

Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

Embed Size (px)

Citation preview

Page 1: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

Catalan Numbers

Page 2: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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

Page 3: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

n C(n)

1 1

2 1

3 2

4 5

5 14

6 42

7 132

Multiplying n Numbers – small n

Page 4: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

Chain-Matrix Multiplication

Dr Nazir A. Zafar Advanced Algorithms Analysis and Design

Page 5: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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

Page 6: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

Chain Matrix Multiplication

(Brute Force Approach)

Page 7: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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

Page 8: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

• 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

Page 9: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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

Page 10: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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

Page 11: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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

Page 12: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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)

Page 13: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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)

Page 14: Catalan Numbers. Objective: Find C(n), the number of ways to compute product x 1. x 2 …. x n. nmultiplication order 2(x 1 · x 2 ) 3(x 1 · (x 2 · x 3 ))

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