6.045 Lecture 18: More NP-Complete Problems 1
The Clique Problem a d f c b e g Given a graph G and positive k, does G contain a complete subgraph on k nodes? CLIQUE = { (G,k) G is an undirected graph with a k-clique } 2
The Clique Problem Given a graph G and positive k, does G contain a complete subgraph on k nodes? CLIQUE = { (G,k) G is an undirected graph with a k-clique } Theorem (Karp): CLIQUE is NP-complete 3
Theorem: CLIQUE is NP-Complete P NP 3SAT CLIQUE 4
3SAT P CLIQUE Transform a 3-cnf formula into (G,k) such that 3SAT (G,k) CLIQUE Want transformation that can be done in time that is polynomial in the length of How can we encode a logic problem as a graph problem? 5
3SAT P CLIQUE We transform a 3-cnf formula into (G,k) such that 3SAT (G,k) CLIQUE Let C 1, C 2,, C m be clauses of. Assign k := m. Make a graph G with m groups of 3 nodes each. Group i corresponds to clause C i of Each node in group i is labeled with a literal of C i Put edges between all pairs of nodes in different groups, except pairs of nodes with labels x i and :x i Put no edges between nodes in the same group When done putting in all the edges, erase the labels 6
(x 1 x 1 x 2 ) ( x 1 x 2 x 2 ) ( x 1 x 2 x 2 ) x 1 x 2 x 2 x 1 x 1 x 1 x 2 x 2 x 2 V = 3(number of clauses) k = number of clauses 7
(x 1 x 1 x 1 ) ( x 1 x 1 x 2 ) (x 2 x 2 x 2 ) ( x 2 x 2 x 1 ) x 1 x 1 x 2 x 1 x 2 x 1 x 2 x 1 x 2 x 2 x 2 x 1 8
Claim: 3SAT (G,m) CLIQUE Claim: If 3SAT then (G,m) CLIQUE Proof: Let A be a SAT assignment of. For every clause C of, some literal in C is set true by A For every clause C, let v C be a vertex from group C of G, whose label is a literal that is set true by A Claim: S = {v C C 2 } is an m-clique in G. (note S =m) Proof: Let v C,v C be in S. If (v C,v C ) E Then v C and v C must label inconsistent literals, call them x and :x But assignment A cannot satisfy both x and :x Therefore (v C,v C ) 2 E, for all v C, v C 2S. Hence S is an m-clique, and (G,m) CLIQUE 9
Claim: 3SAT (G,m) CLIQUE Claim: If (G,m) CLIQUE then 3SAT Proof: Let S be an m-clique of G. We ll construct a satisfying assignment A of. Claim: S contains exactly one node from each group. For each variable x of, make variable assignment: A(x) := 1, if there is a vertex v ϵ S with label x A(x) := 0, otherwise For all i = 1,,m, one vertex from group i is in S. Therefore, for all i = 1,,m A satisfies at least one literal in the ith clause of Therefore A is a satisfying assignment to 10
Independent Set IS: Given a graph G = (V, E) and integer k, is there S µ V such that S k and no two vertices in S have an edge? CLIQUE: Given G = (V, E) and integer k, is there S µ V such that S k and every pair of vertices in S have an edge? CLIQUE P IS: Given G = (V, E), output G = (V, E ) where E = {(u,v) (u,v) E}. (G, k) 2 CLIQUE iff (G, k) 2 IS 11
The Vertex Cover Problem a d a d c c b e b e vertex cover = set of nodes C that cover all edges For all edges, at least one endpoint is in C 12
VERTEX-COVER = { (G,k) G is a graph with a vertex cover of size at most k} Theorem: VERTEX-COVER is NP-Complete (1) VERTEX-COVER NP (2) IS P VERTEX-COVER 13
IS P VERTEX-COVER Want to transform a graph G and integer k into G and k such that (G,k) IS (G,k ) VERTEX-COVER 14
IS P VERTEX-COVER Claim: For every graph G = (V,E), and subset S µ V, S is an independent set if and only if (V S) is a vertex cover Proof: S is an independent set (8 u, v 2 V)[ (u 2 S and v 2 S) ) (u,v) E ] (8 u, v 2 V)[ (u,v) 2 E ) (u S or v S) ] (V S) is a vertex cover Therefore (G,k) IS (G, V k) VERTEX-COVER Our polynomial time reduction: f(g,k) := (G, V k) 15
The Subset Sum Problem Given: Set S = {a 1,, a n } of positive integers and a positive integer t Is there an A µ {1,,n} such that t = i 2 A a i? SUBSET-SUM = {(S, t) 9 S µ S s.t. t = b 2 S b } A simple number-theoretic problem! Theorem: There is a O(n t) time algorithm for solving SUBSET-SUM. But t can be specified in (log t) bits this isn t an algorithm that runs in polytime in the input! 16
The Subset Sum Problem Given: Set S = {a 1,, a n } of positive integers and a positive integer t Is there an A µ {1,,n} such that t = i 2 A a i? SUBSET-SUM = {(S, t) 9 S µ S s.t. t = b 2 S b } A simple number-theoretic problem! Theorem: SUBSET-SUM is NP-complete 17
VC P SUBSET-SUM Want to reduce a graph to a set of numbers Given (G, k), let E = {e 0,,e m-1 } and V = {1,,n} Our subset sum instance (S, t) will have S = n+m Edge numbers : For every e j 2 E, put b j = 4 j in S Node numbers : For every i 2 V, put a i = 4 m + j : i 2 ej 4 j in S Set the target number: t = k 4 m + j=0 m-1 (2 4 j ) 18
For every e j 2 E, put b j = 4 j in S For every i 2 V, put a i = 4 m + j : i 2 ej 4 j in S Set t = k 4 m + j=0 m-1 (2 4 j ) Claim: If (G,k) VC then (S,t) SUBSET-SUM Suppose C µ V is a VC with k vertices. Let S = {a i : i 2 C} [ {b j : e j \ C = 1} S = (node numbers corresponding to nodes in C) plus (edge numbers corresponding to edges covered only once by C) Claim: The sum of all numbers in S equals t! Think of the numbers as being in base 4 as vectors with m+1 components 19
For every e j 2 E, put b j = 4 j in S For every i 2 V, put a i = 4 m + j : i 2 ej 4 j in S Set t = k 4 m + j=0 m-1 (2 4 j ) Claim: If (S,t) SUBSET-SUM then (G,k) VC Suppose C µ V and F µ E satisfy i 2 C a i + ej 2 F b j = t = k 4 m + j=0 m-1 (2 4 j ) Claim: C is a vertex cover of size k. Proof: Subtract out the b j numbers from the above sum. What remains is a sum of the form: i 2 C a i = k 4 m + j=0 m-1 (c j 4 j ) where each c j > 0. But c j = number of nodes in C covering e j This implies C is a vertex cover! 20
The Knapsack Problem Given: S = {(v 1,c 1 ), (v n,c n )} of pairs of positive integers a capacity budget C a value V Is there an S µ {1,,n} such that ( i 2 S v i ) V and ( i 2 S c i ) C? Define KNAPSACK = {(S, C, V) the answer is yes} A classic economics/logistics problem! Theorem: KNAPSACK is NP-complete 21
KNAPSACK is NP-complete KNAPSACK is in NP? Theorem: SUBSET-SUM P KNAPSACK Proof: Given an instance (S = {a 1,,a n }, t) of SUBSET-SUM, create a KNAPSACK instance: For all i, set (p i, c i ) := (a i, a i ) Define T = {(p 1, c 1 ),, (p n, c n )} Define C := P := t Then, (S,t) 2 SUBSET-SUM (T,C,P) 2 KNAPSACK Subset of S that sums to t = Solution to the Knapsack instance! 22
The Partition Problem Given: Set S = {a 1,, a n } of positive integers Is there an S µ S such that ( a_i 2 S a i ) = ( a_i 2 S-S a i )? (Formally, PARTITION is the set of all S such that the answer to this question is yes.) In other words, is there a way to partition S into two parts, with equal sum in both parts? A problem in fair division Theorem: PARTITION is NP-complete 23
PARTITION is NP-complete (1) PARTITION is in NP (2) SUBSET-SUM P PARTITION Given: Set S = {a 1,, a n } of positive integers positive integer t Output T := {a 1,, a n,2a-t,a+t}, where A := i a i Claim: (S,t) 2 SUBSET-SUM T 2 PARTITION 24
Given: Set S = {a 1,, a n } of positive integers positive integer t Output T := {a 1,, a n,2a-t,a+t}, where A := i a i Claim: (S,t) 2 SUBSET-SUM T 2 PARTITION What s the sum of all numbers in T? 4A Therefore: T 2 PARTITION There is a T µ T that sums to 2A. Proof of: (S,t) 2 SUBSET-SUM T 2 PARTITION: If (S,t) 2 SUBSET-SUM, let S µ S sum to t. Then S {2A-t} µ T sums to 2A, so T 2 PARTITION 25
Given: Set S = {a 1,, a n } of positive integers positive integer t Output T := {a 1,, a n,2a-t,a+t}, where A := i a i Claim: (S,t) 2 SUBSET-SUM T 2 PARTITION T 2 PARTITION There is a T µ T that sums to 2A. Proof of: T 2 PARTITION (S,t) 2 SUBSET-SUM If T 2 PARTITION, let T µ T be a subset that sums to 2A. Observation: Exactly one of {2A-t,A+t} is in T. If (2A-t) 2 T, then T {2A-t} sums to t. But T {2A-t} is a subset of S! So (S,t) 2 SUBSET-SUM If (A+t) 2 T, then (T T ) {2A-t} sums to (2A (2A-t)) = t Note that (T T ) {2A-t} is a subset of S. Therefore (S,t) 2 SUBSET-SUM 26
The Bin Packing Problem Given: Set S = {a 1,, a n } of positive integers, a bin capacity B, and a target integer K. Can we partition S into K subsets such that each subset sums to at most B? Is there a way to pack the items of S into K bins, with each bin having capacity B? Ubiquitous in shipping and optimization Theorem: BIN PACKING is NP-complete 27
BIN PACKING is NP-complete BIN PACKING is in NP? Theorem: PARTITION P BIN PACKING Proof: Given an instance S = {a 1,, a n } of PARTITION, create an instance of BIN PACKING with: S = {a 1,, a n } B = ( i a i )/2 k = 2 Then, S 2 PARTITION (S,B,k) 2 BIN PACKING: Partition of S into two equal sums = Solution to the Bin Packing instance! 28
Two Problems Let G denote a graph, and s and t denote nodes. SHORTEST PATH = {(G, s, t, k) G has a simple path of length < k from s to t } LONGEST PATH = {(G, s, t, k) G has a simple path of length > k from s to t } Are either of these in P? Are both of them? 29