📅  最后修改于: 2023-12-03 15:26:27.967000             🧑  作者: Mango
最接近的点对问题是计算平面上最接近的两个点之间的距离。O(n^2)的算法需要两重循环枚举点对,但是通过分治法和归并排序,可以将其优化至O(nlogn)。
import sys
import math
class Point:
def __init__(self, x, y):
self.x = x
self.y = y
def distance(a, b):
return math.sqrt((a.x - b.x) ** 2 + (a.y - b.y) ** 2)
def closest_pair(points):
points.sort(key=lambda point: point.x)
return _closest_pair(points)
def _closest_pair(points):
n = len(points)
if n <= 1:
return sys.float_info.max
mid = n // 2
left = points[:mid]
right = points[mid:]
d = min(_closest_pair(left), _closest_pair(right))
split_points = []
for point in points:
if abs(point.x - points[mid].x) < d:
split_points.append(point)
split_points.sort(key=lambda point: point.y)
min_dist = d
for i in range(len(split_points)):
for j in range(i + 1, len(split_points)):
if split_points[j].y - split_points[i].y >= d:
break
dist = distance(split_points[i], split_points[j])
if dist < min_dist:
min_dist = dist
return min_dist
这个算法利用了分治法和归并排序算法,时间复杂度为O(nlogn)。