Video lecture
Watch on YouTube(opens in a new tab)Sub topic 3
Problems, Algorithms and Programs Are Different Things
This chapter assumes you can read simple Python, know what a function and a loop are, and are comfortable with the remainder operator (%). It also builds on the previous chapter's idea that an algorithm is a finite, definite, effective procedure.
People often use "the problem", "the algorithm" and "the code" as if they meant the same thing. They are three separate layers, and keeping them apart makes it much easier to reason about each one.
Three layers
- A problem states what must be computed. It relates inputs to correct outputs and says nothing about how to get there.
- An algorithm is a step-by-step method that solves the problem. It says how, but it does not depend on any particular language.
- A program is one concrete implementation of an algorithm in a programming language. It runs on a real machine and has to deal with syntax, types and resource limits.
The relationship fans out. One problem can be solved by many algorithms, and one algorithm can be implemented by many programs.
One problem, two algorithms
The problem here is to find the greatest common divisor of two non-negative integers that are not both zero. Here are two different algorithms that solve it, each written as a Python program.
def gcd_euclid(a, b):
while b != 0:
a, b = b, a % b
return a
def gcd_trial(a, b):
if a == 0 or b == 0:
return a + b # gcd(x, 0) is x
d = min(a, b)
while a % d != 0 or b % d != 0:
d -= 1
return d
for pair in [(48, 18), (17, 5), (0, 9), (100, 75)]:
print(pair, gcd_euclid(*pair), gcd_trial(*pair))Both functions print the same answers: 6, 1, 9 and 25. They solve the same problem, but they are different algorithms. Trial division counts downward from the smaller number and can take up to min(a, b) steps. Euclid's algorithm replaces the pair with smaller remainders and finishes far sooner on large inputs. Comparing algorithms for the same problem is how you choose between them, and later chapters show how to measure that cost.
One algorithm, many programs
The code below is a second program for Euclid's algorithm. It is written differently from gcd_euclid but runs the same steps.
def gcd_euclid_v2(a, b):
while b > 0:
remainder = a % b
a = b
b = remainder
return a
print(gcd_euclid_v2(48, 18)) # 6It uses a temporary variable where the first version used tuple assignment, and it tests b > 0 where the first tested b != 0. Those details belong to the program layer. The algorithm, "replace (a, b) with (b, a mod b) until b is 0", has not changed.
Common errors
| Mistake | Why it causes trouble |
|---|---|
| Treating a bug as a flaw in the algorithm | The algorithm can be correct while the program is wrong, for example because of an off-by-one error or the wrong operator. |
| Treating the code as the specification | Whatever the code happens to return becomes "correct", so there is nothing independent to test it against. |
| Judging speed from a single run | Run time mixes the algorithm's cost with language, hardware and input. Compare algorithms by counting their steps. |
| Ignoring language limits | An algorithm on unbounded integers can overflow in a program that uses 32-bit integers. |
Exercise
A colleague writes a C function for Euclid's algorithm that returns wrong results for very large inputs. Python's gcd_euclid gives correct results on the same inputs. Which layer is at fault, and why?
Show answer
The program layer. Both functions implement the same algorithm, and the Python version is correct on those inputs, so the method is sound. The C version most likely suffers from integer overflow or a type error, which is an implementation issue, not a flaw in Euclid's algorithm or in the problem statement.
Exercise
Name the problem, the algorithm and the program in this sentence: "I used binary search, written in Java, to find a name in a sorted list."
Show answer
The problem is finding a name in a sorted list. The algorithm is binary search. The program is the Java code that implements binary search.