ps2sol - Introduction to Algorithms October 7, 2005...

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

View Full Document Right Arrow Icon

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

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

Unformatted text preview: Introduction to Algorithms October 7, 2005 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik D. Demaine and Charles E. Leiserson Handout 12 Problem Set 2 Solutions Problem 2-1. Is this (almost) sorted? Harry Potter, the child wizard of Hogwarts fame, has once again run into trouble. Professor Snape has sent Harry to detention and assigned him the task of sorting all the old homework assignments from the last 200 years. Being a wizard, Harry waves his wand and says, ordinatus sortitus , and the papers rapidly pile themselves in order. Professor Snape, however, wants to determine whether Harry’s spell correctly sorted the papers. Unfortunately, there are a large number n of papers and determining whether they are in perfect order takes ( n ) time. Professor Snape instead decides to check whether the papers are almost sorted. He wants to know whether 90% of the papers are sorted: is it possible to remove 10% of the papers and have the resulting list be sorted? In this problem, we will help Professor Snape to find an algorithm that takes as input a list A containing n distinct elements, and acts as follows: • If the list A is sorted, the algorithm always returns true . • If the list A is not 90% sorted, the algorithm returns false with probability at least 2/3. (a) Professor Snape first considers the following algorithm: Repeat k times: 1. Choose a paper i independently and uniformly at random from the open in- terval (1 , n ) . (That is, 1 < i < n .) 2. Compare paper A [ i − 1] and A [ i ] . Output false and halt if they are not sorted correctly. 3. Compare paper A [ i ] and A [ i + 1] . Output false and halt if they are not sorted correctly. Output true . Show that for this algorithm to correctly discover whether the list is almost sorted with probability at least 2 / 3 requires k = ( n ) . Hint: Find a sequence that is not almost sorted, but with only a small number of elements that will cause the algorithm to return false . Solution: We show that Snape’s algorithm does not work by constructing a counter- example that has the following two properties: • A is not 90% sorted. = 2 Handout 12: Problem Set 2 Solutions • Snape’s algorithm outputs false with probability 2 / 3 only if k = ( n ) . In particular, we consider the following counter-example: A = [ ≥ n/ 2 ∅ + 1 , . . . , n, 1 , 2 , 3 , . . . , ≥ n/ 2 ∅ ] . Lemma 1 A is not 90% sorted. Proof. Assume, by contradiction, that the list is 90% sorted. Then, there must be some 90% of the elements that are correctly ordered with respect to each other. There must be one of these correctly ordered elements in the first half of the list, i.e., with index i ≥ n/ 2 ∅ . Also, there must be one of these correctly ordered elements in the second half of the list, i.e. with index j > ≥ n/ 2 ∅ . However, A [ i ] > A [ j ] , by construction, which is a contradiction. Therefore A is not 90% sorted....
View Full Document

This note was uploaded on 02/01/2010 for the course COMPUTERSC 6.046J/18. taught by Professor Erikd.demaineandcharlese.leiserson during the Fall '05 term at MIT.

Page1 / 16

ps2sol - Introduction to Algorithms October 7, 2005...

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

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