Pebellslearn by building
0%
RoadmapsMatrixCoursesSandboxSign in
Asymptotic Notation  ›  Lesson

Polynomial, Exponential and Factorial Growth

Exponential 12 minNested loops over the input give polynomial growth such as n squared, trying every yes-or-no combination gives exponential growth of 2 to the n, and trying every ordering gives factorial growth.

Polynomial, Exponential and Factorial Growth

A delivery company has 10 stops. Checking every pair of stops for the shortest link takes a moment. Checking every order in which to visit them means 3,628,800 routes. At 20 stops, the pairs are still easy, but the orders number in the quintillions. Knowing which kind of counting your algorithm does is the difference between "instant" and "never".

The rule

Nested loops give powers; yes-or-no choices give 2ⁿ; orderings give n!.

Three families grow faster than linear:

  • Polynomial, like n² or n³. Comes from loops inside loops over the input. Comparing every item with every other item is about n².
  • Exponential, 2ⁿ. Comes from making an independent yes-or-no choice for each item: every subset of n items. Each extra item doubles the work.
  • Factorial, n! = n × (n − 1) × … × 1. Comes from trying every ordering of n items. Each extra item multiplies the work by the new n.

The numbers pull apart fast:

n n² 2ⁿ n!
5 25 32 120
10 100 1,024 3,628,800
20 400 1,048,576 about 2.4 × 10¹⁸

Put the whole ladder together, slowest-growing first: 1, log n, n, n log n, n², n³, 2ⁿ, n!.

The shortcut

Ask what the algorithm is enumerating. Pairs or triples of items: polynomial. Subsets, or each item in or out: exponential. Arrangements or orders: factorial.

Classifying by what gets enumeratedStep 1 of 4
  1. Find duplicate names by comparing every pair

    Every item against every other item: pairs, so about n².

The trap is judging from small inputs. At n = 5, 2ⁿ (32) barely beats n² (25). The difference only shows as n grows, which is exactly when it hurts.

Drills

Answer fast — trust the rule.

quick drill

Two nested loops each run over all n items. What is the growth rate?

quick drill

An algorithm checks every subset of 20 items. About how many subsets is that?

quick drill

An exponential algorithm handles 30 items in 1 second. Roughly how long for 31 items?

quick drill

Which order runs from slowest-growing to fastest-growing?

Remember

  • Nested loops give powers of n; in-or-out choices give 2ⁿ; orderings give n!.
  • The ladder: 1, log n, n, n log n, n², n³, 2ⁿ, n!.
  • The trap: small inputs hide the gap; exponential and factorial explode as n grows.

Practice — level up

0/1 solved

Write your answer, then compare it with a rubric and a model answer.

ExplainPairs, subsets and orderings

For a set of 20 items, compare three jobs: checking every pair of items, checking every subset of items, and checking every ordering of items. Name each growth rate, estimate the amount of work for n = 20, and say which ones are practical.