Skip to content
Sahithyan's S2
Sahithyan's S2 — Data Structures and Algorithms

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

Aka. 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)=aT(nb)+O(nk)T(n) = a\,T\left( n - b \right) + O(n^k)

Where a,b>0a, b \gt 0 and k0k \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)=aT(nb)+O(nklogpn)T(n) = a\,T\Big( \frac{n}{b} \Big) + O(n^k \log^p n)

Where a1a \ge 1, b>1b \gt 1 and k0k \ge 0.

Here are the cases:

  • If k<logbak < \log_b{a} then T(n)=O(nlogba)T(n) = O(n^{\log_b a}).
  • If k=logbak = \log_b{a} and p<1p<-1 then T(n)=O(nk)T(n) = O(n^k).
  • If k=logbak = \log_b{a} and p=1p=-1 then T(n)=O(nkloglogn)T(n) = O(n^k \log \log n).
  • If k=logbak = \log_b{a} and p>1p>-1 then T(n)=O(nklogp+1n)T(n) = O(n^k \log^{p+1} n).
  • If k>logbak > \log_b{a} and p0p\ge 0 then T(n)=O(nklogpn)T(n) = O(n^k \log^p n).
  • If k>logbak > \log_b{a} and p<0p\lt 0 then T(n)=O(nk)T(n) = O(n^k).