0% found this document useful (0 votes)
17 views6 pages

MVVSSwaroop Assignment2

Uploaded by

mounishch9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
17 views6 pages

MVVSSwaroop Assignment2

Uploaded by

mounishch9
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 6

Assignment – 2

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).

Key Operations and Their Complexities


Euclidean Distance Calculation:
 Complexity: O(1)
 Reason: Calculating the distance between two points involves basic
arithmetic operations.
Closest Pair in the Strip:
1. Sorting the Strip by y-coordinates:
 Complexity: O(n logn)
 Reason: Standard sorting algorithms (like std::sort) have O(n logn)
complexity.
2. Finding the Closest Pair in the Strip:
 Complexity: O(n)
 Reason: Each point is compared with the next 7 points, leading to
O(7n)=O(n) comparisons.
Recursive Closest Pair:
1. Base Case (3 or fewer points):
 Complexity: O(1)
 Reason: Directly comparing a constant number of pairs is done in constant
time.
2. Dividing Points into Two Halves:
 Complexity: O(n)
 Reason: Splitting the points into two halves and partitioning by y-
coordinate can be done in linear time.
3. Recursive Calls:
 Complexity: 2T(n/2)
 Reason: Each half of the points is processed recursively.
4. Combining Results:
 Complexity: O(n)
 Reason: Checking the strip around the median line involves linear
operations.
Initialization:
1. Sorting Points by x and y coordinates:
 Complexity: O(n logn)
 Reason: Initial sorting of the points is performed once using standard
sorting algorithms.

Overall Time Complexity


 The overall time complexity of the algorithm is derived from the recurrence
relation:
T(n)=2T(n/2)+O(n)
 This is a classic recurrence relation for divide and conquer algorithms and
solves to:
T(n)=O(n logn)
Here's a breakdown of the key components and their complexities:
o Sorting by x and y coordinates: O(n logn)
o Dividing points and recursive calls: 2T(n/2)
o Combining step (strip processing): O(n)
The O(n logn) sorting and the O(n) combining step are both dominated by the
2T(n/2) recursive structure, resulting in an overall O(n logn) complexity.

CODE:
#include <iostream>
#include <vector>
#include <algorithm>
#include <cmath>
#include <limits>

using namespace

std; struct Point {


double x, y;
};

// Structure to hold the result


struct ClosestPairResult {
Point point1;
Point point2;
double distance;
};

// Calculate Euclidean distance between two points - O(1)


double euclideanDistance(const Point& p1, const Point&
p2) {
return sqrt((p1.x - p2.x) * (p1.x - p2.x) + (p1.y - p2.y) *
(p1.y - p2.y)); // O(1)
}

// Find the closest pair in the strip - O(7n) which is O(n)


ClosestPairResult closestPairInStrip(vector<Point>& strip, double
d) {
double min_dist = d; // O(1)
Point p1, p2;

// Sort points in strip by their y coordinates - O(n log n)


sort(strip.begin(), strip.end(), [](const Point& p1, const Point&
p2) {
return p1.y < p2.y; // O(1) per comparison
});
for (size_t j = i + 1; j < strip.size() && (strip[j].y -
strip[i].y) < min_dist; ++j) { // O(7) per point
double dist = euclideanDistance(strip[i], strip[j]); //
O(1) if (dist < min_dist) { // O(1)
min_dist = dist; //
O(1) p1 = strip[i];
// O(1) p2 =
strip[j]; // O(1)
}
}
}

return {p1, p2, min_dist}; // O(1)


}

// Recursive function to find the closest pair - O(n log n)


ClosestPairResult closestPairRec(vector<Point>& Px, vector<Point>&
Py) {
if (Px.size() <= 3) { // O(1)
double min_dist = numeric_limits<double>::max(); //
O(1) Point p1, p2;
for (size_t i = 0; i < Px.size(); ++i) { // O(1)
for (size_t j = i + 1; j < Px.size(); ++j) { // O(1)
double dist = euclideanDistance(Px[i], Px[j]); //
O(1) if (dist < min_dist) { // O(1)
min_dist = dist; //
O(1) p1 = Px[i]; //
O(1)
p2 = Px[j]; // O(1)
}
}
}
return {p1, p2, min_dist}; // O(1)
}

size_t mid = Px.size() / 2; //


O(1) Point midPoint = Px[mid];
// O(1)

vector<Point> Qx(Px.begin(), Px.begin() + mid); // O(n/2)


vector<Point> Rx(Px.begin() + mid, Px.end()); // O(n/2)

vector<Point> Qy, Ry; // O(1)


for (const auto& point : Py) { //
O(n) if (point.x <= midPoint.x)
{ // O(1)
Qy.push_back(point); // O(1)
} else {
Ry.push_back(point); // O(1)
}
}

ClosestPairResult leftResult = closestPairRec(Qx, Qy); // O(n log n)


ClosestPairResult rightResult = closestPairRec(Rx, Ry); // O(n log
n)

ClosestPairResult minResult = (leftResult.distance <


rightResult.distance)
? leftResult : rightResult; // O(1)

vector<Point> strip; // O(1)


for (const auto& point : Py) { // O(n)
if (fabs(point.x - midPoint.x) < minResult.distance) {
// O(1) strip.push_back(point); // O(1)
}
}

ClosestPairResult stripResult = closestPairInStrip(strip,


minResult.distance); // O(n)

return (stripResult.distance < minResult.distance) ?


stripResult : minResult; // O(1)
}

// Function to initialize the process - O(n log n)


ClosestPairResult closestPair(vector<Point>& points) {
vector<Point> Px(points), Py(points); // O(n)
sort(Px.begin(), Px.end(), [](const Point& p1, const Point&
p2) { return p1.x < p2.x; // O(1) per comparison
}); // O(n log n)
sort(Py.begin(), Py.end(), [](const Point& p1, const Point&
p2) { return p1.y < p2.y; // O(1) per comparison
}); // O(n log n)

return closestPairRec(Px, Py); // O(n log n)


}

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)
}

You might also like