Ordinary Induction
Mathematical induction proves a statement for every natural number in two steps. It is the most important proof technique in computer science, because it is the logic of loops and recursion: if the first step works and each step preserves correctness, every step works.
The principle
To prove :
- Base case: prove .
- Inductive step: prove for every . The assumption is the induction hypothesis.
Think of dominoes: the first falls, and each falling domino knocks over the next, so all fall. The principle is equivalent to the Well Ordering Principle, but usually shorter to write.
A sum formula
Theorem. For all : .
Proof by induction on , with the formula.
Base case : the left side is , the right side is .
Inductive step: assume . Then
which is .
This is why the largest unsigned 32-bit integer is : it is the sum of 32 one-bits.
for n in range(8):
assert sum(2 ** i for i in range(n + 1)) == 2 ** (n + 1) - 1
print("checked n = 0..7")
print("sum of 2^i for i = 0..31:", sum(2 ** i for i in range(32)), "= 2^32 - 1 =", 2 ** 32 - 1)checked n = 0..7
sum of 2^i for i = 0..31: 4294967295 = 2^32 - 1 = 4294967295
An inequality and a divisibility claim
The base case does not have to be 0. To prove for all , prove and for .
Theorem. for all .
Proof. Base: . Step: assume with . Then . It remains to show , that is , which holds because for . So .
Theorem. for all .
Proof. Base: . Step: . The first term is divisible by 3 by the hypothesis, the second obviously.
A tiling argument
Induction also proves the existence of constructions. A courtyard with one square removed (for a statue) can always be tiled with L-shaped trominoes (three squares). Induction: split the board into four quarters; the statue lies in one of them; place one L-tromino at the centre covering one square of each of the other three quarters; now each quarter has exactly one square that is missing or covered, and the hypothesis tiles each. The proof is a recursive algorithm:
def tile(n, hole):
size = 2 ** n
board = [[0] * size for _ in range(size)]
counter = [0]
def solve(top, left, s, hr, hc):
if s == 1:
return
counter[0] += 1
t, h = counter[0], s // 2
holes = []
for dr in (0, 1):
for dc in (0, 1):
r0, c0 = top + dr * h, left + dc * h
if r0 <= hr < r0 + h and c0 <= hc < c0 + h:
holes.append((r0, c0, hr, hc))
else: # cover the corner touching the centre
cr, cc = top + h - 1 + dr, left + h - 1 + dc
board[cr][cc] = t
holes.append((r0, c0, cr, cc))
for r0, c0, a, b in holes:
solve(r0, c0, h, a, b)
board[hole[0]][hole[1]] = -1
solve(0, 0, size, *hole)
return board, counter[0]
board, used = tile(3, (2, 5))
for row in board:
print(" ".join(" #" if v == -1 else f"{v:2d}" for v in row))
print("trominoes used:", used, "= (4^3 - 1) / 3 =", (4 ** 3 - 1) // 3) 3 3 4 4 8 8 9 9
3 2 2 4 8 7 7 9
5 2 6 6 10 # 7 11
5 5 6 1 10 10 11 11
13 13 14 1 1 18 19 19
13 12 14 14 18 18 17 19
15 12 12 16 20 17 17 21
15 15 16 16 20 20 21 21
trominoes used: 21 = (4^3 - 1) / 3 = 21
Each number marks one L-shaped piece and # is the statue.
Common mistakes
- Forgetting the base case. " implies " is a valid inductive step, but there is no base case and the claim is false.
- A step that fails for small . The famous "all horses are the same color" argument uses two overlapping groups of horses; when going from 1 to 2 horses the groups do not overlap, and the proof breaks there.
In software
A recursive function is correct by induction: prove it for the base case, then prove each call is correct assuming the smaller recursive calls are. A loop is correct by induction on the number of iterations, using a loop invariant.
Exercises
Exercise 1: Sum of squares
Prove for .
Answer
Base : . Step: add and factor.
Solution
Base: . Step: assuming the formula for ,
which is the formula for .
Exercise 2: Divisibility
Prove that is divisible by 3 for all .
Answer
.
Solution
Base: . Step: . Both terms are multiples of 3, the first by the hypothesis.
Exercise 3: Find the flaw
"Claim: every satisfies . Step: if , add 1 to both sides to get ." What is wrong?
Answer
The base case fails: .
Solution
The inductive step is logically fine, but without a true base case nothing gets started. Induction needs both parts.
Exercise 4: Count tromino tilings' pieces (code)
How many trominoes tile a board with one square removed? Check the formula for .
Answer
: 1, 5, 21, 85, 341, 1365.
Solution
The board has free squares and each piece covers 3. Exercise 2 shows this is always an integer.
print([(4 ** n - 1) // 3 for n in range(1, 7)], all((4 ** n - 1) % 3 == 0 for n in range(1, 7)))[1, 5, 21, 85, 341, 1365] True
Test yourself
In the inductive step you prove…
- for all directly
To prove for all , the base case is…
Which programming construct is proved correct by induction most naturally?
- A constant declaration
- A recursive function
- An import statement
- A comment