x 1 x 1 x 2 x 2 x 3 x 3 x 4 x 4 12 22 32 CS 4407 1 13 21 23 31 33 Algorithms NP-Complete Reductions 3 Prof. Gregory Provan Department of Computer Science University College Cork 1
HARDEST PROBLEMS IN NP Definition: Theorem: A language B is NP-complete if: 1. B NP 2. Every A is NP-complete A in is poly-time and A P reducible B to B (i.e. B is NP-hard) If B is NP-Complete and P NP, then There is no fast algorithm for B.
REDUCTION STRATEGIES A reduction by restriction shows that the source problem is a special case of the target problem. For example, 3SAT P CNF-SAT because every satisfiable 3CNF is also a satisfiable CNF. Example. Prove 3SAT P 4SAT by restriction. A 3CNF can be converted to an equivalent 4CNF by repeating one literal in each clause.
COLORING COLOR = { G,k G is k-colorable } a d a d c b e b c 3COLOR = { G G is 3-colorable} Prove that 3COLOR P COLOR
HAMILTONIAN PATHS b f e g a d i c h HAMPATH = { G,s,t G has a hamiltonian path from s to t }
HAM PATH BETWEEN 2 VERTICES (H2V) INSTANCE: Graph G(V,E), vertices u,v V QUESTION: Does G contain a HAMILTON PATH from u to v? Prove that H2V is NP-complete
HAM-PATH P H2V Proof: H2V is NP-complete Restrict HAM-PATH such that the beginning node is u and the end node is v HAM-PATH is a special case of H2V
LONGEST PATH INSTANCE: Graph G(V,E), positive integer k V QUESTION: Does G contain a simple path with k or more edges.
LONGEST-PATH is NP-Complete Perform reduction from H2V Assume for LONGEST-PATH we have the case where k= V LONGEST-PATH is a special case of H2V where the path is Hamiltonian
VERTEX COVER a d a d c c b e b e VERTEX-COVER = { G,k G has a vertex cover of size at most k }
INDEPENDENT SET a d a d c c b e b e INDSET = { G,k G has an independent set of size at least k } Prove that VERTEX-COVER P INDSET.
SUBSET SUM SUBSET-SUM = { y 1,, y n,t S {1,,n}. Σ j2s y j =t } Which of the following are in SUBSET-SUM? YES 1,3,5,7, 10 NO 19,11,27,4, 13 YES 19,11,27,4, 61
KNAPSACK = { (w 1,v 1 ),(w n,v n ),W, V S {1 n} so that Σ i S w i W and Σ i S v i V} 15 lbs $500 ½ lb $15 3 lbs $2000 50 1lb, $20
Theorem. SUBSET-SUM P KNAPSACK Proof. A subset sum instance is a knapsack where the weights are equal to the values: Let ƒ(y 1,,y n,t) = (y 1,y 1 ) (y n,y n ),t,t. Then S. Σ i S y i = t iff S. Σ i S y i t and Σ i S y i t, so y 1 y n,t SUBSET-SUM iff ƒ(y 1 y n,t) KNAPSACK
SET-COVER = { S 1,,S n,k i,s i U and i[1 k] so that S i[1] S i[2] S i[k] = U } Which of the following are in SET-COVER? YES NO YES {1}, {1,2}, {2}, {3}, 2 {1,4}, {1,2}, {1,3}, {4}, 2 {1}, {2}, {1,2}, 2 Theorem. VERTEX-COVER P SET-COVER Proof. A vertex cover instance is just a set cover instance where every node is a set of edges.
REDUCTION STRATEGIES A reduction from A to B by local replacement shows how to translate between units of A and units of B. Example. vertex cover units are vertices and edges; set cover units are elements and sets. Example. CIRCUIT-SAT units are gates, CNFSAT units are clauses, 3SAT units are 3-literal clauses.
GRADUATION A transcript is a set of course numbers a student has taken A major consists of: Pairs: exactly one of which must be taken Lists: at least one course of which must be taken GRADUATION = { T,M a subset of T satisfies M} For example: T = {1901A, 1902B, 1902A, 2011, 4041A, 4061, 4211} M = [1901A,1901B], [1902A,1902B] (4011,4041A,4041B), (4211,4707), (4061)
GRADUATION 2 NP: The subset is a proof that (T,M) 2 GRADUATION. 3SAT P GRADUATION: T = {101, 102, 201, 202, 301, 302} (x 1 x 2 x 3 ) ( x 1 x 2 x 2 ) ( x 2 x 3 x 1 ) M = [101, 102], [201, 202], [301, 302] (101,201,302), (101,201,201), (101,202,301)
GRADUATION 2 NP: The subset is a proof that (T,M) 2 GRADUATION. 3SAT P GRADUATION: Let = C 1 C 2 C m have variables x 1 x k For each x i : add classes i01 and i02 to T. add pair (i01,i02) to M. For each C j, we add a triple to M: if x i is a literal in C j, the triple includes i01. if x i is a literal in C j, the triple include i02.
UHAMPATH No HAM PATH Undirected HAM PATH A B A B C D C D E F E F UHAMPATH = { G,s,t G is an undirected graph with a Hamiltonian path from s to t}
HAMPATH P UHAMPATH A B A in A mid Aout C D E F
HAMPATH P UHAMPATH A B C D E F
HAMPATH P UHAMPATH f(g,s,t) = (G,s,t ) where: For each node u G: add nodes u in, u out, u mid to G. add edges {u in,u mid } and {u mid,u out } to G For each edge (u,v) G, add {u out,v in } to G s = s in, t = t out. If (G,s,t) HAMPATH, then (G,s,t ) UHAMPATH: (s,u,v,..,t)! (s in,s mid,s out,u in,u mid,u out,v in,v mid,v out, t out )
HAMPATH P UHAMPATH If (G,s,t ) 2 UHAMPATH, then (G,s,t)2 HAMPATH: Let (s in =v 1,v 2,,v 3n =t out ) be the undirected path. Claim: for all i 0, there exists u G so that: v 3i+1 =u in, v 3i+2 =u mid, v 3i+3 =u out. i=0: If v 2 s mid, then no v i =s mid. So v 2 =s mid, v 3 =s out Induction: if v 3i = u out, then v 3i+1 =u in, so v 3i+2 =u mid. The directed Hamiltonian path is (u 1, u 2,, u n )
3SAT P SUBSET-SUM We transform a 3-cnf formula into (y 1 y n, t): 3SAT (y 1 y n,t) SUBSET-SUM The transformation can be done in time polynomial in the length of
Each variable and each clause result in two y i s. Each y i will have a digit for each clause and variable. (x 1 x 2 x 2 ) ( x 1 x 2 x 2 ) (x 1 x 2 x 2 ) x 1 x 2 C 3 C 2 C 1 x 2 x 1 C 3 C 2 C 1 y 1 1 1 0 1 y 2 1 0 1 0 y 3 1 0 1 1 1 y 4 1 0 1 0 0 y 5 1 0 0 y 6 1 0 0 y 7 1 0 y 8 1 0 y 9 1 y 10 1 t 1 1 3 3 3
3SAT P SUBSET-SUM Let = C 1 Æ C 2 Æ C m have k variables x 1 x k. We output y 1 y 2k + 2m, each a k+m-digit number. for each 1 j k: for each 1 i m: the i th digit of y 2j-1 is 1 if x j 2 C i, else 0 the i th digit of y 2j is 1 if :x j 2 C i, else 0. digit j+m of y 2j, y 2j-1 is 1. For each 1 j m: y 2k+2j = y 2k+2j-1 = 10 j-1 Output t = 11..1133..3