NP (complexity)
This article includes a list of references, but its sources remain unclear because it has insufficient inline citations. (October 2015) (Learn how and when to remove this template message) 
Unsolved problem in computer science: (more unsolved problems in computer science)

In computational complexity theory, NP (nondeterministic polynomial time) is a complexity class used to classify decision problems. NP is the set of decision problems for which the problem instances, where the answer is "yes", have proofs verifiable in polynomial time by a deterministic Turing machine.^{[2]}^{[Note 1]}
An equivalent definition of NP is the set of decision problems solvable in polynomial time by a nondeterministic Turing machine. This definition is the basis for the abbreviation NP; "nondeterministic, polynomial time." These two definitions are equivalent because the algorithm based on the Turing machine consists of two phases, the first of which consists of a guess about the solution, which is generated in a nondeterministic way, while the second phase consists of a deterministic algorithm that verifies if the guess is a solution to the problem.^{[3]}
Decision problems are assigned complexity classes (such as NP) based on the fastest known algorithms. Therefore, decision problems may change classes if faster algorithms are discovered.
It is easy to see that the complexity class P (all problems solvable, deterministically, in polynomial time) is contained in NP (problems where solutions can be verified in polynomial time), because if a problem is solvable in polynomial time then a solution is also verifiable in polynomial time by simply solving the problem. But NP contains many more problems^{[Note 2]}, the hardest of which are called NPcomplete problems. An algorithm solving such a problem in polynomial time is also able to solve any other NP problem in polynomial time. The most important P versus NP (“P = NP?”) problem, asks whether polynomial time algorithms exist for solving NPcomplete, and by corollary, all NP problems. It is widely believed that this is not the case.^{[4]}
The complexity class NP is related to the complexity class coNP for which the answer "no" can be verified in polynomial time. Whether or not NP = coNP is another outstanding question in complexity theory.^{[5]}
Formal definition[edit]
The complexity class NP can be defined in terms of NTIME as follows:
where is the set of decision problems that can be solved by a nondeterministic Turing machine in time.
Alternatively, NP can be defined using deterministic Turing machines as verifiers. A language L is in NP if and only if there exist polynomials p and q, and a deterministic Turing machine M, such that
 For all x and y, the machine M runs in time p(x) on input
 For all x in L, there exists a string y of length q(x) such that
 For all x not in L and all strings y of length q(x),
Background[edit]
Many computer science problems are contained in NP, like decision versions of many search and optimization problems.
Verifierbased definition[edit]
In order to explain the verifierbased definition of NP, consider the subset sum problem: Assume that we are given some integers, {−7, −3, −2, 5, 8}, and we wish to know whether some of these integers sum up to zero. Here, the answer is "yes", since the integers {−3, −2, 5} corresponds to the sum (−3) + (−2) + 5 = 0. The task of deciding whether such a subset with zero sum exists is called the subset sum problem.
To answer if some of the integers add to zero we can create an algorithm which obtains all the possible subsets. As the number of integers that we feed into the algorithm becomes larger, both the number of subsets and the computation time grows exponentially.
But notice that if we are given a particular subset we can efficiently verify whether the subset sum is zero, by summing the integers of the subset. If the sum is zero, that subset is a proof or witness for the answer is "yes". An algorithm that verifies whether a given subset has sum zero is a verifier. Clearly, summing the integers of a subset can be done in polynomial time and the subset sum problem is therefore in NP.
The above example can be generalized for any decision problem. Given any instance I of problem and witness W, if there exists a verifier V so that given the ordered pair (I, W) as input, V returns "yes" in polynomial time if the witness proves that the answer is "yes" or "no" in polynomial time otherwise, then is in NP.
The "no"answer version of this problem is stated as: "given a finite set of integers, does every nonempty subset have a nonzero sum?". The verifierbased definition of NP does not require an efficient verifier for the "no"answers. The class of problems with such verifiers for the "no"answers is called coNP. In fact, it is an open question whether all problems in NP also have verifiers for the "no"answers and thus are in coNP.
In some literature the verifier is called the "certifier" and the witness the "certificate".^{[2]}
Machinedefinition[edit]
Equivalent to the verifierbased definition is the following characterization: NP is the class of decision problems solvable by a nondeterministic Turing machine that runs in polynomial time. That is to say, a decision problem is in NP whenever is recognized by some polynomialtime nondeterministic Turing machine with an existential acceptance condition, meaning that if and only if some computation path of leads to an accepting state. This definition is equivalent to the verifierbased definition because a nondeterministic Turing machine could solve an NP problem in polynomial time by nondeterministically selecting a certificate and running the verifier on the certificate. Similarly, if such a machine exists, then a polynomial time verifier can naturally be constructed from it.
In this light, we can define coNP dually as the class of decision problems recognizable by polynomialtime nondeterministic Turing machines with an existential rejection condition. Since an existential rejection condition is exactly the same thing as a universal acceptance condition, we can understand the NP vs. coNP question as asking whether the existential and universal acceptance conditions have the same expressive power for the class of polynomialtime nondeterministic Turing machines.
Properties[edit]
NP is closed under union, intersection, concatenation, Kleene star and reversal. It is not known whether NP is closed under complement (this question is the socalled "NP versus coNP" question)
Why some NP problems are hard to solve[edit]
Because of the many important problems in this class, there have been extensive efforts to find polynomialtime algorithms for problems in NP. However, there remain a large number of problems in NP that defy such attempts, seeming to require superpolynomial time. Whether these problems are not decidable in polynomial time is one of the greatest open questions in computer science (see P versus NP ("P=NP") problem for an indepth discussion).
An important notion in this context is the set of NPcomplete decision problems, which is a subset of NP and might be informally described as the "hardest" problems in NP. If there is a polynomialtime algorithm for even one of them, then there is a polynomialtime algorithm for all the problems in NP. Because of this, and because dedicated research has failed to find a polynomial algorithm for any NPcomplete problem, once a problem has been proven to be NPcomplete this is widely regarded as a sign that a polynomial algorithm for this problem is unlikely to exist.
However, in practical uses, instead of spending computational resources looking for an optimal solution, a good enough (but potentially suboptimal) solution may often be found in polynomial time. Also, the real life applications of some problems are easier than their theoretical equivalents.
Equivalence of definitions[edit]
The two definitions of NP as the class of problems solvable by a nondeterministic Turing machine (TM) in polynomial time and the class of problems verifiable by a deterministic Turing machine in polynomial time are equivalent. The proof is described by many textbooks, for example Sipser's Introduction to the Theory of Computation, section 7.3.
To show this, first suppose we have a deterministic verifier. A nondeterministic machine can simply nondeterministically run the verifier on all possible proof strings (this requires only polynomially many steps because it can nondeterministically choose the next character in the proof string in each step, and the length of the proof string must be polynomially bounded). If any proof is valid, some path will accept; if no proof is valid, the string is not in the language and it will reject.
Conversely, suppose we have a nondeterministic TM called A accepting a given language L. At each of its polynomially many steps, the machine's computation tree branches in at most a finite number of directions. There must be at least one accepting path, and the string describing this path is the proof supplied to the verifier. The verifier can then deterministically simulate A, following only the accepting path, and verifying that it accepts at the end. If A rejects the input, there is no accepting path, and the verifier will always reject.
Relationship to other classes[edit]
NP contains all problems in P, since one can verify any instance of the problem by simply ignoring the proof and solving it. NP is contained in PSPACE—to show this, it suffices to construct a PSPACE machine that loops over all proof strings and feeds each one to a polynomialtime verifier. Since a polynomialtime machine can only read polynomially many bits, it cannot use more than polynomial space, nor can it read a proof string occupying more than polynomial space (so we do not have to consider proofs longer than this). NP is also contained in EXPTIME, since the same algorithm operates in exponential time.
coNP contains those problems which have a simple proof for no instances, sometimes called counterexamples. For example, primality testing trivially lies in coNP, since one can refute the primality of an integer by merely supplying a nontrivial factor. NP and coNP together form the first level in the polynomial hierarchy, higher only than P.
NP is defined using only deterministic machines. If we permit the verifier to be probabilistic (this however, is not necessarily a BPP machine^{[6]}), we get the class MA solvable using an ArthurMerlin protocol with no communication from Arthur to Merlin.
NP is a class of decision problems; the analogous class of function problems is FNP.
The only known strict inclusions came from the time hierarchy theorem and the space hierarchy theorem, and respectively they are and .
Other characterizations[edit]
In terms of descriptive complexity theory, NP corresponds precisely to the set of languages definable by existential secondorder logic (Fagin's theorem).
NP can be seen as a very simple type of interactive proof system, where the prover comes up with the proof certificate and the verifier is a deterministic polynomialtime machine that checks it. It is complete because the right proof string will make it accept if there is one, and it is sound because the verifier cannot accept if there is no acceptable proof string.
A major result of complexity theory is that NP can be characterized as the problems solvable by probabilistically checkable proofs where the verifier uses O(log n) random bits and examines only a constant number of bits of the proof string (the class PCP(log n, 1)). More informally, this means that the NP verifier described above can be replaced with one that just "spotchecks" a few places in the proof string, and using a limited number of coin flips can determine the correct answer with high probability. This allows several results about the hardness of approximation algorithms to be proven.
Example[edit]
This is a list of some problems that are in NP:
All problems in P, denoted . Given a certificate for a problem in P, we can ignore the certificate and just solve the problem in polynomial time.
The decision version of the travelling salesman problem is in NP. Given an input matrix of distances between n cities, the problem is to determine if there is a route visiting all cities with total distance less than k.
A proof can simply be a list of the cities. Then verification can clearly be done in polynomial time. It simply adds the matrix entries corresponding to the paths between the cities.
A nondeterministic Turing machine can find such a route as follows:
 At each city it visits it will "guess" the next city to visit, until it has visited every vertex. If it gets stuck, it stops immediately.
 At the end it verifies that the route it has taken has cost less than k in O(n) time.
One can think of each guess as "forking" a new copy of the Turing machine to follow each of the possible paths forward, and if at least one machine finds a route of distance less than k, that machine accepts the input. (Equivalently, this can be thought of as a single Turing machine that always guesses correctly)
A binary search on the range of possible distances can convert the decision version of Traveling Salesman to the optimization version, by calling the decision version repeatedly (a polynomial number of times).^{[citation needed]}
The decision problem version of the integer factorization problem: given integers n and k, is there a factor f with 1 < f < k and f dividing n?^{[citation needed]}
The Subgraph isomorphism problem of determining whether graph G contains a subgraph that is isomorphic to graph H.
The boolean satisfiability problem, where we want to know whether or not a certain formula in propositional logic with boolean variables is true for some value of the variables.
See also[edit]
Notes[edit]
References[edit]
 ^ Ladner, R. E. (1975). "On the structure of polynomial time reducibility". J. ACM. 22: 151–171. doi:10.1145/321864.321877. Corollary 1.1.
 ^ ^{a} ^{b} Kleinberg, Jon; Tardos, Éva (2006). Algorithm Design (2nd ed.). AddisonWesley. p. 464. ISBN 0321372913.
 ^ Alsuwaiyel, M. H.: Algorithms: Design Techniques and Analysis, p. 283
 ^ William Gasarch (June 2002). "The P=?NP poll" (PDF). SIGACT News. 33 (2): 34–47. doi:10.1145/1052796.1052804. Retrieved 20081229.
 ^ Kleinberg, Jon; Tardos, Éva (2006). Algorithm Design (2nd ed.). p. 496. ISBN 0321372913.
 ^ "Complexity Zoo:E  Complexity Zoo". complexityzoo.uwaterloo.ca. Retrieved 23 March 2018.
Further reading[edit]
 Thomas H. Cormen, Charles E. Leiserson, Ronald L. Rivest, and Clifford Stein. Introduction to Algorithms, Second Edition. MIT Press and McGrawHill, 2001. ISBN 0262032937. Section 34.2: Polynomialtime verification, pp. 979–983.
 Michael Sipser (1997). Introduction to the Theory of Computation. PWS Publishing. ISBN 053494728X. Sections 7.3–7.5 (The Class NP, NPcompleteness, Additional NPcomplete Problems), pp. 241–271.
 David Harel, Yishai Feldman. Algorithmics: The Spirit of Computing, AddisonWesley, Reading, MA, 3rd edition, 2004.
External links[edit]
 Complexity Zoo: NP
 Graph of NPcomplete Problems^{[dead link]}
 American Scientist primer on traditional and recent complexity theory research: "Accidental Algorithms"