Sie sind auf Seite 1von 3

# CSci 231 Homework 1 Solution

## Insertion sort and Growth of Functions CLRS Chapter 1, 2 and 3, appendix A

Write and justify your answers on this sheet in the space provided.1 1. (CLRS 1.2-2) Suppose we are comparing implementations of insertion sort and merge sort on the same machine. For inputs of size n, insertion sort runs in 8n2 steps, while merge sort runs in 64n log n steps. For which values of n does insertion sort beat merge sort? Solution: We want to nd n such that 8n2 64n lg n: 8n2 64n lg n n 8 lg n lg n 1 , 8 n which is satised for n 43. 2. (CLRS 1-1) For each function f (n) and time t in the following table, determine the largest size n of a problem that can be solved in time t, assuming that the algorithms to solve the problem 1 second 1 minute 1 day 1 month n 106 60 106 864 108 26 1011 takes f (n) microseconds. Solution: n2 103 7745 29 104 16 105 n 2 20 26 36 41

3. (CLRS 3.1-3) Explain why the statement The running time of algorithm A is at least O(n2 ) is content free. Solution: This just checks that you understand what an upper bound is. The statement is saying the running time is at least at most n2 , but says nothing about the upper bound, which could be O(n3 ), O(2n ), etc. We cannot extract any information regarding lower bounds either, so the statement is meaningless and content free.

Collaboration is allowed, even encouraged, provided that the names of the collaborators are listed along with the solutions. Write up the solutions on your own.

4. (part of CLRS 3-3) Order the following expressions by their asymptotic growth and justify your answer. n 2n , n!, (log n)!, n3 , en , 2log2 n , n log n, 22 , nlog log n Solution: 2log2 n < n log n < n3 < (log n)! < nlog log n < 2n < en < n! < 22 . One typical way to compare the growth of two functions is to compute their limit when (n) n goes to , that is, limn f g (n) . If the limit is nite, then f and g have the same order of growth (f = (g )). If the limit is then f > g (f = w(g )). If the limit is zero, than f < g (f = o(g )). What you should remember is that any (positive) polynomial growth faster than any logarithm, and any (positive) exponential growth faster than any polynomial, that is: loga < nb < cn , for any a, b > 0 and any c > 1 One other trick you can use is to take the logarithm. For instance, nlg lg n = (lg n)lg n < 2n because lg n lg lg n < n. Typically you can use the Stirling formula to prove bounds involving n!: lg n! = (n lg n) so that comparing n! and 22 is the same as taking the logarithm of both and comparing n lg n and 2n . The rst is smaller than the second, since n lg n is polynomially bounded and 2n is exponential. 5. (CLRS A.1-1) Find a simple formula for Solution:
n n n k=1 (2k
n n

1).

(2k 1) = 2
k=1 k=1

kn

n(n + 1) n 2 = n2 + n n = 2 = n2 .

n 2 i=1 i

n(n+1)(2n+1) . 6

n=1

i2 = 1 =
i=1

## n(n + 1)(2n + 1) 123 = . 6 6

Assume that the statement is true for some k and use this to show k + 1:
k+1 k

i2 =
i=1 i=1

i2 + (k + 1)2 =

n

## Let P (n) denote i2 =

i=1

n(n + 1)(2n + 1) . 6

Since we proved the statement P (1) and showed that the inductive hypothesis P (k ) P (k + 1), P (n) is true for all n 1.

7. (CLRS 3-2) Indicate, for each pair of expressions (A, B ) below, whether A is O, o, , , or of B . Assume that k 1, > 0 and c > 1 are constants. Your answer should be in the form of a table with yes or no written in each box: (lgk n, n ), (nk , cn ), ( n, nsin n ), (2n , 2n/2 ), (nlg m , mlg n ), (lg(n!), lg(nn )). Solution: (A, B ) (lg k n, n ) (nk , cn ) ( n, nsin n ) (2n , 2n/2 ) (nlg m , mlg n ) (lg(n!), lg(nn )) O yes yes no no yes yes o yes yes no no no no no no no yes yes yes no no no yes no no no no no no yes yes

a. b. c. d. e. f.