DM 06 Induction and recursion

Previous: DM 05 Functions and Relations | DM 00 Course Map | Next: DM 07 Counting

One sentence

Induction proves a statement for every natural number by proving the first case and proving that each case forces the next; recursion is the same idea used to define and compute.

The principle

To prove that holds for all integers :

  1. Base case. Prove .
  2. Inductive step. Prove that for every , if then .

The assumption in step 2 is the inductive hypothesis.

Why it works: dominoes. The base case knocks over the first domino. The inductive step says each falling domino knocks over the next. So all fall. Both parts are essential: without the base case nothing starts; without the step the chain breaks.

You are not assuming what you want to prove

You assume for one arbitrary and derive . That is proving the implication , which is a different statement from ” for all ”.

Worked example 1: a sum

Claim. For all ,

Base case : left side , right side . True.

Inductive step. Assume . Then

That is the formula with . By induction it holds for all .

The key move: split off the last term so the inductive hypothesis can be applied to the rest. Always write down what says before you start, so you know your target.

Worked example 2: divisibility

Claim. is divisible by 3 for all .

Base: , divisible by 3.

Step. Assume . Then

Divisible by 3.

Worked example 3: an inequality

Claim. for all .

Base: .

Step. Assume with . Then . It remains to show , that is , that is , true for . So .

The base case starts at 5 for a reason: the claim is false at . Induction can begin anywhere.

Strong induction

Sometimes depends on cases earlier than . Strong induction lets you assume all of when proving . It is logically equivalent to ordinary induction, just more convenient.

Theorem. Every integer is a product of primes.

Proof. Base: 2 is prime. Step: assume every integer from 2 to is a product of primes. If is prime, done. Otherwise with . By hypothesis and are products of primes, so is too.

Ordinary induction fails here because the factors are not ; they are somewhere smaller.

Example with several base cases. Every amount of 12 or more can be paid with 4- and 5-unit coins. Bases: , , , . Step: for , the amount is payable by hypothesis; add one 4. You need four base cases because the step reaches back four.

A famous false proof

“All horses are the same colour.” Base: one horse, trivially true. Step: take horses. The first share a colour; the last share a colour; the two groups overlap, so all match.

The flaw: the step fails for . With two horses, “the first one” and “the last one” do not overlap. The chain breaks between the first and second domino. Check that your inductive step really works for the smallest .

Recursion

A recursive definition gives base values and a rule for building later values from earlier ones.

The Fibonacci numbers run

Recursion and induction are two faces of one idea: recursion defines by reduction to smaller cases; induction proves by reduction to smaller cases. Facts about recursively defined objects are proved by induction.

def factorial(n):
    if n == 0:                       # base case
        return 1
    return n * factorial(n - 1)      # recursive case

Correctness by induction. Base: factorial(0) returns . Step: if factorial(k) returns , then factorial(k+1) returns .

Every recursive function needs a base case and must move toward it. Otherwise it never stops.

Worked example: Tower of Hanoi

Move disks from one peg to another, one at a time, never placing a larger disk on a smaller. Strategy: move the top disks out of the way, move the largest, move the back on top. The number of moves satisfies

First values: . Guess: .

Proof by induction. Base: . Step: .

Guess the pattern from small cases, then prove it by induction. That is the standard workflow for solving recurrences (DM 09 Recurrences and Asymptotics).

Structural induction

Induction also works on recursively built structures such as lists, trees and formulas. Prove the property for the base objects, then show that each construction rule preserves it.

Example. A binary tree is either a single leaf or a root with two subtrees. Claim: a binary tree in which every internal node has two children has one more leaf than internal nodes. Base: one leaf, zero internal. Step: if the subtrees have and , the whole tree has leaves and internal nodes, and .

Loop invariants

A loop invariant is a statement that is true before the loop and stays true after each iteration. Proving it is induction on the number of iterations. When the loop ends, the invariant plus the exit condition gives correctness.

total = 0
for i in range(1, n + 1):
    total += i

Invariant: after the iteration with index , total equals . At exit, , so total is .

Common mistakes

  • Skipping the base case, or proving the wrong one.
  • Assuming somewhere inside the step.
  • Not using the inductive hypothesis. If you never used it, either the proof is wrong or induction was unnecessary.
  • Too few base cases when the step reaches back more than one.

Learn by doing

  1. Prove .
  2. Prove for .
  3. Prove that is divisible by 4 for .
  4. A sequence has , . Guess a formula and prove it.

Where this shows up

Dynamic programming is recursion with stored results. The Bellman equation defines the value of a state in terms of the values of its successors, and value iteration is proved to converge by an inductive argument (09 Reinforcement Learning). Backpropagation walks a computation graph recursively.