Biorąc pod uwagę listę punktów 2d, znajdź punkt najbliższy wszystkim innym punktom
Input: list of 2d points (x,y) where x and y are integers.
Distance: distance is defined as the Manhattan distance.
ie:
def dist(p1,p2)
return abs(p1.x-p2.x) + abs(p1.y - p2.y)
Jaki jest skuteczny algorytm, aby znaleźć punkt najbliższy wszystkim innym punktom.
Mogę tylko myśleć o brutalnym rozwiązaniu O (n ^ 2):
minDist=inf
bestPoint = null
for p1 in points:
dist = 0
for p2 in points:
dist+=distance(p1,p2)
minDist = min(dist,minDist)
bestPoint = argmin(p1, bestPoint)
zasadniczo spójrz na każdą parę punktów.