Given two (large) sets of points, how can I efficiently find pairs that are nearest to each other?

后端 未结 4 2019
囚心锁ツ
囚心锁ツ 2021-02-01 22:39

I need to solve a computational problem that boils down to searching for reciprocally-nearest pairs of points between two sets. The problem goes something like this:

Giv

4条回答
  •  慢半拍i
    慢半拍i (楼主)
    2021-02-01 23:01

    Sorry for picking up a rather old thread but I just wanted to add a solution I've found in my textbook for an Algorithm Design class:

    There is a divide-and-conquer (think merge-sort) approach to solve this problem that should be O(n logn), I've only seen it for finding the shortest distance within one set of points but it should be easily adapted to require each pairing to consist of points from different sets.

    1. Sort all points according to X-value.
    2. Split the whole set in two equal parts.
    3. Recurse on each half and pick the minimal distance of the two (d)
    4. Find the right-most point (p) in the left half and check the distance for all points between p_x and p_x + d, if any of these distances are shorter than d that is the d to return, otherwise return d.

提交回复
热议问题