Karatsuba Integer Multiplication
Problem
Problem. Given two positive $n$-bit integers $x$ and $y$, compute their product $xy$.
The elementary school algorithm runs in $O(n^2)$. Our goal is a subquadratic algorithm.
Note: Efficiency is measured with respect to the input size (number of bits). $O(PQ)$ is exponential in the input size.
Divide-and-Conquer Approach
Split $x = x_1 \cdot 2^{\lfloor n/2 \rfloor} + x_0$ and $y = y_1 \cdot 2^{\lfloor n/2 \rfloor} + y_0$:
\[xy = x_1 y_1 \cdot 2^{2\lfloor n/2 \rfloor} + (x_1 y_0 + x_0 y_1) \cdot 2^{\lfloor n/2 \rfloor} + x_0 y_0\]4-recursion version:
1 | |
Theorem 1. $T(n) = 4T(n/2) + \Theta(n) = \Theta(n^2)$ — no improvement over the elementary algorithm.
Karatsuba Algorithm
Key observation: $x_1 y_0 + x_0 y_1 = (x_1 + x_0)(y_1 + y_0) - x_1 y_1 - x_0 y_0$
This reduces 4 multiplications to 3.
1 | |
Theorem 2 (Correctness). Proved by induction on $n$:
\[p_2 \cdot 2^{2\lfloor n/2 \rfloor} + (p_1 - p_2 - p_3) \cdot 2^{\lfloor n/2 \rfloor} + p_3 = xy\]Theorem 3 (Time complexity). The Karatsuba algorithm runs in $O(n^{\log_2 3})$ time.
Proof. $T(n) = 3T(n/2) + cn$. By the Master Theorem:
\[T(n) = \Theta(n^{\log_2 3}) = O(n^{1.59}) \qquad \square\]