Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

CMPSCI 311 Solution to homework 1 1a Solution (Exercises1.

2-2): If insertion sort is faster for n, then we have: (found using a calculator) (considering only integer values of n for the input size).

1b Solution (Exercises1.2-3): We must satisfy the following condition: 100n2 < 2n By simple enumeration for values of n starting from 1, we find that n=15 is the smallest value of n for which 100n2=22500 is smaller than 2n=32768. Hence, smallest value of n is 15 such that an algorithm whose running time is 100n2 runs faster than an algorithm whose running time is 2n. 2 Solution (Problems 1-1): We assume that all months are 30 days and all years are 365 days. 1 second lg n 1012 n n lg n n2 n3 2n n! 106 62746 103 102 19 9 36.1014 6.107 2801417 24494897 391 25 11 1296.1016 36.108 1.68*108 6.104 1532 31 12 746496.1016 864.108 4.03*109 293938 4420 36 13 6718464.1018 2592.109 1.21*1011 1609968 13736 41 15 8950673664.1020 94608.1010 1.47*1012 30758413 98169 49 17 8950673664.1024 94608.1012 1.47*1014 307584134 455661 56 18 1 minute 1 hour 1 day 1 month 1 year 1 century

3 Solution (Problems 3-2):

a. b. c. d. e. f. Justifications:

O Yes Yes No No Yes Yes

No No No Yes Yes Yes

No No No No Yes Yes

a. b. By k-fold application of LHopitals rule, we get:

c. -1 Sin n 1. When Sin n = 1, grows faster. When Sin n = -1,

implying that nsinn implying

that n1/2 grows faster. So we cannot compare the two functions asymptotically. d. e. f.

Also

4a Solution (Exercises 4.3-1): a. . With a=4, b=2, for ,

so case 1 of the master theorem applies. The cost of the leaves in the recursion tree dominates the overall cost, which is b. . Here we find that , so according

to case 2 of the master theorem, c. . Now the cost of the root of the recursion tree dominates the total for . Furthermore, . Thus we are

cost. First we note that for and n >1, we can show

justified by case 3 of the master theorem in concluding that 4b Solution (Problems 4-1): a.

For this recurrence: a=2, b=2, k=3, bk=8. Since a < bk, case 3 of master theorem applies, and T(n)=(n3). b. For this recurrence: a=1, b=10/9, k=1, bk=10/9. Since a < bk, case 3 of master theorem applies, and T(n)=(n). c. For this recurrence: a=16, b=4, k=2, bk=16. Since a= bk, case 2 of master theorem applies, and T(n)=(n2lg n) d. For this recurrence: a=7, b=3, k=2, bk=9. Since a < bk, case 3 of master theorem applies, and T(n)=(n2) e. For this recurrence: a=7, b=2, k=2, bk=4. Since a > bk, case 1 of master theorem applies, and T(n)=(nlg7) f. For this recurrence: a=2, b=4, k=1/2, bk=2. Since a= bk, case 2 of master theorem applies, and . g. T(n) = T(n-1) + n By unrolling this recursion, and using the recursion tree thus generated, we get a guess of T(n) = (n2). First we prove that T(n) = (n2), by induction. The inductive hypothesis is T(n) cn2 for some constant c>0. T(n) = T(n-1) + n c(n-1)2 + n (using inductive hypothesis) = cn2 2cn + c + n cn2 if -2cn + c + n 0 or, n(1-2c) + c 0. This is true for n 0 and 0 < c . Now we prove that T(n) = O(n2). In this case, the inductive hypothesis is T(n) cn2 for some constant c > 0. By a derivation similar to the one outlined above, we get that T(n) cn2 if -2cn + c + n 0 or, n(1-2c) + c 0. This is true for n 1 and c =1. Therefore, T(n) = (n2) and T(n) = O(n2). This implies that T(n) = (n2). h. We solve this using change of variables method. Let n = 2m and S(m) = T(2m). We have: T(2m) = T(2m/2) + 1, so S(m) = S(m/2) + 1. Using the master theorem, a=1, b=2, k=0, bk=1. Since a= bk, case 2 applies and S(m) = (lg m). Hence T(n) = (lglg n) Extra credit solution (Problems 3-3):

a. Here is the ordering, where functions on the same line are in the same equivalent class, and those higher on the page are of those below them:

(n+1)! n! en n.2n 2n (3/2)n (lg n)! n3 n2 = 4lg n n lg n and lg(n!) n = 2lg n

see justification 7 see justification 1

see identity 1 see justifications 2,8 see identity 2 see justification 6 see identity 3 see identity 6, justification 3 see identity 5, justification 4

lg2n ln n ln ln n lg*n and lg*(lg n) lg(lg*)n n1/lg n(=2) and 1

see justification 5 see identity 7 see identity 4

Much of the ranking is based on the following properties: Exponential functions grow faster than polynomial functions, which grow faster than polylogarithmic functions. The base of a logarithm doesnt matter asymptotically, but the base of an exponential and the degree of a polynomial do matter. We have the following identities: 1. because 2. because lg n 3. 2 = n 4. 2 = n1/lg n by raising identity 3 to the power 1/lg n 5. by raising identity 4 to the power 6. because 7. lg*(lg n) = (lg*n)-1

The following justifications explain some of the rankings: 1. , since 3 2. (lg n)! = (n ) by taking logs: lg(lg n)! = (lg n lg lg n) by Stirlings approximation, lg(n3)=3lg n. Therefore, lg lg n = (3) 3. Therefore, 4. 5. 6. lg(n!) = 7. 8. by taking logs: , lg lg2n = 2lg lg n. Therefore, by taking logs: , .

by taking logs: , lg ln ln n = (lg*n) (n lg n) (equation (3.18)) by dropping constants and low-order terms in equation (3.17) by substituting lg n for n in the previous justification. because

b. The following f(n) is nonnegative, and for all functions gi(n) in part (a), f(n) is neither O(gi(n)) nor (gi(n)). if n is even if n is odd

You might also like