computer science engineering 423 823
play

Computer Science & Engineering 423/823 Introduction Design and - PowerPoint PPT Presentation

CSCE423/823 Computer Science & Engineering 423/823 Introduction Design and Analysis of Algorithms Finding Minimum and Maximum Lecture 01 Medians and Order Statistics (Chapter 9) Selection of Arbitrary Order Statistic Stephen Scott


  1. CSCE423/823 Computer Science & Engineering 423/823 Introduction Design and Analysis of Algorithms Finding Minimum and Maximum Lecture 01 — Medians and Order Statistics (Chapter 9) Selection of Arbitrary Order Statistic Stephen Scott (Adapted from Vinodchandran N. Variyam) Spring 2012 1 / 24

  2. Introduction CSCE423/823 Given an array A of n distinct numbers, the i th order statistic of A is its i th smallest element Introduction i = 1 ) minimum Finding i = n ) maximum Minimum and Maximum i = b ( n + 1) / 2 c ) (lower) median Selection of Arbitrary E.g. if A = [8 , 5 , 3 , 10 , 4 , 12 , 6] then min = 3, max = 12, median = Order Statistic 6, 3rd order stat = 5 Problem: Given array A of n elements and a number i 2 { 1 , . . . , n } , find the i th order statistic of A There is an obvious solution to this problem. What is it? What is its time complexity? Can we do better? What if we only focus on i = 1 or i = n ? 2 / 24

  3. Finding Minimum CSCE423/823 Introduction small = A [1] Finding 1 Minimum and Maximum for i = 2 to n do 2 Selection of if small > A [ i ] then 3 Arbitrary Order Statistic small = A [ i ] 4 end 5 return small 6 Algorithm 1: Minimum( A, n ) 3 / 24

  4. E ffi ciency of Minimum( A ) CSCE423/823 Introduction Loop is executed n � 1 times, each with one comparison Finding Minimum and ) Total n � 1 comparisons Maximum Can we do better? Selection of Arbitrary Order Statistic Lower Bound: Any algorithm finding minimum of n elements will need at least n � 1 comparisons Proof of this comes from fact that no element of A can be considered for elimination as the minimum until it’s been compared at least once 4 / 24

  5. Correctness of Minimum( A ) CSCE423/823 Introduction Finding Observe that the algorithm always maintains the invariant that at Minimum and Maximum the end of each loop iteration, small holds the minimum of A [1 · · · i ] Selection of Arbitrary Easily shown by induction Order Statistic Correctness follows by observing that i == n before return statement 5 / 24

  6. Simultaneous Minimum and Maximum CSCE423/823 Introduction Finding Minimum and Given array A with n elements, find both its minimum and maximum Maximum Selection of What is the obvious algorithm? What is its (non-asymptotic) time Arbitrary Order Statistic complexity? Can we do better? 6 / 24

  7. Simultaneous Minimum and Maximum CSCE423/823 large = max( A [1] , A [2]) 1 Introduction small = min( A [1] , A [2]) 2 Finding for i = 2 to b n/ 2 c do Minimum and 3 Maximum large = max( large, max( A [2 i � 1] , A [2 i ])) 4 Selection of small = min( small, min( A [2 i � 1] , A [2 i ])) Arbitrary 5 Order Statistic end 6 if n is odd then 7 large = max( large, A [ n ]) 8 small = min( small, A [ n ]) 9 return ( large, small ) 10 Algorithm 2: MinAndMax( A, n ) 7 / 24

  8. Explanation of MinAndMax CSCE423/823 Introduction Finding Idea: For each pair of values examined in the loop, compare them Minimum and Maximum directly Selection of Arbitrary For each such pair, compare the smaller one to small and the larger Order Statistic one to large Example: A = [8 , 5 , 3 , 10 , 4 , 12 , 6] 8 / 24

  9. E ffi ciency of MinAndMax CSCE423/823 How many comparisons does MinAndMax make? Introduction Finding Initialization on Lines 1 and 2 requires only one comparison Minimum and Maximum Each iteration through the loop requires one comparison between Selection of Arbitrary A [2 i � 1] and A [2 i ] and then one comparison to each of large and Order Statistic small , for a total of three Lines 8 and 9 require one comparison each Total is at most 1 + 3( b n/ 2 c � 1) + 2  3 b n/ 2 c , which is better than 2 n � 3 for finding minimum and maximum separately 9 / 24

  10. Selection of the i th Smallest Value CSCE423/823 Introduction Finding Now to the general problem: Given A and i , return the i th smallest Minimum and Maximum value in A Selection of Arbitrary Obvious solution is sort and return i th element Order Statistic Algorithm Time complexity is Θ ( n log n ) Overview Algorithm Pseudocode Can we do better? Example Time Complexity Master Theorem 10 / 24

  11. Selection of the i th Smallest Value (2) CSCE423/823 Introduction Finding New algorithm: Divide and conquer strategy Minimum and Maximum Idea: Somehow discard a constant fraction of the current array after Selection of spending only linear time Arbitrary Order Statistic If we do that, we’ll get a better time complexity Algorithm Overview More on this later Algorithm Pseudocode Which fraction do we discard? Example Time Complexity Master Theorem 11 / 24

  12. Procedure Select CSCE423/823 1 if p == r then Introduction 2 return A [ p ] Finding 3 q = Partition( A, p, r ) // Like Partition in Quicksort Minimum and Maximum 4 k = q � p + 1 // Size of A [ p · · · q ] Selection of if i == k then 5 Arbitrary 6 return A [ q ] // Pivot value is the answer Order Statistic Algorithm 7 else if i < k then Overview return Select ( A, p, q � 1 , i ) // Answer is in left subarray 8 Algorithm Pseudocode 9 else Example Time Complexity 10 return Select ( A, q + 1 , r, i � k ) // Answer is in right subarray Master Theorem Algorithm 3: Select( A, p, r, i ), which returns i th smallest element from A [ p · · · r ] 12 / 24

  13. What is Select Doing? CSCE423/823 Like in Quicksort, Select first calls Partition, which chooses a pivot Introduction element q , then reorders A to put all elements < A [ q ] to the left of Finding A [ q ] and all elements > A [ q ] to the right of A [ q ] Minimum and Maximum E.g. if A = [1 , 7 , 5 , 4 , 2 , 8 , 6 , 3] and pivot element is 5, then result is Selection of A 0 = [1 , 4 , 2 , 3 , 5 , 7 , 8 , 6] Arbitrary Order Statistic Algorithm If A [ q ] is the element we seek, then return it Overview Algorithm Pseudocode If sought element is in left subarray, then recursively search it, and Example Time Complexity ignore right subarray Master Theorem If sought element is in right subarray, then recursively search it, and ignore left subarray 13 / 24

  14. Partitioning the Array CSCE423/823 x = ChoosePivotElement( A, p, r ) // Returns index of pivot 1 Introduction 2 exchange A [ x ] with A [ r ] Finding 3 i = p � 1 Minimum and Maximum 4 for j = p to r � 1 do if A [ j ]  A [ r ] then Selection of 5 Arbitrary 6 i = i + 1 Order Statistic 7 exchange A [ i ] with A [ j ] Algorithm Overview 8 end Algorithm Pseudocode 9 exchange A [ i + 1] with A [ r ] Example Time Complexity 10 return i + 1 Master Theorem Algorithm 4: Partition( A, p, r ), which chooses a pivot element and partitions A [ p · · · r ] around it 14 / 24

  15. Partitioning the Array: Example (Fig 7.1) CSCE423/823 Introduction Finding Minimum and Maximum Selection of Arbitrary Order Statistic Compare each element A [ j ] to x ( = 4 ) and swap with A [ i ] if A [ j ]  x Algorithm Overview Algorithm Pseudocode Example Time Complexity Master Theorem 15 / 24

  16. Choosing a Pivot Element CSCE423/823 Introduction Finding Minimum and Maximum Choice of pivot element is critical to low time complexity Selection of Why? Arbitrary Order Statistic Algorithm What is the best choice of pivot element to partition A [ p · · · r ] ? Overview Algorithm Pseudocode Example Time Complexity Master Theorem 16 / 24

  17. Choosing a Pivot Element (2) CSCE423/823 Introduction Finding Minimum and Want to pivot on an element that it as close as possible to being the Maximum median Selection of Arbitrary Order Statistic Of course, we don’t know what that is Algorithm Overview Will do median of medians approach to select pivot element Algorithm Pseudocode Example Time Complexity Master Theorem 17 / 24

  18. Median of Medians CSCE423/823 Introduction Given (sub)array A of n elements, partition A into m = b n/ 5 c Finding Minimum and groups of 5 elements each, and at most one other group with the Maximum remaining n mod 5 elements Selection of Arbitrary Make an array A 0 = [ x 1 , x 2 , . . . , x m +1 ] , where x i is median of group Order Statistic Algorithm i , found by sorting (in constant time) group i Overview Algorithm Pseudocode Call Select ( A 0 , 1 , m + 1 , b ( m + 1) / 2 c ) and use the returned element Example Time Complexity as the pivot Master Theorem 18 / 24

  19. Example CSCE423/823 Introduction Finding Minimum and Maximum Split into teams, and work this example on the board: Find the 4th Selection of smallest element of A = [4 , 9 , 12 , 17 , 6 , 5 , 21 , 14 , 8 , 11 , 13 , 29 , 3] Arbitrary Order Statistic Show results for each step of Select, Partition, and ChoosePivotElement Algorithm Overview Algorithm Pseudocode Example Time Complexity Master Theorem 19 / 24

Recommend


More recommend