AQA 7517, section 4.4.4.2
Big-O notation (4.4.4.3) describes how fast a function grows. Before using it, you need the maths of functions and the main families of growth.
Functions
A function is a mapping from one set of values, the domain, to another set of values drawn from the co-domain. Each input in the domain is mapped to exactly one output.
We write to say that maps values in to values in . For example, with maps each natural number to double itself.
| Input | 0 | 1 | 2 | 3 |
|---|---|---|---|---|
| Output | 0 | 2 | 4 | 6 |
In algorithm analysis, the input is the problem size and the output is the amount of work (such as the number of steps).
def f(n):
return 2 * n
print(f(3)) # 6
Types of function
Linear
A linear function has the form (or ). Doubling doubles . Example: . Its graph is a straight line.
Polynomial
A polynomial function has raised to fixed whole-number powers. Example: (a quadratic). The highest power is the degree. Doubling in makes four times bigger.
Exponential
An exponential function has the variable in the exponent. Example: . Each time increases by 1, doubles. These grow extremely fast.
Logarithmic
A logarithmic function is the reverse of an exponential one. For example, is the power you must raise 10 to, to get . In computing, is common: it is the number of times you can halve before reaching 1. Logarithms grow very slowly.
| Type | Example | Growth |
|---|---|---|
| Constant | No growth at all | |
| Logarithmic | Very slow | |
| Linear | Steady | |
| Polynomial | Faster, and faster | |
| Exponential | Explosive |
Comparing growth
Look at how each grows as increases:
| 1 | 0 | 1 | 1 | 2 | 1 |
| 5 | 2.3 | 5 | 25 | 32 | 120 |
| 10 | 3.3 | 10 | 100 | 1 024 | 3 628 800 |
| 20 | 4.3 | 20 | 400 | 1 048 576 | about |
Going from to , goes up by 1, goes up by a factor of 4, but goes up by a factor of about 1 000. Eventually, any exponential function overtakes any polynomial, no matter how large the polynomial's coefficients.
Permutations and factorials
A permutation is an ordering of a set of objects. The letters of the word CAT can be arranged in these orders:
CAT, CTA, ACT, ATC, TCA, TAC
That's 6 permutations. For distinct objects, the number of permutations is factorial, written :
So , and . In other words, is the product of all the positive whole numbers up to .
import math
print(math.factorial(3)) # 6
print(math.factorial(10)) # 3628800
Factorial growth is even faster than exponential. If an algorithm has to try every possible ordering of items (such as every route through cities), its work grows like , which quickly becomes impossible, as the table shows.
Worked example
Task: a delivery van must visit 8 houses and a brute-force program tries every possible order. How many orders is that? How does it compare with 10 houses?
- 8 houses: orders.
- 10 houses: orders.
Just 2 more houses makes the work 90 times larger (). This explosive growth is why the problem becomes impractical for large .
Exam tip: be able to identify a function's type from its formula: is linear, is polynomial, is exponential (the variable is in the power), and is logarithmic. Remember the number of permutations of distinct objects is .
Check yourself
What is a function in this context?
A mapping from one set of values (the domain) to another set (the co-domain), where each input is mapped to one output.
Classify: , , ,
Linear, polynomial, exponential and logarithmic respectively.
How many different orderings are there of the letters in the word "BAKE" (all different)?
.
Which grows faster as gets large: or ?
(exponential growth eventually overtakes any polynomial).