Why Recursive Fibonacci Is Exponential: A Step‑by‑Step Complexity Analysis
Introduction
In this lesson we break down the time complexity of the classic recursive implementation of the Fibonacci sequence and compare it with its iterative counterpart.
How the Recursive Algorithm Works
- The Fibonacci sequence starts with 0 and 1; every subsequent term is the sum of the two preceding terms.
- The recursive function
fib(n): - Returns
nifn ≤ 1(covers the first two elements). - Otherwise calls
fib(n‑1)andfib(n‑2), adds the results, and returns the sum.
Building the Recurrence Relation
Assume each elementary operation (comparison, subtraction, addition) costs 1 unit of time.
- For n > 1 the function performs:
* 1 comparison
* 2 subtractions (n‑1, n‑2)
* 1 addition
→ 4 units of overhead.
- Therefore the running time satisfies:
T(n) = T(n‑1) + T(n‑2) + 4 for n > 1
with base cases T(0) = T(1) = 1 (only the comparison).
Lower‑Bound Approximation
If we pretend T(n‑1) ≈ T(n‑2), the recurrence becomes:
T(n) ≈ 2·T(n‑2) + C where C = 4.
Unfolding this relation repeatedly yields:
T(n) = 2^{k}·T(n‑2k) + (2^{k}‑1)·C
Setting n‑2k = 0 gives k = n/2 and:
T(n) ≈ 2^{n/2}·T(0) + (2^{n/2}‑1)·C
Thus T(n) = Θ(2^{n/2}), a lower bound.
Upper‑Bound Approximation
Now assume the opposite extreme: T(n‑2) ≈ T(n‑1). The recurrence simplifies to:
T(n) ≈ 2·T(n‑1) + C
Repeating the expansion leads to:
T(n) = 2^{k}·T(n‑k) + (2^{k}‑1)·C
Choosing k = n (so n‑k = 0) gives:
T(n) ≈ 2^{n}·T(0) + (2^{n}‑1)·C
Hence T(n) = Θ(2^{n}), an upper bound.
Putting It All Together – Big‑O
Both bounds show exponential growth. In algorithm analysis we usually quote the tightest upper bound, i.e., the Big‑O notation:
fib_recursive(n) ∈ O(2^{n})
In contrast, an iterative Fibonacci implementation runs in linear time:
fib_iterative(n) ∈ O(n)
Why This Matters
- Exponential algorithms (like the recursive Fibonacci) become infeasible even for modest inputs (e.g.,
n = 100would take years). - Linear algorithms handle huge inputs (e.g.,
n = 1,000,000) instantly. - Understanding the growth rate helps you choose the right approach and avoid hidden performance traps.
Key Concepts Covered
- Recurrence relations for recursive functions
- Approximation techniques for lower and upper bounds
- Exponential vs. linear time complexity
- Big‑O notation as an upper‑bound descriptor
Quick Takeaway
The naïve recursive Fibonacci algorithm grows exponentially (O(2^{n})), making it dramatically slower than the simple loop‑based version (O(n)). Knowing how to derive and interpret these bounds is essential for writing efficient code.
Recursive Fibonacci runs in exponential time (O(2^n)), while an iterative version runs in linear time (O(n)); therefore, for any realistic input size, the iterative approach is vastly more efficient.
Frequently Asked Questions
Who is mycodeschool on YouTube?
mycodeschool is a YouTube channel that publishes videos on a range of topics. Browse more summaries from this channel below.
Does this page include the full transcript of the video?
Yes, the full transcript for this video is available on this page. Click 'Show transcript' in the sidebar to read it.
How the Recursive Algorithm Works
- The Fibonacci sequence starts with 0 and 1; every subsequent term is the sum of the two preceding terms. - The recursive function `fib(n)`: 1. Returns `n` if `n ≤ 1` (covers the first two elements). 2. Otherwise calls `fib(n‑1)` and `fib(n‑2)`, adds the results, and returns the sum.
Why This Matters
- **Exponential algorithms** (like the recursive Fibonacci) become infeasible even for modest inputs (e.g., `n = 100` would take years). - **Linear algorithms** handle huge inputs (e.g., `n = 1,000,000`) instantly. - Understanding the growth rate helps you choose the right approach and avoid hidden performance traps.
Helpful resources related to this video
If you want to practice or explore the concepts discussed in the video, these commonly used tools may help.
Links may be affiliate links. We only include resources that are genuinely relevant to the topic.