750 likes | 1.73k Views
Complexity Classes: P and NP. CS 130 Theory of Computation HMU Textbook: Chap 10. Turing machines and complexity. Time and space complexity The class P Non-determinism The class NP Reduction and NP-Completeness. Time and space complexity.
E N D
Complexity Classes: P and NP CS 130 Theory of Computation HMU Textbook: Chap 10
Turing machines and complexity • Time and space complexity • The class P • Non-determinism • The class NP • Reduction and NP-Completeness
Time and space complexity • Running time (or time complexity) for a TMis f(n), where • n is the length of the input tape • f(n) maximum number of steps/transitions the TM makes before halting • Could be infinite if the TM does not halt on some inputs • Space complexity is the maximum number of cells on the tape used/encountered by the TM during execution
The class P • The class P describes all languages(or problems) described by a Turing Machine decider, whose time complexity is bounded by a polynomial on n • Examples: • Divisibility of a number by another number • Recognizing palindromes • Matching symbols in a string • Many other more complex problems (e.g., searching, shortest paths, min-cost spanning tree)
The class P solvable(decidable) problems recursive P solvable withinpolynomial time
Extensions to the basic TM • Multi-tape turing machine • multiple tapes, input placed on first tape, other tapes filled with blanks • multiple heads, moving independently • Nondeterminism: • allow several possible transitions, given a state and symbol • Alternatives to TMs • Counter machines, stack machines, etc… • None of these extensions extend the capability of TMs, but may impact on time/space complexity
Non-deterministic Turing Machine • An non-deterministic turing machine, or NDTM,is a tuple M = (Q, , , , q0, B, F), where • Q is a set of states • is the input alphabet • is the tape alphabet = {B} other tape symbols • : Q (Q D)* is the state transition function mapping (state, symbol) to(state, symbol, direction) possibilities; D = {,}; may be empty/undefined for some pairs • q0 is the start state of M • Bis the blank symbol (default symbol on input tape) • F Q is the set of accepting states or final states of M(if applicable)
Non-deterministic Turing Machine • Difference from a regular TM: • : Q (Q D)* • Multiple transitions, given a state and a symbol, are now possible • Impact on the turing machine as a recognizer • String is acceptable as long as at least one path of transitions leads to a final state • Impact on the turing machine as a decider • String is acceptable as long as at least one path of transitions leaves a YES on the tape;not acceptable if all paths leave a NO on the tape
The class NP • The class NP describes all languages(or problems) described by an NDTM decider, whose time complexity is bounded by a polynomial on n • Clearly P NP, but it is not yet known or proven that P NP(though many believe this is true)
The classes P and NP not yet proven that this regionis empty,but it likely isn’t solvable(decidable) problems recursive NP P solvable withinpolynomial time
Some problems in NP • Independent set • Hamiltonian cycle • Satisfiability • Vertex cover • “Student Reps”
Independent set • Given a graph G = (V,E) and an integer K,is there a subset S of K vertices in Vsuch that no two vertices in S are connected by an edge? • There is an easy brute-force method to solve this problem: • for each possible subset S of V (2n such subsets): check if S contains K vertices and then check if each pair in S is connected by an edge • Answer yes if there is an S that satisfies the condition, answer no if all subsets do not
Independent set and TMs • A vertex subset can be represented by an n-bit string (string of 0’s and 1’s: 1 means a vertex is part of the subset) • Deterministic TM solution • Loop that generates each subset on the tape and then checks if a subset satisfies the condition • Exponential time complexity because there are 2n subsets • NDTM solution • Non-deterministically write a subset on the tape,then check if the subset satisfies the condition • Polynomial-time complexity because there is no exponential loop involved
NDTM and possibilities B,0, B,0, B,0, B,1, B,1, B,1, q1 q2 q3 q4 writes one of the following 3-bit strings on the tape:000,001,010,011,100,101,110,111
Hamiltonian cycle • Given a graph G = (V,E), is there a simple cycle containing all vertices in V? • Easy brute-force method to solve this problem: • for each possible permutation P of V(n! possibilities): check if the appropriate edges implied by the permutation exist, forming the cycle • Answer yes if there is a P that forms a cycle, answer no if all permutations do not
Alternative characterization of NP • A problem is in NP if a feasible solution to the problem can be verified in polynomial time • A problem is in NP if it can be solved by the following “framework”: • for each possibility P: check (in polynomial time) if the possibility P satisfies the condition stated in the problem • Answer yes if there is a P that satisfies the condition, answer no if all possibilities do not
Satisfiability • Given a set V of variables, and a boolean expression E over V, consisting of a conjunction of clauses of disjunctions of literals (conjunctive normal form),is there a truth assignment for V that satisfies E(E evaluates to true under the assignment)? • Example: V = {a,b,c}, E = (a+b)(b+c)(c)Assignment that satisfies E: A=true, B=true, c=false • Easy brute-force method to solve this problem: • for each possible truth assignment A (2n possibilities): evaluate E under A • Answer yes if there is an A that satisfies E,answer no if all assignments do not
Vertex cover • Given a graph G = (V,E) and an integer K,is there a subset S of K vertices in Vsuch that every edge in E has at least one endpoint in S? • There is an easy brute-force method to solve this problem: • for each possible subset S of V (2n such subsets): check if S contains K vertices and then check if edges in E have an incident vertex in S • Answer yes if there is an S that satisfies the condition, answer no if all subsets do not
Student reps • Given: • A set S of all students in a university • A set O of student organizations,each having members that comprise a subset of S • An integer K • Question: • Can I find K students from S such that all organizations are represented? • Exercise: Formulate a brute-force solution to this problem following the framework mentioned, thereby showing that this problem is in NP
NP-complete problems • The problems we have identified so far are “hard” in the sense that there are no known polynomial-time solutions using a regular TM but there are “easy” exponential-time solutions (or, polynomial solutions in an NDTM) • Some of these problems have been shown “complete” in the sense that all problems in NP reduce to these problems
Reduction • Reduction entails converting an instance of one problem into an equivalent instance of another • If a problem A reduces to a problem B, then a solution to B can be used to solve A • Means that B is at least as hard as A • Remember HP and HPA? • Cook’s Theorem: Satisfiability (SAT) is NP-complete; all problems in NP reduce to SAT • What does this mean? If someone discovers a polynomial-time solution for SAT, all other problems are solved
Reduction • Important condition: the reduction has to be carried out in polynomial-time • How does one show that a problem P isNP-complete? • Use a proof similar to Cook’s theorem(too hard, and too much work!) • Easier option: reduce a known NP-complete problem(such as SAT) to P, so that P is NP-complete by transitivity • Thousands of problems in NP have already been shown NP-complete • If any one of these problems turns out to be solvable in polynomial time, it is a proof that P=NP! ($1M prize)
Reduction and NP-completeness • SAT is NP-complete by Cook’s theorem • Proof is beyond the scope of this course • SAT reduces to Vertex Cover (VC) • Convert variables and clauses to a graph and an integer such that a truth assignment corresponds to a vertex cover in the converted graph • With a successful polynomial-time reduction, this shows that VC is NP-complete • VC reduces to Independent Set (IS) andto Student Reps (SR) • Which means IS and SR are NP-complete
SAT to VC V = {a,b,c,d} E = (c)(a+b)(b+c+d) a b c d - + - + - + - + K = 4 +0+1+2= 7 G b c a b c d (b+c+d) (c) (a+b)
Reduction and NP-completeness NP all other NPproblems All other NPproblems SAT VC HC IS SR
Summary • Turing machines model computability • The class P: problems (languages) that can be solved in polynomial time using a TM decider • The class NP: problems that can be solved in polynomial time using a NDTM (they can be solved in exponential time using a regular TM) • Not yet proven whether P NP • There are problems in NP that are NP-complete;i.e., all other NP problems reduce to it • Saying that a problem is NP-complete is a statement of “hardness” of that problem • Proving NP-completeness: reduce from a known NP-complete problem