hw3_solutions_final_withrubric

Such functions do not arise naturally in algorithmic

Info iconThis preview shows pages 7–8. 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: 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 = 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 ). The worst-case running time of the program is therefore Θ(log log n ). 5 points for correctness of code given (working code that achieves the given runtime bound) 10 points for analysis and correct conclusions 8...
View Full Document

{[ snackBarMessage ]}

Page7 / 8

Such functions do not arise naturally in algorithmic...

This preview shows document pages 7 - 8. Sign up to view the full document.

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