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) • Variants: CSAT, nSAT • The class Co-NP • Polynomial time reductions
A Punch-Card Puzzle Consider punch cards which contain 3 notches and a number of holes contained in two columns
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.
A Punch-Card Puzzle Pictured are punch cards on the pegs:
A Punch-Card Puzzle The 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.
A Punch-Card Puzzle Q: Find a solution to this instance: C1 C2 C3 C4
A Punch-Card Puzzle A: Solution is C1, C2, C3, flip(C4): C1 C2 C3 C4
A Punch-Card Puzzle A: Solution is C1, C2, C3, flip(C4): C1 C2 C3 C4
A Punch-Card Puzzle A: Solution is C1, C2,C3, flip(C4): C1 C2 C3 C4
A Punch-Card Puzzle A: Solution is C1, C2,C3,flip(C4): C1 C2 C3 C4
A Punch-Card Puzzle Q: Can puzzle be solved systematically?
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 Cn put cards on pegs if (no holes) ACCEPT REJECT Q: Running time?
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 W(2n). Therefore, the algorithm has exponential running time. Q: Can you come up with a polynomial-time algorithm?
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) !
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?
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? a|bR L bR a|bR 0 1 2 3
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: a|bR L bR a|bR 0 1 2 3
Nondeterministic Running Time a|bR L bR a|bR 0 1 2 3 height = f (5) = 6 0ababa a0baba ab0aba ab1aba aba0ba aba2ba crash abab0a abab1a ababa0 ababa2 crash abab3a
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) ).
The Class NP A: 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:
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?
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:
Puzzle ExampleShort Proof C1 C2 C3 C4 CLAIM: Solution is C1, C2,C3,flip(C4). • C2 covers (1,L) • C2 covers (1,R) • C3 covers (2,L) … and so on … • flip(C4) covers (6,R) Q: How long would a proof be for n cards, m rows?
Puzzle ExampleShort Proof C1 C2 C3 C4 A: 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?
The Class Co-NP A: 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.
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.
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.
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.
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.
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.
Punch-Card PuzzleNondeterministic Algorithm A: NondeterministicSolvePuzzle(cards C1, C2,…,Cn) for(i = 1 to n) flip Ci OR skip //nondeterministic “OR” put cards on pegs if (no holes) ACCEPT REJECT Q: Running time?
Punch-Card PuzzleNondeterministic Algorithm A: 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.
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.
Poly-Time ReductionNotation • A is poly-time mapping reducible to B: A PB • A is poly-time co-mapping reducible B: A PB
Visualizing Poly-Time Reductions Poly-Time mapping reduction: Poly-Time co-mapping reductions:
Lemmas aboutPoly-Time Reductions LEMMA: Let A and B be languages: • If A PB and B is in P, then A is in P • If A PBand B is in P, then A is in P • If A PB and B is in NP, then A is in NP • If A PBand B is in NP, then A is in co-NP • If A PBand B is in co-NP, then A is in NP These facts amount to showing that the poly-time reductions can be composed with poly-time algorithms (detc or nondetc) to give poly-time algorithms.
Lemmas aboutPoly-Time Reductions Transitivity Lemma. Let A and B be languages: • If A PB and B PC then A PC • If A PB and B PC then A PC Composing polynomial time reductions is a polynomial time reduction because polynomial(polynomial) is a polynomial.
SAT The the most famous problem in NP is the satisfiability problem SAT: Given: a well-formed boolean formula f involving some variables and connectives, , and . Decide: isf satisfiable? I.e., can an assignment of “true” and “false” be given to variables which makes f 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.
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:
Reducing Punch-Card Puzzleto SAT C1 C2 C3 C4 First card creates table of formulas:
Reducing Punch-Card Puzzleto SAT C1 C2 C3 C4 First card flipped adds to table:
Reducing Punch-Card Puzzleto SAT C1 C2 C3 C4 Second card adds:
Reducing Punch-Card Puzzleto SAT C1 C2 C3 C4 Second card flipped adds:
Reducing Punch-Card Puzzleto SAT C1 C2 C3 C4 Third card adds:
Reducing Punch-Card Puzzleto SAT C1 C2 C3 C4 Third card flipped adds:
Reducing Punch-Card Puzzleto SAT C1 C2 C3 C4 Fourth card adds:
Reducing Punch-Card Puzzleto SAT C1 C2 C3 C4 Fourth card flipped adds:
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:
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.