Grade School Revisited: How To Multiply Two NumbersSlide 2Time complexity of grade school multiplicationSlide 4Slide 5Slide 6Slide 7Slide 8Slide 9Slide 10Divide And ConquerMultiplication of 2 n-bit numbersSlide 13Same thing for numbers in decimalMultiplying (Divide & Conquer style)Slide 16Slide 17Slide 18Divide, Conquer, and GlueSlide 20Slide 21Slide 22Slide 23Slide 24Slide 25Slide 26Slide 27Slide 28Slide 29Time required by MULTRecurrence RelationSimplified Recurrence RelationSlide 33Slide 34Slide 35Slide 36Slide 37Divide and Conquer MULT: Θ(n2) time Grade School Multiplication: Θ(n2) timeGauss’ Complex PuzzleGauss’ $3.05 MethodSlide 41Karatsuba, Anatolii Alexeevich (1937-2008)Gaussified MULT (Karatsuba 1962)Slide 44Slide 45Slide 46Slide 47Slide 48Slide 49Dramatic Improvement for Large nSlide 513-Way MultiplicationSlide 53Slide 54Is it possible to reduce the number of multiplications to 5?Slide 56Further GeneralizationsSlide 58Is it always possible to find such 2k-1 multiplications?T(n) = aT(n/b) + f(n)Multiplication AlgorithmsSlide 62Study BeeGrade School Revisited:How To Multiply Two NumbersGreat Theoretical Ideas In Computer ScienceVictor AdamchikDanny SleatorCS 15-251 Spring 2010Lecture 15 Mar 02, 2010 Carnegie Mellon University+T(n) = amount of time grade school addition uses to add two n-bit numbers* * * * * * * * * ** * * * * * * * * ** * * * * * * * * * *Time complexity of grade school additionT(n) is linear:T(n) = Θ(n)Time complexity of grade school multiplicationT(n) = The amount of time grade school multiplication uses to multiply two n-bit numbersT(n) is quadratic:T(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) Claim: 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 at position i in one of the numbers* * * * * * * * ** * * * * * * * ** * * * * * * * * *A did notread this bitat position iAny addition algorithm takes Ω(n)If A is not correct on the inputs, we found a bugIf A is correct, flip the bit at position i 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 decimalX = 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) + O(n) conquering time divide and glueRecurrence RelationT(1) = 1T(n) = 4 T(n/2) + O(n)Simplified Recurrence RelationT(1) = 1T(n) = 4 T(n/2) + nconquering time divide and gluen=T(n)T(n/2) T(n/2) T(n/2) T(n/2)n=T(n)T(n/2) T(n/2) T(n/2)n/2T(n/4)T(n/4)T(n/4)T(n/4)n=T(n)T(n/2) T(n/2)n/2T(n/4)T(n/4)T(n/4)T(n/4)n/2T(n/4)T(n/4)T(n/4)T(n/4)n n/2 + n/2 + n/2 + n/2. . . . . . . . . . . . . . . . . . . . . . . . . . 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1012iLevel i is the sum of 4i copies of n/2in n/2 + n/2 + n/2 + n/2Level i is the sum of 4i copies of n/2i. . . . . . . . . . . . . . . . . . . . . . . . . . 1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+1+12n =4n =2in =(n)n =1n =n(1+2+4+8+ . . . +n) = n(2n-1) = 2n2-nDivide and Conquer MULT: Θ(n2) time
View Full Document