Big-O, Big-Omega and Big-Theta
State O, Ω and Θ with the constants c and n₀, and prove with witnesses that 3n² + 5n is Θ(n²).
It opens on
The graph shows 3n² + 5n above 4n² for n up to 4. Further right, will 4n² catch up with it and pass it for good, or stay below it forever?
Why it matters
A step count such as 3n² + 5n has details, the 3 and the 5n, that change with the computer and the way of counting, so we need a precise way to say that what lasts is the n², and to compare algorithms by it.
Good to know first
- Counting steps: a function's work is counted as a formula in n such as 3n + 2 or n², and the formulas are compared for large n
- Traversing lists: a loop over a list of n items runs its body n times
Step by step
Big-O with witnesses
State the definition of O and prove that 3n² + 5n is O(n²) by finding a constant c and a threshold n₀ and checking them with numbers.
Let f(n) = 3n² + 5n and g(n) = n², and try c = 4. We need 3n² + 5n ≤ 4n². Subtracting 3n² gives 5n ≤ n², and dividing by n, which is positive, gives 5 ≤ n. So the inequality holds exactly when n ≥ 5.
3n² + 5n ≤ 4n² gives 5n ≤ n² gives n ≥ 5
A common mix-up
O(n²) means exactly n² steps, or means the worst case.
In fact: O is an upper bound on a function from some n₀ on, and it says nothing about which input is meant; 3n² + 5n is O(n²) but also O(n³).
Lower bounds and tight bounds
State the definitions of Ω and Θ and prove that 3n² + 5n is Θ(n²), and say why it is not Θ(n³).
For f(n) = 3n² + 5n and g(n) = n², take c = 3 and n₀ = 1. Since 5n is positive, 3n² + 5n ≥ 3n² for every n ≥ 1, so f is Ω(n²).
the curve 3n² + 5n above the curve 3n²
A common mix-up
O is the worst case, Ω the best case and Θ the average case.
In fact: All three bound one function from above, from below and from both sides; the case is chosen first, and then a bound is given for that case's count.
The growth ladder
Order the common growth rates 1, log₂ n, n, n log₂ n, n², n³ and 2ⁿ, place 3n² + 5n among them, and say why the base of a logarithm does not matter.
Look at n = 8 and n = 16. At n = 8 the values are 3, 24, 64 and 256, and at n = 16 they are 4, 64, 256 and 65,536. When n doubles, log₂ n grows by 1, n log₂ n a bit more than doubles, n² quadruples and 2ⁿ is squared.
the table of growth rates at 8 and 16
A common mix-up
A bigger constant in front makes a faster-growing rate.
In fact: 100n is still Θ(n) and is below n² for all n above 100, so the shape counts and constants do not.
Try it
Find c and n₀ that show 2n + 10 is O(n).Answer
c = 3 and n₀ = 10, since 2n + 10 ≤ 3n means n ≥ 10
Hint: Choose c = 3 and solve for n
Show that n² + 6n is Θ(n²) by giving c₁, c₂ and n₀.Answer
c₁ = 1, c₂ = 7 and n₀ = 1, since n² ≤ n² + 6n ≤ 7n² for n ≥ 1
Hint: 6n ≤ 6n² for n ≥ 1
What is Θ of 5n + n log₂ n + 40, and why?Answer
Θ(n log₂ n), since n log₂ n outgrows both 5n and 40
Hint: Keep the fastest term, drop constants
Keep f(n) = 3n² + 5n but take a larger constant, c = 8. What n₀ works?Answer
n₀ = 1, because 3n² + 5n ≤ 8n² means 5n ≤ 5n², which holds for every n ≥ 1.
At n = 10, where is the strip, that is, what are 3n², 3n² + 5n and 8n²?Answer
300, 350 and 800, and 300 ≤ 350 ≤ 800.
