26.0 KB · commit a9602da
In Section 3.1 we discussed the concept of an algorithm. We introduced algorithms that solve a variety of problems, including searching for an element in a list and sorting a list. In Section 3.3 we will study the number of operations used by these algorithms. In particular, we will estimate the number of comparisons used by the linear and binary search algorithms to find an element in a sequence of n elements. We will also estimate the number of comparisons used by the
bubble sort and by the insertion sort to sort a list of n elements. The time required to solve a problem depends on more than only the number of operations it uses. The time also depends on the hardware and software used to run the program that implements the algorithm. However, when we change the hardware and software used to implement an algorithm, we can closely approximate the time required to solve a problem of size n by multiplying the previous time required by a constant factor. For instance, if we run a program on a computer that is twice as fast, it will take half as long to solve this problem.
One of the most important things to know when we design or use an algorithm is how it behaves as its input grows. With the help of this notation, we can determine whether it is practical to use a particular algorithm to solve a problem as the size of the input increases. Furthermore, with big-O notation we can compare the efficiency of two different algorithms for solving the same problem to determine which is more efficient, especially when the size of the input grows. For instance, if we have two algorithms for solving a problem, one using 100n operations and the other using n2 operations, big-O notation helps us see that the first algorithm is much more efficient than the second for large inputs.
The constants C and k in the definition of big-O notation are called witnesses to the relationship f(x) is O(g(x)). To establish that f(x) is O(g(x)) we need only find one pair of each of witnesses C and k. That is, to show that f(x) is O(g(x)), we need only find a pair of constants C and k, the witnesses, such that ∣f(x)∣≤C∣g(x)∣ whenever x>k.
A useful approach for finding a pair of witnesses is to first select a value for k for which the size of ∣f(x)∣ can be easily estimated when x>k. Then, a value for C can be established using this estimate. This approach is illustrated in Example 1.
Show that f(x)=x2+2x+1 is O(x2).
Solution: We observe that we can readily estimate the size of f(x) when x>1 because x<x2 and 1<x2 when x>1. It follows that 0≤x2+2x+1≤x2+2x2+x2=4x2 whenever x>1, as shown in Figure 1. Consequently, we can take C=4 and k=1 as witnesses to show that f(x) is O(x2). That is, f(x)=x2+2x+1<4x2 whenever x>1.
Alternatively, we can estimate f(x) by noting that when x>2, then 2x≤x2 and 1≤x2. Consequently, if x>2, we have 0≤x2+2x+1≤x2+x2+x2=3x2. It follows that C=3 and k=2 are also witnesses to the relation f(x) is O(x2).
![]() |
|---|
| Figure 1 — The Function x2+2x+1 is O(x2). |
Observe that in the relationship "f(x) is O(x2)," x2 can be replaced by any function with larger values than x2. For example, f(x) is O(x3), f(x) is O(x2+x+7), and so on.
It is also true that x2 is O(x2+2x+1), because x2<x2+2x+1 whenever x>1. This means that C=1 and k=1 are witnesses to the relationship x2 is O(x2+2x+1). ◀
Note that in Example 1 we have two functions, f(x)=x2+2x+1 and g(x)=x2, such that f(x) is O(g(x)) and g(x) is O(f(x))—the latter fact following from the inequality x2≤x2+2x+1, which holds for all nonnegative real numbers x. We say that two functions f(x) and g(x) that satisfy both of these big-O relationships are of the same order. We will return to this notion later in this section.
When f(x) is O(g(x)), and h(x) is a function that has larger absolute values than g(x) does for sufficiently large values of x, it follows that f(x) is O(h(x)). In other words, the function g(x) in the relationship f(x) is O(g(x)) can be replaced by a function with larger absolute values. To see this, note that if ∣f(x)∣≤C∣g(x)∣if x>k, and if ∣h(x)∣>∣g(x)∣ for all x>k, then ∣f(x)∣≤C∣h(x)∣if x>k. Hence, f(x) is O(h(x)).
When big-O notation is used, the function g in the relationship f(x) is O(g(x)) is chosen to be as small as possible (sometimes from a set of reference functions, such as functions of the form xn, where n is a positive integer).
![]() |
|---|
| Figure 2 — The Function f(x) is O(g(x)). |
In subsequent discussions, we will almost always deal with functions that take on only positive values. All references to absolute values can be dropped when working with big-O estimates for such functions. Figure 2 illustrates the relationship f(x) is O(g(x)).
Example 2 illustrates how big-O notation is used to estimate the growth of functions.
Show that 7x2 is O(x3).
Solution: Note that when x>7, we have 7x2<x3. (We can obtain this inequality by multiplying both sides of x>7 by x2.) Consequently, we can take C=1 and k=7 as witnesses to establish
the relationship 7x2 is O(x3). Alternatively, when x>1, we have 7x2<7x3, so that C=7 and k=1 are also witnesses to the relationship 7x2 is O(x3). ◀
Example 3 illustrates how to show that a big-O relationship does not hold.
Show that n2 is not O(n).
Solution: To show that n2 is not O(n), we must show that no pair of witnesses C and k exist such that n2≤Cn whenever n>k. We will use a proof by contradiction to show this.
Suppose that there are constants C and k for which n2≤Cn whenever n>k. Observe that when n>0 we can divide both sides of the inequality n2≤Cn by n to obtain the equivalent inequality n≤C. However, no matter what C and k are, the inequality n≤C cannot hold for all n with n>k. In particular, once we set a value of k, we see that when n is larger than the maximum of k and C, it is not true that n≤C even though n>k. This contradiction shows that n2 is not O(n). ◀
Example 2 shows that 7x2 is O(x3). Is it also true that x3 is O(7x2)?
Solution: To determine whether x3 is O(7x2), we need to determine whether witnesses C and k exist, so that x3≤C(7x2) whenever x>k. We will show that no such witnesses exist using a proof by contradiction.
If C and k are witnesses, the inequality x3≤C(7x2) holds for all x>k. Observe that the inequality x3≤C(7x2) is equivalent to the inequality x≤7C, which follows by dividing both sides by the positive quantity x2. However, no matter what C is, it is not the case that x≤7C for all x>k no matter what k is, because x can be made arbitrarily large. It follows that no witnesses C and k exist for this proposed big-O relationship. Hence, x3 is not O(7x2). ◀
Polynomials can often be used to estimate the growth of functions. Instead of analyzing the growth of polynomials each time they occur, we would like a result that can always be used to estimate the growth of a polynomial. Theorem 1 does this. It shows that the leading term of a polynomial dominates its growth by asserting that a polynomial of degree n or less is O(xn).
Proof: Using the triangle inequality (see Exercise 7 in Section 1.8), if x>1 we have
∣f(x)∣=∣anxn+an−1xn−1+⋯+a1x+a0∣≤∣an∣xn+∣an−1∣xn−1+⋯+∣a1∣x+∣a0∣=xn(∣an∣+∣an−1∣/x+⋯+∣a1∣/xn−1+∣a0∣/xn)≤xn(∣an∣+∣an−1∣+⋯+∣a1∣+∣a0∣).This shows that ∣f(x)∣≤Cxn,
where C=∣an∣+∣an−1∣+⋯+∣a0∣ whenever x>1. Hence, the witnesses C=∣an∣+∣an−1∣+⋯+∣a0∣ and k=1 show that f(x) is O(xn). ■
We now give some examples involving functions that have the set of positive integers as their domains.
How can big-O notation be used to estimate the sum of the first n positive integers?
Solution: Because each of the integers in the sum of the first n positive integers does not exceed n, it follows that 1+2+⋯+n≤n+n+⋯+n=n2. From this inequality it follows that 1+2+3+⋯+n is O(n2), taking C=1 and k=1 as witnesses. (In this example the domains of the functions in the big-O relationship are the set of positive integers.) ◀
In Example 6 big-O estimates will be developed for the factorial function and its logarithm. These estimates will be important in the analysis of the number of steps used in sorting procedures.
Give big-O estimates for the factorial function and the logarithm of the factorial function, where the factorial function f(n)=n! is defined by n!=1⋅2⋅3⋯n whenever n is a positive integer, and 0!=1. For example,
1!2!3!4!=1,=1⋅2=2,=1⋅2⋅3=6,=1⋅2⋅3⋅4=24.Note that the function n! grows rapidly. For instance, 20!=2,432,902,008,176,640,000.
Solution: A big-O estimate for n! can be obtained by noting that each term in the product does not exceed n. Hence,
n!=1⋅2⋅3⋯n≤n⋅n⋅n⋯n=nn.This inequality shows that n! is O(nn), taking C=1 and k=1 as witnesses. Taking logarithms of both sides of the inequality established for n!, we obtain logn!≤lognn=nlogn. This implies that logn! is O(nlogn), again taking C=1 and k=1 as witnesses. ◀
In Section 4.1, we will show that n<2n whenever n is a positive integer. Show that this inequality implies that n is O(2n) and that logn is O(n).
Solution: Using the inequality n<2n, we quickly can conclude that n is O(2n) by taking k=C=1 as witnesses. Note that by taking the base 2 logarithm of both sides of the inequality n<2n, we see that logn<n. It follows that logn is O(n). (Again we take C=k=1 as witnesses.) If we have logarithms to a base b, where b is different from 2, we still have logbn is O(n) because logbn=logblogn<logbn whenever n is a positive integer. We take C=1/logb and k=1 as witnesses. (We have used Theorem 3 in Appendix 2 to express logbn in terms of logn.) ◀
As mentioned before, big-O notation is used to estimate the number of operations needed to solve a problem using a specified algorithm. We will display a list of the functions commonly used in big-O estimates for the number of operations used by an algorithm. In this list, the functions are ordered so that each function is big-O of the next function. That is, 1,logn,n,nlogn,n2,2n,n!
Using calculus it can be shown that each function in the list is smaller than the succeeding function, in the sense that the ratio of this function and the next function tends to zero as n grows without bound. Figure 3 displays the graphs of these functions, using a scale for the vertical axis that doubles each unit.
![]() |
|---|
| Figure 3 — A Display of the Growth of Functions Commonly Used in Big-O Estimates. |
We now give some useful facts that help us determine whether big-O relationships hold between pairs of functions when each of the functions is a power of a logarithm, a power, or an exponential function of the form bn where b>1. Their proofs are left as Exercises 57–60 for readers skilled with calculus.
Theorem 1 shows that if f(n) is a polynomial of degree d, then f(n) is O(nd). Applying this theorem, we see that if d>c>1, then nc is O(nd). We leave it to the reader to show that the reverse of this relationship does not hold. Putting these facts together, we see that if d>c>1, then nc is O(nd), but nd is not O(nc).
In Example 7 we showed that logbn is O(n) whenever b>1. More generally, whenever b>1 and c and d are positive, we have (logbn)c is O(nd), but nd is not O((logbn)c). This tells us that every positive power of the logarithm of n to the base b, where b>1, is big-O of every positive power of n, but the reverse relationship never holds.
In Example 7, we also showed that n is O(2n). More generally, whenever d is positive and b>1, we have nd is O(bn), but bn is not O(nd). This tells us that every power of n is big-O of every exponential function of n with a base that is greater than one, but the reverse relationship never holds. Furthermore, we have when c>b>1, bn is O(cn) but cn is not O(bn). This tells us that if we have two exponential functions with different bases greater than one, one of these functions is big-O of the other if and only if its base is smaller or equal.
Many algorithms are made up of two or more separate subprocedures. The number of steps used by a computer to solve a problem with input of a specified size using such an algorithm is the sum of the number of steps used by these subprocedures. To give a big-O estimate for the number of steps needed, it is necessary to find big-O estimates for the number of steps used by each subprocedure and then combine these estimates.
Big-O estimates of combinations of functions can be provided if care is taken when different big-O estimates are combined. In particular, it is often necessary to estimate the growth of the sum and the product of two functions. What can be said if big-O estimates for each of two functions are known? To see what sort of estimates hold for the sum and the product of two functions, suppose that f1(x) is O(g1(x)) and f2(x) is O(g2(x)).
From the definition of big-O notation, there are constants C1,C2,k1, and k2 such that ∣f1(x)∣≤C1∣g1(x)∣when x>k1, and ∣f2(x)∣≤C2∣g2(x)∣when x>k2.
To estimate the sum of f1(x) and f2(x), note that
∣(f1+f2)(x)∣=∣f1(x)+f2(x)∣≤∣f1(x)∣+∣f2(x)∣using the triangle inequality ∣a+b∣≤∣a∣+∣b∣.
When x is greater than both k1 and k2, it follows from the inequalities for ∣f1(x)∣ and ∣f2(x)∣ that
∣f1(x)∣+∣f2(x)∣≤C1∣g1(x)∣+C2∣g2(x)∣≤C1∣g(x)∣+C2∣g(x)∣=(C1+C2)∣g(x)∣=C∣g(x)∣,where C=C1+C2 and g(x)=max(∣g1(x)∣,∣g2(x)∣). [Here max(a,b) denotes the maximum, or larger, of a and b.]
This inequality shows that ∣(f1+f2)(x)∣≤C∣g(x)∣ whenever x>k, where k=max(k1,k2). We state this useful result as Theorem 2.
We often have big-O estimates for f1 and f2 in terms of the same function g. In this situation, Theorem 2 can be used to show that (f1+f2)(x) is also O(g(x)), because max(g(x),g(x))=g(x). This result is stated in Corollary 1.
In a similar way big-O estimates can be derived for the product of the functions f1 and f2. When x is greater than max(k1,k2) it follows that
∣(f1f2)(x)∣=∣f1(x)∣∣f2(x)∣≤C1∣g1(x)∣C2∣g2(x)∣≤C1C2∣(g1g2)(x)∣≤C∣(g1g2)(x)∣,where C=C1C2. From this inequality, it follows that f1(x)f2(x) is O(g1(x)g2(x)), because there are constants C and k, namely, C=C1C2 and k=max(k1,k2), such that ∣(f1f2)(x)∣≤C∣g1(x)g2(x)∣ whenever x>k. This result is stated in Theorem 3.
The goal in using big-O notation to estimate functions is to choose a function g(x) as simple as possible, that grows relatively slowly so that f(x) is O(g(x)). Examples 8 and 9 illustrate how to use Theorems 2 and 3 to do this. The type of analysis given in these examples is often used in the analysis of the time used to solve problems using computer programs.
Give a big-O estimate for f(n)=3nlog(n!)+(n2+3)logn, where n is a positive integer.
Solution: First, the product 3nlog(n!) will be estimated. From Example 6 we know that log(n!) is O(nlogn). Using this estimate and the fact that 3n is O(n), Theorem 3 gives the estimate that 3nlog(n!) is O(n2logn).
Next, the product (n2+3)logn will be estimated. Because (n2+3)<2n2 when n>2, it follows that n2+3 is O(n2). Thus, from Theorem 3 it follows that (n2+3)logn is O(n2logn).
Using Theorem 2 to combine the two big-O estimates for the products shows that f(n)=3nlog(n!)+(n2+3)logn is O(n2logn). ◀
Give a big-O estimate for f(x)=(x+1)log(x2+1)+3x2.
Solution: First, a big-O estimate for (x+1)log(x2+1) will be found. Note that (x+1) is O(x). Furthermore, x2+1≤2x2 when x>1. Hence, log(x2+1)≤log(2x2)=log2+logx2=log2+2logx≤3logx, if x>2. This shows that log(x2+1) is O(logx).
From Theorem 3 it follows that (x+1)log(x2+1) is O(xlogx). Because 3x2 is O(x2), Theorem 2 tells us that f(x) is O(max(xlogx,x2)). Because xlogx≤x2, for x>1, it follows that f(x) is O(x2). ◀
Big-O notation is used extensively to describe the growth of functions, but it has limitations. In particular, when f(x) is O(g(x)), we have an upper bound, in terms of g(x), for the size of f(x) for large values of x. However, big-O notation does not provide a lower bound for the size of f(x) for large x. For this, we use big-Omega (big-Ω) notation. When we want to give both an upper and a lower bound on the size of a function f(x), relative to a reference function g(x), we use big-Theta (big-Θ) notation. Both big-Omega and big-Theta notation were introduced by Donald Knuth in the 1970s. His motivation for introducing these notations was the common misuse of big-O notation when both an upper and a lower bound on the size of a function are needed.
We now define big-Omega notation and illustrate its use. After doing so, we will do the same for big-Theta notation.
There is a strong connection between big-O and big-Omega notation. In particular, f(x) is Ω(g(x)) if and only if g(x) is O(f(x)). We leave the verification of this fact as a straightforward exercise for the reader.
The function f(x)=8x3+5x2+7 is Ω(g(x)), where g(x) is the function g(x)=x3. This is easy to see because f(x)=8x3+5x2+7≥8x3 for all positive real numbers x. This is equivalent to saying that g(x)=x3 is O(8x3+5x2+7), which can be established directly by turning the inequality around. ◀
Often, it is important to know the order of growth of a function in terms of some relatively simple reference function such as xn when n is a positive integer or cx, where c>1. Knowing the order of growth requires that we have both an upper bound and a lower bound for the size of the function. That is, given a function f(x), we want a reference function g(x) such that f(x) is O(g(x)) and f(x) is Ω(g(x)). Big-Theta notation, defined as follows, is used to express both of these relationships, providing both an upper and a lower bound on the size of a function.
When f(x) is Θ(g(x)), it is also the case that g(x) is Θ(f(x)). Also note that f(x) is Θ(g(x)) if and only if f(x) is O(g(x)) and g(x) is O(f(x)). Furthermore, note that f(x) is Θ(g(x)) if and only if there are real numbers C1 and C2 and a positive real number k such that C1∣g(x)∣≤∣f(x)∣≤C2∣g(x)∣ whenever x>k. The existence of the constants C1,C2, and k tells us that f(x) is Ω(g(x)) and that f(x) is O(g(x)), respectively.
Usually, when big-Theta notation is used, the function g(x) in Θ(g(x)) is a relatively simple reference function, such as xn,cx,logx, and so on, while f(x) can be relatively complicated.
We showed (in Example 5) that the sum of the first n positive integers is O(n2). Is this sum of order n2?
Solution: Let f(n)=1+2+3+⋯+n. Because we already know that f(n) is O(n2), to show that f(n) is of order n2 we need to find a positive constant C such that f(n)>Cn2 for sufficiently large integers n. To obtain a lower bound for this sum, we can ignore the first half of the terms. Summing only the terms greater than ⌈n/2⌉, we find that
1+2+⋯+n≥⌈n/2⌉+(⌈n/2⌉+1)+⋯+n≥⌈n/2⌉+⌈n/2⌉+⋯+⌈n/2⌉=(n−⌈n/2⌉+1)⌈n/2⌉≥(n/2)(n/2)=n2/4.This shows that f(n) is Ω(n2). We conclude that f(n) is of order n2, or in symbols, f(n) is Θ(n2). ◀
Show that 3x2+8xlogx is Θ(x2).
Solution: Because 0≤8xlogx≤8x2, it follows that 3x2+8xlogx≤11x2 for x>1. Consequently, 3x2+8xlogx is O(x2). Clearly, x2 is O(3x2+8xlogx). Consequently, 3x2+8xlogx is Θ(x2). ◀
One useful fact is that the leading term of a polynomial determines its order. For example, if f(x)=3x5+x4+17x3+2, then f(x) is of order x5. This is stated in Theorem 4, whose proof is left as Exercise 50.
The polynomials 3x8+10x7+221x2+1444, x19−18x4−10,112, and −x99+40,001x98+100,003x are of orders x8,x19, and x99, respectively. ◀
Unfortunately, as Knuth observed, big-O notation is often used by careless writers and speakers as if it had the same meaning as big-Theta notation. Keep this in mind when you see big-O notation used. The recent trend has been to use big-Theta notation whenever both upper and lower bounds on the size of a function are needed.