Closest Pair of Points
Closest Pair of Points
Naïve algorithm:
1. For each point p in P, check all points in P \ {p}.
2.Compute the minimum distances with respect to p, i.e., min_p
3. Repeat step 1 and 2 for all points in P,
4. Finally return min of all min_p’s.
complexity??
PL PR
dL
s=min(dL, dR) dR
s s
dL
s=min(dL, dR) dR
dL
s=min(dL, dR) dR
s s
So, we need to find the
closest pair in strip only.
naïve way: O(n2)
dL T(n)=T(n/2)+ O(n2)
s=min(dL, dR) dR
s s
Do we need to
compare this pair?
NO!!
dL dR
s=min(dL, dR)
s s
Do we need to
compare this pair?
NO!!
Let’s dissects the strip.
dL dR
s=min(dL, dR) s
s/2 2/2 s/2 s/2
s s
s/2
so, in a box, can have one point.
s/2
• All O(n)
23
Prepared by Pawan K. Mishra
2 dimensions, divide and conquer
24
Prepared by Pawan K. Mishra
Combining solutions
• Let dQ be closest distance in Q
and dR be closest distance in R
• Let d be min(dQ , dR)
• Only need to consider points
across the separator at most distance
d from separator
➢Any pair outside this strip
cannot be closest pair overall
25
Prepared by Pawan K. Mishra
Combining solutions
• From Qy, Ry, extract Sy,
points in d-band sorted by y coordinate
• Scan Sy from bottom to top,
comparing each point against next
7 points in Sy
• Linear scan
26
Prepared by Pawan K. Mishra
Algorithm
ClosestPair(Px,Py)
if (|Px| <= 3)
compute pairwise distances and
return the closest pair and distance
Return (dQ ,q1, q2) , (dR ,r1, r2) , (dS,s1, s2) depending on which among
(dQ ,dR, dS) is minimum
27
Prepared by Pawan K. Mishra
Analysis
• Computing ( Px, Py ) from P takes O(n log n) (before recursion call)
• Recursive algorithm
➢ Setting up (Qx, Qy, Rx, Ry) from ( Px, Py ) is O(n)
➢ Setting up Sy from Qy, Ry is O(n)
➢ Scanning Sy is O(n)
28
Prepared by Pawan K. Mishra