Docsity

Order Statistics: Finding the ith Smallest Element, Slides of Introduction to Computers

Partial preview of the text

Download Order Statistics: Finding the ith Smallest Element and more Slides Introduction to Computers in PDF only on Docsity!

COMS21102: Order Statistics

Raphaël Clifford [email protected]

Bristol University, Department of Computer Science Bristol BS8 1UB, UK

21st November 2006

Raphaël Clifford [email protected]

Order Statistics

I (^) In this lecture we look at order statistics. I (^) Select the i th smallest of n elements (the element of rank i ). I (^) If i = 1 we have the minimum I (^) If i = n , we have the maximum I (^) If i = b( n + 1 )/ 2 c or d( n + 1 )/ 2 e, then we have the median I (^) A simple solution is just to sort the input and choose the i th element of the sorted array. I (^) Worst case is Θ( n log n ) time using Merge Sort or Heap Sort for example. I (^) We can do better than that!

Raphaël Clifford [email protected]

Partitioning revisited

PARTITION( A , p , q ) returns the index of the pivot element after partitioning.

Input : A , p , q Output : Index of pivot x ← A [ q ] B x is the pivot; i ← p − 1; for j ← p to q − 1 do if A [ j ] ≤ x then i ← i + 1; swap A [ i ] and A [ j ]; end end swap A [ i + 1 ] and A [ q ]; return i + 1;

3 8 1 7 4 6 2

3 8 1 7 2 6 4

3 1 8 7 6

3 1 8 7 6

3

3

1 2 6 4

2

2

4

4

1 2 4 6

7 8

8 7

I (^) At the start of every iteration, the following invariants are true I (^) For p ≤ k ≤ i , A [ k ] ≤ x I (^) For i + 1 ≤ k < j , A [ k ] > x I (^) A [ q ] = x Raphaël Clifford [email protected]

Partitioning revisited

PARTITION( A , p , q ) returns the index of the pivot element after partitioning.

Input : A , p , q Output : Index of pivot x ← A [ q ] B x is the pivot; i ← p − 1; for j ← p to q − 1 do if A [ j ] ≤ x then i ← i + 1; swap A [ i ] and A [ j ]; end end swap A [ i + 1 ] and A [ q ]; return i + 1;

3 8 1 7 4 6 2

3 8 1 7 2 6 4

3 1 8 7 6

3 1 8 7 6

3

3

1 2 6 4

2

2

4

4

1 2 4 6

7 8

8 7

I (^) At the start of every iteration, the following invariants are true I (^) For p ≤ k ≤ i , A [ k ] ≤ x I (^) For i + 1 ≤ k < j , A [ k ] > x I (^) A [ q ] = x Raphaël Clifford [email protected]

Randomised algorithm

RAND-SELECT( A , p , q , i ) returns the i th smallest element of A [ p ,... , q ]

Input : A , p , q and i Output : The i th smallest value in A [ p ,... , q ] if p = q then return A [ p ]; end r ← RAND-PARTITION( A , p , q ); k ← r − p + 1 B pivot index in A [ p ,... , q ]; if i = k then return A [ r ] ; end if i < k then return RAND-SELECT( A , p , r − 1 , i ); else return RAND-SELECT( A , r + 1 , q , i − k ); end

Raphaël Clifford [email protected]

Order Statistics Example

In this example we want to find the 5th smallest number in the array 3 , 8 , 1 , 7 , 4 , 6 , 2.

3 8 1 7 4 6 2 i=

Pivot

8 7 k=

Rand−Select(A,5,7,1)

Raphaël Clifford [email protected]

Running Time - Better Intuition

We can be slightly more mathematical about our intuition. I (^) Call a good partition one where the largest side in the partition is of size ≤ 3 n /4. I (^) Fifty percent of the pivot choices will give us this. I (^) On average, we need only two pivot choices to find such a partition. This is the same as the expected number of times you need to toss a coin to get a head. I (^) E = 1 + E / 2 ⇒ E = 2 and so on average, after two partitions the array will shrink to at most size 3 n /4. I (^) Therefore the expected running time is T ( n ) ≤ T ( 3 n / 4 ) + O ( n ) I (^) This gives us T ( n ) ∈ O ( n ) on average.

Raphaël Clifford [email protected]

Worst case linear time order statistics

The problem with the randomised approach is that we might choose bad pivots. Blum, Floyd, Pratt, Rivest, and Tarjan [1973] found a way to guarantee a good pivot each time. SELECT( A , p , q , i )

  1. Divide the n = q − p + 1 elements into groups of 5. Find the median of each 5-element group.
  2. Recursively SELECT the median x of the b n / 5 c group medians to be the pivot.
  3. Partition A [ p ,... , q ] using the pivot x. k = rank( x )
  4. if i = k then return x
  5. elsif i < k I (^) then recursively SELECT the i th smallest element in the lower part I (^) else recursively SELECT the i − k th smallest element in the higher part

Raphaël Clifford [email protected]

Worst case linear time analysis

We simplify by noticing that for n ≥ 20, 3b n / 10 c ≥ n /4. For n < 20 we can find the order statistics in constant time. I (^) Therefore the final recursive call is on an array of no more than 3 n / 4 elements. I (^) This takes at most T ( 3 n / 4 ) time I (^) Therefore T ( n ) ≤ T ( 3 n / 4 ) + T ( n / 5 ) + Θ( n ) I (^) Guess T ( n ) ∈ Θ( n ) and substitute T ( n ) = cn giving T ( n ) ≤ 3 cn / 4 + cn / 5 + Θ( n ) I (^) T ( n ) = 19 / 20 cn + Θ( n ) I (^) T ( n ) = cn − ( cn / 20 − Θ( n )) I (^) T ( n ) ≤ cn , if we choose c to be large enough to compensate for the Θ( n ) term

Raphaël Clifford [email protected]

Summary

I (^) RAND-SELECT is linear time on average and simple to implement but harder to analyse. I (^) However, RAND-SELECT runs in Θ( n^2 ) in the worst case. I (^) SELECT runs in linear time in the worst case but is slower in practice unless you are very unlucky. I (^) SELECT is slightly harder to implement but simpler to analyse.

Raphaël Clifford [email protected]