Integer multiplication: Karatsuba`s algo

advertisement
Divide-and-Conquer
Integer Multiplication: Karatsuba (and Ofman)’s algo
R. Inkulu
http://www.iitg.ac.in/rinkulu/
(Integer Multiplication)
1/9
Description
Given two n-bit integers x and y, multiply x and y.
Assume RAM model computation: primitive operations are arithmatic operations on a pair of bits,
comparison between two bits and copying a bit value to a bit position; each takes O(1) time
(Integer Multiplication)
2/9
Brute-force (review)
X=
11 00
Y=
11 01
11 00
00 00
11 00
11 00
1001 1100
Takes O(n2 ) time as we had seen. Further, even a simple recursive algo
took O(n2 ) time.
(Integer Multiplication)
3/9
Algorithm: four recursive calls per level
x1
X=
x0
11 00
Y= y11 01y
0
1
0 0 0
0 1 1
1 0 0 1
x 0 * y0
x1 * y 0 + x 0* y 1
x1 * y 1
11 00
x 1 * y0
x 1 * y1
0 0 0 0 x0 * y0
11 00
x0 * y1
11 00
1 0 0 1 1 1 0 0
• divide each number into two strings of higher-order and lower-order bits
xy = (x1 ∗ 2n/2 + x0 ) ∗ (y1 ∗ 2n/2 + y0 )
= ((x1 ∗ y1 ) ∗ 2n ) + ((x1 ∗ y0 + x0 ∗ y1 ) ∗ 2n/2 ) + (x0 ∗ y0 )
• conquer four multiplications
• combine conquered solutions with constant number of n-bit word
additions
(Integer Multiplication)
4/9
Analysis
T(n) ≤ 4T(n/2) + cn
hence, the time complexity is Θ(nlg2 4 ) = Θ(n2 )
(Integer Multiplication)
5/9
Algorithm: three recursive calls per level
• reduce the number of recursive calls by carefully rearranging the terms:
xy = (x1 ∗ 2n/2 + x0 ) ∗ (y1 ∗ 2n/2 + y0 )
= ((x1 ∗ y1 ) ∗ 2n ) + ((x1 ∗ y0 + x0 ∗ y1 ) ∗ 2n/2 ) + (x0 ∗ y0 )
= ((x1 ∗ y1 ) ∗ 2n ) + ((((x1 + x0 ) ∗ (y1 + y0 )) − (x1 ∗ y1 ) − (x0 ∗ y0 )) ∗
2n/2 ) + (x0 ∗ y0 )
• conquer three multiplications
• combine conquered solutions with constant number of n-bit word
additions
(Integer Multiplication)
6/9
Analysis
T(n) ≤ 3T(n/2) + cn
hence, the time complexity is Θ(nlg2 3 ) = Θ(n1.584963 )
(Integer Multiplication)
7/9
Correctness
immediate from algebra
(Integer Multiplication)
8/9
Algorithms with better worst-case asymptotic time
Karastuba and Ofman algo ’60: O(n1.584963 )
Toom-Cook algo ’66: O(n1.465 )
SchönhageStrassen algo ’71: O(n(lg n)(lg lg n))
∗
Fürer’s algo ’07: O(n(lg n)(24 lg n ))
∗
Harvey-Hoeven-Lecerf algo ’14: O(n(lg n)(23 lg n ))
(Integer Multiplication)
9/9
Algorithms with better worst-case asymptotic time
Karastuba and Ofman algo ’60: O(n1.584963 )
Toom-Cook algo ’66: O(n1.465 )
SchönhageStrassen algo ’71: O(n(lg n)(lg lg n))
∗
Fürer’s algo ’07: O(n(lg n)(24 lg n ))
∗
Harvey-Hoeven-Lecerf algo ’14: O(n(lg n)(23 lg n ))
Conjectured worst-case lower bound on the problem: Ω(n lg n)
(Integer Multiplication)
9/9
Download