hw3_solutions_final_withrubric

# And use the recurrence relation for t to state an

• Notes
• 8

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

) and use the recurrence relation for T to state an easier to solve recurrence relation for S : S ( k ) = S ( k - 1) + 1 for all k 1 and S (0) = 1 (we are only using the recurrence relation for T for n 4) This is easy to solve, with the same technique we have been using several times. Add up the sides of 6

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

the following equalities: S ( k ) = S ( k - 1) + 1 · · · = · · · S (1) = S (0) + 1 After crossing out the terms that appear on both sides we get S ( k ) = S (0)+ k therefore S ( k ) = k +1. So now T (2 2 k ) = S ( k ) = k + 1 therefore, for n a double power of 2 we have T ( n ) = log log n + 1. From this we can conclude that T ( n ) is Θ(log log n ). Why can we conclude this? Well, for any positive real number n 2 let k = b log log n c , then 2 2 k n < 2 2 k +1 If T is a monotonically increasing (not necessarily strictly so) function then S ( k ) = T (2 2 k ) T ( n ) T (2 2 k +1 ) S ( k + 1), thus k + 1 T ( n ) k + 2 therefore b log log n c + 1 T ( n ) ≤ b log log n c + 2 hence T ( n ) is Θ(log log n ). If T is not monotonically increasing then some other reasonable conditions can be put on T to get this behavior, like a bound on how much the function varies between certain bounds. But, warning , there are pathological functions that, for example, oscilate wildly on small intervals for which this kind of reasoning will be unjustified. Such functions do not arise naturally in algorithmic complexity analysis so in this class we can safely ignore them and say things like “suppose n is of the form . . . ”. 5 pts for the 2 2 k substitution 5 pts for solving the recurrence relation 5 pts for arriving at Θ(log log n ) Extra Credit 2: 15 points Without using the Java math library to compute logarithms give a Java method that runs in worst- case time that is Θ(log log n ) while taking more than 10 12 steps for inputs of size 5. You should give your program and analyze it to show the running time. Answer Here is the program: public static void logLog(char[] a) { long n = a.length; if ( n==5 ) { for (long i = 1; i <= 1000000000000000L; i++ ) a[0] = z; return; 7
} for (long j = 2; j < n; j = j*j ) a[0] = z; } And here is the analysis. The i-loop inside the if-statement executes exactly when the input has size 5. Its body takes at least one step (actually it takes three steps) and runs in O (1). Moreover this i-loop iterates exactly 10 12 times so it takes more than 10 12 steps. Nonetheless, the i-loop runs in O (1). Therefore the if-statement also runs in O (1). The body of the j-loop runs in O (1). Now we have to see how many iterations does the j-loop perform. Let V ( k ) be the value of j after k iterations. We have V (0) = 2 and V ( k + 1) = V ( k ) 2 . Because 2 2 0 = 2 1 = 2 and (2 2 k ) 2 = 2 (2 k ) · 2) = 2 2 k +1 it follows that V ( k ) = 2 2 k (I just used induction here, surreptitiously :). So the number k of iterations of the j-loop is given by the smallest k such that 2 2 k n , i.e., k = d log log n e . Therefore the j-loop runs in time O (log log n ), and in fact, since the j-loop actually takes d log log n e iterations the running time is Θ(log log n ).
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