Video lecture
Watch on YouTube(opens in a new tab)Sub topic 1
What Is an Algorithm?
An algorithm is a finite list of precise steps that takes some input and produces an output that solves a stated problem. This chapter gives you a working definition and introduces the example used throughout the tutorial: Euclid's method for finding the greatest common divisor.
Prerequisites
This chapter assumes you can already do the following:
- Write basic programs in any language, using variables, conditionals, loops and functions.
- Do integer arithmetic, including the remainder operation (modulo). For example, 48 mod 18 is 12.
- Read simple Python code.
You do not need any background in mathematics or computer science theory.
A working definition
An algorithm is a procedure that meets three conditions:
- It is made of clear steps. Each step says exactly what to do, so two people following it get the same result.
- It turns input into output. It starts from given data and ends with an answer.
- It finishes. After a finite number of steps it stops.
A cooking recipe is often given as an example, but it is a loose one. "Season to taste" is not a precise step, so two cooks following it will get different results. An algorithm cannot contain steps like that. A later chapter lists the formal properties in full. For now, keep the three conditions above in mind.
The running example: Euclid's algorithm
The greatest common divisor (GCD) of two integers is the largest integer that divides both of them exactly. The GCD of 48 and 18 is 6. Euclid described a method for finding it more than 2,000 years ago:
Euclid's algorithm (plain language)
Input: two non-negative integers a and b, not both zero.
Output: the greatest common divisor of a and b.
1. If b is 0, the answer is a. Stop.
2. Otherwise, compute r, the remainder of a divided by b.
3. Replace a with b, and b with r.
4. Go back to step 1.Each step is precise. The input and output are stated. The method also stops: the remainder is always smaller than b, so b gets smaller every time around the loop and must eventually reach 0.
Here is the same algorithm in Python:
def gcd(a, b):
while b != 0:
a, b = b, a % b
return a
print(gcd(48, 18)) # 6The values of (a, b) move through (48, 18), (18, 12), (12, 6) and (6, 0). When b reaches 0 the function returns 6.
The algorithm is the idea, not the code
The plain-language steps, the Python function and the flowchart all describe one algorithm. The algorithm is the method. Each of these is a different way of writing it down. Later chapters separate the problem, the algorithm and the program more carefully, and cover each notation in detail.
Common mistakes
- Treating any program as an algorithm. A program that loops forever on some inputs does not meet the definition for those inputs.
- Leaving out the input conditions. Euclid's algorithm needs inputs that are "not both zero". Without that rule, the case gcd(0, 0) has no defined answer.
- Confusing the algorithm with one way of writing it. Rewriting the Python code in Java does not create a new algorithm.
Exercise
A friend gives you this procedure: "Start with a positive integer n. While n is not 1, replace n with n - 2. Output 'done'." Does it meet all three conditions of the working definition for every positive integer input? Explain.
Show answer
No. The steps are clear and there is an input and an output, but the procedure does not always stop. For an even n such as 4, n goes 4, 2, 0, -2, and so on. It skips past 1 and runs forever. It stops only for odd n.
Exercise
Use the plain-language steps of Euclid's algorithm to compute gcd(35, 10). List the (a, b) pair at each step.
Show answer
(35, 10), then (10, 5), then (5, 0). Now b is 0, so the answer is 5.