Grade School Revisited: How To Multiply Two NumbersSlide 2Slide 3Gauss’ Complex PuzzleGauss’ $3.05 MethodSlide 6Time complexity of grade school additionTime complexity of grade school multiplicationGrade School Addition: Linear time Grade School Multiplication: Quadratic timeSlide 10Any addition algorithm takes Ω(n) timeSlide 12Slide 13Grade School Addition: Θ(n) time Furthermore, it is optimal Grade School Multiplication: Θ(n2) timeSlide 15Slide 16Grade School Multiplication: The Kissing IntuitionDivide And ConquerMultiplication of 2 n-bit numbersSlide 20Same thing for numbers in decimal!Multiplying (Divide & Conquer style)Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Slide 35Slide 36Slide 37Slide 38Slide 39Slide 40Slide 41Slide 42Slide 43Slide 44Slide 45Slide 46Slide 47Divide, Conquer, and GlueSlide 49Slide 50Slide 51Slide 52Slide 53Slide 54Slide 55Slide 56Slide 57Slide 58Slide 59Time required by MULTSlide 61Recurrence RelationLet’s be concrete and keep it simpleSlide 64Technique 1: Guess and VerifySlide 66Technique 2: Guess Form and Calculate CoefficientsTechnique 3: Labeled Tree RepresentationSlide 69Node labels: time not spent conqueringSlide 71Slide 72Slide 73Slide 74Slide 75Slide 76Slide 77Divide and Conquer MULT: Θ(n2) time Grade School Multiplication: Θ(n2) timeSlide 79MULT revisitedGauss’ optimizationKaratsuba, Anatolii Alexeevich (1937-)Gaussified MULT (Karatsuba 1962)Slide 84Slide 85Slide 86Slide 87Slide 88Slide 89Substituting into our formula….Slide 91Dramatic improvement for large nSlide 93Slide 94Slide 95Mystery MULTSlide 97Slide 98Multiplication AlgorithmsSlide 100REFERENCESGrade School Revisited:How To Multiply Two NumbersGreat Theoretical Ideas In Computer ScienceJohn Lafferty CS 15-251 Fall 2005Lecture 18 October 27, 2005 Carnegie Mellon University2 X 2 = 5The best way isoften far from obvious!(a+bi)(c+di) GaussGauss’ Complex PuzzleRemember how to multiply twocomplex numbers a + bi and c + di?(a+bi)(c+di) = [ac –bd] + [ad + bc] iInput: a,b,c,d Output: ac-bd, ad+bcIf multiplying two real numbers costs $1 and adding them costs a penny, what is the cheapest way to obtain the output from the input?Can you do better than $4.02?Gauss’ $3.05 MethodX1 = a + bX2 = c + dX3 = X1 X2 = ac + ad + bc + bdX4 = acX5 = bdX6 = X4 – X5 = ac - bdX7 = X3 – X4 – X5 = bc + adInput: a,b,c,d Output: ac-bd, ad+bc(a+bi)(c+di) = [ac –bd] + [ad + bc] icc$$$cccThe Gauss optimization saves one multiplication out of four. It requires 25% less work.Time complexity of grade school addition *** * *** * *** * *** * *** * *** * *** * *** * *** * *** +* * * *** T(n) = The amount of time grade school addition uses to add two n-bit numbersWe saw that T(n) was linear.T(n) = Θ(n)Time complexity of grade school multiplicationT(n) = The amount of time grade school multiplication uses to add two n-bit numbersWe saw that T(n) was quadratic.T(n) = Θ(n2)X* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *n2Grade School Addition: Linear timeGrade School Multiplication: Quadratic timeNo matter how dramatic the difference in the constants the quadratic curve will eventually dominate the linear curve# of bits in numberstimeGrade school addition is linear time. Is there a sub-linear time method for addition?Any addition algorithm takes Ω(n) timeClaim: Any algorithm for addition must read all of the input bits Proof: Suppose there is a mystery algorithm A that does not examine each bit Give A a pair of numbers. There must be some unexamined bit position i in one of the numbers•If A is not correct on the inputs, we found a bug•If A is correct, flip the bit at position i and give A the new pair of numbers. A gives the same answer as before, which is now wrong.Any addition algorithm takes Ω(n) time* * * * * * * * ** * * * * * * * ** * * * * * * * * *A did notread this bitat position iSo any algorithm for addition must use time at least linear in the size of the numbers.Grade school addition can’t be improved upon by more than a constant factor.Grade School Addition: Θ(n) timeFurthermore, it is optimalGrade School Multiplication: Θ(n2) timeIs there a clever algorithm to multiply two numbers in linear time?Despite years of research, no one knows! If you resolve this question, Carnegie Mellon will give you a PhD!Can we even break the quadratic time barrier?In other words, can we do something very different than grade school multiplication?Grade School Multiplication:The Kissing IntuitionIntuition: Let’s say that each time an algorithm has to multiply a digit from one number with a digit from the other number, we call that a “kiss”. It seems as if any correct algorithm must kiss at least n2 times.Divide And ConquerAn approach to faster algorithms:1. DIVIDE a problem into smaller subproblems2. CONQUER them recursively3. GLUE the answers together so as to obtain the answer to the larger problemMultiplication of 2 n-bit numbersX = Y = a bc dX = a 2n/2 + b Y = c 2n/2 + d n/2 bitsn/2 bitsn bitsX × Y = ac 2n + (ad + bc) 2n/2 + bd XYMultiplication of 2 n-bit numbersX = Y = a bc dn/2 bitsn/2 bitsX × Y = ac 2n + (ad + bc) 2n/2 + bd MULT(X,Y): If |X| = |Y| = 1 then return XY break X into a;b and Y into c;d return MULT(a,c) 2n + (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d)Same thing for numbers in decimal!X = Y = a bc dX = a 10n/2 + b Y = c 10n/2 + d n/2 digitsn/2 digitsn digitsX × Y = ac 10n + (ad + bc) 10n/2 + bdMultiplying (Divide & Conquer style)12345678 * 21394276*4276 5678*2139 5678*4276X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc dMultiplying (Divide & Conquer style)12345678 * 213942761234*2139 1234*4276 5678*2139 5678*4276X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc dMultiplying (Divide & Conquer style)12345678 * 213942761234*2139 1234*4276 5678*2139 5678*4276X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc dMultiplying (Divide & Conquer style)12345678 * 213942761234*2139 1234*4276 5678*2139 5678*4276X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc dMultiplying (Divide & Conquer style)12345678 * 213942761234*2139 1234*4276 5678*2139 5678*4276X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc dMultiplying (Divide & Conquer style)12345678 * 213942761234*2139 1234*4276 5678*2139 5678*4276X = Y = X × Y = ac 10n + (ad + bc) 10n/2 +
View Full Document