This divide-and-conquer multiplication algorithm involves four recursive calls to \mathsf {mul} on inputs of size n/2, two shifts (which take constant time), and three n-bit additions (which take \Theta (n)). Hence we can write a recurrence for the time M(n) taken by \mathsf {mul} on n-bit inputs:
\begin {align*} M(1) &= \Theta (1) \\ M(n) &= 4M(n/2) + \Theta (n) \end {align*} But what is a closed form for M(n)? Is it better than n^2?
We can unroll this recurrence into a recursion tree to figure out how much total work happens.
- At the root, an amount of work happens proportional to the input size, n.
- At the first level of recursion, there are four recursive calls, each on n/2-bit numbers, giving a total something like 4 \cdot (n/2) = 2n.
- At the second level, there are sixteen recursive calls on n/4-bit inputs, giving a total of 4n.
- In general, at depth k, there are 4^k recursive calls, each on an input of size n/2^k, for a total of 4^k \cdot n/2^k = 2^k n.
The tree has height
\lg n. Therefore, the total amount of work in the whole tree (noting that
2^{\lg n} = n) is
\begin {align*}
\sum _{k = 0}^{\log _2 n} 2^k n
&= n + 2n + 4n + \dots + 2^{\lg n} n \\
&= n + 2n + 4n + \dots + n^2 \\
&= (1 + 2 + 4 + \dots + n) \cdot n \\
&= (2n - 1) \cdot n \\
&= \Theta (n^2)
\end {align*}
Drat! This is no better than the
original, naïve algorithm. But it is a good exercise, and gives us insight into more
general situations—not to mention that this approach can be salvaged by
doing something a bit more clever when we split up the problem into
subproblems. But first, let's prove a more general theorem.