0% found this document useful (0 votes)
33 views

Closest Pair of Points

The document describes an algorithm for finding the closest pair of points among a set of n points in 2 dimensions. It uses a divide and conquer approach, recursively splitting the point set into halves and computing the closest pairs within each half. It also efficiently finds the closest pair that could exist across the splitting line by only considering points within a strip defined by the closest distances in each half. This results in an overall runtime of O(n log n).

Uploaded by

HARSH PANDEY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
33 views

Closest Pair of Points

The document describes an algorithm for finding the closest pair of points among a set of n points in 2 dimensions. It uses a divide and conquer approach, recursively splitting the point set into halves and computing the closest pairs within each half. It also efficiently finds the closest pair that could exist across the splitting line by only considering points within a strip defined by the closest distances in each half. This results in an overall runtime of O(n log n).

Uploaded by

HARSH PANDEY
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 28

Closest Pair

Prepared by Pawan K. Mishra


P12. Closest pair of n points

I/P: Given P={p1, p2,…, pn} a set of n points. Each pi=(xi,yi).


O/P: Find the closest pair of points.

Prepared by Pawan K. Mishra


P12. Closest pair of n points

I/P: Given P={p1, p2,…, pn} a set of n points. Each pi=(xi,yi).


O/P: Find the closest pair of points.

Prepared by Pawan K. Mishra


Closest pair of n points

I/P: Given P={p1, p2,…, pn} a set of n points. Each pi=(xi,yi).


O/P: Find the 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.

Prepared by Pawan K. Mishra


Let’s do the problem in 1-Dimension
y-coordinate of all points are same.
O(nlogn)
1.Sort each point with respect to x-coordinate.

2. For each point pi , check two neighbours pi-1 and pi+1.


3. Do it for all points and return the minimum. O(n)

Prepared by Pawan K. Mishra


Can we do via divide and conquer?
Here input is not sorted….. Means the point are not sorted with
respect to x-coordinate…. ??

How to apply D&C??

Intuition is to divide points set in equal halves ??– But how??

We need median to partition in two halves….??

Prepared by Pawan K. Mishra


Lets apply the unknown algorithm to partition point set in two halves.

P split in two halves PL and PR.

Recurse on both halves.

PL contains the median point (W.L.O.G)…

Prepared by Pawan K. Mishra


P

complexity??
PL PR

Suppose the closest pair in PL is dL s=min (dL, dR)


the closest pair in PR is dR
dL s dR

Prepared by Pawan K. Mishra


In 2D

Prepared by Pawan K. Mishra


In 2D

dL
s=min(dL, dR) dR

Prepared by Pawan K. Mishra


In 2D

s s

dL
s=min(dL, dR) dR

Prepared by Pawan K. Mishra


In 2D

s s Can this point be used ?


No, so check points in strip.

dL
s=min(dL, dR) dR

Prepared by Pawan K. Mishra


In 2D

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

Prepared by Pawan K. Mishra


In 2D

s s
Do we need to
compare this pair?
NO!!

dL dR
s=min(dL, dR)

Prepared by Pawan K. Mishra


In 2D

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

Prepared by Pawan K. Mishra


s/sqrt(2)

s/2
so, in a box, can have one point.
s/2

Prepared by Pawan K. Mishra


for green point, do we need
to check these red points ?
NO? Why?
s

Prepared by Pawan K. Mishra


for green point, we need
to check these red points ?
Yes? Why?
s 7 points

Prepared by Pawan K. Mishra


how to find seven points for each point?

Prepared by Pawan K. Mishra


O(nlog2n)
for green point, we need
to check these red points ?
Yes? Why?
s 7 points

so, for each point, we need to


check at most next 7 points.
T(n)=2T(n/2)+nlogn To get next 7, need to sort

Prepared by Pawan K. Mishra


2 dimensions, divide and conquer

• Split set of points into two halves by vertical line

• Recursively compute closest pair in left and right half

• Need to then compute closest pairs across separating line

• How can we do this efficiently?

Prepared by Pawan K. Mishra


Sorting points by x and y

Given n points P = {p1,p2,…,pn}, compute


➢ Px : P sorted by x coordinate
➢ Py : P sorted by y coordinate

Divide P by vertical line into equal


size sets Q and R.

Need to efficiently compute


Qx, Qy, Rx, Ry
22
Prepared by Pawan K. Mishra
Sorting points by x and y

• Need to efficiently compute


Qx, Qy, Rx, Ry
➢Qx is first half of Px
➢ Rx is second half of Px
• When splitting Px,
note the largest x coordinate in Q, xQ
• Separate Py as Qy, Ry by checking x
coordinate with xQ. xQ

• All O(n)
23
Prepared by Pawan K. Mishra
2 dimensions, divide and conquer

• Basic recursive call is ClosestPair(Px, Py)

• Set up recursive calls ClosestPair(Qx, Qy)


and ClosestPair(Rx, Ry) for left and right half of P in
time O(n)
• How to combine these recursive solutions?

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

Construct (Qx, Qy, Rx, Ry)


(dQ ,q1, q2) = ClosestPair(Qx, Qy)
(dR ,r1, r2) = ClosestPair(Rx, Ry)
Construct Sy and scan to find (dS,s1, s2)

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)

T(n) = 2T(n/2) +O(n)=O(n log n)

28
Prepared by Pawan K. Mishra

You might also like