Article
Reasonable and unreasonable time
Imagine a computer that does one billion steps every second. Give it 100 items. An algorithm that needs about n × n steps finishes before you blink. An algorithm that needs 2ⁿ steps would need about forty trillion years, and the universe is only about fourteen billion years old. Both algorithms work. Only one is usable.
How the number of steps grows
In the last article you counted steps. Now we sort algorithms by how fast the steps grow as n gets bigger:
| Name | Steps | Example |
|---|---|---|
| Constant | 1 (the same for any n) | show the first item of a list |
| Linear | n | add up every item; linear search in the worst case |
| Square (quadratic) | n × n | compare every item with every item |
| Cube | n × n × n | three loops inside each other |
| Exponential | 2ⁿ (2 times 2, n times) | try every on/off combination of n switches |
| Factorial | n! (n × (n−1) × … × 1) | try every possible order of n items |
Constant, linear, square and cube are all polynomial: n multiplied by itself a fixed number of times (n¹, n², n³, …). Some algorithms even grow slower than linear. In the next lesson you will meet one that needs only about 20 checks for a million items.
Now look at what the numbers do. This table is for n = 10, 20 and 50:
| Items | Linear: n | Square: n² | Cube: n³ | 2ⁿ | n! |
|---|---|---|---|---|---|
| 10 | 10 | 100 | 1,000 | 1,024 | 3,628,800 |
| 20 | 20 | 400 | 8,000 | 1,048,576 | about 2.4 × 10¹⁸ |
| 50 | 50 | 2,500 | 125,000 | about 1.1 × 10¹⁵ | about 3 × 10⁶⁴ |
The first four columns grow gently. The last two explode. Try it yourself:
Change n to 10, then 30, then 50, and run it again.
Check your understanding
An algorithm takes about n × n steps for a list of n items. How is its run time described?
What happens when the input grows?
Another way to see the difference is to ask what happens if the list gets bigger.
| The list gets… | linear | square | cube | exponential |
|---|---|---|---|---|
| twice as long | 2 times the steps | 4 times | 8 times | the steps are squared |
| just one item longer | a little more | a little more | a little more | twice the steps |
For an exponential algorithm, adding one item doubles the work. Ten more items multiply the work by about a thousand.
Reasonable and unreasonable time
Here is how the exam splits these into two groups:
Two kinds of run time
Reasonable time (polynomial or slower)
- Constant: 1
- Linear: n
- Square: n²
- Cube: n³
Unreasonable time
- Exponential: 2ⁿ
- Factorial: n!
An algorithm runs in reasonable time if its steps grow like a polynomial, or slower. It runs in unreasonable time if its steps grow exponentially or factorially. Remember: "unreasonable" does not mean "wrong". The algorithm gives correct answers. It just cannot finish in a useful time for big n.
Here is how long the steps take on a computer doing one billion steps per second:
| Items (n) | n² steps | 2ⁿ steps | n! steps |
|---|---|---|---|
| 20 | less than a millisecond | about 0.001 seconds | about 77 years |
| 50 | less than a millisecond | about 13 days | about 10⁴⁸ years |
| 100 | about 0.00001 seconds | about 40 trillion years | longer than the age of the universe, by far |
Why a faster computer does not rescue it
You might think: buy a faster computer. For a polynomial algorithm that helps a lot: a machine 1,000 times faster can handle far bigger inputs. For an exponential one it barely helps. A computer 1,000 times faster does 1,000 times more steps. But 2ⁿ doubles with every extra item, and 2¹⁰ is about 1,000, so you can handle only about 10 more items. Factorial is even harsher.
So when an unreasonable algorithm is the only one we know, people look for different ways. A later article shows one: accept an answer that is good enough, but quick.
Worked example
Which run time describes this code?
count ← 0
FOR EACH a IN aList
{
FOR EACH b IN aList
{
IF(a = b)
{
count ← count + 1
}
}
}The inner loop runs n times for each of the n passes of the outer loop. That is n × n steps: square, so it is polynomial and runs in reasonable time. Now compare "try every possible order for visiting n cities, and keep the shortest route". There are n! orders, so that is factorial: unreasonable time.
Check your understanding
Which algorithm runs in an unreasonable time?
Common mistakes
- Thinking exponential means "very big n" only. For n = 10 it is fine (1,024 steps). It is the growth that is the problem.
- Thinking polynomial is bad. Polynomial, even n³, is reasonable. Only exponential and factorial are unreasonable.
- Mixing up n² and 2ⁿ. In n², n is the base (n × n). In 2ⁿ, n is the exponent (2 times itself n times). For n = 10: 100 against 1,024.
- Believing a faster computer fixes it. It helps a little, never enough.
- Calling unreasonable algorithms wrong. They are correct. They are too slow.
On the exam
You may be given an algorithm described in words or code, or a table of step counts, and asked "Which of these runs in reasonable time?" Find the pattern. A fixed number of loops over the data (nested or not) is polynomial. "Try every combination", "try every subset" or "try every ordering" is exponential or factorial. In a table, check what happens when n goes from 10 to 20 to 50: gentle growth is polynomial, explosive growth is not.
Key words
Polynomial: steps that grow like n, n², n³ and so on (n to a fixed power).
Reasonable time: polynomial or slower growth. A computer can finish for large inputs.
Unreasonable time: exponential (2ⁿ) or factorial (n!) growth. Too slow for large inputs, even on the fastest computer.
Exponential: steps double each time n grows by 1.
Factorial: n! = n × (n − 1) × … × 1.