Recurrence relations are mathematical equations or inequalities that define a sequence where each term is given as a function of its preceding terms. They are fundamental in computer science for analyzing the time complexity and space complexity of recursive algorithms, providing a precise way to understand how an algorithm's runtime scales with input size.
A recurrence relation expresses the value of a function (often representing time or space complexity) in terms of its values on smaller inputs, typically where . It consists of two main parts:
For example, the running time of an algorithm that divides a problem of size into two subproblems of size and then combines their results in time can be expressed as:
with a base case like .
Solving a recurrence relation means finding a closed-form solution, a non-recursive function of that describes . There are several common methods:
The Substitution Method involves two main steps:
Consider the recurrence for computing :
double power(double x, int n) {
if (n == 0) return 1;
else return x * power(x, n - 1);
}The recurrence relation for its time complexity is for , and , where and are constants.
Guess: . Let's try to prove for some constant and .
Base Case: For , is not possible unless . Let's try . For , . Let .
Inductive Step: Assume for all . Then . We want . This implies , or . So, if we choose and , the solution holds.
Consider the recursive Fibonacci sequence algorithm:
To simplify, the source suggests approximating , which makes . This approximation is generally loose but can give an upper bound.
Applying Iteration/Substitution with this approximation:
Following this pattern, after substitutions:
Let . Substitute :
Since (a constant), . Thus, , which is exponential.
The Iteration Method (also known as the Unrolling Method) involves repeatedly substituting the recurrence into itself until the base case is reached. This process helps reveal a pattern that can then be expressed as a summation.
Let .
After iterations:
The recursion stops when , which means , so . Substitute into the equation:
Since (a constant):
Thus, .
The Recursion Tree Method provides a visual way to analyze recurrences. Each node in the tree represents the cost of a subproblem. The total cost is the sum of costs at all nodes.
Let's visualize the recursion tree:
n^2
/ \
/ \
T(n/4) T(n/2)
/ \ / \
/ \ / \
T(n/16) T(n/8) T(n/8) T(n/4)
...Costs at each level:
Total Cost: The total cost is the sum of costs at all levels:
This is a geometric series with ratio . Since , the sum converges. The sum of an infinite geometric series is .
Therefore, .
The Master Method (or Master Theorem) provides a straightforward way to solve recurrences of the form:
where and are constants, is an asymptotically positive function, and can be or .
The theorem describes three cases:
Case 1: If for some constant (i.e., is polynomially smaller than ), then
Case 2: If (i.e., is asymptotically equal to ), then
Case 3: If for some constant (i.e., is polynomially larger than ), AND it satisfies the (for some constant and all sufficiently large , ), then
Example 1 (Case 1):
The Tower of Hanoi is a classic puzzle that demonstrates recursive problem-solving. It involves moving disks from a source peg to a destination peg using an auxiliary peg, with rules:
The recursive solution breaks down into three steps:
Let be the number of moves required for disks.
Solving (by Iteration):
After iterations:
The sum is a geometric series sum .
The recursion stops when , so . Substitute :
Since :
The time complexity is .
#include <iostream>
#include <vector>
#include <string>
// Function to solve Tower of Hanoi puzzle
// n: number of disks
// source: starting peg
// auxiliary: intermediate peg
// destination: target peg
void towerOfHanoi(int n, char source, char auxiliary, char destination) {
// Base case: If there's only one disk, move it directly from source to destination
if (n == 1) {
std::cout << "Move disk 1 from " << source << " to " << destination << std::endl;
return;
}
// Step 1: Move n-1 disks from source to auxiliary, using destination as temporary
towerOfHanoi(n - 1, source, destination, auxiliary);
The Fibonacci sequence is defined as , and for . A direct recursive implementation leads to the recurrence:
As shown earlier in the substitution method, this recurrence yields an exponential time complexity of due to redundant computations (many subproblems are solved multiple times). While the exact solution involves the golden ratio, for a basic upper bound, the approximation to suffices to show its exponential nature.
An equation or inequality that describes a function in terms of its values on smaller inputs, crucial for analyzing recursive algorithms.
A technique for solving recurrence relations by making an educated guess for the solution and then proving its correctness using mathematical induction.
A method that involves repeatedly expanding the recurrence relation, identifying a pattern in the terms, and summing the costs to reach a closed-form solution.
A visual technique that models the costs of a recursive algorithm by drawing a tree where each node represents the cost of a subproblem, allowing for systematic summation of costs across levels.
A powerful theorem providing a cookbook approach to solve recurrence relations of the form $T(n) = aT(n/b) + f(n)$, covering three distinct cases based on the relationship between $f(n)$ and $n^{\log_b a}$.
Test your understanding with 5 questions
Which of the following is the recurrence relation for the time complexity of the classic Tower of Hanoi problem with $n$ disks?
Using the Master Method, what is the time complexity of a recurrence relation $T(n) = 4T(n/2) + O(n)$?
For a recurrence relation $T(n) = 2T(n/2) + O(n)$, which of the following describes its time complexity using the Master Method?
If a recurrence relation is given by $T(n) = 3T(n/4) + O(n^2)$, what is its asymptotic time complexity using the Master Method?
Which method for solving recurrence relations is best suited for generating an educated guess about the form of the solution, which can then be formally proven using induction?
Example 2 (Case 2): (e.g., Merge Sort)
Example 3 (Case 3):
6 Modules
6 Modules