The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

24
The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007

Transcript of The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

Page 1: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The Class NP

Lecture 39Section 7.3

Mon, Nov 26, 2007

Page 2: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

Nondeterministic Algorithms

• When we solve a problem nondeterministically, we do not just magically write down the solution.

• We must be able to• Generate the solution in polynomial

time, and• Verify the solution in polynomial time.

Page 3: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The COMPOSITES Problem

• The COMPOSITES Problem: Given an integer m, determine whether it is composite.

• The integer m is composite if there exist integers a and b, both greater than 1, such that m = ab.

Page 4: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The COMPOSITES Problem

• How do we generate a solution in polynomial time?

• Let n be the length of the integer m, in bits.

• To choose a divisor a, we choose n bits to create the binary representation of a.

Page 5: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The COMPOSITES Problem

• Then do the same for b.• That can be done in polynomial

time.• How do we verify that m = ab?• We just multiply a times b and

compare to m.• That too can be done in polynomial

time.

Page 6: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

Verifiers

• A verifier for a language A is an algorithm V such that

A = {w | V accepts w, c for some string c}.

• c is a string containing additional information needed for verification.

• c is called a certificate.

Page 7: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

Verifiers

• The language of the COMPOSITES problem is

{m | m is composite}.• The verifier is a Turing machine

that accepts m, a, b whenever m = ab.

Page 8: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The Class NP

• A potential solution to a problem is verifiable in polynomial time if the verifier V is a polynomial-time Turing machine.

Page 9: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The Class NP

• The class NP is the class of all languages whose members can be generated in polynomial time and that have polynomial-time verifiers.

Page 10: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The Class NP

• Let NTIME(t(n)) be the set of all languages that are decidable nondeterministically in time O(t(n)).

• Then

0

)(NTIMENPk

kn

Page 11: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

Nondeterministic Programs in C++

• We will assume that we have available the following three functions.• accept()• reject()• choose()

Page 12: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The choose() Function

• The function accept() outputs “accept.”

• The function reject() outputs “reject.”

• The function choose() takes a set S as its parameter.

Page 13: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The choose() Function

• choose() will return a single value from the set S.

• If there is a choice of values from S that will eventually lead to calling accept() and a choice that leads to calling reject(), then choose() will choose a value that leads to accept().

Page 14: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The choose() Function

• If there is no such choice, i.e., every choice leads to accept() or every choice leads to reject(), then choose() will choose a random value from the set S.

• The execution time of choose() is O(|S|).

Page 15: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The COMPOSITES Problem

COMPOSITE(m){ a = 0; b = 0;// Generate values for a and b for (i = 1; i <= n; i++) { a = 2*a + choose({0, 1}); b = 2*b + choose({0, 1}); }// Verify the choice if (m == a*b && a > 1 && b > 1) accept(); else reject();}

Page 16: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The COMPOSITES Problem

• This is programming as it was meant to be.

Page 17: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

Examples of NP Problems

• Some NP problems• CLIQUE• VERTEX-COVER• SUBSET-SUM• SAT• 3SAT

Page 18: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The CLIQUE Problem

• Given a graph G, a clique of G is a complete subgraph of G.

• The CLIQUE Problem: Given a graph G and an integer k, does G contain a clique of size k?

• Let n be the number of vertices in G.

Page 19: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

A CLIQUE AlgorithmCLIQUE(G, k){// Generate a clique C = {}; for (i = 0; i < n; i++) if (choose({true, false})) Add vertex i to C;// Verify the clique if (size of C != k) reject(); for (i = 0; i < k; i++) for (j = 0; j < k; j++) if (i != j && (C[i], C[j]) E) { reject(); exit(); } accept(); }

Page 20: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The VERTEX-COVER Problem

• Given a graph G, a vertex cover of G is a set of vertices S of G such that every edge of G is incident to a vertex in S.

• The VERTEX-COVER Problem: Given a graph G and an integer k, does G have a vertex cover of size k?

Page 21: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

A VERTEX-COVER Algorithm

VERTEX_COVER(G, k){// Generate a vertex cover C = {}; for (i = 0; i < n; i++) if (choose({true, false}) Add vertex i to C;// Verify the vertex cover if (size of C != k) reject(); for (i = 0; i < n; i++) // For every edge in G for (j = i + 1; j < n; j++) if (edge(i, j) G) { bool ok = false; for (v = 0; v < k; v++) // For every vertex in C if (C[v] is incident to edge(i, j)) ok = true; if (!ok)

reject(); } accept(); }

Page 22: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The SUBSET-SUM Problem

• The SUBSET-SUM Problem: Given a set S = {a1, a2, …, an} of positive integers and an integer k, does there is a subset {b1, b2, …, bm} of S such that

b1 + b2 + … + bm = k?

Page 23: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The SAT Problem

• The SAT Problem: Given a Boolean expression f(x1, x2, …, xn) in n Boolean variables x1, x2, …, xn, do there exist truth values for x1, x2, …, xn for which f(x1, x2, …, xn) will be true?

• That is, is f satisfiable?

Page 24: The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007.

The 3SAT Problem

• The 3SAT Problem: This is the same as SAT except that the Boolean expression is in conjunctive normal form with exactly 3 literals in each clause.