PROJECTENGRAM

[HO] 04 - RECURRENCES

Recurrences

Tags: #◇Computer Science #◇CCDSALG #◇Recurrences #◇Problem Solving #◇M1


◇Overview

A recurrence is an equality or inequality that describes a function over integers or reals using the function itself. It is primarily used to describe the running time of recursive algorithms. A typical recurrence contains more than one case depending on the argument:

  • Base case: The terminating condition of the recursion.
  • Recursive case: The part of the function that calls itself with smaller arguments.

By convention, if a recurrence is stated without an explicit base case, it is assumed to be algorithmic. This means that for every sufficiently large threshold constant n0>0n_0 > 0:

  1. For all n<n0n < n_0, T(n)=Θ(1)T(n) = \Theta(1).
  2. For all n≥n0n \ge n_0, every path of recursion terminates in a defined base case within a finite number of recursive invocations.

◇Important Formulas

Solving Recurrences: Iteration Method

The iteration method involves expanding the recurrence repeatedly to identify a pattern. By expressing T(n)T(n) as a summation of terms, we can simplify it into a closed form or explicit formula and determine its Big-Oh complexity.

Useful Summation Formulas

  • Constant: ∑j=ab1=b−a+1\sum_{j=a}^{b} 1 = b - a + 1
  • Arithmetic Series: ∑j=0nj=n(n+1)2\sum_{j=0}^{n} j = \frac{n(n + 1)}{2}
  • Sum of Squares: ∑j=0nj2=n(n+1)(2n+1)6\sum_{j=0}^{n} j^2 = \frac{n(n + 1)(2n + 1)}{6}
  • Sum of Cubes: ∑j=0nj3=n2(n+1)24\sum_{j=0}^{n} j^3 = \frac{n^2(n + 1)^2}{4}
  • Geometric Series: ∑j=0nxj=xn+1−1x−1\sum_{j=0}^{n} x^j = \frac{x^{n+1} - 1}{x - 1}

Useful Logarithm Formulas

For all real a>0,b>0,c>0,na > 0, b > 0, c > 0, n and logarithm bases ≠1\neq 1:

  • a=blog⁡baa = b^{\log_b a}
  • log⁡b(an)=nlog⁡ba\log_b(a^n) = n \log_b a
  • Change of base: log⁡ba=log⁡calog⁡cb\log_b a = \frac{\log_c a}{\log_c b}
  • Logarithmic identity: alog⁡bc=clog⁡baa^{\log_b c} = c^{\log_b a}
  • Product rule: log⁡c(ab)=log⁡ca+log⁡cb\log_c(ab) = \log_c a + \log_c b
  • Reciprocal rule: log⁡b(1/a)=−log⁡ba\log_b(1/a) = -\log_b a
  • Base flip: log⁡ba=1log⁡ab\log_b a = \frac{1}{\log_a b}

◇Worked Examples

Example 1: Formulating a Recurrence for Factorial

『』
C
int factorial(int n) {
    if (n == 0) return 1;
    return n * factorial(n - 1);
}

The recurrence relation T(n)T(n) for this algorithm is:
T(n)={aif n=0T(n−1)+bif n>0T(n) = \begin{cases} a & \text{if } n = 0 \\ T(n - 1) + b & \text{if } n > 0 \end{cases}
where aa is the constant time taken for the base case, and bb is the constant time taken for the multiplication and function overhead in the recursive step.

Example 2: Iteration Method
Determine the explicit formula and Big-Oh notation for:
T(n)={2if n=1T(n−1)+3if n>1T(n) = \begin{cases} 2 & \text{if } n = 1 \\ T(n - 1) + 3 & \text{if } n > 1 \end{cases}
We expand T(n)T(n):

  1. T(n)=T(n−1)+3T(n) = T(n-1) + 3
  2. T(n)=(T(n−2)+3)+3=T(n−2)+2(3)T(n) = (T(n-2) + 3) + 3 = T(n-2) + 2(3)
  3. T(n)=(T(n−3)+3)+2(3)=T(n−3)+3(3)T(n) = (T(n-3) + 3) + 2(3) = T(n-3) + 3(3)

Generalizing to kk iterations:
T(n)=T(n−k)+k(3)T(n) = T(n - k) + k(3)

To reach the base case where n−k=1n - k = 1, we set k=n−1k = n - 1. Substituting this back:
T(n)=T(1)+3(n−1)T(n) = T(1) + 3(n - 1)
T(n)=2+3n−3T(n) = 2 + 3n - 3
T(n)=3n−1T(n) = 3n - 1

Therefore, the closed form is T(n)=3n−1T(n) = 3n - 1, which is O(n)O(n).

◇References & Navigation