Recursion

Recursive

A recursive algorithm or function is the one that calls itself to solve a smaller version of its task. Terminating or base conditions are required.

Divide & Conquer

  • Divide
    Split a problem into smaller sub-problems.
  • Conquer
    Solve a subset of smaller sub-problems recursively.
  • Combine
    Combine the solutions to solve the initial problem.

Examples:

Analysis

Recurrence Relation

A mathematical equation that defines the overall cost of solving a problem in terms of the cost of solving its smaller sub-problems. It provides a way to express the time complexity of recursive algorithms.

3 common methods are used to solve a recurrence relation.

Substitution Method

The form of the solution is guessed and verified by induction. Then the constants are solved for.

Iteration Method

Also known as recursion-tree method. The recurrence is expanded step-by-step to identify patterns and dervice a solution. Good for generating guesses for substituion method but can be unreliable.

Master Theorem

For Decreasing Functions

Applies to below form:

T(n)=a T(n−b)+O(nk)T(n) = a\,T\left( n - b \right) + O(n^k)

Where a,b>0a, b \gt 0 and k≥0k \ge 0.

Here are the cases:

  • If a<1a \lt 1 then T(n)=O(nk)T(n) = O(n^k)
  • If a=1a=1 then T(n)=O(nk+1)T(n) = O(n^{k+1})
  • If a>1a \gt 1 then T(n)=O(nkan/b)T(n) = O(n^{k} a^{n/b})

For Dividing Functions

Applies to recurrences of the below form:

T(n)=a T(nb)+O(nklog⁡pn)T(n) = a\,T\Big( \frac{n}{b} \Big) + O(n^k \log^p n)

Where a≥1a \ge 1, b>1b \gt 1 and k≥0k \ge 0.

Here are the cases:

  • If k<log⁡bak < \log_b{a} then T(n)=O(nlog⁡ba)T(n) = O(n^{\log_b a}).
  • If k=log⁡bak = \log_b{a} and p<−1p<-1 then T(n)=O(nk)T(n) = O(n^k).
  • If k=log⁡bak = \log_b{a} and p=−1p=-1 then T(n)=O(nklog⁡log⁡n)T(n) = O(n^k \log \log n).
  • If k=log⁡bak = \log_b{a} and p>−1p>-1 then T(n)=O(nklog⁡p+1n)T(n) = O(n^k \log^{p+1} n).
  • If k>log⁡bak > \log_b{a} and p≥0p\ge 0 then T(n)=O(nklog⁡pn)T(n) = O(n^k \log^p n).
  • If k>log⁡bak > \log_b{a} and p<0p\lt 0 then T(n)=O(nk)T(n) = O(n^k).
Written by October 17, 2026 2 min read
Was this helpful?