Vous êtes sur la page 1sur 4

Homework Assignment #3

Growth of functions

Q1) Show that (n^2+1) /(n+1) is O(n)

Answer:

(n^2+1) /(n+1) =

(n^2) / n = n

T(n) = O(n)

Q2) Sort the following running time functions in an ascending asymptotic order.
n^1/2, 1000log(n), nlog(n), 2n!, 2^n , 3^n , 1.1^n , n^2 /1000

Answer:

1000log(n) => nlog(n) => n^1/2 => n^2 /1000 => 1.1^n => 2^n => 3^n => 2n!

Q3) Is nlog(n) = Ω(n2 )? Is 2n = Ω(n2 )? Explain your answer for each one.

Answer:

Yes nlog(n) is Ω (n2) because Omega notation provides lower bound for any
algorithm or we can say the time complexity of the best case . For larger value of n ,
n2 is much more larger than nlog(n). So it means we can get solution in Ω (n2).

Q4) Find the big-O notation for the following running time functions

a) T(n) = (n^2 + 8)(n+2) => O(n^2)

b) T(n) = (nlog(n) + n^2 ) (n^3 + 3) => O(n^3)

c) T(n) = 3n(5 + 2log(n^2 )) => O(log n^2)

d) T(n) = log(2^n ) + 2^n + n^2 => O(log 2^n)


Q5) Prove using the definition of big-O notation that either 3^n is O(2^n) or not.

Answer:

To prove 3^n is O(2^n), we must find n0, c such that f(n) <= c*g(n) for all n>=n0

3^n >= c*2^n

(3/2)^n >= c

n log(3/2) >= log c / log 3/2

n > = log 2c/3

For every n >= log 2c/3, 3^n >= 2^n. Therefore, 3^n is not O (2^n).

Q6) let T(n) be a running time function defined recursively as 𝑇(𝑛) = { 0, 𝑛 = 0 1, 𝑛 = 1


3𝑇(𝑛 − 1) − 2𝑇(𝑛 − 2), 𝑛 > 1

a) Find a non-recursive formula for T(n).

Answer:

T(n) = T(n/3) + T(n-2) + T(n-1) + n

b) Prove by induction that your answer in part (a) is correct.

Answer:

Let's suppose T(k) <= ck^3 for k<n

T(n) = 3T(n-1) – 2T(n-2)

<= 3c(n-1)^3 – 2c(n-2)^3 = cn^3 – (n-1)(n-2)n^3

<= cn^3 as long as (n-1)(n-2)n^3 > 0


c) Find a tight bound for T(n).

Answer:

T(n) = T(n/3) + T(n-2) + T(n-1) + n

(n/3) -> O(1)

(n-2) -> O(1)

(n-1) -> O(1)

(n) -> O(n)

=> O(n)

Q7) Find the running time function of the following algorithms as we learned in the
class and then the tightest big-O notation.

Answer a):

Line 1 -> O (1)


Line 2,3 -> O (n)
Line 4,5 -> O (n)
Line 6 -> O (1)

big-O is O(n)
Answer b):

Line 1 -> O (1)


Line 2-6 -> nested loop is O (n^3)
Line 7 -> O (1)

big-O is O(n^3)

Answer c):

Line 1 -> O (1)


Line 2-6 -> nested loop is O (n^2)
Line 7 -> O (1)

big-O is O(n^2)

Vous aimerez peut-être aussi