Appendix: Orders of Growth
Section 2.9 compared the efficiency of functions by measuring how long they took for many different inputs and graphing the results. The resulting graphs had a few characteristic shapes: a straight line for exp, a curve that flattens out for exp_fast, and a curve that bends upward for overlap. This appendix introduces the terminology that computer scientists use to describe these shapes precisely using upper and lower bounds.
Exactly determining just how much space or time will be used when calling a function is a very difficult task that depends upon many factors. A useful way to analyze a process is to categorize it along with a group of processes that all have similar requirements. A useful categorization is the order of growth of a process, which expresses in simple terms how the resource requirements of a process grow as a function of the input.
As an introduction to orders of growth, we will analyze the function count_factors below, which counts the number of positive integers that evenly divide an input n. The function attempts to divide n by every integer less than or equal to its square root. The implementation takes advantage of the fact that if
How much time is required to evaluate count_factors? The exact answer will vary on different machines, but we can make some useful general observations about the amount of computation involved. The total number of times this process executes the body of the while statement is the greatest integer less than while statement are executed exactly once. So, the total number of statements executed is while body and while statement. Although it isn’t exact, this formula generally characterizes how much time will be required to evaluate count_factors as a function of the input n.
A more exact description is difficult to obtain. The constants factors are sometimes executed but sometimes not. An order of growth analysis allows us to gloss over such details and instead focus on the general shape of growth. In particular, the order of growth for count_factors expresses in precise terms that the amount of time required to compute count_factors(n) scales at the rate
Theta Notation. Let
We say that
for any value of
- A lower bound
and - An upper bound
We can apply this definition to show that the number of steps required to evaluate count_factors(n) grows as
First, we choose count_factors(n) requires at least while statement, each of which takes at least 1 step to execute. There are at least two lines executed within the while body, along with the while header itself. All of these require at least one step. The while body is evaluated at least
Second, we can verify the upper bound. We assume that any single line in the body of count_factors requires at most count_factors(n) can require at most while statement and 4 within (including the header), and the header is evaluated one more time than the body. This upper bound holds even if every if header evaluates to true. Finally, if we choose
Therefore, the number of steps is always bounded below by count_factors(n) grows as
Example: Exponentiation
Section 2.9 introduced two functions for computing exp, follows the recursive definition
>>> def exp(b, n):
if n == 0:
return 1
else:
return b * exp(b, n-1)This is a linear recursive process that requires
>>> def exp_iter(b, n):
result = 1
for _ in range(n):
result = result * b
return resultThe second, exp_fast, uses successive squaring:
>>> def square(x):
return x * x
>>> def exp_fast(b, n):
if n == 0:
return 1
elif n % 2 == 0:
return square(exp_fast(b, n//2))
else:
return b * exp_fast(b, n-1)
>>> exp_fast(2, 100)
1267650600228229401496703205376The process evolved by exp_fast grows logarithmically with n in both space and number of steps. Computing exp_fast requires only one more multiplication than computing n grows about as fast as the logarithm of n base 2. The process has exp_fast for n of 1000 requires only 15 multiplications instead of 1000.
Growth Categories
Orders of growth are designed to simplify the analysis and comparison of computational processes. Many different processes can all have equivalent orders of growth, which indicates that they scale in similar ways. It is an essential skill of a computer scientist to know and recognize common orders of growth and identify processes of the same order.
Constants. Constant terms do not affect the order of growth of a process. So, for instance,
Logarithms. The base of a logarithm does not affect the order of growth of a process. For instance,
Nesting. When an inner computational process is repeated for each step in an outer process, then the order of growth of the entire process is a product of the number of steps in the outer and inner processes.
For example, this version of the overlap function from Section 2.9 uses the in operator to compute the number of items in list a that also appear in list b.
>>> def overlap(a, b):
count = 0
for item in a:
if item in b:
count += 1
return count
>>> overlap([1, 3, 2, 2, 5, 1], [5, 4, 2])
3The in operator for lists requires b. It is applied a. The item in b expression is the inner process, and the for item in a loop is the outer process. The total order of growth for this function is therefore
Lower-order terms. As the input to a process grows, the fastest growing part of a computation dominates the total resources used. Theta notation captures this intuition. In a sum, all but the fastest growing term can be dropped without changing the order of growth.
For instance, consider the one_more function that returns how many items of a list a are one more than some other item of a. That is, in the list [3, 14, 15, 9], the item 15 is one more than 14, so one_more will return 1.
>>> def one_more(a):
return overlap([x-1 for x in a], a)
>>> one_more([3, 14, 15, 9])
1There are two parts to this computation: the list comprehension and the call to overlap. For a list a of length overlap requires
Common categories. Given these equivalence properties, a small set of common categories emerges to describe most computational processes. The most common are listed below from slowest to fastest growth, along with descriptions of the growth as the input increases.
| Category | Theta Notation | Growth Description | Example |
|---|---|---|---|
| Constant | Growth is independent of the input | abs | |
| Logarithmic | Multiplying input increments resources | exp_fast | |
| Linear | Incrementing input increments resources | exp | |
| Quadratic | Incrementing input adds n resources | one_more | |
| Exponential | Incrementing input multiplies resources | fib |
Other categories exist, such as the count_factors. However, these categories are particularly common.
Exponential growth describes many different orders of growth, because changing the base fib grows exponentially in its input
where
It turns out that the number of calls made by fib(n) is