Finding the farthest point in one set from another set

前端 未结 6 1720
旧巷少年郎
旧巷少年郎 2021-02-06 12:05

My goal is a more efficient implementation of the algorithm posed in this question.

Consider two sets of points (in N-space. 3-space for the example case of RGB colorsp

6条回答
  •  不思量自难忘°
    2021-02-06 12:26

    First you need to find every element's nearest neighbor in the other set.

    To do this efficiently you need a nearest neighbor algorithm. Personally I would implement a kd-tree just because I've done it in the past in my algorithm class and it was fairly straightforward. Another viable alternative is an R-tree.

    Do this once for each element in the smallest set. (Add one element from the smallest to larger one and run the algorithm to find its nearest neighbor.)

    From this you should be able to get a list of nearest neighbors for each element.

    While finding the pairs of nearest neighbors, keep them in a sorted data structure which has a fast addition method and a fast getMax method, such as a heap, sorted by Euclidean distance.

    Then, once you're done simply ask the heap for the max.

    The run time for this breaks down as follows:

    N = size of smaller set
    M = size of the larger set

    • N * O(log M + 1) for all the kd-tree nearest neighbor checks.
    • N * O(1) for calculating the Euclidean distance before adding it to the heap.
    • N * O(log N) for adding the pairs into the heap.
    • O(1) to get the final answer :D

    So in the end the whole algorithm is O(N*log M).

    If you don't care about the order of each pair you can save a bit of time and space by only keeping the max found so far.

    *Disclaimer: This all assumes you won't be using an enormously high number of dimensions and that your elements follow a mostly random distribution.

提交回复
热议问题