Question 1. (10 points) for (i = 1; i <= n; i++) { j = 1; while (j < n) { Algorithms Test 1 for (k = 1; k <= j; k++) { something that takes O(1) } // end for k j = j * 2 } // end while j } // end for i a) Analyze the above algorithm to determine an obvious big-oh notation, O( ). b) Analyze (by tracing) the above algorithm to determine its theta notation, ( ). Question 2. (10 points) Suppose that you have an (n 5 ) algorithm that required 10 seconds to run on a problem size of 1,000. How long would you expect the algorithm to run on a problem size of 10,000? 1
Question 3. (10 points)the general idea of 4-way merge sort is as follows. Assume n items to sort. Divide the unsorted part into quarters to get four smaller sorting problems of about equal size = n/4 Conquer/Solve the smaller problems recursively using 4-way merge sort Merge the solution to the smaller problems together using two levels of merging Unsorted size n Divide Conquer recursively Merge quarters pairs into halves Sorted size n/2 Sorted size n/2 Sorted size n Merge halves to get whole sorted array. Note: Assume that the same merge code is used as in the 2-way merger sort algorithm discussed in class. a) In the worst-case, how many total comparisons are performed to merge the quarters into two halves followed by merging the two halves into the whole sorted array? b) Write a recurrence relation for the 4-way merge sort's worst-case number of compares, including base-case(s). W(n) = W(1) = 2
Question 4. (15 points) The "Master Theorem" (Theorem B.5) for solving most common recurrences is as follows: Suppose a complexity function T(n) is eventually nondecreasing and satisfies the recurrence T(n) = at(n/b) + cn k for n > 1, n a power of b T(1) = d, where constant a > 0, b is an integer 2, c > 0, d 0, and k is an integer 0. Then T(n) { θ ( n k ) θ ( n k log n) θ ( n log b a ) if a < b k if a = b k if a > b k Use the Master Theorem to solve both of the following recurrence relations: a) T(n) = 4T( n ) 2 + 3n 3 T(1) = 2 b) T(n) = 5T( n ) 4 + 4n T(1) = 3 Question 5. (15 points) The general idea of Quick sort is as follows. Assume n items to sort. Select a random item (e.g., the first item) in the unsorted part as the pivot item Rearrange (called partitioning) the unsorted items such that: low Pivot Point high Pivot All items < Pivot Item All items >= Pivot Quick sort the unsorted part to the left of the pivot item Quick sort the unsorted part to the right of the pivot item Explain why Quick sort is ( n 2 ) in the worst-case. 3
Question 6. (15 points) Theorem B.1 Let the homogeneous linear recurrence equation with constant coefficients a 0 t n + a 1 t n 1 + a 2 t n 2 +... +a k t n k = 0 be given. If the characteristic equation a 0 r k + a 1 r k 1 + a 2 r k 2 +... +a k r 0 = 0 has k distinct solutions r 1, r 2,..., r k, then the only solutions to the recurrence are t n = c 1 r 1 n + c 2 r 2 n + c 3 r 3 n +... +c k r k n where the c i terms are arbitrary constants. (The value of these c i can be determined by the initial/base-case conditions, but we generally can stop here to find the theta-notation.) Theorem B.2 Let r be a root of multiplicity m of the characteristic equation for a homogeneous linear recurrence equation with constant coefficients. Then, the general solution includes the terms t n =... + c 1 r n + c 2 nr n + c 3 n 2 r n +... +c m 1 n m 1 r n +... where the c i terms are arbitrary constants. Theorem B.3 A nonhomogeneous linear recurrence equation of the form a 0 t n + a 1 t n 1 + a 2 t n 2 +... +a k t n k = b n p(n) can be transformed into a homogeneous linear recurrence that has the characteristic equation (a 0 r k + a 1 r k 1 + a 2 r k 2 +... + a k r 0 )(r b) d+1 = 0, where b is a constant and p(n) is a polynomial of degree d. If there is more than one term like b n p(n) on the right-hand side, each one contributes a (r b) d+1 term to the characteristic equation. a) For the recurrence: t n = 5 t n-1-6 t n-2 + n 2 4 n for n > 0 t 0 = 4 Obtain the characteristic equation Factor the characteristic equation to find the roots Write the general solution to the characteristic equation and determine its theta notation. b) For the recurrence: T(n) = 4T( n ) 2 + 3n 3 T(1) = 2 for n > 2 with n a power of 2 (n = 2 k and log 2 n = k) Obtain the characteristic equation Factor the characteristic equation to find the roots Write the general solution to the characteristic equation and determine its theta notation. 4
For the last question, you can choose which of the problems to solve (do only one)! Question 7. (25 points) Let A be an array indexed from 0 to n-1. Also, let A contains n different integer values that are sorted in ascending order (from smallest to largest). Give a divide-and-conquer algorithm that finds an index i such that A[i] = i (if one exists) and runs in O(log 2 n) time. If such an index does not exist, then it should return -1. Question 7. (25 points) The longest ascending subsequence problem is defined as follows. Given an array A of n integers, find the length of the longest ascending subsequence in A. For example, n = 8 A: 0 1 2 3 4 5 6 7 5 8 2 3 9 8 5 7 The longest ascending subsequence is of length 3 from A[2] to A[4]. Write a dynamic programming algorithm to solve this problem. Hint: Construct an n element array B such that B[i] contains the length of the longest ascending subsequence that starts at A[i]. Fill B in one pass by starting at one end and filling to the other. You may describe your algorithm in English or high-level language (C++, Java, Python, Ada, etc.) pseudo-code. 5