760 likes | 1.26k Views
NP-completeness. Class of hard problems. Outline. Introduction Problems and Languages Turing machines and algorithms P and NP Satisfiability problem Reduction Other NP-complete problems. Problems. A problem can be defined as a mapping from the instance to the answer. Examples:
E N D
NP-completeness Class of hard problems
Outline • Introduction • Problems and Languages • Turing machines and algorithms • P and NP • Satisfiability problem • Reduction • Other NP-complete problems NP-complete Problems
Problems • A problem can be defined as a mapping from the instance to the answer. • Examples: • Shortest path problem {<graph G, node u, node v>} {a shortest path P} • 0/1 Knapsack problem {<set S of objects, capacity C>} {the maximum value} • Closest-pair-of-points problem {<set P of points>} {<point q, point r>, which is the closest pair of points in P}. • Satisfiable problem {<boolean formula>} {T, F} NP-complete Problems
Abstract problems A set of instances A set of solutions NP-complete Problems
Decision Problems • A decision problem is a problem whose answer is either “yes” or “no”. • Example: Satisfiable problem {<boolean formula>} {T, F} • Any problem can be described in term of a decision problem. NP-complete Problems
Reformulate Problems as Decision Problems • Shortest path problem • {<graph G, node u, node v, int k>} {T, F} • The answer is T iff there is no path between u and v, which is shorter than k. • 0/1 Knapsack problem • {<set S of objects, capacity C, value V>} {T, F} • The answer is T iff there is no selection of objects fitted in the sack which yields higher value than V. NP-complete Problems
Encoding of Decision Problems • An encoding of a problem is a mapping from the set of instances of the problem to a set of strings. • Example: shortest path • The encoding of <graph G, node u, node v, path p> in the Shortest path problem is a string which is used to describe G, u, v, and p. • G can be described by a string of its adjacency matrix. • u and v can be described by two strings, indicating the node labels. • p can be described by a sequence of node in the path. NP-complete Problems
Abstract/ Concrete problems A set of instances Mapping (encoding) Abstract problem Mapping (encoding) A set of solutions Concrete problem NP-complete Problems
Encoding of Problems • Is X a prime ? f:{X |X>0 and is a binary number}{Y,N} f(x) = Y if X is prime f(x) = N otherwise • Is a 3-CNF expression X satisfiable? f:{X | X is a 3-CNF expression} {Y, N} f(x) = Y if X is satisfiable f(x) = N otherwise NP-complete Problems
Why encoding is important? • Encoding determines the instance size. • Example: an integer n • n encoded as one number => instance size = 1 • n encoded as binary number => instance size = log2 n • n encoded as unary number => instance size = n NP-complete Problems
Languages and Decision Problems • Let be an alphabet. • A language over is a set of strings created from symbols in . • B={0,1}* is the set of all binary strings. • { is a palindrome} is a language over B. • A yes-instance (or no-instance) is an instance whose answer is “yes” (or “no”). • An instance must be either a yes- or a no-instance. NP-complete Problems
Languages and Decision Problems • A decision problem can be encoded in a language {| is an encoding of a yes- instance}. • The shortest path problem can be encoded as {e(G, u, v, p>)| p is the shortest path between u and v in the graph G}. NP-complete Problems
Turing machines (TM) • A Turing machine is a machine which takes a string on its input tape, and decides whether to accept or to reject the string. • A Turing machine contains • A finite-state control with • A start state • A halt state • Transition function: state x symbol state x symbol x {R,L} • A tape with a Read/Write headwhich can move left and right. NP-complete Problems
Deterministic/ Nondeterministic Turing machines • Difference • A transition is deterministic or nondeterministic. • What is nondeterminism? • Given a state of a machine/program, there is more than one possible next step. • Examples • Deterministic: functions. • Nondeterministic: relations. NP-complete Problems
Example of a DTM s /@,R /,L 1/,R p1 q1 /,R 0/,R @/,R 0/0,L 1/1,L 0/0,R 1/1,R p2 p4 q2 0/,L /,L /1,L p3 h NP-complete Problems
How a DTM works s /,L /@,R 1/,R p1 q1 @ 1 0 0 0 1 0 0 0 /,R 0/,R @/,R 0/0,L 1/1,L 0/0,R 1/1,R p2 p4 q2 /1,L /,L 0/,L On the input 0001000, the TM halts. h p3 NP-complete Problems
How a DTM works s /,L /@,R 1/,R p1 q1 @ 0 0 0 0 0 0 0 /,R 0/,R @/,R 0/0,L 1/1,L 0/0,R 1/1,R p2 p4 q2 /1,L On the input 0000000, the TM hangs. /,L 0/,L h p3 NP-complete Problems
Example of NTM • Let L={ww| w{0,1}*} r0 @/,R 0/@,R 0/0,R 1/1,R 0/0,L 1/1,L /,L s q0 t0 /@,R 0/0,L 1/1,L 0/,L 0/,L /,R /@,L @/,L v p u h /,R /,R 1/,L 1/,L 0/0,R 1/1,R q1 t1 0/0,L 1/1,L /,L @/,R 1/@,R 0/0,R 1/1,R r1 NP-complete Problems
Accept/Decide • Let T be a Turing machine. • For a string over , • T accepts iff T halts on with output “1”. • T rejects iff T halts on with output “0”. • T hangs on iff T dose not halts on . • For a language L over , • T accepts L iff, for any string in L, T accepts . • T decides iff • for any string in L, T accepts , and • for any string inL, T rejects . NP-complete Problems
Time complexity • A language L is accepted/decided in O(f(n)) by a Turing machine T if, for any length-n string in L, is accepted/decided in k·f(n) by a Turing machine T, for some constant k. • A language L is accepted/decided in polynomial time by a Turing machine T if, L is accepted/decided in O(nk) by a Turing machine T, for some constant k. NP-complete Problems
Church-Turing Thesis • If there is a Turing machine deciding a problem in O(f), then there is an algorithm solving in O(f). • Turing machine algorithm NP-complete Problems
Class of P • If is a problem in P, given any instance I of , • there is an algorithm which solves I in O(nk), or • there is a Turing machine which accepts e(I) in O(nk) steps, where n is the size of e(I) and k is a constant. • What does this mean? • If is in P, is easy because it can be solved in polynomial time. NP-complete Problems
Class of NP • If is a problem in P, given any instance I and any certificate C of , • there is an algorithm which verifies, in O(nk), that C is the answer of I, for any C and I. • there is a Turing machine which verifies, in O(nk), that C is the answer of I, for any C and I. • there is a nondeterministic Turing machine which accepts e(I), in O(nk), for any I. where n is the size of e(I) and k is a constant. What does this mean? • If is in NP, is not necessarily easy because it can only be verified in polynomial time. NP-complete Problems
Can be solved in polynomial time Prove by showing the algorithm. Cannot be solved in polynomial time Prove by showing that there is no polynomial-time algorithm. Can verify the answer in polynomial time. Can usually be solved by an exponential-time algorithm. Brute-force algorithm: try every possible answers. Easy problems / Hard problems NP-complete Problems
Class of co-NP • L is in co-NP ifL is in NP. NP co-NP P NP-complete Problems
Reduction Is X more difficult than Y?
Reduction • Let L1 and L2 be languages over 1 and 2, respectively. L1 is (polynomial-time) reducible to L2, denoted by L1L2, if there is a TM M computing a function f: 1*2* such that wL1 f(w)L2 in polynomial time. • Let P1 and P2 be problems. P1 is (polynomial-time) reducible to P2, denoted by P1P2, if there is an algorithm computing a function f: 1*2* such that w is a yes-instance of P1 f(w) is a yes-instance of P2 in polynomial time. NP-complete Problems
Meaning of Reduction P1 is reducible to P2 if DTM computing, in polynomial time, a function f: 1*2* such that w is a yes-instance of P1 f(w) is a yes-instance of P2. • If you can map yes-instances of problem A to yes-instances of problem B, then • we can solve A in polynomial time if we can solve B in polynomial time. • it doesn’t mean we can solve B in polynomial time if we can solve A in polynomial time. NP-complete Problems
Satisfiability problem (SAT) • Given a Boolean expression , is satisfiable? • (A~BC)(~A~C)B~C is not satisfiable. NP-complete Problems
3CNF-SAT Problem • Given an expression in 3CNF, is satisfiable? • Given an expression in 3CNF, is there a truth assignment of propositions in which makes true? • Example: • =(PQR)(PQR)(PSR)(PSR) • is satisfiable when • P=t, Q=f, R=f, S=t or • P=f, Q=f, R=f, S=t NP-complete Problems
Clique or Complete Subgraph • A graph G=(V,E) has a clique if there is W V such that for all nodes a and b in W, there is an edge between a and b. C B F A E D NP-complete Problems
Clique Problem • Given a graphG=(V,E) and an integer k, is there a clique of size k in G? • Example: Is there a clique of size 3, 4, or 5, in the graph below? C B F A E D NP-complete Problems
3CNF-SAT Clique Proof: To prove that 3CNF-SAT Clique, • Find a function f such that • a 3CNF expression is satisfiable a graph G has a clique of size k, where <G,k>=f() and • there is a TM computing f in polynomial time. f can be defined as follows: Let = (111213)(212223)…(n1n2n3) f() = <G,n>, where G=(V, E), • V={ij|0<i<n+1, 0<j<4}, and • E={(ij, km)| i≠k and ij≠~km} NP-complete Problems
Building a graph from a 3CNF exp. (PQR)(PSR)(PSR) ~Q P R ~P P ~S S ~R ~R NP-complete Problems
3CNF-SAT Clique (cont’d) • Show is satisfiableG has clique of size n • Let G has a clique of size n. • From the definition of G, there is no edge between vertices • in the same disjunctive clause, and • representing a literal and its negation. • A link between two nodes means that the two literals can be true at the same time, and they are in different disjunctive clauses. NP-complete Problems
Satisfiability and Clique (PQR)(PSR)(PSR) ~Q P R ~P P ~S S ~R ~R NP-complete Problems
Satisfiability and Clique (PQR)(PQR)(PQR)(PQR) Q P R ~P P Q Q R ~R ~P ~R Q NP-complete Problems
Satisfiability and Clique (PQR)(PQR)(PQR)(PQR) Q P R ~P P Q Q R ~R ~P ~R Q NP-complete Problems
3CNF-SAT Clique (cont’d) • There is a TM computing f in polynomial time. NP-complete Problems
Reflexivity of reduction Theorem: Let L be a language over . L L. Proof: Let L be a language over . Let f be an identity function from **. Then, there is a TM computing f. Because f is an identity function, wL f(w)=wL. By the definition, L L. NP-complete Problems
Property of reduction Theorem: Let L1 and L2 be languages over . If L1L2, thenL1L2. Proof: Let L1 and L2 be languages over . Because L1L2, there are a function f such that wL1 f(w)L2, and a TM T computing f in polynomial time. wL1 f(w)L2. By the definition,L1L2. NP-complete Problems
Transitivity of reduction Theorem: Let L1, L2 and L3 be languages over . If L1 L2 and L2 L3, then L1 L3. Proof: Let L1, L2 and L3 be languages over . There is a function f such that wL1 f(w)L2, and a TM T1 computing f in polynomial time because L1 L2. There is a function g such that wL2 g(w)L3, and a TM T2 computing g in polynomial time because L2 L3. wL1 f(w)L2 g(f(w))L3, and T1T2 computes g(f(w)) in polynomial time . By the definition, L1 L3. NP-complete Problems
Using reduction to prove P/NP Theorem: If L2 is in P/NP, and L1L2, then L1 is also P/NP. Proof: Let L1 and L2 be languages over , L1L2, and L2 be in P/NP. Because L2 is in P/NP, there is a DTM/NTM T2 accepting L2 in polynomial time. Because L1L2, there is a DTM T1 computing a function f such that wL1 f(w)L2 in polynomial time. Construct a DTM/NTM T=T1T2. We show that T accepts L1in polynomial time. • If wL1, T1 in T computes f(w)L2 and T2 in T accepts f(w), in polynomial time. • If wL1, T1 in T computes f(w) L2 and T2 in T does not accept f(w) in polynomial time. Thus, L1 is also in P/NP. NP-complete Problems
Using reduction to prove co-NP. Theorem: If L2 is in co-NP, and L1L2, then L1 is also in co-NP. Proof: Let L1 and L2 be languages over , L1L2, and L2 be in co-NP. Because L2 is in co-NP,L2 is in NP. Because L1L2,L1L2. Then,L1 is in NP. Thus, L1 is co-NP. NP-complete Problems