CS CS 3343 -- Algorithms
Mid-Term Exam
Thursday, 4 March 2004

Questions about heaps, heapsort, and priority queues (20 points).

  1. Consider the numbers 23, 18, 12, 14, 9, 7, 10, 6, 5, 8 in successive locations A[1], A[2], ..., A[10] of an array A.

    1. Write the array and its numbers as a complete binary tree as is done for heaps. Explain carefully why these numbers already form a heap.

    2. Carry out the first two steps of the heapsort algorithm on these numbers, that is, produce two numbers in the proper final locations and restore the heap property for the remaining numbers each time. Be careful to show all your work in a readable form.

  2. Using the same 10 numbers as in the previous problem, show how to insert the number 20 into this heap as a new entry. You must restore the heap property and show the resulting heap of 11 numbers.


Use of the Master Theorem to find the order of a recurrence solution (10 points).

The Master Theorem:

  1. In each case use the Master Theorem to find the Big-Theta order of the solution to the recurrence:

 


Find an exact solution to a recurrence (15 points).

Consider the recurrence equations:

  1. Use the Master Theorem to find the order of the solution.

  2. Knowing the order, you could eventually guess the solution -- it is: T(n) = 2*n2 - n. Prove using mathematical induction that this is the solution.


Question about material covered during recitations (10 points).

  1. One recitation problem was concerned with matching up n bolts of different sizes with n nuts. This problem turns out to have a solution very similar to another algorithm that we studied.

    1. What is the other algorithm?
    2. What is the Big-Theta average performance of these algorithms?
    3. The nut and bolt algorithm started out by choosing a nut or a bolt at random. What does this correspond to in the other algorithm that we studied?


Questions about Strassen's algorithm to multiply matrices (15 points).

  1. Using the "ordinary" method for multiplying 4-by-4 matrices, how many multiplications of numbers will be needed? Give a reason for your answer, more than just that your answer is given by a formula.

  2. Use Strassen's method for multiplying matrices to see how many multiplications of numbers would be required to multiply 4-by-4 matrices in this case. [Hint: you must not only use Strassen's basic method, but also the divide-and-conquer strategy.] Give a reason for your answer. For full credit you must explain in some detail how Strassen's basic result allows one to multiply the 4-by-4 matrices using the number of multiplications that you indicate.


Calculation Using Memoization (15 points).

  1. The Stirling numbers S(n, k) satisfy the following recurrence equations:

    Here is a function to calculate these numbers:

    Use the declaration int S[50][50]; and assume all the S values are initialized to -1. Add code to the function so that it will do a memoized computation.


Proof of correctness of an algorithm (15 points).

Consider the function below:

  1. Assume that m >= 0 initially. Use the method from class with its four separate steps to prove that this function will always return the value

    You should use

    as the loop invariant. (There will be little if any credit if you don't follow the 4-step method taught in class.)