1. Tear off this sheet and use it to keep your answers covered at all times. If you need more scratch paper, I have some.
  2. Write your name on the back of the test by the staple to facilitate returning the exams.
  3. If you don't understand a question, come and ask me about it!
  4. Do NOT use ink. Programming often requires one to change his/her mind.
  5. No question is meant to have a syntax error. If it does, fix it or ask me how I want the error resolved.
  6. You are graded not only on the correct answer, but also the best answer.
  7. NO calculators are allowed on the test. They will not be needed.



1 CS 2200 - EXAM 3
Fall 2000 100 Points

Multiple Choice (3 points each)

  1. A breadth first traversal of a graph is commonly done using
    1. a stack
    2. a queue
    3. a heap
    4. a topological ordering
    5. none of the above
  2. A depth first traversal, visiting all n nodes of a balanced tree has what complexity?
    1. O(n log n)
    2. O(n)
    3. O(log n)
    4. O(1)
    5. none of the above
  3. Finding a node (in an AVL tree) has what complexity?
    1. O(n log n)
    2. O(n)
    3. O(log n)
    4. O(1)
    5. none of the above
  4. Which is true of the complexity of a quicksort and an insertion sort?
    1. A quicksort has better worst-case complexity.
    2. Although they both have the same average-case complexity, quicksort is normally faster.
    3. If the data is mostly in order, insertion sort can be much faster than quicksort.
    4. Neither are stable.
    5. All of the above.

  5. Stating a total order of the nodes in a directed acyclic graph is termed
    1. dag
    2. adjacent
    3. matching
    4. topological order
    5. first fit
  6. For the spanning tree below, which edge would be part of a minimal spanning tree.

    tex2html_wrap117

    1. EF
    2. CD
    3. FB
    4. BC
    5. all of the above
  7. Which is true of a tournament tree which is used to find a first fit in the bin packing algorithm?
    1. The item to be packed is placed in the winner bin.
    2. The item to be packed is placed in the loser bin.
    3. The item to be packed is placed in the bin representing the leftmost bin.
    4. If the leftmost bin is big enough, it is used to place the current item.
  8. Which is true of the stable marriage problem.
    1. stable marriage problem worries about maximizing the number of matches
    2. the stable marriage problem is only defined for a bipartite graph.
    3. edges representing the match are allowed to be adjacent
    4. the stable marriage problem requires an undirected graph

  9. A friend explains he has written a greedy algorithm. This implies
    1. The algorithm is efficient, but not optimal.
    2. The algorithm is optimal, but not efficient.
    3. The algorithm runs in polynomial time.
    4. An exhaustive search will be performed.
  10. Which is true of a mergesort and a quicksort?
    1. both have the same average case complexity
    2. both are greedy
    3. both take O(n) to combine the pieces
    4. all of the above
  11. A friend decides to solve the shortest path problem but decides to use a regular queue to store the edges (rather than the priority queue you used in program seven). What type of results would you expect to see?
    1. The results would be nothing meaningful.
    2. If all edges were the same cost, it would work fine.
    3. You have produces a minimal spanning tree instead of a shortest path tree.
    4. You have produced a depth first traversal.
  12. You decide on the following strategy for finding a bipartite cover. You select the node which is connected to the highest number of nodes which are not already covered. What is true of your algorithm?
    1. you may not find a cover
    2. you will always find a cover, but it might not be minimal
    3. if a minimal cover exists, you will find it
    4. a minimal cover always will be found, but it may not be unique
  13. You are trying to determine the cost of the single source shortest path algorithm with n nodes and e edges. You decide the complexity is
    1. O(log n)
    2. O(e log e)
    3. O( n)
    4. O(tex2html_wrap_inline113)
    5. O(tex2html_wrap_inline115)

Short Answer

  1. (8 points)
    1. What does the following code on an adjacency matrix (isArc[][]) do? Notice, I want summary of what the code accomplishes not a line by line description of which each C++ statement does.
    2. For the graph below, what is returned when doit is called?
    int doit()
    {
      for (int i=0; i < MAX;i++)
      { int ct = 0;
        for (int j=0;j < MAX;j++)
            if (i!=j)
               if (isArc[i][j] || isArc[j][i]) ct++;
        if (ct ==0) return i;
      }
      return -1;
    }

    tex2html_wrap119

  2. (10 points) For a quicksort, explain the need for sentinels and middle of three pivot selection. Does either technique change the complexity of a quicksort?
  3. (4 points) In the following AVL tree, show the tree after insertion of 56.

    tex2html_wrap121

  4. (20 points) A graph is colored legally if no node has the same color as an adjacent node. Assuming colors are just represented by different integers (1=yellow, 2= blue, etc.) For a graph (stored as an adjacency list) that has already been read in and colored, determine if the coloring is legal. Assume all variables are public.

    class arc
    { int node1;
      int node2;
      int weight;
      arc * next;
    }
    class node
    { arc * adj; // head pointer for the adjacency list
      boolean visited;
      int color;
    }
    class graph{
       int size;  //  the actual number of nodes in the graph
       node n[MAX];
    }

  5. (19 points) For the code below,
    1. What does the following code accomplish?
    2. Show the tree after doit(A) is called. Show the tree after doit(B) is called. Be sure to label each node with its apprpriate weight.
    3. Does this always work? If not, show a counter example.

    enum weight {TOOLEFT=-2, HEAVYLEFT, EQUAL, HEAVYRIGHT, TOORIGHT};
    void doit( AvlNode * & k2 )
    {
      AvlNode *k1 =  k2->Right;
      k2->Right = k1->Left;
      k1->Left = k2;
      k2->wght = EQUAL;
      k1->wght = EQUAL;
      if(k2->Right == NULL)
         k2->wght = (weight)((k2->wght)-1);
      if(k2->Left == NULL)
         k2->wght = (weight)((k2->wght)+1);
      k2 = k1;
    }

    tex2html_wrap123



Vicki Allan
Fri Aug 3 13:00:10 MDT 2001