Sub topic 1
Measuring Algorithm Cost: Input Size and Cases
Prerequisites
Assume you can write loops and functions, index arrays, and read simple summations. You should also know that an algorithm is a finite procedure that transforms input into output.
What algorithm cost means
Algorithm analysis estimates the resources an algorithm needs as the input grows. The main resources are:
- Time cost: the number of basic operations performed.
- Space cost: the additional memory used while running.
- Input size: a measure of how much data the algorithm receives.
The input size is not always the number of array elements. For an integer, its size may be the number of bits needed to represent it. For a graph, it is often described using both vertices and edges, written as \(V\) and \(E\).
A cost model treats a basic operation, such as a comparison, assignment, or array access, as taking constant time. The exact processor time is usually less important than how the operation count changes with input size.
| Case | Meaning | Typical use |
|---|---|---|
| Best case | Least work for any input of size \(n\) | Shows a favorable input |
| Worst case | Most work for any input of size \(n\) | Gives a guaranteed upper bound |
| Average case | Expected work under a stated input distribution | Estimates typical behavior |
Best, worst, and average cases
Consider searching for a key in an unsorted array. A linear search checks elements from left to right.
- In the best case, the key is the first element, so one comparison is enough.
- In the worst case, the key is last or absent, so all \(n\) elements are checked.
- Under a uniform position assumption, a successful search checks \((n+1)/2\) elements on average.
The average case is meaningful only when the probability distribution is clear. Do not call a result “average” merely because it feels typical.
def linear_search(values, key):
comparisons = 0
for index, value in enumerate(values):
comparisons += 1
if value == key:
return index, comparisons
return -1, comparisons
values = [8, 3, 12, 5, 9]
print(linear_search(values, 5)) # (3, 4)
print(linear_search(values, 20)) # (-1, 5)The function performs at most \(n\) comparisons. Its worst-case time grows linearly with \(n\). The returned comparison count makes the cost visible for particular inputs.
Counting the work
When analyzing a loop, count how many times its body executes. This loop performs \(n\) iterations:
def sum_values(values):
total = 0
for value in values:
total += value
return totalThe initialization and final return take constant work. The loop contributes \(n\) additions, so the total cost can be written as \(an+b\) for constants \(a\) and \(b\). Later, asymptotic notation will keep the growth term and ignore those constants.
Exercise
An array contains n elements. A search checks exactly the first n/2 elements and then stops without finding the key. How many element comparisons does it perform, and which case does this represent if the search could have continued through the entire array?
Show answer
It performs n/2 comparisons. Relative to a search that may inspect all n elements, this is neither the best case nor the worst case; it is a particular intermediate input case.