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:
Recursive Function (C Code):
int factorial(int n) {
if (n == 1)
return 1;
else
return n * factorial(n - 1);
}
Time Complexity Recurrence:
By expanding:
Final Time Complexity:
2. Power Function
Mathematical Definition:
Recursive Function (C Code):
long power(long x, long n) {
if (n == 0)
return 1;
else
return x * power(x, n - 1);
}
Time Complexity:
3. Fibonacci Numbers
Mathematical Definition:
Recursive Function:
int fib(int n) {
if (n <= 1)
return n;
else
return fib(n - 1) + fib(n - 2);
}
Time Complexity: due to overlapping subproblems.
Optimized (Dynamic Programming):
◇Solving Recurrence Relations
Three Primary Methods:
- Iteration Method (Expand and derive pattern)
- Substitution Method (Guess and prove by induction)
- Master's Theorem (Used for divide-and-conquer)
Iteration Method Example
Given:
Expanding:
Time Complexity:
Master's Theorem (For Divide and Conquer)
For recurrences of the form:
where , , and is asymptotically positive:
- If for some , then
- If , then
- If for some , and if for some and large enough , then
Examples of Divide and Conquer Recurrences
Binary Search
Using Master's Theorem:
, , Since , case 2 applies Thus,
Merge Sort
Using Master's Theorem:
, , Since , case 2 applies Thus,
◇Common Recurrence Relations and Their Solutions
| Recurrence Relation | Algorithm Example | Solution |
|---|---|---|
| Factorial | ||
| Binary Search | ||
| Merge Sort | ||
| Tree Traversal | ||
| Recursive Fibonacci | ||
| Recursive Function | ||
| Fast Multiplication | ||
| Strassen's Matrix Multiplication |
◇Common Pitfalls
Forgetting the base case when writing a recurrence — without it, the recurrence has no termination and cannot be solved Misidentifying , , and when applying the Master Theorem — is the number of subproblems, is the factor by which the input shrinks, and is the non-recursive work Applying the Master Theorem to recurrences where it doesn't apply (e.g., is not in the form ) Incorrect substitution in the iteration method — always expand at least 3 levels and verify the pattern before generalizing Confusing (, merge sort) with (, 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 ( with three cases). Key examples: binary search , merge sort , Fibonacci .
◇Links
Related: Source / Reference:
◇References & Navigation
- Previous Topic: ◇Worst-case running time
- Next Topic: ◇Searching Algorithms
- Related Notes: ◇Recursion Tree Method, ◇Substitution Method
ACTIVE RECALL VERIFICATION
Select an answer to verify conceptual comprehension and reveal the technical rationale.
What is a recurrence relation?
In a recursive algorithm, what is the "base case"?
For the factorial function , what is the final time complexity?
What is the time complexity of the naive recursive Fibonacci function ?
Which method for solving recurrences expands the equation step-by-step to derive a pattern?
Using the Master's Theorem on Binary Search , what is the final time complexity?
What is a common pitfall when solving recurrences?
In the Master Theorem formula , what does represent?
Can the Master Theorem be applied to the recurrence ?
What is the time complexity of the recurrence , representing a simple binary tree traversal?