Sub topic 1
Measuring Algorithm Cost: Input Size and Cases
Prerequisites
This chapter assumes you can write loops, functions, and recursion in Python. You should also understand arrays and indexing, logarithms, and simple summations.
Measuring Algorithm Cost
Measure cost by counting basic operations, such as comparisons and array reads, as a function of the input size n. Counting operations instead of seconds keeps the result the same on any machine.
Cost also depends on the input. The best case is the cheapest input, the worst case is the most expensive input, and the average case is the expected cost over a chosen distribution of inputs. GATE questions usually ask for the worst case unless they state otherwise.
Asymptotic Notation: O, Omega, and Theta
- f(n) = O(g(n)) means f grows no faster than g. This is an upper bound.
- f(n) = Omega(g(n)) means f grows at least as fast as g. This is a lower bound.
- f(n) = Theta(g(n)) means f is bounded above and below by g, so both grow at the same rate.
Drop constants and lower-order terms. For example, 3n² + 5n + 7 is Theta(n²).
Comparing Growth Rates
From slowest to fastest, common rates are: 1, log n, n, n log n, n², 2ⁿ. Doubling n doubles n and n log n, multiplies n² by 4, and multiplies 2ⁿ by 2ⁿ.
Analyzing Loops and Nested Loops
A single loop that runs n times costs Theta(n). Nested loops multiply their counts.
def count_pairs(a):
total = 0
for i in range(len(a)):
for j in range(i, len(a)):
total += a[i] * a[j]
return totalThe inner loop runs n − i times. The sum of (n − i) over all i is n(n+1)/2, so the cost is Theta(n²).
Recurrence Relations
Divide-and-conquer algorithms define their cost recursively. Binary search costs T(n) = T(n/2) + c. Expanding k times gives T(n) = T(n/2^k) + kc. Stopping at n/2^k = 1 gives k = log₂ n, so the cost is Theta(log n).
A recursion tree shows the same result. Each level costs a fixed amount, and there are log n levels.
The Master Theorem
For T(n) = aT(n/b) + f(n) with a ≥ 1 and b > 1, compare f(n) with n^(log_b a):
- If f(n) is polynomially smaller, T(n) = Theta(n^(log_b a)).
- If f(n) matches it, T(n) = Theta(n^(log_b a) · log n).
- If f(n) is polynomially larger and satisfies the regularity condition, T(n) = Theta(f(n)).
| Recurrence | log_b a | Case | Result |
|---|---|---|---|
| T(n) = 2T(n/2) + n | 1 | 2 | Theta(n log n) |
| T(n) = T(n/2) + 1 | 0 | 2 | Theta(log n) |
| T(n) = 4T(n/2) + n | 2 | 1 | Theta(n²) |
Linear Search
Linear search checks each element in order.
def linear_search(a, key):
for i in range(len(a)):
if a[i] == key:
return i
return -1The best case is Theta(1), when the key is first. The worst case is Theta(n), when the key is last or absent. For a key present at a uniformly random position, the average is (n + 1)/2 comparisons, which is Theta(n).
Exercise
Using the Master theorem, what is the worst-case time of T(n) = 2T(n/2) + n²?
Show answer
Here a = 2, b = 2, so n^(log_b a) = n. Since f(n) = n² is polynomially larger than n, case 3 applies (the regularity condition holds). The result is Theta(n²).