Sub topic 2
Asymptotic Notation: O, Omega, and Theta
Assume you know loops, recursion, logarithms, and basic summations. You should also know that an algorithm’s running time depends on input size, usually written as \(n\). This chapter uses asymptotic notation to describe how that time grows as \(n\) becomes large.
Why Asymptotic Notation Matters
Exact running time depends on hardware, programming language, and implementation details. Asymptotic analysis ignores these details and focuses on the dominant growth rate.
For example, an algorithm that performs \(3n^2 + 5n + 7\) operations is described as quadratic. When \(n\) is large, the \(n^2\) term dominates the smaller terms and constants.
O, Omega, and Theta
Big-O notation gives an asymptotic upper bound. If an algorithm takes \(O(f(n))\) time, its growth is no faster than a constant multiple of \(f(n)\), after sufficiently large inputs.
Big-Omega gives an asymptotic lower bound. If an algorithm takes \(\Omega(f(n))\) time, it requires at least a constant multiple of \(f(n)\) operations for sufficiently large inputs.
Big-Theta gives a tight bound. If an algorithm takes \(\Theta(f(n))\) time, both its upper and lower bounds grow like \(f(n)\).
| Notation | Meaning | Example |
|---|---|---|
| \(O(f(n))\) | At most this growth rate | \(3n+2 = O(n)\) |
| \(\Omega(f(n))\) | At least this growth rate | \(3n+2 = \Omega(n)\) |
| \(\Theta(f(n))\) | Exactly this asymptotic growth rate | \(3n+2 = \Theta(n)\) |
A statement can have several valid bounds. For example, \(n\) is also \(O(n^2)\), but \(\Theta(n)\) is more precise. In GATE questions, distinguish a valid upper bound from the tight bound.
Reading Loops
def count_steps(n):
total = 0
for i in range(n):
total += 1
for j in range(n * n):
total += 1
return totalThe first loop runs \(n\) times. The second runs \(n^2\) times. The total is \(n+n^2\), so the function takes \(\Theta(n^2)\) time. It is also \(O(n^2)\) and \(\Omega(n^2)\).
For sequential sections, add costs and keep the fastest-growing term. For nested loops, multiply iteration counts when the bounds are independent.
Related Growth Notation
Little-o, written \(o(f(n))\), means a function grows strictly slower than \(f(n)\). For example, \(n=o(n^2)\).
Little-omega, written \(\omega(f(n))\), means a function grows strictly faster than \(f(n)\). For example, \(n^2=\omega(n)\).
Exercise
What is the tight asymptotic complexity of a loop whose body runs once for each pair (i, j), where both i and j range from 0 through n-1?
Show answer
There are n choices for i and n choices for j, so the body runs n squared times. The complexity is Theta(n^2).