Create a fast algorithm for a "weighted" median

Posted by Hameer Abbasi on Programmers See other posts from Programmers or by Hameer Abbasi
Published on 2012-10-31T10:54:43Z Indexed on 2012/10/31 11:13 UTC
Read the original article Hit count: 266

Filed under:

Suppose we have a set S with k elements of 2-dimensional vectors, (x, n). What would be the most efficient algorithm to calculate the median of the weighted set?

By "weighted set", I mean that the number x has a weight n. Here is an example (inefficient due to sorting) algorithm, where Sx is the x-part, and Sn is the n-part. Assume that all co-ordinate pairs are already arranged in Sx, with the respective changes also being done in Sn, and the sum of n is sumN:

sum <= 0; i<= 0
while(sum < sumN)
  sum <= sum + Sn(i)
  ++i
if(sum > sumN/2)
  return Sx(i)
else
  return (Sx(i)*Sn(i) + Sx(i+1)*Sn(i+1))/(Sn(i) + Sn(i+1))

EDIT: Would this hold in two or more dimensions, if we were to calculate the median first in one dimension, then in another, with n being the sum along that dimension in the second pass?

© Programmers or respective owner

Related posts about algorithms