PROJECTENGRAM

RECCURENCES

Reccurences

Tags: #◇CCDSALG #◇Algorithms #◇Computer Science #◇Searching #◇Data Structures #◇Search Efficiency #◇Analysis of Algorithms #◇Recursion #◇Sorting #◇Recursion and Iteration #◇Computational Complexity #◇Time Complexity #◇Space Complexity


◇Overview

A recurrence relation defines a sequence of values recursively: the value at a certain index is based on previous values in the sequence. These are...

◇Key Concepts

Base case Recursive case

◇Details / Theory

◇What are Recurrence Relations?

A recurrence relation defines a sequence of values recursively: the value at a certain index is based on previous values in the sequence. These are often used to express the running time of recursive algorithms.

◇What is Recursion?

Recursion is a programming technique where a function calls itself to solve smaller instances of the same problem until a base condition is met.

Key Concepts:

Base case: the condition under which recursion ends. Recursive case: the part of the function where the recursion happens.

◇Examples of Recursive Algorithms

1. Factorial

Mathematical Definition:

n!={1,if n=1n⋅(n−1)!,if n>1n! = \begin{cases} 1, & \text{if } n = 1 \\ n \cdot (n - 1)!, & \text{if } n > 1 \end{cases}

Recursive Function (C Code):

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

Time Complexity Recurrence:

T(n)=T(n−1)+b,T(1)=aT(n) = T(n - 1) + b, \quad T(1) = a

By expanding:

T(n)=T(n−1)+b=T(n−2)+2b=…=T(1)+(n−1)b=a+(n−1)b\begin{aligned} T(n) &= T(n - 1) + b \\ &= T(n - 2) + 2b \\ &= \ldots \\ &= T(1) + (n - 1)b = a + (n - 1)b \end{aligned}

Final Time Complexity: O(n)O(n)

2. Power Function

Mathematical Definition:

kn={1,if n=0k⋅kn−1,if n>0k^n = \begin{cases} 1, & \text{if } n = 0 \\ k \cdot k^{n-1}, & \text{if } n > 0 \end{cases}

Recursive Function (C Code):

『』
C
long power(long x, long n) {
    if (n == 0)
        return 1;
    else
        return x * power(x, n - 1);
}

Time Complexity: O(n)O(n)

3. Fibonacci Numbers

Mathematical Definition:

F(n)={0,if n=01,if n=1F(n−1)+F(n−2),if n>1F(n) = \begin{cases} 0, & \text{if } n = 0 \\ 1, & \text{if } n = 1 \\ F(n-1) + F(n-2), & \text{if } n > 1 \end{cases}

Recursive Function:

『』
C
int fib(int n) {
    if (n <= 1)
        return n;
    else
        return fib(n - 1) + fib(n - 2);
}

Time Complexity: O(2n)O(2^n) due to overlapping subproblems.

Optimized (Dynamic Programming): O(n)O(n)

◇Solving Recurrence Relations

Three Primary Methods:

  1. Iteration Method (Expand and derive pattern)
  2. Substitution Method (Guess and prove by induction)
  3. Master's Theorem (Used for divide-and-conquer)

Iteration Method Example

Given:

T(n)=T(n−1)+3,T(1)=2T(n) = T(n - 1) + 3, \quad T(1) = 2

Expanding:

T(n)=T(n−1)+3=T(n−2)+6=…=T(1)+3(n−1)=2+3(n−1)=3n−1\begin{aligned} T(n) &= T(n - 1) + 3 \\ &= T(n - 2) + 6 \\ &= \ldots \\ &= T(1) + 3(n - 1) = 2 + 3(n - 1) = 3n - 1 \end{aligned}

Time Complexity: O(n)O(n)

Master's Theorem (For Divide and Conquer)

For recurrences of the form:

T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)

where a≥1a \geq 1, b>1b > 1, and f(n)f(n) is asymptotically positive:

  1. If f(n)=O(nlog⁡ba−ϵ)f(n) = O(n^{\log_b a - \epsilon}) for some ϵ>0\epsilon > 0, then T(n)=Θ(nlog⁡ba)T(n) = \Theta(n^{\log_b a})
  2. If f(n)=Θ(nlog⁡ba)f(n) = \Theta(n^{\log_b a}), then T(n)=Θ(nlog⁡balog⁡n)T(n) = \Theta(n^{\log_b a} \log n)
  3. If f(n)=Ω(nlog⁡ba+ϵ)f(n) = \Omega(n^{\log_b a + \epsilon}) for some ϵ>0\epsilon > 0, and if af(n/b)≤cf(n)af(n/b) \leq cf(n) for some c<1c < 1 and large enough nn, then T(n)=Θ(f(n))T(n) = \Theta(f(n))

Examples of Divide and Conquer Recurrences

T(n)=T(n/2)+cT(n) = T(n/2) + c

Using Master's Theorem:
a=1a = 1, b=2b = 2, f(n)=cf(n) = c nlog⁡ba=nlog⁡21=n0=1n^{\log_b a} = n^{\log_2 1} = n^0 = 1 Since f(n)=c=Θ(1)=Θ(nlog⁡ba)f(n) = c = \Theta(1) = \Theta(n^{\log_b a}), case 2 applies Thus, T(n)=Θ(log⁡n)T(n) = \Theta(\log n)

Merge Sort

T(n)=2T(n/2)+cnT(n) = 2T(n/2) + cn

Using Master's Theorem:
a=2a = 2, b=2b = 2, f(n)=cnf(n) = cn nlog⁡ba=nlog⁡22=n1=nn^{\log_b a} = n^{\log_2 2} = n^1 = n Since f(n)=cn=Θ(n)=Θ(nlog⁡ba)f(n) = cn = \Theta(n) = \Theta(n^{\log_b a}), case 2 applies Thus, T(n)=Θ(nlog⁡n)T(n) = \Theta(n \log n)

◇Common Recurrence Relations and Their Solutions

Recurrence RelationAlgorithm ExampleSolution
T(n)=T(n−1)+cT(n) = T(n-1) + cFactorialT(n)=O(n)T(n) = O(n)
T(n)=T(n/2)+cT(n) = T(n/2) + cBinary SearchT(n)=O(log⁡n)T(n) = O(\log n)
T(n)=2T(n/2)+cnT(n) = 2T(n/2) + cnMerge SortT(n)=O(nlog⁡n)T(n) = O(n \log n)
T(n)=2T(n/2)+cT(n) = 2T(n/2) + cTree TraversalT(n)=O(n)T(n) = O(n)
T(n)=T(n−1)+T(n−2)T(n) = T(n-1) + T(n-2)Recursive FibonacciT(n)=O(2n)T(n) = O(2^n)
T(n)=2T(n−1)T(n) = 2T(n-1)Recursive FunctionT(n)=O(2n)T(n) = O(2^n)
T(n)=4T(n/2)+cnT(n) = 4T(n/2) + cnFast MultiplicationT(n)=O(n2)T(n) = O(n^2)
T(n)=8T(n/2)+cn2T(n) = 8T(n/2) + cn^2Strassen's Matrix MultiplicationT(n)=O(n3)T(n) = O(n^3)

◇Common Pitfalls

Forgetting the base case when writing a recurrence — without it, the recurrence has no termination and cannot be solved Misidentifying aa, bb, and f(n)f(n) when applying the Master Theorem — aa is the number of subproblems, bb is the factor by which the input shrinks, and f(n)f(n) is the non-recursive work Applying the Master Theorem to recurrences where it doesn't apply (e.g., T(n)=2T(n−1)T(n) = 2T(n-1) is not in the form T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n)) Incorrect substitution in the iteration method — always expand at least 3 levels and verify the pattern before generalizing Confusing T(n)=2T(n/2)+nT(n) = 2T(n/2) + n (O(nlog⁡n)O(n \log n), merge sort) with T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1 (O(n)O(n), binary tree traversal)

◇Summary

TL;DR — Recurrences model the running time of recursive algorithms. Solve them via iteration (expand and find a pattern), substitution (guess and verify), or the Master Theorem (T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n) with three cases). Key examples: binary search O(log⁡n)O(\log n), merge sort O(nlog⁡n)O(n \log n), Fibonacci O(2n)O(2^n).

Related: Source / Reference:

◇References & Navigation

CONFIRMATION // 理解度の確認

ACTIVE RECALL VERIFICATION

Select an answer to verify conceptual comprehension and reveal the technical rationale.

Q1

What is a recurrence relation?

Q2

In a recursive algorithm, what is the "base case"?

Q3

For the factorial function T(n)=T(n−1)+bT(n) = T(n - 1) + b, what is the final time complexity?

Q4

What is the time complexity of the naive recursive Fibonacci function T(n)=T(n−1)+T(n−2)T(n) = T(n-1) + T(n-2)?

Q5

Which method for solving recurrences expands the equation step-by-step to derive a pattern?

Q6

Using the Master's Theorem on Binary Search T(n)=T(n/2)+cT(n) = T(n/2) + c, what is the final time complexity?

Q7

What is a common pitfall when solving recurrences?

Q8

In the Master Theorem formula T(n)=aT(n/b)+f(n)T(n) = aT(n/b) + f(n), what does bb represent?

Q9

Can the Master Theorem be applied to the recurrence T(n)=2T(n−1)T(n) = 2T(n-1)?

Q10

What is the time complexity of the recurrence T(n)=2T(n/2)+1T(n) = 2T(n/2) + 1, representing a simple binary tree traversal?