CISC 4080 Yanjun Li
Computer Algorithms
NP-Complete Problems
• 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.
NP-completeness
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.
CISC 4080 Yanjun Li
• P : Problems that can be solved in polynomial time.
– T(n) = O(nc)
– 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(nu(n))
Classification of Problems
CISC 4080 Yanjun Li
• 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.
Classification of Problems
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.
CISC 4080 Yanjun Li
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(mn) 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
CISC 4080 Yanjun Li
• 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 2nof them.
• It is in NP : it is easy to test whether a solution is correct.
Knapsack
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)
CISC 4080 Yanjun Li
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
CISC 4080 Yanjun Li
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
– This solution does O(m) work outside m calls to the subroutine.
• Hamiltonian cycle < longest simple path
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
CISC 4080 Yanjun Li
• 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 NP- complete.
• 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.
NP-completeness
CISC 4080 Yanjun Li