Mastering Recurrence Relations: The Substitution Method

Mastering Recurrence Relations: The Substitution Method

Verified Sources
May 26, 2026

In computer science, analyzing the execution time of recursive algorithms requires solving a recurrence relation (https://mitpress.mit.edu/9780262046305/introduction-to-algorithms/). A classic example is the recurrence:

T(n)=2T(n/2)+nT(n) = 2T(n/2) + n

This equation frequently arises in divide-and-conquer algorithms, such as Mergesort, where a problem of size nn is split into two equal subproblems of size n/2n/2, alongside a linear cost nn for splitting and recombining the subproblems (https://mitpress.mit.edu/9780262046305/introduction-to-algorithms/).

To establish an asymptotic upper bound for T(n)T(n), we can use the substitution method (https://mitpress.mit.edu/9780262046305/introduction-to-algorithms/). This method leverages mathematical induction to prove that a guessed bound holds true for all inputs larger than a baseline value n0n_0 (https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/).

Visualizing the Recurrence Tree

To formulate a strong initial guess, we can visualize how the recurrence splits. The total work is the sum of the work done at each level of the recursion tree:

Summing the costs across any level ii yields a constant cost of nn per level. Since the tree has a depth of log2n\log_2 n levels, the total complexity is visually estimated to be O(nlogn)O(n \log n) (https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/).

Footnotes

  1. Introduction to Algorithms (CLRS) - Chapter 4: Divide-and-Conquer and Recurrences. 2 3

  2. Mathematical Induction for Recurrences - Stanford CS161 Lecture Notes on solving recurrences using the substitution method. 2

Formulating a Good Guess

Before starting the substitution method, always sketch a quick recursion tree. If the tree reveals that every level does nn work and there are log2n\log_2 n levels, your guess should be O(nlogn)O(n \log n). This ensures you do not waste algebraic effort proving a bound that is too tight or too loose.

Mathematical Induction Walkthrough

  1. 1
    Step 1

    Based on our visual analysis, we guess that T(n)cnlog2nT(n) \le c n \log_2 n for some constant c>0c > 0 and all nn0n \ge n_0. This formulation represents our inductive hypothesis.

  2. 2
    Step 2

    We assume the inductive hypothesis holds true for all positive numbers smaller than nn, which specifically includes n/2n/2. Thus, we substitute the hypothesis into the recurrence relation: T(n/2)c(n/2)log2(n/2)T(n/2) \le c (n/2) \log_2(n/2). Inserting this into the original equation yields: T(n)2(cn2log2(n2))+nT(n) \le 2\left(c \frac{n}{2} \log_2\left(\frac{n}{2}\right)\right) + n.

  3. 3
    Step 3

    Simplify the substituted inequality using the laws of logarithms. We know that log2(n/2)=log2nlog22=log2n1\log_2(n/2) = \log_2 n - \log_2 2 = \log_2 n - 1. Expanding the expression gives: T(n)cn(log2n1)+n=cnlog2ncn+nT(n) \le c n (\log_2 n - 1) + n = c n \log_2 n - c n + n.

  4. 4
    Step 4

    To complete the induction, we must show that our simplified inequality is less than or equal to our target bound: cnlog2ncn+ncnlog2nc n \log_2 n - c n + n \le c n \log_2 n. This inequality holds true if and only if cn+n0-c n + n \le 0, which simplifies directly to c1c \ge 1.

  5. 5
    Step 5

    Show that the boundary condition holds. If we assume a base case T(1)=1T(1) = 1, our inductive hypothesis yields T(1)c1log21=0T(1) \le c \cdot 1 \log_2 1 = 0, which contradicts the base case. Because asymptotic bounds only require the inequality to hold for nn0n \ge n_0, we can choose n0=2n_0 = 2 and n0=3n_0 = 3 as our base cases. With T(2)=2T(1)+2=4T(2) = 2T(1) + 2 = 4, we choose cc large enough (e.g., c2c \ge 2) to satisfy the base cases.

The Base Case Trap

A common pitfall in mathematical induction is neglecting the base case. If your base case is n=1n = 1, T(1)c1log21T(1) \le c \cdot 1 \log_2 1 evaluates to T(1)0T(1) \le 0, which is impossible for positive running times. Always remember that asymptotic notation only requires the bound to hold for nn0n \ge n_0. You can freely choose n0=2n_0 = 2 or n0=3n_0 = 3 as your base cases for induction [(https://web.stanford.edu/class/archive/cs/cs161/cs161.1168/)].

Footnotes

  1. Mathematical Induction for Recurrences - Stanford CS161 Lecture Notes on solving recurrences using the substitution method.

A rigorous mathematical technique where we guess the form of the solution and prove it using mathematical induction. Highly precise but requires a good initial guess.

Key Advantage: Perfect mathematical rigor.\text{Key Advantage: Perfect mathematical rigor.} Key Disadvantage: Hard to formulate a guess for complex equations.\text{Key Disadvantage: Hard to formulate a guess for complex equations.}

Advanced Substitution Strategies & Edge Cases

Knowledge Check

Question 1 of 3
Q1Single choice

Which of the following is the correct inductive hypothesis to prove that T(n)=2T(n/2)+nT(n) = 2T(n/2) + n is O(nlogn)O(n \log n)?

Explore Related Topics

1

Dynamic Programming and Greedy Algorithms: Comparative Analysis, Failure Cases, and Core DP Properties

The article contrasts greedy algorithms with dynamic programming, shows when greedy fails and DP is necessary, and explains DP’s core properties of optimal substructure and overlapping subproblems.

  • Greedy makes irrevocable local choices and works only with the greedy-choicegreedy\text{-}choice property, while DP stores and reuses subproblem results to guarantee optimality.
  • Counterexamples such as coin change ({1,3,4}\{1,3,4\} for amount 6) and 0/10/1 knapsack illustrate failures of greedy and the need for DP recurrences like dp[x]=1+mincxdp[xc]dp[x]=1+\min_{c\le x}dp[x-c] and dp[i][w]=max(dp[i1][w],vi+dp[i1][wwi])dp[i][w]=\max(dp[i-1][w],\,v_i+dp[i-1][w-w_i]).
  • DP relies on optimal substructureoptimal\ substructure to form recurrences and on overlapping subproblems to justify caching.
  • Design steps: recognize structure, detect repetition, formulate recurrence, compute once (memoization/tabulation), optionally reconstruct solution.
  • Rule of thumb: use greedy if local choices can be proved globally safe; otherwise apply DP.
2

Syntax-Directed Translation: Infix to Prefix Notation

The module shows how a syntax‑directed translation scheme using only synthesized attributes can convert infix arithmetic expressions into prefix (Polish) notation while preserving operator precedence and left‑associativity.

  • Grammar: E → E + T | E - T | T; T → T * F | F; F → digit, enforcing precedence ( *  > + / - ).
  • Semantic actions compute a val string for each non‑terminal, concatenating the operator before its operand strings.
  • Example results: 9 - 5 + 2+ - 9 5 2; 9 - 5 * 2- 9 * 5 2.
  • Synthesized (S‑attributed) attributes allow immediate bottom‑up evaluation during LR‑style parsing.
  • Left‑recursive rules enable left‑associativity; to use LL parsers the grammar must be transformed and inherited attributes introduced.
3

Asymptotic Notation in Algorithm Analysis