next up previous
Next: Up: Previous:

Closest Pair Of Points

Brute force solution: n2



Better solution:

Closest-Pair(P, X, Y)
$\;\;\;\;\;$if $\vert P\vert \leq 3$
$\;\;\;\;\;$then compute closest pair and return $\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$; O(1)
$\;\;\;\;\;$divide points evenly along x axis at x = l into PL(XL,YL) and PR(XR,XR)
$\;\;\;\;\;$dL = distance(Closest-Pair(PL,XL,YL))
$\;\;\;\;\;$dR = distance(Closest-Pair(PR,XR,YR))
$\;\;\;\;\;$d = min(dL,dR)
$\;\;\;\;\;$foreach point p in (l - d) $\leq$ x $\leq$ (l + d)
$\;\;\;\;\;$ $\;\;\;\;\;$check 7 points p' closest to p by y-coordinate
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$d' = distance(p,p')
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$if d' < d
$\;\;\;\;\;$ $\;\;\;\;\;$ $\;\;\;\;\;$then retain new closest pair
$\;\;\;\;\;$return closest pair


next up previous
Next: Up: Previous: