To compare growth rates we need a precise language. Big-O, Omega (Ω) and Theta (Θ) let us say “this function grows no faster than”, “no slower than” or “at the same rate as” another function, while ignoring constant factors and small inputs.
Intuition
Compare a cycle and a car on a trip. For a 100-metre trip, the cycle may even win (no parking, no traffic lights). For a 500-km trip the car always wins. Asymptotic notation only cares about the long trip: what happens when n n n becomes large. The constants c c c and n 0 n_0 n 0 in the definitions say “after some starting point n 0 n_0 n 0 , and allowing a fixed factor c c c ”.
Big-O: an upper bound
Definition 1.5 · Big-O notation
For functions f ( n ) f(n) f ( n ) and g ( n ) g(n) g ( n ) , we write f ( n ) = O ( g ( n ) ) f(n) = O(g(n)) f ( n ) = O ( g ( n )) if there exist positive constants c c c and n 0 n_0 n 0 such that
0 ≤ f ( n ) ≤ c ⋅ g ( n ) for all n ≥ n 0 . 0 \le f(n) \le c \cdot g(n) \quad \text{for all } n \ge n_0. 0 ≤ f ( n ) ≤ c ⋅ g ( n ) for all n ≥ n 0 .
In words: from n 0 n_0 n 0 onwards, f f f never rises above c c c times g g g . The upper bound g g g may be loose: 3 n + 4 3n + 4 3 n + 4 is O ( n ) O(n) O ( n ) , but also O ( n 2 ) O(n^2) O ( n 2 ) and O ( n 3 ) O(n^3) O ( n 3 ) . We normally give the smallest (tightest) one.
Example 1.6 Prove 3n + 4 = O(n)
Goal. Find positive constants c c c and n 0 n_0 n 0 with 3 n + 4 ≤ c ⋅ n 3n + 4 \le c \cdot n 3 n + 4 ≤ c ⋅ n for all n ≥ n 0 n \ge n_0 n ≥ n 0 .
Pick c. Try c = 4 c = 4 c = 4 . We need 3 n + 4 ≤ 4 n 3n + 4 \le 4n 3 n + 4 ≤ 4 n .
Solve for n. Subtract 3 n 3n 3 n from both sides: 4 ≤ n 4 \le n 4 ≤ n . So the inequality holds for every n ≥ 4 n \ge 4 n ≥ 4 .
Check one value. At n = 4 n = 4 n = 4 : 3 ( 4 ) + 4 = 16 3(4) + 4 = 16 3 ( 4 ) + 4 = 16 and 4 ⋅ 4 = 16 4 \cdot 4 = 16 4 ⋅ 4 = 16 , so 16 ≤ 16 16 \le 16 16 ≤ 16 holds.
Result. With c = 4 c = 4 c = 4 and n 0 = 4 n_0 = 4 n 0 = 4 , 3 n + 4 = O ( n ) 3n + 4 = O(n) 3 n + 4 = O ( n ) .
Omega: a lower bound
Definition 1.7 · Omega notation
We write f ( n ) = Ω ( g ( n ) ) f(n) = \Omega(g(n)) f ( n ) = Ω ( g ( n )) if there exist positive constants c c c and n 0 n_0 n 0 such that
0 ≤ c ⋅ g ( n ) ≤ f ( n ) for all n ≥ n 0 . 0 \le c \cdot g(n) \le f(n) \quad \text{for all } n \ge n_0. 0 ≤ c ⋅ g ( n ) ≤ f ( n ) for all n ≥ n 0 .
In words: from n 0 n_0 n 0 onwards, f f f never falls below c c c times g g g . This is a lower bound . For example, 3 n + 4 ≥ 3 n 3n + 4 \ge 3n 3 n + 4 ≥ 3 n for all n ≥ 1 n \ge 1 n ≥ 1 , so 3 n + 4 = Ω ( n ) 3n + 4 = \Omega(n) 3 n + 4 = Ω ( n ) with c = 3 c = 3 c = 3 , n 0 = 1 n_0 = 1 n 0 = 1 .
Correction · Omega needs a positive constant
Study material says: In §1.2, the set definition of Ω(g(n)) says “there exists a constant c and n0 ≥ 0”, without saying that c must be positive.
Correct version: Ω ( g ( n ) ) = { f ( n ) : there exist positive constants c and n 0 such that 0 ≤ c g ( n ) ≤ f ( n ) for all n ≥ n 0 } \Omega(g(n)) = \{ f(n) : \text{there exist positive constants } c \text{ and } n_0 \text{ such that } 0 \le c\,g(n) \le f(n) \text{ for all } n \ge n_0 \} Ω ( g ( n )) = { f ( n ) : there exist positive constants c and n 0 such that 0 ≤ c g ( n ) ≤ f ( n ) for all n ≥ n 0 } .
Why: If c = 0 c = 0 c = 0 were allowed, 0 ≤ f ( n ) 0 \le f(n) 0 ≤ f ( n ) would always hold, so every non-negative function would be Ω \Omega Ω of every other function and the notation would say nothing. Cormen et al., Introduction to Algorithms , §3.1, require c > 0 c > 0 c > 0 .
Theta: a tight bound
Definition 1.8 · Theta notation
We write f ( n ) = Θ ( g ( n ) ) f(n) = \Theta(g(n)) f ( n ) = Θ ( g ( n )) if there exist positive constants c 1 c_1 c 1 , c 2 c_2 c 2 and n 0 n_0 n 0 such that
0 ≤ c 1 ⋅ g ( n ) ≤ f ( n ) ≤ c 2 ⋅ g ( n ) for all n ≥ n 0 . 0 \le c_1 \cdot g(n) \le f(n) \le c_2 \cdot g(n) \quad \text{for all } n \ge n_0. 0 ≤ c 1 ⋅ g ( n ) ≤ f ( n ) ≤ c 2 ⋅ g ( n ) for all n ≥ n 0 .
Θ \Theta Θ “sandwiches” f f f between two multiples of g g g . Then g g g is called an asymptotically tight bound for f f f : f f f and g g g grow at the same rate.
Theorem 1.9 · Theta is Big-O plus Omega
f ( n ) = Θ ( g ( n ) ) f(n) = \Theta(g(n)) f ( n ) = Θ ( g ( n )) if and only if f ( n ) = O ( g ( n ) ) f(n) = O(g(n)) f ( n ) = O ( g ( n )) and f ( n ) = Ω ( g ( n ) ) f(n) = \Omega(g(n)) f ( n ) = Ω ( g ( n )) .
Big-O Omega (Ω) Theta (Θ) Kind of bound Upper Lower Tight (both) Inequality after n 0 n_0 n 0 f ≤ c g f \le c\,g f ≤ c g f ≥ c g f \ge c\,g f ≥ c g c 1 g ≤ f ≤ c 2 g c_1 g \le f \le c_2 g c 1 g ≤ f ≤ c 2 g Rough analogy f f f ”≤” g g g f f f ”≥” g g g f f f ”=” g g g 3 n + 4 3n + 4 3 n + 4 is …O ( n ) O(n) O ( n ) , O ( n 2 ) O(n^2) O ( n 2 ) , …Ω ( n ) \Omega(n) Ω ( n ) , Ω ( 1 ) \Omega(1) Ω ( 1 ) , …only Θ ( n ) \Theta(n) Θ ( n )
Example 1.10 Prove n²/3 − 4n = Θ(n²)
Goal. Find positive c 1 c_1 c 1 , c 2 c_2 c 2 , n 0 n_0 n 0 with c 1 n 2 ≤ 1 3 n 2 − 4 n ≤ c 2 n 2 c_1 n^2 \le \frac{1}{3}n^2 - 4n \le c_2 n^2 c 1 n 2 ≤ 3 1 n 2 − 4 n ≤ c 2 n 2 for all n ≥ n 0 n \ge n_0 n ≥ n 0 .
Divide by n². For n > 0 n > 0 n > 0 this gives c 1 ≤ 1 3 − 4 n ≤ c 2 c_1 \le \frac{1}{3} - \frac{4}{n} \le c_2 c 1 ≤ 3 1 − n 4 ≤ c 2 .
Right-hand side. 1 3 − 4 n \frac{1}{3} - \frac{4}{n} 3 1 − n 4 is always less than 1 3 \frac{1}{3} 3 1 , so c 2 = 1 3 c_2 = \frac{1}{3} c 2 = 3 1 works for every n ≥ 1 n \ge 1 n ≥ 1 .
Left-hand side. 1 3 − 4 n \frac{1}{3} - \frac{4}{n} 3 1 − n 4 grows as n n n grows. At n = 13 n = 13 n = 13 it equals 13 − 12 39 = 1 39 \frac{13 - 12}{39} = \frac{1}{39} 39 13 − 12 = 39 1 , and for larger n n n it is bigger. So c 1 = 1 39 c_1 = \frac{1}{39} c 1 = 39 1 works for every n ≥ 13 n \ge 13 n ≥ 13 .
Check with a program. Multiplying every term by 39 keeps whole numbers: we need n 2 ≤ 13 n 2 − 156 n ≤ 13 n 2 n^2 \le 13n^2 - 156n \le 13n^2 n 2 ≤ 13 n 2 − 156 n ≤ 13 n 2 .
C · theta_check.c Copy #include <stdio.h>
/* Check c1*g(n) <= f(n) <= c2*g(n) for f(n) = n*n/3 - 4n, g(n) = n*n,
c1 = 1/39 and c2 = 1/3. We multiply every term by 39 so that we can
work with whole numbers: n*n <= 13*n*n - 156*n <= 13*n*n */
int main( void )
{
printf( " n 39*c1*g(n) 39*f(n) 39*c2*g(n) holds? \n " );
for ( long n = 10 ; n <= 16 ; n ++ ) {
long low = n * n;
long mid = 13 * n * n - 156 * n;
long high = 13 * n * n;
printf( "%3ld %12ld %9ld %12ld %s \n " , n, low, mid, high,
(low <= mid && mid <= high) ? "yes" : "no" );
}
return 0 ;
}
Output Copy n 39*c1*g(n) 39*f(n) 39*c2*g(n) holds?
10 100 -260 1300 no
11 121 -143 1573 no
12 144 0 1872 no
13 169 169 2197 yes
14 196 364 2548 yes
15 225 585 2925 yes
16 256 832 3328 yes
The sandwich fails for n ≤ 12 n \le 12 n ≤ 12 and holds from n = 13 n = 13 n = 13 , exactly as calculated.
Result. 1 3 n 2 − 4 n = Θ ( n 2 ) \frac{1}{3}n^2 - 4n = \Theta(n^2) 3 1 n 2 − 4 n = Θ ( n 2 ) with c 1 = 1 39 c_1 = \frac{1}{39} c 1 = 39 1 , c 2 = 1 3 c_2 = \frac{1}{3} c 2 = 3 1 , n 0 = 13 n_0 = 13 n 0 = 13 .
Showing that a bound is false
To show f ( n ) ≠ O ( g ( n ) ) f(n) \ne O(g(n)) f ( n ) = O ( g ( n )) , assume constants exist and reach a contradiction.
Example 1.11 Show 6n³ is not Θ(n²)
Goal. Show that no constants make 6 n 3 ≤ c n 2 6n^3 \le c\,n^2 6 n 3 ≤ c n 2 true for all large n n n (then the upper half of Θ fails).
Assume the opposite. Suppose 6 n 3 ≤ c n 2 6n^3 \le c\,n^2 6 n 3 ≤ c n 2 for all n ≥ n 0 n \ge n_0 n ≥ n 0 .
Divide by 6 n 2 6n^2 6 n 2 . This gives n ≤ c 6 n \le \frac{c}{6} n ≤ 6 c .
Contradiction. c 6 \frac{c}{6} 6 c is a fixed number, but n n n can be chosen larger than both n 0 n_0 n 0 and c 6 \frac{c}{6} 6 c , for example n = n 0 + c n = n_0 + c n = n 0 + c . Then n ≤ c 6 n \le \frac{c}{6} n ≤ 6 c is false.
Result. 6 n 3 ≠ O ( n 2 ) 6n^3 \ne O(n^2) 6 n 3 = O ( n 2 ) , so 6 n 3 ≠ Θ ( n 2 ) 6n^3 \ne \Theta(n^2) 6 n 3 = Θ ( n 2 ) .
O, Ω, Θ and the cases of an algorithm
Correction · Big-O is not 'the worst case' and Omega is not 'the best case'
Study material says: §1.2 states that Big-O “is often used to describe the worst case running time” and that Ω “is used to bound the best case running time of an algorithm”, which suggests that each notation belongs to one case.
Correct version: O, Ω and Θ are bounds on functions . Worst, best and average case are different functions of n n n . Each one can be described with any of the three notations. For insertion sort: the worst-case time is Θ ( n 2 ) \Theta(n^2) Θ ( n 2 ) (so it is both O ( n 2 ) O(n^2) O ( n 2 ) and Ω ( n 2 ) \Omega(n^2) Ω ( n 2 ) ), and the best-case time is Θ ( n ) \Theta(n) Θ ( n ) .
Why: Cormen et al., Introduction to Algorithms , §3.1, define all three notations for arbitrary functions and give exactly this example: the worst-case running time of insertion sort is Ω ( n 2 ) \Omega(n^2) Ω ( n 2 ) as well as O ( n 2 ) O(n^2) O ( n 2 ) . Mixing notation with case leads to wrong statements such as “insertion sort is Ω(n), so it is fast”.
Note
There are also strict versions: little-o (f f f grows strictly slower than g g g , e.g. n = o ( n 2 ) n = o(n^2) n = o ( n 2 ) ) and little-omega. They are rarely needed in this course.
Key idea
O gives an upper bound, Ω a lower bound and Θ a tight bound, each “up to a constant factor c c c , for all n ≥ n 0 n \ge n_0 n ≥ n 0 ”; to prove one, you must show concrete constants that work.
TEE Exam note
For “Define Big-O / Ω / Θ” write the set definition with “positive constants c c c (or c 1 c_1 c 1 , c 2 c_2 c 2 ) and n 0 n_0 n 0 ”, draw the matching sketch (f below / above / between the c·g curves, with n 0 n_0 n 0 marked) and give one numeric example such as 3 n + 4 = O ( n ) 3n + 4 = O(n) 3 n + 4 = O ( n ) with c = 4 c = 4 c = 4 , n 0 = 4 n_0 = 4 n 0 = 4 . For “prove that …” questions, marks are given for choosing constants, showing the algebra and checking the inequality holds from n 0 n_0 n 0 .
Common mistake
Reading f ( n ) = O ( g ( n ) ) f(n) = O(g(n)) f ( n ) = O ( g ( n )) as a true equality. It means ”f f f belongs to the set O ( g ( n ) ) O(g(n)) O ( g ( n )) ”. You cannot reverse it: n = O ( n 2 ) n = O(n^2) n = O ( n 2 ) is true, but "O ( n 2 ) = n O(n^2) = n O ( n 2 ) = n " makes no sense.
Common mistake
Forgetting n 0 n_0 n 0 . "n 2 ≤ 10 n n^2 \le 10n n 2 ≤ 10 n " is false for n = 20 n = 20 n = 20 , so n 2 ≠ O ( n ) n^2 \ne O(n) n 2 = O ( n ) , even though it holds for n ≤ 10 n \le 10 n ≤ 10 . The bound must hold for all n ≥ n 0 n \ge n_0 n ≥ n 0 .
Key takeaways
f = O ( g ) f = O(g) f = O ( g ) : f ≤ c g f \le c\,g f ≤ c g for all n ≥ n 0 n \ge n_0 n ≥ n 0 (upper bound).
f = Ω ( g ) f = \Omega(g) f = Ω ( g ) : f ≥ c g f \ge c\,g f ≥ c g for all n ≥ n 0 n \ge n_0 n ≥ n 0 (lower bound), with c > 0 c > 0 c > 0 .
f = Θ ( g ) f = \Theta(g) f = Θ ( g ) : both at once; g g g is a tight bound.
To prove a bound, find constants; to disprove it, show any constants lead to a contradiction.
The notations describe functions; worst, best and average case are separate functions.
Practice questions Answer on paper first, then check. The explanation matters more than the letter you chose.
Question 1 · multiple choice To prove 5 n + 20 = O ( n ) 5n + 20 = O(n) 5 n + 20 = O ( n ) with c = 6 c = 6 c = 6 , what is the smallest valid n 0 n_0 n 0 ?
A 4 B 6 C 10 D 20
Check answer
Show answer Answer: D. 20
5 n + 20 ≤ 6 n 5n + 20 \le 6n 5 n + 20 ≤ 6 n means 20 ≤ n 20 \le n 20 ≤ n , so n 0 = 20 n_0 = 20 n 0 = 20 . With n 0 = 4 n_0 = 4 n 0 = 4 the check fails, e.g. 5 ( 4 ) + 20 = 40 > 24 5(4) + 20 = 40 > 24 5 ( 4 ) + 20 = 40 > 24 .
Question 2 · multiple choice Which statement is false?
Check answer
Show answer Answer: B. n 2 = O ( n ) n^2 = O(n) n 2 = O ( n )
n 2 ≤ c n n^2 \le c\,n n 2 ≤ c n would mean n ≤ c n \le c n ≤ c for all large n n n , which is impossible. n = O ( n 2 ) n = O(n^2) n = O ( n 2 ) is true because Big-O may be a loose upper bound.
Question 3 · multiple choice Which function is in Ω ( n 2 ) \Omega(n^2) Ω ( n 2 ) ?
Check answer
Show answer Answer: C. n 3 n^3 n 3
n 3 ≥ 1 ⋅ n 2 n^3 \ge 1 \cdot n^2 n 3 ≥ 1 ⋅ n 2 for all n ≥ 1 n \ge 1 n ≥ 1 , so n 3 = Ω ( n 2 ) n^3 = \Omega(n^2) n 3 = Ω ( n 2 ) . 100 n 100n 100 n is smaller than n 2 n^2 n 2 once n > 100 n > 100 n > 100 , so it is not Ω ( n 2 ) \Omega(n^2) Ω ( n 2 ) .
Question 4 · multiple choice If f ( n ) = O ( n 2 ) f(n) = O(n^2) f ( n ) = O ( n 2 ) and f ( n ) = Ω ( n 2 ) f(n) = \Omega(n^2) f ( n ) = Ω ( n 2 ) , then:
Check answer
Show answer Answer: A. f ( n ) = Θ ( n 2 ) f(n) = \Theta(n^2) f ( n ) = Θ ( n 2 )
By the theorem, Big-O and Omega with the same g g g together give Theta.
Question 5 · multiple choice The worst-case running time of insertion sort is Θ ( n 2 ) \Theta(n^2) Θ ( n 2 ) . Which statement is correct?
Check answer
Show answer Answer: C. Its worst case is both O ( n 2 ) O(n^2) O ( n 2 ) and Ω ( n 2 ) \Omega(n^2) Ω ( n 2 ) .
Θ means both bounds hold. The notations describe any function, including the worst-case function. Ω ( n 3 ) \Omega(n^3) Ω ( n 3 ) is false because n 2 n^2 n 2 is eventually smaller than c n 3 c\,n^3 c n 3 .
Question 6 · short answer
Prove that 2 n 2 + 3 n = Θ ( n 2 ) 2n^2 + 3n = \Theta(n^2) 2 n 2 + 3 n = Θ ( n 2 ) by finding c 1 c_1 c 1 , c 2 c_2 c 2 and n 0 n_0 n 0 .
Show worked answer Lower bound: 2 n 2 ≤ 2 n 2 + 3 n 2n^2 \le 2n^2 + 3n 2 n 2 ≤ 2 n 2 + 3 n for all n ≥ 1 n \ge 1 n ≥ 1 , so c 1 = 2 c_1 = 2 c 1 = 2 . Upper bound: 3 n ≤ 3 n 2 3n \le 3n^2 3 n ≤ 3 n 2 for n ≥ 1 n \ge 1 n ≥ 1 , so 2 n 2 + 3 n ≤ 2 n 2 + 3 n 2 = 5 n 2 2n^2 + 3n \le 2n^2 + 3n^2 = 5n^2 2 n 2 + 3 n ≤ 2 n 2 + 3 n 2 = 5 n 2 , giving c 2 = 5 c_2 = 5 c 2 = 5 . Hence 2 n 2 ≤ 2 n 2 + 3 n ≤ 5 n 2 2n^2 \le 2n^2 + 3n \le 5n^2 2 n 2 ≤ 2 n 2 + 3 n ≤ 5 n 2 for all n ≥ 1 n \ge 1 n ≥ 1 , so 2 n 2 + 3 n = Θ ( n 2 ) 2n^2 + 3n = \Theta(n^2) 2 n 2 + 3 n = Θ ( n 2 ) with c 1 = 2 c_1 = 2 c 1 = 2 , c 2 = 5 c_2 = 5 c 2 = 5 , n 0 = 1 n_0 = 1 n 0 = 1 .