Class NP, NP - Complete, and NP - Hard Problems
Class NP, NP - Complete, and NP - Hard Problems
Class N P
Class N P contains all computational problems such that the corresponding decision problem can be solved in a polynomial time by a nondeterministic Turing machine. I.e. N P is a time complexity class which contains a set of problems. We should just dene what are corresponding decision problems, a non-deterministic Turing machine, and the time requirement of a Turing machine.
1.1
Any problem which in principle can be modelled to be solved by a computer, is called a computational problem. Examples of computational problems are: Calculate the answer of an arithmetic expression e. Sort customers names into alphabetic order. Given a map of cities and roads, search the shortest route which goes through all cities exactly once and returns to the starting point, if such exists.
Non-computational problems cannot be solved by a computer or by any mechanical means. They are often related philosophy, ethics, or emotions. For example, What is the meaning of life? Is it right to eat meat on Fridays? Phantom and Batman are ghting. Who wins? In computer science, we consider only computational problems. For any computational problem, we can inevent at least one related decision problem. The decision problem can have only answers yes or no. For example, the following decision problems correspond the previous computational problems: Given an arithmetic expression e and some number x, is x the solution of e? Given a list of customers names, are they in an alphabetic order? Given a map of cities and roads, does it contain a route which goes through all cities exactly once and is at most 50 km? Such decision problems can be used to estimate the diculty of the original problem. If the corresponding decision problem requires exponential time, the original problem cannot be solved any faster, and it is also (at least) exponential. The goal is to invent such a decision problem which measures the diculty of the original problem as well as possible (i.e. has the same diculty).
1.2
A Turing machine is an abstract model of computation. According to ChurchTuring thesis, any problem which can be solved by a computer can be solved by a Turing machine. (Note that this is only a thesis, and nobody has been able to prove it universally true, but it is widely believed to be true.) This means that we can use Turing machines to nd out whether a problem is solvable or not. If we can solve it by a Turing machine which halts by all inputs, then the problem is solvable, and otherwise unsolvable. In addition, Turing machines can be used to estimate how dicult a problem is or what is its time or space complexity. For example, if a problem requires a Turing machine which runs in exponential time, we cannot invent a Java program 2
which would solve the problem in a polynomial time. The benet of Turing machines is that they hide all implementation details and it is easier to make proofs and analyze the complexity of problems. Turing machines are divided into two main types: deterministic and nondeterministic. A deterministic machine is like a state machine which reads the input character by character and enters to the next state according to the current state and the read character. In the same time, it writes a character to the tape and moves its reading-writing head to the right or left. This means that the functionality of the machine is fully determined by the current state and the currently read character. The computation path is always one sequence of states with any input (from the beginning state to the nal state). A non-deterministic Turing machine works otherwise like a deterministic machine, but it can have states where there are several alternative successor states, given the same input character. This means that the computation is not a direct path, but contains branches (it is like a tree with several paths from the same beginning state to nal states). The macine always selects the correct alternative among possible successor states. I.e. if there is at least one possible path which leads to the accepting nal state, the machine accepts the input (answers yes), and otherwise it rejects it (answers no). You can imagine that the non-deterministic machine has power to guess the correct path or that it simulates all possible (direct) computation paths simultaneuosly and selects the best one. (If we want to implement a non-deterministic Turing machine as a computer program, we really have to simulate all possible computations with that input and test if any of them leads to the accepting nal state.)
1.3
The time complexity of a problem is estimated by Turing machines which solve the problem. If we know a solution (a Turing machine) to the problem and it has complexity x, we know that the problem has at most complexity x. It is possible that in the future somebody invents a faster solution, and thus x is only an upperbound for the complexity. Lets take an example: Lets suppose that we have a Turing machine M which solves the Travelling Salesmans problem. We give the machine an input, i.e. a graph which describes the cities and roads, and ask if it contains a route of length 20 km which goes through all cities exactly once and returns to the starting place. The size of the input is measured by the number of cities n and the number of roads between them (edges of the graph), e. We know that a
graph of n nodes can contain at most n(n21) edges1 , and thus the input size is at most n(n21) . Now we can express the time requirement of the Turing machine as a number of steps it takes to solve the problem. I.e. how many steps the machine takes, before it can answer yes, the map contains such a route or no, it doesnt. The time requirement should be expressed as a function of n, e.g. f (n), so that for any input the machine takes at most f (n) steps. This is the time complexity of the solution algorithm. In addition, it tells that the problem has at most complexity f (n) (so it can be less complex, if we just invent a better solution). Note that usually the complexity of a solution algorithm is expressed by O-notation which tells only the order of function f (n). I.e. we give a simpler function g (n) which has the same growing rate as f (n). For example, if f (n) = 77n5 +16n3 12n2 +3, we say that the worst case complexity is O(n5 ), because it is the most important term in f (n) and dominates its growth when n increases. The constant factor 77 is dropped from the simpler expression. 2 2 Or, if f (n) = a2n 5n+7 + 3n2 , we say that the complexity is O(2n ). The simpler function g (n) doesnt have to hold for small values of n, but it is enough that the real comlexity f (n) is at most ag (n) when n is high enough, given some constant factor a. I.e. we can use complexity O(g (n)) instead of f (n), if f (n) ag (n), when n n0 for some n0 and some constant a.
1.4
If a problem can be solved by a deterministic Turing machine in polynomial time, the problem belongs to the complexity class P . All problems in this class have a solution whose time requirement is a polynom on the input size n. I.e. f (n) is of form ak nk + ak1 nk1 + ... + a2 n2 + a1 n + a0 where ak , ..., a0 are contant factors (possibly 0). The order of polynom is the largest exponent k such that ak = 0 (if ak = 0, then the polynom is ak1 nk1 + ... + a0 and the order is k 1, unless ak1 = 0. If ak1 = 0, too, then the polynom is ak2 nk2 + ... + a0 etc.). If the problem can be solved by a deterministic Turing machine in exponential time, then it belongs to class E . The time complexity can be for example f (n) = 2n or f (n) = nn . It doesnt matter, if f (n) contains also polynomial part, because the exponential part (where n is in the exponent) dominates the complexity. So, for example f (n) = 2n/3 +10x10 has complexity O(2n/3 ) = O(2n ), and is an exponential function. Note that if a problem belongs to class P , then it also belongs to class E , because P
The rst city can have roads to n 1 other cities, the second one to n 2 cities in addition to city 1, the third city to n 3 cities, etc.
1
E , but usually we give only the smallest (fastest) class where the problem belongs. Note also that E is not the most complex class of problems, but for example problems with complexity O(n!) (n! = n(n 1)(n 2)...1) are more dicult. An interesting class of problems is class N P which contains all problems which can be solved by a non-deterministic Turing machine in polynomial time. Class N P is between classes P and E : E N P E . This means that all problems in class N P belong to class E , too, and they can be solved by a deterministic Turing machine (or a computer program) in an exponential time. It is also possible that some of them can be solved faster, in polynomial time, and they actually belong to class P . However, it is not known, if all of them could be solved in polynomial time by some clever algorithms. So, the important open question is whether P = N P or P N P . (A million dollars is yours, if you solve this!)
N P -complete problems
N P -complete problems are special problems in class N P . I.e. they are a subset of class N P . An problem p is N P -complete, if 1. p N P (you can solve it in polynomial time by a non-deterministic Turing machine) and 2. All other problems in class N P can be reduced to problem p in polynomial time. This means that the N P -compelete problems are the most dicult problems in class N P . If we could solve just one of them in a polynomial time, we could solve all problems in class N P in a polynomial time (and win 1 000 000 dollars). If you want to show a new problem to be N P -complete, you have to do two things: 1. Invent a non-deterministic Turing machine which solves the problem in polynomial time. 2. Reduce one known N P -complete problem to the new problem in a polynomial time. The rst part is usually easy. You just construct a machine which contains two parts. The rst part guesses an answer non-deterministically (e.g. a list 5
of cities c1 , c2 , ..., cn for TSP) and the second part checks that the answer is correct (e.g. the list of cities contains all cities exactly once and the length of route c1 c2 c3 ... cn c1 is less than required). The idea of the second part is the following: 1. You want to show that all poblems in class N P can be reduced to problem p. 2. You select a known N P -complete problem q . This means that all problems pi N P can be reduced to q in polynomial time poli (and you dont have to show it anymore!). 3. You give a method to reduce q to p in polynomial time pol 4. Because all pi N P can be reduced to q and q can be reduced to p, all pi N P can be reduced to p in time poli + pol which is also a polynom (a polynom + polynom is still a polynom). Notice that if your reduction from p to q would take exponential time, then you could just say that you can reduce all problems in N P to p in exponential time (an exponential function + a polynom = an exponential function). Thats why the reduction should also take a polynomial time. Inventing the reductions is often a matter of art, and it is best to study some exaples from text books. Usually it is enough to describe the idea of the reduction method and show that it takes polynomial time. The reduction from q to p should be such that 1. It transforms all inputs of problem q to inputs of problem p, and 2. If the solution to p is yes, the solution to q is yes, and if the solution to p is no, the solution to q is no. For example, we can reduce Independet set (IS) problem to Clique problem as follows: If graph G contains an independent set of size k (i.e. there are at least k vertices which are not connected to each other), then the complement graph G contains a clique of size k (there are at least k vertices which are all connected to each other). Complement graph G means a graph which contains an edge between two nodes, if G didnt, and it doesnt contain an edge between two nodes, if G did. Thus, the reduction is a method, which trasfroms G to its complement graph G. This reduction can be done in a polynomial time, because we just have to change 0s and 1s in the matrix describing graph G. The matrix contains 6
n n cells, but if the graph is undirected, it is enough to use only n(n21) of them (If v 1 is connected to v 2, then v 2 is also connected to v 1. Edges from the vertex to itself, e.g. from v 1 to v 1, are not marked.) So the reduction takes time O(n2 ) and is polynomial.
N P -hard problems
N P -hard problems are partly similar but more dicult problems than N P complete problems. They dont themselves belong to class N P (or if they do, nobody has invented it, yet), but all problems in class N P can be reduced to them. Very often, the N P -hard problems really require exponential time or even worse. Notice that N P -complete problems are a subset of N P -hard problems, and thats why N P -complete problems are sometimes called N P -hard. It can also happen that some problem which is nowadays known to be only N P -hard, will be proved to be N P -complete in the future it is enough that somebody just invents a nondeterministic Turing machine, which solves the problem in polynomial time. To summarize the relationships: N P -complete N P -hard N P -complete N P P NP E