Sub topic 2
Why Polynomial Time Means Tractable
This chapter assumes you can read Python, know Big-O notation, and are comfortable with logarithms and exponents. The goal is to explain why complexity theory draws its main line between polynomial and exponential running time, and why that line stays put when you change hardware, programming language, or machine model.
Measuring Running Time Against Input Size
A running time is a function of the input size, written n. In complexity theory, n is the number of bits (or symbols) needed to write the input down. For a graph, n is roughly the length of its adjacency list. For a list of integers, n counts the digits of every number, not how many numbers there are and not how large they are.
An algorithm runs in polynomial time if there is a constant k such that it uses O(n^k) steps on every input of size n. Linear, n log n, quadratic, and cubic algorithms are all polynomial. Note that O(n log n) is bounded by O(n^2), so it counts.
An algorithm runs in exponential time if its step count grows like c^n for some constant c > 1, or faster. Examples are 2^n, 3^n, and n!. Trying every subset of n items is the standard case of exponential growth.
How Fast the Gap Opens
Suppose a machine performs 10^9 simple steps per second. The table shows how long each growth rate takes.
| Steps | n = 10 | n = 50 | n = 100 |
|---|---|---|---|
| n^2 | 0.1 µs | 2.5 µs | 10 µs |
| n^3 | 1 µs | 125 µs | 1 ms |
| 2^n | about 1 µs | about 13 days | about 4 × 10^13 years |
At n = 10 the exponential algorithm keeps up. At n = 100 it needs thousands of times the age of the universe. The polynomial algorithms barely notice the change.
The chart plots the base-10 logarithm of the step count, so each unit up the vertical axis is ten times more work.
On a log scale an exponential becomes a straight line, and a polynomial flattens out. The space between the two lines keeps widening as n grows.
Faster Hardware Helps Polynomial Algorithms Much More
A practical way to see the difference is to ask how much faster hardware helps. The script below finds the largest input each algorithm can handle within a step budget, then multiplies the budget by 1000.
import math
def largest_n(cost, budget):
"""Largest n >= 0 with cost(n) <= budget, assuming cost is increasing."""
hi = 1
while cost(hi) <= budget:
hi *= 2
lo = hi // 2
while lo < hi - 1:
mid = (lo + hi) // 2
if cost(mid) <= budget:
lo = mid
else:
hi = mid
return lo
costs = {
"n": lambda n: n,
"n^2": lambda n: n ** 2,
"n^3": lambda n: n ** 3,
"2^n": lambda n: 2 ** n,
"n!": math.factorial,
}
old_budget = 10 ** 12
new_budget = 1000 * old_budget
print(f"{'cost':>5} {'old max n':>16} {'new max n':>16} {'gain':>8}")
for name, cost in costs.items():
a = largest_n(cost, old_budget)
b = largest_n(cost, new_budget)
print(f"{name:>5} {a:>16,} {b:>16,} {b / a:>7.1f}x")Output:
cost old max n new max n gain
n 1,000,000,000,000 1,000,000,000,000,000 1000.0x
n^2 1,000,000 31,622,776 31.6x
n^3 10,000 100,000 10.0x
2^n 39 49 1.3x
n! 14 17 1.2xWith a polynomial algorithm, a 1000× faster machine multiplies the solvable input size by a constant factor (1000^(1/k) for n^k). With 2^n, the same upgrade adds only about 10 to n, because 2^10 ≈ 1000. Exponential algorithms gain almost nothing from hardware progress. This is the practical meaning of "intractable."
Why the Line Is Drawn Exactly Here
Speed is only part of the reason. Complexity theory uses polynomial time because the class of polynomial-time algorithms has three useful properties.
Closure under composition
Polynomials are closed under addition, multiplication, and composition. Suppose an algorithm makes p(n) calls to a subroutine, and each call takes q(m) time on an input of size m. Every intermediate input has size at most polynomial in n, so the total time is still polynomial. You can build efficient algorithms out of efficient parts without leaving the class. Later chapters depend on this property, because reductions chain one polynomial-time transformation into another.
Independence from the machine model
Single-tape Turing machines, multi-tape Turing machines, and random-access machines can simulate one another with at most polynomial slowdown. A multi-tape Turing machine running in time T can be simulated on a single tape in O(T^2) time. Whether a problem has a polynomial-time algorithm therefore does not depend on the model you pick. The claim that this holds for every reasonable deterministic model is called the Cobham–Edmonds thesis. It is a working assumption, not a theorem.
Independence from encoding
Any reasonable encoding of the input changes its size only polynomially. Adjacency list versus adjacency matrix, or binary versus decimal, does not move a problem in or out of polynomial time. Unary encoding is the exception, covered in the next section.
Common Errors
Measuring size by value instead of length
The number N takes about log2 N bits to write down. Testing primality by trial division up to √N takes about √N = 2^(n/2) steps when n = log2 N. That is exponential in the input size, even though it looks like "square root time." The same mistake comes back with Subset Sum in a later chapter on pseudo-polynomial algorithms.
Treating "between polynomial and exponential" as polynomial
A bound like n^(log n) = 2^((log n)^2) is called quasi-polynomial. It grows faster than every n^k, so it is not polynomial, although it grows far slower than 2^n.
Assuming polynomial always means fast
An O(n^100) algorithm is polynomial and useless in practice, and an algorithm that takes 1.0001^n steps beats it for any input you could store. The argument for the definition rests on what happens in practice. Once a problem gets any polynomial algorithm, researchers usually push the exponent down to 2 or 3 fairly quickly. Large constant factors can also matter.
Confusing worst case with typical case
The definition is about the worst case. The simplex method for linear programming takes exponential time on specially built inputs but runs fast on most real ones. SAT solvers, covered at the end of the tutorial, handle many large industrial instances even though no polynomial worst-case bound is known for them.
Exercise
An algorithm runs in n^3 steps. If your hardware becomes 1000 times faster, by what factor does the largest solvable input size grow? What happens for a 2^n algorithm?
Show answer
For n^3, the new size n' satisfies n'^3 = 1000 · n^3, so n' = 10n, a tenfold increase. For 2^n, 2^(n') = 1000 · 2^n gives n' = n + log2(1000) ≈ n + 10, which adds about ten to the size.
Exercise
An algorithm decides whether an integer N is prime by testing every divisor from 2 to N − 1. Is it a polynomial-time algorithm?
Show answer
No. The input size is n ≈ log2 N bits, and the algorithm does about N = 2^n divisions, which is exponential in n. A polynomial-time primality test does exist (AKS, 2002), but it relies on very different ideas.
Exercise
Algorithm A calls a subroutine n^2 times, each call takes O(m^3) time on an input of size m, and every input passed to the subroutine has size at most 5n. Is A polynomial? What if each call instead receives the previous call's output, and that output is twice the size of its input?
Show answer
In the first case the total is n^2 · O((5n)^3) = O(n^5), which is polynomial. In the second case the sizes grow as 2^i · n, so the last call works on an input of size about 2^(n^2) · n. That is exponential, so A is not polynomial, even though each individual call is.