np-complete problems csc 331: algorithm analysis np-complete problems

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

Upload: joella-barrett

Post on 27-Dec-2015

234 views

Category:

Documents


0 download

TRANSCRIPT

Page 1: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

NP-Complete ProblemsCSC 331: Algorithm Analysis

NP-Complete Problems

Page 2: NP-Complete Problems CSC 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.

Page 3: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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

Page 4: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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”.

Page 5: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 6: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

NP-Complete ProblemsCSC 331: Algorithm Analysis

Satisfiability (SAT)

Is there a truth assignment that satisfies all clauses?

Page 7: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 8: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 9: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 10: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 11: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 12: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 13: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

NP-Complete ProblemsCSC 331: Algorithm Analysis

Traveling Salesperson Problem (TSP)

3

5

3

2

6

3

1

4

2 3

4

Page 14: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 15: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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)

Page 16: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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

Page 17: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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

Page 18: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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

Page 19: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 20: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 21: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 22: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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?

Page 23: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 24: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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?

Page 25: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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!

Page 26: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 27: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 28: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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

Page 29: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 30: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 31: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 32: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 33: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 34: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 35: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.

Page 36: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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?

Page 37: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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

Page 38: NP-Complete Problems CSC 331: Algorithm Analysis NP-Complete Problems

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.