MVVSSwaroop Assignment2
MVVSSwaroop Assignment2
M V V S Swaroop (CB.EN.U4AIE22136)
Problem Definition
Finding the closest pair of points: Given a set of ‘n’ points in a 2D plane, find
the pair of points that has the minimum Euclidean distance between them. The
simplest way to solve this problem is to calculate the distance between every pair
of points and then find the pair with the smallest distance. This approach has a
time complexity of O(n^2).
The problem of finding the closest pair of points among ‘n’ points in the plane
can be efficiently solved using a divide-and-conquer approach. The algorithm,
originally developed by Shamos and Hoey in the early 1970s, achieves a time
complexity of O(nlogn).
Algorithm Outline
The algorithm is structured similarly to Merge sort, using the divide and conquer
paradigm:
Divide: Split the set of points into two halves.
Conquer: Find the closest pair of points in each half recursively.
Combine: Determine the closest pair of points where one point lies in the
left half and the other in the right half.
Detailed Steps
Preprocessing:
Sort the points by their x-coordinates to form a list P_x.
Sort the points by their y-coordinates to form a list P_y.
Recursive Division:
Divide the points into two halves: Q (left half) and R (right half). This
division is done using the median of the x-coordinates.
Recursively find the closest pairs in Q and R, denoted as δ_Q and δ_R
respectively.
Combining the Results:
The minimum distance found in the recursive calls is δ=min(δ_Q,δ_R).
Consider only the points that lie within δ distance from the dividing line L.
Let this set of points be S.
Sort S by their y-coordinates (if not already sorted).
Finding the Closest Pair Across the Divide:
For each point s in S, compute the distance to the next 15 points in S (since
there can be at most 6 points within δ distance in each half, a total of 12
points across both halves within 2δ distance).
Keep track of the minimum distance found in this step.
Return the Minimum Distance:
The final answer is the minimum of the distances found in the left half, right
half, and across the divide.
Key Points
Recursive Structure: The recursion ensures that each subproblem has half the
number of points, leading to a logarithmic depth of recursion.
Efficient Combination: By focusing only on points within δ distance from the
dividing line and limiting the number of distance calculations to a constant (15),
the combination step runs in linear time.
Sorting Steps: Initial sorting of points and maintaining sorted order in recursive
calls ensures that each division and combination step is efficient.
Analysis
The algorithm for finding the closest pair of points in a plane uses a combination
of divide and conquer and a clever use of sorting to achieve an overall time
complexity of O(n logn).
CODE:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>
using namespace
int main() {
vector<Point> points = {{2.1, 3.5}, {1.0, 4.0}, {4.5, 2.0}, {2.0,
1.0},
{5.0, 3.0}};
ClosestPairResult result = closestPair(points); // O(n log n)
cout << "The closest pair of points are: (" << result.point1.x << ",
" << result.point1.y << ") and ("
<< result.point2.x << ", " << result.point2.y << ")" << endl;
// O(1) cout << "The smallest distance is: " << result.distance <<
endl; // O(1) return 0; // O(1)
}