Sub topic 13
A Case Is Not a Bound: Avoiding a Common Confusion
This chapter assumes you know how to count basic operations. It also assumes you have seen best, worst, and average case counts for linear search and insertion sort. Big-O, Omega, and Theta are used here only at the level of "upper, lower, and tight bound on a growth rate."
Two separate choices
Two ideas are often merged into one:
- A case selects which inputs of size n you measure: the cheapest, the most expensive, or an average over a distribution. Each case gives its own running time function, such as Tbest(n), Tworst(n), or T_avg(n).
- A bound describes how a function grows. O gives an upper bound, Omega gives a lower bound, and Theta gives both.
You make the choices in order. First pick a case to get a function. Then describe that function with a bound.
Seeing it with insertion sort
This code counts comparisons for the best and worst inputs at several sizes.
def insertion_sort_comparisons(values):
a = list(values)
comparisons = 0
for i in range(1, len(a)):
key = a[i]
j = i - 1
while j >= 0:
comparisons += 1
if a[j] > key:
a[j + 1] = a[j]
j -= 1
else:
break
a[j + 1] = key
return comparisons
for n in (10, 100, 1000):
best = insertion_sort_comparisons(range(n))
worst = insertion_sort_comparisons(range(n, 0, -1))
print(n, best, worst)The best case gives n - 1 and the worst case gives n(n-1)/2. Both functions can be described with a tight bound:
| Case | Function | Tight bound |
|---|---|---|
| Best | n - 1 | Theta(n) |
| Worst | n(n-1)/2 | Theta(n^2) |
Each case has its own Theta. The best case is not "Omega" and the worst case is not "O." Theta(n) and Theta(n^2) are both exact descriptions, of different functions.
What you can say about the algorithm as a whole
If you want one statement covering every input of size n, the bounds apply to the whole family of inputs:
- "Insertion sort is O(n^2)" is true, because the worst case is the largest cost on any input.
- "Insertion sort is Omega(n)" is true, because no input costs less than the best case.
- "Insertion sort is Theta(n^2)" is false as a statement about all inputs, because the best case is only linear.
- "Insertion sort is Omega(n^2)" is false for the same reason, even though it is true of the worst case.
Quick check before you write a claim
- Name the case: best, average, worst, or "any input."
- Write the function for that case.
- Choose the bound that matches what you can prove about that function.
A statement like "the worst case of linear search is Theta(n)" names its case and its bound. A statement like "linear search is O(n)" is also correct, but it leaves the case unnamed.
Exercise
Insertion sort on already sorted input makes n - 1 comparisons. A student says "Insertion sort is Theta(n), so it is a linear time algorithm." What is wrong, and what could the student correctly say?
Show answer
Theta(n) describes only the best case function n - 1. On reversed input the cost is n(n-1)/2, which grows as n^2, so Theta(n) is not a bound on the algorithm over all inputs. The student could say the best case is Theta(n), the worst case is Theta(n^2), and the running time on any input is O(n^2) and Omega(n).