UML CS 91.404 Practice Problems and Solutions 1. (20 points) What can you conclude? Given: 1) 1 log4 n f1 (n) 16 3) f 3 (n) O(3 lg lg n 7) 2) 4) 1 f 2 ( n ) 3 n f 4 (n) (3 lg lg n 7) a) (10 points) Can we conclude from statements (1)-(4) that f 2 (n) O( f 3 (n)) ? Why or why not? Either prove or provide a counterexample. SOLUTION: The statement is FALSE. f4(n) 3 lg lg n 7 f3(n) f1(n) f2(n) Counterexample: f 2 ( n) n f 3 (n) lg lg n 1 of 6 1 16 1 n3 log4 n UML CS 91.404 b) (10 points) Can we conclude from statements (1)-(4) that f 4 (n) ( f1 (n)) ? Why or why not? Either prove or provide a counterexample. SOLUTION: The statement is TRUE. Proof: First, observe that: 1 16 log4 n 1 2 4 log4 n 1 4 2 log4 n 1 4 log4 n 2 1 n2 Now, f 4 (n) (3 lg lg n 7) and 1 3 lg lg n 7 16 1 transitivity of yields: f 4 (n) 16 log4 n log4 n Applying the definition of and using transpose symmetry yields: log4 n 1 log4 n 1 log4 n f1 (n) O 1 f1 (n) ( f1 (n)) 16 16 16 Applying transitivity of again yields: f 4 (n) ( f1 (n)) 2 of 6 UML CS 91.404 2. (20 points) Recurrence In this problem, you will find a tight upper bound on the closed-form solution for the following recurrence: n 5T log 7 n n 1 T ( n) 7 1 n 1 That is, find the smallest function f (n) that you can, such that T (n) O( f (n)) . You may assume that n is a power of 7. SOLUTION: The Master Theorem is applicable because the ratio test f (n) log 7 n in which there is polynomial dominance in the n logb a n log7 5 denominator. That is, log 7 n O(n log7 5 ) for some 0<. Case 1 applies, so yields: the solution is T (n) (n log 5 ) . 7 We can use a recursion tree to obtain a looser upper bound. At each level of the tree each problem gives rise to 5 subproblems. The height of the recursion tree is: log 7 n. The summation corresponding to the work is given by: i n 5 log 7 i 7 i 0 log7 n 7 7 7 7 i i i i i 5 log 7 n log 7 7 5 log 7 n i 5 log 7 n log 7 n 5 i 0 i 0 i 0 i 0 log n log n log n log n The closed form solution to the summation is: 1 log7 n 5(5log7 n ) 1 5(5log5 n ) 1 5(n) 1 (n) 4 4 4 4 i 0 The work is therefore O(n lg n) . (Note that n log7 5 n n log7 5 n lg n , so this is 5 5 log7 n i 1 a looser upper bound than what we derived using the Master Theorem.) 3 of 6 UML CS 91.404 3. (20 points) Probability If you roll a pair of fair dice once, what is the expected value of the sum of the dice? Note: “Sum of the dice” here means the sum of the values that are “face up” after the roll. SOLUTION: The expected value = 7. To derive this, let X be a random variable representing the sum of the dice for one roll. To find E[X], you can either use the formula for expectation of a random value directly or use indicator random values. We show both methods. Method 1: (no indicator random variables) E[ X ] x Pr( X x) 2 Pr( X 2) 12 Pr( X 12) x Note that there are 12 different possible values for the sum of the dice. To calculate the probabilities, you can enumerate the different possible outcomes. There are no outcomes for which the sum is 0 or 1, 1 outcome for which the sum =2, 2 for sum=3, 3 for sum=4, 4 for sum=5, 5 for sum = 6, 6 for sum=7, 5 for sum=8, 4 for sum=9, 3 for sum=10, 2 for sum=11, and 1 for sum=12. Total # outcomes = 36. 1 2 3 4 5 6 5 4 3 2 1 E[ X ] x Pr( X x) 2 3 4 5 6 7 8 9 10 11 12 36 36 36 36 36 36 36 36 36 36 36 x Total = 252/36 = 7. Method 2: (indicator random variables) Number the dots on each die. There are 21 dots on each die. Now, define an indicator random variable: dot i of die 1 shows up on top when the dice are rolled once 1 Xi 0 dot i of die 1 does not show up on top when the dice are rolled once dot i of die 2 shows up on top when the dice are rolled once 1 Yi 0 dot i of die 2 does not show up on top when the dice are rolled once E[ X ] E X Y 21 i 1 i 21 21 i 1 i i 1 E[ X i ] E[Yi ] i 1 E[ X i ] E[Yi ] i 1 Pr{dot i of die 1 shows up on top} Pr{dot i of die 2 shows up on top} 21 21 1 42 21 1 i 1 7. 6 6 6 4 of 6 UML CS 91.404 4. (40 points) Designing a Sorting Algorithm In this problem you will design an efficient sorting algorithm that accepts, as input, an array A of n integers and outputs A with its elements in nondecreasing order. The algorithm has n iterations. During iteration j, your algorithm should: - randomly (according to a uniform random distribution) select a value key from what is currently stored in A[ j…n ] - put key into the correct place in A so that, at the end of iteration j, A[ 1…j ] will be sorted and will contain key. Your algorithm should not use more than (1) additional storage beyond A. See the next several pages for parts (a), (b), and (c) that ask for pseudocode, correctness and analysis. Note/Hint: If you use an algorithm from the textbook, without modifications, then you do not need to write down all its steps – just reference the page number and name of the procedure. In this case, you can also reference its correctness proof and analysis. However, if you modify an algorithm from the textbook, you need to write down your modifications to the pseudocode, correctness justification and analysis. a) (14 points) Pseudocode. Describe your algorithm using the pseudocode conventions in our textbook. SOLUTION: 1 RANDOM SORT ( A) 2 3 4 5 6 7 8 9 for j 1 to length[ A] do swap A[ j ] RANDOM ( j length[ A]) key A[ j ] i j 1 while i 0 and A[i] key do A[i 1] A[i ] i i 1 A[i 1] key 5 of 6 UML CS 91.404 b) (13 points) Correctness. SOLUTION: RANDOM-SORT is a modified INSERTION-SORT. First, observe that RANDOM-SORT has n iterations, as required by the problem’s specification; this is implemented by line 2. This represents a change from INSERTION-SORT, which only has n-1 iterations. Next we show that iteration j of RANDOM-SORT satisfies the invariant given in the problem’s specification: iteration j: - Randomly (according to a uniform random distribution) selects a value key from what is currently stored in A[ j…n ]. This is implemented by line 3. - Puts key into the correct place in A so that, at the end of iteration j, A[ 1…j ] is sorted and contains key. This is implemented by lines 3-9. Line 3 puts the randomly selected item into A[ j ]. Note that to preserve what had been stored in A[ j ] we swap A[ j ] with the randomly selected item. The swap ensures that, at the end of the iteration, the randomly selected value is in A[ j…n ]. Now we claim that, at the end of the iteration, A[ j…n ] is sorted. This is true because lines 4-9 are exactly the statements in the body of the for loop of INSERTION-SORT, so the correctness justification of INSERTION-SORT from p. 17-18 of the textbook guarantees that the elements will be sorted correctly into nondecreasing order. Finally, we observe that RANDOM-SORT does not use more than (1) additional storage beyond A. c) (13 points) Running Time Analysis. Give a worst-case asymptotic upper bound on the running time of your algorithm as a function of n. Explain your answer. Note/Hint: You may assume that a call to RANDOM takes (1) time. SOLUTION: We observed above that RANDOM-SORT is a modified INSERTION-SORT. One modification is the line that calls RANDOM and performs a swap; these steps take (1) time in the worst case. The other modification is in the number of iterations. The total worst-case time is the sum of the time for the steps outside the while loop + the time for the steps inside the while loop. The upper bound on the time for the steps inside the n n(n 1) while loop is j (n 2 ) , so the total time is (n) (n 2 ) (n 2 ) , so the 2 j 1 worst-case time is in O(n 2 ). 6 of 6