Sub topic 14
Floors, Ceilings, and Base Cases Without Worry
This chapter assumes you can apply the master theorem, build a recursion tree, and run a substitution proof by induction. Real code rarely splits inputs into exact halves. The question here is when you can ignore the rounding and the base case, and how to justify doing so.
The Problem with "n/2"
Merge sort on 7 elements splits into 3 and 4, not 3.5 and 3.5. Its honest recurrence is
T(n) = T(⌊n/2⌋) + T(⌈n/2⌉) + Θ(n), with T(n) = Θ(1) for small n.
The analyses in earlier chapters wrote 2T(n/2) + n and assumed n was a power of 2. For asymptotic bounds, that shortcut is safe in almost every divide-and-conquer recurrence you will meet. The reasons are below.
Why Rounding Does Not Change the Bound
Three facts do the work.
- Rounding changes subproblem sizes by less than 1. On a recursion tree, the error builds up over at most log_b n levels. At depth j the sizes stay within a constant additive amount of n/b^j, because the errors form a geometric sum bounded by b/(b−1). A constant shift in size changes f by only a constant factor when f is a polynomial.
- Running times are usually monotone. If T is nondecreasing, you can solve it exactly at powers of b. Any n lies between b^k and b^(k+1), so T(n) is squeezed between two values whose ratio is a constant.
- The base case only sets the leaf cost. There are Θ(n^(log_b a)) leaves. Whether each leaf costs 1 or 50, the total changes by a constant factor only.
The standard statement of the master theorem already covers aT(⌊n/b⌋) and aT(⌈n/b⌉). Akra-Bazzi goes further and allows perturbations up to O(n / log² n).
Checking It Numerically
import math
from functools import lru_cache
@lru_cache(maxsize=None)
def T(n):
# Merge sort's exact recurrence with floors and ceilings, T(1) = 1
if n <= 1:
return 1
return T(n // 2) + T((n + 1) // 2) + n
for n in [1000, 7777, 10**5, 123457, 10**6]:
print(n, T(n), round(T(n) / (n * math.log2(n)), 4))The sizes in this list are not powers of 2, but the ratio still settles near 1, as it did for powers of 2.
Where Care Is Still Needed
| Situation | Can you ignore it? |
|---|---|
| ⌊n/b⌋ or ⌈n/b⌉ in place of n/b | Yes |
| Constant shift, e.g. T(n/2 + 17) | Yes |
| Base case T(n) = Θ(1) for n ≤ n₀ | Yes |
| Base case cost depends on a parameter (cutoff k to insertion sort) | No, keep k in the bound |
| Exact counts, such as comparisons in a worst-case input | No, solve exactly |
| Non-monotone or oscillating T | No, check directly |
For the cutoff case, merge sort that switches to insertion sort at size k costs Θ(nk + n log(n/k)). The base case is now the term that decides the bound.
Exercise
Prove T(n) = 2T(⌈n/2⌉) + 1 is O(n) by substitution, for n ≥ 2, handling the ceiling.
Show answer
Guess T(n) ≤ cn − d. Then T(n) ≤ 2(c⌈n/2⌉ − d) + 1 ≤ 2(c(n+1)/2 − d) + 1 = cn + c − 2d + 1. To get cn + c − 2d + 1 ≤ cn − d we need d ≥ c + 1. Pick d = c + 1, then choose c large enough that the base cases satisfy T(n) ≤ cn − d. Ceilings never shrink the subproblem below 2, so the induction is well founded.
Exercise
Why does replacing T(1) = 1 with T(1) = 100 leave merge sort at Θ(n log n)?
Show answer
The recursion tree has n leaves, so the leaf cost goes from n to 100n, which is still Θ(n). The n log n of merge work across the levels is unchanged, so the total is still Θ(n log n).