This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: (a) There is no algorithm that sorts an array of n elements and whose running time is O (7 n log n ), true or false? (b) Let A be an algorithm that, for each n > 0, takes less than 1000 steps for all inputs of size n except for one of these inputs, on which it takes exactly n steps. Then, the worstcase running time of A is Θ( n ), true or false? (c) In a Java array implementation of queues, the method enqueue has worstcase running time that is O (1), true or false? (d) There is no complete binary tree with 17 nodes, true or false? 2. (20 points) In this problem you NOT allowed to use any of the theorems about BigOh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of BigOh. Prove that if f ( n ) is Ω( n ) then 2 f ( n ) + n is O ( f ( n ). 3. (20pts) (This is an easy problem. You may possibly expect something harder like Problem 5 in Homework 3.) Consider the following recursive method: 2 static int Frodo(int n) { if (n==0) return 1; else { int y = Frodo(n1); return n * y + n; } } Let F ( n ) denote the running time taken by the above method on the input n . (a) Analyze this code and formulate a recurrence relation for F ( n ). (b) For what input n does Frodo ( n ) simply terminate, without making a recursive call? Let n be that input. What is F ( n ) ? (c) Solve the above recurrence relation. 4. (20 points) static int foo(char a) { for (int j = 2; j < a.length; j = j*j ) a[j] = ’z’; return (a.length * a.length * a.length); } static void bar(char b) { for (int i = 1; i < foo(b); i=2*i) b[i]=’z’; } Analyze the worstcase running time of bar(b) as a function of n = b.length and give a BigOh bound. 5. (20 pts) A binary minheap is storing 15 items in the array implementation starting with the root being at index 1. Write the sequence of array indices visited in a preorder traversal of the tree. 6. (20 pts) Without altering the output, modify the following code to reduce the asymptotic upper bound on the worstcase running time, and provide an analysis of the improvement. You should include the original running time as well as the improved running time. public static int doSomething (int inArr, int foo) { int outArr = new int[inArr.length]; for (int i = 0; i < inArr.length; i++) { int bar = 0; for(int j = 0; j <= i; j++) { if (inArr[j] % foo == 0) { bar += inArr[j]; } } 3 outArr[i] = bar; } return outArr; } 4 Additional Problems 1. The order in which nodes are visited in the preorder traversal of a binary tree is exactly the reverse of the order in which nodes are visited in the postorder traversal, true or false? 2. (20 points) Give a function f ( n ) such that ( * ) f ( n ) X i =0 2 i is not O (2 n ) After you give f ( n ) prove ( * ) without using any of the theorems stated in the lecture notes, the textbook, or the lab notes. Your proof should rely only on the definition of BigOh and, of course, on the formula for the sum of a geometric progression that the review sheet told you to memorize.on the formula for the sum of a geometric progression that the review sheet told you to memorize....
View
Full Document
 Spring '09
 TANNEN
 Algorithms, Data Structures, Big O notation, Analysis of algorithms, Binary heap, Tree traversal, static int foo

Click to edit the document details