Conceptintro
Polynomial time
Also called O(n^k) time.
Running time bounded by a polynomial in the input size, the standard formal notion of efficient computation and the class P.
An algorithm runs in polynomial time when its worst-case time is for some constant , where is the length of the input in bits (or symbols). The decision problems that a deterministic algorithm solves in polynomial time form the complexity class P. Polynomial time is the usual formal meaning of "efficient": it contains the classes of quadratic and cubic time, and it does not contain exponential time.
In plain words
A program passes the polynomial-time test when its running time is at most the input length raised to some fixed power, such as 2, 3 or 10. The power can be large, but it must not change when the input grows.
A program that fails the test grows much faster. A program that tries each subset of the items doubles its time each time one item is added. The test does not promise speed in practice: a power of 50 passes it and is useless. But a program built from parts that pass the test passes it too. This is why theory uses it.
The test counts the length of the input as written, not the value of a number. A program that counts up to a number in the input can be fast for the value and slow for the length.
The idea
Definition. Fix an alphabet with at least two symbols and write for the length of a string . An algorithm on strings, in a stated machine model, runs in polynomial time when there are constants such that for each the algorithm stops on within steps. This is the same as for the worst-case time , for . The term only handles the empty string. The class P is the set of languages (decision problems) for which some deterministic polynomial-time algorithm decides, for each , whether .
Here is the complexity class of the languages that a deterministic algorithm decides in time .
The class FP is the same idea for functions: the functions that a deterministic polynomial-time algorithm computes.
The input length is in bits. The size is the length of a standard encoding of the input. A graph given by its adjacency list has bits, and an integer has bits. Two reasonable encodings of the same object have lengths that are polynomially related, so the class does not depend on the choice. A number written in unary is not a reasonable encoding, because it is exponentially longer than the binary form. See input size and unary encoding.
Model. The class P is the same in the common models, because they simulate each other with a polynomial slowdown: the Turing machine (single or multi-tape), and the word RAM with words of bits. The exponent can change from one model to another, so a statement such as "cubic time" needs a model, and the statement "polynomial time" does not. A model with unit cost, unbounded integers and multiplication in one step is the exception. A RAM that multiplies integers of any size in one step can compute by squarings in steps, and the answer has bits. So this unit-cost model does not simulate the Turing machine with a polynomial slowdown. See unit-cost model.
Closure under composition. The reason for the choice of polynomials is that the class is closed under the operations by which algorithms are built.
Claim. Let be computable in time and in time on a Turing machine, where and are polynomials that are non-decreasing. Then is computable in time , which is polynomial.
Proof. On input with , run the machine for . It stops after at most steps. A Turing machine writes at most one symbol in each step, so the output has length at most . Now run the machine for on . By the bound for , this takes at most steps, because is non-decreasing. Copying to the input tape of the machine for and moving the head back takes more steps. So the total is . The composition of two polynomials is a polynomial, and the sum of two polynomials is a polynomial.
The same argument shows that an algorithm that makes at most calls to a polynomial-time subroutine, with arguments of length at most , runs in polynomial time. The sum and the product of two polynomial running times are polynomial. This is the base of the polynomial-time reductions of the theory of NP-completeness.
The exponent matters. Polynomial time is not one level. The deterministic time hierarchy theorem shows that, on multi-tape Turing machines, for each . So there are problems that are solvable in time and not in time . Above polynomial time are quasi-polynomial time, the class , that is, for some constant , then subexponential time, then exponential time.
Pseudo-polynomial time. The test counts the length of the input, not the value of a number in the input. An algorithm with time polynomial in the value is pseudo-polynomial, and it is exponential in the length when the number is written in binary. Two examples:
- Trial division tests whether is prime with about divisions. The length is bits, and , so the time is exponential in the length.
- The dynamic program for subset sum and for the knapsack problem takes time for items and a target or capacity . The input length is about bits plus the length of the items, so can be , and the time is then exponential. The problem is NP-complete in the usual sense, and the dynamic program does not show that P equals NP.
See pseudo-polynomial time for the exact definition, and weak and strong NP-hardness.
Strong and weak polynomial time. For problems whose input is a list of numbers, a second test is used in the arithmetic model. An algorithm is strongly polynomial when the number of arithmetic operations and comparisons is polynomial in the count of the input numbers, whatever their values, and the sizes of the numbers during the run are polynomial in the input length. It is weakly polynomial when it is polynomial in the length in bits only. Sorting numbers by comparisons is strongly polynomial. The ellipsoid method for linear programming is weakly polynomial: its number of iterations grows with the bit length of the input, so it is not strongly polynomial. Whether linear programming has any strongly polynomial algorithm is a long-standing open question.
Examples
- Sorting numbers with merge sort takes comparisons. This is , so it is in polynomial time.
- Shortest paths with non-negative weights, minimum spanning trees, maximum matching in graphs and maximum flow are in P.
- Linear programming is in P by the ellipsoid method (Khachiyan, 1979), and this method is slow in practice. The simplex method, which many practitioners prefer, has exponential worst-case time for several classical pivot rules.
- Primality testing is in P (Agrawal, Kayal and Saxena, 2004).
- Non-example: the usual algorithm for the subset sum problem with numbers in binary tries subsets. The time is exponential, and no polynomial-time algorithm is known. The problem is NP-complete, so a polynomial algorithm would prove (see the P versus NP problem).
- Non-example: no polynomial-time algorithm is known on a classical computer for the factoring of an integer given in binary. It is not known to be NP-complete.
- Non-example: a function such as grows faster than each polynomial. For it is , and .
Where it is used
Polynomial time is the standard meaning of "tractable" or "efficient" in complexity theory, and the class P is the base for the definitions of NP, of NP-hardness and of the polynomial-time reduction. The thesis that feasible means polynomial time is called the Cobham-Edmonds thesis. The thesis is named after Alan Cobham, who proposed the class of functions computable in polynomial time as a machine-independent class in 1965, and Jack Edmonds. In 1965 Edmonds called an algorithm "good" when its running time has a polynomial bound. The thesis is a rule, not a theorem, and it has known weak points. An algorithm with time or with a huge constant is polynomial and is not practical. An exponential algorithm can be fast on the inputs that occur, as the simplex method is. The thesis keeps its use for two reasons. The natural problems in P tend to have algorithms of low degree, and the class does not depend on the machine model. See computational tractability.
References
- Michael Sipser, Introduction to the Theory of Computation, third edition, Section 7.2 (the class P)
- Arora and Barak, Computational Complexity, A Modern Approach, Chapter 1
- Cormen, Leiserson, Rivest and Stein, Introduction to Algorithms, fourth edition, Chapter 34
- Jack Edmonds, Paths, trees, and flowers, Canadian Journal of Mathematics 17 (1965), the digression on good algorithms
- Alan Cobham, The intrinsic computational difficulty of functions, Proceedings of the 1964 International Congress for Logic, Methodology and Philosophy of Science (1965), the class of functions computable in polynomial time
- Leonid G. Khachiyan, A polynomial algorithm in linear programming, Soviet Mathematics Doklady 20 (1979), the ellipsoid method for linear programming
- Manindra Agrawal, Neeraj Kayal and Nitin Saxena, PRIMES is in P, Annals of Mathematics 160 (2004), the main theorem
About this page
The build computes these levels from the evidence in the repository. Each row says what the next level needs. Read what the levels mean.
| Ladder | Level | What the next level needs |
|---|---|---|
| Human proof | H0 no proof |
|
| Lean | L0 no Lean part |
|
| Visual | V0 none |
|
| Writing | W4 complete |
|
Tag Y931HC Edit this page History Report an error Markdown JSON