TExcelTAD
Home › Discrete Mathematics › Divide-and-Conquer Recurrences

Divide-and-Conquer Recurrences

Divide-and-conquer algorithms split a problem of size nn into aa subproblems of size n/bn/b, solve them recursively, and combine the results with g(n)g(n) extra work:

T(n)=a T(n/b)+g(n)T(n) = a\,T(n/b) + g(n)

Merge sort (a=2,b=2,g=na = 2, b = 2, g = n), binary search (a=1,b=2,g=1a = 1, b = 2, g = 1), Karatsuba multiplication (a=3,b=2,g=na = 3, b = 2, g = n) and Strassen's matrix multiplication (a=7,b=2,g=n2a = 7, b = 2, g = n^2) all fit. Instead of unrolling each one, use a general theorem.

The recursion tree

Draw the recursion as a tree. Level ii has aia^i subproblems of size n/bin/b^i, doing aig(n/bi)a^i g(n/b^i) work in total. There are log⁡bn\log_b n levels and alog⁡bn=nlog⁡baa^{\log_b n} = n^{\log_b a} leaves. So the outcome is a contest between the leaves, nlog⁡ban^{\log_b a}, and the top-level work g(n)g(n):

  • if the work per level shrinks going down, the root dominates: T=Θ(g(n))T = \Theta(g(n));
  • if it stays equal, every level counts: T=Θ(g(n)log⁡n)T = \Theta(g(n) \log n);
  • if it grows, the leaves dominate: T=Θ(nlog⁡ba)T = \Theta(n^{\log_b a}).

The master theorem

Let c=log⁡bac = \log_b a (the critical exponent). For g(n)=Θ(nk)g(n) = \Theta(n^k):

Case Condition Solution
1 k<ck < c T(n)=Θ(nc)T(n) = \Theta(n^{c})
2 k=ck = c T(n)=Θ(nclog⁡n)T(n) = \Theta(n^{c} \log n)
3 k>ck > c T(n)=Θ(nk)T(n) = \Theta(n^{k})
Algorithm aa, bb, gg c=log⁡bac = \log_b a Result
binary search 1, 2, 11 0 case 2: Θ(log⁡n)\Theta(\log n)
merge sort 2, 2, nn 1 case 2: Θ(nlog⁡n)\Theta(n \log n)
Karatsuba 3, 2, nn ≈1.585\approx 1.585 case 1: Θ(n1.585)\Theta(n^{1.585})
Strassen 7, 2, n2n^2 ≈2.807\approx 2.807 case 1: Θ(n2.807)\Theta(n^{2.807})
tree traversal 2, 2, 11 1 case 1: Θ(n)\Theta(n)
"halve and scan" 1, 2, nn 0 case 3: Θ(n)\Theta(n)

The Akra–Bazzi theorem generalizes this to unequal splits, T(n)=∑iaiT(bin)+g(n)T(n) = \sum_i a_i T(b_i n) + g(n), and also shows that floors and ceilings in the recurrence do not change the asymptotic answer.

Checking numerically

Compute T(n)T(n) exactly and divide by the predicted growth: the ratio should level off at a constant.

python · try it yourself
import math
from functools import lru_cache

def make(a, b, g):
    @lru_cache(None)
    def T(n):
        return 1 if n <= 1 else a * T(n // b) + g(n)
    return T

cases = {
    "merge sort   2T(n/2)+n  ~ n log n": (make(2, 2, lambda n: n), lambda n: n * math.log2(n)),
    "Karatsuba    3T(n/2)+n  ~ n^1.585": (make(3, 2, lambda n: n), lambda n: n ** math.log2(3)),
    "Strassen     7T(n/2)+n^2 ~ n^2.807": (make(7, 2, lambda n: n * n), lambda n: n ** math.log2(7)),
    "halve+scan   T(n/2)+n   ~ n": (make(1, 2, lambda n: n), lambda n: n),
}
for name, (T, pred) in cases.items():
    ratios = [T(2 ** k) / pred(2 ** k) for k in (8, 12, 16, 20)]
    print(f"{name}: " + "  ".join(f"{r:.3f}" for r in ratios))
merge sort   2T(n/2)+n  ~ n log n: 1.125  1.083  1.062  1.050
Karatsuba    3T(n/2)+n  ~ n^1.585: 2.922  2.985  2.997  2.999
Strassen     7T(n/2)+n^2 ~ n^2.807: 2.318  2.332  2.333  2.333
halve+scan   T(n/2)+n   ~ n: 1.996  2.000  2.000  2.000

Each row approaches a constant, confirming the predicted order of growth.

Why it matters: Karatsuba

Schoolbook multiplication of two nn-digit numbers takes Θ(n2)\Theta(n^2) digit operations. Karatsuba splits each number into halves, x=x1B+x0x = x_1 B + x_0, and computes x1y1x_1y_1, x0y0x_0y_0 and (x1+x0)(y1+y0)(x_1 + x_0)(y_1 + y_0): three half-size products instead of four, since the middle term is (x1+x0)(y1+y0)−x1y1−x0y0(x_1 + x_0)(y_1 + y_0) - x_1y_1 - x_0y_0. Going from a=4a = 4 to a=3a = 3 changes the exponent from 2 to 1.585. CPython uses Karatsuba for large integers.

python · try it yourself
def karatsuba(x, y):
    if x < 10 or y < 10:
        return x * y
    m = max(len(str(x)), len(str(y))) // 2
    B = 10 ** m
    x1, x0 = divmod(x, B)
    y1, y0 = divmod(y, B)
    z2 = karatsuba(x1, y1)
    z0 = karatsuba(x0, y0)
    z1 = karatsuba(x1 + x0, y1 + y0) - z2 - z0
    return z2 * B * B + z1 * B + z0

a, b = 31415926535897932384, 27182818284590452353
print(karatsuba(a, b))
print(karatsuba(a, b) == a * b)
853973422267356706510386982199967699552
True

In software

The master theorem is the quick way to read the complexity of recursive code. It also explains design choices: a recursive algorithm that does O(n)O(n) work per call but makes only one recursive call on half the input (quickselect's average case, binary-search-like scans) is linear overall, while one making two calls on halves with linear work is nlog⁡nn \log n.

Exercises

Exercise 1: Apply the theorem

Solve T(n)=4T(n/2)+nT(n) = 4T(n/2) + n, T(n)=4T(n/2)+n2T(n) = 4T(n/2) + n^2 and T(n)=4T(n/2)+n3T(n) = 4T(n/2) + n^3.

Answer

Θ(n2)\Theta(n^2), Θ(n2log⁡n)\Theta(n^2 \log n), Θ(n3)\Theta(n^3).

Solution

c=log⁡24=2c = \log_2 4 = 2. With k=1<2k = 1 < 2: case 1. With k=2k = 2: case 2. With k=3>2k = 3 > 2: case 3.

Exercise 2: Ternary search

An algorithm splits the input into 3 parts, recurses on one, and does constant extra work. What is its running time?

Answer

Θ(log⁡n)\Theta(\log n).

Solution

T(n)=T(n/3)+1T(n) = T(n/3) + 1: a=1a = 1, b=3b = 3, c=0=kc = 0 = k, case 2: Θ(n0log⁡n)=Θ(log⁡n)\Theta(n^0 \log n) = \Theta(\log n).

Exercise 3: Count operations (code)

Count the single-digit multiplications Karatsuba performs for two 2k2^k-digit numbers (k=1,…,8k = 1, \ldots, 8) and compare with 3k3^k and schoolbook 4k4^k.

Answer

Exactly 3k3^k, versus 4k4^k for schoolbook.

Solution

M(n)=3M(n/2)M(n) = 3M(n/2) with M(1)=1M(1) = 1 gives M(2k)=3k=nlog⁡23M(2^k) = 3^k = n^{\log_2 3}.

python · try it yourself
M = lambda n: 1 if n == 1 else 3 * M(n // 2)
for k in range(1, 9):
    n = 2 ** k
    print(n, M(n), 3 ** k, 4 ** k)
2 3 3 4
4 9 9 16
8 27 27 64
16 81 81 256
32 243 243 1024
64 729 729 4096
128 2187 2187 16384
256 6561 6561 65536

Going further: The Master Theorem and When the Master Theorem Fails in the Algorithm Theory track continue this topic.

Test yourself

What is the critical exponent cc for T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2?

  1. 2
  2. 3
  3. 4
  4. 8

Merge sort's recurrence T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n) falls into which case?

  1. Case 1 (leaves dominate)
  2. Case 2 (all levels equal)
  3. Case 3 (root dominates)
  4. None

Why is Karatsuba faster than schoolbook multiplication?

  1. It uses floating point
  2. It replaces 4 half-size products by 3
  3. It avoids recursion
  4. It uses the FFT