Big-O Notation
Big-O Notation
Big-O
This chapter covers asymptotic analysis of function growth and big-O notation.
13.1
161
13.2
So, suppose that you model the running time of a program as a function
F (n), where n is some measure of the size of the input problem. E.g. n
might be the number of entries in a database application. For a numerical
program, n might be the magnitude or the number of digits in an input
number. Then, to compare the running times of two programs, we need to
compare the growth rates of the two running time functions.
So, suppose we have two functions f and g, whose inputs and outputs
are real numbers. Which one has bigger outputs?
Suppose that f (x) = x and g(x) = x2 . For small positive inputs, x2 is
smaller. For the input 1, they have the same value, and then g gets bigger
and rapidly diverges to become much larger than f . Wed like to say that g
is bigger, because it has bigger outputs for large inputs.
162
13.3
Primitive functions
Lets look at some basic functions and try to put them into growth order.
Any constant function grows more slowly than a linear function (i.e. because a constant function doesnt grow!). A linear polynomial grows more
slowly than a quadratic. For large numbers, a third-order polynomial grows
faster than a quadratic.
Earlier in the term (as an example of an induction proof), we showed that
2 n! for every integer n 4. Informally, this is true because 2n and n!
are each the product of n terms. For 2n , they are all 2. For n! they are the
first n integers, and all but the first two of these are bigger than 2. Although
we only proved this inequality for integer inputs, youre probably prepared
to believe that it also holds for all real inputs 4.
n
In a similar way, you can use induction to show that n2 < 2n for any
integer n 4. And, in general, for any exponent k, you can show that nk < 2n
for any n above some suitable lower bound. And, again, the intermediate
real input values follow the same pattern. Youre probably familiar with how
163
13.4
164
Lets write out the formal definition. Suppose that f and g are functions
whose domain and co-domain are subsets of the real numbers. Then f (x) is
O(g(x)) (read big-O of g) if and only if
There are positive real numbers c and k such that 0 f (x)
cg(x) for every x k.
The constant c in the equation models the fact that we dont care about
multiplicative constants in comparing functions. The restriction that the
equation only holds for x k models the fact that we dont care about the
behavior of the functions on small input values.
So, for example, 3x2 is O(2x ). 3x2 is also O(x2 ). But 3x2 is not O(x). So
the big-O relationship includes the possibility that the functions grow at the
same rate.
When g(x) is O(f (x)) and f (x) is O(g(x)), then f (x) and g(x) must grow
at the same rate. In this case, we say that f (x) is (g(x)) (and also g(x) is
(f (x))).
Big-O is a partial order on the set of all functions from the reals to
the reals. The relationship is an equivalence relation on this same set of
functions. So, for example, under the relation, the equivalence class [x2 ]
contains functions such as x2 , 57x2 301, 2x2 + x + 2, and so forth.
13.5
165
To satisfy our formal definition, we also need to make sure that both
functions produce positive values for all inputs k. If this isnt already the
case, increase k.
13.6
In a formal big-O proof, you first choose values for k and c, then show that
0 f (x) cg(x) for every x k. So the example from the previous
section would look like:
Claim 51 3x2 + 7x + 2 is O(x2 ).
Proof: Consider c = 4 and k = 100. Then for any x k, x2
100x 7x + 2. Since x is positive, we also have 0 3x2 + 7x + 2.
Therefore, for any x k, 0 3x2 +7x+2 3x2 +x2 = 4x2 = cx2 .
So 3x2 + 7x + 2 is O(x2 ).
Notice that the steps of this proof are in the opposite order from the work
we used to find values for c and k. This is standard for big-O proofs. Count
on writing them in two drafts (e.g. the first on scratch paper).
Heres another example of a big-O proof:
166
13.7
Sample disproof
Suppose that we want to show that a big-O relationship does not hold. Were
trying to prove that suitable values of c and k cannot exist. Like many nonexistence claims, this is best attacked using proof by contradiction.
For example:
Claim 53 x3 is not O(7x2 ).
Proof by contradiction. Suppose x3 were O(7x2). Then there are
c and k such that 0 x3 c7x2 for every x k. But x3 c7x2
implies that x 7c. But this fails for values of x that are greater
than 7c. So we have a contradiction.
13.8
Variation in notation
167
Outside theory classes, computer scientists often say that f (x) is O(g(x))
when they actually mean the (stronger) statement that f (x) is (g(x)). Or
this drives theoreticians nutsthey will say that g(x) is a tight big-O
bound on f (x). In this class, well stick to the proper theory notation, so
that you can learn how to use it. That is, use when you mean to say that
two functions grow at the same rate or when you mean to give a tight bound.
Very, very annoyingly, for historical reasons, the statement f (x) is O(g(x))
is often written as f (x) = O(g(x)). This looks like a sort of equality, but it
isnt. It is actually expressing an inequality. This is badly-designed notation
but, sadly, common.