sol3 - Introduction to Algorithms Solution Set 3 CS 482,...

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 Solution Set 3 CS 482, Spring 2008 (1) We describe an algorithm Test ( S ) whose input is a set S of bank cards and whose output is: a bank card x S such that more than half the elements of S are equivalent to x , if any such bank card exists; otherwise, the output is null . Let n = | S | . When n = 0, Test ( S ) outputs null . When n = 1, Test ( S ) outputs the unique element of S . Otherwise, Test ( S ) partitions S into two (arbitrary) subsets S 1 ,S 2 of size b n/ 2 c and d n/ 2 e . Let x 1 = Test ( S 1 ) ,x 2 = Test ( S 2 ). For each non- null element of the set { x 1 ,x 2 } , the algorithm takes this bank card and performs an equivalence test against all other bank cards. Let n 1 be the number of bank cards equivalent to x 1 (including x 1 itself), or n 1 = 0 if x 1 = null . Define n 2 similarly, but with x 2 in place of x 1 . If n 1 > n/ 2 then Test ( S ) outputs x 1 ; else if n 2 > n/ 2 then Test ( S ) outputs x 2 ; else Test ( S ) outputs null . Running time. Equivalence-testing x 1 against all other bank cards takes n- 1 comparisons or 0 comparisons, depending on whether x 1 = null , and similarly for x 2 . Thus the running time, f ( n ), of Test ( S ) satisfies f ( n ) f ( b n/ 2 c ) + f ( d n/ 2 e ) + 2( n- 1) . The solution of this recurrence satisfies f ( n ) = O ( n log n ) . Correctness. The proof of correctness is by induction on n , the base cases n = 0 , 1 being trivial. The algorithm never outputs a bank card unless it has directly compared that bank card to all others and determined that it is equivalent to at least b n/ 2 c of them, hence it never produces an incorrect non- null output. We only need to prove that if there is a set T S of bank cards which are all equivalent, such that | T | > n/ 2, then the algorithm outputs an element of T . To prove this, let m = | T | , m 1 = | T S 1 | , m 2 = | T S 2 | . If 2 m 1 | S 1 | and 2 m 2 | S 2 | then 2 m | S | contradicting our assumption that | T | > n/ 2. So at least one of the following inequalities holds: m 1 > | S 1 | / 2 or m 2 > | S 2 | / 2 . Assume without loss of generality that the first of these holds. Then Test ( S 1 ) outputs an element x 1 T , and Test ( S ) will compare x 1 with all other elements of S and discover that the set of elements equivalent to x 1 (i.e., the set T ) contains more than half the elements of S . Hence Test ( S ) will output an element of T , as desired. (2) We present two solutions. The first is a divide-and-conquer algorithm which resembles mergesort in that it divides the input into two equal-sized subproblems, solves both subproblems, then combines the results by carefully walking through a pair of ordered lists. The second is a simpler algorithm that sorts the lines and processes them in order of increasing slope, maintaining a stack whose contents are all the lines currently visible. Solution 1: Let = { L 1 ,...,L n } be any collection of nonvertical lines. Assume without loss ofbe any collection of nonvertical lines....
View Full Document

This note was uploaded on 10/02/2008 for the course CS 482 taught by Professor Kleinberg during the Spring '08 term at Cornell University (Engineering School).

Page1 / 6

sol3 - Introduction to Algorithms Solution Set 3 CS 482,...

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