Fibonacci Using Recursion in DSA C - Time & Space Complexity
We want to understand how the time needed to find a Fibonacci number grows as the input number gets bigger.
How does the number of steps change when we use recursion to find Fibonacci numbers?
Analyze the time complexity of the following code snippet.
int fibonacci(int n) {
if (n <= 1) {
return n;
}
return fibonacci(n - 1) + fibonacci(n - 2);
}
This code calculates the nth Fibonacci number by calling itself twice for smaller values until it reaches the base cases.
Identify the loops, recursion, array traversals that repeat.
- Primary operation: Recursive calls to fibonacci for n-1 and n-2.
- How many times: Each call generates two more calls until base cases, causing many repeated calculations.
Each number requires calculating two smaller Fibonacci numbers, which themselves require two smaller ones, and so on.
| Input Size (n) | Approx. Operations |
|---|---|
| 10 | About 177 calls |
| 20 | About 21,891 calls |
| 30 | About 2,692,537 calls |
Pattern observation: The number of calls grows very fast, roughly doubling with each increase in n.
Time Complexity: O(2^n)
This means the time needed roughly doubles with each increase in n, making it very slow for large numbers.
[X] Wrong: "Since the function calls itself twice, the time complexity is just twice the input size, or O(n)."
[OK] Correct: Each call creates two more calls, leading to an exponential number of calls, not just a simple multiple.
Understanding this recursive pattern helps you recognize when recursion can be inefficient and when to use better methods like memoization or iteration.
"What if we add a memory to store results of fibonacci calls (memoization)? How would the time complexity change?"
Practice
Solution
Step 1: Understand Fibonacci sequence definition
The Fibonacci sequence is defined such that each number is the sum of the two numbers before it.Step 2: Relate to recursion
Recursion calls the function for the two previous numbers and adds their results to get the current number.Final Answer:
Each number is the sum of the two preceding numbers -> Option AQuick Check:
Fibonacci = sum of two previous numbers [OK]
- Thinking Fibonacci multiplies previous numbers
- Confusing subtraction or division with addition
- Assuming Fibonacci uses only one previous number
Solution
Step 1: Identify base cases for Fibonacci
Fibonacci sequence starts with 0 and 1 for n=0 and n=1 respectively, so base case returns n when n is 0 or 1.Step 2: Match base case syntax
The condition if (n <= 1) return n; correctly returns 0 for n=0 and 1 for n=1.Final Answer:
if (n <= 1) return n; -> Option CQuick Check:
Base case returns n for n=0 or 1 [OK]
- Returning 2 for base case
- Returning 0 for n=1 incorrectly
- Missing base case leading to infinite recursion
int fib(int n) {
if (n <= 1) return n;
return fib(n-1) + fib(n-2);
}
int main() {
printf("%d", fib(4));
return 0;
}Solution
Step 1: Calculate fib(4) using recursion
fib(4) = fib(3) + fib(2) - fib(3) = fib(2) + fib(1) = (fib(1)+fib(0)) + 1 = (1+0)+1=2 - fib(2) = fib(1) + fib(0) = 1 + 0 = 1 So fib(4) = 2 + 1 = 3Step 2: Confirm output printed
The printf prints the integer 3.Final Answer:
3 -> Option BQuick Check:
fib(4) = 3 [OK]
- Confusing fib(4) with fib(5)
- Adding wrong fib values
- Forgetting base cases
int fib(int n) {
if (n == 0) return 0;
else return fib(n-1) + fib(n-2);
}Solution
Step 1: Check base cases
Function only returns 0 for n==0 but misses base case for n==1, causing infinite recursion for n=1.Step 2: Fix by adding base case for n==1
Adding if (n == 1) return 1; fixes recursion and returns correct Fibonacci values.Final Answer:
Add base case for n == 1 to return 1 -> Option AQuick Check:
Both n=0 and n=1 base cases needed [OK]
- Missing base case for n=1
- Changing base case values incorrectly
- Removing else causing syntax confusion
Solution
Step 1: Understand repeated calculations in recursion
Recursive Fibonacci calls fib(n-1) and fib(n-2) repeatedly, causing many duplicate calls and inefficiency.Step 2: Use memoization to store results
Memoization saves computed Fibonacci values in a cache, so repeated calls return stored results instantly, improving speed.Final Answer:
Use memoization to store and reuse computed Fibonacci values -> Option DQuick Check:
Memoization avoids repeated work [OK]
- Increasing recursion depth doesn't fix inefficiency
- Using loops without caching still repeats work
- Calling fib(n-1) twice wastes time
Ready to prove it?
Pass the test and this pattern moves unknown
