Bounding Sums: Integrals, Harmonic Numbers and Stirling
Many sums have no closed form, but for algorithm analysis a tight estimate is enough. The integral method squeezes a sum between two integrals. It gives the growth of the harmonic numbers (behind quicksort, hashing and the coupon collector) and, through , Stirling's formula for factorials (behind the lower bound for sorting).
The integral method
If is increasing on , each term lies between the area under on and on . Summing gives
For a decreasing the same bounds hold with and swapped:
So the sum is the integral up to an error of at most one term.
Harmonic numbers
With (decreasing) and :
More precisely , where is Euler's constant. The harmonic numbers grow without bound, but very slowly: .
import math
gamma = 0.5772156649015329
H, n = 0.0, 0
for target in [10, 100, 1000, 10 ** 6]:
while n < target:
n += 1
H += 1 / n
print(f"n={n:8d} H_n={H:.6f} ln n + gamma={math.log(n) + gamma:.6f} "
f"bounds [{1 / n + math.log(n):.4f}, {1 + math.log(n):.4f}]")n= 10 H_n=2.928968 ln n + gamma=2.879801 bounds [2.4026, 3.3026]
n= 100 H_n=5.187378 ln n + gamma=5.182386 bounds [4.6152, 5.6052]
n= 1000 H_n=7.485471 ln n + gamma=7.484971 bounds [6.9088, 7.9078]
n= 1000000 H_n=14.392727 ln n + gamma=14.392726 bounds [13.8155, 14.8155]
Book stacking. How far can a stack of identical books (length 1) overhang the edge of a table? Placing the top book's centre at the edge of the one below, and so on, the overhang is . Since , the overhang can be as large as you like, with enough books: needs , so 31 books give an overhang of 2 book lengths.
Factorials and Stirling's formula
Take logarithms: . With (increasing) and :
Exponentiating, is between and . The precise version is Stirling's formula:
The relative error is about , so it is already within 1% at .
import math
for n in [1, 5, 10, 20, 50]:
exact = math.factorial(n)
stirling = math.sqrt(2 * math.pi * n) * (n / math.e) ** n
print(f"n={n:2d} n!={exact:.6e} Stirling={stirling:.6e} ratio={exact / stirling:.6f}")
print("log2(52!) =", round(math.log2(math.factorial(52)), 2), "bits to describe a shuffled deck")n= 1 n!=1.000000e+00 Stirling=9.221370e-01 ratio=1.084438
n= 5 n!=1.200000e+02 Stirling=1.180192e+02 ratio=1.016784
n=10 n!=3.628800e+06 Stirling=3.598696e+06 ratio=1.008365
n=20 n!=2.432902e+18 Stirling=2.422787e+18 ratio=1.004175
n=50 n!=3.041409e+64 Stirling=3.036345e+64 ratio=1.001668
log2(52!) = 225.58 bits to describe a shuffled deck
Why sorting needs comparisons
A comparison sort must distinguish all orderings of its input, and each comparison has 2 outcomes, so it needs at least comparisons in the worst case. By Stirling, , so every comparison sort needs comparisons. Merge sort meets this bound.
In software
appears in the expected number of comparisons of quicksort (), in the coupon collector problem (about random draws to see all items, important for hash tables and load balancing) and in the expected number of "new maximum" events in a random stream (). Stirling estimates the size of search spaces like or the number of possible permutations in brute-force algorithms.
Exercises
Exercise 1: Bound a sum of square roots
Use the integral method to bound .
Answer
Between 667 and 676 (the true value is about 671.46).
Solution
is increasing and . So the sum is between and .
print(sum(i ** 0.5 for i in range(1, 101)))671.4629471031477
Exercise 2: Coupon collector
About how many random draws (with replacement) from 100 equally likely stickers do you need on average to collect all 100?
Answer
.
Solution
When you have stickers, a new one appears with probability , so the wait is draws on average. Summing over gives .
Exercise 3: Comparisons for 10 items
What is the minimum worst-case number of comparisons to sort 10 items, according to the bound?
Answer
Solution
and , so at least 22 comparisons are needed (and 22 are in fact enough for 10 items).
Test yourself
How fast do the harmonic numbers grow?
- Like
- Like
- They converge to 2
- Like
Stirling's formula approximates by…
Why does every comparison sort need comparisons?
- Because of cache misses
- It must distinguish orderings, and
- Because merging is slow
- It doesn't