Asymptotic Analysis & Recurrences



Shared context
Given the algorithm below, with procedure A taking O(1) time and B(n) taking O(1/n) time, derive its time complexity in O-notation.
what(n):
if n = 1: call A
else:
what(n-1)
call B(n)
Assume n is a positive integer.
Answer & solution
Answer: Procedure-cost-only model: O(log n). With ordinary constant recursion/control overhead: Θ(n). (no official key was published)
In the idealized accounting suggested by the question, count only costs of A and B. Let their costs be a and b(k), with a=O(1) and b(k)=O(1/k). The recurrence is T(1)=a and T(n)=T(n−1)+b(n). Expanding gives T(n)=a+Σ(k=2..n)b(k)≤C₀+C₁Σ(k=2..n)1/k for suitable constants, absorbing finitely many small inputs into C₀.
The harmonic sum is bounded above by 1+ln n, by comparison with the integral of 1/x. Thus T(n)=O(log n). If the supplied procedure costs are actually Θ(1) and Θ(1/n), the lower integral bound gives Θ(log n), but upper bounds alone do not prove this tight classification.
Cost-model qualification: a real RAM implementation must perform a test, decrement, recursive call and return at each of n recursion levels. These contribute Θ(1) per level, giving T_RAM(n)=T_RAM(n−1)+Θ(1)+O(1/n)=Θ(n), with Θ(n) stack depth. Moreover a nonzero discrete instruction cost cannot asymptotically decrease as 1/n. Therefore O(log n) is the intended abstract procedure-cost result, not an unconditional full implementation running-time claim.