Solving recurrences: substitution, tree and Master theorem
Solving the recurrences of recursive algorithms: the recursion tree, the substitution method with induction, the Master theorem with its three cases and the logarithmic extension, the recurrences it does not cover, and a table of the standard solutions.
Why it exists
A recursive algorithm's cost satisfies an equation in terms of itself: mergesort on items sorts two halves and then merges, so . To compare algorithms we need in closed form, at least its order. Three tools do almost everything: the recursion tree (see the pattern), the Master theorem (a shortcut for the most common shape), and substitution (proof by induction of a guess).
Prerequisites
You need asymptotic notation (asymptotic notation) and a little about recurrence relations (recurrence relations).
Precise definitions
Recurrence. An equation defining by smaller values of together with a base case (usually , which we assume whenever it is not written). Floors and ceilings in the arguments (n/2 as ) do not change the order for the recurrences here, so one pretends that is a power of the divisor.
Divide-and-conquer recurrence. with subproblems of size () and the cost of dividing and combining.
Recursion tree method. Draw the tree of calls. Level has subproblems of size ; if , one subproblem at level costs and the whole level costs . There are levels, the last one with leaves. The total is the sum over the levels, a geometric series with ratio :
- ratio (the levels get more expensive downwards): dominated by the leaves, ;
- ratio (every level costs the same, ): ;
- ratio (the levels get cheaper): dominated by the root, .
The Master theorem. Let with constants , , and let (so is the number of leaves). Then
- If for some ( polynomially smaller): .
- If with ( the same order, up to logarithms): . For : .
- If for some ( polynomially larger) and the regularity condition holds for some constant and large : .
For the conditions reduce to comparing with : is case 1; is case 2; is case 3 (the regularity condition holds automatically for such ). The theorem does not cover the gap where is smaller than only by a logarithmic factor: for the solution is , and for with it is (these need the tree).
Substitution method. (1) Guess the form of the solution (from the tree, or from small cases). (2) Prove by induction that for a suitable constant , assuming it for smaller arguments, substituting into the recurrence. (3) Check the base case. If the induction fails by a lower-order term, strengthen the hypothesis by subtracting a lower-order term.
Change of variable. For put : satisfies , so and .
Recurrences with subtraction. is solved by summing: (so gives , gives ). With a multiplier, doubles at each step: , exponential. grows like the Fibonacci numbers, with . The Master theorem does not apply to any of these (the subproblem size is , not ).
How it works
The recursion tree of for . Every level costs 1024: two subproblems of half the size replace one, and the sum of sizes is constant. There are 11 levels, so the total is about . This is the pattern of mergesort.
Recursion tree of T(n) = 2T(n/2) + n: start
Computed by a script, not typed| level | subproblems | size of each | cost of one | cost of the level |
|---|---|---|---|---|
| 0 | ||||
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 | ||||
| 10 |
Step 0
Every level of the tree of T(n) = 2T(n/2) + n costs the same, n: the number of subproblems doubles while their size halves. Here n = 1024, so there are 11 levels.
For the levels cost : a geometric series with ratio , dominated by the root. The total is about : (case 3).
Recursion tree of T(n) = T(n/2) + n: start
Computed by a script, not typed| level | subproblems | size of each | cost of one | cost of the level |
|---|---|---|---|---|
| 0 | ||||
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 | ||||
| 10 |
Step 0
Now there is one subproblem per level, half the size: the costs per level fall geometrically.
For the levels cost : ratio 2, dominated by the leaves, of them: (case 1).
Recursion tree of T(n) = 4T(n/2) + n: start
Computed by a script, not typed| level | subproblems | size of each | cost of one | cost of the level |
|---|---|---|---|---|
| 0 | ||||
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 | ||||
| 7 | ||||
| 8 | ||||
| 9 | ||||
| 10 |
Step 0
Four subproblems of half the size: the number of subproblems grows faster than their cost shrinks.
Ten Master theorem verdicts for standard algorithms. For each, was evaluated exactly for up to 60 and the claimed bound was confirmed (the ratio stays steady, so it is neither too low nor too high by a factor such as ).
Master theorem verdicts: start
Computed by a script, not typed| recurrence | n^(log_b a) | f(n) | case | solution | confirmed | |
|---|---|---|---|---|---|---|
| 1 | ||||||
| 2 | ||||||
| 3 | ||||||
| 4 | ||||||
| 5 | ||||||
| 6 | ||||||
| 7 | ||||||
| 8 | ||||||
| 9 | ||||||
| 10 |
Step 0
Master theorem for T(n) = aT(n/b) + f(n): compare f(n) with n^(log_b a). If f is polynomially smaller the leaves dominate (case 1); if f has the same growth, every level costs about the same and a log factor appears (case 2); if f is polynomially larger the root dominates (case 3). Each verdict below was confirmed by evaluating T(b^m) for m up to 60 and checking that T(n) / g(n) stays within a factor of two between m = 20 and m = 60.
And the recurrences that need other tools:
Recurrences outside the Master theorem: start
Computed by a script, not typed| recurrence | solution | why | method | |
|---|---|---|---|---|
| 1 | ||||
| 2 | ||||
| 3 | ||||
| 4 | ||||
| 5 | ||||
| 6 |
Step 0
Recurrences that are not of the form aT(n/b) + f(n), or that need another method. Each solution below was confirmed numerically by evaluating the recurrence.
Worked examples
EX 1Master theorem: read off a, b, f
Easy(Karatsuba): , , , , : case 1, . : : case 3 (regularity: ), . (binary search): , , : case 2, . : : . (Strassen): : .
EX 2A logarithm in f(n)
Easy: and , the middle case with : (a tree with levels each costing about at the top, with the logarithm shrinking towards the bottom, gives in the sum). By contrast : , so is polynomially larger, case 3, with regularity : . Case 3 needs to be larger by a polynomial factor: versus qualifies.
EX 3Substitution proof
MediumClaim: is . Hypothesis: for all . Then for . The base cases are covered by choosing large enough. A subtle case: is , but the induction gives , which does not close; the stronger hypothesis gives and works. (The exact solution with is ; checked for up to .)
EX 4When the Master theorem does not apply
Medium: and is smaller than but not by a polynomial factor, so no case applies. Level of the tree (with ) costs , so the sum over the levels is plus the leaves: , where is the harmonic sum: . (Confirmed with the exact value for up to 39.) : subtraction, not division: . has two different subproblem sizes: every level of the tree costs at most and the depth is between and , so (the numerical ratio is steady for from to ).
EX 5Quick solutions
GATE level(a) : . (b) : . (c) : , : case 1, . (d) : , : middle case with : . (e) : the two subproblems cost and , so the levels shrink by : the root dominates, . (f) : , . When a question asks "which of the following is the solution", try the Master theorem first, then the tree.
Cheat box
Cheat box
- : compare with .
- Case 1: polynomially smaller gives . Case 2: same order gives (with in : ). Case 3: polynomially larger (and regular) gives .
- Standard: : ; : ; : ; : ; : ; : ; : .
- Gap (): tree gives . Not Master: forms, unequal splits.
- : sum of . . Fibonacci-like: .
- : . Unequal split : .
- Substitution: guess, induct, strengthen the hypothesis by subtracting a lower-order term if needed.
Traps and GATE patterns
Connections
Builds on
Unlocks
Across subjects
Recurrences describe the cost of divide and conquer algorithms (mergesort, quicksort, binary search) and, with sums, of recursive algorithms generally. The tools of asymptotic notation are used throughout; analysing loops covers the iterative case. The mathematical side of linear recurrences is in recurrence relations.
PYQs
20 previous year questions from your book. I re-solved each one myself (19 by running or simulating, 1 by reasoning only); the book's key is shown next to my answer. All PYQs
Consider the following recurrence relations: For all n>1, T1(n)=4T1(n/2)+T2(n) T2(n)=5T2(n/4)+Θ(log2n) Assume that for all n≤1, T1(n)=1 and T2(n)=1. Which one of the following options is correct?
- (a)T1(n)=θ(n2)
- (b)T1(n)=θ(n2log2n)
- (c)T1(n)=θ(nlog4 5)
- (d)T1(n) = Θ(nlog45log2n)
Which of the following can be recurrence relation(s) corresponding to an algorithm with time complexity O(n)?
- (a)T(n)=T(n-1) +1, T(1)=1
- (b)T(n)=2T(n/2)+1 , T(1)=1
- (c)T(n)=2T(n/2)+n , T(1)=1
- (d)T(n)=T(n-1)+n , T(1)=1
Consider the following recurrence relation: T(n)=2T(n-1)+ n2n for n>0, T(0)=1 Which ONE of the following options is CORRECT?
- (a)T(n)=θ(n22n)
- (b)T(n)=θ(n2n)
- (c)T(n)=θ((logn)22n)
- (d)T(n)=θ(4n) 4
Consider the following recurrence relation:
T(n) = {√n ⋅ T(√n) + n
for n ≥ 1
1
for n = 1
Which one of the following options is CORRECT?- (a)T(n) = θ(nloglogn)
- (b)T(n) = θ(n2logn)
- (c)T(n) = θ(nlogn)
- (d)T(n) = θ(n2loglogn)
Let T(n) be the recurrence relation defined as follows: T(0) = 1, T(1) = 2 and T(n) = 5T(n - 1) - 6T(n - 2) for n ≥ 2 Which one of the following statement is TRUE?
- (a)T(n) = θ(n·3n)
- (b)T(n) = θ(n·2n)
- (c)T(n) = θ(3n)
- (d)T(n) = θ(2n) ALGORITHMS b b
For constants a ≥ 1 and b > 1, consider the following recurrence defined on the non-negative integers: T(n) = a T (n/b) + f(n) Which one of the following options is correct about the recurrence T(n) ?
- (a)f(n) is O(nlog (a)-∈) for some ∈>0, then T(n) is T(n)=Θ(n log (a))
- (b)If f(n) is n/log2(n), then T(n) is Θ(log2(n))
- (c)If f(n) is Θ(nlogb (a)), then T(n) is Θ(nlogb (a))
- (d)If f(n) is n log2(n), then T(n) is Θ(nlog2(n))
Consider the following recurrence relation.
T(n) = T(n/2) + T(2n/5) + 7n
, if n > 0
{
1 , if n = 0For parameters a and b, both of which are ω(1), T(n) = T(n 1/a ) + 1, and T(b)=1. Then T(n) is:
- (a)θ(log ab n)
- (b)θ(log a log b n)
- (c)θ(log 2 log 2 n)
- (d)θ(log b log a n)
Consider the recurrence function 2 × T(√n) + 1, if n > 2
The given diagram shows the flowchart for a recursive function A(n). Assume that all statements, except for the recursive calls, have O(1) time complexity. If the worst-case time complexity of this function is O(nα ), then the least possible value (accurate up to two decimal positions) of α is . Flowchart for Recursive Function A(n)
Which one of the following correctly determines the solution of the recurrence relation with T(1)=1? T(n)=2T(n/2) + logn
- (a)θ( n )
- (b)θ( n log n )
- (c)θ( n2 )
- (d)θ( log n )
The running time of an algorithm is represented
by the following recurrence relation:
n ; n ≤ 3
T(n) = {
n
T ( ) + cn; otherwise
3When n = 2(2k) for some k >= 0, the recurrence relation T(n) = √(2)T(n/2) + √(n), T(1) = 1 evaluates to :
- (a)(√n)(log n + 1)
- (b)(√n) log n
- (c)(√n) log (√n)
- (d)n log (√n) if(n == 0 || n == 1) return n; else return (2*f1(n-1) + 3*f1(n-2)); } int f2(int n) { int i; int X[N], Y[N], Z[N] ; X[0] = Y[0] = Z[0] = 0; X[1] = 1; Y[1] = 2; Z[1] = 3; for(i = 2; i <= n; i++) { X[i] = Y[i-1] + Z[i-2]; Y[i] = 2*X[i]; Z[i] = 3*X[i]; } return X[n] ; } The running time of f1(n) and f2(n) are
- (a)Θ(n) and Θ(n)
- (b)Θ(2n) and Θ(n)
- (c)Θ(n) and Θ(2n)
- (d)Θ(2n) and Θ(2n) ALGORITHMS Check Out Our Gate Courses & Secure Your Dream Rank !
What is the Asymptotic Notation of the following
recursive function:
int DoSomething (int n) {
if (n <= 2)
return 1;
else
return (DoSomething (floor(sqrt(n))) + n); }- (a)θ (n2)
- (b)θ (nlog2n)
- (c)θ (log2n)
- (d)θ (log2log2n)
Consider the following recurrence:
T(n) = 2T([√n]) + 1, T(1) = 1
Which one of the following is true?
int i,n;
for(i=2;i<=sqrt(n);i++)
if(n%i == 0)
{printf("Not Primen"); return 0;}
return 1;
} Let T(n)denote the number of times the for
loop is executed by the program on input n.
Which of the following is TRUE?- (a)T(n)=O(√n ) and T(n)=Ω(√n )
- (b)T(n)=O(n) and T(n)=Ω(√n )
- (c)T(n)=O(√n ) and T(n)=Ω(1)
- (d)None of the above
- (a)T(n)= θ (loglogn)
- (b)T(n)= θ (logn)
- (c)T(n)= θ (√n)
- (d)T(n)= θ (n)
Suppose T(n)=2T(n/2)+n, T(0)=T(1)=1 Which one of the following is FALSE?
- (a)T(n)=O(n2)
- (b)T(n)=Θ(n log n)
- (c)T(n)=Ω(n2)
- (d)T(n)=O(n log n) ALGORITHMS Check Out Our Gate Courses & Secure Your Dream Rank ! 4 4
Consider the following C-function:
double foo (int n)
{
int i;
double sum;
if (n = = 0) return 1.0;
else
{
sum = 0.0;
for (i = 0; i < n; i++)
sum += foo (i);
return sum;
}
}
Suppose we modify the above function foo() and
store the values of foo (i), 0≤i < n, as and
when they are computed. With this modification,
the time complexity for function foo() is
significantly reduced. The space complexity of
the modified function would be:- (a)O(1)
- (b)O(n)
- (c)O(n!)
- (d)O(nn)
Consider the following C functions:
int f1(int n)
{
if (n == 0 || n == 1)
return n;
else
return (2 * f1(n - 1) + 3 * f1(n - 2));
}
int f2(int n)
{
int i;
int X[N], Y[N], Z[N];
X[0] = Y[0] = Z[0] = 0;
X[1] = 1;
Y[1] = 2;
Z[1] = 3;
for (i = 2; i <= n; i++) {
X[i] = Y[i - 1] + Z[i - 2];
Y[i] = 2 * X[i];
Z[i] = 3 * X[i];
}
return X[n];
}
The running time of f1(n) and f2(n) are- (a)Θ(n) and Θ(n)
- (b)Θ(2n) and Θ(n)
- (c)Θ(n) and Θ(2n)
- (d)Θ(2n) and Θ(2n)
f1(8) and f2(8) return the values
- (a)1661 and 1640
- (b)59 and 59
- (c)1640 and 1640
- (d)1640 and 1661
The recurrence relation capturing the optimal execution time of the Towers of Hanoi problem with n discs is
- (a)T(n) = 2T(n - 2) + 2
- (b)T(n)= 2T(n - 1) + n
- (c)T(n)= 2T(n/2) + 1
- (d)T(n)= 2T(n - 1) + 1
Self-check
Question 1
Solve .
Show answerHide
: case 3 with regularity : .
Question 2
Solve .
Show answerHide
: case 2, .
Question 3
Solve .
Show answerHide
.
Question 4
Why can the Master theorem not solve with case 3, and what is the answer?
Show answerHide
is not polynomially larger than . It falls under case 2 with a logarithmic factor: .
Revision card
One-screen revision
- ; compare with : smaller gives ; equal gives ; larger (regular) gives .
- Tree: levels cost : geometric up (leaves), constant (log factor), down (root).
- Not Master: forms (sum, , Fibonacci), (), unequal splits ( for , ).
- Substitution: guess, induct, strengthen if the lower-order term breaks the induction.
References
- ·Cormen, Leiserson, Rivest, Stein, Introduction to Algorithms (3rd edition), Chapter 4 Divide-and-Conquer (4.3 the substitution method, 4.4 the recursion-tree method, 4.5 the master method)
- ·Kleinberg and Tardos, Algorithm Design, Chapter 5 Divide and Conquer (5.2 further recurrence relations)
Verification
The Master theorem as implemented in scripts/algo_lib.py was compared with the exact numerical solution of 486 recurrences T(n) = a T(n/b) + n^k log^p n (a up to 9, b in 2 to 4, six exponents including fractions, log powers 0 to 2): at n = b^300 the ratio T(n) / g(n) for the claimed bound g is steady between n = b^100 and b^300, and a missing or an extra logarithm is rejected (scripts/enum-checks/algo_analysis.py). The solutions of the recurrences outside the Master theorem were checked by exact integers (2T(n-1) + 1 = 2^n - 1), exact sums (2T(n/2) + n/log n = n(1 + H_m)) or ratio tests. The recursion-tree tables are computed for n = 1024. Textbook chapter references are from memory; the statement of case 2 with a logarithmic factor follows the common extended form. SECOND PASS (2026-11-02, done by the assistant, not a human check): every Self-check question was solved before the printed answer was read and all agreed; the definitions, worked examples, cheat box and traps were read claim by claim and the numbers re-derived by hand or by a small script (loop counts, recurrences, merge and quicksort counts and averages, hash probes and load-factor formulas, DP tables, MST and shortest-path runs, Floyd matrices); long C programs were not re-read line by line (they are compiled and compared by npm run verify:c, and their printed outputs were checked against the prose); tables and animations are machine output and were read, not re-run; No other defect found. Status raised to self-checked; verified needs a textbook or human check.
last verified: never
My notes (saved in this browser)