midterm2 - CS 573 Midterm 2 Questions Fall 2010 This exam...

Info iconThis preview shows pages 1–2. Sign up to view the full content.

View Full Document Right Arrow Icon
CS 573 Midterm 2 Questions Fall 2010 This exam lasts 90 minutes. Write your answers in the separate answer booklet. Please return this question sheet with your answers. 1. Assume we have access to a function R ANDOM ( k ) that returns, given any positive integer k , an integer chosen independently and uniformly at random from the set { 1,2,. .., k } , in O ( 1 ) time. For example, to perform a fair coin flip, we could call R ANDOM ( 2 ) . Now suppose we want to write an efficient function R ANDOM P ERMUTATION ( n ) that returns a permutation of the set { 1,2,. .., n } chosen uniformly at random; that is, each permutation must be chosen with probability 1 / n !. (a) Prove that the following algorithm is not correct. [Hint: Consider the case n = 3 .] R ANDOM P ERMUTATION ( n ) : for i 1 to n π [ i ] i for i 1 to n swap π [ i ] π [ R ANDOM ( n )] return π (b) Describe and analyze a correct R ANDOM P ERMUTATION algorithm that runs in O ( n ) expected time. (In fact, O ( n ) worst-case time is possible.) 2. Suppose we have
Background image of page 1

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

View Full DocumentRight Arrow Icon
Image of page 2
This is the end of the preview. Sign up to access the rest of the document.

Page1 / 2

midterm2 - CS 573 Midterm 2 Questions Fall 2010 This exam...

This preview shows document pages 1 - 2. Sign up to view the full document.

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