Computer Algorithms NP-Complete Problems NP-completeness The quest for efficient algorithms is about finding clever ways to bypass the process of exhaustive search, using clues from the input in order to dramatically narrow down the search space. NP-completeness is a form of bad news. Evidence that many important problems can't be solved quickly. 1
Computational Complexity Theory This subject is dedicated to classifying problems by how hard they are. Yes-or-no problems We care more about Does a certain structure exist rather than how do I find the structure. We will discuss the classification defined in terms of yes-or-no problems. Classification of Problems P : Problems that can be solved in polynomial time. T(n) = O(n c ) Example: sorting, minimum spanning tree E : Problems that can be solved in exponential time if no polynomial-time algorithm can be developed for it. T(n) = O(n u(n) ) 2
Classification of Problems NP : Nondeterministic polynomial time. Nondeterministic is guessing a solution. A problem is in NP if you can quickly (in polynomial time) test whether a solution is correct (without worrying about how hard it might be to find the solution). Problems in NP are still relatively easy: if only we could guess the right solution, we could then quickly test it. Classification of Problems Undecidable: For some problems, we can prove that there is no algorithm that always solves them, no matter how much time is allowed. There are as many problems as there are real numbers, and only as many programs as there are integers, so there are not enough programs to solve all the problems. 3
Long Simple Paths A simple path in a graph is just one without any repeated edges or vertices. Problem of finding long simple path: Given a graph G, vertices s and t, and a number k, does there exist a simple path from s to t with at least k edges? We do not know whether it is in P. So far, no algorithm running in polynomial time that solves this problem. It is in NP: Testing whether the answer is correct could be done in linear time. Examination Scheduling A school has n courses and m days in which to schedule examinations. An optimal schedule would be one where no student has to take two examinations on the same day. There are O(m n ) possible different schedules. It is in NP: it is difficult to find a good one, it is easy to check a schedule to see how near perfect it is. 4
Knapsack Given n items, each with a weight and a value, is there a collection of items with total weight less than W, which has a total value greater than g? A dynamic programming scheme for KNAPSACK with running time O(nW), which is exponential in the input size since it involves W rather than logw. And we have the usual exhaustive algorithm as well, which looks at all subsets of items - all 2 n of them. It is in NP : it is easy to test whether a solution is correct. Hamiltonian Cycle Does a given graph G have a cycle visiting each vertex exactly once? It is in NP : Perform an exhaustive search for all possible path ( not polynomial time) Test whether one path is a Hamiltonian cycle (in polynomial time) 5
Traveling Salesman Problem In the traveling salesman problem (TSP) we are given n vertices and all n(n-1)/2 distances (cost) between them, as well as a budget b. We are asked to find a tour, a cycle that passes through n vertex exactly once, of total cost is b or less. It is in NP : In fact, n factorial different tours are possible. And, if we have a tour, we can easily check to see the cost of it. Problems of complexity theory The most famous open problem in theoretical science is whether P = NP. In other words, if it's always easy to check a solution, should it also be easy to find the solution? It's false. But we also don't have a proof... 6
Reduction One problem is easier than another. A is easier than B (A < B) if we have an algorithm for solving A that uses a small number of calls to a subroutine for B. Easier means if one problem can be solved in polynomial time, so can the other. It is possible for the algorithms for A to be slower than those for B, even though A < B. If A < B, and B is in P, so is A. Reduction Example Hamiltonian cycle vs. longest simple path Solution with longest simple path as subroutine for each edge (u,v) of G if there is a simple path of length n-1 from u to v return yes //path + edge from a cycle return no This solution does O(m) work outside m calls to the subroutine. Hamiltonian cycle < longest simple path 7
NP-completeness A problem A in NP is NP-complete when, for every other problem B in NP, B < A. Theorem: an NP-complete problem exists. if A < B and B < C, then A < C. if A is NP-complete, B is in NP, and A < B, B is NPcomplete. We start with one specific problem that we prove NP-complete, and we then prove that it's easier than lots of others which must therefore also be NP-complete. Hamiltonian cycle is known to be NP-complete. 8
What we should do? Use a heuristic : a solution to a reasonable fraction of the common cases. Solve the problem approximately instead of exactly. Use an exponential time solution anyway. If you really have to solve the problem exactly. 9