Homework 7: Due December 7 at midnight Eastern time. Submit your solutions typed and in a pdf document. To receive full credit, explain your answers. If you collaborate with another student or use outside sources, please list those students’ names and the URL/title/etc. of the sources that you re ferred to. Collaboration is permitted, but you must write up your own solu tions. Treat arithmetic operations (addition, subtraction, multiplication, and divi sion) as constant time operations. Problem 1: Suppose we have a standard stack with Push and Pop opera tions, each costing 1 unit of time. For backup purposes, every time the stack reaches k elements, the OS automatically writes a copy to the hard drive (here k is some fixed value specified by the user) and delete all elements from the stack. Writing to the hard drive takes k units of time (1 unit of time per element), and deleting all elements takes an additional k units of time (1 unit of time per element). Perform an amortized running time analysis to find an upper bound on the average length of time per operation for a sequence of n Push/Pop operations. Report your answer in units of time, not bigO notation. Problem 2: In class, we analyzed the binary counter, in which a binary array was used as a counter. Suppose that in addition to paying $1 to flip a bit, we also have to pay to initialize a bit, or create the element in the array the first time it is used. The cost of this initialization is $2i , where i is the index of the bit (assume the furthest right bit has index 0). After initialization, the new bit will be set to 0 (so you must pay an additional $1 to change it to a 1, if you want it to be a 1). Assume that the array starts as the empty array [], which we interpret to represent the value 0. For example, to perform the first increment, we must first add a bit at index 0, which costs $1, and then flip this bit to 1, at an additional cost of $1. This increment thus costs a total of $2, and the array is [1]. The next increment adds a new bit to the array (making it [0, 1]), then flips both bits, so the array is [1, 0]. This increment has a total cost of $4: $2 for creating the new bit (which was at index 1 so had cost $21 ), and $2 for flipping two bits. The 1 third increment flips one bit, creating array [1, 1], at a cost of $1 for flipping one bit. No new bits need to be created here! The fourth increment creates another bit at index 2 to get array [0, 1, 1], and then flips all 3 bits to get [1, 0, 0]. This increment has a total cost of $7: $4 for creating the new bit ($22 ), and $3 for flipping 3 bits. Assume that there are no costs other than those stated above. Over a sequence of n increments, what is the amortized cost per increment? State your answer in terms of $s, not bigO. Problem 3: The Subset Sum problem is as follows: Given a set of integers S and an integer t, is there a subset S 0 of S such that the sum of all elements in S 0 is equal to t? The Equal Partition problem is as follows: Given a set of integers K, is it possible to divide K into two sets such that the two subsets have the same sum? Show that Equal Partition reduces to Subset Sum. Hint: We have to figure out how to solve Equal Partition, assuming that we are given an algorithm to Subset Sum. Both problems take a set as input; what should we set t to in Subset Sum? Problem 4: The Hitting Set problem is as follows: Suppose we have a set of elements V = {v1 , ..., vn }. Suppose we have a collection of sets S1 , ..., Sk , where each Si is a set containing some of the vj elements. A hitting set is a set H that is a subset of V , such that H contains at least one element from every Si . Given some value b, we wish to find a hitting set with b or fewer elements, if it exists. For example, suppose S1 = {v1 , v2 , v4 }, S2 = {v2 , v3 , v5 }, S3 = {v1 , v3 , v5 }, and b = 3. Is there a hitting set with 3 or fewer elements? One example of a hitting set is {v2 , v5 }. This hitting set has size 2, so we have found a solution to the problem. The Vertex Cover problem is as follows: Given a graph G and some value c, is there a set of c vertices in the graph such that every edge in the graph is adjacent to at least one of those c vertices (in other words, every edge must have at least one of its two endpoints included in the set of c vertices)? (Note that this problem definition is slightly different from what we did in class.) 2 Suppose we know that Vertex Cover is NPComplete. Use that to show that Hitting Set is NPComplete. Extra Credit: In class, we covered the Clique problem, in which we were given a graph G and had to find the largest clique in G. Recall that a clique is a set of nodes such that every node is connected to every other node in the set (i.e., everybody in the set knows everybody else in the set). The Clique3 problem is the same problem, except that we are guaranteed that every node has degree at most 3. Suppose that I want to prove that Clique3 is NPComplete. To do this, I make the following argument: We know that the Clique problem is NP Complete. Clique3 reduces to Clique, because if we can solve Clique for graphs in general, then clearly we can also solve it for graphs where the nodes have degree at most 3. Thus, Clique3 is also NPComplete. Part a: What is wrong with the above argument? Part b: Show that Clique3 is not NPComplete (assuming that P 6= NP). (Hint: what is the largest possible clique in a graph where the nodes have degree at most 3?) 3
Enter the password to open this PDF file:











