Of course, as well as being accurate, a good program will be fast. This page sets out the mathematical notation we use to describe how fast a program is. This notation is called "Big O" notation and aims to give an upper bound of the worst case time. These details will be filled in later. The important thing here is to explain the notation which is a definition similar to the idea of the limit of a sequence that you learnt in first year.
Warning: some mathematicians and/or web pages define this notation in an incorrect way that is incompatible with that here.
We are going to try to compare the behaviour of a function against a function . We want to say that " grows no faster than up to a constant". The official definition is as follows.
Definition.
Let . We write " as " to mean there is and such that for all we have .
In words, " as " means that eventually (i.e. from some point onwards) we have that up to a multiplicative constant .
This is the official definition. There are similar definitions of related ideas, but we do not need them. Note that is not a function. The strange notation is not saying that is a function of or anything. It's just a notation, nothing else.
For this module, we need only look at special cases of this. The first thing to say is that we will only need to look at the case of functions from to the nonnegative reals, . The fact that our functions are defined on the naturals rather than on all reals makes no difference to the above definition: the definition works in exactly the same way for just the same reasons with natural number inputs. The fact that our functions map to nonnegative reals means that the absolute value signs can be omitted.
Secondly, we are only interested in the case when the function is a nondecreasing function, i.e. for . This simplifies quite a few things to do with the definition.
Since inputs to functions are natural numbers rather than reals, we will always use the letter for them instead. Since we will never be interested in any other limit except that when we omit the phrase "as ". Then the definition becomes,
Definition.
Let . We write " is " to mean there is and such that for all we have .
Example.
Let and suppose the function is the constant function with value zero. Then holds if and only if the function is eventually constant with value zero.
Example.
Let and suppose the function is nondecreasing and is not the constant function with value zero. Then holds if and only if there is such that for all , .
Indeed, for the most tricky part of this argument, if then there are such that for all . So for all and by replacing with we get the result we need.
Some programs run in a fixed amount of time for each input. Most of this page concerns programs whose running time varies with the input.
It can be very difficult indeed to analyse a program and give a formula for the exact amount of time it uses. We do not try to do this here. Instead we simplify the process considerably. We decide that, as a useful simplification each program line doing one step in the program takes one unit of time. (In practice this is not true, but usually each program line takes some constant amount of time, where the constant depends on how complicated the expression is, and we regard that constant as being essentially 1, at least up to a multiplicative factor.) The question is how to assign time to loops.
In almost all cases where run-time depends on the input there is some "obvious" parameter that it depends on. This parameter is always called , and is almost always the "length" of the input. (How one interprets "length" may differ in different situations. It is usually the number of entries in a matrix or array, or the number of vertices in a graph. For multi-precision arithmetic where the number of digits can grow to any size that might fit in memory, might be the number of digits in the input. In any particular case you will be told exactly what is.)
In cases where several different inputs can have the same "length" parameter we always take the run-time to be the worst case time taken over all these possible inputs.
We aim to describe the run-time of the program using a simple nondecreasing function . We can then describe the run-time as being . Most of the time very simple functions will suffice.
The following functions are commonly seen in such analyses: , , , , and other powers; , , , and so on, also , , and other exponential terms like these; , , and other terms like these involving log; also occasionally , and other similar terms involving log log.
This list just given contains almost everything seen in practical situations and will certainly suffice for anything you encounter in this module.
Example.
If a program takes no more than 2018 steps whetever its input is, then its run-time is .
In this subject it rarely matters what base logarithms are taken to. (See an exercise later on on this page for why.) Because of this it is usually convenient to take logarithms to base 2. The logarithm to base 2 of a positive integer is approximately the number of binary digits required to write that number down.
The following exercises should help you understand the concepts.
Exercise.
Prove that if a function is then is bounded.
Exercise.
Prove that the function is .
Exercise.
Prove that if the function is where is to base then it is for logarithms to any base greater than 1.