Docsity

Homework Assignment 2 Solved Questions - Algorithm Design | ECS 122A, Assignments of Computer Science

Toggle sidebar
bg1
ECS122A: Algorithm Design and Analysis
Homework Assignment #2 Solutions
1. Exercise 3.1-2: Show that for any real constants aand b, where b > 0.
(n+a)b= Θ(nb).
Solution: We need to show that there are constants c1, c2, n0>0 such that 0 ≤c1nb≤(n+a)b≤
c2nbfor all n≥n0. Note that
n+a≤n+|a| ≤ 2nwhen n≥ |a|.
and
n+a≥n− |a| ≥ 1
2nwhen n≥2|a|.
Thus, when n≥2|a|,
0≤1
2n≤n+a≤2n.
Since b > 0, the inequality still holds when all parts are raised to the power b:
0≤µ1
2nb
≤(n+a)b≤(2n)b.
Thus, c1= (1/2)b,c2= 2b, and n0=⌈2|a|⌉.
2. Exercise 3.1-3: Explain why the statement “The running time of Algorithm A is at least O(n2)”
is meaningless.
Solution: Let the running time an algorithm be T(n). T(n)≥O(n2) means that T(n)≥f(n) for
some function f(n) in the set O(n2). This is true for any running time T(n), since the function
g(n) = 0 for any nis in O(n2) and run times are always >0. So the statement tells us nothing
about the running time.
3. Order the following functions into a list such that if fcomes before gin the list then f(n) =
O(g(n)). If any two (or more) of the same asymptotic order, indicate which.
(a) Start with these basic functions
n2nnlg n n3
n2lg n n −n3+ 7n5n2+ lg n
Solution: (a)
lg n,
n,
nlg n,
n2(≈n2+ lg n),
n3,
n−n3+ 7n5,
2n
1
pf3

Partial preview of the text

Download Homework Assignment 2 Solved Questions - Algorithm Design | ECS 122A and more Assignments Computer Science in PDF only on Docsity!

ECS122A: Algorithm Design and Analysis Homework Assignment #2 Solutions

  1. Exercise 3.1-2: Show that for any real constants a and b, where b > 0.

(n + a)b^ = Θ(nb).

Solution: We need to show that there are constants c 1 , c 2 , n 0 > 0 such that 0 ≤ c 1 nb^ ≤ (n + a)b^ ≤ c 2 nb^ for all n ≥ n 0. Note that

n + a ≤ n + |a| ≤ 2 n when n ≥ |a|.

and n + a ≥ n − |a| ≥

n when n ≥ 2 |a|.

Thus, when n ≥ 2 |a|, 0 ≤

n ≤ n + a ≤ 2 n. Since b > 0, the inequality still holds when all parts are raised to the power b:

( 1 2

n

)b ≤ (n + a)b^ ≤ (2n)b.

Thus, c 1 = (1/2)b, c 2 = 2b, and n 0 = ⌈ 2 |a|⌉.

  1. Exercise 3.1-3: Explain why the statement “The running time of Algorithm A is at least O(n^2 )” is meaningless. Solution: Let the running time an algorithm be T (n). T (n) ≥ O(n^2 ) means that T (n) ≥ f (n) for some function f (n) in the set O(n^2 ). This is true for any running time T (n), since the function g(n) = 0 for any n is in O(n^2 ) and run times are always > 0. So the statement tells us nothing about the running time.
  2. Order the following functions into a list such that if f comes before g in the list then f (n) = O(g(n)). If any two (or more) of the same asymptotic order, indicate which. (a) Start with these basic functions

n 2 n^ n lg n n^3 n^2 lg n n − n^3 + 7n^5 n^2 + lg n

Solution: (a) lg n, n, n lg n, n^2 (≈ n^2 + lg n), n^3 , n − n^3 + 7n^5 , 2 n

(b) Combine the following functions into your answer for part (a). Assume that 0 < ǫ < 1.

en^

n 2 n−^1 lg lg n (

2)lg^ n ln n (lg n)^2 n! n1+ǫ^1

Solution: (b) 1 lg lg n, lg n(≈ ln n), (lg n)^2 , √ n = (

2)lg^ n, n, n lg n, n1+ǫ, n^2 (≈ n^2 + lg n), n^3 , n − n^3 + 7n^5 , 2 n(≈ 2 n−^1 ), en, n! The problem does not ask you to justify these relations (otherwise, it will be too long!). But here is an example. Why n lg n = O(n1+ǫ)? We can compute limn→∞[n lg n/n1+ǫ] = 0 (using L’Hopital’s rule).

  1. Exercise 4.3-

Solution: (a) This is a divide-and-conquer recurrence with a = 4, b = 2 and f (n) = n, and nlogb^ a^ = nlog^2 4 = n^2. The quotient f (n) nlogb^ a^

n n^2

n

Case 1 of the master theorem applies, and T (n) = Θ(nlogb^ a) = Θ(n^2 ). (b) In this divide-and-conquer recurrence, a = 4, b = 2 and f (n) = n^2 , and nlogb^ a^ = nlog^2 4 = n^2. The quotient f (n) nlogb^ a^

n^1 n^2

Case 2 of the master theorem applies, and T (n) = Θ(nlogb^ a^ lg n) = Θ(n^2 lg n). (c) In this divide-and-conquer recurrence, a = 4, b = 2 and f (n) = n^3 , and nlogb^ a^ = nlog^2 4 = n^2. The quotient f (n) nlogb^ a^

n^3 n^2

= n.

Case 3 of the master theorem applies, and T (n) = Θ(f (n)) = Θ(n^3 ).

  1. Give asymptotic upper and lower bounds for T (n) in each of the following recurrences. Assume that T (n) is constant for sufficient small n, and c is a constant. Make your bounds as tight as possible, and justify your answers.