Sub topic 16
Uneven Splits: The Akra-Bazzi Method
This chapter assumes you know the master theorem and its three cases, recursion trees, and how to integrate simple powers like u^k and 1/u. It covers the recurrences the master theorem cannot solve because the subproblems have different sizes.
The problem with uneven splits
The master theorem needs every subproblem to have the same size n/b. Many algorithms split unevenly. A quicksort that always splits 1/3 to 2/3 gives T(n) = T(n/3) + T(2n/3) + n. The median-of-medians selection algorithm gives T(n) = T(n/5) + T(7n/10) + n. Neither recurrence fits aT(n/b) + f(n), and no single b describes both calls.
You could draw a recursion tree. For uneven splits the tree is lopsided: one branch reaches the base case much sooner than the others. Summing the levels then gives only upper and lower bounds that you must match by hand. The Akra-Bazzi method gives the answer directly.
The Akra-Bazzi formula
Akra-Bazzi handles recurrences of this form:
T(n) = a₁T(b₁n + h₁(n)) + ... + akT(bkn + h_k(n)) + g(n)
These conditions must hold:
- Each ai > 0 and each 0 < bi < 1.
- g(n) is nonnegative and polynomially bounded. Polynomials, logs, and their products qualify. 2^n does not.
- Each perturbation h_i(n) is O(n / log² n). Floors and ceilings meet this easily.
The method has two steps:
- Find the unique real p with a₁b₁^p + ... + akbk^p = 1.
- Then T(n) = Θ(n^p (1 + ∫₁ⁿ g(u) / u^(p+1) du)).
The exponent p plays the role of logb a. For a single term, a·b^p = 1 means p = log(1/b) a, so the master theorem is a special case.
Worked examples
For the 1/3 to 2/3 split, (1/3)^p + (2/3)^p = 1 holds at p = 1. The integral becomes ∫₁ⁿ u / u² du = ln n. So T(n) = Θ(n(1 + ln n)) = Θ(n log n).
For median of medians, at p = 1 the sum is 1/5 + 7/10 = 0.9, which is less than 1. Each term shrinks as p grows, so p must be below 1. The integral is ∫₁ⁿ u^(-p) du, which is Θ(n^(1-p)) when p < 1. Then T(n) = Θ(n^p · n^(1-p)) = Θ(n). You never need the exact value of p.
You can find p numerically when you want it. The left-hand sum decreases as p increases, so bisection works:
def akra_bazzi_p(terms, lo=-10.0, hi=10.0):
"""Find p with sum(a * b**p) == 1 for terms [(a, b), ...], 0 < b < 1."""
def excess(p):
return sum(a * b ** p for a, b in terms) - 1
for _ in range(100):
mid = (lo + hi) / 2
if excess(mid) > 0:
lo = mid # sum too large, so p must be bigger
else:
hi = mid
return (lo + hi) / 2
print(round(akra_bazzi_p([(1, 1/3), (1, 2/3)]), 4)) # 1.0
print(round(akra_bazzi_p([(1, 1/5), (1, 7/10)]), 4)) # about 0.84
print(round(akra_bazzi_p([(2, 1/2)]), 4)) # 1.0, matches log_2 2Exercise
Solve T(n) = T(n/2) + T(n/4) + n² with the Akra-Bazzi method.
Show answer
At p = 2 the sum is 1/4 + 1/16 = 5/16 < 1, so p < 2. The integral ∫₁ⁿ u² / u^(p+1) du = ∫₁ⁿ u^(1-p) du is Θ(n^(2-p)). Therefore T(n) = Θ(n^p · n^(2-p)) = Θ(n²).
Exercise
Why can floors, as in T(⌊n/3⌋) + T(⌈2n/3⌉) + n, be ignored here?
Show answer
A floor or ceiling changes the argument by less than 1. That perturbation h(n) is O(1), which is O(n / log² n), so the Akra-Bazzi bound is unchanged.