Algorithms and Theory of Computation Lecture 22: NP-Completeness (2) Xiaohui Bei MAS 714 November 8, 2018 Nanyang Technological University MAS 714 November 8, 2018 1 / 20
Set Cover Set Cover Input: a set U of elements, a collection S 1, S 2,..., S m of subsets of U, and an integer k Question: does there exist a collection of no more than k of these sets whose union is equal to U? Sample applications: m available pieces of software Set U of n capabilities that we would like our system to have The ith piece of software provides the set S i U of capabilities Goal: achieve all n capabilities using fewest pieces of software Nanyang Technological University MAS 714 November 8, 2018 2 / 20
Reducing Vertex Cover to Set Cover Theorem Set Cover is NP-complete. Part 1: Set Cover NP: guess the collection Part 2: reduction from Vertex Cover Let G = (V, E) and k be an instance of Vertex Cover. Create an instance of Set Cover U = E Create S u for each u V, where S u contains the edges adjacent to u. Proposition U can be covered by k sets iff G has a vertex cover of size k. = : If k sets S u1,..., S uk cover U then every edge is adjacent to at least one of the vertices u 1,... u k, yielding a vertex cover of size k. =: If u 1,..., u k is a vertex cover, then sets S u1,..., S uk cover U. Nanyang Technological University MAS 714 November 8, 2018 3 / 20
Hamiltonian Cycle A Hamiltonian cycle in a directed graph G is a cycle that visits every vertex in G exactly once. Hamiltonian Cycle Input: a directed graph G = (V, E) Question: does G have a Hamiltonian cycle? Nanyang Technological University MAS 714 November 8, 2018 4 / 20
Reducing 3SAT to Hamiltonian Cycle Theorem Hamiltonian Cycle is NP-complete Part 1: Hamiltonian Cycle NP: exercise Part 2: we will show 3SAT P Hamiltonian Cycle Given 3SAT formula ϕ, we want to create a graph G ϕ, such that G ϕ has a Hamiltonian cycle if and only if ϕ is satisfiable G ϕ should be constructible from ϕ by a polynomial time algorithm A Notation: ϕ has n variables x 1, x 2,..., x n and m clauses C 1, C 2,..., C m. Nanyang Technological University MAS 714 November 8, 2018 5 / 20
Reduction: First Ideas Viewing SAT: Assign values to n variables, and each clauses has 3 ways in which it can be satisfied. Construct graph with 2 n Hamiltonian cycles, where each cycle corresponds to some boolean assignment. Then add more graph structure to encode constraints on assignments imposed by the clauses. Nanyang Technological University MAS 714 November 8, 2018 6 / 20
The Reduction: Phase 1 3-satisfiability reduces to directed hamilton cycle Traverse path i from left to right iff x i is set to true Construction. Given 3-SAT instance Φ with n variables x i and k clauses. Construct G to have 2 n Hamilton cycles. Each path has 3(m + 1) vertices where m is number of clauses; vertice Intuition: numbered traverse from path left i from to left right to right (1 to 3m set + variable 3) x i = true. s x1 x2 x3 t 3k + 3 33 Nanyang Technological University MAS 714 November 8, 2018 7 / 20
The Reduction: Phase 2 Add3-satisfiability vertex c j for reduces clause to directed C j. c j has hamilton edgecycle from vertex 3j and to vertex 3j + 1 on path i if x i appears in clause C j, and has edge from vertex 3j + Construction. 1 and to vertex Given 3-SAT 3j if instance x i appears Φ with n invariables C x j i and k clauses. For each clause, add a node and 6 edges. C 1 = x 1 x 2 x 3 clause node 1 clause node 2 C 2 = x 1 x 2 x 3 s x1 x2 x3 t 3k + 3 34 Nanyang Technological University MAS 714 November 8, 2018 8 / 20
Correctness Lemma ϕ is satisfiable iff G has a Hamiltonian cycle. Proof. = : Suppose ϕ has a satisfying assignment x. Then, define Hamiltonian cycle in G as follows: if x i = true, traverse row i from left to right if x i = false, traverse row i from right to left for each clause C j, there will be at least one row i in which we are going in correct direction to splice clause vertex C j into cycle (and we splice in C j exactly once) Nanyang Technological University MAS 714 November 8, 2018 9 / 20
Correctness Lemma ϕ is satisfiable iff G has a Hamiltonian cycle. Proof. =: Suppose G has a Hamiltonian cycle Γ. If Γ enters clause vertex C j, it must depart on mate edge vertice immediately before and after C j are connected by an edge removing C j from cycle, and replacing it with that edge yields Hamiltonian cycle on G {C j } Continuing in this way, we are left with a Hamiltonian cycle Γ in G {C 1, C 2,..., C m }. Set x i to true iff Γ traverses row i left to right. Since Γ visits each clause node C j, at least one of the paths is traversed in correct direction, and each clause is satisfied. Nanyang Technological University MAS 714 November 8, 2018 10 / 20
TSP Traveling Salesman Problem Traveling salesperson problem Given a set of n cities and a pairwise distance function d(u, v), is there a TSP. Given a set of n cities and a pairwise distance function d(u, v), tour of length D? is there a tour of length D? 13,509 cities in the United States http://www.tsp.gatech.edu Figure: 13,509 cities in the United States http://www.tsp.gatech.edu 38 Nanyang Technological University MAS 714 November 8, 2018 11 / 20
TSP Traveling Salesman Problem Traveling salesperson problem Given a set of n cities and a pairwise distance function d(u, v), is there a TSP. Given a set of n cities and a pairwise distance function d(u, v), tour of length D? is there a tour of length D? optimal TSP tour http://www.tsp.gatech.edu Figure: optimal TSP tour http://www.tsp.gatech.edu 39 Nanyang Technological University MAS 714 November 8, 2018 12 / 20
Reducing Hamiltonian Cycle to TSP Theorem Hamiltonian Cycle P TSP Proof. Given a Hamiltonian Cycle instance G = (V, E), create n cities with distance function { 1 if (u, v) E d(u, v) = 2 if (u, v) / E TSP instance has tour of length n iff G has a Hamiltonian cycle. Nanyang Technological University MAS 714 November 8, 2018 13 / 20
Subset Sum Subset Sum Given natural numbers w 1,..., w n and an integer W, is there a subset that adds up to exactly W? Example Input: {1, 4, 16, 64, 256, 1040, 1041, 1093, 1284, 1344}, W = 3754 Answer: Yes. 1 + 16 + 64 + 256 + 1040 + 1093 + 1284 = 3754 Remark. With arithmetic problems, input integers are encoded in binary. Poly-time reduction must be polynomial in binary encoding. Theorem 3SAT P Subset Sum Nanyang Technological University MAS 714 November 8, 2018 14 / 20
Reducing 3SAT to Subset Sum Construction. Given a 3SAT formula ϕ with n variables and m clauses, form 2n + 2m decimal integers, each of n + m digits: include one digit for each variable x i and for each clause C j two numbers for each variable x i two numbers for each clause C j sum of each x i digit is 1; sum of each C j digit is 4. C 1 = x 1 x 2 x 3 C 2 = x 1 x 2 x 3 C 3 = x 1 x 2 x 3 x 1 x 2 x 3 C 1 C 2 C 3 x 1 1 0 0 0 1 0 x 1 1 0 0 1 0 0 x 2 0 1 0 1 0 0 x 2 0 1 0 0 1 1 x 3 0 0 1 1 1 0 x 3 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 2 W 1 1 1 4 4 4 Nanyang Technological University MAS 714 November 8, 2018 15 / 20
Reducing 3SAT to Subset Sum Lemma ϕ is satisfiable iff there exists a subset that sums to W. Proof. = : Suppose ϕ is satisfiable Choose integers corresponding to each true literal. Since ϕ is satisfiable, each C j digit sums to at least 1 from x i rows. Choose dummy integers to make clause digits sum to 4. C 1 = x 1 x 2 x 3 C 2 = x 1 x 2 x 3 C 3 = x 1 x 2 x 3 x 1 x 2 x 3 C 1 C 2 C 3 x 1 1 0 0 0 1 0 x 1 1 0 0 1 0 1 x 2 0 1 0 1 0 0 x 2 0 1 0 0 1 1 x 3 0 0 1 1 1 0 x 3 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 2 W 1 1 1 4 4 4 Nanyang Technological University MAS 714 November 8, 2018 16 / 20
Reducing 3SAT to Subset Sum Lemma ϕ is satisfiable iff there exists a subset that sums to W. Proof. =: Suppose there is a subset that sums to W Digit x i forces subset to select either row x i or x i (but not both). Digit C j forces subset to select at least one literal in clause. Assign x i = true iff row x i selected. C 1 = x 1 x 2 x 3 C 2 = x 1 x 2 x 3 C 3 = x 1 x 2 x 3 x 1 x 2 x 3 C 1 C 2 C 3 x 1 1 0 0 0 1 0 x 1 1 0 0 1 0 1 x 2 0 1 0 1 0 0 x 2 0 1 0 0 1 1 x 3 0 0 1 1 1 0 x 3 0 0 1 0 0 1 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 2 0 0 0 0 0 0 1 0 0 0 0 0 2 W 1 1 1 4 4 4 Nanyang Technological University MAS 714 November 8, 2018 17 / 20
More NP-Complete Problems Basic genres of NP-complete problems and paradigmatic examples Packing/covering problems: Set Cover, Vertex Cover, Independent Set Constraint satisfaction problems: SAT, 3SAT Sequencing problems: Hamiltonian Cycle, TSP Partitioning problems: 3D-Matching, 3-Color Numerical problems: Subset Sum, Partition Most NP problems are known to be either in P or NP-complete Notable exceptions: Factoring, Graph Isomorphism, Nash Equilibrium Theorem(Ladner 1975) Unless P = NP, there exist problems in NP that are neither in P nor NP-complete. Nanyang Technological University MAS 714 November 8, 2018 18 / 20
Karp s Karp's 21 21 NP-complete problems problems Dick Karp (1972) 1985 Turing Award Nanyang Technological University MAS 714 November 8, 2018 19 / 20 76
Small Difference? Big Difference P shortest path min cut 2-satisfiability planar 4-colorability bipartite vertex cover matching linear programming NP-complete longest path max cut 3-satisfiability planar 3-colorability vertex cover 3d-matching integer linear programming Nanyang Technological University MAS 714 November 8, 2018 20 / 20