1. Gyanastra
  2. Algorithms
  3. Recurrence Relations and the Master Theorem
  4. Forming Recurrence Equations from Recursive Functions
Free lesson, no account needed 5 min read 14 practice questions after it
Asked in past papers31 times
  • GATE CSE2026202520242021202020172016201520142012200920082007200620052004200219961993
  • ISRO202020182017201620152014
  • UGC NET202220192018201720142013

Algorithms · Recurrence Relations and the Master Theorem

Forming Recurrence Equations from Recursive Functions

Overview

A recursive function calls itself on a smaller input, so its running time cannot be found by counting loop iterations. Instead, the time is expressed as a recurrence equation, which is then solved to obtain the complexity.

Key concepts

  • Recursive function: a function that calls itself, typically on a smaller input such as n/2n/2 (in general, on a reduced size of the input).
  • Recurrence equation (recursive equation): the equation that expresses the time cost of a recursive function in terms of its cost on smaller inputs.
    It is needed because a recursive function has nothing to "count" directly, so its analysis is very different from that of an iterative function.
  • T(n)T(n): the time taken to solve the problem on an input of size nn.
  • Scaling relation: if A(n)A(n) takes time T(n)T(n), then a call A(n/2)A(n/2) takes time T(n/2)T(n/2) — the same function symbol with the argument scaled.
  • Constant work: the base checks/comparisons (e.g. the if condition) and the combining work (e.g. adding/summing the results) each cost a constant, written as cc or as 1.

How it works

  1. Let the time taken to solve the problem on an input of size nn be T(n)T(n).
  2. Break the total time into the parts actually spent inside one call:
    • constant time for the base checks/comparisons — e.g. the if condition — call it some constant cc (or 1);
    • time for each recursive call: a call A(n/2)A(n/2) takes T(n/2)T(n/2);
    • constant time for the combining work — e.g. the addition/summing up of the results.
  3. Add all parts together. For a function that makes two calls each on input n/2n/2: constant time for the if condition + constant time for the addition + 2 calls each costing T(n/2)T(n/2).
  4. Write this as the recurrence, then solve the recurrence (several methods exist).

Formulas

  • T(n)T(n) = time taken to solve the problem of size nn.
  • cc (or 1) = constant time for the if condition and for the addition/combining step.
  • T(n)=2T(n/2)+cT(n) = 2T(n/2) + c where 2T(n/2)2T(n/2) is the cost of the two recursive calls and cc is the constant work (if condition + addition) inside the call.

Worked examples

Forming the recurrence for a function A(n)A(n) that performs an if comparison (constant time), calls A(n/2)A(n/2) twice, and adds/sums the two results (constant time):

  1. Time for A(n)A(n) is T(n)T(n).
  2. if condition costs a constant.
  3. First call A(n/2)A(n/2) costs T(n/2)T(n/2); second call A(n/2)A(n/2) costs T(n/2)T(n/2) — i.e. 2⋅T(n/2)2 \cdot T(n/2) in total.
  4. The addition of the results costs a constant.
  5. Total: constant + 2T(n/2)2T(n/2) + constant, written as T(n)=2T(n/2)+cT(n) = 2T(n/2) + c (the constants are collected into one constant term).
Growth chart: Forming Recurrence Equations from Recursive Functions
Figure. Forming Recurrence Equations from Recursive Functions

Revision summary

  • A function calling itself is recursion; its cost is expressed as a recurrence, not by counting loop iterations.
  • Always define T(n)T(n) = time for input size nn first, then split the time into: constant work inside the call + (number of calls) × T(reduced size)T(\text{reduced size}) + constant combining work.
  • If A(n)A(n) costs T(n)T(n), then A(n/2)A(n/2) costs T(n/2)T(n/2) — substitute the scaled argument into the same TT.
  • Constants can be written as cc or as 1; it does not matter for the recurrence.
  • After forming the recurrence, the next task is to solve it (several methods exist).

Previous-year exam insights

  • Master Theorem: for T(n) = aT(n/b) + f(n) with a ≥ 1, b > 1, compare f(n) with nlog⁡ban^{\log_b a};
    if f(n)=O(n(log⁡baf(n) = O(n^(\log_{b} a − ε)) then T(n) = Θ(nlog⁡ba)\Theta(n^{\log_{b} a});
    if f(n) = Θ(nlog⁡ba)\Theta(n^{\log_{b} a}) then T(n) = Θ(nlog⁡balog⁡n)\Theta(n^{\log_{b} a} \log n);
    if f(n) = Ω(nlog⁡ba+εn^{\log_b a + ε}) and the regularity condition a·f(n/b) ≤ c·f(n) holds for some c < 1, then T(n) = Θ(f(n))\Theta(f(n)).
    Asked inGATE 2021GATE 2026
  • For recurrences with unequal subproblem sizes such as T(n) = T(αn) + T(βn) + f(n) with f(n) = Θ(n)\Theta(n): if α + β < 1 the total work forms a geometrically decreasing series, so T(n) = Θ(n)\Theta(n);
    if α + β = 1 each level costs Θ(n)\Theta(n) and T(n) = Θ(nlog⁡n)\Theta(n \log n).
    Asked inGATE 2021
  • For T(n)=T(n1/a)+1T(n) = T(n^{1/a}) + 1 with a > 1 (and T(b) = 1), repeatedly taking roots gives T(n) = Θ(log⁡alog⁡bn)\Theta(\log_{a} \log_{b} n), i.e. the number of times a logarithm must be applied to reduce n to a constant.
    Asked inGATE 2020GATE 2017
  • Linear recurrences with constant coefficients (e.g. T(n) = c1·T(n−1) + c2·T(n−2)) are solved by the characteristic equation;
    the dominant root r gives T(n) = Θ(rn)\Theta(r^{n}), with a factor of n if the root is repeated.
    Asked inGATE 2024
  • For T(n) = aT(n−1) + f(n) with a > 1, the homogeneous part dominates: T(n) = Θ(an⋅Σf(i)/ai)\Theta(a^{n} \cdot Σ f(i)/a^{i}), and for a polynomial f(n) the sum converges, so T(n) = Θ(an)\Theta(a^{n}) (e.g. T(n) = 2T(n−1) + n is Θ(2n)\Theta(2^{n})).
    Asked inGATE 2025
  • A recurrence of the form T(n) = T(n−1) + c (or T(n) = 2T(n/2) + c) solves to Θ(n)\Theta(n), so different-looking recurrences can have the same asymptotic complexity;
    always solve before comparing.
    Asked inGATE 2026
Pages 1–2 of 2