the class np

53
The Class NP Zeph Grunschlag

Upload: asta

Post on 12-Jan-2016

23 views

Category:

Documents


0 download

DESCRIPTION

The Class NP. Zeph Grunschlag. Agenda. Nondeterministic complexity The class NP Defined by… …acceptance by poly-time NTM’s …positive instance give poly-size proofs …acceptance by poly-time verifiers Examples in: Punch-Card Puzzle SAT (satisfiability of Boolean expressions) - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: The Class  NP

The Class NP

Zeph Grunschlag

Page 2: The Class  NP

AgendaNondeterministic complexity The class NP Defined by…

…acceptance by poly-time NTM’s …positive instance give poly-size proofs …acceptance by poly-time verifiers

Examples in: Punch-Card Puzzle SAT (satisfiability of Boolean expressions)

Variants: CSAT, nSAT

The class Co-NPPolynomial time reductions

Page 3: The Class  NP

A Punch-Card Puzzle

Consider punch cards which contain 3 notches and a number of holes contained in two columns

Page 4: The Class  NP

A Punch-Card Puzzle

In general, the notches must always be there, as they go on 3 pegs, but holes may or may not be filled.

Page 5: The Class  NP

A Punch-Card Puzzle

Pictured are punch cards on the pegs:

Page 6: The Class  NP

A Punch-Card PuzzleThe point of the puzzle is to try to

put all the punch-cards on the pegs, in such a way that every possible hole is opaque. Flipping cards is allowed.

Page 7: The Class  NP

A Punch-Card PuzzleQ: Find a solution to this instance:

C1 C2 C3 C4

Page 8: The Class  NP

A Punch-Card PuzzleA: Solution is C1, C2, C3, flip(C4):

C1 C2 C3 C4

Page 9: The Class  NP

A Punch-Card PuzzleA: Solution is C1, C2, C3, flip(C4):

C1 C2 C3 C4

Page 10: The Class  NP

A Punch-Card PuzzleA: Solution is C1, C2, C3, flip(C4):

C1 C2 C3 C4

Page 11: The Class  NP

A Punch-Card PuzzleA: Solution is C1, C2, C3, flip(C4):

C1 C2 C3 C4

Page 12: The Class  NP

A Punch-Card Puzzle

Q: Can puzzle be solved systematically?

Page 13: The Class  NP

Punch-Card Puzzleis Decidable

A: Yes. Here’s an algorithm:SolvePuzzle(cards C1, C2, … Cn)

for(i1= 0 to 1)

if (i1= 1) flip C1

for(i2= 0 to 1)

if (i2= 1) flip C2 …

for(in= 0 to 1)

if (in= 1) flip Cnput cards on pegsif (no holes) ACCEPT

REJECT

Q: Running time?

Page 14: The Class  NP

Punch-Card Puzzleis Decidable

A: Running time: In worst case, reject, and have to go through all possible iterations: n nested for loops each with two possibilities so worst-case running time is (2n). Therefore, the algorithm has exponential running time.

Q: Can you come up with a polynomial-time algorithm?

Page 15: The Class  NP

Punch-Card Puzzleis NP-Complete

A: If so, you win a $1,000,000!!!This is because it turns out that this

problem is NP-complete, as will be explained in the next two lectures. Solving this problem quickly would yield a fast solution to thousands of other important algorithmic problems in CS, including the breaking of the RSA crypto-system! So could parlay $1,000,000 into billions and more (if you’re evil) !

Page 16: The Class  NP

NondeterministicRunning Time

Recall that in a nondeterministic TM, a computation consists of a computation tree, whose branches from root to leaves are the various possible computations that could take place.

DEF: The nondeterministic running time of a nondeterministic Turing machine N is the function f (n) which is the maximum number of transitions that N takes to halt (or crash) on any computation branch when given an arbitrary input of length n.

Q: When is N said to be a decider?

Page 17: The Class  NP

NondeterministicRunning Time

A: When f (n) is finite for all possible n.

Consider the NTM decider for (ab)*b(ab) (from lecture 16):

Q: What is the running time?

0 1 32

a|bR

bR

a|bR �L

Page 18: The Class  NP

NondeterministicRunning Time

A: f (n) = n + 1. Since all transitions move right, the longest possible branch is the accepting branch that goes through entire string, then sees and halts.

We can also see this from the computation tree. The running time of a computation is the height of the tree. EG:

0 1 32

a|bR

bR

a|bR �L

Page 19: The Class  NP

Nondeterministic Running Time

height = f (5) = 6

0 1 32

a|bR

bR

a|bR �L

0ababa

a0baba

ab0aba ab1aba

aba0ba

abab0a abab1a

aba2ba

crash

ababa0

crash

ababa2

abab3a

Page 20: The Class  NP

Nondeterministic Time Complexity Classes

We can define language classes based on nondeterministic TM’s just as we did with deterministic TM’s

DEF: Suppose that g (n) is a real function. The nondeterministic time complexity class NTIME(g (n) ) consists of all languages which are decided by some NTM with running time O(g (n)). Any such language is said to be of nondeterministic time complexity g (n).

Q: Give a simple function g (n) for which previous example was in NTIME(g (n) ).

Page 21: The Class  NP

The Class NPA: g (n) = n. This is an example of a

nondeterministic polynomial time TM as defined below.

NP is the class of languages which are decided by a NTM of nondeterministic time complexity some polynomial. IE:

DEF 1:

0

)(NTIMEk

kn NP

Page 22: The Class  NP

The Class NP Alternate Definitions: Short Proofs

Alternately, NP is the class of languages whose positive instance can be proved (or verified) to be in the language in polynomial time. Though the search for a proof may take arbitrarily long, we are only concerned with the proof itself being short.

Q: According to second definition, is the Punch-Card Puzzle in NP?

Page 23: The Class  NP

Puzzle ExampleShort Proof

A: Yes. If a solution to the puzzle has been found one can prove quickly that the found solution really is a solution.

EG: The proof runs as follows on 4-card example:

Page 24: The Class  NP

Puzzle ExampleShort Proof

C1 C2 C3 C4CLAIM: Solution is C1, C2, C3, flip(C4).1) C2 covers (1,L) 2) C2 covers (1,R)3) C3 covers (2,L)

… and so on …12)flip(C4) covers (6,R)Q: How long would a proof be for n cards, m rows?

Page 25: The Class  NP

Puzzle ExampleShort Proof

C1 C2 C3 C4A: For m rows we require 2m+1 lines in

the proof. First line is O (n) characters long. All other lines are O(1). So in total, length of proof is O(m+n). Input size of problem is also O(m+n) because can describe each card with O(m) characters. Thus proof is a linear size proof, and puzzle is in NP.

Q: How would one prove that a puzzle has no solution?

Page 26: The Class  NP

The Class Co-NPA: That is seemingly a much harder

problem. There is seemingly no systematic proof that no solution exists except for trying every possible flip/no-flip combination and verifying that holes always remain. The problem of deciding if a puzzle has no solution is in Co-NP:

DEF: The class Co-NP consists of languages whose complements are in NP.

Page 27: The Class  NP

The Class NP Alternate Definitions: TM Verifiers

One way of making the notion of short proof rigorous is by using TM verifiers:

DEF: A Turing machine verifier is a deterministic 2-tape TM which accepts any input on its 1st tape when some initial 2nd tape contents exists which allows the TM to accept. The initial contents of the 2nd tape are called the certificate or proof.

Q: Give a TM verifier for punch-card puzzle.

Page 28: The Class  NP

The Class NP Alternate Definitions: TM Verifiers

A: The second tape contains a bit string of length n (the no. of cards). Each bit is 1 if the corresponding card is flipped and 0 if not. The algorithm:

“On input <punch-cards>1. Flip each card according to 2nd tape.2. Verify that each possible hole is filled.”

Step 1 is O (n) and step 2 is O (m) on a RAM. So algorithm is poly-time on a RAM and therefore poly-time on a TM.

Page 29: The Class  NP

The Class NP Alternate Definitions: TM Verifiers

DEF2: NP is the class of languages which are accepted by a TM verifier of polynomial time complexity.

NOTE: This means that can assume that certificates (proofs) are polynomial in the size of the problem, as polynomial time ensures that only a polynomial-size part of 2nd tape can be explored, and hence could restrict certificate to be poly-size.

Page 30: The Class  NP

The Class NPEquivalence of Definitions

LEMMA: DEF1 and DEF2 are equivalent.Proof outline. DEF1 DEF2: One converts a NTM

decider into a verifier by using the successful computation branch as the proof. Poly-time ensures that the branch is poly-size so verification (which amounts to showing that each node is a legitimate child) is poly-time.

Page 31: The Class  NP

The Class NPEquivalence of Definitions

DEF2 DEF1: One converts a verifier into a NTM decider by appending a NTM certificate generator on the second tape, before composing with the verifier. To ensure that the running time is polynomial, one needs to make sure that the certificate generator halts when a given certificate exceeds the minimum necessary for verification.

�Q: Give a nondeterministic algorithm for

card puzzle.

Page 32: The Class  NP

Punch-Card PuzzleNondeterministic

AlgorithmA: NondeterministicSolvePuzzle(cards C1, C2,

…,Cn)for(i = 1 to n)

flip CiOR skip //nondeterministic “OR”

put cards on pegsif (no holes) ACCEPTREJECT

Q: Running time?

Page 33: The Class  NP

Punch-Card PuzzleNondeterministic

AlgorithmA: Running time is O (m + n)

nondeterministic time on a RAM since have a for-loop of size n with O (1) work each time, and checking to see that there are no holes is O (m). So in terms of input, nondeterministic algorithm in linear on a RAM, so poly-time on a NTM.

Page 34: The Class  NP

Polynomial Time Reductions

Recall that a language A is Turing reducible to a languages B when a decider for A can be constructed assuming that a decider for B exists. When dealing with complexities, would like to also make sure that reduction completes in polynomial time:

DEF: If there is a computable polynomial time function f such that f (A)B and f (A ) B then A is polynomial time mapping reducible to B .

Similarly, can define polynomial time co-mapping reductions.

Page 35: The Class  NP

Poly-Time ReductionNotation

A is poly-time mapping reducible to B:

A P B

A is poly-time co-mapping reducible B:

A P B

Page 36: The Class  NP

Visualizing Poly-Time Reductions

A A_

B B_

f A_

( )

f A( )

f

f

A A_

B B_

f A_

( )f A( )f

f

Poly-Time mapping reduction:

Poly-Time co-mapping reductions:

Page 37: The Class  NP

Lemmas aboutPoly-Time Reductions

LEMMA: Let A and B be languages: If A P B and B is in P, then A is in P

If A PB and B is in P, then A is in P

If A P B and B is in NP, then A is in NP

If A PB and B is in NP, then A is in co-NP

If A PB and B is in co-NP, then A is in NPThese facts amount to showing that the poly-

time reductions can be composed with poly-time algorithms (detc or nondetc) to give poly-time algorithms.

Page 38: The Class  NP

Lemmas aboutPoly-Time Reductions

Transitivity Lemma. Let A and B be languages: If A P B and B P C then A P C

If A PB and B PC then A P C

Composing polynomial time reductions is a polynomial time reduction because polynomial(polynomial) is a polynomial.

Page 39: The Class  NP

SATThe the most famous problem in NP is the

satisfiability problem SAT: Given: a well-formed boolean formula

involving some variables and connectives , , and .

Decide: is satisfiable? I.e., can an assignment of “true” and “false” be given to variables which makes true ?

EG: (xx)y is an instance of SAT but xx isn’t.

It turns out that every problem NP can be reduced to SAT in poly-time!

Let’s see how this works for Punch-Card Puzzle.

Page 40: The Class  NP

Reducing Punch-Card Puzzleto SAT

To reduce the problem to SAT we need to figure out what the variables are and how they should be used to convert punch-cards to formulas.

IDEA: Create a sub-formula for each possible hole. Each card gives a variable which when true, satisfies the formulas corresponding to the missing holes. Flipping will make variable false, in which case other formulas should be satisfied:

Page 41: The Class  NP

Reducing Punch-Card Puzzleto SAT

C1 C2 C3 C4First card creates table of formulas:

x1

x1

x1 x1

Page 42: The Class  NP

Reducing Punch-Card Puzzleto SAT

C1 C2 C3 C4First card flipped adds to table:

x1 x1

x1 x1

x1x1 x1x1

Page 43: The Class  NP

Reducing Punch-Card Puzzleto SAT

C1 C2 C3 C4Second card adds:

x1 x2 x1x2

x1 x1

x2

x2 x2

x1x1 x1x1

Page 44: The Class  NP

Reducing Punch-Card Puzzleto SAT

C1 C2 C3 C4Second card flipped adds:

x1 x2x2 x1x2 x2

x1 x1

x2 x2

x2 x2 x2 x2

x1x1 x1x1

Page 45: The Class  NP

Reducing Punch-Card Puzzleto SAT

C1 C2 C3 C4Third card adds:

x1 x2x2 x1x2 x2

x1x3 x1

x2x3 x2

x2 x2 x2 x2

x1x1 x1x1

Page 46: The Class  NP

Reducing Punch-Card Puzzleto SAT

C1 C2 C3 C4Third card flipped adds:

x1 x2x2 x1x2 x2

x1x3 x1x3

x2x3 x2x3

x2 x2 x2 x2

x1x1 x1x1

Page 47: The Class  NP

Reducing Punch-Card Puzzleto SAT

C1 C2 C3 C4Fourth card adds:

x1 x2x2 x1x2 x2

x1x3 x1x3 x4

x2x3 x4x2x3

x2 x2 x2 x2

x1x1 x1x1

x4 x4

Page 48: The Class  NP

Reducing Punch-Card Puzzleto SAT

C1 C2 C3 C4Fourth card flipped adds:

x1 x2x2 x1x2 x2

x1x3 x4 x1x3 x4

x2x3x4 x2x3 x4

x2 x2 x2 x2

x1x1 x1x1

x4 x4 x4 x4

Page 49: The Class  NP

Reducing Punch-Card Puzzleto SAT

Insisting that all holes should be filled is same as asking that all the formulas in table be satisfied simultaneously.

I.e. that the following conjunction be satisfiable:

x1 x2x2 x1x2 x2

x1x3 x4 x1x3 x4

x2x3x4 x2x3 x4

x2 x2 x2 x2

x1x1 x1x1

x4 x4 x4 x4

Page 50: The Class  NP

Reducing Punch-Card Puzzleto SAT

(x1 x2x2)(x1x2 x2 )( x1x3 x4 ) ( x1x3

x4 ) ( x2x3 x4) (x2x3 x4) ( x2 x2 )(x2

x2)(x1x1)(x1x1 ) ( x4x4) ( x4 x4 )

In fact, setting first three variables True and fourth variable False satisfies the formula because puzzle is solved by keeping 1st three cards and flipping 4th.

Page 51: The Class  NP

Variants of SAT-CSAT

Notice that formula we got was basically a conjunction (AND’s) of disjunctions (OR’s). So to solve the punch-card puzzle, it would suffice to know how to solve SAT for only such expressions.

DEF: A literal is a variable such as x or its negation x. A clause is a disjunction of literals (e.g. x1x3 x4 ). A boolean formula is a conjunctive normal formula (or cnf ) if it is a conjunction of clauses.

CSAT is the following algorithmic problem:Given: A cnf .Decide: Is satisfiable?

Page 52: The Class  NP

Variants of SAT-nSAT

DEF: A boolean formula is an n-cnf if it is a conjunction of clauses each containing n or less literals.

nSAT is the following algorithmic problem:

Given: An n-cnf .Decide: Is satisfiable?

Page 53: The Class  NP

Blackboard Exercises

1. Show that PRIME is in co-NP.2. Show that 3COLOR is in NP.