Big-O notation: The formal method of expressing the upper bound of an algorithm’s running time (worst case) Big-Omega notation: The formal method of expressing the lower bound of an algorithm’s running time (best case) Theta Notation: The method of expressing that a given function is bounded from both top to bottom by the same function This exists if and only if f(n) is O(g(n)) and f(n) is Ω(g(n)) Little-O notation: f(n) is little-O(g(n))--denoted o(g(n))--if and only if f(n) = O(g(n)), but f(n) ¹ Q(g(n)) Little-Omega notation: f(n) is little-omega(g(n))—denoted w(g(n))—if and only if f(n)=Ω(g(n)), but f(n) ¹ Q(g(n))
Big-Oh:
http://www.youtube.com/watch?v=foUNGAsN-1M&feature=related Formal definition (symbols): f(n) = O(g(n)) if $ c, n0 | f(n) <= c * f(n) when n >= n0. Formal definition (words): f(n) = O(g(n)) if a real positive numbers c and a positive integer N exist such that f(n) <= c * g(n) for all n >= N. That is, c * g(n) is an upper bound on f(n) when n is sufficiently large. Reduction steps:
For the example code: Cost:
1) int sum(int n) {
2) int sum; 0
3) sum = 0; 1
4) for(int i = 1; i <= n.length; n++) 2n + 2
5) sum += i * i * i; 4n
6) return sum; + 1
Add up the total cost of the function = 6n + 4
c. Reduce all constants to 1 = 1n + 1
- Highest power will be Big-Oh
d. Apply O(f(n)) definition
- Ex) Let c = 6+1 = 7
Is 6n