Solving Recurrence Relations
Master the mathematical techniques to calculate the time complexity of recursive algorithms. This section covers the Substitution Method, Recursion Tree Method, and the highly-tested Master's Theorem.
Learning Goals
- Understand what a recurrence relation is and why it's necessary for recursive algorithms.
- Memorize the three cases of the Master's Theorem and identify when it fails.
- Trace mathematical solutions using the Substitution Method for linear recurrences.
- Visualize how recursive function calls split work using the Recursion Tree Method.
The Basics of Recurrence
While calculating the time complexity of a standard for loop is straightforward, analyzing a recursive algorithm (an algorithm that calls itself, like Merge Sort or Binary Search) is mathematically complex. We cannot simply count loops. Instead, we must define the running time as a mathematical equation called a Recurrence Relation.
A recurrence relation defines a sequence based on its previous terms. For example, the time to sort an array of size might be defined as the time to sort two halves plus the time to merge them .
There are three primary methods to solve these recurrences and find the asymptotic bound:
- The Substitution Method: Expanding the equation mathematically to guess and prove the bound.
- The Recursion Tree Method: Visualizing the recursive calls as a tree and summing the work done at each level.
- The Master's Theorem: A direct, formulaic shortcut for solving divide-and-conquer recurrences.
The Master's Theorem (The Shortcut)
The Master's Theorem provides a "cookbook" method for solving recurrences of the specific form:
Where:
- : The number of recursive subproblems.
- : The factor by which the input size is divided.
- : The cost of the work done outside the recursive calls (like merging or partitioning).
To solve, you always calculate the critical exponent: , and compare it to .
The 3 Cases:
- Case 1 (Heavy Leaves): If for some constant . Meaning: The work at the leaves dominates. Solution:
- Case 2 (Balanced Work): If . Meaning: The work is evenly distributed across the tree levels. Solution:
- Case 3 (Heavy Root): If for some constant . Meaning: The work at the root (the term) completely dominates. Regularity Condition: You must also prove for . Solution:
Trace: Master's Theorem Application
- 1Step 1
Given the recurrence . We identify the constants: , , and the function .
- 2Step 2
We calculate , which is . Since , our critical exponent is .
- 3Step 3
We compare with our critical exponent . Since grows polynomially faster than (specifically, ), Case 3 applies. The work at the root dominates.
- 4Step 4
For Case 3, we MUST check if . Substituting our values: . This holds true for any constant . The condition is satisfied.
- 5Step 5
By Master's Theorem Case 3, the time complexity is strictly determined by . Therefore, .
Trace: The Substitution Method
- 1Step 1
Given with a base case of . Let's expand to see the pattern: .
- 2Step 2
Substitute the expansion back into the original equation: . Let's expand one more time for clarity: . So, .
- 3Step 3
We can clearly see a pattern emerging. After expansions, the equation looks like this: .
- 4Step 4
The recursion stops when we hit the base case . This happens when , which means . Substitute into our generalized equation: .
- 5Step 5
Since , the equation becomes the sum of the first natural numbers: . The mathematical formula for this arithmetic progression is . Expanding this gives . Dropping constants yields .
The Recursion Tree Method
While the Substitution Method relies heavily on algebra, the Recursion Tree Method relies on visualization. It converts the recurrence into a tree where:
- Each node represents the cost of a single subproblem.
- The tree branches represent the recursive calls.
To find the total complexity, you sum the costs across all nodes at each depth to get a set of per-level costs, and then sum those per-level costs across the entire depth of the tree.
Example Visualization for (Merge Sort):
- Level 0 Cost: 1 node =
- Level 1 Cost: 2 nodes =
- Level 2 Cost: 4 nodes =
At every single level, the total work done is . How deep does the tree go before hitting size 1? The depth of a tree that halves at each step is . Therefore, Total Cost = (Cost per level) (Number of levels) = .
Knowledge Check
The recurrence T(n) = T(n/2) + 1 has a time complexity of: