1. Gyanastra
  2. Algorithms
  3. Dynamic Programming Basics
  4. Fibonacci Series Recursion and Exponential Time
Free lesson, no account needed 5 min read 5 practice questions after it
Asked in past papers1 time
  • UGC NET2011

Algorithms · Dynamic Programming Basics

Fibonacci Series Recursion and Exponential Time

Overview

The Fibonacci series is the classic example that shows why dynamic programming matters.
Its naive recursive implementation recomputes the same subproblems many times, making the running time exponential even though each individual call costs only O(1)O(1). Storing computed values in a table avoids this waste.

Key concepts

  • Fibonacci series: defined recursively as F(n)=0F(n) = 0 if n=0n = 0, F(n)=1F(n) = 1 if n=1n = 1, and F(n)=F(n−1)+F(n−2)F(n) = F(n-1) + F(n-2) otherwise. The series begins with 0 and 1 as initial values; each subsequent term is the sum of the previous two terms.
  • Order of recursive calls: write down the recursion tree, then traverse it top-down, left-to-right. That is the order in which the recursive calls are made (the same principle as in merge sort and quick sort).
  • Recomputation: the same subproblems are computed many times over. For example, F(2) is computed three times while finding F(5).
  • Exponential blow-up: if all calls are made, the number of calls is 2F(n+1)−1=Θ(ϕn)2F(n+1) - 1 = \Theta(\phi^n) with ϕ≈1.618\phi \approx 1.618. Each call costs O(1)O(1), so the total time complexity is Θ(ϕn)\Theta(\phi^n), which is O(2n)O(2^n) — exponential.
  • The fix (motivating dynamic programming): whenever a value is computed, save it in a table, since it is known the value will be of use again later.
    Before calling the function, check whether the value is already computed; if it is stored in the table, use the stored value instead of recomputing.

How it works

The naive recursive program for Fibonacci:

Fibonacci(n):
    if n == 0:
        return 0
    else if n == 1:
        return 1
    else:
        return Fibonacci(n-1) + Fibonacci(n-2)

Tracing the calls for F(5):

  1. F(5) calls F(4) and F(3).
  2. F(4) calls F(3) and F(2).
  3. F(3) calls F(2) and F(1).
  4. F(2) calls F(1) and F(0).
  5. When n=1n = 1, it simply returns 1.
  6. When n=0n = 0, it returns 0.
  7. F(2) adds these two results and returns the value upward.
  8. F(3) then calls F(1) again — but F(1) was already computed earlier. It is recomputed anyway.
  9. F(4) then calls F(2) — but F(2) was already computed earlier. It is recomputed again, which is completely a waste.

Formulas

  • F(n)=F(n−1)+F(n−2)F(n) = F(n-1) + F(n-2) for n≥2n \geq 2; F(0)=0F(0) = 0, F(1)=1F(1) = 1 — nn is the index of the Fibonacci term, F(n)F(n) is the value of the nn-th term.
  • Time per function call = O(1)O(1).
  • Total time complexity of naive recursion =O(2n)= O(2^n) (exponential; more tightly Θ(ϕn)\Theta(\phi^n) with ϕ≈1.618\phi \approx 1.618), since the number of calls is 2F(n+1)−12F(n+1) - 1.

Worked examples

Building the Fibonacci series term by term:

nF(n)How computed
00base case
11base case
21F(1) + F(0) = 1 + 0
32F(2) + F(1) = 1 + 1
43F(3) + F(2) = 2 + 1

Recursion tree for F(5):

  • F(5) → F(4), F(3)
  • F(4) → F(3), F(2)
  • F(3) → F(2), F(1)
  • F(2) → F(1), F(0)
  • F(3) (second occurrence) → F(2), F(1)
  • F(2) (second occurrence) → F(1), F(0)
  • F(2) (third occurrence) → F(1), F(0)

Execution order (top-down, left-to-right):

  1. F(5) calls F(4); F(4) calls F(3); F(3) calls F(2); F(2) calls F(1) → returns 1.
  2. F(2) calls F(0) → returns 0.
  3. F(2) adds 1 + 0 and returns the result upward.
  4. F(3) calls F(1) again — already computed before, recomputed anyway → returns 1.
  5. F(3) adds the two results and returns the value upward.
  6. F(4) calls F(2) again — already computed before, recomputed again (completely a waste).
  7. The same repeated recomputation continues for the remaining branches.

The repeated computation of F(1), F(2), F(3) at multiple places in the tree is the source of the exponential blow-up: the number of calls is 2F(n+1)−1=Θ(ϕn)2F(n+1) - 1 = \Theta(\phi^n) with ϕ≈1.618\phi \approx 1.618, each costing O(1)O(1), so total time is Θ(ϕn)=O(2n)\Theta(\phi^n) = O(2^n) — exponential.

Revision summary

  • Fibonacci recurrence: F(n)=F(n−1)+F(n−2)F(n) = F(n-1) + F(n-2), with F(0)=0F(0) = 0 and F(1)=1F(1) = 1.
  • The naive recursive program recomputes the same subproblems many times (e.g., F(2) is computed three times while finding F(5)).
  • Recursive call order = traverse the recursion tree top-down, left-to-right.
  • Each function call costs O(1)O(1), but the number of calls is 2F(n+1)−1=Θ(ϕn)2F(n+1) - 1 = \Theta(\phi^n) with ϕ≈1.618\phi \approx 1.618, so total time is O(2n)O(2^n) (tightly Θ(ϕn)\Theta(\phi^n)) — exponential.
  • The fix that motivates dynamic programming: store each computed value in a table; before calling the function, check the table and reuse the stored value if it is already computed.

Previous-year exam insights

  • The exact number of calls made by the naive recursive Fibonacci is 2F(n+1) − 1, which is Θ(φ^n) where φ = (1+√5)/2 ≈ 1.618 (the golden ratio);
    the recursion tree has F(n+1) leaves.
  • The space complexity of naive recursive Fibonacci is O(n)O(n) due to the maximum recursion-stack depth, not O(2n)O(2^{n}).
  • Memoized (top-down) and tabulated (bottom-up) Fibonacci both run in O(n)O(n) time; bottom-up uses O(n)O(n) space (or O(1)O(1) with two variables), while memoization uses O(n)O(n) space for the table plus O(n)O(n) stack.
  • The closed form (Binet's formula) is F(n) = (φ^n − ψ^n)/√5 with φ = (1+√5)/2 and ψ = (1−√5)/2, so F(n) grows as Θ(φ^n).
  • Dynamic programming requires optimal substructure and overlapping subproblems; Fibonacci has overlapping subproblems (repeated identical subproblems), which is exactly what memoization/tabulation eliminates.
  • The recursion tree for F(n) has F(n+1) leaves and F(n+1) − 1 internal nodes; the total number of nodes (calls) is 2F(n+1) − 1.
Pages 1–2 of 2