This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: f ( n ) = n 2 , on all other real nonnegative arguments n we have f ( n ) = n 2 . Note Most solutions assumed that f is only defined on natural numbers and we accepted such solutions, of course. A. Prove that f ( n ) is O ( n 2 ). Answer Let N = 0 . 5 and c = 1. We will show that ∀ n ≥ N we have f ( n ) ≤ cn 2 . 4 Indeed, fix an arbitrary nonnegative real number n that is bigger than N . Either n is an odd natural number and then f ( n ) = n 2 ≤ n 2 = cn 2 . Otherwise f ( n ) = n/ 2 ≤ n 2 = cn 2 because n ≥ N = 1 / 2. Note Some of the submitted solutions proved first that n/ 2 is O ( n 2 ) and then that n 2 is O ( n 2 ) and then jumped to the conclusion that f ( n ) is O ( n 2 ). This is a big jump that needs proof! Some people stated that they used the same N,c in both proofs of n/ 2 is O ( n 2 ) and n 2 is O ( n 2 ). That’s a key observation in bridging the jump we are talking about so we have some partial credit for that. But it’s still not a complete proof of the required statement. Just for fun try to prove the following. Let f,g,h be three functions such that f is O ( h ) and g is O ( h ). Let A be an infinite set of nonnegative real numbers and define a function k such that k ( n ) = f ( n ) when n ∈ A and k ( n ) = g ( n ) otherwise. Prove that k is O ( h ). B. Prove that f ( n ) is not O ( n ) Answer We must show that, for any c,N > 0, there exists an n ≥ N such that f ( n ) > cn . Let c,N > 0 arbitrary. Note that if n > c then n 2 > cn . So it suffices to use an odd n , so that f ( n ) = n 2 such that n > c and n ≥ N . Such n ’s exist, for example n = 2 d max( c,N ) e + 1 Extra Credit 2: 15 points Analyze the following code fragment and give a BigOh characterization of the running time of doMoreWork . public static void doMoreWork (int barr) { for (int i=1; i < barr.length; i *= 2) { for (int j=0; j < i; j++) { barr[j]=j; } foo(barr); } } public static void foo(int a){ for (int x : a) System.out.print("foo!"); } } Answer 5 Let us first analyze foo . Let m = a . length be the size of its input. The for loop always iterates m times. The body of the loop takes constant time. Hence foo runs in time O ( m ). Now let n = barr . length () be the size of the input of doMoreWork . For simplicity we assume that n is a power of 2 (just like we did in the lecture notes when we analyzed the running time of binary search). At every iteration of the outer loop in doMoreWork i doubles so after k iterations i is 2 k . The iterations stop when 2 k = n so the outer loop iterates log n times. Now we analyze the running time of the body of the outer loop. It has two components: the inner loop and the statement foo(barr); . We have analyzed the latter and it runs in time O ( n ). The body of the inner loop runs in time O (1) and for each i the inner loop iterates i times. So the inner loop runs in time O ( i ). But i < n so it runs in time O ( n ). From both components the body of the outer loop runs in time O ( n + n ) which is O ( n ). We conclude that the method doMoreWork runs in time O ( n log n ) 6...
View
Full Document
 Spring '09
 TANNEN
 Algorithms, Data Structures, Negative and nonnegative numbers, 2k, doMoreWork

Click to edit the document details