Short answer
In complexity theory, a reasonable algorithm runs in polynomial time. Its running time grows like n, n log n, n squared or n cubed, for some fixed power. An unreasonable algorithm usually runs in exponential time, such as 2^n, or grows even faster. To tell which is which, look at how the running time grows as the input gets bigger.
Start by defining the input size n
Before you can judge growth, you need to know what n measures. It is usually the amount of data the algorithm reads: the number of items in a list, the number of vertices in a graph, or the number of digits in a number.
Numbers can be misleading. Trial division checks possible divisors up to about the square root of a number N. For a number with d digits, that is on the order of 10^(d/2) steps. That is exponential in d, even though the code looks like a simple loop.
Check how growth responds to changes in n
Two quick tests help:
- The doubling test. Double n and see what happens to the running time. For n squared, the time multiplies by 4. For n cubed, it multiplies by 8. A fixed multiplier like this points to polynomial growth.
- The adding test. Add one to n and see what happens. For 2^n, the time doubles with each extra input item. When adding one multiplies the time by a fixed amount, the growth is exponential.
Common growth rates look like this:
| Running time | Example | Usually called |
|---|---|---|
| n | Scanning a list once | Reasonable |
| n log n | Efficient comparison sorting | Reasonable |
| n squared | Comparing every pair of items | Reasonable |
| n cubed | Three nested loops over n items | Reasonable |
| 2^n | Trying every subset | Unreasonable |
| n! | Trying every ordering | Unreasonable, and growing faster still |
Why polynomial time is the dividing line
In 1965, Alan Cobham and Jack Edmonds both pointed to polynomial time as the line between feasible and infeasible computation. The idea is now called the Cobham-Edmonds thesis. It is a rule of thumb, so it has limits.
A polynomial algorithm with a large exponent can still be impractical. An exponential algorithm can handle small inputs without trouble. Use the label as a guide to how the running time scales, and check the actual numbers for your inputs.
Watch for hidden exponential work
Some exponential behavior is easy to miss:
- Naive recursion. A recursive Fibonacci function that recalculates the same values can make an exponential number of calls. In this case, storing answers as you go, the idea behind dynamic programming, reduces the work to linear.
- Trial division on large numbers. As described above, the number of digits is what matters.
- Exhaustive search. Brute force algorithm examples, such as testing every subset or every ordering, are typically exponential or factorial in the input size.
Worst case is not the whole story
Reasonable and unreasonable usually describe the worst case. Some exponential algorithms work well in practice. The simplex method for linear programming has exponential worst-case time, yet it is widely used. Small inputs also change the picture, since 2^20 is only about a million steps.
If an algorithm is unreasonable
If you find that an algorithm is unreasonable, you have several options:
- Limit the input size, if the problem allows it.
- Use heuristics or approximation algorithms, which give good answers without guaranteeing the best one. Many readers start with the question “what are heuristics in computer science”, and the short answer is that they trade guarantees for speed.
- Look for a better method. Dynamic programming can turn some exponential searches into polynomial ones.
If no correct algorithm exists at all, the question is different. For that case, see “What is the difference between undecidable problems and unreasonable time algorithms?”
Summary
- Reasonable usually means polynomial time. Unreasonable usually means exponential or worse.
- Use the doubling test and the adding test to judge growth.
- Watch for hidden exponential work in recursion, in trial division on large numbers, and in exhaustive search.
- Remember that worst-case growth does not always match what you see in practice.