CST-370 Week 2
This week, I learned to mathematically analyze algorithms using Asymptotic notations on algorithms such as non-recursive and recursive.
Asymptotic Notations
Algorithm analysis is to identify the time category of an algorithm (time complexity). The most common categories are: 1, log n, n, nlogn, n^2, n^3, 2^n, n!
Three notations that represent an algorithm's efficiency: O(f(n)) <- upper bound, Θ (f(n)) <- tight bound, and Ω(f(n)) <- lower bound.
f(n) written as O(n^2), Θ (n), Ω(nlogn)
Upper bound: All functions with lower or same order of growth as f(n). Ex: f(n) = O(n^2) Functions that satisfy: 1, n, nlogn, n^2, 5n, 7n^2, 500.
Tight bound: All functions with the same order of growth as f(n). Ex: f(n) = Θ (n). Functions that satisfy: 4n, 20n
Lower bound: All functions with same or higher order of growth as f(n). Ex: f(n) = Ω(n*logn). Functions that satisfy: n*logn, n, n^2, n^3, 4n^3
Two Rules to Simplify T(n)
T(n) => O(n)
- 1. Eliminate lower order terms. Ex: T(n) = 4*n + 5 => 4*n
- 2. Eliminate constant coefficients. T(n) = 4*n => n => O(n)
- 1. Algorithms that do not contain recursive calls
- 2. Use of Asymptotic notations for time efficiency analysis
Example:
factorial function n!: n! = n*(n-1)!, 0! = 1
Algorithm F(n)
1. if (n = 0)
3. return 1
4. else
5. return F(n-1) * n
Comments
Post a Comment