This preview shows pages 1–2. Sign up to view the full content.
This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Illinois Institute of Technology Department of Computer Science Lecture 17: March 31 CS 330 Discrete Mathematics Spring Semester, 2008 1 Divideandconquer algorithms 1.1 Multiplying numbers How do we multiply two ndigit numbers? First we multiply the first number by the rightmost digit of the second number. Then we multiply the first number by the second digit of the second number, and so on. Finally, we add the first result to the second result shifted left one digit, add that to the third result shifted left two digits, and so on. ( ) ( ) ( ) ( ) ( ) . . . ( ) ( ) n numbers n digits Treating additions and multiplications of digits as our atomic operations, we can accomplish this in ( n 2 ) operations. Let us consider a divideandconquer algorithm. 1 We divide each ndigit number into two n 2digit numbers: ( ) ( ) A B ( ) ( ) ( ) ( ) A 1 A B 1 B Since we are dividing A and B in half, we know that A = 10 n 2 A 1 + A and B = 10 n 2 B 1 + B . So: AB = (10 n 2 A 1 + A )(10 n 2 B 1 + B ) = 10 n A 1 B 1 + 10 n 2 ( A 1 B + A B 1 ) + A B This yields four multiplications of n 2digit numbers, as well as ( n ) additions and shifts. This leads us to the recurrence: T (1) = 1 T ( n ) = 4 T n 2 + kn 1 This is discussed in Rosen, page 475, example 4. CS 330Spring, 2008...
View
Full
Document
 Spring '08
 Reingold,EdwardM.
 Algorithms

Click to edit the document details