Unit 4: Algorithms · Evaluating algorithms

Article

Reasonable and unreasonable time

8 min read

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:

NameStepsExample
Constant1 (the same for any n)show the first item of a list
Linearnadd up every item; linear search in the worst case
Square (quadratic)n × ncompare every item with every item
Cuben × n × nthree loops inside each other
Exponential2ⁿ (2 times 2, n times)try every on/off combination of n switches
Factorialn! (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:

ItemsLinear: nSquare: n²Cube: n³2ⁿn!
10101001,0001,0243,628,800
20204008,0001,048,576about 2.4 × 10¹⁸
50502,500125,000about 1.1 × 10¹⁵about 3 × 10⁶⁴

The first four columns grow gently. The last two explode. Try it yourself:

Runs in your browser

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…linearsquarecubeexponential
twice as long2 times the steps4 times8 timesthe steps are squared
just one item longera little morea little morea little moretwice 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

How the steps grow
  • Reasonable time (polynomial or slower)

    • Constant: 1
    • Linear: n
    • Square: n²
    • Cube: n³
  • Unreasonable time

    • Exponential: 2ⁿ
    • Factorial: n!
Reasonable means the work grows slowly enough that a computer can still finish for big inputs.

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² steps2ⁿ stepsn! steps
20less than a millisecondabout 0.001 secondsabout 77 years
50less than a millisecondabout 13 daysabout 10⁴⁸ years
100about 0.00001 secondsabout 40 trillion yearslonger 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?

AP pseudocode
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.

AP® is a trademark registered by the College Board, which is not affiliated with, and does not endorse, this site. All lessons and questions here are written by JrCodex.