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 for larger than the base case, with constants , , and , then
Proof. Assume is a power of (other sizes only change constants). Draw the recursion tree. At level (the root is level 0) there are calls, each on a problem of size , and each does non-recursive work. So level costs
The tree has levels, so with ratio . This is a geometric series:
- If (), the sum is at most , a constant: . The root dominates.
- If (), all terms equal 1: . Every level costs the same.
- If (), the last term dominates: the sum is at most , and . Since , this is . The leaves dominate: there are of them.
The same argument with instead of shows that each bound is in fact .
The subtraction formula
Claim. If for larger than the base case, with , then if , and if .
Proof. The recursion has about levels. If , there is a single call per level, with sizes , each costing at most : in total at most . (It is also , because the first half of the levels have sizes at least .)
If , level has calls, each of cost at most , so
A more careful count, using that the calls at the deep levels have small sizes, removes the factor and gives ; the polynomial factor is irrelevant anyway next to an exponential.
Karatsuba's identity
Karatsuba's algorithm multiplies two numbers of digits by splitting each one in halves, and , where is a power of the base with zeros.
Claim. can be computed with only three products of half-size numbers.
Proof. Expanding, , which seems to need four products. But
so the middle coefficient is . With , and , we get : three products, plus additions and shifts, which are .
The recurrence is , with , so by the division formula.
Euclid's algorithm is correct and fast
Claim 1. For integers and , .
Proof. Write with . Any common divisor of and divides , so it is a common divisor of and . Conversely, any common divisor of and divides . The two pairs have exactly the same common divisors, so the same greatest one.
Since , 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 ().
Claim 2. Every two iterations, the first number is at least halved, so the algorithm performs iterations.
Proof. After one iteration the pair becomes with ; after two, the first number is . If , then . If , then . Either way, .
(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 (at most ).
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 , and after a good pivot the remaining range has at most of the elements. Divide the execution into phases: phase lasts while the range has between and elements. Each partition in phase costs at most 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 ). Adding over the phases:
A more precise analysis gives at most about comparisons for the median. In the worst case (always the smallest or largest pivot) Quickselect is still ; the median-of-medians algorithm of Blum, Floyd, Pratt, Rivest and Tarjan guarantees in every case.
Errors, suggestions and public questions open a form on GitHub, visible to everyone.