Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science S. Rudich V. Adamchik CS 15-251 Spring 2006 Lecture 18March.

Slides:



Advertisements
Similar presentations
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture.
Advertisements

CS Divide and Conquer/Recurrence Relations1 Divide and Conquer.
Lecture 3: Parallel Algorithm Design
5/5/20151 Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Lectures on Recursive Algorithms1 COMP 523: Advanced Algorithmic Techniques Lecturer: Dariusz Kowalski.
September 12, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science Victor Adamchik Danny Sleator CS Spring 2010 Lecture.
Divide-and-Conquer Recursive in structure –Divide the problem into several smaller sub-problems that are similar to the original but smaller in size –Conquer.
Lecture 30 CSE 331 Nov 8, HW 7 due today Place Q1, Q2 and Q3 in separate piles I will not accept HWs after 1:15pm DO NOT FORGET TO WRITE DOWN YOUR.
11 Computer Algorithms Lecture 6 Recurrence Ch. 4 (till Master Theorem) Some of these slides are courtesy of D. Plaisted et al, UNC and M. Nicolescu, UNR.
CS Section 600 CS Section 002 Dr. Angela Guercio Spring 2010.
Lecture 3 Nearest Neighbor Algorithms Shang-Hua Teng.
Divide-and-Conquer1 7 2  9 4   2  2 79  4   72  29  94  4.
Data Structures, Spring 2004 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Notes on the analysis of multiplication algorithms.. Dr. M. Sakalli, Marmara University.
Data Structures, Spring 2006 © L. Joskowicz 1 Data Structures – LECTURE 3 Recurrence equations Formulating recurrence equations Solving recurrence equations.
Introduction Credits: Steve Rudich, Jeff Edmond, Ping Xuan.
Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 16March 4,
On Time Versus Input Size Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2004 Lecture 15March 2, 2004Carnegie Mellon University.
Introduction By Jeff Edmonds York University COSC 3101 Lecture 1 So you want to be a computer scientist? Grade School Revisited: How To Multiply Two Numbers.
Analysis of Algorithms
Arithmetic.
October 1, Algorithms and Data Structures Lecture III Simonas Šaltenis Nykredit Center for Database Research Aalborg University
MA/CSSE 473 Day 03 Asymptotics A Closer Look at Arithmetic With another student, try to write a precise, formal definition of “t(n) is in O(g(n))”
Divide-and-Conquer 7 2  9 4   2   4   7
Analysis of Algorithms
Analysis of Algorithms CS 477/677
Computer Algorithms Ch. 2
Dynamic Programming. Well known algorithm design techniques:. –Divide-and-conquer algorithms Another strategy for designing algorithms is dynamic programming.
Great Theoretical Ideas in Computer Science.
1 Computer Algorithms Lecture 7 Master Theorem Some of these slides are courtesy of D. Plaisted, UNC and M. Nicolescu, UNR.
Project 2 due … Project 2 due … Project 2 Project 2.
Recursive Algorithms Introduction Applications to Numeric Computation.
Recursion Jeff Edmonds York University COSC 6111 Lecture 3 Friends & Steps for Recursion Derivatives Recursive Images Multiplying Parsing Ackermann.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
10/25/20151 CS 3343: Analysis of Algorithms Lecture 6&7: Master theorem and substitution method.
Divide & Conquer  Themes  Reasoning about code (correctness and cost)  recursion, induction, and recurrence relations  Divide and Conquer  Examples.
On Time Versus Input Size Great Theoretical Ideas In Computer Science S. Rudich V. Adamchik CS Spring 2006 Lecture 17March 21, 2006Carnegie Mellon.
COMPSCI 102 Introduction to Discrete Mathematics.
J. Elder COSC 3101N Thinking about Algorithms Abstractly Lecture 1. Introduction.
Introduction Pizzanu Kanongchaiyos, Ph.D.
Recurrences David Kauchak cs161 Summer Administrative Algorithms graded on efficiency! Be specific about the run times (e.g. log bases) Reminder:
4/3/2003CSE More Math CSE Algorithms Euclidean Algorithm Divide and Conquer.
Divide and Conquer Andreas Klappenecker [based on slides by Prof. Welch]
Recurrences – II. Comp 122, Spring 2004.
Lecture 5 Today, how to solve recurrences We learned “guess and proved by induction” We also learned “substitution” method Today, we learn the “master.
Divide and Conquer. Recall Divide the problem into a number of sub-problems that are smaller instances of the same problem. Conquer the sub-problems by.
Master Method Some of the slides are from Prof. Plaisted’s resources at University of North Carolina at Chapel Hill.
1 How to Multiply Slides by Kevin Wayne. Copyright © 2005 Pearson-Addison Wesley. All rights reserved. integers, matrices, and polynomials.
Divide and Conquer Faculty Name: Ruhi Fatima Topics Covered Divide and Conquer Matrix multiplication Recurrence.
Grade School Revisited: How To Multiply Two Numbers CS Lecture 4 2 X 2 = 5.
Unary, Binary, and Beyond Great Theoretical Ideas In Computer Science Steven RudichCS Spring 2003 Lecture 2Jan 16, 2003Carnegie Mellon University.
On Time Versus Input Size Great Theoretical Ideas In Computer Science S. Rudich V. Adamchik CS Spring 2006 Lecture 17March 21, 2006Carnegie Mellon.
Mathematical Foundations (Solving Recurrence)
Lecture 3: Parallel Algorithm Design
CS 3343: Analysis of Algorithms
Great Theoretical Ideas in Computer Science
Great Theoretical Ideas in Computer Science
Algorithms with numbers (1) CISC4080, Computer Algorithms
How To Multiply Two Numbers
CS 3343: Analysis of Algorithms
Grade School Revisited: How To Multiply Two Numbers
Generating Functions II
By Jeff Edmonds York University
CS 3343: Analysis of Algorithms
Grade School Revisited: How To Multiply Two Numbers
CSCI 256 Data Structures and Algorithm Analysis Lecture 12
Topic: Divide and Conquer
Introduction to Discrete Mathematics
Algorithms and Data Structures Lecture III
Presentation transcript:

Grade School Revisited: How To Multiply Two Numbers Great Theoretical Ideas In Computer Science S. Rudich V. Adamchik CS Spring 2006 Lecture 18March 23, 2006Carnegie Mellon University 2 X 2 = 5

Time complexity of grade school addition *** *** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** **** + * ** * **** **** T(n) = The amount of time grade school addition uses to add two n-bit numbers We saw that T(n) was linear. T(n) = Θ(n)

Time complexity of grade school multiplication T(n) = The amount of time grade school multiplication uses to add two n-bit numbers We saw that T(n) was quadratic. T(n) = Θ(n 2 ) X * * * * * * * * * * * * * * * * * * * * * * * * * * * * n2n2

Grade school addition Grade school addition is linear time. Is there a sub-linear time method for addition?

Any addition algorithm takes Ω(n) time Claim: 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 not read this bit at position i

Grade school addition can’t be improved upon by more than a constant factor.

Grade school multiplication Grade School Multiplication: Θ(n 2 ) time Is there a clever algorithm to multiply two numbers in linear time?

Repetitive addition int mult( int n, int m) { if( m==0 ) return 0; return mult(n, m-1) + n; } What is the complexity of this algorithm?

Peasant multiplication x

Peasant multiplication Why is it called the “Russian” peasant’s algorithm?

Peasant multiplication It’s also called the Egyptian multiplication.

But if the number does not fit the CPU? Can we do something very different than grade school multiplication?

Divide And Conquer An approach to faster algorithms: 1.DIVIDE a problem into smaller subproblems 2.CONQUER them recursively 3.GLUE the answers together so as to obtain the answer to the larger problem

Multiplication of 2 n-bit numbers X = Y = ab cd X = a 2 n/2 + b Y = c 2 n/2 + d n/2 bits n bits X × Y = ac 2 n + (ad + bc) 2 n/2 + bd X Y

Same thing for decimals X = Y = ab cd X = a 10 n/2 + b Y = c 10 n/2 + d n/2 digits n digits X × Y = ac 10 n + (ad + bc) 10 n/2 + bd

Multiplying (Divide & Conquer style) * * * *4276 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * *4276 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * *4276 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * *4276 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * *4276 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * *4276 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hence: 12*21 = 2* (1 + 4) = X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hence: 12*21 = 2* (1 + 4) = X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx 1*2 1*1 2*2 2*1 xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx Hence: 12*21 = 2* (1 + 4) = 252 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx 1*3 1*9 2*3 2*9 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd * * * *1

Multiplying (Divide & Conquer style) * * * * * *21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd * * * *1

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx * * * *1 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * * *21 12*39 34*21 34*39 xxxxxxxxxxxxxxxxxxxxxxxxx = * * * *1 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * *4276 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * *1 X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Multiplying (Divide & Conquer style) * * * * *1 = X = Y = X × Y = ac 10 n + (ad + bc) 10 n/2 + bd ab cd

Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d

Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d Mult( a,c )Mult( a,d )Mult( b,c ) Mult( b,d )

Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d Mult( a,c ) Mult( a,d )Mult( b,c ) Mult( b,d )

Divide, Conquer, and Glue MULT(X,Y): X=a;b Y=c;d Mult( a,d )Mult( b,c ) Mult( b,d ) ac

MULT(X,Y): X=a;b Y=c;d Mult( a,d ) Mult( b,c ) Mult( b,d ) ac ac, Divide, Conquer, and Glue

MULT(X,Y): X=a;b Y=c;d Mult( b,c ) Mult( b,d ) ac ad ac, ad, Divide, Conquer, and Glue

MULT(X,Y): X=a;b Y=c;d Mult( b,c ) Mult( b,d ) ac ad ac, ad, Divide, Conquer, and Glue

MULT(X,Y): X=a;b Y=c;d Mult( b,d ) ac ad bc ac, ad,bc, Divide, Conquer, and Glue

MULT(X,Y): X=a;b Y=c;d Mult( b,d ) ac ad bc ac, ad,bc, Divide, Conquer, and Glue

MULT(X,Y): X=a;b Y=c;d ac ad bc bd ac, ad,bc,bd XY = ac2 n + (ad+bc)2 n/ 2 + bd

Time required by MULT T(n) = time taken by MULT on two n-bit numbers What is T(n)? What is its growth rate? Big Question: Is it Θ(n 2 )?

X=a;b Y=c;d ac ad bc bd XY = ac2 n + (ad + bc)2 n/2 + bd T(n/2) Time required by MULT

X=a; b Y=c; d ac ad bc bd XY = ac2 n + (ad + bc)2 n/2 + bd T(n/2) T(n) = 4 T(n/2) + (c 1 n + c 2 ) Conquering time divide and glue

Recurrence Relation T(1) = c 3 T(n) = 4 T(n/2) + c 1 n + c 2 What is the growth rate of T(n)?

Let’s keep it simple T(1) = 1 T(n) = 4 T(n/2) + n What is the growth rate of T(n)?

Guess: T(n) = 2n 2 – n Verify: T(1) = 1 and T(n) = 4 T(n/2) + n 2n 2 – n = 4 [2(n/2) 2 – n/2] + n =2n 2 – 2n + n =2n 2 – n Technique: Guess and Verify

Recursion Tree Representation We visualize the recursion as a tree, where each node represents a recursive call. The root is the initial call. Leaves correspond to the exit condition.

T(n) = 4 T(n/2) + n T(n/2) T(n) T(n/4)

Recursion Tree n/2 n n/4 each node reflects the combining step

Recursion Tree n/2 n n/4 n 2n 4n write down the work at each level

= 1*n = 2 * n = 4 * n = 2 * i n = n * n T(n)=n ( h ), where h = log 2 n T(n) = n(2n-1) = Θ(n 2 )

1 + X 1 + X 2 + X 3 + … + X n-1 + X n = 1 + X 1 + X 2 + X 3 + … + X n-1 + X n = X n X - 1 The Geometric Series T(n)=n ( h ), where h = log 2 n T(n) = n(2n-1) = Θ(n 2 )

Divide and Conquer MULT: Θ (n 2 ) time Grade School Multiplication: Θ (n 2 ) time All that work for nothing!

Karatsuba, Anatolii Alexeevich (1937-) In 1962 Karatsuba had formulated the first algorithm to break n 2 barrier!

Multiplication of 2 n-bit numbers X = a 2 n/2 + b Y = c 2 n/2 + d X × Y = ac 2 n + (ad + bc) 2 n/2 + bd z1 = (a + b) (c + d) = ac + ad + bc + bd z2 = ac z3 = bd z4 = z2 – z3 = ac - bd z5 = z1 – z2 – z3 = ad + bc X × Y = z2 2 n + z5 2 n/2 + z3

Karatsuba (1962) T(1) = 1 T(n) = 3 T(n/2) + n What is the growth rate of T(n)?

n T(n/2) T(n) =

n T(n/2) T(n) = n/2 T(n/4)

n T(n) = n/2 T(n/4) n/2 T(n/4) n/2 T(n/4)

n n/2 + n/2 + n/2 Level i is the sum of 3 i copies of n/2 i n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/4 + n/ i

= 1n = 3/2n = 9/4n = (3/2) i n T(n) = n (1+3/2+(3/2) (3/2) log 2 n ) = ?? = (3/2) log n n

The Geometric Series 1 + X 1 + X 2 + X 3 + … + X k-1 + X k = 1 + X 1 + X 2 + X 3 + … + X k-1 + X k = X k X - 1 We have: X = 3/2 k = log 2 n (3/2) × (3/2) log 2 n - 1 ½ 3 × (3 log 2 n /2 log 2 n ) - 2 = 3 × (3 log 2 n /n) - 2 =

Dramatic improvement for large n T(n) = 3 n log 2 3 – 2n = Θ(n log 2 3 ) = Θ(n 1.58… ) A huge savings over Θ(n 2 ) when n gets large.

Recursion Tree Representation f(n/b) f(n) f(n/b 2 ) T(n) = a T(n/b) + f(n) a nodes

Solve the following recurrence T(n) = p T(n/3) + n where p>0. Exercise

T(n) = p T(n/3) + n Let p>3 be an arbitrary parameter. Using the tree method, we obtain T(n) = n(1 + p/3 + p 2 /9 + …+ (p/3) h ) where the tree height is log 3 n. It follows T(n) = Θ(n log 3 p )

Recurrence T(n) = p T(n/3) + n has the following asymptotic solution T(n) = n log 3 p ) T(n) = Θ (n log 3 p ) when p>3.

T(n) = p T(n/3) + n Cases p =1, 2 and 3 should be considered separately If p =3, then T(n) = n log n) T(n) = Θ (n log n)

3-Way Multiplication X = a 1 2 n/4 + a 2 2 n/2 + a 3 Y = b 1 2 n/4 + b 2 2 n/2 + b 3 T(1) = 1 T(n) = 9 T(n/3) + n

3-Way Multiplication T(n) = p T(n/3) + n T(n) = Θ(n log 3 p ) To get an improvement over Karatsuba’s, we have to decrease the number of multiplications to at least 5.

Toom and Cook (1963, 1966) T(1) = 1 T(n) = 5 T(n/3) + n T(n) = Θ(n log 3 5 ) = Θ(n 1.46… )

3-Way Multiplication X = a 1 2 n/4 + a 2 2 n/2 + a 3 Y = b 1 2 n/4 + b 2 2 n/2 + b 3 Is it possible to reduce the number of multiplications to 5?

Multiplication Algorithms Grade Schooln2n2 Karatsuban 1.58… 3-wayn 1.46… FFTn log 2 n Schönhage and Strassenn logn loglogn

Study Bee Asymptotic notations Karatsuba’s algorithm Recursion tree method