This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: Illinois Institute of Technology Department of Computer Science Solutions to First Examination CS 430 Introduction to Algorithms Spring, 2009 11:25am12:40pm, Wednesday, February 23, 2009 104 Stuart Building 1. Randomized Min Consider the following algorithm to find the minimum element in an unordered array of n elements: 1: RandomMin ( A [1 ..n ]) 2: m 3: for i 1 to n in random order do 4: if A [ i ] &lt; m then 5: m A [ i ] 6: end if 7: end for 8: return m Notice that the for loop (line 3) takes the numbers 1 , 2 , . . . , n in random order. (a) In the worst case, how many times does RandomMin execute line 5? (b) What is the probability that n th (last) iteration executes line 5? (c) Analyze the expected number of executions of line 5. Solution. (a) Every time; that is, n times. (b) 1 /n because each of the n indices is equally likely to be the last in the random order, including the index at which the minimum element occurs. (c) From the previous part, the expected number of executions is given by e n = e n 1 +1 /n , e 1 = 1. Thus e n is the n th harmonic number H n = 1 + 1 / 2 + + 1 /n = ln n + o (1). 2. Unusual Sorting Algorithm A TA in CS 430 suggested the following unusual algorithm to sort n elements in an ar ray: 1: TaSort ( A [0 ..n 1]) 2: if n = 2 and A [0] &gt; A [1] then 3: swap A [0] A [1] 4: else 2 5: if n &gt; 2 then 6: m d 2 n/ 3 e 7: TaSort ( A [0 ..m 1]) 8: TaSort ( A [ n m..n 1]) 9: TaSort ( A [0 ..m 1]) 10: end if 11: end if (a) Prove that TaSort correctly sorts its input. (b) When the TA first proposed the algorithm, she mistyped line 6 using the floor instead of the ceiling operation. The algorithm failed to sort correctly; explain why. ( Hint : Consider n = 4.) (c) State (using precise floor/ceiling operations) the recurrence relation and initial values describing the number of comparisons A [0] &gt; A [1] performed in line 2 of the algorithm in the worst case. Then, ignoring the floor/ceiling operations, solve the recurrence.in the worst case....
View
Full
Document
This note was uploaded on 04/07/2009 for the course CS 430 taught by Professor Kapoor during the Spring '08 term at Illinois Tech.
 Spring '08
 KAPOOR
 Algorithms

Click to edit the document details