Chapter 2

Analysing time and spaceCS

State the input size, basic operation and input class before simplifying. A worst-case function and an expected function answer different questions. General recurrence algebra belongs to Engineering Mathematics; this chapter applies it to algorithm costs.

2.1 Asymptotic bounds and growthCS

Past papers · CS 2014–2026 (23 papers): 7 questions, 8 marks

Let f(n),g(n)f(n),g(n) be eventually nonnegative, with g(n)>0g(n)>0 for sufficiently large integer nn. Constants in bounds cannot depend on nn.

For eventually nonnegative functions, f+g=Θ(max⁡(f,g))f+g=\Theta(\max(f,g)). O,Ω,ΘO,\Omega,\Theta are transitive; only Θ\Theta is symmetric, while f∈O(g)f\in O(g) is equivalent to g∈Ω(f)g\in\Omega(f). Big O does not mean worst case: it can upper-bound any specified cost function.

2.2 Loops and exact workCS

Past papers · CS 2014–2026 (23 papers): 4 questions, 8 marks

Nested loops are summed over the actual inner range; adjacent loops are added. If a variable persists between outer iterations, do not restart its count mentally. The number returned by a function need not equal its running time.

Shape of workTight orderReason
Inner range 11 through ii, outer 11 through nnΘ(n2)\Theta(n^2)Triangular sum
Inner increment ii, inner range at most nn, outer 11 through nnΘ(nlog⁡n)\Theta(n\log n)Harmonic sum
Work nn then n/2n/2 then n/4n/4Θ(n)\Theta(n)Geometric sum
Work nn on each halving iterationΘ(nlog⁡n)\Theta(n\log n)Same work at each level
Unit work over 1≤i≤j<k≤n1\le i\le j<k\le nΘ(n3)\Theta(n^3)Exactly n(n−1)(n+1)/6n(n-1)(n+1)/6 triples

2.3 Recursion trees and the master theoremCS

Past papers · CS 2014–2026 (23 papers): 4 questions, 6 marks

A call creates subproblems and performs local work. For equal reductions, count the nodes and the work per node on each level; include the leaves. Use a constant-size base case with positive constant cost.

For the critical case f(n)=Θ(nplog⁡qn)f(n)=\Theta(n^p\log^q n) with negative qq, sum the levels: q>−1q>-1 gives Θ(nplog⁡q+1n)\Theta(n^p\log^{q+1}n), q=−1q=-1 gives Θ(nplog⁡log⁡n)\Theta(n^p\log\log n), and q<−1q<-1 gives Θ(np)\Theta(n^p). Stop at an input size above 11 so logarithmic denominators are defined. These extensions follow from a power sum over the remaining levels; do not use the polynomial-gap cases when only a logarithmic gap exists.

2.4 Recurrences beyond equal splitsCS

Past papers · CS 2014–2026 (23 papers): 6 questions, 9 marks

Choose a new variable when the input shrinks by a root. Normalize by an exponential or by nn when it cancels the multiplier. For coupled recurrences, solve the inner cost first and substitute its order into the outer one.

For T(n)=T(αn)+T(βn)+Θ(n)T(n)=T(\alpha n)+T(\beta n)+\Theta(n) with fixed 0<α,β<10<\alpha,\beta<1 and α+β<1\alpha+\beta<1, work shrinks geometrically across levels and T(n)=Θ(n)T(n)=\Theta(n). If the fractions sum to 11, both bounded away from zero, the result is Θ(nlog⁡n)\Theta(n\log n). The ordinary master theorem does not apply directly to unequal subproblem sizes.

For repeated aath roots with a fixed threshold b>1b>1 and fixed a>1a>1, the number of reductions from n>bn>b is ⌈log⁡a(log⁡bn)⌉\lceil\log_a(\log_b n)\rceil. Include the base call in total calls. If parameters vary with nn, retain their dependence instead of replacing this by Θ(log⁡log⁡n)\Theta(\log\log n).

2.5 Peak storage and runtime scalingCS

Past papers · CS 2014–2026 (23 papers): 1 question, 2 marks

Time adds all executed subcalls. Stack space measures only simultaneously live calls. Sequential recursive children do not occupy their entire recursion trees at once. Include local arrays retained by ancestor calls.

A balanced recursion with two sequential children and constant-size frames has logarithmic stack depth even if there are linearly many calls. If each active level retains an array proportional to its subproblem size, the path sum n+n/2+⋯n+n/2+\cdots is Θ(n)\Theta(n) storage. Naive slicing may copy data; do not charge only the stack in that implementation.

Last-minute recall

  • Specify worst, best or expected cost before writing a bound.
  • Constants in asymptotic definitions are independent of input size.
  • Sum dependent loop lengths; do not multiply their maxima blindly.
  • Count local work and leaf work in recursion trees.
  • Master-theorem case 3 needs regularity.
  • Logarithmic gaps are not polynomial gaps.
  • Root recurrences need a threshold or rounding convention.
  • Peak stack depth and total call count are different quantities.

Revise offline: the whole Algorithms handbook as one PDF

52 pages · 2.6 MBFree to read online. Sign in to download the PDF.We save the handbook, version and request time in My downloads. Privacy policy

All chapters · Corrections