Skip to main content

DSAPath10 mincore

Fibonacci: Recursive, Iterative, Matrix, and Golden-Ratio Forms

The Fibonacci sequence is a compact case study in recursion, iteration, dynamic programming, matrix exponentiation, and closed-form analysis.

Why This Matters

Fibonacci is small enough to compute by hand and rich enough to show several algorithmic styles. The same recurrence can be exponential, linear, or logarithmic depending on representation.

Core Idea

The sequence satisfies:

F0=0,F1=1,Fn=Fn1+Fn2.F_0 = 0,\quad F_1 = 1,\quad F_n = F_{n-1} + F_{n-2}.

MethodTimeIdea
naive recursionexponentialrecomputes subproblems
memoized recursionO(n)O(n)caches subproblems
iterationO(n)O(n)keeps last two values
matrix exponentiationO(logn)O(\log n)repeated squaring
closed formmodel-dependentuses the golden ratio formula

Worked example: computing F_5 recursively recomputes F_3 more than once. Memoization stores it once.

Common Mistakes

  • Using naive recursion as if it were a reasonable implementation.
  • Treating the closed form as exact floating-point code for large nn.
  • Missing that matrix exponentiation is divide and conquer on powers.

Exercises

  • Draw the recursion tree for F_5.
  • Write the iterative state update for Fibonacci.
  • Explain why memoization changes the number of computed states.

Next Topics

References

  • Cormen, Leiserson, Rivest, Stein. Introduction to Algorithms (4th ed., 2022). Ch. 4, 14.
  • Sedgewick and Wayne. Algorithms (4th ed., 2011). Ch. 1.