450 likes | 461 Views
Explore the computational complexity of algorithms through asymptotic notation and recurrence equations. Learn to estimate growth rates and examine worst-case scenarios. Dive into examples, bounds, and solutions for various algorithms.
E N D
Asymptotics and Recurrence Equations Analysis of Algorithms Prepared by John Reif, Ph.D.
Asymptotics and Recurrence Equations • Computational Complexity of a Program • Worst Case and Expected Bounds • Definition of Asymptotic Equations • Solution of Recurrence Notation
Readings • Main Reading Selection: • CLR, Chapter 3, 4 and Appendix A
Goal of Asymptotic Notation • To estimate and compare growth rates of functions • Ignore constant factors of growth
”f(n) is little o g(n) ” • f(n) is o(g(n)) if
”f(n) is big O g(n) ” • f(n) is O(g(n)) if
“f(n) is order at least g(n)” • f(n) is Ω(g(n)) if
“f(n) is order tight with g(n)” • f(n) is Θ(g(n)) if
Suppose my algorithm runs in time O(n) • Don’t say: • “his algorithm runs in time O(n2) so is worse” • But prove: • “his algorithm runs in time Ω(n2) so is worse” • Must find a worst case input of length n for which his algorithm takes time > cn2 for all n > n0
Use of O Notation • n is O(n2) sometimes written n = O(n2) • But n2 is not O(n) so can’t use identities! • The two sides of the equality do not play a symmetric role
Use of Asymptotic Notation • Write • As • Example
Convergent Power Sum • A polynomial is asymptotically equal to its leading term as x ∞
Sums of Powers • for n ∞ • Or equivalently
Examples • 2nd order asymptotic expansion
Asymptotic Expansion of f(n) as n n0 • If • and
Bounding Sums by Integrals (cont’d) • So • Example then
Bounding Sums by Integrals (cont’d) • So • Since • So
Other Approximations Derived from Integrals • Harmonic Numbers
Stirling’s Approximation for Factorial • Factorial n! = 1·2 · 3 · · · (n-1) · n • So
Recurrence Equations Approximate Solution of Recurrence Relations
Recurrence Equations (over integers) • Homogenous of degree d n > d • Given constant coefficients a1, …, ad initial values x1, x2 ,…, xd
Example: Fibonacci Sequence • n > 2 Fn = Fn-1 + Fn-2 F0 = 0, F1 =1
Solution of Fibonacci Sequence “golden ratio” where
A Useful Theorem • c > 0, d > 0 • If • then
Proof • Is solution
Example: Mergesort input list L of length N if N=1 then return L else do let L1 be the first elements of L let L2 be the last elements of L M1 Mergesort (L1) M2 Mergesort (L2) return Merge (M1 , M2)
Time Bounds of Mergesort • Initial Value T(1) = c1 for N > 1 for some constants c1, c2> 1
Time Bound (cont’d) • N > 1 guess Holds if Solution
Time Bound (cont’d) • N > 1 • Transform Variables • Recurrence equation:
Advanced Material Exact Solution of Recurrence Relations
Homogenous Recurrence Relations (no constant additive term) • Solve: try Multiply by • Get characteristic equation:
Case of Distinct Roots • Distinct Roots r1, r2, …, rd • Where ri is dominant root
Other Case • Roots are not distinct r1= r2 = r3 • Then solutions not independent, so additional terms:
Inhomogenous Recurrence Equations • Nonzero constant term • Solution Method (1) Solve homogenous equation
Solution Method • Solve homogenous equation • Case , add particular solution
Solution Method (cont’d) Case , add particular solution • Add particular and homogeneous solutions, and solve for constants This is all we usually need!!
Asymptotics and Recurrence Equations Analysis of Algorithms Prepared by John Reif, Ph.D.