Sub topic 7
Assumptions about which inputs are likely
This chapter assumes you can read Python loops, index lists, and sum series such as 1 + 2 + ... + n. It also assumes you know that the best and worst case are the cheapest and most expensive inputs of a given size.
Why the average needs an assumption
The best case and worst case each pick one input, so they need no extra information. The average case does not. "Average over what?" has no answer until you say how likely each input is.
That statement is the input distribution. It assigns a probability to every possible input of size n. The average case is the cost of the algorithm weighted by those probabilities. Change the distribution and you change the average.
A common default: uniform over positions
For searching, a standard assumption is that the target is present and equally likely to be at any of the n positions. Each position then has probability 1/n.
A different assumption gives a different answer. Suppose the target is absent half the time. Then the absent inputs each cost n comparisons, and the average moves toward n.
def linear_search(items, target):
comparisons = 0
for i, x in enumerate(items):
comparisons += 1
if x == target:
return i, comparisons
return -1, comparisons
n = 10
items = list(range(n))
# Assumption A: target present, uniform over positions
present = sum(linear_search(items, t)[1] for t in items) / n
# Assumption B: half the queries are for an absent value
absent_cost = linear_search(items, -1)[1]
mixed = 0.5 * present + 0.5 * absent_cost
print(present) # 5.5
print(mixed) # 7.75Both numbers are correct averages. They answer different questions because they rest on different assumptions.
Common errors
- Treating "average" as "typical" without checking. Real data is often skewed. Search queries may repeat the same few keys, and a uniform assumption would then be wrong.
- Using uniform over values when you mean uniform over orderings. For sorting, the usual assumption is that all n! orderings of distinct values are equally likely. Inputs with many duplicates or nearly sorted data break that assumption.
- Leaving the assumption out. A claim such as "linear search averages (n + 1) / 2 comparisons" is incomplete. It holds only when the target is present and its position is uniform.
- Assuming independence you do not have. If one query predicts the next, per-query averages may not describe the whole workload.
How to state an assumption
Write the distribution as part of the result. A good statement names the input space, the probability of each element, and the cost measured. For example: "Over all n! orderings of distinct values, each equally likely, insertion sort does on average n(n-1)/4 comparisons."
When you cannot justify a distribution, the worst case is the safer claim because it needs none.
Exercise
Linear search runs on a list of n = 4 items. The target is present with probability 3/4, and when present it is equally likely at each of the 4 positions. When absent, the search costs 4 comparisons. What is the average number of comparisons?
Show answer
When present, the average is (1 + 2 + 3 + 4) / 4 = 2.5. When absent, it is 4. The weighted average is 0.75 × 2.5 + 0.25 × 4 = 1.875 + 1 = 2.875 comparisons.