Slide 1Grade School Revisited: How To Multiply Two NumbersSlide 3Gauss’ Complex PuzzleGauss’ $3.05 MethodSlide 6Slide 7Time complexity of grade school multiplicationSlide 9Slide 10Slide 11Slide 12Slide 13Slide 14Slide 15Divide And ConquerMultiplication of 2 n-bit numbersSlide 18Same thing for numbers in decimal!Multiplying (Divide & Conquer style)Slide 21Slide 22Slide 23Divide, Conquer, and GlueSlide 25Slide 26Slide 27Slide 28Slide 29Slide 30Slide 31Slide 32Slide 33Slide 34Time required by MULTRecurrence RelationSimplified Recurrence RelationSlide 38Slide 39Slide 40Slide 41Slide 42Divide and Conquer MULT: Θ(n2) time Grade School Multiplication: Θ(n2) timeMULT revisitedGauss’ optimizationKaratsuba, Anatolii Alexeevich (1937-)Gaussified MULT (Karatsuba 1962)Slide 48Slide 49Slide 50Slide 51Slide 52Slide 53Dramatic Improvement for Large nSlide 55Multiplication AlgorithmsSlide 57A case studyExamples(Novice Level Solution)Performance Analysis Counting without executing“Expert” Level SolutionThe hacker is satisfied and reflects no furtherThe master keeps trying to refine the solutionMaster SolutionSuppose the dictionary was the list below.After each word, write its “signature” (sort its letters)Sort by the signaturesThe Master’s ProgramSlide 70Slide 7115-251Great Theoretical Ideas in Computer ScienceGrade School Revisited:How To Multiply Two NumbersLecture 22 (November 5, 2009)Gauss (a+bi)Gauss’ Complex PuzzleCan you do better than $4.02?Remember how to multiply two complex 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?Gauss’ $3.05 MethodInput: a,b,c,d Output: ac-bd, ad+bcX1 = a + bX2 = c + dX3 = X1 X2 = ac + ad + bc + bdX4 = acX5 = bdX6 = X4 – X5 = ac - bdX7 = X3 – X4 – X5 = bc + adc$$$ccccThe Gauss optimization saves one multiplication out of four. It requires 25% less work.+T(n) = amount of time grade school addition uses to add two n-bit numbers* * * * * * * * * ** * * * * * * * * ** * * * * * * * * ** * * * * * * * * * *Time complexity of grade school additionWe saw that T(n) was linearT(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 quadraticT(n) = Θ(n2)X* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *n2# of bits in the numberstimeGrade School Addition: Linear timeGrade School Multiplication: Quadratic timeNo matter how dramatic the difference in the constants, the quadratic curve will eventually dominate the linear curveIs there a sub-linear time method for addition?Any addition algorithm takes Ω(n) timeClaim: Any algorithm for addition must read all of the input bitsProof: Suppose there is a mystery algorithm A that does not examine each bitGive A a pair of numbers. There must be some unexamined bit position i in one of the numbers* * * * * * * * ** * * * * * * * ** * * * * * * * * *A did notread this bitat position iAny addition algorithm takes Ω(n) timeIf A is not correct on the inputs, we found a bugIf 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.Grade school addition can’t be improved upon by more than a constant factorGrade School Addition: Θ(n) time.Furthermore, 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?Divide And ConquerAn approach to faster algorithms:DIVIDE a problem into smaller subproblemsCONQUER them recursivelyGLUE the answers together so as to obtain the answer to the larger problemX = Y = a bc dX = a 2n/2 + bn/2 bitsn/2 bitsn bitsX × Y = ac 2n + (ad + bc) 2n/2 + bd XYMultiplication of 2 n-bit numbers Y = c 2n/2 + dMultiplication 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 XYelse break X into a;b and Y into c;dreturn 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)X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc d1234*213912345678 * 2139427612*21 12*39 34*21 34*391*2 1*1 2*2 2*12 1 4 2Hence: 12*21 = 2*102 + (1 + 4)101 + 2 = 2521234*42765678*21395678*4276Multiplying (Divide & Conquer style)X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc d1234*2139 1234*4276 5678*2139 5678*427612345678 * 2139427612*21 12*39 34*21 34*39252 468 714 1326*104 + *102 + *102 + *1 = 2639526Multiplying (Divide & Conquer style)X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc d1234*2139 1234*4276 5678*2139 5678*427612345678 * 213942762639526 5276584 12145242 24279128*108 + *104 + *104 + *1= 264126842539128Multiplying (Divide & Conquer style)X = Y = X × Y = ac 10n + (ad + bc) 10n/2 + bd a bc d12345678 * 21394276= 264126842539128Divide, Conquer, and GlueMULT(X,Y)if |X| = |Y| = 1 then return XY, else…Divide, Conquer, and GlueMULT(X,Y):X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):Mult(a,c)Mult(a,d)Mult(b,c)Mult(b,d)X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):Mult(a,c)Mult(a,d)Mult(b,c)Mult(b,d)X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):acMult(a,d)Mult(b,c)Mult(b,d)X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):acMult(a,d)Mult(b,c)Mult(b,d)X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):acadMult(b,c)Mult(b,d)X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):acadMult(b,c)Mult(b,d)X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):acadbcMult(b,d)X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):acadbcMult(b,d)X=a;b Y=c;d Divide, Conquer, and GlueMULT(X,Y):acadbcbdXY = ac2n +(ad+bc)2n/2 + bdTime required by MULTT(n) = time taken by MULT on two n-bit numbersWhat is T(n)? What is its growth rate? Big Question: Is it Θ(n2)?T(n) = 4 T(n/2) conquering time
View Full Document