Recursion
Learning objectives
- Write a recursive definition for a function on
- Compute values of a recursively defined sequence by unwinding
- Distinguish well-defined recursions from ill-defined ones
- Connect recursion to induction as a proof technique
Recursion is induction's twin sibling. Induction proves a statement is true for every natural number by chaining base case and step; recursion defines a function at every natural number by combining a base value with a rule that builds on smaller cases. The factorial , the Fibonacci numbers, the Tower of Hanoi move-count, and every divide-and-conquer algorithm in your toolkit are recursive definitions in disguise. Once you see the pattern — say what happens at the base, then say how the answer at depends on smaller answers — you can build infinitely many objects from finite text, then prove their properties by induction.
Anatomy of a recursive definition
A function is defined recursively by giving:
- One or more base values:
- A recursive clause: a formula for when is larger than the base cases, depending on for some .
The recursion is well-defined when every chain of recursive calls eventually reaches a base case. The most common way to guarantee that is to insist that the recursive clause only refers to smaller arguments. The clause is not well-defined: computing would require , then , forever.
Three canonical examples
Factorial. and for . Unwinding gives .
Fibonacci. , and for . Two base values are needed because the rule reaches back two steps. The sequence starts
Tower of Hanoi. Let be the smallest number of single-disk moves to transfer a stack of disks. Then and for — move the top disks to a spare peg (that's moves), move the biggest disk (one move), then move the disks on top of it ( more). A short induction shows .
From recursion to closed form
A recursive definition tells you how to compute step by step but rarely gives an explicit formula. Finding a closed form (when one exists) usually has two acts. Act 1: guess. Compute and look for a pattern. Act 2: verify. Prove by induction that your conjectured formula matches. The Tower of Hanoi proof goes: compute . Conjecture . Verify base case . Induction: .
Pause and think: the definition for looks innocent. Is it well-defined? Trace a computation of and see what happens.
Try it
- Predict before computing. Then unwind: in order.
- Predict first, then verify by recursion: , then , all the way down to the base case.
- Compute for Tower of Hanoi using . Check against the closed form .
- The sequence gives . Conjecture a closed form and verify your conjecture for .
A trap to watch for
A common pitfall is writing a "recursion" that points the wrong way — toward larger arguments. The definition is not a recursion at all; it cannot be evaluated. Another pitfall is forgetting that two base values are needed when the recursive clause reaches back two steps (as in Fibonacci). If you write only and the rule , then is undefined and the sequence cannot be computed. Rule of thumb: the number of base cases equals the depth of the recursive reach. A two-step rule needs two base values; a three-step rule needs three.
What you now know
You can read and write recursive definitions, decide whether a recursion is well-defined (do the calls reach smaller arguments?), unwind a recursion to a numeric answer, and use induction to verify a guessed closed form. The next section, strong induction, gives you the proof tool that matches recursive definitions where the recursive clause reaches back more than one step at a time.
References
- Velleman, D. J. (2019). How to Prove It: A Structured Approach (3rd ed.). Cambridge University Press, §6.3.
- Knuth, D. E. (1997). The Art of Computer Programming, Vol. 1: Fundamental Algorithms (3rd ed.). Addison-Wesley, §1.2.8 (Fibonacci).
- Graham, R. L., Knuth, D. E., Patashnik, O. (1994). Concrete Mathematics (2nd ed.). Addison-Wesley, §6.6 (Fibonacci) and §1.1 (Hanoi).
- Wilf, H. S. (1994). generatingfunctionology. Academic Press, ch. 2.
- Cormen, T. H., Leiserson, C. E., Rivest, R. L., Stein, C. (2009). Introduction to Algorithms (3rd ed.). MIT Press, ch. 4.