Big O Notation and Asymptotic Analysis
Basic Rules
- O (big-oh): upper bound
- Ω (big-omega): lower bound
- Θ (theta): average bound
Example 1: Big-O Notation
Definition: The function f(n)=O(g(n)) if there exist constants c and n0 such that:
f(n)≤c⋅g(n) for all n≥n0
For f(n)=2n+3:
A simple approach is to multiply by n:
f(n)=2n+3≤2n+3n=5n for n≥1
Therefore, f(n)=O(n)
We can also show:
f(n)=2n+3≤2n2+3n2=5n2 for n≥1
So f(n)=O(n2) is also true.
Asymptotic Growth Rates
From slowest to fastest growing:
1<logn<n<n<nlogn<n2<n3<…<2n<3n<…<nn
For f(n)=O(n):
- Functions larger than n (like nlogn) can satisfy the O(n) upper bound
- Functions smaller than n (like n) can satisfy the Ω(n) lower bound
Note that:
f(n)=O(n)⟹f(n)=O(n2)⟹f(n)=O(2n)
Example 2: Big-Omega Notation
Definition: The function f(n)=Ω(g(n)) if there exist constants c and n0 such that:
f(n)≥c⋅g(n) for all n≥n0
For our function f(n)=2n+3:
- f(n) cannot be written as Ω(logn)
- The most similar bound is Ω(n)
Note that:
f(n)=Ω(n)⟹f(n)=Ω(logn)⟹f(n)=Ω(n)⟹f(n)=Ω(1)
Example 3: Theta Notation
Definition: The function f(n)=Θ(g(n)) if there exist constants c1, c2, and n0 such that:
c1⋅g(n)≤f(n)≤c2⋅g(n) for all n≥n0
For f(n)=2n+3:
1⋅n≤2n+3≤5⋅n for n≥3
Where 1⋅n is c1⋅g(n) and 5⋅n is c2⋅g(n)
Therefore, f(n)=Θ(n)
Example 4
For f(n)=2n2+3n+4:
Upper bound:
f(n)=2n2+3n+4≤2n2+3n2+4n2=9n2 for n≥1
Therefore, f(n)=O(n2)
Lower bound:
f(n)=2n2+3n+4≥1⋅n2 for n≥1
Therefore, f(n)=Ω(n2)
Since f(n)=O(n2) and f(n)=Ω(n2), we have f(n)=Θ(n2)
Example 5
For f(n)=n2logn:
n2logn≤f(n)=n2logn≤10n2logn
Therefore, f(n)=O(n2logn) and f(n)=Ω(n2logn)
Thus, f(n)=Θ(n2logn)
Example 6
For f(n)=n!:
1≤n!=n⋅(n−1)⋅...⋅1≤n⋅n⋅...⋅n=nn
Therefore, f(n)=O(nn) and f(n)=Ω(1)
For n!, we cannot establish a tight Θ bound using simple functions.
Example 7
For f(n)=log(n!):
log(1)≤log(n!)≤log(nn)=nlogn
Therefore, f(n)=O(nlogn) and f(n)=Ω(1)
For log(n!), a tight Θ bound would require more analysis.