Vous êtes sur la page 1sur 6

SOLUTION SET 3DUE 2/13/2008

Please report any errors in this document to Ian Sammis (isammis@math.berkeley.edu). Problem 1 (#3.1.4). Describe an algorithm that takes as input a list of n integers and produces as output the largest dierence obtained by subtracting an integer in the list from the one following it. Solution. We need to compute the n 1 dierences, and keep track of the biggest so far. procedure maxDi(n Z, a1 . . . an Z) begin if n < 2 then throw error m := a2 a1 for i := 2 to n 1 do if m < an+1 an then m := an+1 an end for return m end procedure

Problem 2 (#3.1.44). Describe an algorithm based on the binary search for determining the correct position in which to insert an element into an already sorted list. Solution. This is almost like a binary search, except that were looking for a position n to insert the new element. Well return the position of the rst element bigger than the one were inserting. Let F be whatever set the list elements are drawn from. Let b be the new element that were trying to add. procedure insertPos(n Z, b F, a1 . . . an F) begin if a1 > b return 1 if an < b return n+1 l := 1 h := n while h l > 1 do m := (n + 1)/2 if am < b then l := m else h := m end while return h end procedure

Problem 3 (#3.1.48). Compare the number of comparisons used by the insertion sort and the binary insertion sort to sort the list 7, 4, 3, 8, 1, 5, 4, 2.
1

SOLUTION SET 3DUE 2/13/2008

Solution. Lets run the insertion sort rst: First step: 4 compared with 7, inserted before. 1 comparison. List: 4,7,3,8,1,5,4,2 Second Step: 3 compared with 4, inserted before. 1 comparison (2 total so far). List: 3,4,7,8,1,5,4,2 Third Step: 8 compared with 3. 8 compared with 4. 8 compared with 7, inserted after. 3 comparisons (5 total so far).List: 3,4,7,8,1,5,4,2 Fourth Step: 1 compared with 3, inserted before. 1 comparison (6 total so far). List: 1,3,4,7,8,5,4,2 Fifth Step: 5 compared with 1. 5 compared with 3. 5 compared with 4. 5 compared with 7, inserted before 4 comparisons (10 total so far). List: 1,3,4,5,7,8,4,2 Sixth Step: 4 compared with 1. 4 compared with 3 4 compared with 4, inserted before. 3 comparisons (13 total so far). List 1,3,4,4,5,7,8,2 Seventh Step:2 compared with 1. 2 compared with 3, inserted before. 2 comparisons (15 total so far). List sorted. So, an insertion sort run takes 15 comparisons. Now lets look at a binary search for comparison. First step: 4 compared with 7, inserted before. 1 comparison. List: 4,7,3,8,1,5,4,2 Second Step: 3 compared with 4, inserted before. 1 comparison (2 total so far). List: 3,4,7,8,1,5,4,2 Third Step: 8 compared with 4. 8 compared with 7, inserted after 2 comparisons (4 total so far). List: 3,4,7,8,1,5,4,2 Fourth Step: 1 compared with 4. 1 compared with 3, inserted before. 2 comparisons (6 total so far). List: 1,3,4,7,8,5,4,2 Fifth Step: 5 compared with 4. 5 compared with 7, inserted before. 2 comparisons (8 total so far). List: 1,3,4,5,7,8,4,2 Sixth Step: 4 compared with 4, inserted before. 1 comparison (9 total so far). List: 1,3,4,4,5,7,8,2 Seventh Step:2 compared with 4 2 compared with 3. 2 compared with 1, inserted after. 3 comparisons (12 total so far). List sorted. So the binary insertion sort required only 12 comparisons.

SOLUTION SET 3DUE 2/13/2008

Problem 4 (#3.1.60). Show that the problem of determining whether a program with a given input ever prints the digit 1 is unsolvable. Proof. Suppose we had a program O(P, I) that generates the string YES if program P ever prints a 1, and NO otherwise. Now, generate K(P ) such that K(P ) does not print a 1 if O(P, P ) prints YES, and prints a 1 if O(P, P ) prints NO. Now, consider K(K). We have the same problem as for the halting problem: If K prints a 1, then it doesnt print a 1, and if K doesnt print a 1, then it does. Contradiction, so O cant exist, and the problem of determining whether a program ever prints the digit 1 is unsolvable. Problem 5 (#3.2.8). Find the least integer n such that f (x) is O(xn ) for each of these functions: a) f (x) = 2x2 + x3 log x. b) f (x) = 3x5 + (log x)4 . c) f (x) = (x4 + x2 + 1)/(x4 + 1) d) f (x) = (x4 + 5 log x)/(x4 + 1) Solution. In each of these problems, well use the techniques developed in the chapter. Since the course itself has calculus as a prerequisite, we could also in principle use techniques (like LHopitals rule) that we developed in Math 1B to determine the behavior of functions as we increase a parameter without bound. a) We know log x is O(x), so x3 log x is O(x4 ). Since it isnt O(x3 ) (the logarithm will eventually surpass any constant) thats the least integer that works for that term. Since 2x2 is O(x2 ), f (x) is O(x4 ). b) Since log x is O(x), (log x)4 is O(x4 ). Since 3x5 is O(x5 ), the sum is also O(x5 ), and so f (x) is O(x5 ). c) f (x) < 1 + x2 + x4 , so f (x) is O(1) d) Similarly, this is O(1). Problem 6 (#3.2.18). Let k be a positive integer. Show that 1k + + nk is O(nk+1 ). Proof. For any given k, 1k + + nk n(nk ) = nk+1 . For n > 1 the inequality is strict, so the sum is O(nk+1 ). Problem 7 (#3.2.34). Show that for all real numbers a and b with a > 1 and b > 1, if f (x) is O(logb x), then f (x) is O(loga x). Proof. Suppose f (x) is O(logb x). Then there exists a k and a C such that f (x) < log x C logb x for x > k. By the usual change of base formula, f (x) < C loga b = a C C loga x. Thus (using log x as our new constant), f (x) is O(loga x). log b
a a

Problem 8 (#3.2.60). Let Hn be the nth harmonic number 1 1 1 Hn = 1 + + + + 2 3 n Show that Hn is O(log n).
1 Proof. Consider the function f (x) = x . Because it is strictly decreasing for x > n 0, the right-hand Riemann sum i=2 lies entirely within the area measured by n 1 f (x)dx, so Hn 1 < ln n. Thus, Hn 1 is log n, and so Hn is O(log n) as well.

SOLUTION SET 3DUE 2/13/2008

Problem 9 (#3.2.62). Determine whether log n! is (n log n). Justify your answer. Proof. Yes, log n! is (n log n). Since log n! = log 1 + log 2 + + log n < log n + + log n = n log n, log n! is O(n log n). Now, lets consider the other bound. We wish to show that for some k and C, for n > k log n! Cn log n Lets see if we cant nd a C that will work. Suppose n were even. Then we need a C such that log n! Cn log n 1 log n! C n log n 1 [logn 1 + + logn n] C. n Well, n 1 1 logn + + logn n [logn 1 + + logn n] n n 2 1 n n > logn n 2 2 1 = [logn n logn 2] 2 log 2 1 1 1 > = 2 log n 4 for n > 4. The odd case is analogous. Thus log n! is (n log n). This is a handy result for analyzing some algorithms (thus the pointy nger next to the number). Problem 10 (#3.3.4). Determine the number of multiplications used to nd x2 k starting with x and successively squaring. Is this a more ecient way to nd x2 than multiplying x by itself the appropriate number of times? Solution. Computing x2 by successively squaring takes k multiplications. Doing it directly takes 2k 1 multiplications, so this is much more ecient. Problem 11 (#3.3.8). There is a more ecient algorithm (in terms of the number of multiplications and additions used) for evaluating polynomials than the conventional algorithm. It is called Horners method. This pseudocode shows how to use the method to nd the value of an xn + an1 xn1 + + a1 x + a0 at x = c: procedure Horner(c, a0 , , an : R) y := an for i := 1 to n do y := y c + ani end for return y a) Evaluate 3x2 + x + 1 at x = 2 by working through each step of the algorithm. b) Exactly how many multiplications and additions are required by this algorithm to evaluate a polynomial of degree n at x = c? (Do not count additions used to increment the loop variable.)
k k

SOLUTION SET 3DUE 2/13/2008

Solution. Running through it, a) Start with 3. Multiply by 2 and add 1, giving 7. Multiply by 2 and add 1, giving 15. b) On each pass through the loop, you do one multiply and one add, so two arithmetic operations. Since you pass through the loop n times, thats 2n arithmetic operations. Problem 12 (#3.3.28). Describe how the number of comparisons used in the worst case changes when the size of the list to be sorted changes from n to 2n, where n is a positive integer when these sorting algorithms are used. a) bubble sort b) insertion sort c) selection sort d) binary insertion sort Solution. When possible, well use actual comparison counts instead of () estimates. n1 (n1)(n) a) As implemented in Rosen, a bubble sort always takes i=1 i = 2 comparisons. Thus, when the list size increases to 2n, the number of comparisons increases by a factor of
4n2 2n 2 n2 n 2

4n2 2n 1 1/2n =4 4 2n n 1 1/n

as n . This is the asymptotic behavior youd expect for a (n2 ) sort like bubble sort sorting twice as many objects takes four times as many comparisons. b) In worst-case an insertion sort requires exactly as many comparisons as a bubble sort. (The worst case is an already-sorted list, in which case the ith insertion takes i comparisons. Again, doubling the size of the list requires asymptotically 4 times as many comparisons. c) Selection sort is very much like a bubble sortat each step youre required to scan the entire unsorted portion of the list. Once again, we have asymptotically 4 times as many comparisons when the list size is doubled. d) Binary insertion sort is much better. A binary search takes log2 n comparisons. At step i the search is over i items, so the number of searches is bounded n above by i=1 log2 i, which we showed earlier in the set is n log n. Thus, sorting twice as long a list increases the number of comparisons by 2(log 2 + log n) 2n log 2n = = 2 + logn 2 2 n log n log n Asymptotically, it takes twice as many comparisons to sort twice as long a search with the binary insertion sort. This is the classic behavior of an n log n algorithm the bigger the sample, the closer to linear behavior (while always growing slightly faster(x) and (x log x) arent equivalent). Problem 13 (#3.4.6). Show that if a, b, c, and d are integers such that a|c and b|d, then ab|cd. Proof. Since a|c, there exists an n such that c = an. Similarly, there exists an m such that d = bm. Thus cd = (an)(bm) = (mn)(ab), so ab|cd.

SOLUTION SET 3DUE 2/13/2008

Problem 14 (#3.4.10). What are the quotient and remainder when? a) 44 is divided by 8? b) 777 is divided by 21? c) -123 is divided by 19? d) -1 is divided by 23? e) -2002 is divided by 87? f ) 0 is divided by 17? g) 1,234,567 is divided by 1001? h) -100 is divided by 101? Solutions. Presented without comment as a = (q)b + r a) 44 = (5)8+4 b) 777 = (37)21+0 c) -123 = (-7)19+10 d) -1 = (-1)23+22 e) -2002 = (-24)87+86 f ) 0 = 0(17)+0 g) 1,234,567 = (1233)1001 + 334 h) -100 = -1(101)+1 Problem 15 (#3.4.14). Show that if a is an integer and d is a positive integer greater than 1, then the quotient and remainder obtained when a is divided by d are a/d and a da/d respectively. Proof. By the division algorithm, we have a = qd + r, where 0 r < d. Stepping into the rationals, we see that 0 r/d < 1, and q a/d = q + r/d < q + 1. By the denition of the oor function, a/d = q, from which it follows that r = ada/d, as desired. Problem 16 (#3.4.22). Show that if a, b, c, and m are integers such that m 2, c > 0, and a b (mod m), then ac bc (mod mc). Proof. Since a b (mod c), a b = mk for some k Z. But then ac bc = mkc, so ac bc (mod mc), as desired. Problem 17 (#3.4.24). Prove that if n is an odd positive integer, then n2 1 (mod 8). Proof. Since n is an odd positive integer, for some k N, n = 2k + 1. Thus, n2 = 4k 2 + 4k + 1, or n2 = 4(k 2 + k) + 1. Now, if k is even, then so is k 2 , so k 2 + k is even. If k is odd, then so is k 2 , so k 2 + k is again even. Thus k 2 + k = 2 for some N, and so n2 = 8 + 1. Thus, n2 1 (mod 8), as desired.

Vous aimerez peut-être aussi