hw3_solutions_final_withrubric

F n is not ω g n because for any n c 0 we can find n

Info icon This preview shows pages 4–7. Sign up to view the full content.

View Full Document Right Arrow Icon
f ( n ) is not Ω( g ( n )) because for any N 0 , c 0 > 0 we can find n 0 N 0 that is an even natural number such that 2 n 0 < 4 n 0 . For example, take n 0 to be any even natural number bigger than N 0 . Reasonable functions (neither are big O of the other, proven correctly), but they hit zero - 10/20 Reasonable functions (neither are big O of the other, proven correctly), but they go negative - 5/20pts One function is big O of the other, but right idea - 5/20pts Functions are incorrect - 0/20pts -8pts for no big O proof -5pts for incorrect big O proof Problem 5: 20 points Consider the following program: public static int iHeart121 (int[] a) { int n = a.length; for (int i = 0; i < n; i++ ) { for (j = 0; j < n; j++ ) 4
Image of page 4

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

View Full Document Right Arrow Icon
System.out.println(n); } String foo = "bar"; Char[] charFoo = foo.tocharArray(); for(char f: charFoo) System.out.println(f); return aux(n); } private static int aux(int m) { if (m < 2) return 0; int j = 0; for(int i = 0; i<m; i++) { j++; System.out.println("Ben heart 121"); } return aux(m/2) * aux(m/2); } Analyze this code and give and give a Big-Oh characterization of the running time of the method 1 iHeart121 . Explain your analysis. Answer (and grading rubric!) (2 pts – can arrive at this as you wish): Runs in O ( n 2 ) time Outer loop runs n times, which make n calls to println , a constant time function. n * n = n 2 = O ( n 2 ) for (int i = 0; i < n; i++ ) { for (j = 0; j < n; j++ ) System.out.println(n); } (2 pts) Runs in O(1) time Recognize that for loop goes through ”bar” once, but this is independent of n... String foo = "bar"; Char[] charFoo = foo.tocharArray(); for(char f: charFoo) System.out.println(f); return aux(n); Call to auxiliary function, which runs in O ( n * log ( n )) 3 pts for correctly adjudging that aux T ( n ) = 2 * T ( n/ 2) + n 5
Image of page 5
Two calls to half its value with a constant-time operation w/ each call 8 pts for correctly deriving recurrence relation to be O ( n * log ( n )) T ( m ) = 2 T ( m/ 2) + m T ( m/ 2) = 2 T ( m/ 4) + m/ 2 ... ...... T (2) = 2 T (1) + 2 Multiplying the i th equation by 2 i , you get: T ( m ) = 2 T ( m/ 2) + m 2 * T ( m/ 2) = (2 T ( m/ 4) + m/ 2) * 2 ... ...... 2 log ( m ) * T (2) = (2 T (1) + m/ 2 log ( m ) ) * 2 log ( m ) Summing both sides up and canceling, you get: T ( m ) = m + m + m + .... + m for a total of log ( m ) times since there are log ( m ) equations Therefore, you end up with T ( m ) = m * log ( m ) 2 pts if you show that you understand the concept of ”adding” up these parts: O (1) + O ( n 2 ) + O (1) + O ( n * log ( n )) 3 pts if you get = O ( n 2 ) Extra Credit 1: 15 points Consider a function T ( n ) that maps nonnegative real numbers to positive real numbers and satisfies the recurrence relation T ( n ) = T ( n ) + 1 for all n 2 and T (2) = 1 Find an elementary function f ( n ) such that T ( n ) is Θ( f ( n )). (For our purposes, an “elementary” function is a function of one real variable built from a finite number of exponentials, logarithms, constants, and roots through composition and combinations using the four elementary operations addition, multiplication, subtraction, division.) Answer As we have already done with such recurrences, we will assume that n is of a special form, n = 2 2 k . (Below we justify this and other such assumptions and discuss a bit when we can make them.) We use this particular form because for such n we have n = 2 2 k - 1 . This allows us to consider another function, S ( k ) = T (2 2 k
Image of page 6

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

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

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern