Notes on Problems for Quiz 7 (11/14/97) from Epp:

9.1: 17, 19, 26, 27

9.2: 1-4, 9-12, 17, 19-22, 23, 26, 31

9.3: 4-5, 6-14, 20-26

9.4: 32-36, 39, 41, 42

9.5: 1-6, 20-23


Problem 9.1(17):

(1) Here is the graph.

 


Problem 9.1(19)

(1) Suppose that a < b. Consider (f+g)(a) and (f+g)(b).


Problem 9.1(26)

(1) Note that f(x)/2g(x) = (1/2) + (5/x) + (3/x^2). We need to have (5/x) + (1.5/x^2) < 1/2. But this clearly happens for some x=c, since (5/x) + (1.5/x^2) goes to zero as x goes to infinity. The exact value of c can be found from a quadratic equation. See page 767 for the graph. The quadratic is x^2 - 10x -3.


Problem 9.1(27):

(1) g(x) - f(x) = x^2 - 126x - 35. Solve the quadratic to find when g(x) - f(x) > 0, x>0.


Problem 9.2(1):

(1) The solution is given on page 767. But, be careful. Take f(x) = 1, g(x) = 0 for 0<=x<=2, and f(x) = g(x) = 0 for x>2. Then f is O(g) and g is O(f). It is always true under these conditions that f is O(g), it is sometimes true that g is O(f) as well.


Problem 9.2(2)

(1) All quantities are real numbers. The negation of "f is O(g)" is

(you should try to draw a picture of this negation).


Problem 9.2(3)

(1) This expression is of the form |f(x)| < 20|g(x)| for all x>1. We can take M=20 and x_0 = 1 in the general definition to get f is O(g). Note for these functions, we have


Problem 9.2(4)

(1) For f(x) the rational function on the left and g(x)=x, this inequality says that f is O(g). But, we don't need to work directly with the definition here. We will show in class that whenever the limit as x tends to infinity of f(x)/g(x) exists (is not infinite) then "f is O(g)." In this case, this limit is 4 so "f is O(g)" follows from that fact alone. This also means that the limit of g(x)/f(x) is 1/4 and hence that "g is O(f)." You should review how to take these limits (calculus stuff).


Problem 9.2(9)

(1) The solution is on page 768. Another way to write this is to let f(x)=7x^2+12x and g(x)=x^2. |f(x)/g(x)| < 7 + 12/x for x > 0. Thus, for x > 1, |f(x)/g(x)| < 19 (the sum of the coefficients). This same trick works for comparing any polynomial f(x) of degree n to g(x)=x^n. You can, in such a case, take M to be the sum of the absolute values of the coefficients and x_0 = 1. The result is "f is O(g)." You can use this trick on 9.2(10, 11, 12) as well.


Problem 9.2(17)

(1) Parts (a) and (b) are proved on page 769. For (c), if f is O(g), then there is an M>0 and real number r such that |f(x)| < M|g(x)| for all x>r. This means that |cf(x)|< M|c||g(x)| for x>r. Thus cf(x) is O(g(x)), by taking x_0 of the definition to be r and M of the definition to be |c|M. Part (d) follows from the theorem on polynomial orders or from the 9.2(9) trick. It is not very natural to use (a) and (c), but to do that you note that x^5 is O(x^5), x^2 is O(x^5), and the constant function 1 is clearly O(x^5). By (c), 12x^5 is O(x^5), -34x^2 is O(x^5), and 7x1 is O(x^5). By part (a), the sum of these monomials, 12x^5 - 34x^2 + 7 is O(x^5).


Problem 9.2(19, 20, 21, 22) All follow immediately from the theorem on polynomial orders 9.2.1.


Problem 9.2(23)

(1) This sum is n(n+1)(2n+1)/6 which is a polynomial of degree 3. Apply the theorem on polymomial orders.


Problem 9.2(24)

(1) This sum is [n(n+1)/2}^2 which is a polynomial of degree 4. Apply the theorem on polymomial orders.

(2) What about the sum 1^k + 2^k + . . . + n^k? Is this O(n^{k+1})? Why?


Problem 9.2(25)

(1) This sum is n(n+1)(2n+1)/6 + n(n+1)/2 which is a polynomial of degree 3. Apply the theorem on polymomial orders.


Problem 9.2(26)

(1) This sum is n(n+1) which is a polynomial of degree 2. Apply the theorem on polymomial orders.


Problem 9.2(31)

(1) Start the induction proof as shown on page 770, up to and including the phrase "suppose that." Replace the rest with the following:

 


Problem 9.3(4)

(1) Answer is on page 771.


Problem 9.3(4)

(1) For part (a), algorithm A is O(n^2) and algorithm B is O(n^(5/3)). For (b) you need to solve 4n^2 = 200n^(5/3) and for (c) solve 4n^2 = 20000n^(3/2). The reasoning is the same as 9.3(4).


Problem 9.3(6)

(1) Solution page 771.


Problem 9.3(7)

(1) The loop is iterated n-2+1 = n-1 times. There are n-1 comparisons, n-1 assignments (not requested in the count by Epp). The order is O(n).


Problem 9.3(8)

(1) Solution page 771.


Problem 9.3(9)

(1) There are n iterations of the outer loop, 2n of the inner, for 2n^2 total. Three operations are preformed in the inner loop, so the total is 6n^2. The order is O(n^2).


Problem 9.3(10)

(1) Solution page 771.


Problem 9.3(11)

(1) The inner loop has n-i comparisons and three assignments for each i. We aren't counting assignments, so the total number of comparisons is (n-1)+(n-2)+...+1. This sum is (n-1)n/2, so by the theorem on polynomial orders this is O(n^2).

(2) What if we were counting assignments as well as comparisons?


Problem 9.3(12)

(1) Solution page 771.


Problem 9.3(13)

(1) The inner loop is executed n-1 times, the next loop 2n times, and the outer loop n times. There are two mulitplications on the inner loop. The total number of multiplications is thus (n-1)2n^2. By the theorem on polynomial orders, the number of multiplications is O(n^3).

(2) How many multiplications in the following function of n? What is the order?

 

 


Problem 9.3(14)

(1) For each value of j, the inner loop is executed j times for a total of 1+2+...+i times for each i. Thus, for each i, the inner loop is executed a total of i(i+1)/2 times. The values of i range from 1 to n, giving a total of 1(1+1)/2 + 2(2+1)/2 + ... + n(n+1)/2 executions of the inner loop. This sum equals

(1/2)[ (1^2 + 2^2 + ... + n^2) + (1 + 2 + ... +n)] =(1/2) [n(n+1)(2n+1)+n(n+1)].

By the theorem on polynomial orders, this is O(n^3).


Problem 9.3(20)

(1) Solution page 772.


Problem 9.3(21)

(1)

 6  4 5 8 1
 4 6 5 8 1
 1 6 5 8 4
1 5  6  8  4
 1 4 6 8 5
1 4 5 8 6
1 4 5 6 8


Problem 9.3(22)

(1) Solution page 772.


Problem 9.3(23)

(1) Six.


Problem 9.3(24)

(1) 3 + 2 + 1.


Problem 9.3(25)

(1) For part (a), n-1. For part (b) n-3 +1 = n-2. For part (c) n - (i+1) +1 = n-i. For part (d), n-1 + n-2 + ... + 1 = (n-1)n/2, so the order is O(n^2).


Problem 9.3(26)

(1) 5 4 3 2 1


Problem 9.4(32)

(1) See page 776.


Problem 9.4(33)

(1) f(n) = n(1 + (1/2) + (1/2)^2 + ... + (1/2)^n) = 2n(1-(1/2)^{n+1}) <2n, n>0. Thus f is O(n). In fact, f is asymptotic to 2n.


Problem 9.4(34)

(1) f(n) =( 2n/3)(1 + (1/3) + (1/3)^2 + ... + (1/3)^n) = n(1 - (1/3)^{n+1}) < n. So f is O(n). In fact, f is asymptotic to n.


Problem 9.4(35)

(1) Let f(n) = kn + knlog(n) and g(n) = nlog(n). Then the limit as n approaches infinity of f(n)/g(n) is k (the limit as n approaches infinity of n/(nlog(n)) is zero). Since k>0 by assumption, we have both f is O(g) and g is O(f).


Problem 9.4(36)

(1) Let f(n) = n^2 + 2^n and let g(n) = 2^n. The limit as n approaches infinity of f(n)/g(n) is 1. Thus f is O(g) and g is O(f) both hold.


Problem 9.4(39)

(1) Example 9.4.7 on page 515 shows that if f(n) = 1 + 1/2 + 1/3 + ... + 1/n and g(n) = ln(n) then f is asymptotic to g. That is, the limit as n goes to infinity of f(n)/g(n) is 1. In particular, Figure 9.4.6 shows that

ln(n+1) < 1 + 1/2 + 1/3 + ... + 1/n < 1 + ln(n).

Dividing through by ln(n) gives

ln(n+1)/ln(n) < f(n)/ln(n) < (1+ln(n))/ln(n).

Letting n go to infinity, we see that the limit of ln(n+1)/ln(n) is 1 (L'Hospital's Rule) and the limit of (1+ln(n))/ln(n) is 1. Thus the limit of f(n)/ln(n) is 1. So, 1 + 1/2 + 1/3 + ... + 1/n is asymptotic to ln(n). Thus, 1 + 1/2 + 1/3 + ... + 1/n is O(ln(n)). Since ln(n)/log(n) is constant and nonzero for any log base b>0, 1 + (1/2) + (1/3) + ... + 1/n is O(log(n)) for any log base b>0.


Problem 9.4(41)

(1) From the hint on page 776, we have n!/n^n = 1(1-(n-1)/n)(1-(n-2)/n) ... (1-1/n) < 1 for all n>1. Directly from the definition of big-oh notation, we have n! is O(n^n). This shows part (a). In fact, by Stirling's formula, n! is asymptotic to (2nPi)^{1/2}(n/e)^n . So, n! is O(n^(1/2)(n/e)^n). Is this a stronger or weaker statement than "n! is O(n^n)." Explain. For part (b), we note that we showed in part (a) that n! < n^n for n>1. Taking logs of both sides and using the fact that log base b>1 is increasing gives log(n!) < nlog(n) for n>1 and for log to any base b>1 (including b=2).


Problem 9.4(42)

(1) Note that 2^n/n! = (2/n)(2/(n-1))(2/(n-2)) ... (2/2)(2/1) < 2 if n>1. Thus, directly from the definition of big-oh notation, 2^n is O(n!). But, n! is not O(2^n). You can use Stirling's formula (problem 9.4(41)) to see that the limit as n goes to infinity of 2^n/n! is zero.


Problem 9.5(1,3,5) are solved on page778


Problem 9.5(4)

(1) Here is the graph of f(x)/g(x) where f(x)=x^2/10 and g(x) = xlg(x) (lg(x) is a common notation for log base 2).

 


Problem 9.5(6)

(1) For (a), bot=1, top=10; index=0, mid=5, bot=1, top=5; index=0, mid=3, bot=1, top=2; index=0, mid=1, bot=1, top=0 (end of while loop, index=0, Amanda is not in the array). You should do the same for (b), Roy, who is in the array to see how the algorithm terminates in this case.