Video lecture
Watch on YouTube(opens in a new tab)Sub topic 2
The Properties Every Algorithm Must Have
This chapter assumes you can read simple Python, follow loops and conditionals, and compute remainders with %. It also builds on the running example from the previous chapter, Euclid's algorithm for the greatest common divisor (GCD).
A procedure is any list of steps. An algorithm is a procedure that meets five extra requirements, which Donald Knuth set out in The Art of Computer Programming. If a procedure fails any one of them, you cannot rely on it to solve a problem.
The Five Properties
| Property | Question it answers | Euclid's algorithm |
|---|---|---|
| Finiteness | Does it always stop? | Yes. `b` shrinks on every pass and cannot go below 0. |
| Definiteness | Is every step unambiguous? | Yes. "Remainder of a divided by b" has one meaning. |
| Input | What does it take in? | Two non-negative integers, not both zero. |
| Output | What does it produce? | One integer, the GCD. |
| Effectiveness | Can each step actually be carried out? | Yes. Division with remainder is basic arithmetic. |
Common Ways a Procedure Fails
Finiteness
A procedure can be clear and still run forever on some inputs. The procedure below never reaches zero if you start from an odd number:
def count_down_by_two(n, max_steps=20):
steps = 0
while n != 0:
n -= 2
steps += 1
if steps >= max_steps:
return f"gave up after {steps} steps"
return f"reached 0 in {steps} steps"
print(count_down_by_two(8)) # reached 0 in 4 steps
print(count_down_by_two(7)) # gave up after 20 stepsThe max_steps guard exists only so the demo stops. Without it, the call with 7 would loop forever. Finiteness has to hold for every valid input, so passing on most inputs is not enough. A later chapter shows how to prove finiteness with a decreasing measure.
Definiteness
Phrases such as "pick a suitable number" or "repeat until it looks right" fail this test, because two people following them can do different things. Programming languages enforce definiteness, but plain-language and pseudocode descriptions do not. You have to check those descriptions yourself.
Input and Output
Each input must come from a stated set. Euclid's algorithm with a = b = 0 has no meaningful answer, so the input set excludes that case. The output must be tied to the input. A procedure that always prints "done" produces output, but that output says nothing about the problem.
Effectiveness
Every step must be simple enough to do exactly, in finite time. "Compute the exact decimal expansion of √2" fails, because the expansion never ends. "Compute √2 to 10 decimal places" passes.
Exercises
Exercise
A recipe says "Add salt to taste, then simmer until done." Which algorithm properties does it violate, and why?
Show answer
It violates definiteness, because "to taste" and "until done" mean different things to different cooks. Because "until done" has no exact stopping test, you also cannot show that it has finiteness.
Exercise
Someone proposes a procedure: "While n is not 1: if n is even, set n to n/2; otherwise set n to 3n+1." Which property is hard to confirm, and why?
Show answer
Finiteness. Every step is definite and effective, but nobody has proved that the loop reaches 1 for every positive integer n. This is the open Collatz conjecture. Until it is proved, you cannot claim the procedure is an algorithm for all positive inputs.