Spring 2002: 15-211 Fundamental Structures of Computer Science Midterm Name: Andrew ID: Section or TA name: Instructions Fill out the Name/Andrew ID/Section box above now. This exam is open book, plus 1 sheet of notes allowed. Make sure your test form is complete: It is 9 pages long (8 pages plus one page of scratch paper). There are 8 questions, many of which include several parts. If you make a mess, clearly cross out invalid attempts, and circle the nal answer. In multiple choice questions, pick one answer. The point scores for the questions are shown in the table below. Organize your work accordingly. Advice: If you get stuck onaproblem,move on and go back to it later. Good luck! Question 1 2 3 4 5 6 7 8 Total Points 20 12 15 17 13 16 20 12 125 1
1. Asymptotic Analysis. For (a)-(d), circle the correct expression. For (e)-(h), circle true or false. (a) 24n +4n 2 +7is (log n) (n) (n log n) (n 2 ) (n 100 ) (2 n ) (b) 17 + 7n log n +30n is (c) n + 3n log n + p n is (log n) (n) (n log n) (n 2 ) (n 100 ) (2 n ) (log n) (n) (n log n) (n 2 ) (n 100 ) (2 n ) (d) n 100 +2 n + n log n is (log n) (n) (n log n) (n 2 ) (n 100 ) (2 n ) (e) log n = O(n 2 ): (f) log n =(n 2 ): (g) n=2 =(n): (h) 2 n=2 = (2 n ): True True True True False False False False 2. Algorithm analysis. Suppose an algorithm takes 0.5 milliseconds to run on an input of size 100. How long would it take on an input of size 1,000 (assuming that low-order terms are negligible) if the running time of the algorithm is: running time linear O(nlogn) quadratic (that is, O(n 2 )) cubic (that is, O(n 3 )) 2
3. Below isjava code for a binary search routine for nding the location of x in array a (copied directly from Figure 5.11 on page 168 of the textbook): public static int binarysearch (Comparable[] a, Comparable x) { int low = 0 int high = a.length - 1 int mid while (low <= high) { mid = (low+high) / 2 if (a[mid].compareto(x) < 0) low = mid + 1 else if (a[mid].compareto(x) > 0) high = mid - 1 else return mid return -1 // The return value if not found (a) The running time of this algorithm is O(log n). However suppose we replace the line, mid = (low+high)/2, with, mid=low. Does this algorithm still work, and if so, what is its running time in O notation? (b) Going back to the original code, what would be the consequence of changing the while-test to be low < high? Works ne. Might run forever. Might not nd x even though it is in a. (c) Going back to the original code, what would be the consequence of changing the statement low = mid + 1 to be low = mid? Works ne. Might run forever. Might not nd x even though it is in a. 3
4. Binary Search Trees. (a) Suppose we use the basic (that is, not AVL or other self-balancing) binary-search tree insertion algorithm. Write down an ordering of the numbers 1,2,3,4,5,6,7 so that if the numbers were inserted in that order, the result would be a completely balanced tree. (b) Consider the tree below. Draw the tree that results from rotating the edge 20-30: 10 5 20 15 30 25 40 35 50 (c) Write down the binary search tree whose PREORDER traversal is 4 2 1 3 5 6 4
5. Min-Heaps (a) Draw the heap that results from performing the remove-min operation on the following heap: 10 20 12 23 30 40 15 25 50 (b) Fill in the array corresponding to the answer you gave for part (a). (c) Suppose we put the root of the heap into arr[0] instead of arr[1]. What would the rules be for the locations of the left child, right child, and parent of the node in arr[i]? left child: right child: parent: (d) The number of elements we need to look at to nd the smallest element in a heap of size n is: O(1) O(log n) O( p n) O(n) (e) The number of elements we need to look at to nd the largest element in a heap of size n is: O(1) O(log n) O( p n) O(n) 5
6. (a) Fill in the following chart with the time it takes each of the following dictionary structures to do the operations: insert, lookup, deletemin, ndnext (ndnext means: given an entry in the data structure (or a reference to it), nd the next largest entry). For hashing, assume you have a good hash function. dictionary insert lookup deletemin ndnext sorted list O( ) O( ) O( ) O( ) hash table O( ) O( ) O( ) O( ) balanced tree O( ) O( ) O( ) O( ) (b) Explain briey why the following would not be a good hash function for hashing 10,000 5-character words into an array of size 10,000: for (i=0, value = 0 i < word.length i++) value += (int) word.charat(i) return value % tablesize 6
7. Each codefragmentbelow has a bug. For each one, either circle the oending statement, or insert extra code to x the bug, or briey explain the problem. (a) /** Compute base^n. */ public static long power (int base, int n) { if (n%2 == 0) // n is even return power (base*base, n/2) else // n is odd return power (base*base, (n-1)/2) * base (b) public int f(int n) { if (n >= 0) return n*(n+1) else return "Bad input to method f." (c)... // names is an array of strings for (int i=0 i<=names.length i++) { System.out.println(i + ": " + names[i]) System.out.println("All done printing") (d) Stack s Stack t s.push("hello") s.push("goodbye") s.pop() 7
8. Induction Consider the following (very slow) code for computing the nth Fibonacci number. (Recall that the Fibonacci numbers are given by the sequence 0, 1, 1, 2, 3, 5, 8,... In particular F 0 =0,F 1 = 1, and for i>1, F i = F i;1 + F i;2.) /** This code appears in Figure 7.6 on page 242 of the textbook. */ public static long fib (int n) { if (n <= 1) return n else return fib(n-1) + fib(n-2) Let C(n) be the number of calls to fib made during the evaluation of fib(n). For n 1, we have C(n) =F n+2 + F n;1 ; 1 where F n is the n th Fibonacci number. Using induction, prove that this equation is valid. answer. Be concise and precise in your 8
scratch paper 9