This preview has intentionally blurred sections. Sign up to view the full version.
View Full 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 iloop inside the ifstatement 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 iloop iterates exactly 10 12 times so it takes more than 10 12 steps. Nonetheless, the iloop runs in O (1). Therefore the ifstatement also runs in O (1). The body of the jloop runs in O (1). Now we have to see how many iterations does the jloop 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 jloop is given by the smallest k such that 2 2 k ≥ n , i.e., k = d log log n e . Therefore the jloop runs in time O (log log n ), and in fact, since the jloop actually takes d log log n e iterations the running time is Θ(log log n ). The worstcase 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
 Spring '09
 TANNEN
 Algorithms, Data Structures, Recursion, Sort, Big O notation, Negative and nonnegative numbers, running time, Recurrence relation

Click to edit the document details