{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

hw_8_soln - CS 473 Algorithms Fall 2008 HW 8(Solution This...

Info icon This preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 473: Algorithms, Fall 2008 HW 8 (Solution) This homework contains four problems. Read the instructions for submitting homework on the course webpage . In particular, make sure that you write the solutions for the problems on separate sheets of paper; the sheets for each problem should be stapled together. Write your name and netid on each sheet. Collaboration Policy: For this home work, Problems 1-3 can be worked in groups of up to 3 students each. Problem 0 should be answered in Compass as part of the assessment HW7-Online and should be done individually. 1. (35 pts) Suppose we want to write an efficient function Shuffle ( n ) that returns a permutation of the set { 1,2,..., n } chosen uniformly at random. (a) Prove that the following algorithm is not correct. [ Hint: Consider n = 3. ] Solution: Here’s the one-line ‘proof’: 3 3 / 3! = 27 / 6 = 9 / 2 is not an integer. Here are the output probabilities for each permutation when n = 3: Pr [ 1 2 3 ] = 4 / 27 Pr [ 1 3 2 ] = 5 / 27 Pr [ 2 1 3 ] = 5 / 27 Pr [ 2 3 1 ] = 5 / 27 Pr [ 3 1 2 ] = 4 / 27 Pr [ 3 2 1 ] = 4 / 27 More generally, there are exactly n n possible outcomes for this algorithm, because there are n possible return values for each of the n calls to R ANDOM . Each of these outcomes occurs with probability 1 / n n . If each permutation is equally likely, then exactly n n / n ! different outcomes lead to each permutation. But for any n 3, the number n n / n ! is not an integer— n ! is divisible by n - 1, but n n is not—so we have a contradiction. Thus, for any n 3, some permutations must be more likely than others. Rubric: 7 points for a correct proof. Considering the case n = 3 is enough. (b) Prove that the following implementation of Shuffle ( n ) is correct. What is its expected running time? Solution: The algorithm is vacuously correct when n = 0, so assume n 1. The algorithm places the integer 1 uniformly at random into the array π , say into slot π [ j ] . By the induction hypothesis, the remainder of the algorithm fills the other n - 1 slots of π with a permutation of the set { 2 , 3 ,..., n } chosen uniformly at random. Thus, the output array π is equally likely to be any permutation of the set { 1,2,..., n } . Let X i denote the number of calls to R ANDOM during the i th iteration of the outer loop; the running time of the algorithm is clearly Θ( i X i ) . During the i th iteration of the outer loop, exactly i - 1 of the slots in the output array are already occupied. Thus, with probability ( i - 1 ) / n , the while loop iterates more than once. Since the random numbers produced by R ANDOM are identically distributed, we have a recurrence E [ X i ] = 1 + i - 1 n E [ X i ] , 1
Image of page 1

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

View Full Document Right Arrow Icon
whose solution is E [ X i ] = n / ( n - i + 1 ) . Thus, the total expected number of calls to R ANDOM is exactly n X i = 1 n n - i + 1 = n n X j = 1 1 j = n H n , which implies that the algorithm’s expected running time is Θ( n H n ) = Θ( n log n ) .
Image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    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.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern