Sie sind auf Seite 1von 11

Divide-and-Conquer

Finding a closest pair of points in R2

R. Inkulu
http://www.iitg.ac.in/rinkulu/

(Finding a closest pair of points in R2 )

1 / 11

Problem description
Find a closest pair of points in the given set S of n points in R2 .
111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

"
111
000
000 p
111
000
111
111
000

111
000
000
111

111
000
000
111
111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

000
111
111
000
000
111
000
111
000
111
000
111

closest pair of points are p and p

for convenience, assume that no two points in S are having the same x- or y-coordi
(Finding a closest pair of points in R2 )

2 / 11

Brute-force algorithm
Computing the distance between every two points in S, and choosing a
pair with the smallest distance: O(n2 ) time.
111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111 p"
000
000
111
000
111
000
p111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111
111
000
000
111

(Finding a closest pair of points in R2 )

111
000
000
111
111
000
000
111

111
000
000
111

3 / 11

Divide-Conquer-Combine

recursively do the following:


(1) partition points into left and right halves
(2) conquer the sub-problems in both the halves
(3) combine the solutions for halves critical

(Finding a closest pair of points in R2 )

4 / 11

Combining solutions
(i) closest pair is from left half of points
(ii) closest pair is from right half of points
(iii) closest pair is formed by one point from left half and another from right
half how to do in O(n)?
L

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

"
111
000
000 p
111
000
111
111
000

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111
111
000
000
111
(Finding a closest pair of points in R2 )

111
000
000
111
111
000
000
111

111
000
000
111
5 / 11

Vertical strip containing p and p


For the Case 3 to occur p and p must lie in the vertical strip of width 2d
that has the dividing line between L and R at its center.
L

111
000
000
111

111
000
000
111
111
000
000
111

111
000
000
111
000
p"111
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111
000
111
000 d
111

111
000
000
111

dR

111
000
000
111

111
000
000
111

111
000
000
111

111
000
000
111
111
000
000
111

111
000
000
111
d

(Finding a closest pair of points in R2 )

111
000
000
111

6 / 11

Horizontal strip containing p and p

00
11

00
11p
d

Euclidean distance between

(Finding a closest pair of points in R2 )

and p is not less than d

7 / 11

Bounding the number of candidate p s w.r.t. a p

1
d/sqrt(2)

d/2
d/2

d
d/2
p

Euclidean distance between red points is less than d: contradicting


d = min(dL , dR )

(Finding a closest pair of points in R2 )

8 / 11

Combine step detailed

(a) let S be the y-sorted list of points falling within the vertical band
for each point p, find the distance between p and each of the 7 points
above p in S
(b) among all such searches, let p and p be the point pair with the minimum
distance
then return min(dist(p , p ), dL , dR ) with the corresponding points

(Finding a closest pair of points in R2 )

9 / 11

Correctness

termination is ensured as each branch of the recursion terminates with at

most O(1) points


algorithm is correct: induction on the number of points

(Finding a closest pair of points in R2 )

10 / 11

Analysis: time complexity

initial sorting according to x-coordi: O(n lg n)


initial sorting according to y-coordi: O(n lg n)
T(n) = 2T(n/2) + O(n) when n > c; otherwise, T(n) is O(1)

i.e., T(n) = O(n lg n)


total: O(n lg n) time

(Finding a closest pair of points in R2 )

11 / 11

Das könnte Ihnen auch gefallen