From 5a1cb91748137c93748047d89ef10a2b43c4dc7d Mon Sep 17 00:00:00 2001 From: Prabhat <87269080+Prabhatsingh001@users.noreply.github.com> Date: Thu, 24 Oct 2024 20:18:33 +0530 Subject: [PATCH] closest pair of points algorithm --- .../closest_pair_of_points.py | 98 +++++++++++++++++++ 1 file changed, 98 insertions(+) create mode 100644 Algorithms_and_Data_Structures/closest_pair_of_points/closest_pair_of_points.py diff --git a/Algorithms_and_Data_Structures/closest_pair_of_points/closest_pair_of_points.py b/Algorithms_and_Data_Structures/closest_pair_of_points/closest_pair_of_points.py new file mode 100644 index 00000000..82e0b52a --- /dev/null +++ b/Algorithms_and_Data_Structures/closest_pair_of_points/closest_pair_of_points.py @@ -0,0 +1,98 @@ +import math + +class Point: + def __init__(self, x, y): + self.x = x + self.y = y + +#sort array of points according to X coordinate +def compareX(a, b): + p1, p2 = a, b + return (p1.x != p2.x) * (p1.x - p2.x) + (p1.y - p2.y) + +#sort array of points according to Y coordinate +def compareY(a, b): + p1, p2 = a, b + return (p1.y != p2.y) * (p1.y - p2.y) + (p1.x - p2.x) + + +# A utility function to find the distance between two points +def dist(p1, p2): + return math.sqrt((p1.x - p2.x)**2 + (p1.y - p2.y)**2) + +'''A Brute Force method to return the smallest distance between two points +in P[] of size n''' +def bruteForce(P, n): + min = float('inf') + for i in range(n): + for j in range(i+1, n): + if dist(P[i], P[j]) < min: + min = dist(P[i], P[j]) + return min + +# A utility function to find a minimum of two float values +def min(x, y): + return x if x < y else y + +'''# A utility function to find the distance between the closest points of strip of a given size. All points in strip[] are sorted according to +y coordinate. They all have an upper bound on minimum distance as d.Note that this method seems to be a O(n^2) method, but it's a O(n) method as the inner loop runs at most 6 times''' +def stripClosest(strip, size, d): + min = d + for i in range(size): + for j in range(i+1, size): + if (strip[j].y - strip[i].y) < min: + if dist(strip[i],strip[j]) < min: + min = dist(strip[i], strip[j]) + + return min + +'''A recursive function to find the smallest distance. +The array Px contains all points sorted according to x coordinates and Py contains all points sorted according to y coordinates''' +def closestUtil(Px, Py, n): + if n <= 3: + return bruteForce(Px, n) + + mid = n // 2 + midPoint = Px[mid] + + Pyl = [None] * mid + Pyr = [None] * (n-mid) + li = ri = 0 + for i in range(n): + if ((Py[i].x < midPoint.x or (Py[i].x == midPoint.x and Py[i].y < midPoint.y)) and li