ABSTRACT
To find the recurrence relation, we model the time complexity by accounting for every action the algorithm takes. We break the code down into recursive costs (calls to self) and overhead costs (everything else).
The Extraction Framework
To build your equation for , analyze the algorithm’s code and identify these three components:
1. Identify the Recursive Calls ()
Count how many times the function calls itself in a single execution path.
- If it calls itself once:
- If it calls itself twice:
- Variable (): Represents the “branching factor” of your recursion tree.
2. Identify the Input Shrinkage ( or )
Look at the arguments passed to those recursive calls. How much smaller is the new input?
- Subtractive (Linear): If the input size decreases by a fixed amount (e.g.,
n-1), the call is . - Dividing (Geometric): If the input size is halved (e.g.,
n/2), the call is .
3. Identify the Local Work ()
Calculate the cost of all operations inside the current function call, excluding the recursive calls. This is the “overhead” or “combine” step.
- : Constant time operations (simple comparisons,
ifstatements, basic arithmetic). - : Linear operations (a
forloop that runs from to inside the function).
Common Patterns
| Algorithm Structure | Resulting Recurrence Relation |
|---|---|
Linear Reduction (e.g., FindMax) | |
Double Reduction (e.g., Fibonacci) | |
Divide & Conquer (e.g., Binary Search) | |
Split & Merge (e.g., Merge Sort) |
Walkthrough: countDoubleRec
Let’s extract the relation from your “00” counting algorithm:
Code snippet
countDoubleRec(string, n):
if n < 2:
// Base Case
return 0
if string[0,1] == "00":
// Recursive Call A
return 1 + countDoubleRec(string[1:], n-1)
else:
// Recursive Call B
return countDoubleRec(string[1:], n-1)- Recursive Calls (): Only one path is taken (either the
ifor theelse), so there is 1 recursive call. - Input Shrinkage: The string is sliced starting from the second character, so the size becomes .
- Local Work: The comparison
== "00"and the addition1 + ...are constant time operations, denoted as .
The Resulting Relation:
Next Steps
Once you have extracted the relation (like ), you can solve for the Closed Form to find the Big-O:
- For Linear reductions (), use Unraveling.
- For Geometric reductions (), use the Master Theorem.