{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw2solutions

B prove that d n is o c n answer since af n and bg n

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: B. Prove that D ( n ) is O ( C ( n )). Answer Since af ( n ) and bg ( n ) are both positive we have max( af ( n ) ,bg ( n )) ≤ af ( n ) + bg ( n ) for all nonnegative n . Therefore we have have shown that there exist c = 21 and N = 1 (to ensure that N > 0) such that ∀ n ≥ N D ( n ) ≤ cC ( n ). C. Prove that C ( n ) is Θ( D ( n )). Answer Since we have just shown that C ( n ) and D ( n ) are Big-Oh of each other it follows that C ( n ) is Θ( D ( n )). Problem 5: 20 points Analyze the following fragment of code and give a Big-Oh characterization of its running time (that is, give the best upper bound on the worst-case running time of the algorithm that you can find). Explain your analysis. public static void doWork (int arr) { for (int i=1; i < arr.length; i++) { for (int j=0; j < i; j++) { System.out.print(j); } } } Answer Let n = arr . length be the size of the input. The body of the inner loop runs in time O (1). For each i = 1 ,...,n- 1 the inner loop executes i times therefore it runs in time O ( i · 1), that is O ( i ). But i < n so the each of the executions runs in time O ( n ). Therefore the body of the outer loop runs in time O ( n ). The outer loop executes n- 1 times so it runs in time O (( n- 1) n ), that is, O ( n 2 ). The whole method runs in time O ( n 2 ). Note. An alternative more detailed analysis computes the sum n- 1 X i =1 i = ( n- 1)( n- 1 + 1) 2 = n 2- n 2 but still, the (asymptotically) best we can state is that the method runs in time O ( n 2 ). 3 Problem 6: 20 points Write a Java method that takes an argument an array a of type int and returns true if the elements of a are in strictly increasing order and false otherwise. Then, analyze your method and give a Big-Oh characterization of its running time (that is, give the best upper bound you can find on the worst-case running time of your method). Explain your analysis. Answer One method that accomplishes this task goes as follows: public static bool sorted(int arr) { if (arr.length<2) return true; for (int i=0; i<arr.length-1; i++) { if (arr[i]>=arr[i+1]) { return false; } } return true; } Let n = arr . length be the size of the input. The worst case running time is when the array is sorted in strictly increasing order. In this case the for the loop iterates n- 1 times. The body of the loop runs in time O (1). Thus, we can conclude that the method runs in time O ( n ). Extra Credit 1: 15 points In this problem you are NOT allowed to use the theorems about Big-Oh stated in the lecture notes. Your proof should follow just from the definition of Big-Oh. (As usual in problems with Big-Oh, we assume that the functions map nonnegative reals to strictly positive reals.) Let f ( n ) be such that for any even n , f ( n ) = n 2 and for any odd n , f ( n ) = n 2 . TYPO We should not have formulated the problem like this because it does not give any information about f ( n ) when n is not a natural number! And it is quite possible that on arguments that are not natural numbers f would behave asymptotically worse than n 2 ! We reformulate the problem as follows: Let f ( n ) be such that for any odd natural number n , f...
View Full Document

{[ snackBarMessage ]}

Page3 / 6

B Prove that D n is O C n Answer Since af n and bg n are...

This preview shows document pages 3 - 5. Sign up to view the full document.

View Full Document Right Arrow Icon bookmark
Ask a homework question - tutors are online