This note is part of my learning notes on Data Structures & Algorithms.


Recurrence

A recurrence is an equation that describes the running time of a recursive algorithm. The general form of a recurrence relation is:

an=f(an1,an2,...,ank)a_n = f(a_{n-1},a_{n-2}, ..., a_{n-k})

For example, the running time of the merge sort algorithm can be described by the recurrence T(n)=2Tn2+O(n)T(n) = 2T\frac{n}{2} + O(n), where T(n)T(n) is the running time of the algorithm on an input size of nn. We can solve this recurrence to find the running time of the algorithm for any input size.

More Examples

  • Arithmetic Sequence

an=an1+da_n = a_{n-1} + d

  • Geometric Sequence

an=ran1a_n = r \cdot a_{n-1}

  • Fibonacci Sequence

Fn=Fn1+Fn2F_n = F_{n-1} + F_{n-2}

Methods use to solve recurrences:

  • Substitution Method
  • Recursion Tree Method
  • Master Theorem

Substitution Method

Substitution method solves recurrences by guessing the form of the solution and using mathematical induction to prove it.

Steps:

  1. Based on the recurrence relation, make an educated guess about the asymptotic form of the solution. This guess is often based on experience or patterns observed in simpler cases.
  2. Induction hypothesis: assume that the guessed form is correct for smaller values of kk.
  3. Inductive step: Prove that if the guess is correct for smaller values, it also holds for the general case nn.
  4. Verify that the solution holds for the initial conditions or base case.

Recursion Tree

Visualize the recurrence as a tree and sum the costs at each level.

Example: constructing a recursion tree for the recurrence T(n)=2T(n2)+cnT(n) = 2T(\frac{n}{2}) + cn


Master Theorem

The master theorem is a general method for solving recurrences of the form

T(n)=aT(nb)+f(n)T(n) = aT (\frac{n}{b}) + f(n)

where a1a \geq 1 and b>1b >1

  • nn : the size of the current problem
  • aa : the number of subproblems in the recursion
  • nb\frac{n}{b} : the size of each subproblem
  • f(n)f(n) : the cost of the work that has to be done outside the recursive calls (cost of dividing + merging)

The Master Theorem gives asymptotic bounds for the solution to such recurrences by comparing f(n)f(n) with nlogban^{log_b a}:

  • Case 1: Running time is dominated by the cost at the leaves
    • If f(n)=O(nlogbaϵ)f(n) = O(n^{log_b a-\epsilon}) for some ϵ>0\epsilon > 0, then

T(n)=θ(nlogba)T(n) = \theta(n^{log_b a})

  • Case 2: Running time is evenly distributed throughout the recursion tree
    • If f(n)=θ(nlogba)f(n) = \theta(n^{log_b a })

T(n)=θ(nlogbalog(n))T(n) = \theta(n^{log_b a} log(n))

  • Case 3: Running time is dominated by the cost at the root
    • If f(n)=Ω(nlogba+ϵ)f(n) = \Omega(n^{log_b a + \epsilon})

T(n)=θ(f(n))T(n) = \theta (f(n))

Steps

  1. Extract aa, bb and f(n)f(n) from a given recurrence.
  2. Determine nlogban^{log_b a}
  3. Compare f(n)f(n) and nlogban^{log_b a}
  4. Determine which case to apply

Akra-Bazzi Theorem

The Akra-Bazzi theorem is a generalization of the master theorem that applies to recurrences of the form:

T(n)=aiT(bin)+f(n)T(n) = \sum a_i T(b_i n) + f(n)

Steps

  1. Extract ai,bia_i, b_i and f(n)f(n) from a given recurrence.

  2. Find ρ\rho, where ρ\rho is a unique real number such that i=1kaibiρ=1\sum^k_{i=1} a_i b_i^{\rho} =1 (such ρ\rho always exist).

  3. The solution for the recurrence is then

T(n)=θ(nρ(1+1ng(u)up+1du))T(n) = \theta (n^{\rho} (1 + \int^n_1 \frac{g(u)}{u^{p+1}} du))


References