SlideShare a Scribd company logo
1 of 51
Download to read offline
Analysis of Algorithms
Medians and Order Statistics
Andres Mendez-Vazquez
September 22, 2014
1 / 31
Outline
1 Introduction
2 Selection problem
3 Minimum-Maximum
4 Selection in expected linear time
RANDOMIZED-SELECT
5 Selection in worst-case linear time
6 SELECT the ith element in n elements
7 Summary
2 / 31
Introduction
Fact:
The ith order statistic of a set of n elements is the ith smallest element
Examples
i = 1 we are talking the minimum.
i = n we are talking the maximum.
When n is an odd number, the position i of the median is defined by
i = n+1
2
3 / 31
Introduction
Fact:
The ith order statistic of a set of n elements is the ith smallest element
Examples
i = 1 we are talking the minimum.
i = n we are talking the maximum.
When n is an odd number, the position i of the median is defined by
i = n+1
2
3 / 31
Selection Problem
Input:
A set A of n (distinct) numbers and an integer i, with 1 ≤ i ≤ n.
Output:
The element x ∈ A that is larger than exactly i − 1 other elements of A.
4 / 31
Selection Problem
Input:
A set A of n (distinct) numbers and an integer i, with 1 ≤ i ≤ n.
Output:
The element x ∈ A that is larger than exactly i − 1 other elements of A.
4 / 31
Minimum-Maximum
Minimum using n − 1 comparissons
Minimum(A)
1 min = A [1]
2 for i = 2 to A.length
3 if min > A [i]
4 min = A [i]
5 return min
5 / 31
Using an O(n log n) Algorithm
Assumption
Suppose n elements are sorted by an O(n log n) algorithm, e.g.,
MERGE-SORT.
Then the following properties hold for the ordered set
Minimum: The first element.
Maximum: The last element.
The ith order statistic corresponds to the ith element.
For the median:
If n is odd, then the median is equal to the n+1
2 th element
If n is even:
The lower median is equal to the n+1
2
th element.
The upper median is equal to the n+1
2
th element.
6 / 31
Using an O(n log n) Algorithm
Assumption
Suppose n elements are sorted by an O(n log n) algorithm, e.g.,
MERGE-SORT.
Then the following properties hold for the ordered set
Minimum: The first element.
Maximum: The last element.
The ith order statistic corresponds to the ith element.
For the median:
If n is odd, then the median is equal to the n+1
2 th element
If n is even:
The lower median is equal to the n+1
2
th element.
The upper median is equal to the n+1
2
th element.
6 / 31
Using an O(n log n) Algorithm
Important fact!
All selections can be done in O(1), so total: O(n log n).
Question!!!
Can we do better?
How many comparisons are needed to get the max and min of n
elements?
7 / 31
Using an O(n log n) Algorithm
Important fact!
All selections can be done in O(1), so total: O(n log n).
Question!!!
Can we do better?
How many comparisons are needed to get the max and min of n
elements?
7 / 31
Minimum and Maximum at the same time... better choice
Naively
The naïve Maximum and Minimum at the same will take 2n − 2
comparisons.
Something better?
Take two elements at the same time.
Compare them to get the min and max in the tuple.
Compare the min in the tuple with the global min and do the same
with the tuple max.
8 / 31
Minimum and Maximum at the same time... better choice
Naively
The naïve Maximum and Minimum at the same will take 2n − 2
comparisons.
Something better?
Take two elements at the same time.
Compare them to get the min and max in the tuple.
Compare the min in the tuple with the global min and do the same
with the tuple max.
8 / 31
Minimum and Maximum at the same time... better choice
This will give you
3 n
2 comparisons.
Why?
Let’s see!
9 / 31
Minimum and Maximum at the same time... better choice
This will give you
3 n
2 comparisons.
Why?
Let’s see!
9 / 31
Selection in expected linear time O(n)
Selecting in expected linear time implies:
Selecting the ith element.
Use the divide and conquer algorithm RANDOMIZED-SELECT.
Similar to Quicksort, partition the input array recursively.
Unlike Quicksort, which works on both sides of the partition, just work
on one side of the partition. This is called PRUNE-AND-SEARCH,
prune one side, just search the other.
Homework
Please review or read Quicksort in Cormen’s book (chapter 7).
10 / 31
Selection in expected linear time O(n)
Selecting in expected linear time implies:
Selecting the ith element.
Use the divide and conquer algorithm RANDOMIZED-SELECT.
Similar to Quicksort, partition the input array recursively.
Unlike Quicksort, which works on both sides of the partition, just work
on one side of the partition. This is called PRUNE-AND-SEARCH,
prune one side, just search the other.
Homework
Please review or read Quicksort in Cormen’s book (chapter 7).
10 / 31
Outline
1 Introduction
2 Selection problem
3 Minimum-Maximum
4 Selection in expected linear time
RANDOMIZED-SELECT
5 Selection in worst-case linear time
6 SELECT the ith element in n elements
7 Summary
11 / 31
Strategy
First partition the set of n elements
How? Remember Randomized Quicksort!!!
Thus
We get a q from the random partition!!!
12 / 31
Strategy
First partition the set of n elements
How? Remember Randomized Quicksort!!!
Thus
We get a q from the random partition!!!
12 / 31
Example
Positions
p rq
1 2 3 4 5 6 7
1 2 3 4 5 6 7 8 9 10
Global Index Position
Local Index Position
p-q=3 then k=p-q+1 =4
13 / 31
Thus, the Strategy
If i < k
The possible ith smallest element is between p and q − 1.
If i > k
The possible ith smallest element is between q + 1 and r.
But the new i = i − k, this will work because we start at p = 1 and
r = n.
If i == k ← We need to convert this to local index too
return A [q]
14 / 31
Thus, the Strategy
If i < k
The possible ith smallest element is between p and q − 1.
If i > k
The possible ith smallest element is between q + 1 and r.
But the new i = i − k, this will work because we start at p = 1 and
r = n.
If i == k ← We need to convert this to local index too
return A [q]
14 / 31
Thus, the Strategy
If i < k
The possible ith smallest element is between p and q − 1.
If i > k
The possible ith smallest element is between q + 1 and r.
But the new i = i − k, this will work because we start at p = 1 and
r = n.
If i == k ← We need to convert this to local index too
return A [q]
14 / 31
RANDOMIZED-SELECT
RANDOMIZED-SELECT Algorithm
Randomized-Select(A, p, r, i)
1 if p == r
2 return A [p]
3 q = Randomized-Partition(A, p, r)
4 k = q − p + 1 // Local Index
5 if i == k // The Answer
6 return A [q]
7 elseif i < k
8 return Randomized-Select(A, p, q − 1, i)
// Converting to local index
9 else return Randomized-Select(A, q + 1, r, i − k)
15 / 31
Analysis of RANDOMIZED-SELECT
Worst-case running time Θ(n2
). Why?
An empty side and a side with remaining elements. So every partitioning
of m elements will take Θ(m) time where m = n, n − 1, ..., 2. Thus in total
Θ(n) + Θ(n − 1) + ... + Θ(2) = Θ(n(n−1)
2 − 1) = Θ(n2).
Moreover
No particular input elicits the worst-case behavior.
In average, RANDOMIZED-SELECT is good because of the
randomness.
16 / 31
Analysis of RANDOMIZED-SELECT
Worst-case running time Θ(n2
). Why?
An empty side and a side with remaining elements. So every partitioning
of m elements will take Θ(m) time where m = n, n − 1, ..., 2. Thus in total
Θ(n) + Θ(n − 1) + ... + Θ(2) = Θ(n(n−1)
2 − 1) = Θ(n2).
Moreover
No particular input elicits the worst-case behavior.
In average, RANDOMIZED-SELECT is good because of the
randomness.
16 / 31
Selection in worst-case linear time O(n).
Goal:
Select the ith smallest element of S = {a1, a2, ..., an}.
Solution:
Use the so called PRUNE-AND-SEARCH technique:
Let x ∈ S, and partition S into three subsets.
S1 = {aj |aj < x}, S2 = {aj |aj = x}, S3 = {aj |aj > x}.
If |S1| > i, search ith smallest elements in S1 recursively, (prune S2 and
S3 away).
Else If |S1| + |S2| > i, then return x (the ith smallest element).
Else search the (i − (|S1| + |S2|))th element in S3 recursively (prune S1
and S2 away).
A question arises
How to select x such that S1 and S3 are nearly equal in cardinality? Force
an even search!!!
17 / 31
Selection in worst-case linear time O(n).
Goal:
Select the ith smallest element of S = {a1, a2, ..., an}.
Solution:
Use the so called PRUNE-AND-SEARCH technique:
Let x ∈ S, and partition S into three subsets.
S1 = {aj |aj < x}, S2 = {aj |aj = x}, S3 = {aj |aj > x}.
If |S1| > i, search ith smallest elements in S1 recursively, (prune S2 and
S3 away).
Else If |S1| + |S2| > i, then return x (the ith smallest element).
Else search the (i − (|S1| + |S2|))th element in S3 recursively (prune S1
and S2 away).
A question arises
How to select x such that S1 and S3 are nearly equal in cardinality? Force
an even search!!!
17 / 31
The way to select x
Divide elements into n
5
groups of 5 elements each and find the median of each one
We cannot say anything about the order between elements, but between median
an elements
Thus, arrows go from less to greater!!!
The Medians
Elements<Median
Elements>Median
18 / 31
The way to select x
Find the Median of the Medians
Again the arrows indicate the order from greater to less
The Medians
Elements<Median
Elements>Median
x
19 / 31
The way to select x
We have (Here, again the worst case scenario!!!)
At least 1
2
n
5 − 2 possible groups with 3 elements greater than x
The Medians
Elements<Median
Elements>Median
x
THIS AND THIS
20 / 31
The way to select x
We have (Here, again the worst case scenario!!!)
At least 1
2
n
5 − 2 possible groups with 3 elements less than x
The Medians
Elements<Median
Elements>Median
x
21 / 31
Thus, we have
First
3 1
2
n
5 − 2 = 3n
10 − 6 elements < x
Second
3 1
2
n
5 − 2 = 3n
10 − 6 elements > x
22 / 31
Thus, we have
First
3 1
2
n
5 − 2 = 3n
10 − 6 elements < x
Second
3 1
2
n
5 − 2 = 3n
10 − 6 elements > x
22 / 31
SELECT the ith element in n elements
Proceed as follows:
1 Divide n elements into n
5 groups of 5 elements.
2 Find the median of each group.
3 Use SELECT recursively to find the median x of the
above n
5 medians.
4 Partition n elements around x into S1, S2, and S3.
5 If |S1| > i, search ith smallest element in S1 recursively.
Else If |S1| + |S2| > i, then return x (the ith smallest element).
Else search (i − (|S1| + |S2|)) th in S3 recursively
23 / 31
Analysis of SELECT
Analysing complexity
Steps 1,2 and 4 take O(n).
Step 3 takes T( n
5 ).
Let us see step 5:
At least half of the medians in step 2 are greater or equal than x,
thus at least 1
2
n
5 − 2 groups contribute 3 elements which are greater
or equal than x. i.e., 3( 1
2
n
5 − 2) ≥ 3n
10 − 6.
Similarly, the number of elements less or equal than x is also at least
3n
10 − 6.
Thus, |S1| is at most 7n
10 + 6, similarly for |S3|.
Thus SELECT in step 5 is called recursively on at most 7n
10 + 6
elements.
24 / 31
Analysis of SELECT
Analysing complexity
Steps 1,2 and 4 take O(n).
Step 3 takes T( n
5 ).
Let us see step 5:
At least half of the medians in step 2 are greater or equal than x,
thus at least 1
2
n
5 − 2 groups contribute 3 elements which are greater
or equal than x. i.e., 3( 1
2
n
5 − 2) ≥ 3n
10 − 6.
Similarly, the number of elements less or equal than x is also at least
3n
10 − 6.
Thus, |S1| is at most 7n
10 + 6, similarly for |S3|.
Thus SELECT in step 5 is called recursively on at most 7n
10 + 6
elements.
24 / 31
Final Recursion
We have then
T (n) =



O(1) if n < some value (i.e. 140)
T n
5 + T 7n
10 + 6 + O (n) if n ≥ some value (i.e. 140)
25 / 31
Solve recurrence by substitution
Suppose T(n) ≤ cn for some c
T(n) ≤ c n
5 + c(7n
10 + 6) + an
T(n) ≤ 1
5 cn + c + 7
10 cn + 6c + an
T(n) ≤ 9
10 cn + 7c + an
T(n) ≤ cn + (− 1
10 cn + an + 7c)
26 / 31
Solve recurrence by substitution.
Suppose T(n) ≤ cn for some c
T(n) ≤ c n
5 + c(7n
10 + 6) + an
T(n) ≤ 1
5 cn + c + 7
10 cn + 6c + an
T(n) ≤ 9
10 cn + 7c + an
T(n) ≤ cn + (− 1
10 cn + an + 7c)
T(n) is at most cn
If − 1
10 cn + an + 7c < 0.
i.e., c ≥ 10a( n
n−70 ) when n > 70.
So, select n = 140, and then c ≥ 20a.
Note:
n may not be 140, any integer greater than 70 is OK.
27 / 31
Solve recurrence by substitution.
Suppose T(n) ≤ cn for some c
T(n) ≤ c n
5 + c(7n
10 + 6) + an
T(n) ≤ 1
5 cn + c + 7
10 cn + 6c + an
T(n) ≤ 9
10 cn + 7c + an
T(n) ≤ cn + (− 1
10 cn + an + 7c)
T(n) is at most cn
If − 1
10 cn + an + 7c < 0.
i.e., c ≥ 10a( n
n−70 ) when n > 70.
So, select n = 140, and then c ≥ 20a.
Note:
n may not be 140, any integer greater than 70 is OK.
27 / 31
Solve recurrence by substitution.
Suppose T(n) ≤ cn for some c
T(n) ≤ c n
5 + c(7n
10 + 6) + an
T(n) ≤ 1
5 cn + c + 7
10 cn + 6c + an
T(n) ≤ 9
10 cn + 7c + an
T(n) ≤ cn + (− 1
10 cn + an + 7c)
T(n) is at most cn
If − 1
10 cn + an + 7c < 0.
i.e., c ≥ 10a( n
n−70 ) when n > 70.
So, select n = 140, and then c ≥ 20a.
Note:
n may not be 140, any integer greater than 70 is OK.
27 / 31
Final Thoughts
Why group of size 5?
Using groups of 3 does not work, you can try and plug it into the
claculations
What about 7 or bigger odd number
It does not change the computations, only by a constant
28 / 31
Final Thoughts
Why group of size 5?
Using groups of 3 does not work, you can try and plug it into the
claculations
What about 7 or bigger odd number
It does not change the computations, only by a constant
28 / 31
Applications
Computer Vision
In the Median Filter:
Given a neighborhood of n members of x, you need to find the
median to substitute the value in x
Statistical Applications
Confidence intervals for quantiles
A machine may run on 10 batteries and shuts off when the ith
battery dies. You will want to know the distribution of X(i).
In machine-learning if you want to convert a continuous valued
feature into Boolean features by bucketing it, one common approach
is to partition it by percentile so that the cardinality of each Boolean
feature is somewhat similar.
29 / 31
Applications
Computer Vision
In the Median Filter:
Given a neighborhood of n members of x, you need to find the
median to substitute the value in x
Statistical Applications
Confidence intervals for quantiles
A machine may run on 10 batteries and shuts off when the ith
battery dies. You will want to know the distribution of X(i).
In machine-learning if you want to convert a continuous valued
feature into Boolean features by bucketing it, one common approach
is to partition it by percentile so that the cardinality of each Boolean
feature is somewhat similar.
29 / 31
Summary.
The ith order statistic of n elements
S = {a1, a2, ..., an}:ith smallest elements:
Minimum and maximum.
Median, lower median, upper median.
Selection in expected average linear time
Worst case running time
PRUNE-AND-SEARCH
Selection in worst-case linear time
The fast randomized version is due to Hoare.
It is still unknown exactly how many comparisons are needed to
determine the median.
30 / 31
Summary.
The ith order statistic of n elements
S = {a1, a2, ..., an}:ith smallest elements:
Minimum and maximum.
Median, lower median, upper median.
Selection in expected average linear time
Worst case running time
PRUNE-AND-SEARCH
Selection in worst-case linear time
The fast randomized version is due to Hoare.
It is still unknown exactly how many comparisons are needed to
determine the median.
30 / 31
Summary.
The ith order statistic of n elements
S = {a1, a2, ..., an}:ith smallest elements:
Minimum and maximum.
Median, lower median, upper median.
Selection in expected average linear time
Worst case running time
PRUNE-AND-SEARCH
Selection in worst-case linear time
The fast randomized version is due to Hoare.
It is still unknown exactly how many comparisons are needed to
determine the median.
30 / 31
Exercises
From Cormen’s Book Chapter 9
9.1-1
9.2-3
9.3-4
9.3-8
9.2
31 / 31

More Related Content

What's hot

CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functionsallyn joy calcaben
 
Markov Chain Monte Carlo explained
Markov Chain Monte Carlo explainedMarkov Chain Monte Carlo explained
Markov Chain Monte Carlo explaineddariodigiuni
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIMohamed Loey
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1Amrinder Arora
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsMohamed Loey
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting AlgorithmsPranay Neema
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and ComplexityRajandeep Gill
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiProfessor Lili Saghafi
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic NotationsRishabh Soni
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithmsRajendran
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IMohamed Loey
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IMohamed Loey
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm KristinaBorooah
 

What's hot (20)

CMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of FunctionsCMSC 56 | Lecture 8: Growth of Functions
CMSC 56 | Lecture 8: Growth of Functions
 
Asymptotic Notation
Asymptotic NotationAsymptotic Notation
Asymptotic Notation
 
Markov Chain Monte Carlo explained
Markov Chain Monte Carlo explainedMarkov Chain Monte Carlo explained
Markov Chain Monte Carlo explained
 
Divide and conquer
Divide and conquerDivide and conquer
Divide and conquer
 
Best,worst,average case .17581556 045
Best,worst,average case .17581556 045Best,worst,average case .17581556 045
Best,worst,average case .17581556 045
 
Algorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms IIAlgorithms Lecture 5: Sorting Algorithms II
Algorithms Lecture 5: Sorting Algorithms II
 
Divide and Conquer - Part 1
Divide and Conquer - Part 1Divide and Conquer - Part 1
Divide and Conquer - Part 1
 
Splay Tree
Splay TreeSplay Tree
Splay Tree
 
Complexity of Algorithm
Complexity of AlgorithmComplexity of Algorithm
Complexity of Algorithm
 
Algorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to AlgorithmsAlgorithms Lecture 1: Introduction to Algorithms
Algorithms Lecture 1: Introduction to Algorithms
 
Disjoint sets
Disjoint setsDisjoint sets
Disjoint sets
 
Sorting Algorithms
Sorting AlgorithmsSorting Algorithms
Sorting Algorithms
 
Asymptotic Notation and Complexity
Asymptotic Notation and ComplexityAsymptotic Notation and Complexity
Asymptotic Notation and Complexity
 
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili SaghafiEffective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
Effective Algorithm for n Fibonacci Number By: Professor Lili Saghafi
 
Asymptotic Notations
Asymptotic NotationsAsymptotic Notations
Asymptotic Notations
 
Greedy algorithms
Greedy algorithmsGreedy algorithms
Greedy algorithms
 
Algorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms IAlgorithms Lecture 4: Sorting Algorithms I
Algorithms Lecture 4: Sorting Algorithms I
 
Algorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms IAlgorithms Lecture 2: Analysis of Algorithms I
Algorithms Lecture 2: Analysis of Algorithms I
 
Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm Linked list in Data Structure and Algorithm
Linked list in Data Structure and Algorithm
 
Computational Complexity
Computational ComplexityComputational Complexity
Computational Complexity
 

Viewers also liked

median and order statistics
median and order statisticsmedian and order statistics
median and order statisticsShashank Singh
 
Estimation theory 1
Estimation theory 1Estimation theory 1
Estimation theory 1Gopi Saiteja
 
Multivariate normal proof
Multivariate normal proofMultivariate normal proof
Multivariate normal proofCole Arora
 
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...ganuraga
 
lecture 11
lecture 11lecture 11
lecture 11sajinsc
 
Next higher number with same number of binary bits set
Next higher number with same number of binary bits setNext higher number with same number of binary bits set
Next higher number with same number of binary bits setGowriKumar Chandramouli
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Traian Rebedea
 
hive HBase Metastore - Improving Hive with a Big Data Metadata Storage
hive HBase Metastore - Improving Hive with a Big Data Metadata Storagehive HBase Metastore - Improving Hive with a Big Data Metadata Storage
hive HBase Metastore - Improving Hive with a Big Data Metadata StorageDataWorks Summit/Hadoop Summit
 
الاحصاء اللامعلمي Non-parametric methods
الاحصاء اللامعلمي Non-parametric methodsالاحصاء اللامعلمي Non-parametric methods
الاحصاء اللامعلمي Non-parametric methodsnihad alqattan
 
Modified Distribution Method
Modified Distribution MethodModified Distribution Method
Modified Distribution MethodAlvin Niere
 
Spark Summit EU talk by Kent Buenaventura and Willaim Lau
Spark Summit EU talk by Kent Buenaventura and Willaim LauSpark Summit EU talk by Kent Buenaventura and Willaim Lau
Spark Summit EU talk by Kent Buenaventura and Willaim LauSpark Summit
 

Viewers also liked (20)

median and order statistics
median and order statisticsmedian and order statistics
median and order statistics
 
Sufficient statistics
Sufficient statisticsSufficient statistics
Sufficient statistics
 
Estimation theory 1
Estimation theory 1Estimation theory 1
Estimation theory 1
 
Multivariate normal proof
Multivariate normal proofMultivariate normal proof
Multivariate normal proof
 
Augmenting Data Structures
Augmenting Data StructuresAugmenting Data Structures
Augmenting Data Structures
 
Algorithm
AlgorithmAlgorithm
Algorithm
 
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
A Measure Of Independence For A Multifariate Normal Distribution And Some Con...
 
lecture 11
lecture 11lecture 11
lecture 11
 
Session 9 10
Session 9 10Session 9 10
Session 9 10
 
Estadísticos de orden
Estadísticos de ordenEstadísticos de orden
Estadísticos de orden
 
Next higher number with same number of binary bits set
Next higher number with same number of binary bits setNext higher number with same number of binary bits set
Next higher number with same number of binary bits set
 
Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8Algorithm Design and Complexity - Course 8
Algorithm Design and Complexity - Course 8
 
hive HBase Metastore - Improving Hive with a Big Data Metadata Storage
hive HBase Metastore - Improving Hive with a Big Data Metadata Storagehive HBase Metastore - Improving Hive with a Big Data Metadata Storage
hive HBase Metastore - Improving Hive with a Big Data Metadata Storage
 
الاحصاء اللامعلمي Non-parametric methods
الاحصاء اللامعلمي Non-parametric methodsالاحصاء اللامعلمي Non-parametric methods
الاحصاء اللامعلمي Non-parametric methods
 
Life table analysis
Life table analysisLife table analysis
Life table analysis
 
Life table
Life tableLife table
Life table
 
Modified Distribution Method
Modified Distribution MethodModified Distribution Method
Modified Distribution Method
 
Spark Summit EU talk by Kent Buenaventura and Willaim Lau
Spark Summit EU talk by Kent Buenaventura and Willaim LauSpark Summit EU talk by Kent Buenaventura and Willaim Lau
Spark Summit EU talk by Kent Buenaventura and Willaim Lau
 
Non parametric tests
Non parametric testsNon parametric tests
Non parametric tests
 
Measures of fertility
Measures of fertilityMeasures of fertility
Measures of fertility
 

Similar to 07 Analysis of Algorithms: Order Statistics

lecture 10
lecture 10lecture 10
lecture 10sajinsc
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortzukun
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisSNJ Chaudhary
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingzukun
 
Top school in noida
Top school in noidaTop school in noida
Top school in noidaEdhole.com
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024RUHULAMINHAZARIKA
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxnikshaikh786
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptxParagAhir1
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & AlgorithumsAin-ul-Moiz Khawaja
 
Sesquickselect: One and a half pivot for cache efficient selection
Sesquickselect: One and a half pivot for cache efficient selectionSesquickselect: One and a half pivot for cache efficient selection
Sesquickselect: One and a half pivot for cache efficient selectionSebastian Wild
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2Deepak John
 

Similar to 07 Analysis of Algorithms: Order Statistics (20)

lecture 10
lecture 10lecture 10
lecture 10
 
Skiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksortSkiena algorithm 2007 lecture08 quicksort
Skiena algorithm 2007 lecture08 quicksort
 
Quick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And AnalysisQuick sort Algorithm Discussion And Analysis
Quick sort Algorithm Discussion And Analysis
 
Data Structure (MC501)
Data Structure (MC501)Data Structure (MC501)
Data Structure (MC501)
 
Skiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sortingSkiena algorithm 2007 lecture09 linear sorting
Skiena algorithm 2007 lecture09 linear sorting
 
n-squared_sorts
n-squared_sortsn-squared_sorts
n-squared_sorts
 
Top school in noida
Top school in noidaTop school in noida
Top school in noida
 
Sorting2
Sorting2Sorting2
Sorting2
 
DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024DAA-Divide and Conquer methodology, DAA 2024
DAA-Divide and Conquer methodology, DAA 2024
 
Module 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptxModule 2_ Divide and Conquer Approach.pptx
Module 2_ Divide and Conquer Approach.pptx
 
simple-sorting algorithms
simple-sorting algorithmssimple-sorting algorithms
simple-sorting algorithms
 
Daa chapter5
Daa chapter5Daa chapter5
Daa chapter5
 
Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1Bs,qs,divide and conquer 1
Bs,qs,divide and conquer 1
 
sorting and searching.pptx
sorting and searching.pptxsorting and searching.pptx
sorting and searching.pptx
 
Sorting algorithums > Data Structures & Algorithums
Sorting algorithums  > Data Structures & AlgorithumsSorting algorithums  > Data Structures & Algorithums
Sorting algorithums > Data Structures & Algorithums
 
Sesquickselect: One and a half pivot for cache efficient selection
Sesquickselect: One and a half pivot for cache efficient selectionSesquickselect: One and a half pivot for cache efficient selection
Sesquickselect: One and a half pivot for cache efficient selection
 
Randomized algorithms ver 1.0
Randomized algorithms ver 1.0Randomized algorithms ver 1.0
Randomized algorithms ver 1.0
 
Unit 2 in daa
Unit 2 in daaUnit 2 in daa
Unit 2 in daa
 
algorithm Unit 2
algorithm Unit 2 algorithm Unit 2
algorithm Unit 2
 
Analysis and design of algorithms part2
Analysis and design of algorithms part2Analysis and design of algorithms part2
Analysis and design of algorithms part2
 

More from Andres Mendez-Vazquez

01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectorsAndres Mendez-Vazquez
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issuesAndres Mendez-Vazquez
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiationAndres Mendez-Vazquez
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep LearningAndres Mendez-Vazquez
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learningAndres Mendez-Vazquez
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusAndres Mendez-Vazquez
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusAndres Mendez-Vazquez
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesAndres Mendez-Vazquez
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variationsAndres Mendez-Vazquez
 

More from Andres Mendez-Vazquez (20)

2.03 bayesian estimation
2.03 bayesian estimation2.03 bayesian estimation
2.03 bayesian estimation
 
05 linear transformations
05 linear transformations05 linear transformations
05 linear transformations
 
01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors01.04 orthonormal basis_eigen_vectors
01.04 orthonormal basis_eigen_vectors
 
01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues01.03 squared matrices_and_other_issues
01.03 squared matrices_and_other_issues
 
01.02 linear equations
01.02 linear equations01.02 linear equations
01.02 linear equations
 
01.01 vector spaces
01.01 vector spaces01.01 vector spaces
01.01 vector spaces
 
06 recurrent neural_networks
06 recurrent neural_networks06 recurrent neural_networks
06 recurrent neural_networks
 
05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation05 backpropagation automatic_differentiation
05 backpropagation automatic_differentiation
 
Zetta global
Zetta globalZetta global
Zetta global
 
01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning01 Introduction to Neural Networks and Deep Learning
01 Introduction to Neural Networks and Deep Learning
 
25 introduction reinforcement_learning
25 introduction reinforcement_learning25 introduction reinforcement_learning
25 introduction reinforcement_learning
 
Neural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning SyllabusNeural Networks and Deep Learning Syllabus
Neural Networks and Deep Learning Syllabus
 
Introduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabusIntroduction to artificial_intelligence_syllabus
Introduction to artificial_intelligence_syllabus
 
Ideas 09 22_2018
Ideas 09 22_2018Ideas 09 22_2018
Ideas 09 22_2018
 
Ideas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data SciencesIdeas about a Bachelor in Machine Learning/Data Sciences
Ideas about a Bachelor in Machine Learning/Data Sciences
 
Analysis of Algorithms Syllabus
Analysis of Algorithms  SyllabusAnalysis of Algorithms  Syllabus
Analysis of Algorithms Syllabus
 
20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations20 k-means, k-center, k-meoids and variations
20 k-means, k-center, k-meoids and variations
 
18.1 combining models
18.1 combining models18.1 combining models
18.1 combining models
 
17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension17 vapnik chervonenkis dimension
17 vapnik chervonenkis dimension
 
A basic introduction to learning
A basic introduction to learningA basic introduction to learning
A basic introduction to learning
 

Recently uploaded

Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...VICTOR MAESTRE RAMIREZ
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm Systemirfanmechengr
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfRajuKanojiya4
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectssuserb6619e
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingBootNeck1
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadaditya806802
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Erbil Polytechnic University
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)Dr SOUNDIRARAJ N
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the weldingMuhammadUzairLiaqat
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating SystemRashmi Bhat
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgsaravananr517913
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptMadan Karki
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdfCaalaaAbdulkerim
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionMebane Rash
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solidnamansinghjarodiya
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONjhunlian
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentBharaniDharan195623
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdfHafizMudaserAhmad
 

Recently uploaded (20)

Design and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdfDesign and analysis of solar grass cutter.pdf
Design and analysis of solar grass cutter.pdf
 
Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...Software and Systems Engineering Standards: Verification and Validation of Sy...
Software and Systems Engineering Standards: Verification and Validation of Sy...
 
Class 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm SystemClass 1 | NFPA 72 | Overview Fire Alarm System
Class 1 | NFPA 72 | Overview Fire Alarm System
 
National Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdfNational Level Hackathon Participation Certificate.pdf
National Level Hackathon Participation Certificate.pdf
 
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in projectDM Pillar Training Manual.ppt will be useful in deploying TPM in project
DM Pillar Training Manual.ppt will be useful in deploying TPM in project
 
System Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event SchedulingSystem Simulation and Modelling with types and Event Scheduling
System Simulation and Modelling with types and Event Scheduling
 
home automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasadhome automation using Arduino by Aditya Prasad
home automation using Arduino by Aditya Prasad
 
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
Comparative study of High-rise Building Using ETABS,SAP200 and SAFE., SAFE an...
 
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
UNIT III ANALOG ELECTRONICS (BASIC ELECTRONICS)
 
welding defects observed during the welding
welding defects observed during the weldingwelding defects observed during the welding
welding defects observed during the welding
 
Main Memory Management in Operating System
Main Memory Management in Operating SystemMain Memory Management in Operating System
Main Memory Management in Operating System
 
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfgUnit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
Unit7-DC_Motors nkkjnsdkfnfcdfknfdgfggfg
 
Indian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.pptIndian Dairy Industry Present Status and.ppt
Indian Dairy Industry Present Status and.ppt
 
Research Methodology for Engineering pdf
Research Methodology for Engineering pdfResearch Methodology for Engineering pdf
Research Methodology for Engineering pdf
 
POWER SYSTEMS-1 Complete notes examples
POWER SYSTEMS-1 Complete notes  examplesPOWER SYSTEMS-1 Complete notes  examples
POWER SYSTEMS-1 Complete notes examples
 
US Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of ActionUS Department of Education FAFSA Week of Action
US Department of Education FAFSA Week of Action
 
Engineering Drawing section of solid
Engineering Drawing     section of solidEngineering Drawing     section of solid
Engineering Drawing section of solid
 
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTIONTHE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
THE SENDAI FRAMEWORK FOR DISASTER RISK REDUCTION
 
Configuration of IoT devices - Systems managament
Configuration of IoT devices - Systems managamentConfiguration of IoT devices - Systems managament
Configuration of IoT devices - Systems managament
 
11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf11. Properties of Liquid Fuels in Energy Engineering.pdf
11. Properties of Liquid Fuels in Energy Engineering.pdf
 

07 Analysis of Algorithms: Order Statistics

  • 1. Analysis of Algorithms Medians and Order Statistics Andres Mendez-Vazquez September 22, 2014 1 / 31
  • 2. Outline 1 Introduction 2 Selection problem 3 Minimum-Maximum 4 Selection in expected linear time RANDOMIZED-SELECT 5 Selection in worst-case linear time 6 SELECT the ith element in n elements 7 Summary 2 / 31
  • 3. Introduction Fact: The ith order statistic of a set of n elements is the ith smallest element Examples i = 1 we are talking the minimum. i = n we are talking the maximum. When n is an odd number, the position i of the median is defined by i = n+1 2 3 / 31
  • 4. Introduction Fact: The ith order statistic of a set of n elements is the ith smallest element Examples i = 1 we are talking the minimum. i = n we are talking the maximum. When n is an odd number, the position i of the median is defined by i = n+1 2 3 / 31
  • 5. Selection Problem Input: A set A of n (distinct) numbers and an integer i, with 1 ≤ i ≤ n. Output: The element x ∈ A that is larger than exactly i − 1 other elements of A. 4 / 31
  • 6. Selection Problem Input: A set A of n (distinct) numbers and an integer i, with 1 ≤ i ≤ n. Output: The element x ∈ A that is larger than exactly i − 1 other elements of A. 4 / 31
  • 7. Minimum-Maximum Minimum using n − 1 comparissons Minimum(A) 1 min = A [1] 2 for i = 2 to A.length 3 if min > A [i] 4 min = A [i] 5 return min 5 / 31
  • 8. Using an O(n log n) Algorithm Assumption Suppose n elements are sorted by an O(n log n) algorithm, e.g., MERGE-SORT. Then the following properties hold for the ordered set Minimum: The first element. Maximum: The last element. The ith order statistic corresponds to the ith element. For the median: If n is odd, then the median is equal to the n+1 2 th element If n is even: The lower median is equal to the n+1 2 th element. The upper median is equal to the n+1 2 th element. 6 / 31
  • 9. Using an O(n log n) Algorithm Assumption Suppose n elements are sorted by an O(n log n) algorithm, e.g., MERGE-SORT. Then the following properties hold for the ordered set Minimum: The first element. Maximum: The last element. The ith order statistic corresponds to the ith element. For the median: If n is odd, then the median is equal to the n+1 2 th element If n is even: The lower median is equal to the n+1 2 th element. The upper median is equal to the n+1 2 th element. 6 / 31
  • 10. Using an O(n log n) Algorithm Important fact! All selections can be done in O(1), so total: O(n log n). Question!!! Can we do better? How many comparisons are needed to get the max and min of n elements? 7 / 31
  • 11. Using an O(n log n) Algorithm Important fact! All selections can be done in O(1), so total: O(n log n). Question!!! Can we do better? How many comparisons are needed to get the max and min of n elements? 7 / 31
  • 12. Minimum and Maximum at the same time... better choice Naively The naïve Maximum and Minimum at the same will take 2n − 2 comparisons. Something better? Take two elements at the same time. Compare them to get the min and max in the tuple. Compare the min in the tuple with the global min and do the same with the tuple max. 8 / 31
  • 13. Minimum and Maximum at the same time... better choice Naively The naïve Maximum and Minimum at the same will take 2n − 2 comparisons. Something better? Take two elements at the same time. Compare them to get the min and max in the tuple. Compare the min in the tuple with the global min and do the same with the tuple max. 8 / 31
  • 14. Minimum and Maximum at the same time... better choice This will give you 3 n 2 comparisons. Why? Let’s see! 9 / 31
  • 15. Minimum and Maximum at the same time... better choice This will give you 3 n 2 comparisons. Why? Let’s see! 9 / 31
  • 16. Selection in expected linear time O(n) Selecting in expected linear time implies: Selecting the ith element. Use the divide and conquer algorithm RANDOMIZED-SELECT. Similar to Quicksort, partition the input array recursively. Unlike Quicksort, which works on both sides of the partition, just work on one side of the partition. This is called PRUNE-AND-SEARCH, prune one side, just search the other. Homework Please review or read Quicksort in Cormen’s book (chapter 7). 10 / 31
  • 17. Selection in expected linear time O(n) Selecting in expected linear time implies: Selecting the ith element. Use the divide and conquer algorithm RANDOMIZED-SELECT. Similar to Quicksort, partition the input array recursively. Unlike Quicksort, which works on both sides of the partition, just work on one side of the partition. This is called PRUNE-AND-SEARCH, prune one side, just search the other. Homework Please review or read Quicksort in Cormen’s book (chapter 7). 10 / 31
  • 18. Outline 1 Introduction 2 Selection problem 3 Minimum-Maximum 4 Selection in expected linear time RANDOMIZED-SELECT 5 Selection in worst-case linear time 6 SELECT the ith element in n elements 7 Summary 11 / 31
  • 19. Strategy First partition the set of n elements How? Remember Randomized Quicksort!!! Thus We get a q from the random partition!!! 12 / 31
  • 20. Strategy First partition the set of n elements How? Remember Randomized Quicksort!!! Thus We get a q from the random partition!!! 12 / 31
  • 21. Example Positions p rq 1 2 3 4 5 6 7 1 2 3 4 5 6 7 8 9 10 Global Index Position Local Index Position p-q=3 then k=p-q+1 =4 13 / 31
  • 22. Thus, the Strategy If i < k The possible ith smallest element is between p and q − 1. If i > k The possible ith smallest element is between q + 1 and r. But the new i = i − k, this will work because we start at p = 1 and r = n. If i == k ← We need to convert this to local index too return A [q] 14 / 31
  • 23. Thus, the Strategy If i < k The possible ith smallest element is between p and q − 1. If i > k The possible ith smallest element is between q + 1 and r. But the new i = i − k, this will work because we start at p = 1 and r = n. If i == k ← We need to convert this to local index too return A [q] 14 / 31
  • 24. Thus, the Strategy If i < k The possible ith smallest element is between p and q − 1. If i > k The possible ith smallest element is between q + 1 and r. But the new i = i − k, this will work because we start at p = 1 and r = n. If i == k ← We need to convert this to local index too return A [q] 14 / 31
  • 25. RANDOMIZED-SELECT RANDOMIZED-SELECT Algorithm Randomized-Select(A, p, r, i) 1 if p == r 2 return A [p] 3 q = Randomized-Partition(A, p, r) 4 k = q − p + 1 // Local Index 5 if i == k // The Answer 6 return A [q] 7 elseif i < k 8 return Randomized-Select(A, p, q − 1, i) // Converting to local index 9 else return Randomized-Select(A, q + 1, r, i − k) 15 / 31
  • 26. Analysis of RANDOMIZED-SELECT Worst-case running time Θ(n2 ). Why? An empty side and a side with remaining elements. So every partitioning of m elements will take Θ(m) time where m = n, n − 1, ..., 2. Thus in total Θ(n) + Θ(n − 1) + ... + Θ(2) = Θ(n(n−1) 2 − 1) = Θ(n2). Moreover No particular input elicits the worst-case behavior. In average, RANDOMIZED-SELECT is good because of the randomness. 16 / 31
  • 27. Analysis of RANDOMIZED-SELECT Worst-case running time Θ(n2 ). Why? An empty side and a side with remaining elements. So every partitioning of m elements will take Θ(m) time where m = n, n − 1, ..., 2. Thus in total Θ(n) + Θ(n − 1) + ... + Θ(2) = Θ(n(n−1) 2 − 1) = Θ(n2). Moreover No particular input elicits the worst-case behavior. In average, RANDOMIZED-SELECT is good because of the randomness. 16 / 31
  • 28. Selection in worst-case linear time O(n). Goal: Select the ith smallest element of S = {a1, a2, ..., an}. Solution: Use the so called PRUNE-AND-SEARCH technique: Let x ∈ S, and partition S into three subsets. S1 = {aj |aj < x}, S2 = {aj |aj = x}, S3 = {aj |aj > x}. If |S1| > i, search ith smallest elements in S1 recursively, (prune S2 and S3 away). Else If |S1| + |S2| > i, then return x (the ith smallest element). Else search the (i − (|S1| + |S2|))th element in S3 recursively (prune S1 and S2 away). A question arises How to select x such that S1 and S3 are nearly equal in cardinality? Force an even search!!! 17 / 31
  • 29. Selection in worst-case linear time O(n). Goal: Select the ith smallest element of S = {a1, a2, ..., an}. Solution: Use the so called PRUNE-AND-SEARCH technique: Let x ∈ S, and partition S into three subsets. S1 = {aj |aj < x}, S2 = {aj |aj = x}, S3 = {aj |aj > x}. If |S1| > i, search ith smallest elements in S1 recursively, (prune S2 and S3 away). Else If |S1| + |S2| > i, then return x (the ith smallest element). Else search the (i − (|S1| + |S2|))th element in S3 recursively (prune S1 and S2 away). A question arises How to select x such that S1 and S3 are nearly equal in cardinality? Force an even search!!! 17 / 31
  • 30. The way to select x Divide elements into n 5 groups of 5 elements each and find the median of each one We cannot say anything about the order between elements, but between median an elements Thus, arrows go from less to greater!!! The Medians Elements<Median Elements>Median 18 / 31
  • 31. The way to select x Find the Median of the Medians Again the arrows indicate the order from greater to less The Medians Elements<Median Elements>Median x 19 / 31
  • 32. The way to select x We have (Here, again the worst case scenario!!!) At least 1 2 n 5 − 2 possible groups with 3 elements greater than x The Medians Elements<Median Elements>Median x THIS AND THIS 20 / 31
  • 33. The way to select x We have (Here, again the worst case scenario!!!) At least 1 2 n 5 − 2 possible groups with 3 elements less than x The Medians Elements<Median Elements>Median x 21 / 31
  • 34. Thus, we have First 3 1 2 n 5 − 2 = 3n 10 − 6 elements < x Second 3 1 2 n 5 − 2 = 3n 10 − 6 elements > x 22 / 31
  • 35. Thus, we have First 3 1 2 n 5 − 2 = 3n 10 − 6 elements < x Second 3 1 2 n 5 − 2 = 3n 10 − 6 elements > x 22 / 31
  • 36. SELECT the ith element in n elements Proceed as follows: 1 Divide n elements into n 5 groups of 5 elements. 2 Find the median of each group. 3 Use SELECT recursively to find the median x of the above n 5 medians. 4 Partition n elements around x into S1, S2, and S3. 5 If |S1| > i, search ith smallest element in S1 recursively. Else If |S1| + |S2| > i, then return x (the ith smallest element). Else search (i − (|S1| + |S2|)) th in S3 recursively 23 / 31
  • 37. Analysis of SELECT Analysing complexity Steps 1,2 and 4 take O(n). Step 3 takes T( n 5 ). Let us see step 5: At least half of the medians in step 2 are greater or equal than x, thus at least 1 2 n 5 − 2 groups contribute 3 elements which are greater or equal than x. i.e., 3( 1 2 n 5 − 2) ≥ 3n 10 − 6. Similarly, the number of elements less or equal than x is also at least 3n 10 − 6. Thus, |S1| is at most 7n 10 + 6, similarly for |S3|. Thus SELECT in step 5 is called recursively on at most 7n 10 + 6 elements. 24 / 31
  • 38. Analysis of SELECT Analysing complexity Steps 1,2 and 4 take O(n). Step 3 takes T( n 5 ). Let us see step 5: At least half of the medians in step 2 are greater or equal than x, thus at least 1 2 n 5 − 2 groups contribute 3 elements which are greater or equal than x. i.e., 3( 1 2 n 5 − 2) ≥ 3n 10 − 6. Similarly, the number of elements less or equal than x is also at least 3n 10 − 6. Thus, |S1| is at most 7n 10 + 6, similarly for |S3|. Thus SELECT in step 5 is called recursively on at most 7n 10 + 6 elements. 24 / 31
  • 39. Final Recursion We have then T (n) =    O(1) if n < some value (i.e. 140) T n 5 + T 7n 10 + 6 + O (n) if n ≥ some value (i.e. 140) 25 / 31
  • 40. Solve recurrence by substitution Suppose T(n) ≤ cn for some c T(n) ≤ c n 5 + c(7n 10 + 6) + an T(n) ≤ 1 5 cn + c + 7 10 cn + 6c + an T(n) ≤ 9 10 cn + 7c + an T(n) ≤ cn + (− 1 10 cn + an + 7c) 26 / 31
  • 41. Solve recurrence by substitution. Suppose T(n) ≤ cn for some c T(n) ≤ c n 5 + c(7n 10 + 6) + an T(n) ≤ 1 5 cn + c + 7 10 cn + 6c + an T(n) ≤ 9 10 cn + 7c + an T(n) ≤ cn + (− 1 10 cn + an + 7c) T(n) is at most cn If − 1 10 cn + an + 7c < 0. i.e., c ≥ 10a( n n−70 ) when n > 70. So, select n = 140, and then c ≥ 20a. Note: n may not be 140, any integer greater than 70 is OK. 27 / 31
  • 42. Solve recurrence by substitution. Suppose T(n) ≤ cn for some c T(n) ≤ c n 5 + c(7n 10 + 6) + an T(n) ≤ 1 5 cn + c + 7 10 cn + 6c + an T(n) ≤ 9 10 cn + 7c + an T(n) ≤ cn + (− 1 10 cn + an + 7c) T(n) is at most cn If − 1 10 cn + an + 7c < 0. i.e., c ≥ 10a( n n−70 ) when n > 70. So, select n = 140, and then c ≥ 20a. Note: n may not be 140, any integer greater than 70 is OK. 27 / 31
  • 43. Solve recurrence by substitution. Suppose T(n) ≤ cn for some c T(n) ≤ c n 5 + c(7n 10 + 6) + an T(n) ≤ 1 5 cn + c + 7 10 cn + 6c + an T(n) ≤ 9 10 cn + 7c + an T(n) ≤ cn + (− 1 10 cn + an + 7c) T(n) is at most cn If − 1 10 cn + an + 7c < 0. i.e., c ≥ 10a( n n−70 ) when n > 70. So, select n = 140, and then c ≥ 20a. Note: n may not be 140, any integer greater than 70 is OK. 27 / 31
  • 44. Final Thoughts Why group of size 5? Using groups of 3 does not work, you can try and plug it into the claculations What about 7 or bigger odd number It does not change the computations, only by a constant 28 / 31
  • 45. Final Thoughts Why group of size 5? Using groups of 3 does not work, you can try and plug it into the claculations What about 7 or bigger odd number It does not change the computations, only by a constant 28 / 31
  • 46. Applications Computer Vision In the Median Filter: Given a neighborhood of n members of x, you need to find the median to substitute the value in x Statistical Applications Confidence intervals for quantiles A machine may run on 10 batteries and shuts off when the ith battery dies. You will want to know the distribution of X(i). In machine-learning if you want to convert a continuous valued feature into Boolean features by bucketing it, one common approach is to partition it by percentile so that the cardinality of each Boolean feature is somewhat similar. 29 / 31
  • 47. Applications Computer Vision In the Median Filter: Given a neighborhood of n members of x, you need to find the median to substitute the value in x Statistical Applications Confidence intervals for quantiles A machine may run on 10 batteries and shuts off when the ith battery dies. You will want to know the distribution of X(i). In machine-learning if you want to convert a continuous valued feature into Boolean features by bucketing it, one common approach is to partition it by percentile so that the cardinality of each Boolean feature is somewhat similar. 29 / 31
  • 48. Summary. The ith order statistic of n elements S = {a1, a2, ..., an}:ith smallest elements: Minimum and maximum. Median, lower median, upper median. Selection in expected average linear time Worst case running time PRUNE-AND-SEARCH Selection in worst-case linear time The fast randomized version is due to Hoare. It is still unknown exactly how many comparisons are needed to determine the median. 30 / 31
  • 49. Summary. The ith order statistic of n elements S = {a1, a2, ..., an}:ith smallest elements: Minimum and maximum. Median, lower median, upper median. Selection in expected average linear time Worst case running time PRUNE-AND-SEARCH Selection in worst-case linear time The fast randomized version is due to Hoare. It is still unknown exactly how many comparisons are needed to determine the median. 30 / 31
  • 50. Summary. The ith order statistic of n elements S = {a1, a2, ..., an}:ith smallest elements: Minimum and maximum. Median, lower median, upper median. Selection in expected average linear time Worst case running time PRUNE-AND-SEARCH Selection in worst-case linear time The fast randomized version is due to Hoare. It is still unknown exactly how many comparisons are needed to determine the median. 30 / 31
  • 51. Exercises From Cormen’s Book Chapter 9 9.1-1 9.2-3 9.3-4 9.3-8 9.2 31 / 31