Next: About this document ...
Up: CSL102: Special on-line examination
Previous: Q3: Divide and conquer
Given a set of points in a plane,
, consider
the problem of determining the closest pair of points; i.e., pairs
and
such that the distance between them
is
minimized.
Clearly the naive idea of computing the
distances and finding the
minimum gives an
algorithm. Here is an improvement using divide and
conquer.
- Find a value
such that half the points have
and half have
(roughly; you may sort the set on their
coordinates to
do this). On this basis split in to two groups
and
.
- Recursively compute the closest pair in
and in
. Let these
pairs be
and
, with distances
and
respectively. Let the smaller of these distances be
.
- It remains to be seen if there is a point in
and a point in
that are less than
distance apart from each other. To this end,
discard all points with
and
and sort the
remaining points in the central band by their
coordinates.
- Now, go through this sorted list, and for each point, compare its
distance to the seven subsequent points in the list (why?). Let
be the closest pair found this way.
- The answer is one of the three pairs
,
and
, whichever is closest.
How efficient is this algorithm? Write an imperative program based on the above idea.
You may write the program in any language of your choice. However it may
be simplest to complete
this Python program template.
Next: About this document ...
Up: CSL102: Special on-line examination
Previous: Q3: Divide and conquer
Subhashis Banerjee
2009-04-03