You are on page 1of 6

CSCE 156

Homework 05
Due 2:00PM, Thursday, April 12st, 2012

Spring 2012

Name(s)

CSE Login

Instructions Follow instructions carefully, failure to do so may result in points being deducted. You may work in pairs if you wish. Type your answers and hand them in hardcopy with this as your coversheet (unless otherwise specied). If you wish, you may hand-write exercises that require math.

Question 1 2 3 4 5 6 7 8 9 10 11 12 13 Total:

Points 4 4 4 4 4 4 24 8 16 8 5 15 0 100

Score

Algorithm Analysis
1. 4 points The variance of n numbers, a1 , . . . , an is a measure of how spread out the numbers are. A simplied dention is: 1 = n
2 n

(ai )2
i=1

CSCE 156 Homework 05 Spring 2012 where is the mean (average) of the numbers a1 , . . . , an . Write pseudocode to compute the variance of a collection of numbers. Analyze it by: identifying the input/input size, identifying the elementary operation and how often it gets it gets executed with respect to the input size and provide an asymptotic characterization. 2. 4 points Consider the following problem: Given a set of n points in the plane, p1 = (x1 , y1 ), p2 = (x2 , y2 ), . . . , pn = (xn , yn ), determine which pair pi , pj are closest. The distance between any two points, pa = (xa , ya ), pb = (xb , yb ) in the plane is (xa xb )2 + (ya yb )2 Write pseudocode to determine the two closest points among a collection of n points. Analyze it by: identifying the input/input size, identifying the elementary operation and how often it gets it gets executed with respect to the input size and provide an asymptotic characterization. 3. 4 points (Weiss 5.3) Suppose T1 (n) = O(f (n)) and T2 (n) = O(f (n)). Mark each as true or false. If false, provide a T1 , T2 for which it is false. (a) T1 (n) + T2 (n) = O(f (n)) (b) T1 (n) T2 (n) = O(f (n)) (c) T1 (n)/T2 (n) = O(1) (d) T1 (n) = O(T2 (n)) Hint: For the next few questions, consider dening a time function with respect to n. That is, if the algorithm is linear, we could write the time that it takes as a function of n: t = f (n) = cn If it is quadratic, we could write it as a quadratic function: t = f (n) = cn2 Both of these instances ignore lower order terms. If we know the time it takes for a particular value of n then we can compute the constant c and consequently predict the time t it takes for other values of n or vice versa. 4. 4 points (Weiss 5.14) An algorithm takes 0.5 ms for an input size 100. How long will it take for input size 500 (assuming that low-order terms are negligible) if the running time is (a) linear (b) O(n log n) (c) quadratic (d) cubic

Page 2 of 6

CSCE 156 Homework 05 Spring 2012 5. 4 points (Weiss 5.15) An algorithm takes 0.5 ms for input size 100. How large can an input size be if a problem can be solved in 1 minute (assuming that low-order terms are negligible) if the running time is: (a) linear (b) O(n log n) (c) quadratic (d) cubic 6. 4 points (Weiss 5.16) For 1000 items, our algorithm takes 10 seconds to run on machine A, but now you replace the machine with machine B that is twice as fast. Approximately how long will the algorithm take to run on machine B for 2000 items if the algorithm is: (a) linear (b) O(n log n) (c) quadratic (d) cubic 7. 24 points Prove each of the following statements by applying the denition of Big-O. That is, derive an inequality (show your work) and identify a c, n0 as per the denition of Big-O. (a) 3n = O(n) (b) 500 n = O(n) (c) n2 + 2n + 1 = O(n2 ) (d) 2048n + 1234 = O(n2 ) (e) n log (32n) = O(n log (n))) (f) 12n3 + 50n2 12n 60 = O(n3 ) (g) n2n = O(3n ) (h) log (n!) = O(n log n) 8. 8 points (Weiss 5.19) Order the following functions by growth rate: n, n, n1.5 , n2 , n log n, n log (log (n)), n log2 (n), n log (n2 ), 2n , 37, n3 , n2 log n. 9. 16 points (Weiss 5.20 modied) For each of the following program fragments do the following: 1. Give a Big-O analysis of the running time 2. Implement the code (assuming that prior to each loop, sum is zero) and run for the following values of n: 10, 100, 1000, 100,000; what is the value of sum at the end of each code fragment (hint: use a long data type)? Note: for some values the code may take too long to execute; for these, indicate how long you let it run before you gave up.

Page 3 of 6

CSCE 156
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 // Fragment 1 for ( int i =0; i < n ; i ++) sum ++; // Fragment 2 for ( int i =0; i < n ; i +=2) sum ++; // Fragment 3 for ( int i =0; i < n ; i ++) for ( int j =0; j < n ; j ++) sum ++; // Fragment 4 for ( int i =0; i < n ; i ++) sum ++; for ( int j =0; j < n ; j ++) sum ++; // Fragment 5 for ( int i =0; i < n ; i ++) for ( int j =0; j < n * n ; j ++) sum ++; // Fragment 6 for ( int i =0; i < n ; i ++) for ( int j =0; j < i ; j ++) sum ++; // Fragment 7 for ( int i =0; i < n ; i ++) for ( int j =0; j < n * n ; j ++) for ( int k =0; k < j ; k ++) sum ++; // Fragment 8 for ( int i =1; i < n ; i = i *2) sum ++;

Homework 05

Spring 2012

Recursion
10. 8 points (Weiss 7.17) Give a Big-O characterization for the following recurrences (with initial conditions T (0) = T (1) = 1. (a) T (n) = T (n/2) + 1 (b) T (n) = T (n/2) + n (c) T (n) = T (n/2) + n2 (d) T (n) = 3T (n/2) + n (e) T (n) = 3T (n/2) + n2 (f) T (n) = 4T (n/2) + n (g) T (n) = 4T (n/2) + n2 (h) T (n) = 4T (n/2) + n3

Page 4 of 6

CSCE 156 Homework 05 11. 5 points (Weiss 7.38 modied) The binomial coecients sively as follows 1 1 C(n, k) = C(n 1, k) + C(n 1, k 1)

Spring 2012 C(n, k) can be dened recurif k = 0 if n = k for 0 < k < n

Write the following recursive static Java method: public static long binomial(long n, long k) (hand in your code hard-copy, typeset it using a monotype font such as Courier) and use it to compute the following values: (a) C(10, 5) (b) C(20, 10) (c) C(35, 11) (d) C(35, 15)

Sorting
12. 15 points For the programming portion of this assignment, you will implement the following Java class/method:
1 2 3 4 5 6 7 8 9 10 package unl . cse . sorting ; import java . util . Comparator ; public class Sorting { public static <T > void sort ( T array [] , Comparator <T > comparator ) { // TODO : implement any sorting algorithm } }

Note: do not change the package or the methods signature. You must implement your method utilizing the Comparator<T> interface. Handin your single Java le (Sorting.java) using the webhandin. You may implement this method using any sorting algorithm that you wish. However, you may not use the standard Java Collections (or Arrays) sort methods. We will test your method by sorting several randomly generated arrays. In addition, we will be empirically measuring your methods execution time on various input sizes and various types. The students with the 10 best running times will receive bonus points proportional to their placement (place 1, 2: 5pts, place 3, 4: 4pts, etc.). 13. (Bonus 5pts each) (a) The binary insertion sort algorithm works like insertion sort, but rather than iteratively comparing and swapping until the element has been inserted where it

Page 5 of 6

CSCE 156 Homework 05 Spring 2012 should be, it uses binary search to nd where the element should go in the alreadysorted list. Write pseudocode for this algorithm and analyze it with respect to the number of comparisons (assume a random access array) and provide an asymptotic characterization. (b) Consider the following sorting algorithm: We apply the mergesort algorithm, but change the base case condition; instead of returning if the sub-array is 1 element or less, we switch to selection sort when the size of the sub-array is of size 20 or less. Write pseudocode for this algorithm and express its complexity by providing a recurrence relation (along with base case) that counts the number of comparisons (in the worst case). Use the Master Theorem to provide a Big-O characterization.

Page 6 of 6

You might also like