{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

C in a java array implementation of queues the method

Info iconThis preview shows pages 2–5. Sign up to view the full content.

View Full Document Right Arrow Icon
(c) In a Java array implementation of queues, the method enqueue has worst-case 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 Big-Oh stated in the lecture slides, the textbook, or the lab writeups. Your proof should rely only on the definition of Big-Oh. 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
Background image of page 2

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
static int Frodo(int n) { if (n==0) return 1; else { int y = Frodo(n-1); 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 0 be that input. What is F ( n 0 ) ? (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 worst-case running time of bar(b) as a function of n = b.length and give a Big-Oh bound. 5. (20 pts) A binary min-heap 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 pre-order traversal of the tree. 6. (20 pts) Without altering the output, modify the following code to reduce the asymptotic upper bound on the worst-case 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
Background image of page 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 Big-Oh and, of course, on the formula for the sum of a geometric progression that the review sheet told you to memorize. 3. Sort the following running times so that f ( n ) is left from g ( n ) if f ( n ) is O ( g ( n )), indicating whether any two are Θ to each other. No proof required. n n, n log n, 1 . 5 n , n 2 , 2 n 2 , log( n 2 ) n 4. Given that f 1 ( n ) is O ( f 2 ( n )), f 2 ( n ) is O ( f 3 ( n )), g 1 ( n ) is O ( g 2 ( n )), and g 2 ( n ) is O ( g 3 ( n )), state whether the following statements are true or false. If true, no proof is required. If false, give a counterexample.
Background image of page 4

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
Image of page 5
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}