Skip to main content

Proofs: Divide and Conquer

This page proves the formulas used to analyze divide and conquer algorithms and the correctness and cost of several of them. The general techniques (induction, recursion trees, geometric series) are in the Mathematical Background appendix.


The division formula​

Claim. If T(n)=a T(n/b)+c nkT(n) = a \, T(n/b) + c \, n^k for nn larger than the base case, with constants a≥1a \ge 1, b>1b \gt 1, k≥0k \ge 0 and c>0c \gt 0, then

T(n)∈{O(nk)if a<bkO(nklog⁡n)if a=bkO(nlog⁡ba)if a>bkT(n) \in \begin{cases} O(n^k) & \text{if } a \lt b^k \\ O(n^k \log n) & \text{if } a = b^k \\ O(n^{\log_b a}) & \text{if } a \gt b^k \end{cases}

Proof. Assume nn is a power of bb (other sizes only change constants). Draw the recursion tree. At level ii (the root is level 0) there are aia^i calls, each on a problem of size n/bin / b^i, and each does c (n/bi)kc \, (n/b^i)^k non-recursive work. So level ii costs

ai⋅c(nbi)k=c nk(abk)ia^i \cdot c \left( \frac{n}{b^i} \right)^k = c \, n^k \left( \frac{a}{b^k} \right)^i

The tree has log⁡bn+1\log_b n + 1 levels, so T(n)=c nk∑i=0log⁡bnriT(n) = c \, n^k \sum_{i=0}^{\log_b n} r^i with ratio r=a/bkr = a / b^k. This is a geometric series:

  • If r<1r \lt 1 (a<bka \lt b^k), the sum is at most 11−r\frac{1}{1 - r}, a constant: T(n)∈O(nk)T(n) \in O(n^k). The root dominates.
  • If r=1r = 1 (a=bka = b^k), all log⁡bn+1\log_b n + 1 terms equal 1: T(n)=c nk(log⁡bn+1)∈O(nklog⁡n)T(n) = c \, n^k (\log_b n + 1) \in O(n^k \log n). Every level costs the same.
  • If r>1r \gt 1 (a>bka \gt b^k), the last term dominates: the sum is at most rr−1rlog⁡bn\frac{r}{r - 1} r^{\log_b n}, and T(n)∈O ⁣(nk rlog⁡bn)T(n) \in O\!\left(n^k \, r^{\log_b n}\right). Since rlog⁡bn=nlog⁡br=nlog⁡ba−kr^{\log_b n} = n^{\log_b r} = n^{\log_b a - k}, this is O(nlog⁡ba)O(n^{\log_b a}). The leaves dominate: there are alog⁡bn=nlog⁡baa^{\log_b n} = n^{\log_b a} of them.

■\blacksquare

The same argument with ≥\ge instead of ≤\le shows that each bound is in fact Θ\Theta.


The subtraction formula​

Claim. If T(n)=a T(n−b)+c nkT(n) = a \, T(n - b) + c \, n^k for nn larger than the base case, with b≥1b \ge 1, then T(n)∈O(nk+1)T(n) \in O(n^{k+1}) if a=1a = 1, and T(n)∈O(an/b)T(n) \in O(a^{n/b}) if a>1a \gt 1.

Proof. The recursion has about n/bn / b levels. If a=1a = 1, there is a single call per level, with sizes n,n−b,n−2b,…n, n - b, n - 2b, \dots, each costing at most c nkc \, n^k: in total at most nb⋅c nk∈O(nk+1)\frac{n}{b} \cdot c \, n^k \in O(n^{k+1}). (It is also Ω(nk+1)\Omega(n^{k+1}), because the first half of the levels have sizes at least n/2n/2.)

If a>1a \gt 1, level ii has aia^i calls, each of cost at most c nkc \, n^k, so

T(n)≤c nk∑i=0n/bai=c nk⋅an/b+1−1a−1∈O(nkan/b)T(n) \le c \, n^k \sum_{i=0}^{n/b} a^i = c \, n^k \cdot \frac{a^{n/b + 1} - 1}{a - 1} \in O(n^k a^{n/b})

A more careful count, using that the calls at the deep levels have small sizes, removes the factor nkn^k and gives O(an/b)O(a^{n/b}); the polynomial factor is irrelevant anyway next to an exponential. ■\blacksquare


Karatsuba's identity​

Karatsuba's algorithm multiplies two numbers of nn digits by splitting each one in halves, x=x1B+x0x = x_1 B + x_0 and y=y1B+y0y = y_1 B + y_0, where BB is a power of the base with n/2n/2 zeros.

Claim. x⋅yx \cdot y can be computed with only three products of half-size numbers.

Proof. Expanding, xy=x1y1B2+(x1y0+x0y1)B+x0y0x y = x_1 y_1 B^2 + (x_1 y_0 + x_0 y_1) B + x_0 y_0, which seems to need four products. But

(x1+x0)(y1+y0)=x1y1+(x1y0+x0y1)+x0y0(x_1 + x_0)(y_1 + y_0) = x_1 y_1 + (x_1 y_0 + x_0 y_1) + x_0 y_0

so the middle coefficient is (x1+x0)(y1+y0)−x1y1−x0y0(x_1 + x_0)(y_1 + y_0) - x_1 y_1 - x_0 y_0. With P1=x1y1P_1 = x_1 y_1, P2=x0y0P_2 = x_0 y_0 and P3=(x1+x0)(y1+y0)P_3 = (x_1 + x_0)(y_1 + y_0), we get xy=P1B2+(P3−P1−P2)B+P2x y = P_1 B^2 + (P_3 - P_1 - P_2) B + P_2: three products, plus additions and shifts, which are O(n)O(n). ■\blacksquare

The recurrence is T(n)=3T(n/2)+O(n)T(n) = 3T(n/2) + O(n), with a=3>bk=2a = 3 \gt b^k = 2, so T(n)∈O(nlog⁡23)≈O(n1.585)T(n) \in O(n^{\log_2 3}) \approx O(n^{1.585}) by the division formula.


Euclid's algorithm is correct and fast​

Claim 1. For integers a≥0a \ge 0 and b>0b \gt 0, gcd⁡(a,b)=gcd⁡(b,a mod b)\gcd(a, b) = \gcd(b, a \bmod b).

Proof. Write a=qb+ra = q b + r with r=a mod br = a \bmod b. Any common divisor of aa and bb divides r=a−qbr = a - q b, so it is a common divisor of bb and rr. Conversely, any common divisor of bb and rr divides a=qb+ra = q b + r. The two pairs have exactly the same common divisors, so the same greatest one. ■\blacksquare

Since gcd⁡(a,0)=a\gcd(a, 0) = a, repeating the step until the second number is 0 gives the gcd. The algorithm terminates because the second number is a non-negative integer that strictly decreases (a mod b<ba \bmod b \lt b).

Claim 2. Every two iterations, the first number is at least halved, so the algorithm performs O(log⁡a)O(\log a) iterations.

Proof. After one iteration the pair (a,b)(a, b) becomes (b,r)(b, r) with r=a mod br = a \bmod b; after two, the first number is rr. If b≤a/2b \le a/2, then r<b≤a/2r \lt b \le a/2. If b>a/2b \gt a/2, then a mod b=a−b<a/2a \bmod b = a - b \lt a/2. Either way, r<a/2r \lt a/2. ■\blacksquare

(The worst case is two consecutive Fibonacci numbers, a result due to Lamé, 1844: the number of iterations is at most about five times the number of decimal digits of the smaller number.)


Quickselect runs in expected linear time​

Quickselect partitions the array around a pivot, as Quicksort does, but recurses only into the part that contains the position it is looking for.

Claim. With a random pivot, the expected number of comparisons is O(n)O(n) (at most 8n8n).

Proof. Call a pivot good if it falls in the middle half of the current range (between the 25% and the 75% positions in sorted order). A random pivot is good with probability 1/21/2, and after a good pivot the remaining range has at most 3/43/4 of the elements. Divide the execution into phases: phase jj lasts while the range has between (3/4)j+1n(3/4)^{j+1} n and (3/4)jn(3/4)^j n elements. Each partition in phase jj costs at most (3/4)jn(3/4)^j n comparisons, and the phase ends at the first good pivot, so its expected number of partitions is at most 2 (the expected number of tries until a success with probability 1/21/2). Adding over the phases:

E[comparisons]≤∑j≥02(34)jn=2⋅4⋅n=8n\mathbb{E}[\text{comparisons}] \le \sum_{j \ge 0} 2 \left( \frac{3}{4} \right)^j n = 2 \cdot 4 \cdot n = 8n

■\blacksquare

A more precise analysis gives at most about 3.4 n3.4\,n comparisons for the median. In the worst case (always the smallest or largest pivot) Quickselect is still O(n2)O(n^2); the median-of-medians algorithm of Blum, Floyd, Pratt, Rivest and Tarjan guarantees O(n)O(n) in every case.

Saved only in your browser
Was this lesson helpful?
Questions, or something wrong?

Errors, suggestions and public questions open a form on GitHub, visible to everyone.