This preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: CS 473: Fundamental Algorithms, Spring 2011 HW 3 Homework is due by Monday, 23:59:59, February 14 Problem 1 is due by Sunday, 23:59:59, February 13 This homework contains four problems. Read the instructions for submitting homework on the course webpage . Collaboration Policy: For this homework, Problems 2–4 can be worked in groups of up to three students. Problem 1 should be answered in Compass as part of the assessment HW3Online and should be done individually. 1. (30 pts.) Short questions to be answered on compass individually. 2. (30 pts.) You are given an array A with n distinct numbers in it, and another array B of ranks i 1 < i 2 < ... < i k . An element x of A has rank u if there are exactly u 1 numbers in A smaller than it. Design an algorithm that outputs the k elements in A that have the ranks i 1 ,i 2 ,...,i k . (A) (5 pts.) As a warmup exercise describe how to solve this problem in O ( nk ) time. Solution : For each 1 ≤ j ≤ k , output the i j th smallest number in A using the O ( n ) time selection algorithm. Clearly the total running time is O ( nk ). Rubrik : • 3pts: If the writeup gives a correct algorithm. If the algorithm is incomplete but gives a key idea that could lead to a correct algorithm, it gets 2pts. • 2pts: If the algorithm has running time O ( nk ). 1 (B) (20 pts.) Describe a O ( n log k ) recursive algorithm for this problem. Prove the bound on the running time of the algorithm. Solution : We give the following algorithm kSelect . It takes two arrays A and B as input. Here B is an array of distinct positive integers, and the numbers are sorted in increasing order. Algorithm kSelect ( A,B,μ ): If  B  >  A  then B ← the smallest  A  elements in B . Let b 1 < b 2 < ... < b  B  be the integers in B If  B  = 1 and  A  ≤ b 1 μ then output the ( b 1 μ )th smallest number in A . Else Let μ = b b B  / 2 c . If μ μ >  A  then kSelect ( A, { b 1 ,b 2 ,...,b b B  / 2 c } ,μ ). Else p ← the ( μ μ ) th smallest element in A . Let A l := { a ∈ A  a ≤ p } and A r := { a ∈ A  a > p } . kSelect ( A l , { b 1 ,b 2 ,...,b b B  / 2 c } ,μ ). kSelect ( A r , { b b B  / 2 c +1 ,...,b  B  } ,μ } . We run kSelect ( A,B, 0) to answer the problem. Let T ( x,y ) denote the running time of the algorithm when  A  = x and  B  = y . Note that T ( x,y ) = T (  A l  , b y/ 2 c ) + T (  A r  , d y/ 2 e )+ O ( x ) or T ( x,y ) = T (  A  , b y/ 2 c )+ O ( x ); T ( x, 1) = O ( x ). It is easy to see that the depth of the recursion tree is O (log k ). Further, we know that the total work at every level is O ( x ) since x =  A l  +  A r  . Hence the running time follows. Rubrik : • 15pts: If the writeup gives a correct algorithm with running time O ( n log k ). If the the algorithm is incomplete but gives a key idea that could lead to a correct algorithm, it gets 10pts....
View
Full
Document
This note was uploaded on 04/18/2011 for the course CS 473 taught by Professor Chekuri,c during the Spring '08 term at University of Illinois, Urbana Champaign.
 Spring '08
 Chekuri,C
 Algorithms

Click to edit the document details