<< Chapter < Page | Chapter >> Page > |
Note that in the while loop two comparisons and one addition are performed. Thus one could use 3n as an estimate just as well. Also note that the very first line and the last two lines are not counted in. The reasons for those are firstly that differences in implementation details such as languages, commands, compilers and machines make differences in constant factors meaningless, and secondly that for large values of n, the highest degree term in n dominates the estimate. Since we are mostly interested in the behavior of algorithms for large values of n , lower terms can be ignored compared with the highest term. The concept that is used to address these issues is something called big-oh, and that is what we are going to study here.
The following example gives the idea of one function growing more rapidly than another. We will use this example to introduce the concept the big-Oh.
Example: f(n) = 100 n2, g(n) = n4, the following table and Figure 2 show that g(n) grows faster than f(n) when n>10. We say f is big-Oh of g.
n | f(n) | g(n) |
10 | 10,000 | 10,000 |
50 | 250,000 | 6,250,000 |
100 | 1,000,000 | 100,000,000 |
150 | 2,250,000 | 506,250,000 |
Definition (big-oh): Let f and g be functions from the set of integers (or the set of real numbers) to the set of real numbers. Then f(x) is said to be O( g(x) ) , which is read as f(x) is big-oh of g(x) , if and only if there are constants C and n0 such that
| f(x) | ≤ C | g(x) |
whenever x>n0 .
Note that big-oh is a binary relation on a set of functions (What kinds of properties does it have ? reflexive ? symmetric ? transitive ?).
The relationship between f and g can be illustrated as follows when f is big-oh of g.
For example, 5 x + 10 is big-oh of x2, because 5 x + 10<5 x2 + 10 x2 = 15 x2 for x>1 .
Hence for C = 15 and n0 = 1 , | 5x + 10 | ≤ C | x2 | . Similarly it can be seen that 3 x2 + 2 x + 4<9 x2 for x>1 . Hence 3 x2 + 2 x + 4 is O( x2 ) . In general, we have the following theorem:
Theorem 1: an xn + ... + a1 x + a0 is O( xn ) for any real numbers an , ..., a0 and any nonnegative number n .
Note: Let f(x) = 3 x2 + 2 x + 4, g(x) = x2, from the above illustration, we have that f(x) is O(g(x)). Also, since x2<3 x2 + 2 x + 4, we can also get g(x) is O(f(x)). In this case, we say these two functions are of the same order.
Big-oh has some useful properties. Some of them are listed as theorems here. Let use start with the definition of max function.
Definition (max function): Let f1(x) and f2(x) be functions from a set A to a set of real numbers B. Then max( f1(x) , f2(x) ) is the function from A to B that takes as its value at each point x the larger of f1(x) and f2(x).
Theorem 2: If f1(x) is O( g1(x) ) , and f2(x) is O( g2(x) ) , then (f1 + f2)( x ) is O( max( g1(x) , g2(x) ) ) .
From this theorem it follows that if f1(x) and f2(x) are O( g(x) ) , then (f1 + f2)( x ) is O( g(x) ) , and
(f1 + f2)( x ) is O( max( f1(x) , f2(x) ) ) .
Theorem 3: If f1(x) is O( g1(x) ) , and f2(x) is O( g2(x) ) , then (f1 * f2)( x ) is O( g1(x) * g2(x) ) .
Notification Switch
Would you like to follow the 'Discrete structures' conversation and receive update notifications?