Growth Rates: What Complexity Means in Practice
Asymptotic notation gives us a precise language. This lesson turns it into practical intuition: what the most common complexity classes mean in real time, how to predict the time an algorithm will need for a larger input, how to estimate the largest input it can handle in a given time, and why no improvement in hardware can change the complexity of an algorithm.
Asymptotic analysis allows us:
- to measure the inherent difficulty of a problem;
- to estimate the time and space required by an algorithm before running it;
- to compare algorithms that solve the same problem in terms of efficiency.
- The main complexity classes, from to , and which ones are acceptable in practice.
- How dramatically the classes differ when grows, and where the "small " exceptions are.
- How to calculate the expected execution time for a new problem size, and the largest problem size solvable in a given time.
- The principle of invariance and its consequences.
The Basic Complexity Classes
| Class | Name | Comment | Typical example |
|---|---|---|---|
| Constant | Optimal, but almost always impossible for problems that must look at their input. | Accessing an element of an array by position. | |
| Logarithmic | Excellent: the time barely grows. | Binary search in a sorted array. | |
| Linear | A good and quite usual complexity. Often optimal, since reading the input already takes steps. | Sequential search; maximum of a vector. | |
| Linearithmic | A good complexity, typical of efficient recursive (divide and conquer) algorithms. | Mergesort; Quicksort on average. | |
| Quadratic | Appears with two nested loops and similar cases. Acceptable for moderate sizes. | Simple sorting algorithms (bubble, insertion, selection). | |
| Cubic | Grows excessively for large values. | Naive multiplication of matrices. | |
| Polynomial | General case of polynomial complexity, for a constant . | Checking all -element subsets of a set; nested loops. | |
| Exponential | Avoid it if possible. | Generating all the subsets of a set. | |
| Exponential | General case of exponential complexity, for a constant . | Graph -coloring by exhaustive search. | |
| Factorial | A bad complexity; it should be avoided. | Generating all the permutations of elements. | |
| Super-exponential | A bad complexity; it should be avoided. | Generating all -tuples over values (all functions from a set of size to itself). |
The classes up to are polynomial; from onwards they are not polynomial. This border is very significant in computer science: problems that can be solved in polynomial time are usually considered tractable, while those that seem to require exponential time are considered intractable for large inputs. Some of the most famous open questions in computer science (such as whether P equals NP) are about exactly this border. Several design techniques of this course, such as backtracking and branch and bound, exist precisely to deal with problems for which no polynomial algorithm is known, trying to avoid exploring the whole exponential search space.
How Fast Do They Grow?
The following table shows the time that an algorithm would need for different input sizes, assuming that each elementary operation takes one nanosecond (a billion operations per second, a reasonable order of magnitude for a modern processor executing simple instructions):
| 10 | 3 ns | 10 ns | 33 ns | 100 ns | 1 µs | 1 µs | 4 ms |
| 20 | 4 ns | 20 ns | 86 ns | 400 ns | 8 µs | 1 ms | 77 years |
| 50 | 6 ns | 50 ns | 282 ns | 2.5 µs | 125 µs | 13 days | years |
| 100 | 7 ns | 100 ns | 664 ns | 10 µs | 1 ms | years | years |
| 1,000 | 10 ns | 1 µs | 10 µs | 1 ms | 1 s | years | — |
| 10,000 | 13 ns | 10 µs | 133 µs | 100 ms | 17 min | — | — |
| 1,000,000 | 20 ns | 1 ms | 20 ms | 17 min | 32 years | — | — |
For comparison, the age of the universe is about years. Some observations:
- Logarithmic algorithms are practically insensitive to the size: going from a thousand to a million elements only doubles the time.
- Linear and linearithmic algorithms handle millions of elements in milliseconds.
- Quadratic algorithms are fine for thousands of elements but take minutes for millions.
- Exponential and factorial algorithms are only usable for very small inputs. Note that goes from a millisecond to 13 days when goes from 20 to 50.
How large a problem can we solve in one second?
Reversing the question gives a very practical rule of thumb. With about simple operations per second:
| Complexity | Largest in about one second |
|---|---|
| about | |
| about | |
| about | |
| about | |
| about | |
| about |
Real programs perform several machine instructions per "operation", access memory, allocate objects... so these limits are optimistic, but the orders of magnitude are very useful. Competitive programmers use exactly this kind of table to decide, from the size of the input, which complexity their solution needs.
Small values of
The asymptotic order describes what happens for large , and for small values the picture can be surprising. Compare, for example, with , or with (which is the same comparison multiplied by ):
| 2 | 1.32 | 1.00 | 1.52 | 2 | 2.64 | 2.0 | 3.03 | 4 |
| 8 | 2.30 | 3.00 | 3.48 | 8 | 18.4 | 24.0 | 27.9 | 64 |
| 16 | 3.03 | 4.00 | 5.28 | 16 | 48.5 | 64.0 | 84.4 | 256 |
| 160 | 7.61 | 7.32 | 21.0 | 160 | 1,218 | 1,172 | 3,362 | 25,600 |
| 1,280 | 17.5 | 10.3 | 73.2 | 1,280 | 22,392 | 13,212 | 93,656 | 1,638,400 |
| 41,943,040 | 1,120 | 25.3 | 37,462 | 41,943,040 |
For small , is larger than and is larger than . The polynomial functions only overtake the logarithmic ones from about , and from then on the gap grows without limit. Asymptotic conclusions are always about the long run.
Predicting Execution Times
Suppose that a program with complexity takes seconds for a problem of size . How long will it take for a larger size ?
Since the time is (approximately) proportional to , with the same constant for both sizes:
Writing , with , we obtain simple formulas for the most common classes:
| Explanation | ||
|---|---|---|
| The time grows in the same proportion as the size. | ||
| Doubling the size multiplies the time by . | ||
| Any base can be used, as long as it is the same in both logarithms. | ||
| A little more than linear. | ||
| Combination of the previous ones. | ||
| Depends on the difference of the sizes, not on their ratio. | ||
| Grows even faster than exponential. |
For example, if a program takes 1 unit of time for :
| Complexity | ||
|---|---|---|
| 1 | 1 | |
| ≈ 1.3 | 2 | |
| 10 | 1,000 | |
| ≈ 13 | ≈ 2,000 | |
| 100 | ||
| ≈ 133 | ≈ | |
| 1,000 | ||
🧠 Checkpoint: Predict the time
Question: A cubic algorithm takes 2 seconds for . How long will it take for ? And an algorithm that takes 2 seconds for , for ?
Answer: For the cubic one, and seconds. For the linearithmic one, and , so seconds.
Predicting the Largest Solvable Problem
The reverse question is just as useful: if a program with complexity solves a problem of size in time , what size can it solve in a larger time ? (Equivalently: what size could it solve in the same time on a computer times faster?)
With , i.e. :
| Effect of times more time (or a times faster computer) | ||
|---|---|---|
| The size grows by the same factor . | ||
| The size grows only by . | ||
| The size grows enormously. | ||
| The size only increases by a constant amount. |
For example, if a program solves a problem of size in 1 unit of time:
| Complexity | With 1,000 units of time | With units of time |
|---|---|---|
| any size | any size | |
| () | ||
| ≈ 316 | ||
| 100 | 1,000 | |
| ≈ 20 | ≈ 30 |
The last row is the most important one: with an exponential algorithm, a computer a million times faster only lets us go from 10 to about 30 elements. Exponential complexity cannot be defeated with hardware.
🧠 Checkpoint: A faster computer
Question: An program processes 5,000 records per night. The company buys a server 16 times faster. How many records can it process per night now? And if the program were and processed 40 records?
Answer: For the quadratic program, records: 16 times more power, but only 4 times more data. For the exponential one, records: a 16 times faster server only adds 4 records.
The Principle of Invariance
Given an algorithm of complexity , it will always have that complexity, regardless of the language in which it is programmed, the quality of the compiler, the speed of the computer, etc. These factors affect the implementation constant, but never the complexity .
More formally: two different implementations of the same algorithm, on any two machines, will never differ in efficiency by more than a constant factor. If one takes and the other , there is a constant such that for large enough .
This principle has three important consequences:
- It justifies the asymptotic approach. Since languages, compilers and machines only change the constant, studying the order of growth tells us something true about the algorithm itself, valid on any computer, present or future.
- It explains the time ratios of the lab. When we compare the same algorithm in two environments (for example, Python and Java), the ratio of their times tends to a constant: the implementation constant.
- It sets the limits of hardware. Better hardware divides the time by a constant. As the previous tables show, that can compensate for a better algorithm only for small inputs: for large inputs, the algorithm with the better growth rate always wins.
- Using the ratio of sizes to predict a time. The time scales with , not with , unless the algorithm is linear.
- Expecting faster hardware to rescue an exponential algorithm. It only adds a constant to the size you can solve.
- Comparing algorithms at one small size. Growth shows up for large ; at small sizes the constants may hide it.
Summary
- The common complexity classes are, from best to worst: , , , , , , , , and . Polynomial classes are considered tractable; exponential ones are not.
- With about operations per second, a quadratic algorithm handles tens of thousands of elements per second, an exponential one only a few dozen.
- To predict times: . To predict sizes: .
- For exponential algorithms, more time or faster hardware only adds a constant to the solvable size.
- The principle of invariance: implementation, language and hardware change only the constant, never the complexity.
Practice
- ⏱️ Predict times. An algorithm takes 3 seconds for . Estimate its time for and .
- 📏 Predict sizes. An algorithm takes 1 minute for . What is the largest it can process in 1 hour if it is ? And if it is ? And , if it took 1 minute for ?
- 🧪 Check with experiments. Take one of the algorithms you measured in the lab sessions. From the time of one size, predict the time for a size 4 times larger, then measure it. How close was the prediction? What could explain the difference?
- 🗺️ Choose a complexity. For each input size, which is the highest complexity that would still solve the problem in about one second: , , , ?
Errors, suggestions and public questions open a form on GitHub, visible to everyone.