Skip to content

Add Divide and Conquer: Closest Pair of Points algorithm #2969

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
88 changes: 88 additions & 0 deletions divide_and_conquer/closest_pair_of_points.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
#include <bits/stdc++.h>
using namespace std;

// Structure to represent a 2D point
struct Point {
int x, y;
};

// Function to compare points based on X coordinate
bool compareX(Point a, Point b) {
return a.x < b.x;
}

// Function to compare points based on Y coordinate
bool compareY(Point a, Point b) {
return a.y < b.y;
}

// Utility function to calculate Euclidean distance
double dist(Point p1, Point p2) {
return sqrt((p1.x - p2.x) * 1.0 * (p1.x - p2.x) +
(p1.y - p2.y) * 1.0 * (p1.y - p2.y));
}

// Brute force method for small number of points
double bruteForce(Point points[], int left, int right) {
double minDist = DBL_MAX;
for (int i = left; i < right; ++i) {
for (int j = i + 1; j < right; ++j) {
minDist = min(minDist, dist(points[i], points[j]));
}
}
return minDist;
}

// Function to find the closest points in the strip
double stripClosest(vector<Point>& strip, double d) {
double minDist = d;
int n = strip.size();

sort(strip.begin(), strip.end(), compareY);

for (int i = 0; i < n; ++i)
for (int j = i + 1; j < n && (strip[j].y - strip[i].y) < minDist; ++j)
minDist = min(minDist, dist(strip[i], strip[j]));

return minDist;
}

// Recursive function using Divide and Conquer
double closestUtil(Point points[], int left, int right) {
if (right - left <= 3)
return bruteForce(points, left, right);

int mid = (left + right) / 2;
Point midPoint = points[mid];

double dl = closestUtil(points, left, mid);
double dr = closestUtil(points, mid, right);

double d = min(dl, dr);

vector<Point> strip;
for (int i = left; i < right; ++i) {
if (abs(points[i].x - midPoint.x) < d)
strip.push_back(points[i]);
}

return min(d, stripClosest(strip, d));
}

// Main function to be called
double closestPair(Point points[], int n) {
sort(points, points + n, compareX);
return closestUtil(points, 0, n);
}

// --------- Sample Driver Code ---------
int main() {
Point points[] = {{2, 3}, {12, 30}, {40, 50},
{5, 1}, {12, 10}, {3, 4}};
int n = sizeof(points) / sizeof(points[0]);

cout << fixed << setprecision(4);
cout << "The smallest distance is " << closestPair(points, n) << endl;

return 0;
}