开发者

Function to calculate smallest set of zip codes in which all zip codes are within x miles

开发者 https://www.devze.com 2023-04-09 05:38 出处:网络
I need a function (in any language but preferably a script) that can take an array of objects (lets say zipcodes) with latitude/longitude coordinates and return the smallest subset in which all the el

I need a function (in any language but preferably a script) that can take an array of objects (lets say zipcodes) with latitude/longitude coordinates and return the smallest subset in which all the elements of the origin开发者_如何学Cal array are within x (lets say 20) miles of at least 1 member of the subset.


Here is a greedy algorithm to get you started.

  1. Start with an empty result set R and let S be the set of all zipcodes.
  2. For each zipcode Zn in S, calculate the set Vn of zipcodes which are within 20 miles of Zn.
  3. Find the set Vmax with the most elements - Add the corresponding zipcode Zmax into the result set R, and remove all the elements of Vmax from S.
  4. With the remaining elements in S, repeat from step 2 until S is empty. Then the final set is R.
0

精彩评论

暂无评论...
验证码 换一张
取 消