hw2solutions

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

• Notes
• 6

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

) 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

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

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
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.
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern