Recurrences
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:
For example, the running time of the merge sort algorithm can be described by the recurrence , where is the running time of the algorithm on an input size of . We can solve this recurrence to find the running time of the algorithm for any input size.
More Examples
- Arithmetic Sequence
- Geometric Sequence
- Fibonacci Sequence
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:
- 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.
- Induction hypothesis: assume that the guessed form is correct for smaller values of .
- Inductive step: Prove that if the guess is correct for smaller values, it also holds for the general case .
- 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
Master Theorem
The master theorem is a general method for solving recurrences of the form
where and
- : the size of the current problem
- : the number of subproblems in the recursion
- : the size of each subproblem
- : 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 with :
- Case 1: Running time is dominated by the cost at the leaves
- If for some , then
- Case 2: Running time is evenly distributed throughout the recursion tree
- If
- Case 3: Running time is dominated by the cost at the root
- If
Steps
- Extract , and from a given recurrence.
- Determine
- Compare and
- 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:
Steps
Extract and from a given recurrence.
Find , where is a unique real number such that (such always exist).
The solution for the recurrence is then