Sub topic 1
What counts as input size
Prerequisites
This chapter assumes you can read a loop and a conditional, index into an array or list, and add up simple sums such as 1 + 2 + ... + n. It also assumes you know what an algorithm is: a fixed procedure that turns an input into an output.
Why running time depends on a size
An algorithm that searches a list of 10 items does less work than the same algorithm on a list of 10 million items. To compare algorithms fairly, we describe their cost as a function of the input size, written n. A function lets us ask how the cost grows as the input grows, which is what matters in practice.
Input size is a number that measures how much data the algorithm must process. It is not the values in the input. Choosing the right measure is the first step of any analysis.
Common choices for n
The right measure depends on the problem. The table lists the usual ones.
| Problem | Input size n |
|---|---|
| Search or sort a list | Number of elements in the list |
| Find a path in a graph | Number of vertices and edges |
| Multiply two big integers | Number of digits (or bits) in each number |
| Process a string | Number of characters |
| Check whether a number is prime | Number of bits in the number, not its value |
The last row is a common source of confusion. A number like 1,000,000 takes only about 20 bits to write down. If an algorithm loops up to the value of the number, its cost is proportional to the value, but that value can be exponentially larger than the bit length. So the same algorithm looks fast or slow depending on which measure you call n. State your measure explicitly.
Size is not the whole story
Two inputs can have the same size and still cost different amounts. Consider searching a list of five items for a target.
def linear_search(items, target):
comparisons = 0
for i, x in enumerate(items):
comparisons += 1
if x == target:
return i, comparisons
return -1, comparisons
items = [7, 3, 9, 1, 5]
print(linear_search(items, 7)) # target is first: 1 comparison
print(linear_search(items, 5)) # target is last: 5 comparisons
print(linear_search(items, 42)) # target is absent: 5 comparisonsIn all three calls n = 5. The list has the same length each time, yet the work ranges from 1 comparison to 5. The size fixes how many items exist. It does not fix which item you are looking for or where it sits.
This gap is the reason for the rest of the tutorial. Once we fix n, the cost can still vary across inputs, and the best, worst, and average cases are three ways of summarizing that variation.
Exercise
An algorithm checks whether an integer m is prime by trying every divisor from 2 up to m - 1. Someone says it runs in time proportional to n, where n is the input size. Under which definition of n is that claim true, and why is it misleading if n is the number of bits in m?
Show answer
The claim is true if n means the value m itself, because the loop runs about m times. If n is the number of bits, then m can be about 2^n, so the loop runs about 2^n times, which is exponential in the input size, not linear.