CISC 320 Algorithms and Advanced Programming, Spring 2001, Saunders
First midterm exam

Name:


Multiple choice problems (2 points each):
  1. Which of the following shows the correct relationship among some of the more common computing time for algorithms?

    A.     O(lg(n)) < O(n) < O(n*lg(n)) < O(2n) < O(n2)

    B.     O(n) < O(lg(n)) < O(n*lg(n)) < O(2n) < O(n2)

    C.     O(lg(n)) < O(n) < O(n*lg(n)) < O(n2) < O(2n)

    D.     O(n) < O(lg(n)) < O(n*lg(n)) < O(n2) < O(2n)

  2. Which of the following sorting algorithms yield approximately the same worst-case and average-case running time behavior in O(n*lg(n))?

    A.     Insertion sort and mergesort

    B.     Quicksort and heap sort

    C.     Insertion sort and quicksort

    D.     Heap sort and mergesort

  3. Provably every comparison based sorting algorithm must use at least about n*lg(n) comparisons. This is because the decision tree for the algorithm must have at least

    A. n2 leaves.       B. 2n nodes       C. n! nodes.       D. n! leaves.

  4. The "select" problem for order statistics discussed in the first lecture is

    A. to determine the median of 5 elements.

    B. to determine the k-th smallest element in an array for given array and k.

    C. To choose a "Comparator" function to use on the median problem.

    D. to determine the ripest apple in a barrel.

  5. Which sort will operate in quadratic time relative to the number of elements in the array (on the average)?

    A. Quicksort                B. Heap sort                

    C. Mergesort                D. None of the above

  6. Given a set of keys in decreasing order, say n, n-1, n-2, ... 1. We sort it using insertion sort. About how many comparisons are done?

    A. 2n lg(n)         B. n2/2         C. n2         D. 2n2

  7. Which is true of the divide and conquer algorithm to the "select k-th largest" problem

    A. It rearranges the array somewhat.

    B. It leaves the array in sorted order.

    C. It uses a "partition" function to divide the array exactly in half if the array size is even and into two segments whose sizes differ by one if the array size is odd.

    D. It uses about n lg(n) array element comparisons.

  8. Give the best answer regarding the problem to find the largest element in an array of n elements.

    A. Provably, any algorithm that solves the problem must use at least n-1 comparisons.

    B. There is an algorithm which uses only n-1 element comparisons.

    C. Both A and B

    D. Neither A nor B

  9. Give the best answer regarding the problem to find the second largest element in an array of n elements.

    A. Provably, any algorithm that solves the problem must use at least 2n - 3 comparisons

    B. Provably, any algorithm that solves the problem must use at least about 3n/2 comparisons.

    C. Provably, any algorithm that solves the problem must use at least about n + lg(n) comparisons.

    D. Provably, any algorithm that solves the problem must use at least about n * lg(n) comparisons.

  10. Give the best answer regarding the problem to find both the largest and smallest element in an array of n elements.

    A. There is an algorithm that uses about 2n - 3 comparisons

    B. There is an algorithm that uses about about 3n/2 comparisons.

    C. There is an algorithm that uses O(n) comparisons.

    D. All of the above.

  11. Two approaches to the selection (and median) problem are a randomized method based on randomized partitioning and a deterministic method which starts by computing the medians of groups of 5 elements.

    A. randomizedSelect has the fewer comparisons in practice (average/expected number of comparisons).

    B. randomizedSelect has the better worst case number of comparisons.

    C. Both A and B

    D. Neither A nor B

  12. The randomized divide and conquer approach to selection based on randomized partitioning

    A. Has an expected case number of comparisons E(n) which is O(n).

    E. Has a worst case number of comparisons W(n) which is O(n*lg(n)).

    C. Both A and B

    D. Neither A nor B

  13. Boyer-Moore and Knuth-Morris-Pratt are two algorithms

    A. to select the k-th largest of an array of characters.

    B. to find approximations to a pattern in a text

    C. to support dynamic dictionary operations.

    D. None of the above

  14. For a pattern of size m and a text of size n,

    A. both Boyer-Moore and Knuth-Morris-Pratt algorithms require no more than O(nm) comparisons.

    B. Knuth-Morris-Pratt can use as few as about n/m comparisions in special cases.

    C. Both A and B

    D. Neither A nor B

  15. Balanced tree schemes such as Red-Black trees are used instead of sorted arrays when

    A. The data is fixed and many look-ups (searches) are to be done.

    B. When there is insufficient real memory for the array.

    C. When the data is dynamic, with many insertions/deletions of items as well as look-ups.

    D. The records have a "color" field.

  16. If the black height of a red-black tree is 4, how many keys are stored in the tree (how many interior nodes are there)?

    A. between 1 and 16

    B. between 4 and 16

    C. between 16 and 256

    D. between 256 and 4096

  17. Which one of the following is NOT an invariant property maintained in red-black trees.

    A. no two red edges in a row from root to leaf

    B. all leaves have same black depth

    C. no node has two red children

    D. the root node is black

  18. A split step in a red-black tree is when

    A. the colors of a black node and it's two red children are flipped.

    B. the colors of a red node and it's two black children are flipped.

    C. a temporary situation in which a red node has a red child is fixed.

    D. a temporary situation in which a black node has a black child is fixed.

  19. A rotation step in a red-black tree is when

    A. the colors of a black node and it's two red children are flipped.

    B. the colors of a red node and it's two black children are flipped.

    C. a temporary situation in which a red node has a red child is fixed.

    D. a temporary situation in which a black node has a black child is fixed.

  20. A semblance of balance is maintained in a red-black tree because the overall depth (counting all edges from root to leaf) of a leaf is never more than this multiple of it's black depth (counting black edges from root to leaf).

    A. 1         B. 2         C. 4         D. 16

  21. The overall depths of two different leaves in a red-black tree may vary by at most how much if the tree has 1024 nodes?

    A. 2         B. 4         C. 5         D. 10

  22. Search in a red-black tree with n nodes requires how many key comparisions?

    A. O(1)         B. O(lg(n))         C. O(n1/2)         D. O(n*lg(n))

  23. Insertion in a red-black tree with n nodes requires how many key comparisions?

    A. O(1)         B. O(lg(n))         C. O(n1/2)         D. O(n*lg(n))

  24. In a heap with n elements, extracting the maximal value (and fixing the heap) requires how many key comparisions?

    A. O(1)         B. O(lg(n))         C. O(n1/2)         D. O(n*lg(n))

  25. Which of the following algorithms have worst case number of comparisons and average case number of comparisons which differ by more than a constant factor.

    A. finding max and min in an unordered array.

    B. merge sort.

    C. Both A and B

    D. Neither A nor B

  26. Which of the following algorithms have worst case number of comparisons and average case number of comparisons which differ by a factor of about 2.

    A. Insertion sort

    B. quick sort

    C. Both A and B

    D. Neither A nor B


  27. (8 points) In Boyer-Moore string searching, consider this text and pattern: (the line of numbers is just to help you identify positions)
               |
               v
             123456789 123456789 123456789 123456789 123456789 123456789 12345
       Text: no analog of asymptotic complexity exists in research on bananas.
    Pattern: banana
    
    The initial comparison sequence fails after "ana" at the right of the pattern has matched and " " fails to match "n" in position 3.

    __________________ What is the next position of the pattern on the basis of the char-table heuristic?

    __________________ What is the next position of the pattern on the basis of the match-table heuristic?

    __________________ Which of the two choices will the Boyer-Moore algorithm use?

    __________________ How many character comparisions will Boyer-Moore require to find this pattern in this text? Answer accurate to within plus/minus 10 comparisions is accepted.

  28. (10 points) Assume that constructHeap(A,n) requires at worst n comparisons to construct a heap of size n in A. Assume that fixHeap(A, k) requires at worst lg(k) comparisons to fix a heap A of size k, which has the heap property everywhere except at the root. Heapsort works as follows.
    void heapSort(A, n)
    {  constructHeap(A, n);
       for (int i = n, i > 1; --i)
       { swap(A[1], A[i]);
         fixHeap(A, i-1);
       }
    }
    
    A. Write a recursion relation or summation formula expressing the worst case number of comparisons used by the heapSort function above.

     

     

     

    B. What is the worst case number of comparisions used by heapSort on an array of size n?

     

     

     

     

     

     

     

     

     

     

  29. (20 points) Write two of the following three algorithms
    1. Assume merge(A, s1, e1, s2, e2, B) is written. If regions A[s1..e1) and A[s2..e2) are already sorted, then merge combines them into one sorted sequence in B[1..n), where n = (e1 - s1) + (e2 - s2). Using this merge function, write mergeSort. mergeSort(A, s, e) sorts A[s..e).
    2. insertSort(A, n) sorts A[1..n) by the insertion method. Write insertSort.
    3. Write randomizedSelect(A, s, e, k). It assumes k <= e - s, and returns the k-th smallest element of A[s..e). A is unsorted. You may assume randomizedPartition has been written and just use it. Assume randomizedPartition(A, s, e) returns an index q in the range [s+1 .. e) and rearranges A[s..e) in such a way that A[s..q) < A[q..e).
     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

     

  30. (10 points) Explain in a few sentences how introspective sort works. (Incidentally, it is included in the C++ standard template library.)