Sub topic 2
Why Polynomial Time Means Tractable
This chapter assumes you can read Big-O notation and simple Python. It explains why complexity theory draws its main line between polynomial and exponential running times.
Two Kinds of Growth
An algorithm runs in polynomial time if its worst-case running time is O(n^k) for some fixed constant k, where n is the size of the input. Examples are O(n), O(n log n), and O(n^3). An algorithm runs in exponential time if its running time grows like 2^n or faster, for example O(2^n) or O(n!).
At small sizes the two kinds look similar. As n grows, they separate quickly:
| n | n^2 | n^3 | 2^n |
|---|---|---|---|
| 10 | 100 | 1,000 | 1,024 |
| 20 | 400 | 8,000 | about 1.05 million |
| 50 | 2,500 | 125,000 | about 1.13 × 10^15 |
| 100 | 10,000 | 1,000,000 | about 1.27 × 10^30 |
At a billion steps per second, 2^100 steps would take roughly 4 × 10^13 years.
Faster Hardware Does Not Rescue Exponential Algorithms
Suppose your computer becomes twice as fast. With an O(n^2) algorithm, the largest input you can handle in the same time grows by a factor of √2, about 41%. With an O(2^n) algorithm, it grows by exactly one element, because 2^(n+1) = 2 · 2^n. Better hardware scales up polynomial algorithms. For exponential ones it barely moves the limit.
import math
def max_n(steps_budget, cost):
n = 1
while cost(n + 1) <= steps_budget:
n += 1
return n
budget = 10**12
for speedup in (1, 2, 1000):
b = budget * speedup
print(speedup, max_n(b, lambda n: n**2), max_n(b, lambda n: 2**n))This prints the largest n for each speedup. A 1000× speedup multiplies the n^2 limit by about 31, but adds only about 10 to the 2^n limit.
Why Polynomials Are the Right Boundary
Polynomial time has properties that make it a stable definition of "efficient":
- Closure under composition. If you call a polynomial-time routine a polynomial number of times, or feed its output into another polynomial-time routine, the total is still polynomial. A later chapter on reductions depends on this.
- Model independence. Turing machines, RAM machines, and real computers can simulate one another with only polynomial overhead. Which problems count as polynomial-time does not depend on the machine model.
- In practice, exponents are small. Polynomial algorithms for natural problems usually run in O(n^3) or better.
The claim that polynomial time captures feasible computation is called the Cobham–Edmonds thesis.
Measure n in Bits
The input size n means the number of bits needed to write the input down. It does not mean the numeric value of the input. A number with value V takes about log₂ V bits. So an algorithm that runs in O(V) time is exponential in the input size. The chapter on pseudo-polynomial time returns to this point.
Exercise
Algorithm A runs in O(n^4) time and algorithm B runs in O(1.5^n) time. You buy a machine 16 times faster. By roughly how much does the largest solvable input grow for each?
Show answer
For A, 16 = k^4 gives k = 2, so the input size doubles. For B, 1.5^m = 16 gives m = log 16 / log 1.5 ≈ 6.8, so the input size grows by only about 7 elements.
Exercise
An algorithm checks whether an integer N is prime by trying every divisor from 2 to N − 1. Is it polynomial time?
Show answer
No. The input has about log₂ N bits, and N − 2 trial divisions is exponential in that bit length.