Introduction
to the Design and Analysis of Algorithms
(Textbook. Click for information.)
 CS 3343/3341
 Analysis of Algorithms
 Spring 2012

 Recitation 8
 Exam Review

    Week 8: Mar 06-Mar 08
Mid-term Exam: Mar 8, in class

 Due (on time): 2012-03-07  21:59:59
 No late submission

Recitation 8 should be submitted following directions at: submissions with deadlines
  • 2012-03-07  21:59:59 (that's Wednesday, 7 March 2012, 9:59:59 pm) for full credit.
  • No late submission.

Note (at 1pm, Wednesday): I hadn't been able to ssh or sftp to UTSA since yesterday evening. Finally, the submission program is online and I'm ready to give a password to those who submit. Sorry for this delay. (In using the password, the "User name" or "Account name" should be "CS3343", without the quotes and with caps for "CS". The password is all lowercase.)

Notice: you may bring to class ONE SHEET of paper with anything you like written on it. Just a single sheet, standard size, for yourself alone.


Induction. See

Not on this review exam.


Loop Invariant. See

  1. Carry out the proof asked for in the table below:

    Consider the following function that will calculate
    the quotient and remainder of its two input
    parameters. (It does with this without using an
    actual "divide", but just with addition and
    subtraction.)

    The inputs are:

       n >= 0 (the "dividend") and
       d >= 1 (the "divisor").

    The algorithm calculates and returns:

       q >= 0 (the "quotient") and
       d >= 1 (the "divisor").
       r (the "remainder"),

    such that on termination, the following are true:

       r >= 0,
       r < d, and
       n = d * q + r.

    Here is the picture:
                  q
              +-------
           d  |   n
         -----+
               -------
                  r
    
    Here is the function:
       // div: inputs n >= 0 and d >= 1
       //   returns the pair (q, r)
       (int, int) div(int n, int d) {
          int q = 0, r = n;
          while (r >= d) {
             q = q + 1;
             r = r - d;
          }
          return (q, r);
       }
    

    The loop invariant is:
       n = d * q + r

    Carefully write out all four steps in the proof.


Exponentiation. See Exponentiation.

  1. We studied to algorithms for raising an integer to an integer power: a slow algorithm and a fast one. Consider the algorithms as raising a fixed a to a power n, so that we have two function expslow(n), and expfast(n), each calculating an.

    1. Give the Θ bounds on the performance of each of these two algorithms.

    2. In one of the weird topics, we found a special application for the fast exponentiation. What was it?


Circular Queues. See Circular Queues. Not on this review exam.
Recursion. See Recitation 2: Recursion. Not on this review exam.


Linked Lists. See Recitation 3: Linked Lists. Not on this review exam.


Quicksort. See Quicksort Explained.

  1. For your text's basic Quicksort algorithm, give the worst case performance, in terms of Θ. Give a specific example of an array for which the worst case is realized. Explain the simple alteration to the basic quicksort algorithm that guarantees an expected better performance. Give this expected better performance in terms of Θ.


Randomization. See Randomize an Array.

  1. [Oops! This is just intro material, and not a question. Just leave #4 blank.] Consider the following algorithm that we studied in class:

    (Here randomInt(a, b) returns a true random integer r, satisfying a <= r <= b such that each of the integers between a and b (inclusive) is equally likely to occur.)

  2. Trace through the execution of this function, showing the result after each loop iteration, using as input the array of integers A = {6,7,8,9}, so that n == 4. Assume the function randomInt returns 2, 1, and 3 the three times it is called.

  3. Say in detail what happens if the loop is written

    with "n" instead of "n-1". (You must say much more than just that "there is another loop iteration.")

  4. What does this function do, and what did we use it for in the course?


Medians. See Recitation 5: Medians. Not on this review exam.


Strassen Matrix Multiplication. See

  1. Consider the matrix multiplication of 4-by-4 matrices.
    1. Using the ordinary method of matrix multiplication, how many scalar multiplications are required? Don't just state this number, but calculate the number from the ordinary matrix multiplication algorithm, where each element of the answer is the inner product of a length 4 row times a length 4 column.
    2. Using the divide-and-conquer approach and Strassen's method of multiplying 2-by-2 matrices, how many scaler multiplications would be required? Again justify your answer.

  2. Now consider 5-by-5 matrices. Again how many scalar multiplications would be required using ordinary matrix multiplication? (Just a number for the answer.) Suppose one could multiply 5-by-5 matrices using 91 scalar multiplications. Give the recurrence for a divide-and-conquer algorithm based on this supposition. Use the Master Method (or some other approach) so get the asymptotic performance in this case. Would this be an interesting result?


Heapsort, Priority Queues. All heaps are max-heaps. See

  1. Suppose an array A has locations A[1], ..., A[9] equal to {5,13,2,25,7,17,20,8,4}. Show step-by-step the process of making this array into a heap.

  2. Suppose an array A has locations A[1], ..., A[9] equal to {25,13,20,8,7,17,2,5,4}. This is a max-heap. Show just the first two steps of the progress of heapsort on this array, starting to sort it into increasing order. At each of the two steps indicate also the value of heapsize.

  3. Suppose an array A has locations A[1], ..., A[11] equal to {20,16,8,14,11,7,4,10,12,6,3}. This is a max-heap. As one of the 2 steps of a priority queue, show step-by-step the process of removing the maximum element and restoring the heap property.

  4. Suppose an array A has locations A[1], ..., A[10] equal to {18,15,9,11,10,6,2,8,1,7}. This is a max-heap. As one of the 2 steps of a priority queue, show step-by-step the process of inserting the element 16 into this heap and restoring the heap property. (This uses the Heap-Increase-Key function.)


Recursion Trees. For analysing an algorithm and for solving recurrences. See the method applied to:

Not on this review exam.


Master Method. Cookbook for solving recurrences. See

  1. Attempt to use the Master Method to solve each of the following recurrences:

    1. 2 T(n / 2) + n
    2. 2 T(n / 2) + √n
    3. 3 T(n / 2) + n
    4. 4 T(n / 2) + n2
    5. 4 T(n / 2) + n
    6. 8 T(n / 2) + n2
    7. 9 T(n / 3) + n3
    8. 2 T(n / 4) + √n
    9. 16 T(n / 4) + n3


Revision date: 2012-03-03. (Please use ISO 8601, the International Standard.)