0% found this document useful (0 votes)
87 views3 pages

Algorithms - CMSC-272XX Divide and Conquer: The Karatsuba Algorithm (Multiplication of Large Integers)

The Karatsuba algorithm provides a more efficient way to multiply large integers that runs in O(nlog3) time rather than the O(n2) time of standard multiplication. It works by dividing the integers into halves, multiplying the halves together, and combining the results. The same approach can also be used to multiply polynomials of degree n in O(nlog3) time rather than O(n2) time by dividing the polynomials into halves.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
87 views3 pages

Algorithms - CMSC-272XX Divide and Conquer: The Karatsuba Algorithm (Multiplication of Large Integers)

The Karatsuba algorithm provides a more efficient way to multiply large integers that runs in O(nlog3) time rather than the O(n2) time of standard multiplication. It works by dividing the integers into halves, multiplying the halves together, and combining the results. The same approach can also be used to multiply polynomials of degree n in O(nlog3) time rather than O(n2) time by dividing the polynomials into halves.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 3

Algorithms – CMSC-272XX

Divide and Conquer: The Karatsuba algorithm


(multiplication of large integers)
Instructor: László Babai

Updated 01-13-2021

NOTATION. In this note, log will always mean log2 (base-2 logarithm).

The Karatsuba algorithm provides a striking example of how the “Divide


and Conquer” technique can achieve an asymptotic speedup over an ancient
algorithm.
The classroom method of multiplying two n-digit integers requires Θ(n2 )
digit operations. We shall show that a simple recursive algorithm solves the
problem in O(nlog 3 ) digit operations. (Note: log 3 ≈ 1.58.) This represents
considerable savings in the asymptotic rate of growth of the number of digit-
operations.
We describe the procedure in pseudocode. We assume that the number
of digits is a power of 2 so we will not need to worry about rounding. This
assumption is justified by padding the number with initial zeros; this will
increase the vaule of n by less than a factor of 2, immaterial for our estimates.

Procedure Karatsuba(X, Y )

Input: X, Y : n-digit integers.


Output: the product P := XY .
Comment: We assume n is a power of 2.

1. if n = 1 then use multiplication table to find P := XY

2. else split X, Y in half:

3. X =: 10n/2 X1 + X2

4. Y =: 10n/2 Y1 + Y2

5. Comment: X1 , X2 , Y1 , Y2 each have n/2 digits.

6. U := Karatsuba(X1 , Y1 )

7. V := Karatsuba(X2 , Y2 )

1
8. W := Karatsuba(X1 − X2 , Y1 − Y2 )

9. Z := U + V − W

10. P := 10n U + 10n/2 Z + V

11. Comment: So U = X1 Y1 , V = X2 Y2 , W = (X1 − X2 )(Y1 − Y2 ),


and therefore Z = X1 Y2 + X2 Y1 . Finally we conclude that
P = 10n X1 Y1 + 10n/2 (X1 Y2 + X2 Y1 ) + X2 Y2 = XY .

12. return P

Analysis. This is a recursive algorithm: during execution, it calls smaller


instances of itself.
Let M (n) denote the number of digit-multiplications (line 1) required by
the Karatsuba algorithm when multiplying two n-digit integers (n = 2k ).
In lines 6,7,8 the procedure calls itself three times on n/2-digit integers;
therefore
M (n) = 3M (n/2). (1)
This equation is a simple recurrence which we may solve directly as follows.
Applying equation (1) to M (n/2) we obtain M (n/2) = 3M (n/4); therefore
M (n) = 9M (n/4). Continuing similarly we see that M (n) = 27M (n/8),
and it follows by induction on i that for every i (i ≤ k),

M (n) = 3i M (n/2i ).

Setting i = k we find that M (n) = 3k M (n/2k ) = 3k M (1) = 3k . Notice


that k = log n (recall: in this note, log refers to base-2 logarithm), therefore
log M (n) = k log 3 and hence M (n) = 2log M (n) = 2k log 3 = (2k )log 3 = nlog 3 .
It would seem that we reduced the number of digit-multiplications to
nlog 3 at the cost of an increased number of additions (lines 9, 10). Appear-
ances are deceptive: actually, the procedure achieves similar savings in terms
of the total number of digit-operations (additions as well as multiplications).
To see this, let T (n) be the total number of digit-operations (additions,
multiplications, bookkeeping (copying digits, maintaining links)) required
by the Karatsuba algorithm. Then

T (n) = 3T (n/2) + O(n) (2)

where the term 3T (n/2) comes, as before, from lines 6,7,8; the additional
O(n) term is the number of digit-additions required to perform the additions

2
and subtractions in lines 9 and 10. The O(n) term also includes bookkeeping
costs.
We shall learn later how to analyse recurrences of the form (2). It turns
out that the additive O(n) term does not change the rate of growth, and
the result will still be
T (n) = O(nlog 3 ). (3)

Theorem 1 (Karatsuba). Multiplication of n-digits integers can be per-


formed at the cost of O(nlog 3 ) digit operations and bookkeeping operations.

Comment: While this was not the last word on the complexity of integer
multiplication (methods based on Fast Fourier Transform are even faster),
it inspired a lot of further progress, including Strassen’s fast matrix multi-
plication.

Multiplication of polynomials of large degree

The same method works for the multiplication of polynomials.


The model:
Input: a pair of polynomials of degree ≤ n−1, X(t) = a0 +a1 t+· · ·+an−1 tn−1
and Y (t) = b0 + b1 t + · · · + bn−1 tn−1 , each with n coefficient (zero coefficients
permitted). The coefficients are real or complex numbers; we refer to these
domains as the domain of scalars. Each polynomial is given as an array of
coefficients.
Output: the product of the two polynomials, as an array of its 2n − 1
coefficients.
Cost: arithmetic operations with scalars (addition, subtraction, multipli-
cation) and copying scalars at unit cost. Bookkeeping operations (copying
links, arithmetic with addresses) at unit cost.

Exercise 2. Multiplication of polynomials of degree ≤ n can be performed


at a cost of O(nlog 3 ) arithmetic operations on scalars, and bookkeeping
operations.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy