NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems.

Post on 27-Dec-2015

232 views 0 download

Tags:

transcript

NP-Complete ProblemsCSC 331: Algorithm Analysis

NP-Complete Problems

NP-Complete ProblemsCSC 331: Algorithm Analysis

Search Problems

In past problems, we were searching for a solution from among an exponential population of possibilities.

A graph with n vertices has nn-2 spanning trees.

A typical graph has an exponential number of paths from s to t.

These problems could have been solved in exponential time by checking all candidate solutions.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Search Problems

An algorithm whose running time is 2n, or worse is all but useless in practice.

The quest for efficient algorithms is about finding clever ways to bypass this process of exhaustive search.

We have seen a number of algorithmic techniques that defeat exponentiality:

greedy, dynamic programming, divide-and-conquer

NP-Complete ProblemsCSC 331: Algorithm Analysis

Search Problems

Now we will look at some “search problems” in which the fastest known algorithms are exponential. Exponential solutions are said to be “intractable”.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Satisfiability (SAT)

This is a Boolean formula in conjunctive normal form.

A satisfying truth assignment is an assignment of false or true to each variable so that every clause contains a literal whose value is true.

SAT: given a Boolean formula in CNF, either find a satisfying truth assignment or else report that none exists.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Satisfiability (SAT)

Is there a truth assignment that satisfies all clauses?

NP-Complete ProblemsCSC 331: Algorithm Analysis

Satisfiability (SAT)

SAT is a typical search problem:

We are given an instance I, and asked to find a solution S. If no such solution exists, say so.

A search problem must have the property that any proposed solution S to an instance I can be quickly checked for correctness.

For SAT: check whether the assignment specified by

S indeed satisfies every clause in I.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Search Problems

A search problem is specified by an algorithm C that takes two inputs, an instance I and a proposed solution S, and runs in time polynomial in |I|. We say S is a solution to I if and only if C(I, S) = true.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Satisfiability (SAT)

Researchers over the past 50 years have tried hard to find efficient ways to solve SAT.

The fastest algorithms we have are still exponential on their worst-case inputs.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Satisfiability (SAT)

There are two natural variants of SAT for which we do have good algorithms.

If all clauses contain at most one positive literal, then the Boolean formula is called a Horn formula, and a satisfying truth assignment, if one exists, can be found by a greedy algorithm.

If all clauses have only two literals, then SAT can be solved in linear time by finding the strongly connected components of a particular graph.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Satisfiability (SAT)

If we allow clauses to contain three literals, then the resulting problem (3SAT) once again becomes hard to solve.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Traveling Salesperson Problem (TSP)

Given n vertices 1, ..., n and all n(n - 1)/2 distances between them, as well as a budget, find a tour of total cost b or less, or report that no such tour exists.

A tour is a cycle that passes through every vertex exactly once.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Traveling Salesperson Problem (TSP)

3

5

3

2

6

3

1

4

2 3

4

NP-Complete ProblemsCSC 331: Algorithm Analysis

Traveling Salesperson Problem (TSP)

Algorithm 1: try all (n - 1)! tours.

Algorithm 2: dynamic programming

For a subset of cities S ⊆ {1, 2, ..., n} that includes 1, and j ∈ S, let C(S, j) be the length of the shortest path visiting each node in S exactly once starting at 1 and ending at j.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Traveling Salesperson Problem (TSP)

C({1},1) = 0for s = 2 to n:

for all subsets S {1,2,...,n} of size s and ⊆containing 1:

C(S,1) = ∞for all j S, j≠1:∈

C(S,j) = min{C(S-{j},i) + dij : i S, i≠j}∈

return minj C({1,...,n},j) + dj1

O(n22n)

http://www.pbs.org/wgbh/nova/tech/making-more-stuff.html#making-stuff-faster

NP-Complete ProblemsCSC 331: Algorithm Analysis

Konigsberg Bridge ProblemIn 1735, Euler was walking the bridges of Konigsberg.

He noticed that it seemed impossible to cross each bridge exactly once.

Smallisland

Southern bank

Northern bank

Bigisland

NP-Complete ProblemsCSC 331: Algorithm Analysis

Konigsberg Bridge Problem

We are looking for a path that goes through each edge exactly once (the path is allowed to repeat vertices).

In other words, when can a graph be drawn without lifting the pencil from the paper?

If and only if:

(a) the graph is connected(b) every vertex, with the possible exception of two vertices, has even degree

NP-Complete ProblemsCSC 331: Algorithm Analysis

Knight’s Tour (Rudrata or Hamiltonian)

Can one visit all the squares of the chessboard, without repeating any square, in one long walk that ends at the starting square and at each step makes a legal knight move?

This is a graph problem:

Graph has 64 vertices, and two squares are joined by an edge if a knight can go from one to the other in a single move.

Find a cycle that goes through all vertices, without repeating any vertex.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Knight’s Tour (Rudrata or Hamiltonian)

This problem is similar to TSP. No polynomial algorithm is known for it.

Note that the main difference between Konigsburg (Euler) and Knight’s Tour (Rudrata) is that Euler visits all edges while Rudrata visits all vertices.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Longest Path

We know the shortest-path problem can be solved very efficiently, but how about the longest path problem?

To avoid trivial solutions we require that the path be simple, containing no repeated vertices.

No efficient algorithm is known for this problem.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Graph coloring

Given a graph and k colors, can you color every vertex so that no edge connects vertices of the same color?

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

A search problem is specified by an algorithm C that takes two inputs, an instance I and a proposed solution S, and runs in time polynomial in |I|. We say S is a solution to I if and only if C(I, S) = true.

Moreover the running time of C(I, S) is bounded by a polynomial in |I|, the length of the instance.

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

We denote the class of all search problems by NP.

The class of all search problems that can be solved in polynomial time is denoted P.

Are there any search problems that cannot be solved in polynomial time?

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

In other words, is P ≠ NP?

Most algorithms researchers think so.

The task of finding a proof for a given mathematical assertion is a search problem and is therefore in NP.

So if P = NP, there would be an efficient way to prove theorems, thus eliminating the need for mathematicians!

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

There are a variety of reasons why it is widely believed that P ≠ NP.

However, proving this has turned out to be extremely difficult, one of the deepest and most important unsolved puzzles in mathematics.

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

P stands for “polynomial”.

NP stands for “nondeterministic polynomial time”.

It means that a solution to any search problem can be found and verified in polynomial time by a special sort of algorithm, called a nondeterministic algorithm.

Such an algorithm has the power of guessing correctly at every step.

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

Hard problems (NP-complete) Easy problems (in P)

3SAT 2SAT, Horn SAT

TSP MST

Longest Path Shortest Path

3D Matching Bipartite Matching

Knapsack Unary Knapsack

Independent Set Independent Set on trees

Integer Linear Programming Linear Programming

Rudrata Path Euler Path

Balanced Cut Minimum Cut

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

Even if we accept that P ≠ NP, on what evidence do we believe that the listed hard problems have no efficient algorithm.

Such evidence is provided by reductions, which translate one search problem into another.

They demonstrate that the problems are all, in some sense, exactly the same problem, except they are stated in different languages.

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

We can also use reductions to show that these problems are the hardest search problems in NP.

If even one of these problems has a polynomial time algorithm, then every problem in NP has a polynomial time algorithm.

Thus if we believe P ≠ NP, then all these search problems are hard.

NP-Complete ProblemsCSC 331: Algorithm Analysis

P and NP

A reduction from search problem A to search problem B:

a polynomial-time algorithm f that transforms any instance I of A into an instance f(I) of B,

a polynomial-time algorithm h that maps any solution S of f(I) back into a solution h(S) of I.

A search problem is NP-complete if all other search problems reduce to it.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Reductions

The book shows that the following search problems can be reduced to one another. As a consequence, they are all NP-complete.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Reduction Example

Lpath(G, a, b, k) is a decision problem – is there a simple path from a to b in G of length at least k?

Show Lpath is NP-Complete.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Reduction Example

Lpath(G, a, b, k) is a decision problem – is there a simple path from a to b in G of length at least k?

Show Lpath is NP-Complete.

Hint: Use Hamiltonian Paths.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Reduction Example

Lpath(G, a, b, k) is a decision problem – is there a simple path from a to b in G of length at least k?

Suppose that I want to know if a Hamiltonian Path exists in G. Let k be the number of nodes in G. Lpath(G,a,b,k) solves the Hamiltonian Path problem.

NP-Complete ProblemsCSC 331: Algorithm Analysis

Reduction Exercise

Examination Scheduling

Given a list of courses, a list of conflicts between them, and an integer k; is there an exam schedule consisting of k dates such that there are no conflicts between courses which have examinations on the same date?

NP-Complete ProblemsCSC 331: Algorithm Analysis

Reduction Exercise

Examination Scheduling

Given a list of courses, a list of conflicts between them, and an integer k; is there an exam schedule consisting of k dates such that there are no conflicts between courses which have examinations on the same date?

Hint: Use graph coloring

NP-Complete ProblemsCSC 331: Algorithm Analysis

King Arthur’s DinnerKing Arthur invites his knights to dinner, but some of the knights are arguing. King Arthur wants to sit the knights all around the round table, but he would like not to sit arguing knights next to each other.

He would like you to write a program that would tell whether or not there is a way to sit all the knights around the table without seating arguing knights next to each other.

-How can you solve the problem?-Prove the solution is NP-Complete.