# hw4 - CS 473 Homework 4 (due March 2, 2010) Spring 2010 1....

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

CS 473 Homework 4 (due March 2, 2010) Spring 2010 1. Suppose we want to write an efﬁcient function S HUFFLE ( 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 the case n = 3 .] S HUFFLE ( n ) : for i 1 to n π [ i ] i for i 1 to n swap π [ i ] π [ R ANDOM ( n )] return π [ 1.. n ] (b) Consider the following implementation of S HUFFLE . S HUFFLE ( n ) : for i 1 to n π [ i ] NULL for i 1 to n j R ANDOM ( n ) while ( π [ j ] ! = NULL) j R ANDOM ( n ) π [ j ] i return π [ 1.. n ] Prove that this algorithm is correct. What is its expected running time? (c) Describe and analyze an implementation of S HUFFLE that runs in O ( n ) time. (An algorithm that runs in O ( n ) expected time is ﬁne, but O ( n ) worst-case time is possible.) 2. Death knocks on your door one cold blustery morning and challenges you to a game. Death knows you are an algorithms student, so instead of the traditional game of chess, Death presents you with a complete binary tree with 4 n leaves, each colored either black or white. There is a token at

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 01/21/2011 for the course CS 473 taught by Professor Chekuri,c during the Spring '08 term at University of Illinois, Urbana Champaign.

### Page1 / 2

hw4 - CS 473 Homework 4 (due March 2, 2010) Spring 2010 1....

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

View Full Document
Ask a homework question - tutors are online