University of Wisconsin Parkside Fall Semester 2008 Department of Computer Science Prof. Dr. F. Seutter Data Structures and Algorithms (CSCI 340) Homework Assignments The numbering of the problems refers to the exercises of Baase, van Gelder, Computer Algorithms. NP-Complete Problems 13.4 Show that each of the following decision problems is in NP.To do this, indicate what a proposed solution for a problem instance would be, and tell what properties would be checked to determine if a proposed solution justifies a yes answer to the problem. (a) the bin packing problem (b) the Hamiltonian cycle problem (c) the satisfiability problem (d) the vertex cover problem Solution: (a) Bin Packing Problem: How to pack or store objects of various sizes and shapes with a minimum of wasted space. Let S =(s 1,...,s n ) where 0 <s i 1, s i Q for 0 i n. The problem is to pack s 1,...,s n into as few bins as possible, where each bin has capacity one. Does S fit in k bins? Proposed solution: Guess a partition P 1,...,P k of S, k n, and check that each P i fits in one bin, 1 i k. The test needs linear time. In total there are up to n additions to execute. (b) Hamiltonian Cycle Problem: Does a given undirected graph have a Hamiltonian cycle? Proposed solution: Guess a permutation of the graph s vertices extended by its first vertex and check that that is a simple cycle. The test needs linear time. In total the existance of up to n edges is to check. (c) Satisfiability Problem: Given a propositional (boolean) expression, is there a truth assignment that satisfies it? Proposed solution: Guess a truth assignment of the formula and check that it satisfies it. The test needs linear time. In an expression of n variables there are n 1 operations to evaluate. 1
(d) Vertex Cover Problem: A vertex cover for an undirected graph G ist a subset C of vertices such that each edge is incident upon some vertex in C. DoesG have a vertex cover consisting of k vertices? Proposed solution: Guess a subset of k vertices and check that each edge of G has at least one end in the set. The test needs linear time in the number of edges. 2
13.11 Show that the Hamiltonian cycle problem is reducible to the traveling salesman problem. Solution: Let I HCP be the input set for the Hamiltonian cycle problem, I TSP be the input set for the traveling salesman problem, and G =(V,E) I HCP be an undirected graph. Define a function T : I HCP I TSP with T (G) =(G,n) as follows, G being a complete undirected weighted graph. G =(V,E E ), where E =(V V ) \ E are the edges to make G a complete graph, with weight function w : E E R { } { 1, falls e E w(e) =, falls e E n = V. 1. T can be computed in polynomial time. (Not asked here.) 2. Let the answer be yes for HCP and G. Then the answer is yes for TSP for (G,n) because there is a Hamiltonian cycle in G with total weight n. 3. Let the answer be no for HCP and G. Then the answer is no for TSP for (G,n) because there is no Hamiltonian cycle in G, with total weight n. Therefore the Hamiltonian cycle problem is (polynomially) reducible to the traveling salesman problem. 3
13.49 The nearest-neighbor strategy and the shortest-link strategy are two greedy strategies for an approximate solution of the Traveling Salesman Problem. Make up an example of a complete, weighted graph for which the tour found by the nearest-neighbor strategy has lower weight than the tour found by the shortest-link strategy. Algorithm 13.49 (Nearest-Neighbor TSP) Input: G =(V,E) complete, weighted graph Output: C cycle in G NearestNeighbor(G) 1 C 2 Select an arbitrary vertex s to start the cycle C 3 v s 4 while there are vertices not yet in C 5 do Select an edge (v, w) of minimum weight, where w is not in C 6 Add edge (v, w) toc 7 v w 8 Add edge (v, s) toc 9 return C Algorithm 13.49 (Shortest-Link TSP) Input: G =(V,E) complete, weighted graph Output: C cycle in G ShortestLink(G) 1 R E 2 C 3 while R 4 do Remove an edge (v, w) of minimum weight from R 5 if (v, w) does not make a cycle with edges in C and 6 (v, w) would not be the third edge in C incident on v or w 7 then Add edge (v, w) toc 8 Add the edge connecting the endpoints of the path in C 9 return C 4
Solution: 15 2 30 1 5 50 45 10 40 35 20 25 3 10 4 Order of edges selected Nearest-neighbor tour: (1, 3), (3, 2), (2, 4), (4, 5), (5, 1) Shortest-link tour: (1, 3), (4, 5), (1, 2), (3, 4), (2, 5) Cycle Weight Nearest-neighbor tour: 1, 3, 2, 4, 5, 1 105 Shortest-link tour: 1, 3, 4, 5, 2, 1 110 Optimal tour: 1, 2, 3, 4, 5, 1 100 5