Finding the $p$ Closest Elements to Median of Set $M$ in $O(m)$ Time

In summary: But this interval will contain $2p$ elements.How can we find the $p$ nearest elements to the median? (Sweating)
  • #1
evinda
Gold Member
MHB
3,836
0
Hello! (Wave)
I want to describe an algorithm with time complexity $O(m)$ that, given a set $M$ with $m$ numbers and a positive integer $p \leq m$, returns the $p$ closest numbers to the median element of the set $M$.

How could we do this? (Thinking)
 
Last edited:
Technology news on Phys.org
  • #2
What could we do in order to find the $p$ closest numbers to the median element of the set $M$ ? Could you give me a hint? (Thinking)
 
  • #3
evinda said:
Hello! (Wave)
I want to describe an algorithm with time complexity $O(m)$ that, given a set $M$ with $m$ numbers and a positive integer $p \leq m$, returns the $p$ closest numbers to the median element of the set $M$.

How could we do this? (Thinking)

Complexity $O(m)$ algorithm operating on a list of size $m$ implies that you're only allowed to do a single pass of the list. Is this list of numbers pre-sorted or not?
 
  • #4
PvsNP said:
Complexity $O(m)$ algorithm operating on a list of size $m$ implies that you're only allowed to do a single pass of the list. Is this list of numbers pre-sorted or not?

No, I think that the algorithm should work for each case... (Thinking)
 
  • #5
evinda said:
No, I think that the algorithm should work for each case... (Thinking)

Without thinking about it in depth, I'm not sure that this is possible. Median is the "middle" value on a set of ranked values, and no sorting algorithm runs in $O(n)$ time.

EDIT: I'm wrong. It's possible to find the median in $O(n)$ time. See if this helps you: Median of medians - Wikipedia, the free encyclopedia
 
  • #6
Doing it like that:

Code:
low=m/2-(p-1)
high=(m-1)/2+(p-1)
Algorithm(A,begin,end,low,high){
  if (begin<end){
      k=medianofMedians(A,low,high)
      q=hoare(A,start,begin,end,k)
      if (q>low) Algorithm(A,begin,q-1,low,high)
      if (q<high) Algorithm(A,q+1,end,low,high)
   }
}

we will have the interval to which the $p$ elements we are looking for belong.
But this interval will contain $2p$ elements.
How can we find the $p$ nearest elements to the median? (Sweating)
 

1. Can you explain the concept of finding the closest elements to the median of a set?

The median of a set is the middle element when the elements are arranged in ascending or descending order. Finding the closest elements to the median means finding the elements that are closest in value to the median element.

2. Why is it important to find the closest elements to the median?

Finding the closest elements to the median can provide useful insights into the distribution of the data. It can also help in identifying outliers or extreme values in the set.

3. How is the time complexity of O(m) achieved in this algorithm?

The algorithm uses a modified version of Quickselect algorithm, which has an average time complexity of O(n), where n is the size of the set. By dividing the set into smaller subarrays and only considering the subarray containing the median, the time complexity is reduced to O(m).

4. Can this algorithm handle sets with duplicate elements?

Yes, the algorithm can handle sets with duplicate elements. In case of duplicate elements, the algorithm will return all the elements that are closest to the median.

5. Are there any limitations to this algorithm?

One limitation of this algorithm is that it requires the entire set to be stored in memory. If the set is too large to fit in memory, this algorithm may not be suitable. Additionally, the algorithm may not perform well if the set is highly skewed or has a large number of outliers.

Similar threads

  • Programming and Computer Science
Replies
27
Views
2K
  • Programming and Computer Science
Replies
6
Views
2K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
20
Views
4K
  • Programming and Computer Science
Replies
1
Views
1K
  • Programming and Computer Science
Replies
9
Views
2K
  • Programming and Computer Science
Replies
1
Views
1K
Replies
9
Views
1K
  • Programming and Computer Science
Replies
4
Views
2K
  • Programming and Computer Science
Replies
29
Views
1K
Back
Top