Solutions to Review Questions for the Final Exam

CS409 - Spring 2000

Please let me know via email (chew@cs.cornell.edu) if you discover mistakes in my solutions.  Past experience indicates that I may have made some.

The review questions given here are not meant to exhaust the possible topics for exam questions.  Check the previous set of review questions (Solutions to Review Questions for Midterm) the online schedule, and consider reviewing the homework assignments.

Review Questions

  1. Fill in the table below with the expected time for each operation. Use big-O notation. The operations are insert (place a new item in the data structure), member (test if a given item is in the data structure), and findMin (return the value of the minimum item in the data structure). You are to use the standard implementation for each of these data structures, although you can use O(1) extra space if you find it useful to do so.

    Note that the findMin operation can be done quickly (O(1)) because there is no operation that deletes an item.  Thus, we can always know the min by simply holding it in a single word; whenever there is an Insert, we update the min.

    Data Structure insert member findMin
    sorted array O(n) O(log n) O(1)
    unsorted array O(1) O(n) O(1)
    balanced tree (red/black tree) O(log n) O(log n) O(1)
    hashing O(1) O(1) O(1)
    priority queue using a heap O(log n) O(n) O(1)
    sorted linked list O(n) O(n) O(1)
    unsorted linked list O(1) O(n) O(1)

     

  2. Briefly explain why sorting with comparisons requires Omega(n log n) time.
    Very Brief: Because there are so many possible answers.
    Less Brief: There are n! different answers, so any algorithm for sorting has a corresponding comparison tree with that many leaves. A binary tree with K leaves has height Omega(log K), so the worst case number of comparisons for any sorting algorithm is the height of the comparison tree or Omega(log(n!)) which is equivalent to Omega(n log n).

     

  3. For this problem, you are to answer some questions about the following graph.
    		       (e)             (f)
    			|		|
    			|		|
    			|17		|15
    			|		|
    		3	|	10	|     1
           (A)-------------(c)-------------(g)---------(h)
    	|		|		|	  /
    	|		|		|	/
    	|7		|9		|11   /5
    	|		|		|   /
    	|	12	|	14	| /
           (b)-------------(d)-------------(i)
    1. In what order are edges added to the Minimum Spanning Tree (MST) using Kruskal's Algorithm (merging subtrees)? List the edges by giving their endpoints.
      gh,Ac,hi,Ab,cd,cg,fg,ce
    2. In what order are edges added to the MST using Prim's Algorithm (growing a single tree) starting from vertex A?
      Ac,Ab,cd,cg,gh,hi,fg,ce

     

  4. Consider the following flow network with source s and sink t.
    		       (e)------------>(t)
    			^	8	^
    			|		|
    			|17		|15
    			|		|
    		3	|	10	|     1
           (a)------------>(c)------------>(g)<--------(h)
    	^		^		^           ^
    	|		|		|           |
    	|7		|9		|11         |5
    	|		|		|           |
    	|	12	|	14	|           |
           (s)------------>(d)------------>(i)----------
    1. What is the maximum flow?
      The maximum flow is 15.  Here is one way to achieve this flow (here, each edge is followed by the flow on that edge): sa:3, ac:3, ce:3, et:3, sd:12, di:12, ig:11, ih:1, hg:1, gt:12
    2. What is the minimum cut?  Where is it?
      The minimum cut value is 15.  This cut is achieved by cutting at edges ac and sd.

     

  5. Consider the following decision problem (we'll call it the Graph Value Problem).
    Input: An undirected graph G with n vertices, a list L of n nonnegative integers, and a bound b.
    Question: Is there a way to assign the numbers in L to the vertices of G such that (1) each number is assigned to exactly one vertex and (2) the graph's value is < b?  The value of the graph is the sum of all the edge values; each edge value is the product of the numbers assigned to its endpoint vertices.
    1. Show that the graph value problem is in NP.
      Given an assignment of values to nodes, it's easy to verify that the assignment produces a valid solution with total value < b.  By definition, GV is in NP.
    2. Show that Graph-Value is NP-complete by showing that Vertex-Cover reduces to Graph-Value.
      Given a Vertex-Cover problem (i.e., a graph G and a bound b), we produce a list of b zeros and n-b ones where n is the number of vertices in the graph.  This list, the original graph G, and the bound b = 0 make a Graph-Value problem.  It's easy to see that the graph has a vertex cover of size b iff there is an assignment that produces the graph-value 0 (the vertex cover corresponds to the vertices that are assigned 0 in the GV problem).
    3. Suppose we disallow zero-values.  Is the problem still NP-complete?
      Yes.  We can use a list of b ones and n-b e's where e is the number of edges in the graph G.  If there is a vertex cover then there is a way to assign the 1's and e's so that each edge has at least one endpoint with value 1 (i.e., the edge has value either 1 or e); thus, the total graph value is < e2.  If there is no vertex cover then some single edge ends up with value e2.  [This still doesn't resolve the case in which G has just a single edge, but that case is trivial and can be resolved separately.  Another solution is to use the value (e+1) instead of e.]

     

  6. [from Goodrich & Tomassia] Consider a sequence of n operations where each operation is either a red operation or a blue operation.  The first operation is red and the second is blue.  The running time of a blue operation is always constant.  The running time of the first red operation is constant, but each successive red operation takes twice as long as the previous red operation.  What is the amortized time of the red and blue operations under the following conditions?
    1. There are always Theta(1) blue operations between successive red operations.
      Amortized time per operation is O(2n/n).
    2. There are always Theta(sqrt(n)) blue operations between successive red operations.
      Amortized time per operation is O(2sqrt(n)/n).
    3. The number of blue operations between a red operation and the next red operation is always twice as many as there were between the previous red operation and the current read operation.
      Amortized time per operation is O(1).

     

  7. Suppose we know A<B (i.e., A reduces to B in linear time) and B<C where A, B, and C are all problems. Further, suppose we know that the running time for B is Theta(n log n).
    1. What can be said about the running time for A?
      There is an algorithm for A with running time O(n log n) (note: big-O, not Theta).
    2. What can be said about the running time for C?
      Any algorithm for C requires Omega(n log n) time (note: Omega, not Theta).

     

  8. In a graph, a cycle is a path that comes back to its initial vertex. A simple cycle is a cycle with no repeated vertices. A Hamiltonian cycle is a simple cycle that visits every vertex of the graph. Consider the following two problems; each takes as input a graph G with n vertices.
        Problem HC: Does G have a Hamiltonian cycle?
        Problem B: Does G have a simple cycle of length exactly floor(n/2)?  (The floor function just rounds its input down to the nearest integer.)
    Problem HC is known to be NP-complete. Show that Problem B is also NP-complete.

    There are two things that have to be proved: (1) B is in NP and (2) HC<B.  The first part is easy; we simply observe that if we are given a claimed-solution to problem B then we can check that the solution is a cycle of length floor(n/2) in polynomial time.  Thus, B is in NP.

    For the other part, we assume that we are given an instance of HC (i.e., we have a graph G with n vertices).  Now we show how to solve HC by making use of an imaginary program for B.  We create a new graph G' that is made of 2 copies of G; each copy is unconnected to the other.  Note that the original graph G has a Hamiltonian cycle iff the new graph G' has a simple cycle of length exactly half the number of vertices in G'.  Thus HC<B and B is NP-complete.

  9.  

  10. Problem 6.1 on page 160 of the text. (Show that Vertex Cover is still NP-complete even when all vertices in the graph are restricted to even degree.)

    It's easy to see that VCE (Vertex Cover with only Even-degree vertices) is in NP.  The harder part is to show that some NP-complete problem reduces to VCE.  The obvious candidate for the reduction is VC (Vertex Cover). Thus, we need to find a way to solve a VC problem (input: a graph G and a bound b) by making use of an imaginary program for VCE.  One idea is to add an edge to each odd-degree vertex to make it have even degree.  But where should each such edge go?  We'll send them all to a single new vertex v.  

    Fortunately, if we do this, the degree of v turns out to be even.  This is because the sum of all vertex degrees in a graph is always even (the sum is equal to the number of endpoints and, since each edge has two endpoints, the total number of endpoints is even); thus, the number of odd-degree vertices must be even.

    This looks good so far.  We have a new graph with one new vertex, it's closely related to the old graph, and all the vertex degrees are even.  If we can find a vertex cover of size b+1 and if that cover uses the new vertex v then we can simply remove v and we've got a vertex cover of size b for our original graph.  But to make this work, we have to know that v is used in our vertex cover; it clearly doesn't have to be used since all the edges to v might be covered by choosing their other endpoints.

    So now we need a way to force v to be part of the vertex cover.  Instead of using a single vertex we can use a triangle of vertices v, v1, and v2.  G' is a graph in which all the odd-degree vertices of the original graph are connected to v and v forms a triangle with v1 and v2.  Consider a vertex cover of G' and assume this cover does not include v.  Then it must include both v1 and v2 or some triangle edge would be uncovered.  If we take v1 out of the cover and replace it with v then, since the triangle vertices remain covered, we still have a vertex cover.  Thus any vertex cover of G' either includes v or, if it doesn't include v, it can be changed into a same-size vertex cover that does include v.

    So here's our algorithm for VC.  Use G to build the graph G' as described above.  Now a vertex cover of G of size b can be made into a vertex cover of G' of size b+2 just by adding vertices v and v2 (observe that all the new edges of G' are covered).  Also a vertex cover of size b+2 of G' either uses v or it can be made into a same-size cover that uses v.  Observe that at least one of v1 and v2 must be in the cover. We can remove v and either v1 or v2 from the cover of G' and we still have a cover of the edges of G. Thus, G has a cover of size b iff G' has a cover of size b+2.

     

  11. Problem 6.2 on page 160 of the text. (Show that Set Cover is NP-complete by making use of Vertex Cover.)

    The easy part is to show that SC (Set Cover) is in NP by observing that a solution for SC can be easily checked in polynomial time.  Now we have to show VC<SC by showing how to solve VC by using an imaginary program for SC.  Consider a VC instance (graph G and bound b).  Number all the edges from 1 to m.  Our set of SC elements will be the set of edge numbers {1,2,...,m}.  Our subsets will correspond to vertices, one subset for each vertex.  The subset for vertex v will consist of all the numbers corresponding to the edges that use vertex v.

    Observe that if VC has a vertex cover of size b then the subsets corresponding to those vertices form a set cover also of size b.  If we start with a set cover of size k then the subsets used in the set cover correspond to a vertex cover of same size.  Thus, G has a set cover of size b iff the derived SC problem has a set cover of size b.  This means that VC<SC and SC is NP-complete.