Preview Selection Problem

3/14/2017
Preview

Selection Problems




Minimum
Maximum
Minimum and Maximum
kth Element (General Selection Problem)
COSC 320 - Spring 2017
Slide courtesy of Dr. Sang-Eon Park
1
Selection Problem




In computer science, a selection algorithm is
an algorithm for finding the kth smallest number
in a list or array (such a number is called the kth
order statistic).
This includes the special cases for finding the
minimum, maximum, and median elements.
Selection algorithms with linear expected running
time exist.
Selection is a sub-problem of more complex
problems like the nearest neighbor and shortest
path problems.
COSC 320 - Spring 2017
Slide
courtesy of Dr. Sang-Eon Park
2
1
3/14/2017
Selection Problem

We can formally specify the selection problem as
follows:



Input: A[n] – a set of n (distinct) numbers, and an
integer i, where 1 ≤ i ≤ n.
Output: x  A, where x is the kth largest number in A.
We can solve the selection problem in Ο(𝑛𝑙𝑜𝑔2 𝑛)
time by sorting and returning the ith element. But
we can improve on this running time!
COSC 320 - Spring 2017
Slide
courtesy of Dr. Sang-Eon Park
3
Selection Problem
(Minimum or Maximum)

The Minimum (or Maximum) selection
problem is a special case of the general
selection problem


Input: A[n] – a set of n (distinct) numbers and
an integer 1 (or n).
Output: The element x  A that is the smallest
(or largest) number in A.
COSC 320 - Spring 2017
Slide
courtesy of Dr. Sang-Eon Park
4
2
3/14/2017
Selection Problem
(Minimum or Maximum)
Minimum(A) {
Min = A[0];
for j = 1 to |A|-1
if Min > A[j]
Min = A[j];
return Min;
Maximum(A) {
Max = A[0];
for j = 1 to |A|-1
if Max < A[j]
Max = A[j];
return Max;
}
}
The running time of Minimum and Maximum are both O(n)
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
5
Selection Problem
(Minimum or Maximum)

In some applications, we may need to find both
the minimum and the maximum of a set of n
elements.
E.g., a graphics program may need to scale (x, y)
coordinate data to fit onto a rectangular display screen
of a particular size. The program must determine both
the maximum and minimum value of each coordinate.

One simple way to determine the maximum and
minimum is to call two functions sequentially to
separately determine the minimum and the
maximum.
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
6
3
3/14/2017
Selection Problem
(Minimum or Maximum)
Both Minimum and Maximum
Min_Max (A)
{
Min = Minimum (A);
Max = Maximum (A);
Return (Min, Max)
}
Running time : (n – 1) + (n – 1) = 2n – 2 comparisons
Is there a better algorithm?
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
7
Min_Max(A) {
if A[0] > A[1] {
Min = A[1];
Max = A[0];
}
else {
Min = A[0];
Max = A[1];
}
for j = 1 to |A|/2 – 1 {
if A[2j ] > A[2j +1] {
Mn = A[2j + 1];
Mx = A[2j];
} else {
Mn = A[2j];
Mx = A[2j +1];
}
if Min > Mn
Min = Mn;
if Max < Mx
Max = Mx;
}
return (Max, Min);
}
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
8
4
3/14/2017
Min_Max(A) {
if A[0] > A[1] {
Min = A[1];
Max = A[0];
}
else {
Min = A[0];
Max = A[1];
}
for j = 1 to |A|/2 – 1 {
if A[2j ] > A[2j +1] {
Mn = A[2j + 1];
Mx = A[2j];
} else {
Mn = A[2j];
Mx = A[2j +1];
}
if Min > Mn
Min = Mn;
if Max < Mx
Max = Mx;
}
return (Max, Min);
}



This algorithm works well
without any modification
when the size of A is an even
number.
If the size of A is an odd
number, 2 more comparisons
are needed to be get the
correct min and max
The # of comparisons for this
|𝑛|
algorithm is 3 ×
when n is
|𝑛|
2
even (or 3 × + 1 when n is
2
odd) to find out maximum
and minimum.
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
9
Selection Problem
(Find kth Element)


The general selection problem is to find the kth
element in a list.
In computer science, the general selection
problem is for finding the kth smallest number
in a list (such a number is called the kth order
statistic).
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
10
5
3/14/2017
Selection Problem
(Find kth Element: Selection by sorting)

One simple way of finding the kth element is to
sort the list and return the kth element. This can
be done in Ο 𝑛𝑙𝑜𝑔2 𝑛 time, e.g…
Select_ith(A, i)
{
MergeSort(A)
Return (A[i-1])
}

Is there better algorithm?
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
11
Selection Problem
(Find kth Element: Nonlinear general selection algorithm)

Using the same ideas used in the selection sort algorithm,
we can construct a simple, but inefficient general algorithm
for finding the kth smallest or kth largest item in a list,
requiring O(kn) time, which is effective when k is small.
Select(A, k) {
for i from 1 to k {
minIndex = i
minValue = list[i]
for j from i+1 to n {
if list[j] < minValue {
minIndex = j
minValue = list[j]
}
}
if i ≠ minIndex
swap (list[i], list[minIndex])
}
return list[k]
}
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
12
6
3/14/2017
Selection Problem
(Find kth Element: in expected linear time)




The general selection problem appears more
difficult than the simple problem of finding a
minimum or maximum.
There are asymptotically linear time running
algorithms for general selection problem by using
divide and conquer approach.
Randomized select use the quicksort idea.
In quicksort, the input is partitioned into two
parts and quicksort is called for both parts. But in
randomized select we only need to work on one
part.
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
13
Selection Problem
(Find kth Element: in “expected” linear time)
Partition(A, p, r) {
x = A[r]; // A[r] used as a pivot
i = p - 1
for j = p to r -1 {
if A[j] ≤ x {
i = i + 1;
Swap(A[i], A[j]);
}
}
Swap(A[i + 1], A[r]);
return i + 1;
}
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
14
7
3/14/2017
Selection Problem
(Find kth Element: in “expected” linear time)
Randomized_Partition(A, p, r) {
i = RANDOM(p, r);
Swap(A[r], A[i]);
return Partition(A, p, r);
}
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
15
Selection Problem
(Find kth Element: in “expected” linear time)
Randomized_Select(A, p, r, i) //returns ith smallest element
{
if (p == r) then // only one element
return A[p]
q = Randomized_Partition (A, p, r)
k = q – p + 1
if (i == k) then // the pivot value is the answer
return A[q]
else if (i < k) then
return Randomized_Select(A, p, q-1, i)
else
return Randomized_Select(A, q+1, r, i-k)
}
COSC 320 – Spring 2017
Slide courtesy of Dr. Sang-Eon Park
16
8
3/14/2017
Selection Problem
(Find kth Element: in “expected” linear time)
p
p+1
…
r
q = Randomized Partition (A, p, r)
p
p+1
…
q-1
q
q+1
Randomized_Select(A, p, q-1, i)
r
Randomized_Select(A, q+1, r, i-k)
COSC 320 - Spring 2017
Slide
courtesy of Dr. Sang-Eon Park
17
Selection Problem
(Find kth Element: in “expected” linear time)
Running time of Randomized_Select

Best case: Partition always divides the list evenly
𝑇 𝑛 =𝑇

𝑛
2
+ 𝑛 = 𝛰(𝑛)
Worst case: Partition always divides the list into a list
with 1 element and a second with n-1 elements
𝑇 𝑛 = 𝑇 𝑛 − 1 + 𝑛 = 𝑂(𝑛2)

Average case: Partition divides the list into n-k and k-1
elements
𝑇 𝑛 = 𝑇 max 𝑛 − 𝑘, 𝑘 − 1
+ 𝑛 = 𝑂(𝑛)
COSC 320 - Spring 2017
Slide
courtesy of Dr. Sang-Eon Park
18
9