ps3sol

ps3sol - Introduction to Algorithms Massachusetts Institute...

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

View Full Document Right Arrow Icon
Introduction to Algorithms October 6, 2006 Massachusetts Institute of Technology 6.046J/18.410J Professors Erik Demaine and Madhu Sudan Problem Set 3 Solutions Problem Set 3 Solutions Problem 3-1. Popular Elements Given an array A [1 . . n ] of n elements, and given a constant 0 < α < 1 , we say that an element x is α -popular in A if x appears more than αn times in A . Our goal is to find an algorithm that reports all α -popular elements in A , i.e., that computes the set B α = ± x : |{ i : A [ i ] = x }| > αn ² . Our goal is to obtain a running time of O ( n lg(1 )) . (a) How many α -popular elements can there be, i.e., how large can | B α | be? Solution: If A has n elements, we must have less than 1 elements that appear more than αn times. Thus, | B α | < 1 . (b) Give a deterministic O ( n ) -time algorithm for computing all α -popular elements when α = 1 / 2 . Your algorithm should call S ELECT O (1) times. Prove that your algorithm reports the correct answer. Solution: By part (a), | B 1 / 2 | < 2 , i.e., | B 1 / 2 | ≤ 1 . Thus, there can be at most one 1 / 2 - popular element x . If such an x exists, we will show that x must be the median. The algorithm is to compute the median element y of A , and then scan A to count whether y appears more than n/ 2 times. Computing the median element y and scanning the array both take O ( n ) time. Suppose for contradiction that x is 1 / 2 -popular, but y 6 = x is the median element. Suppose y < x ; the case y > x is symmetric. If n is odd, then there are at least ( n + 1) / 2 elements greater than y in A . If n is even, then there are least n/ 2 + 1 elements greater than y in A . Both contradict the fact that y is the median. (c) Generalize your algorithm from part (b) to find an O ( n/α ) -time algorithm to compute B α for any constant α . Your algorithm should call S ELECT O (1 ) times. Solution: Let s i = i d αn e , for i = 1 , 2 , . . . b n/ d αn ec , and let t i be the element in A with rank s i . If any object x appears more than αn times in the list, then we must have x = t i for some i . The proof of this fact is a generalization of the argument from part (b). Let A 0 be the sorted version of A . Then any element x which is α -popular must appear in a
Background image of page 1

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

View Full DocumentRight Arrow Icon
2 Problem Set 3 Solutions consecutive segment of A 0 containing at least d αn e elements. By our choice of s i , such a segment must overlap one of the s i ’s, i.e., A 0 [ s i ] = x for some i . Thus, if an element x is α -popular, then it must be one of the O (1 ) order-statistics t i we computed above. We can compute the O (1 ) order statistics in O ( n/α ) time using O (1 ) applications of the deterministic select algorithm. Finally, for each of the
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 01/20/2012 for the course CS 6.006 taught by Professor Erikdemaine during the Fall '08 term at MIT.

Page1 / 9

ps3sol - Introduction to Algorithms Massachusetts Institute...

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