{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

Be such that for any odd natural number n f n n 2 on

Info iconThis preview shows pages 4–6. Sign up to view the full content.

View Full Document Right Arrow Icon
) be such that for any odd natural number n , 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 ) c n 2 . 4
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
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 = c n 2 . Otherwise f ( n ) = n/ 2 n 2 = c n 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 ) > c n . Let c, N > 0 arbitrary. Note that if n > c then n 2 > c n . 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 Big-Oh 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
Background image of page 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.
Background image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}