Sub topic 14
Reasoning with Loop Invariants
This chapter assumes you can write pseudocode loops, build a trace table, and desk check a loop for off-by-one errors. Those skills come from earlier chapters. A trace shows that a loop works for one input. A loop invariant lets you argue that it works for every input.
What an Invariant Is
A loop invariant is a statement about the loop's variables that is true at the start of every iteration. It usually describes the part of the work that is already finished. Take FindMax:
FUNCTION FindMax(A, n)
max ← A[1]
FOR i ← 2 TO n DO
IF A[i] > max THEN
max ← A[i]
END IF
END FOR
RETURN max
END FUNCTIONInvariant: at the start of the iteration for index i, max equals the largest element of A[1..i-1].
The Three Checks
You prove that an invariant holds by checking three things.
| Check | Question | FindMax |
|---|---|---|
| Initialization | Is it true before the first iteration? | i = 2, and max = A[1], the largest element of A[1..1]. True. |
| Maintenance | If it is true at the start of one iteration, is it true at the start of the next? | max is the largest of A[1..i-1]. Comparing it with A[i] makes it the largest of A[1..i]. True for i + 1. |
| Termination | When the loop exits, what does the invariant tell you? | The loop exits with i = n + 1, so max is the largest of A[1..n]. |
The termination check needs both the invariant and the exit condition. Neither one alone tells you the answer is correct.
Checking an Invariant in a Trace
You can add the invariant as a column in your trace table. For A = [3, 7, 2, 9]:
start of iteration | i | A[1..i-1] | max | invariant holds?
-------------------+---+-------------+-----+-----------------
1 | 2 | [3] | 3 | yes
2 | 3 | [3, 7] | 7 | yes
3 | 4 | [3, 7, 2] | 7 | yes
exit | 5 | [3, 7, 2, 9]| 9 | yesIf a row says "no", then either the invariant is wrong or the code has a bug. Both are worth knowing.
Invariants Find Boundary Errors
Here is the corrected summing loop from the desk-checking chapter:
total ← 0
i ← 1
WHILE i ≤ n DO
total ← total + i
i ← i + 1
END WHILE
RETURN totalInvariant: at the start of each iteration, total = 1 + 2 + ... + (i - 1). The loop exits when i = n + 1, so total = 1 + ... + n. With the buggy condition i < n, the loop exits at i = n, and the invariant gives only 1 + ... + (n - 1). The off-by-one error is visible without running any trace.
Common Mistakes
- Stating something that is always true but useless, such as "i is an integer". The invariant must lead to the result when the loop ends.
- Checking the invariant in the middle of the body. It only has to hold at the start of each iteration and must be checked at that point.
- Skipping initialization, especially for loops that run zero times.
Exercise
InsertionSort's outer loop runs FOR j ← 2 TO n. Its invariant is "A[1..j-1] is sorted at the start of each outer iteration." State what initialization and termination each tell you.
Show answer
Initialization: when j = 2, A[1..1] has a single element, so it is sorted. Termination: the loop exits with j = n + 1, so A[1..n] is sorted. The whole array is in order.
Exercise
For the summing loop, n = 0. Does the invariant still give the right answer?
Show answer
Yes. Initialization gives total = 0, which is the empty sum for i = 1. The condition 1 ≤ 0 is false, so the loop exits with i = 1 = n + 1, and total = 0, which is correct.