Fast Multiplication The Divide and Conquer Method Omar

  • Slides: 26
Download presentation
Fast Multiplication The Divide and Conquer Method Omar Hemmali

Fast Multiplication The Divide and Conquer Method Omar Hemmali

Objectives Define Divide and Conquer n Explain the Karatsuba Multiplication Method (Fast Multiplication) n

Objectives Define Divide and Conquer n Explain the Karatsuba Multiplication Method (Fast Multiplication) n Work an example using the Karatsuba Method n

Divide et impera (Divide and Conquer) A Divide and Conquer algorithm works by recursively

Divide et impera (Divide and Conquer) A Divide and Conquer algorithm works by recursively breaking down a problem into two or more sub-problems of the same (or related) type, until these become simple enough to be solved directly. n The solutions to the sub-problems are then combined to give a solution to the original problem. n [taken from wikipedia]

About the Algorithm n n n Discovered by Anatolii Karatsuba in 1960. Published in

About the Algorithm n n n Discovered by Anatolii Karatsuba in 1960. Published in a joint paper with Yu. Ofman in 1962. O( ) Disproved Andrey Kolmogorov's 1956 conjecture that the fastest multiplication algorithm was O( ) First Divide and Conquer Algorithm

How It Works Assume two numbers A, B in base 10 n. A= n.

How It Works Assume two numbers A, B in base 10 n. A= n. B= n m = half the number of digits in A, B n

How It Works (cont. ) 1 2 3 4 Multiplying the four subproducts separately

How It Works (cont. ) 1 2 3 4 Multiplying the four subproducts separately gives O( ) n Example: 25 * 16 n We can do better n

How It Works (cont. ) Let n What? How? n

How It Works (cont. ) Let n What? How? n

How It Works (cont. ) n n n Let Let 1 2 3 Now

How It Works (cont. ) n n n Let Let 1 2 3 Now only 3 multiplications n So n

The “Old” Way Multiplying

The “Old” Way Multiplying

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 *

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 * 79 – X 0 Y 0 Z 0) * 100 + (62 * 60) = Z 0

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 *

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 * 79 – X 0 Y 0 Z 0) * 100 + (62 * 60) = Z 0 11 – 81 19 * 19 = (1 1 * 1) * 100 + (10 * 10 – X Z 1 ) * 10 + (981 * 9) = X 1 Y 1 Z 1

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 *

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 * 79 – X 0 Y 0 Z 0) * 100 + (62 * 60) = Z 0 11 – 81 19 * 19 = (1 1 * 1) * 100 + (10 * 10 – X Z 1 ) * 10 + (981 * 9) = X 1 Y 1 Z 1 * 1) * 100 + (1 1* 1 – 0 X 12 – Z 02 ) * 10 + (0 0* 0) = 100 10 * 10 = (1 1 Z 2 X 2 Y 2

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 *

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 * 79 – X 0 Y 0 Z 0) * 100 + (62 * 60) = Z 0 11 – 81 19 * 19 = (1 1 * 1) * 100 + (10100 * 10 18 –X Z 1 ) * 10 + (981 * 9) = X 1 Y 1 Z 1 * 1) * 100 + (1 1* 1 – 0 X 12 – Z 02 ) * 10 + (0 0* 0) = 100 10 * 10 = (1 1 Z 2 X 2 Y 2

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 *

New Way 1962 * 1960 = (19 * 19) * 10000 + (91 * 79 – X 0 Y 0 Z 0) * 100 + (62 * 60) = Z 0 11 – 81 19 * 19 = (1 1 * 1) * 100 + (10100 * 10 18 –X Z 1 ) * 10 + (981 * 9) = 361 X 1 Y 1 Z 1

New Way 1962 * 1960 = (19361 * 19) * 10000 + (91 *

New Way 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 361 X 0 – X 0 Y 0 Z 0) * 100 + (62 * 60) = Z 0 11 – 81 19 * 19 = (1 1 * 1) * 100 + (10100 * 10 18 –X Z 1 ) * 10 + (981 * 9) = 361 X 1 Y 1 Z 1

New Way 1962 * 1960 = (19361 * 19) * 10000 + (91 *

New Way 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 361 X 0 – X 0 Y 0 Z 0) * 100 + (62 * 60) = Z 0 36 62 * 60 = (636 * 6) * 100 + (848 * 6 – 12 X 1 – Z 01 ) * 10 + (2 0* 0) = 3720 X 1 Y 1 Z 1

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 361 X 0 – 3720) Z 0) * 100 + (62 * 60) = Z 0 X 0 Y 0 62 * 60 = (636 * 6) * 100 + (8 * 6 – 12 X 1 – Z 1 ) * 10 + (0 0* 2) = 3720 X 1 Y 1 Z 1

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 361 X 0 – 3720 Z 0) * 100 + (62 * 60) = 3720) Z 0 X 0 Y 0 91 * 79 = (963 * 7) * 100 + (10 * 16 – 63 X 1 – Z 91 ) * 10 + (1 9 * 9) = X 1 Y 1 Z 1

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 361 X 0 – 3720) Z 0) * 100 + (62 * 60) = Z 0 X 0 Y 0 91 * 79 = (963 * 7) * 100 + (10 * 16 – 63 X 1 – Z 91 ) * 10 + (1 9 * 9) = X 1 Y 1 Z 1 10 * 16 = (1 1* 1) * 100 + (1 7* 7 – 6 X 12 – Z 02 ) * 10 + (0 0* 6) = 160 Z 2 X 2 Y 2

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 361 X 0 – 3720) Z 0) * 100 + (62 * 60) = Z 0 X 0 Y 0 91 * 79 = (963 * 7) * 100 + (10160 * 16 – 63 X 1 – Z 91 ) * 10 + (1 9 * 9) = X 1 Y 1 Z 1 10 * 16 = (1 1* 1) * 100 + (1 7* 7 – 6 X 12 – Z 02 ) * 10 + (0 0* 6) = 160 Z 2 X 2 Y 2

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 361 X 0 – 3720) Z 0) * 100 + (62 * 60) = Z 0 X 0 Y 0 91 * 79 = (963 * 7) * 100 + (10160 * 16 88 – 63 X 1 – Z 91 ) * 10 + (1 9 * 9) = 7189 X 1 Y 1 Z 1

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 361 X 0 – 3720) Z 0) * 100 + (62 * 60) = 7189 Z 0 X 0 Y 0 91 * 79 = (963 * 7) * 100 + (10160 * 16 88 – 63 X 1 – Z 91 ) * 10 + (1 9 * 9) = 7189 X 1 Y 1 Z 1

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91

New Way 3720 1962 * 1960 = (19361 * 19) * 10000 + (91 * 79 – 3108 X 0 – 3720) Z 0) * 100 + (62 * 60) = 361 7189 Z 0 X 0 Y 0 3845520

Observations n 13 Multiplications in the example n Time Complexity = O( n )

Observations n 13 Multiplications in the example n Time Complexity = O( n )

Sources n n http: //en. wikipedia. org/wiki/Karatsuba_algorithm http: //en. wikipedia. org/wiki/Divide_and_conquer_algorithm

Sources n n http: //en. wikipedia. org/wiki/Karatsuba_algorithm http: //en. wikipedia. org/wiki/Divide_and_conquer_algorithm

Questions 1. 2. Who discovered the Karatsuba Multiplication Method? What is the significance of

Questions 1. 2. Who discovered the Karatsuba Multiplication Method? What is the significance of the numbers in the example?