Video lecture
Watch on YouTube(opens in a new tab)Sub topic 11
Does It Stop? Arguing Termination
This chapter assumes you can read Python loops and know the remainder operator (%). It also assumes you remember the finiteness property from earlier: an algorithm must finish after a finite number of steps. Here you will learn how to show that a particular loop actually does finish.
Why Termination Needs an Argument
A loop stops only when its condition becomes false. Testing a few inputs shows that it stopped on those inputs. It says nothing about the next input. For some loops nobody knows the answer. The Collatz loop below has been checked on enormous numbers of inputs, and no one has proved that it stops for every positive integer:
def collatz_steps(n):
steps = 0
while n != 1:
n = n // 2 if n % 2 == 0 else 3 * n + 1
steps += 1
return steps
print(collatz_steps(27)) # 111So you need a general argument. The standard one uses a decreasing measure.
The Decreasing Measure
A decreasing measure (also called a variant) is an expression over the algorithm's variables that meets three conditions:
- It is always a non-negative integer while the loop runs.
- It strictly decreases on every iteration, whatever path the loop body takes.
- It cannot go below its lower bound of zero, so it can decrease only finitely many times.
If such a measure exists, the loop can run at most as many times as the measure's starting value. Then it must stop.
Applying It to Euclid's Algorithm
In the loop while b != 0: a, b = b, a % b, take the measure to be b.
bstarts as a non-negative integer, anda % bis also one.- The new
bisa % b, and a remainder is always smaller than the divisor, so0 <= a % b < b. The measure strictly decreases. - A non-negative integer cannot decrease forever, so
breaches 0 and the loop stops.
For the trace of gcd(48, 18), b goes 18, 12, 6, 0. That is three iterations, which is within the bound of 18.
You can make the argument checkable by asserting it at run time:
def gcd_checked(a, b):
while b != 0:
old = b
a, b = b, a % b
assert 0 <= b < old, "measure did not decrease"
return a
print(gcd_checked(48, 18)) # 6For a for loop over a list, the measure is the number of items left, len(items) - i. That is why linear search always stops.
Common Errors
| Error | Example | Why it breaks the argument |
|---|---|---|
| Measure not an integer | `x = x / 2` while `x > 0` with floats | It can shrink for a very long time. Real numbers are not well-founded, so the count is not bounded by the starting value. |
| Decrease on only some paths | The counter is updated inside an `if` branch only | An iteration that skips the branch leaves the measure unchanged. |
| No lower bound | `i -= 1` with condition `i != 0`, starting at a negative `i` | The measure decreases forever. |
| Forgotten update | `while i < n:` and the body never changes `i` | The measure stays constant. |
Exercise
The loop i = 0 / while i < n: if items[i] == target: break / i += 1 searches a list of length n. Give a decreasing measure and explain why it works.
Show answer
Use n - i. It starts at n >= 0 and is a non-negative integer while i < n. Every iteration either exits immediately with break or increments i, which lowers n - i by 1. It cannot drop below 0 while the loop condition holds, so the loop runs at most n times.
Exercise
Why does "x is positive and halves each iteration" fail as a termination argument for while x > 0: x = x / 2 when x is a real number?
Show answer
The measure is not a non-negative integer. A positive real can be halved forever without reaching 0, so there is no bound on the number of steps. Floating-point underflow eventually stops the loop, but that comes from the machine's number format, not from the algorithm.