Introduction

The running time of a recursive algorithm is naturally described by a recurrence: an equation that gives T(n)T(n) in terms of TT on smaller inputs. Merge sort splits an array into two halves, sorts each and merges in linear time, so T(n)=2T(n/2)+Θ(n)T(n) = 2T(n/2) + \Theta(n). Solving the recurrence means finding a closed-form asymptotic bound, here T(n)=Θ(nlg⁡n)T(n) = \Theta(n \lg n).

This note covers the four standard methods: substitution, recursion trees, the master theorem, and unrolling for subtract-type recurrences, together with change of variables and the cases where the master theorem does not apply.

Prerequisites

  • Asymptotic Notation: OO, Ω\Omega, Θ\Theta, polynomial versus logarithmic growth.
  • Recurrence Relations: exact solution of linear recurrences such as an=an−1+an−2a_n = a_{n-1} + a_{n-2}.
  • Geometric series: ∑i=0kri\sum_{i=0}^{k} r^i is Θ(1)\Theta(1) for r<1r < 1, k+1k + 1 for r=1r = 1, and Θ(rk)\Theta(r^k) for r>1r > 1.

Core concepts

Conventions

  • T(n)T(n) is a running time, so it is positive, and T(n)=Θ(1)T(n) = \Theta(1) for all nn below some constant. The base case is usually omitted because it changes the solution by at most a constant factor.
  • Floors and ceilings (T(⌊n/2⌋)+T(⌈n/2⌉)T(\lfloor n/2 \rfloor) + T(\lceil n/2 \rceil)) are usually dropped; for the recurrences treated in this note they do not change the asymptotic answer (CLRS 3e, Section 4.6 justifies this for the master recurrence).
  • lg⁡n=log⁡2n\lg n = \log_2 n. The critical exponent of the master recurrence is log⁡ba\log_b a; for example log⁡27≈2.807\log_2 7 \approx 2.807 and log⁡43≈0.793\log_4 3 \approx 0.793.

Method 1: substitution (guess and prove by induction)

  1. Guess the form of the solution (from a recursion tree or experience).
  2. Prove it by induction on nn, finding constants that make the inductive step work.

Example: T(n)=2T(⌊n/2⌋)+nT(n) = 2T(\lfloor n/2 \rfloor) + n. Guess T(n)≤c nlg⁡nT(n) \le c\,n \lg n. Assume it for ⌊n/2⌋\lfloor n/2 \rfloor. Then

T(n)≤2c⌊n/2⌋lg⁡⌊n/2⌋+n≤c nlg⁡(n/2)+n=c nlg⁡n−c n+n≤c nlg⁡nfor c≥1.T(n) \le 2c \lfloor n/2 \rfloor \lg \lfloor n/2 \rfloor + n \le c\,n \lg(n/2) + n = c\,n \lg n - c\,n + n \le c\,n \lg n \quad \text{for } c \ge 1.

The base case is handled by choosing n0n_0 and cc large enough (start the induction at n=2,3n = 2, 3 rather than n=1n = 1, since lg⁡1=0\lg 1 = 0).

Two pitfalls.

  • The inductive step must give exactly the hypothesis. Trying to prove T(n)=O(n)T(n) = O(n) for the same recurrence gives T(n)≤cn+nT(n) \le cn + n, which is not ≤cn\le cn for any constant cc. Writing "cn+n=O(n)cn + n = O(n), done" is a wrong proof.
  • Subtract a lower-order term when the guess is right but the induction fails. For T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1, the guess T(n)≤cnT(n) \le cn gives cn+1cn + 1, which fails. The stronger guess T(n)≤cn−dT(n) \le cn - d gives 2(cn/2−d)+1=cn−2d+1≤cn−d2(c n/2 - d) + 1 = cn - 2d + 1 \le cn - d whenever d≥1d \ge 1. So T(n)=O(n)T(n) = O(n).

Method 2: the recursion tree

Draw the tree of recursive calls. Each node is labelled with the non-recursive cost of its call; sum each level, then sum the levels.

For T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n):

  • Level ii (root is level 0) has aia^i nodes, each of size n/bin/b^i, costing aif(n/bi)a^i f(n/b^i) in total.
  • The tree has log⁡bn\log_b n levels below the root, and the bottom level has alog⁡bn=nlog⁡baa^{\log_b n} = n^{\log_b a} leaves, each costing Θ(1)\Theta(1).
T(n)=Θ(nlog⁡ba)+∑i=0log⁡bn−1aif ⁣(nbi).T(n) = \Theta\big(n^{\log_b a}\big) + \sum_{i=0}^{\log_b n - 1} a^i f\!\left(\frac{n}{b^i}\right).

Example: T(n)=2T(n/2)+cnT(n) = 2T(n/2) + cn. Every level costs cncn (2i2^i nodes of cost cn/2icn/2^i), there are lg⁡n+1\lg n + 1 levels, so T(n)=cnlg⁡n+Θ(n)=Θ(nlg⁡n)T(n) = cn \lg n + \Theta(n) = \Theta(n \lg n).

Recursion tree for T(n) = 2T(n/2) + cn
Figure 1. The recursion tree of T(n)=2T(n/2)+cnT(n) = 2T(n/2) + cn for n=8n = 8: levels 0 to 3, node costs cncn, cn/2cn/2, cn/4cn/4, cc, and a level total of cncn on the right of every level; total cn(lg⁡n+1)cn(\lg n + 1).

Unequal splits. For T(n)=T(n/3)+T(2n/3)+cnT(n) = T(n/3) + T(2n/3) + cn, each full level still costs cncn, but the tree is lopsided: the shortest root-to-leaf path has length log⁡3n\log_3 n and the longest log⁡3/2n\log_{3/2} n. Summing gives at most cnlog⁡3/2ncn \log_{3/2} n and at least cnlog⁡3ncn \log_3 n, so T(n)=Θ(nlg⁡n)T(n) = \Theta(n \lg n). The master theorem does not apply here, because the subproblems have different sizes.

Recursion tree with unequal splits
Figure 2. The recursion tree of T(n)=T(n/3)+T(2n/3)+cnT(n) = T(n/3) + T(2n/3) + cn: the first three levels each cost cncn; the leftmost path ends after log⁡3n\log_3 n levels and the rightmost after log⁡3/2n\log_{3/2} n levels.

A useful rule follows from the same argument. If T(n)=T(α1n)+⋯+T(αkn)+Θ(n)T(n) = T(\alpha_1 n) + \dots + T(\alpha_k n) + \Theta(n) with fixed constants 0<αj<10 < \alpha_j < 1, suitable integer rounding and a terminating constant-size base case, then the costs of full levels form a geometric series with ratio s=α1+⋯+αks = \alpha_1 + \dots + \alpha_k.

Partial levels cannot exceed the corresponding full-level cost. With shrinking fractions, the shallowest and deepest leaves both have depth Θ(lg⁡n)\Theta(\lg n); for s=1s=1, the full levels give the Ω(nlg⁡n)\Omega(n\lg n) lower bound, and all levels together give O(nlg⁡n)O(n\lg n). For s<1s<1, summing the full-level upper bounds gives O(n)O(n), while the root gives Ω(n)\Omega(n).

s=∑αjs = \sum \alpha_jSolution
s<1s < 1Θ(n)\Theta(n), for example T(n)=T(n/2)+T(n/4)+T(n/8)+nT(n) = T(n/2) + T(n/4) + T(n/8) + n (s=7/8s = 7/8)
s=1s = 1Θ(nlg⁡n)\Theta(n \lg n), for example T(n)=T(n/3)+T(2n/3)+nT(n) = T(n/3) + T(2n/3) + n

For s>1s > 1 the answer is a power npn^p with p>1p > 1 defined by ∑αjp=1\sum \alpha_j^p = 1; for equal splits this is the master theorem's nlog⁡ban^{\log_b a}.

Method 3: the master theorem

Master theorem (CLRS 3e, Section 4.5). Let a≥1a \ge 1 and b>1b > 1 be constants, let f(n)f(n) be an asymptotically positive function, and let T(n)T(n) be defined on the non-negative integers by

T(n)=a T(n/b)+f(n),T(n) = a\,T(n/b) + f(n),

where n/bn/b may be read as ⌊n/b⌋\lfloor n/b \rfloor or ⌈n/b⌉\lceil n/b \rceil. Compare f(n)f(n) with the watershed function nlog⁡ban^{\log_b a}:

  1. If f(n)=O(nlog⁡ba−ε)f(n) = O(n^{\log_b a - \varepsilon}) for some constant ε>0\varepsilon > 0, then T(n)=Θ(nlog⁡ba)T(n) = \Theta(n^{\log_b a}).
  2. If f(n)=Θ(nlog⁡ba)f(n) = \Theta(n^{\log_b a}), then T(n)=Θ(nlog⁡balg⁡n)T(n) = \Theta(n^{\log_b a} \lg n).
  3. If f(n)=Ω(nlog⁡ba+ε)f(n) = \Omega(n^{\log_b a + \varepsilon}) for some constant ε>0\varepsilon > 0, and if a f(n/b)≤c f(n)a\,f(n/b) \le c\,f(n) for some constant c<1c < 1 and all sufficiently large nn (the regularity condition), then T(n)=Θ(f(n))T(n) = \Theta(f(n)).

Interpretation through the recursion tree: in case 1 the leaves dominate (level costs grow geometrically downwards); in case 2 all log⁡bn+1\log_b n + 1 levels cost the same; in case 3 the root dominates (level costs shrink geometrically).

Per-level costs in the three master-theorem cases
Figure 3. Level totals for three recurrences with a=b=2a = b = 2 or a=8,b=2a = 8, b = 2. Case 1, 8T(n/2)+n28T(n/2) + n^2: n2,2n2,4n2,…n^2, 2n^2, 4n^2, \ldots (leaves dominate). Case 2, 2T(n/2)+n2T(n/2) + n: n,n,n,…n, n, n, \ldots (all equal). Case 3, 2T(n/2)+n22T(n/2) + n^2: n2,n2/2,n2/4,…n^2, n^2/2, n^2/4, \ldots (root dominates).

Notes on the conditions.

  • "Polynomially smaller" (case 1) and "polynomially larger" (case 3) mean by a factor nεn^{\varepsilon}. A logarithmic factor is not enough: nlg⁡nn \lg n is larger than nn but not polynomially larger.
  • For polynomial f(n)=Θ(nk)f(n) = \Theta(n^k) with k>log⁡bak > \log_b a, regularity holds automatically: a(n/b)k=(a/bk)nka (n/b)^k = (a/b^k) n^k and a/bk<1a/b^k < 1. So the condition matters only for unusual ff.
  • The regularity condition can fail. Take a=1a = 1, b=2b = 2, f(n)=n(2−cos⁡n)f(n) = n(2 - \cos n). Then f(n)≥n=Ω(n0+1)f(n) \ge n = \Omega(n^{0 + 1}), so the first part of case 3 holds, but for infinitely many even nn (n=6,18,32,…n = 6, 18, 32, \ldots) f(n/2)>f(n)f(n/2) > f(n), and the ratio f(n/2)/f(n)f(n/2)/f(n) comes arbitrarily close to 3/23/2. No constant c<1c < 1 works, and case 3 cannot be used.

Extended case 2 (CLRS 3e, Exercise 4.6-2). If f(n)=Θ(nlog⁡balg⁡kn)f(n) = \Theta(n^{\log_b a} \lg^k n) for a constant k≥0k \ge 0, then T(n)=Θ(nlog⁡balg⁡k+1n)T(n) = \Theta(n^{\log_b a} \lg^{k+1} n). Case 2 is the special case k=0k = 0. This covers T(n)=2T(n/2)+nlg⁡n=Θ(nlg⁡2n)T(n) = 2T(n/2) + n \lg n = \Theta(n \lg^2 n), which falls in the gap between cases 2 and 3 of the basic theorem.

Related material: the nklg⁡pnn^k \lg^p n table. A wider version is often stated for f(n)=Θ(nklg⁡pn)f(n) = \Theta(n^k \lg^p n) with constants k≥0k \ge 0 and real pp. It is not a CLRS theorem, but each row follows from the recursion tree (level ii costs (a/bk)i(a/b^k)^i times nklg⁡p(n/bi)n^k \lg^p(n/b^i)):

Relation of aa and bkb^kCondition on ppT(n)T(n)
a>bka > b^kany ppΘ(nlog⁡ba)\Theta(n^{\log_b a})
a=bka = b^kp>−1p > -1Θ(nklg⁡p+1n)\Theta(n^k \lg^{p+1} n)
a=bka = b^kp=−1p = -1Θ(nklg⁡lg⁡n)\Theta(n^k \lg \lg n)
a=bka = b^kp<−1p < -1Θ(nk)\Theta(n^k)
a<bka < b^kany ppΘ(nklg⁡pn)\Theta(n^k \lg^p n)

With a=bka = b^k every level contributes nklg⁡pn^k \lg^p of a shrinking argument, so the sum is nk∑j=1log⁡bnjpn^k \sum_{j=1}^{\log_b n} j^p up to constants: a power sum for p>−1p > -1, a harmonic sum for p=−1p = -1, and a convergent series for p<−1p < -1. For example 2T(n/2)+n/lg⁡2n=Θ(n)2T(n/2) + n/\lg^2 n = \Theta(n). Some books write O(nk)O(n^k) for a<bka < b^k, p<0p < 0; that is only an upper bound, since the root alone already costs Θ(nklg⁡pn)\Theta(n^k \lg^p n).

When the master theorem does not apply

RecurrenceWhy notSolution and method
T(n)=2T(n/2)+nlg⁡nT(n) = 2T(n/2) + n \lg nff larger than nn only by lg⁡n\lg nΘ(nlg⁡2n)\Theta(n \lg^2 n), extended case 2 or recursion tree
T(n)=2T(n/2)+n/lg⁡nT(n) = 2T(n/2) + n / \lg nff smaller only by lg⁡n\lg nΘ(nlg⁡lg⁡n)\Theta(n \lg \lg n), recursion tree (harmonic sum of level costs)
T(n)=T(n/3)+T(2n/3)+nT(n) = T(n/3) + T(2n/3) + nunequal subproblem sizesΘ(nlg⁡n)\Theta(n \lg n), recursion tree
T(n)=n T(n/2)+1T(n) = n\,T(n/2) + 1aa is not a constantnot polynomial; unroll
T(n)=T(n−1)+nT(n) = T(n-1) + nsubtractive, no b>1b > 1Θ(n2)\Theta(n^2), unrolling
T(n)=2T(n)+lg⁡nT(n) = 2T(\sqrt{n}) + \lg nargument is not n/bn/bΘ(lg⁡nlg⁡lg⁡n)\Theta(\lg n \lg \lg n), change of variables
T(n)=0.5 T(n/2)+nT(n) = 0.5\,T(n/2) + na<1a < 1Θ(n)\Theta(n) by recursion tree; the theorem needs a≥1a \ge 1

Method 4: unrolling subtract-type recurrences

For recurrences that reduce nn by a constant, expand repeatedly and sum.

  • T(n)=T(n−1)+cT(n) = T(n-1) + c: T(n)=cn+T(0)=Θ(n)T(n) = cn + T(0) = \Theta(n) (linear recursion, for example recursive linear search).
  • T(n)=T(n−1)+nT(n) = T(n-1) + n: T(n)=n+(n−1)+⋯+1+T(0)=n(n+1)/2+T(0)=Θ(n2)T(n) = n + (n-1) + \dots + 1 + T(0) = n(n+1)/2 + T(0) = \Theta(n^2) (for example selection sort written recursively, or quicksort's worst case).
  • T(n)=2T(n−1)+1T(n) = 2T(n-1) + 1, T(1)=1T(1) = 1: T(n)=2n−1T(n) = 2^n - 1 exactly (Tower of Hanoi moves).
  • T(n)=T(n−1)+T(n−2)+1T(n) = T(n-1) + T(n-2) + 1, T(0)=T(1)=1T(0) = T(1) = 1: T(n)=2F(n+1)−1=Θ(ϕn)T(n) = 2F(n+1) - 1 = \Theta(\phi^n), ϕ=(1+5)/2≈1.618\phi = (1 + \sqrt{5})/2 \approx 1.618, where FF is the Fibonacci sequence (naive Fibonacci call count).
  • General bound: if T(n)=a T(n−b)+f(n)T(n) = a\,T(n - b) + f(n) with constants a≥1a \ge 1, b≥1b \ge 1 and f(n)=O(nk)f(n) = O(n^k), then T(n)=O(nk+1)T(n) = O(n^{k+1}) when a=1a = 1 and T(n)=O(nkan/b)T(n) = O(n^k a^{n/b}) when a>1a > 1.

Change of variables

For T(n)=2T(n)+lg⁡nT(n) = 2T(\sqrt{n}) + \lg n, put m=lg⁡nm = \lg n, so n=2mn = 2^m and n=2m/2\sqrt{n} = 2^{m/2}. Define S(m)=T(2m)S(m) = T(2^m). Then S(m)=2S(m/2)+mS(m) = 2S(m/2) + m, which is case 2: S(m)=Θ(mlg⁡m)S(m) = \Theta(m \lg m). Translating back, T(n)=Θ(lg⁡nlg⁡lg⁡n)T(n) = \Theta(\lg n \lg \lg n). Similarly T(n)=T(n)+1T(n) = T(\sqrt{n}) + 1 gives S(m)=S(m/2)+1S(m) = S(m/2) + 1, so T(n)=Θ(lg⁡lg⁡n)T(n) = \Theta(\lg \lg n).

Worked examples

Recurrencenlog⁡ban^{\log_b a}Comparison with ffSolution
T(n)=2T(n/2)+nT(n) = 2T(n/2) + n (merge sort)nnequal: case 2Θ(nlg⁡n)\Theta(n \lg n)
T(n)=T(n/2)+1T(n) = T(n/2) + 1 (binary search)n0=1n^0 = 1equal: case 2Θ(lg⁡n)\Theta(\lg n)
T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2 (naive block matrix multiply)n3n^3ff smaller by n1n^1: case 1Θ(n3)\Theta(n^3)
T(n)=7T(n/2)+n2T(n) = 7T(n/2) + n^2 (Strassen)nlg⁡7≈n2.807n^{\lg 7} \approx n^{2.807}ff smaller by n0.807n^{0.807}: case 1Θ(nlg⁡7)\Theta(n^{\lg 7})
T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1 (tree traversal)nnff smaller by n1n^1: case 1Θ(n)\Theta(n)
T(n)=2T(n/2)+n2T(n) = 2T(n/2) + n^2nnff larger by n1n^1; regularity 2(n/2)2=n2/22(n/2)^2 = n^2/2, c=1/2c = 1/2: case 3Θ(n2)\Theta(n^2)
T(n)=3T(n/4)+nlg⁡nT(n) = 3T(n/4) + n \lg nn0.793n^{0.793}ff larger by more than n0.2n^{0.2}; regularity 3(n/4)lg⁡(n/4)≤34nlg⁡n3(n/4)\lg(n/4) \le \tfrac{3}{4} n \lg n: case 3Θ(nlg⁡n)\Theta(n \lg n)
T(n)=4T(n/2)+n2lg⁡nT(n) = 4T(n/2) + n^2 \lg nn2n^2f=n2lg⁡1nf = n^2 \lg^1 n: extended case 2, k=1k = 1Θ(n2lg⁡2n)\Theta(n^2 \lg^2 n)
T(n)=9T(n/3)+nT(n) = 9T(n/3) + nn2n^2ff smaller by n1n^1: case 1Θ(n2)\Theta(n^2)
T(n)=T(2n/3)+1T(n) = T(2n/3) + 1n0=1n^0 = 1equal: case 2Θ(lg⁡n)\Theta(\lg n)

Exact values check the asymptotics. With T(1)=1T(1) = 1 and n=2kn = 2^k:

  • T(n)=2T(n/2)+nT(n) = 2T(n/2) + n gives T(n)=nlg⁡n+nT(n) = n \lg n + n (for n=1024n = 1024: 11264).
  • T(n)=8T(n/2)+n2T(n) = 8T(n/2) + n^2 gives T(n)=2n3−n2T(n) = 2n^3 - n^2.
  • T(n)=T(n/2)+1T(n) = T(n/2) + 1 gives T(n)=lg⁡n+1T(n) = \lg n + 1.

Recurrence from code

Text
F(n)
    if n <= 1 then return 1
    s <- 0
    for i <- 1 to n
        for j <- 1 to n
            s <- s + 1
    return F(n/2) + F(n/2) + F(n/2) + F(n/2) + s

Four calls on n/2n/2 plus n2n^2 increments: T(n)=4T(n/2)+n2T(n) = 4T(n/2) + n^2, case 2, Θ(n2lg⁡n)\Theta(n^2 \lg n). Counting only the increments of s, with none for n=1n = 1, the exact total is S(n)=n2lg⁡nS(n) = n^2 \lg n for n=2kn = 2^k (1,024 for n=16n = 16).

Replacing the return line by return 4 * F(n/2) + s computes the same value with one call: T(n)=T(n/2)+n2=Θ(n2)T(n) = T(n/2) + n^2 = \Theta(n^2), and the increments total 43(n2−1)\tfrac{4}{3}(n^2 - 1) (84 for n=8n = 8, 340 for n=16n = 16). The number of recursive calls in the code, not the arithmetic in the formula, sets aa.

Important properties and observations

  • The master theorem compares f(n)f(n) with nlog⁡ban^{\log_b a} polynomially; logarithmic differences fall into gaps.
  • The base of the logarithm in log⁡ba\log_b a matters (it is the exponent); the base of lg⁡n\lg n in the case 2 answer does not.
  • The theorem gives Θ\Theta, so it gives both an upper and a lower bound. If ff is given only as O(⋅)O(\cdot), a matching toll-driven lower bound may be unavailable. However, case 1 still gives Θ(nlog⁡ba)\Theta(n^{\log_b a}) for a nonnegative toll and a positive constant base cost, because the leaves supply that lower bound.
  • Adding a constant to an argument requires a terminating cutoff: for T(n)=2T(n/2+17)+nT(n)=2T(n/2+17)+n, choose a constant base-case cutoff n0>34n_0>34, with rounding handled so recursive arguments shrink above it. Then the solution is Θ(nlg⁡n)\Theta(n\lg n). For example, floor rounding with integer n0≥35n_0\ge35 works. An arbitrary unchanged small base case may not terminate: 34 maps to itself. T(n)=2T(n/2+17)+nT(n) = 2T(n/2 + 17) + n is still Θ(nlg⁡n)\Theta(n \lg n).
  • In a substitution proof, never absorb the error term with O(⋅)O(\cdot) inside the induction; carry the explicit constant.

GATE examination perspective

  • Direct application. identify aa, bb, ff, compute log⁡ba\log_b a, decide the case, and check regularity for case 3 when ff is not a polynomial.
  • Recurrence from code. write the recurrence first (how many recursive calls, of what size, plus the non-recursive work), then solve it. A function that calls itself twice on n−1n - 1 is exponential, not quadratic.
  • Traps. F(n/2) + F(n/2) is two calls but 2 * F(n/2) is one; 2T(n/2)+nlg⁡n2T(n/2) + n \lg n is not Θ(nlg⁡n)\Theta(n \lg n); 2T(n/2)+n/lg⁡n2T(n/2) + n/\lg n is Θ(nlg⁡lg⁡n)\Theta(n \lg \lg n), not Θ(n)\Theta(n); T(n−1)+nT(n-1) + n is not covered by the master theorem; unequal splits need a recursion tree; T(n)T(\sqrt{n}) needs a change of variables.
  • Exact answers. when asked for a value (for example, the number of moves or calls), solve the recurrence exactly, not asymptotically.

Summary

Sources

  1. T. H. Cormen, C. E. Leiserson, R. L. Rivest, C. Stein, Introduction to Algorithms, 3rd ed., MIT Press, 2009, Chapter 4, Sections 4.3 (substitution method), 4.4 (recursion-tree method), 4.5 (the master method and the master theorem) and 4.6 (proof of the master theorem; Exercise 4.6-2, the extended case 2).

Next steps