Docsity

Ranking Functions - Introduction to Algorithms - Solved Quiz, Exercises of Algorithms and Programming

Toggle sidebar
bg1
Massachusetts Institute of Technology Handout 8
6.046J/18.410J: Introduction to Algorithms February 15, 2001
Professors Piotr Indyk and Madhu Sudan
Problem Set 1 Solutions
Problem 1-1. Ranking Functions by Order of Growth
The ranking is based on the following facts:
•exponential functions grow faster than polynomial functions, which grow faster than
logarithmic functions;
•the base of a logarithm does not matter asymptotically, but the base of an exponential
and the degree of a polynomial do matter.
•Stirling’s approximation for n! is useful for dealing with factorials asymptotically.
The ranking is:
2
3n(lg n)!
1nlg lg n
lg∗n nlg n
lg lg n3
2n
{ln n , lg n2}en
√2lg n22n
{n , 2blg nc,2dlg ne}(n−1)!
{nlg n , lg(n!) }n!
4lg n(2n)!
{n3, n2+n3}22n
Some notes:
•2
3napproaches zero.
•n= 2lg n≥2blg nc≥2lg n−1= 2−12lg n=1
2nand so 2blg nc= Θ(n). Similarly, n≤2dlg ne≤
2nand so also 2dlg ne= Θ(n).
•√2lg n= 21
2lg n=n1
2=√nand similarly 4lg n= 22 lg n=n2.
•lg(n!) ≈lg √2πe n
en+1
2= Θ(nlg n).
•(lg n)! ≈√2πe lg n
elg n+1
2=√2π(lg n)lg n√lg n
nlg e. Now notice that (lgn)lg n=2lg lg nlg n=
2lg lg nlg n=2lg nlg lg n=nlg lg n. Using this we get: (lg n)! ≈√2πnlg lg n−lg e√lg n. This
is asymptotically greater than any polynomial in n, since for any polynomial nd, there
is some nfor which lg lg n−lg e>d. However, it is strictly less than nlglg nsince they
differ by nlg e
√lg n=ω(1).
pf3
pf4

Partial preview of the text

Download Ranking Functions - Introduction to Algorithms - Solved Quiz and more Exercises Algorithms and Programming in PDF only on Docsity!

Massachusetts Institute of Technology Handout 8 6.046J/18.410J: Introduction to Algorithms February 15, 2001 Professors Piotr Indyk and Madhu Sudan

Problem Set 1 Solutions

Problem 1-1. Ranking Functions by Order of Growth

The ranking is based on the following facts:

  • exponential functions grow faster than polynomial functions, which grow faster than logarithmic functions;
  • the base of a logarithm does not matter asymptotically, but the base of an exponential and the degree of a polynomial do matter.
  • Stirling’s approximation for n! is useful for dealing with factorials asymptotically.

The ranking is:

( 2 3

)n (lg n)! 1 nlg lg^ n lg∗^ n nlg^ n lg lg n

( 3 2

)n

{ ln n , lg n^2 } en (√ 2

)lg n 22 n { n , 2 blg^ nc^ , 2 dlg^ ne^ } (n − 1)! { n lg n , lg(n!) } n! 4 lg^ n^ (2n)! { n^3 , n^2 + n^3 } 22 n

Some notes:

( 2 3

)n approaches zero.

  • n = 2lg^ n^ ≥ 2 blg^ nc^ ≥ 2 lg^ n−^1 = 2−^12 lg^ n^ = 12 n and so 2blg^ nc^ = Θ(n). Similarly, n ≤ 2 dlg^ ne^ ≤ 2 n and so also 2dlg^ ne^ = Θ(n).

)lg n = 2 (^12) lg n = n (^12) =

n and similarly 4lg^ n^ = 22 lg^ n^ = n^2.

  • lg(n!) ≈ lg

2 πe

( n e

)n+ 12 ) = Θ(n lg n).

  • (lg n)! ≈

2 πe

( (^) lg n e

)lg n+ (^12)

2 π (lg n)lg^ n

lg n nlg^ e^. Now notice that (lg^ n)

lg n (^) = ( 2 lg lg n)lg^ n =

2 lg lg^ n^ lg^ n^ =

( 2 lg^ n

)lg lg n = nlg lg^ n. Using this we get: (lg n)! ≈

2 πnlg lg^ n−lg^ e

lg n. This is asymptotically greater than any polynomial in n, since for any polynomial nd, there is some n for which lg lg n − lg e > d. However, it is strictly less than nlg lg^ n^ since they differ by n √lg^ e lg n = ω(1).

Problem 1-2. Asymptotic Notation

(a) Sometimes true: For f (n) = n it is true, while for f (n) = 1/n it is not true. (The statement is always true for f (n) = Ω(1), and hence for most functions with which we will be working in this course, and in particular all time and space complexity functions). (b) Sometimes true: For f (n) = 1 and g(n) = ‖n ∗ sin(n)‖ it is true, while for any f (n) = O(g(n)), e.g. f (n) = g(n) = 1, it is not true. (c) Always true: max(f (n), g(n)) ≤ f (n) + g(n) ≤ 2 max(f (n), g(n)). (d) Always true: Consider f (n) + g(n) where g(n) = o(f (n)) and let c be a constant such that g(n) < cf (n) for large enough n. Then f (n) ≤ f (n) + g(n) ≤ (1 + c)f (n) for large enough n. (e) Never true: If f (n) = Ω(g(n)) then there exists positive constant cΩ and nΩ such that for all n > nΩ, cg(n) ≤ f (n). But if f (n) = o(g(n)), then for any positive constant c, there exists no(c) such that for all n > no(c), f (n) < cg(n). If f (n) = Ω(g(n)) and f (n) = o(g(n)), we would have that for n > max(nΩ, no(cΩ)) it should be that f (n) < cΩg(n) ≤ f (n) which cannot be.

Problem 1-3. Insertion sort on small arrays in merge sort

(a) Insertion sort takes O(k^2 ) time per k-element list. Therefore, sorting n/k such k- element lists, takes O(k^2 n/k) = O(nk) time. (b) Merging requires O(n) work at each level, since we are still working on n elements, even if they are partitioned among sublists. The number of levels, starting with n/k k-element lists and finishing with 1 n-element list, is dlg(n/k)e. Therefore, the total running time for the merging is O(n lg(n/k)). (c) The largest asymptotic value of k, for which the modified algorithm has the same asymptotic running time as standard merge sort, is k = O(lg n). The combined running time is O(n lg n + n lg n − n lg lg n), which is O(n lg n). For larger values of k the term nk would be larger than O(n lg n). (d) In practice, k should be chosen such that it minimizes the running time of the combined algorithm, which is cnk + dn lg(n/k), where c and d are some positive constants that determine the actual running time of the two phases. The value k = d/c minimizes the above expression, and thus the best choice of k does not depend on n, but rather on the ratio between the constants d and c.

Thus T (n) ≤ an^2 whenever a(n^2 /4 + 5n + 25) + n^2 ≤ an^2 , which is equivalent to a(n^2 /4 + 5n + 25) ≤ a(n^2 − 1). This inequality is true, given any value of a, for n sufficiently large, e.g. for n ≥ 100. Thus, we have proved that, for any constant a, T (n) ≥ an^2 , for n ≥ 100. Hence T (n) = O(n^2 ). Combining T (n) = O(n^2 ) and T (n) = Ω(n^2 ), we get T (n) = Θ(n^2 ). (f) T (n) = 2T (n/2) + n lg n =⇒ T (n) = n lg^2 n Unfortunately, the Master Method cannot be used since the added value n lg n is within a logarithmic factor of nlog^2 2 = n. Instead, by iteration:

T (n) = n lg n + 2

n 2

lg

n 2

n 4

lg

n 4

= n

( lg n + lg

n 2

  • lg

n 4

)

= n

∑^ lg^ n

k=

lg 2k^ = n

∑^ lg^ n

k=

k

= n

lg n(lg n + 1) 2

= Θ(n lg^2 n)

(g) T (n) = 4T (n/2) + n^2 + n =⇒ T (n) = n^2 lg n This time its fine to use the second case of the Master Method, since n^2 + n = Θ(nlog^2 4 ) = Θ(n^2 ).