310 likes | 365 Views
Asymptotic Notation. Q , O , W , o , w Used to describe the running times of algorithms Instead of exact running time, say Q ( n 2 ) Defined for functions whose domain is the set of natural numbers Determine sets of functions, in practice used to compare two functions. -notation.
E N D
Asymptotic Notation • Q, O, W, o, w • Used to describe the running times of algorithms • Instead of exact running time, say Q(n2) • Defined for functions whose domain is the set of natural numbers • Determine sets of functions, in practice used to compare two functions Z. Guo
-notation For a given function g(n), we denote by (g(n)) the set of functions (g(n)) = {f(n): there exist positive constants c1, c2 and n0 such that 0 c1g(n) f(n)c2g(n), for all n n0} We say g(n) is an asymptotically tight bound for f(n) Z. Guo
Example • 10n2-3n = Q(n2) (actually = is ϵ) • What constants for n0, c1, and c2 will work? • Make c1 a little smaller than leading coefficient, and c2 a little bigger • To compare orders of growth, look at leading term Z. Guo
O-notation For a given function g(n), we denote by O(g(n)) the set of functions O(g(n)) = {f(n): there exist positive constants c and n0 such that 0 f(n)cg(n) for all n n0} We say g(n) is an asymptotic upper boundfor f(n) Z. Guo
-notation For a given function g(n), we denote by (g(n)) the set of functions (g(n)) = {f(n): there exist positive constants c and n0 such that 0 cg(n)f(n) for all n n0} We say g(n) is an asymptotic lower boundfor f(n) Z. Guo
Relations Between Q, W, O • For any two functions g(n) and f(n), f(n)=(g(n)) if and only if f(n)=O(g(n)) and f(n)=(g(n)). • I.e., (g(n)) =O(g(n)) ÇW(g(n)) Z. Guo
Running Times • “The running time is O(f(n))”Þ Worst case is O(f(n)) • “Running time is W(f(n))”Þ Best case is W(f(n)) • Can still say “Worst case running time is W(f(n))” • Means worst case running time is given by some unkown function g(n)ÎW(f(n)) Z. Guo
Example • Insertion sort takes Q(n2) worst case time, so sorting (as a problem) is O(n2) • Any sort algorithm must look at each item, so sorting is W(n) • In fact, using (e.g.) merge sort, sorting is Q(n lg n) in the worst case Z. Guo
Asymptotic Notation in Equations • Used to replace expressions containing lower-order terms • For example, 4n3 + 3n2 + 2n + 1 = 4n3 + 3n2 + (n) = 4n3 + (n2) = (n3) • In equations, (f(n)) always stands for an anonymous functiong(n) Î(f(n)) • In the example above, (n2) stands for 3n2 + 2n + 1 Z. Guo
o-notation For a given function g(n), we denote by o(g(n)) the set of functions o(g(n)) = {f(n): for any positive constant c > 0, there exists a constant n0 > 0 such that 0 f(n)<cg(n)for all n n0} f(n) becomes insignificant relative to g(n)as n approaches infinity: lim [f(n) / g(n)] = 0 n We say g(n) is anupper boundfor f(n)that is not asymptotically tight. Z. Guo
w-notation For a given function g(n), we denote by w(g(n)) the set of functions w(g(n)) = {f(n): for any positive constant c > 0, there exists a constant n0 > 0 such that 0 cg(n)<f(n)for all n n0} f(n) becomes arbitrarily large relative to g(n)as n approaches infinity: lim [f(n) / g(n)] = n We say g(n) is a lower boundfor f(n)that is not asymptotically tight. Z. Guo
Limits • lim [f(n) / g(n)] = 0 Þ f(n)Îo(g(n)) n • lim [f(n) / g(n)] < Þ f(n)ÎO(g(n)) n • 0 < lim [f(n) / g(n)] < Þ f(n)ÎQ(g(n)) n • 0 < lim [f(n) / g(n)]Þ f(n)Î W(g(n)) n • lim [f(n) / g(n)] = Þ f(n)Îw(g(n)) n • lim [f(n) / g(n)] undefinedÞ can’t say n Z. Guo
Comparison of Functions f g a b f (n) = O(g(n)) a b f (n) = (g(n)) a b f (n) = (g(n)) a = b f (n) = o(g(n)) a < b f (n) = w (g(n)) a > b Z. Guo
Properties • Transitivity f(n) = (g(n)) & g(n) = (h(n)) f(n) = (h(n)) f(n) = O(g(n)) & g(n) = O(h(n)) f(n) = O(h(n)) f(n) = (g(n)) & g(n) = (h(n)) f(n) = (h(n)) f(n) = o (g(n)) & g(n) = o (h(n)) f(n) = o (h(n)) f(n) = w(g(n)) & g(n) = w(h(n)) f(n) = w(h(n)) • Symmetry f(n) = (g(n))if and only if g(n) = (f(n)) • Transpose Symmetry f(n) = O(g(n))if and only if g(n) = (f(n)) f(n) = o(g(n))if and only if g(n) = w((f(n)) Z. Guo
Useful Facts • Fora ³0, b > 0, lim n( lgan / nb ) = 0, so lgan= o(nb), and nb = w(lgan) • Prove using L’Hopital’s rule repeatedly • lg(n!) = (n lg n) Z. Guo
Examples o , , or w? A B • 5n2 + 100n 3n2 + 2 • log3(n2)log2(n3) • nlg43lg n • lg2nn1/2 Z. Guo
Examples o , , or w? A B • 5n2 + 100n 3n2 + 2 • log3(n2)log2(n3) • nlg43lg n • lg2nn1/2 logba = logca / logcb alog b =blog a Z. Guo
Examples A B • 5n2 + 100n 3n2 + 2 A (B) A (n2), n2(B) A (B) • log3(n2)log2(n3) • nlg43lg n • lg2nn1/2 Z. Guo
Examples A B • 5n2 + 100n 3n2 + 2 A (B) A (n2), n2(B) A (B) • log3(n2)log2(n3) A (B) logba = logca / logcb; A = 2lgn / lg3, B = 3lgn, A/B =2/(3lg3) • nlg43lg n • lg2nn1/2 Z. Guo
Examples A B • 5n2 + 100n 3n2 + 2 A (B) A (n2), n2(B) A (B) • log3(n2)log2(n3) A (B) logba = logca / logcb; A = 2lgn / lg3, B = 3lgn, A/B =2/(3lg3) • nlg43lg n A w(B) alog b =blog a; B =3lg n=nlg 3; A/B =nlg(4/3) as n • lg2nn1/2 Z. Guo
Examples A B • 5n2 + 100n 3n2 + 2 A (B) A (n2), n2(B) A (B) • log3(n2)log2(n3) A (B) logba = logca / logcb; A = 2lgn / lg3, B = 3lgn, A/B =2/(3lg3) • nlg43lg n A w(B) alog b =blog a; B =3lg n=nlg 3; A/B =nlg(4/3) as n • lg2nn1/2 A o (B) lim( lgan / nb ) = 0 (herea = 2 and b = 1/2)A o (B) n Z. Guo
Reviews on Summation • Why do we need summation formulas? We need them for computing the running times of some algorithms. (CLRS/Chapter 3) • Example: Maximum Subvector Given an array a[1…n] of numeric values (can be positive, zero and negative) determine the maximum value of sums to all subvectors a[i…j] (1 i j n). Z. Guo
Reviews on Summation MaxSubvector(a, n) maxsum ¬ 0; for i ¬ 1 to n for j = i to n sum ¬ 0; for k ¬ i to j sum += a[k] maxsum ¬ max(sum, maxsum); return maxsum; Z. Guo
Reviews on Summation MaxSubvector(a, n) maxsum ¬ 0; for i ¬ 1 to n for j = i to n sum ¬ 0; for k ¬ i to j sum += a[k] maxsum ¬ max(sum, maxsum); return maxsum; n n j • T(n) = 1 i=1 j=i k=i • NOTE: This is not a simplified solution. What is the final answer? Z. Guo
Reviews on Summation • Constant Series: For n 0, b 1 = b - a + 1 i=a • Linear Series: For n 0, n i = 1 + 2 + … + n = n(n+1)/2 i=1 n n j n n n n-i+1 n • T(n) = 1 = (j-i+1) = a = (n-i+1)(n-i+2)/2 i=1 j=i k=i i=1 j=i i=1 a=1 i=1 Z. Guo
Reviews on Summation • Quadratic Series: For n 0, n i2 = 12 + 22 + … + n2 = (2n3 + 3n2 + n)/6 i=1 • Linear-GeometricSeries: For n 0, n n j n n • T(n) = 1 = (n-i+1)(n-i+2)/2 = ½ b(b+1) i=1 j=i k=i i=1 b=1 = b2+ b = … (reversing order of sum) Z. Guo
Review on Summations • Cubic Series: For n 0, • Geometric Series: For real x 1, For |x| < 1, Comp 550
Review on Summations • Telescoping Series: • Harmonic Series: nth harmonic number, nI+, • Differentiating Series: For |x| < 1, Comp 550
More on Maximum Subvector • Given an array a[1…n] of numeric values (can be positive, zero and negative) determine the maximum value of sums to all subvectors a[i…j] (1 i j n).. • (Hint: How should we treat a subvector with negative sum during the search process?) • def max_subarray(A): max_ending_here = max_so_far = 0 for x in A: max_ending_here = max(0, max_ending_here + x) max_so_far = max(max_so_far, max_ending_here) return max_so_far Z. Guo