Grade School Revisited:How To Multiply Two NumbersGauss’ Complex PuzzleGauss’ $3.05 MethodTime complexity of grade school additionTime complexity of grade school multiplicationGrade School Addition: Linear timeGrade School Multiplication: Quadratic timeAny addition algorithm takes Ω(n) timeAny addition algorithm takes Ω(n) timeGrade School Addition: Θ(n) timeFurthermore, it is optimalGrade School Multiplication: Θ(n2) timeGrade School Multiplication:The Kissing IntuitionDivide And ConquerMultiplication of 2 n-bit numbersMultiplication of 2 n-bit numbersSame thing for numbers in decimal!Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Multiplying (Divide & Conquer style)Divide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueDivide, Conquer, and GlueTime required by MULTRecurrence RelationLet’s be concrete and keep it simpleLet’s be concrete and keep it simpleTechnique 1:Guess and VerifyTechnique 1:Guess and VerifyTechnique 2:Guess Form and Calculate CoefficientsTechnique 3: Labeled Tree RepresentationTechnique 3: Labeled Tree RepresentationNode labels: time not spent conqueringDivide and Conquer MULT: Θ(n2) time Grade School Multiplication: Θ(n2) timeDivide and Conquer MULT: Θ(n2) time Grade School Multiplication: Θ(n2) timeMULT revisitedGauss’ optimizationKaratsuba, Anatolii Alexeevich (1937-)Gaussified MULT(Karatsuba 1962)Substituting into our formula….Dramatic improvement for large nMystery MULTMultiplication AlgorithmsREFERENCESGreat Theoretical Ideas In Computer ScienceAnupam Gupta CS 15-251 Spring 2005Lecture 18 March 17, 2005 Carnegie Mellon UniversityGrade School Revisited:How To Multiply Two Numbers2 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 MethodInput: a,b,c,d Output: ac-bd, ad+bc(a+bi)(c+di) = [ac –bd] + [ad + bc] icc$$$cccX1= a + bX2= c + dX3= X1 X2= ac + ad + bc + bdX4 = acX5= bdX6= X4 –X5 = ac - bdX7= X3–X4–X5 = bc + adThe 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 multiplicationX* * * * * * * * * * * * * * * * * * * * * * * ** * * * * * * ** * * * * * * ** * * * * * * ** * * * * * * ** * * * * * * ** * * * * * * ** * * * * * * ** * * * * * * * * * * * * * * *n2T(n) = The amount of time grade school multiplication uses to add two n-bit numbersWe saw that T(n) was quadratic.T(n) = Θ(n2)Grade School Addition: Linear timeGrade School Multiplication: Quadratic time# of bits in numberstimeNo matter how dramatic the difference in the constants the quadratic curve will eventually dominate the linear curveGrade school addition islinear 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 numbersAny addition algorithm takes Ω(n) time* * * * * * * * ** * * * * * * * ** * * * * * * * * *A did notread this bitat position i•If A is not correct on the inputs, we found a bug•If A is correct, flip the bit at position i and give Athe new pair of numbers. A gives the same answer as before, which is now wrong.So 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 lineartime?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 n2times.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 numbersn bitsX = Y = a bXc dYn/2 bits n/2 bitsX = a 2n/2+ b Y = c 2n/2+ dX × Y = ac 2n+ (ad + bc) 2n/2+ bdMultiplication of 2 n-bit numbersa bX = Y = c dn/2 bits n/2 bitsX × Y = ac 2n+ (ad + bc) 2n/2+ bdMULT(X,Y):If |X| = |Y| = 1 then return XYbreak X into a;b and Y into c;dreturnMULT(a,c) 2n+ (MULT(a,d) + MULT(b,c)) 2n/2 + MULT(b,d)Same thing for numbers in decimal!n digitsX = Y = a bc dn/2 digits n/2 digitsX = a 10n/2+ b Y = c 10n/2+ dX × Y = ac 10n+ (ad + bc) 10n/2+ bdMultiplying (Divide & Conquer style)12345678 *
View Full Document