Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "The Class NP Lecture 39 Section 7.3 Mon, Nov 26, 2007."— Presentation transcript:

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

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

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

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

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

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

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

8 The Class NP A potential solution to a problem is verifiable in polynomial time if the verifier V is a polynomial-time Turing machine.

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

10 The Class NP Let NTIME(t(n)) be the set of all languages that are decidable nondeterministically in time O(t(n)). Then

11 Nondeterministic Programs in C++ We will assume that we have available the following three functions. accept() reject() choose()

12 The choose() Function The function accept() outputs “accept.” The function reject() outputs “reject.” The function choose() takes a set S as its parameter.

13 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().

14 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|).

15 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(); }

16 The COMPOSITES Problem This is programming as it was meant to be.

17 Examples of NP Problems Some NP problems CLIQUE VERTEX-COVER SUBSET-SUM SAT 3SAT

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

19 A CLIQUE Algorithm CLIQUE(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(); }

20 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?

21 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(); }

22 The SUBSET-SUM Problem The SUBSET-SUM Problem: Given a set S = {a 1, a 2, …, a n } of positive integers and an integer k, does there is a subset {b 1, b 2, …, b m } of S such that b 1 + b 2 + … + b m = k?

23 The SAT Problem The SAT Problem: Given a Boolean expression f(x 1, x 2, …, x n ) in n Boolean variables x 1, x 2, …, x n, do there exist truth values for x 1, x 2, …, x n for which f(x 1, x 2, …, x n ) will be true? That is, is f satisfiable?

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


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

Similar presentations


Ads by Google