Sunteți pe pagina 1din 11

CLOSEST PAIR PROBLEM

INTRODUCTION:

The closest pair of points problem or closest pair problem is a problem of computational
geometry: given n points in metric space, find a pair of points with the smallest distance between
them. The closest pair problem for points in the Euclidean plane was among the first geometric
problems that were treated at the origins of the systematic study of the computational
complexity of geometric algorithms.

A naive algorithm of finding distances between all pairs of points in a space of dimension d and
selecting the minimum requires O(n2) time. It turns out that the problem may be solved
in O(n log n) time in a Euclidean space or Lp space of fixed dimension d. In the algebraic
decision tree model of computation, the O(n log n) algorithm is optimal, by a reduction from
the element uniqueness problem. In the computational model that assumes that the floor
function is computable in constant time the problem can be solved in O(n log log n) time. If we
allow randomization to be used together with the floor function, the problem can be solved
in O(n) time

Closest pair of points shown in red


CLOSEST PAIR PROBLEM USING BRUTE FORCE METHOD

INTRODUCTION:

The closest-pair problem calls for finding the two closest points in a set of n
points. It is the simplest of a variety of problems in computational geometry that
deals with proximity of points in the plane or higher-dimensional spaces. Points
in question can represent such physical objects as airplanes or post offices as well
as database records, statistical samples, DNA sequences, and so on. An air-traffic
controller might be interested in two closest planes as the most probable collision
candidates.Aregional postal service manager might need a solution to the closestpair
problem to find candidate post-office locations to be closed.

One of the important applications of the closest-pair problem is cluster analysis


in statistics. Based on n data points, hierarchical cluster analysis seeks to organize
them in a hierarchy of clusters based on some similarity metric. For numerical
data, this metric is usually the Euclidean distance; for text and other nonnumerical
data, metrics such as the Hamming distance (see Problem 5 in this section’s exercises)
are used. A bottom-up algorithm begins with each element as a separate
cluster and merges them into successively larger clusters by combining the closest
pair of clusters.

For simplicity, we consider the two-dimensional case of the closest-pair problem.


We assume that the points in question are specified in a standard fashion by
their (x, y) Cartesian coordinates and that the distance between two points pi(xi,
yi) and pj(xj, yj ) is the standard Euclidean distance

d(pi, pj ) =sqrt((xi− xj )2 + (yi− yj )2).


The brute-force approach to solving this problem leads to the following obvious
algorithm: compute the distance between each pair of distinct points and
find a pair with the smallest distance. Of course, we do not want to compute the
distance between the same pair of points twice. To avoid doing so, we consider
only the pairs of points (pi, pj ) for which i < j

ALGORITHM :

ALGORITHM BruteForceClosestPair(P )
//Finds distance between two closest points in the plane by brute force
//Input: A list P of n (n ≥ 2) points p1(x1, y1), . . . , pn(xn, yn)
//Output: The distance between the closest pair of points
dmin ← ∞
for i ← 1 to n-1 do
for j ← i+1 to n do
d ← sqrt((xi-xj)2 + (yi-yj)2)
if d < dmin then
dmin ← d; index1 ← i; index2 ← j
return index1, index2

The basic operation of the algorithm is computing the square root. In the age
of electronic calculators with a square-root button, one might be led to believe
that computing the square root is as simple an operation as, say, addition or
multiplication. Of course, it is not. For starters, even for most integers, square roots
are irrational numbers that therefore can be found only approximately. Moreover,
computing such approximations is not a trivial matter. But, in fact, computing
square roots in the loop can be avoided! (Can you think how?) The trick is to

realize that we can simply ignore the square-root function and compare the values
(xi− xj )2 + (yi− yj )2 themselves. We can do this because the smaller a number of
which we take the square root, the smaller its square root, or, as mathematicians
say, the square-root function is strictly increasing.

TIME COMPLEXITY:

Then the basic operation of the algorithm will be squaring a number. The
number of times it will be executed can be computed as follows:

C(n) = ∑i=1n-1 ∑j=i+1n 2 = 2∑j=i+1n (n-i) = 2n(n-1)/2 ε Θ(n2)

Of course, speeding up the innermost loop of the algorithm could only decrease
the algorithm’s running time by a constant factor (see Problem 1 in this
section’s exercises), but it cannot improve its asymptotic efficiency class. In Chapter
5, we discuss a linearithmic algorithm for this problem, which is based on a
more sophisticated design technique
CLOSEST PAIR PROBLEM BY DIVIDE AND CONQUER

INTRODUCTION:

We are given an array of n points in the plane, and the problem is to find out the closest pair of
points in the array. This problem arises in a number of applications. For example, in air-traffic
control, you may want to monitor planes that come too close together, since this may indicate a
possible collision. Recall the following formula for distance between two points p and q.

The Brute force solution is O(n^2), compute the distance between each pair and return the
smallest. We can calculate the smallest distance in O(nLogn) time using Divide and Conquer
strategy. In this post, a O(n x (Logn)^2) approach is discussed. We will be discussing a
O(nLogn) approach in a separate post.

ALGORITHM:

Following are the detailed steps of a O(n (Logn)^2) algortihm.


Input: An array of n points P[]
Output: The smallest distance between two points in the given array.
As a pre-processing step, input array is sorted according to x coordinates.

1) Find the middle point in the sorted array, we can take P[n/2] as middle point.
2) Divide the given array in two halves. The first subarray contains points from P[0] to P[n/2].
The second subarray contains points from P[n/2+1] to P[n-1].
3) Recursively find the smallest distances in both subarrays. Let the distances be dl and dr. Find
the minimum of dl and dr. Let the minimum be d.
4) From above 3 steps, we have an upper bound d of minimum distance. Now we need to
consider the pairs such that one point in pair is from left half and other is from right half.
Consider the vertical line passing through passing through P[n/2] and find all points whose x
coordinate is closer than d to the middle vertical line. Build an array strip[] of all such points.
5) Sort the array strip[] according to y coordinates. This step is O(nLogn). It can be optimized to
O(n) by recursively sorting and merging.
6) Find the smallest distance in strip[]. This is tricky. From first look, it seems to be a O(n^2)
step, but it is actually O(n). It can be proved geometrically that for every point in strip, we only
need to check at most 7 points after it (note that strip is sorted according to Y coordinate).
See this for more analysis.
7) Finally return the minimum of d and distance calculated in above step (step 6)

TIME COMPLEXITY:

Let Time complexity of above algorithm be T(n). Let us assume that we use a O(nLogn) sorting
algorithm. The above algorithm divides all points in two sets and recursively calls for two sets.
After dividing, it finds the strip in O(n) time, sorts the strip in O(nLogn) time and finally finds
the closest points in strip in O(n) time. So T(n) can expressed as follows
T(n) = 2T(n/2) + O(n) + O(nLogn) + O(n)
T(n) = 2T(n/2) + O(nLogn)
T(n) = T(n x Logn x Logn)

NOTES:

1) Time complexity can be improved to O(nLogn) by optimizing step 5 of the above algorithm.
We will soon be discussing the optimized solution in a separate post.
2) The code finds smallest distance. It can be easily modified to find the points with smallest
distance.
3) The code uses quick sort which can be O(n^2) in worst case. To have the upper bound as O(n
(Logn)^2), a O(nLogn) sorting algorithm like merge sort or heap sort can be used
CONTENTS:

*INTRODUCTION

*CLOSEST PAIR PROBLEM USING BRUTE FORCE METHOD

*INTRODUCTION

*ALGORITHM

*TIME COMPLEXITY

*CLOSEST PAIR PROBLEM BY DIVIDE AND CONQUER

*INTRODUCTION

*ALGORITHM

*TIME COMPLEXITY

S-ar putea să vă placă și