Attic › Computer science › P (complexity)

Polynomial time grows much more slowly than exponential time as inputs get larger

In computational complexity theory, the difference between a problem inside P and one outside it becomes clear when the input size n gets large. Algorithms in P take polynomial time, which grows slowly enough that modern computers can handle large inputs. Many harder problems require exponential time, where the number of steps grows so fast that even small inputs become impossible to compute.

In a polynomial expression like n² or n³, the input size n is the base and the exponent is a fixed number. In an exponential expression like 2ⁿ, the input size n is the exponent. Every time you add 1 to n, an algorithm that takes 2ⁿ steps must do twice as much work as before.

To see the difference, imagine a computer that can do one billion (10⁹) steps per second. Compare two algorithms for the same problem: a polynomial-time algorithm that takes n³ steps, and an exponential-time algorithm that takes 2ⁿ steps.

1. For an input of size n = 10, the n³ algorithm takes 1,000 steps, and the 2ⁿ algorithm takes 1,024 steps. Both finish in about one-millionth of a second.

2. For n = 50, the n³ algorithm takes 125,000 steps (still less than a millisecond). The 2ⁿ algorithm takes about 1.1 × 10¹⁵ steps, which takes the computer 13 days.

3. For n = 100, the n³ algorithm takes 1,000,000 steps (one millisecond). The 2ⁿ algorithm takes about 1.2 × 10³⁰ steps, which takes 40 trillion years—nearly three thousand times the age of the universe.

In the formal notation of complexity theory, the number of steps a deterministic Turing machine takes is called DTIME. A problem that takes at most n³ steps is in DTIME(n³). The class P is the union of DTIME(nᵏ) for all fixed numbers k.

In theory, a problem in P could require n¹⁰⁰ steps, which would be slower than 2ⁿ for small inputs. In practice, almost all natural problems that are in P have algorithms with small exponents, such as n, n², or n³. Furthermore, when researchers first find a polynomial-time algorithm with a high exponent, they usually find ways to lower that exponent to a practical number soon after.

Many everyday tasks are in P. Finding the shortest path on a road map, sorting a list of millions of names, and testing whether a number with hundreds of digits is prime all have polynomial-time algorithms. Because these problems are in P, computers can solve them reliably even as datasets grow to millions of items.

Fact-checked against P (complexity) on Wikipedia

Part of the Computer science syllabus: Computational Complexity

See it on the timeline of Computer science