Study
CSC-401 route
Unit 1 · Lesson 1 of 4 17 min

Definition of an Algorithm

A finite, unambiguous recipe that always produces the right answer, and the awkward questions that definition raises.

A cook in Kano has a recipe for jollof rice that has been in the family for three generations. It works. It is not an algorithm.

"Add water till it looks right" is not a step you can follow twice and get the same result. "Fry the pepper" does not say how hot, how long, or in what. A recipe that depends on a cook's judgement is not reproducible by a machine, however much skill it embodies.

Now look at the recipe for computing a greatest common divisor, written down 2,300 years ago by Euclid. Every step says exactly what to do. It always finishes.

It gives the same answer on every machine, in every century.

That is an algorithm, and the difference between the two is what this lesson is about.

An algorithm is the oldest idea in computing and still the sharpest.

An algorithm is a sequence of unambiguous instructions for solving a problem — that is, for obtaining a required output for any legitimate input in a finite amount of time.

Every word in that sentence is doing work. Sequence — the order matters, and the instructions are carried out one after another. Unambiguous — each step has exactly one meaning, so two people following it do the same thing.

Required output and legitimate input — the problem must be specified precisely enough to say which inputs are allowed and what answer is wanted. Finite amount of time — it must stop, on every permitted input, and produce something.

Notice what the definition does not say. It does not mention a computer. An algorithm can be carried out with a pencil, or by a person, or by a machine — the machine is just faster and more reliable.

It also does not say the algorithm must be fast, only that it must finish. Speed is the subject of the rest of this course, and it is a separate question from correctness.

The Problem, the Algorithm and the Program

Three things are often confused, and the distinction matters for everything that follows.

  • The problem is a specification: what input is allowed and what output is required. "Sort a list of numbers into non-decreasing order" is a problem. It does not say how.
  • The algorithm is a method for solving that problem: an unambiguous sequence of steps. There can be many algorithms for one problem, and they can differ enormously in cost.
  • The program is an implementation of an algorithm in a particular language, for a particular machine. The same algorithm can be many programs, and one of them may be fast while another is slow — but never by an order of magnitude that changes the class.

The chain is problem → algorithm → program → execution on real input. The middle link is where this course lives, because that is where the intellectual work is and where the cost is decided. If the algorithm is wrong, no amount of tuning saves it; if it is exponential, no amount of hardware saves it either.

A problem is a specification, an algorithm is a method for solving it, and a program is an implementation of that method on a particular machine.

Problem what must be done inputs, outputs Algorithm unambiguous method this course correctness and cost Program in a language for a machine Run on real input One problem, several algorithms; one algorithm, many programs. cost is decided here, not in the code
The chain from problem to execution. The algorithm is the link that decides cost, and it is the link this course analyses.

The Requirements

The classic list of requirements for an algorithm has five items, and it is the list a full answer is expected to contain.

  1. Finiteness. The algorithm must terminate after a finite number of steps, for every legitimate input. An algorithm that loops forever on some input is not an algorithm; it is a procedure that fails.
  2. Definiteness. Every step must be rigorously and unambiguously specified. "Choose a good pivot" is not definite; "choose the first element of the subarray" is.
  3. Input. The set of legitimate inputs must be specified clearly, including the range of values and the special cases.
  4. Output. The required output must be specified, and presumably related to the input in a meaningful way.
  5. Effectiveness. Every step must be sufficiently simple and basic to be carried out, in principle, by a person with pencil and paper in a finite time. "Find the largest prime factor" as a single step is not effective unless you say how.

An algorithm must be correct on every legitimate input, not just the ones you thought of.

That last point is where most student algorithms fail in practice, and where the awkward cases concentrate. An algorithm that works on positive numbers and loops or crashes on zero is not an algorithm for "integers" — it is an algorithm for "positive integers", and the specification must say so. The gap between what the code does and what the problem permits is where bugs live.

Euclid's Algorithm

The oldest algorithm still in daily use solves a genuinely useful problem.

Problem. Given two non-negative integers m and n, not both zero, find gcd(m, n), their greatest common divisor.

The naive approach is to try every integer from min(m, n) downwards until one divides both. That works, and for large numbers it is slow. Euclid's algorithm rests on a single equality:

gcd(m, n) = gcd(n, m mod n), for n not equal to zero.

Why it is true, in one line: any common divisor of m and n also divides m − qn for any integer q, and m mod n is exactly m − qn for some q; the argument runs both ways, so the two pairs have the same set of common divisors, and hence the same greatest one.

So repeatedly replace the pair (m, n) by (n, m mod n). The second number strictly decreases each time (it is a remainder, so it is smaller than n), and it cannot decrease below zero, so the process must stop — and it stops when the second number becomes 0, at which point the first number is the answer.

Trace. gcd(60, 24) = gcd(24, 60 mod 24) = gcd(24, 12) = gcd(12, 24 mod 12) = gcd(12, 0) = 12.

That is the whole algorithm, and it is worth noticing how completely it is an algorithm: definite steps, guaranteed termination, a specified input range ("not both zero" — which is exactly why the problem statement excludes gcd(0, 0), since every integer divides 0, so there is no greatest divisor).

Worked example

Euclid’s Algorithm Written Two Ways

The algorithm is usually written in pseudocode, and the same method can be written iteratively or with a step-and-goto style. Both are algorithms; the pseudocode is easier to reason about.

  1. 1
    State the input and output

    Input: two integers m and n, both non-negative, not both zero. Output: their greatest common divisor.

One more property of Euclid's algorithm is worth naming, because it generalises: there is usually more than one algorithm for a problem, and they can differ enormously. Naive gcd by testing every candidate takes about min(m, n) steps; Euclid takes about log(min(m, n)). For two ten-digit numbers that is the difference between ten billion steps and about thirty.

Speed is a property of the algorithm, not of the computer.

Comparison of an Algorithm and a Program
AlgorithmProgram
What it isAn unambiguous method for solving a problemAn implementation of a method in a language
Machine dependenceNoneDepends on language, compiler, hardware
Expressed inPseudocode, a flowchart, prose, mathematicsSource code
Properties studiedCorrectness, termination, time and space complexityCompilation, memory layout, actual running time
How many for one problemMany, differing in costMany per algorithm, differing in constant factor
Can it be proved correct?Yes, by reasoning about the methodOnly as far as the algorithm beneath it

Pause & think

A student writes a procedure to find the largest number in a list: start with the first element as the maximum; for every other element, if it is larger than the current maximum, remember it; at the end, report the maximum. Is this an algorithm? Check it against all five requirements, and say what happens on an empty list.

📌 If you remember nothing else…

  • An algorithm is a sequence of unambiguous instructions that produces the required output for any legitimate input in finite time.
  • The five requirements are finiteness, definiteness, input, output and effectiveness.
  • A problem is a specification, an algorithm is a method, and a program is an implementation — and cost is decided at the algorithm level.
  • Euclid's algorithm for gcd rests on gcd(m, n) = gcd(n, m mod n), terminates because the second number strictly decreases, and takes about log(min(m, n)) passes.
  • Several algorithms can solve one problem with dramatically different costs; choosing among them is the point of analysis.

Quick check

Did it land?

1. An algorithm is defined by a set of requirements. Which pair is stated correctly?
2. Two procedures produce the same output for every input. Which conclusion is sound?

Spotted a mistake?