mirror of
https://github.com/TheAlgorithms/Python.git
synced 2025-02-25 18:38:39 +00:00
updates in closest pair of points algorithm (#979)
* updated closest pair of points (n*(logn)^2) to (n*logn)
This commit is contained in:
parent
8b2d1b7f50
commit
c85312da89
@ -1,27 +1,27 @@
|
|||||||
"""
|
"""
|
||||||
The algorithm finds distance btw closest pair of points in the given n points.
|
The algorithm finds distance between closest pair of points
|
||||||
|
in the given n points.
|
||||||
Approach used -> Divide and conquer
|
Approach used -> Divide and conquer
|
||||||
The points are sorted based on Xco-ords
|
The points are sorted based on Xco-ords and
|
||||||
& by applying divide and conquer approach,
|
then based on Yco-ords separately.
|
||||||
|
And by applying divide and conquer approach,
|
||||||
minimum distance is obtained recursively.
|
minimum distance is obtained recursively.
|
||||||
|
|
||||||
>> closest points lie on different sides of partition
|
>> Closest points can lie on different sides of partition.
|
||||||
This case handled by forming a strip of points
|
This case handled by forming a strip of points
|
||||||
whose Xco-ords distance is less than closest_pair_dis
|
whose Xco-ords distance is less than closest_pair_dis
|
||||||
from mid-point's Xco-ords.
|
from mid-point's Xco-ords. Points sorted based on Yco-ords
|
||||||
|
are used in this step to reduce sorting time.
|
||||||
Closest pair distance is found in the strip of points. (closest_in_strip)
|
Closest pair distance is found in the strip of points. (closest_in_strip)
|
||||||
|
|
||||||
min(closest_pair_dis, closest_in_strip) would be the final answer.
|
min(closest_pair_dis, closest_in_strip) would be the final answer.
|
||||||
|
|
||||||
Time complexity: O(n * (logn)^2)
|
Time complexity: O(n * log n)
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
||||||
import math
|
|
||||||
|
|
||||||
|
|
||||||
def euclidean_distance_sqr(point1, point2):
|
def euclidean_distance_sqr(point1, point2):
|
||||||
return pow(point1[0] - point2[0], 2) + pow(point1[1] - point2[1], 2)
|
return (point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2
|
||||||
|
|
||||||
|
|
||||||
def column_based_sort(array, column = 0):
|
def column_based_sort(array, column = 0):
|
||||||
@ -66,7 +66,7 @@ def dis_between_closest_in_strip(points, points_counts, min_dis = float("inf")):
|
|||||||
return min_dis
|
return min_dis
|
||||||
|
|
||||||
|
|
||||||
def closest_pair_of_points_sqr(points, points_counts):
|
def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_counts):
|
||||||
""" divide and conquer approach
|
""" divide and conquer approach
|
||||||
|
|
||||||
Parameters :
|
Parameters :
|
||||||
@ -79,12 +79,16 @@ def closest_pair_of_points_sqr(points, points_counts):
|
|||||||
|
|
||||||
# base case
|
# base case
|
||||||
if points_counts <= 3:
|
if points_counts <= 3:
|
||||||
return dis_between_closest_pair(points, points_counts)
|
return dis_between_closest_pair(points_sorted_on_x, points_counts)
|
||||||
|
|
||||||
# recursion
|
# recursion
|
||||||
mid = points_counts//2
|
mid = points_counts//2
|
||||||
closest_in_left = closest_pair_of_points(points[:mid], mid)
|
closest_in_left = closest_pair_of_points_sqr(points_sorted_on_x,
|
||||||
closest_in_right = closest_pair_of_points(points[mid:], points_counts - mid)
|
points_sorted_on_y[:mid],
|
||||||
|
mid)
|
||||||
|
closest_in_right = closest_pair_of_points_sqr(points_sorted_on_y,
|
||||||
|
points_sorted_on_y[mid:],
|
||||||
|
points_counts - mid)
|
||||||
closest_pair_dis = min(closest_in_left, closest_in_right)
|
closest_pair_dis = min(closest_in_left, closest_in_right)
|
||||||
|
|
||||||
""" cross_strip contains the points, whose Xcoords are at a
|
""" cross_strip contains the points, whose Xcoords are at a
|
||||||
@ -92,22 +96,25 @@ def closest_pair_of_points_sqr(points, points_counts):
|
|||||||
"""
|
"""
|
||||||
|
|
||||||
cross_strip = []
|
cross_strip = []
|
||||||
for point in points:
|
for point in points_sorted_on_x:
|
||||||
if abs(point[0] - points[mid][0]) < closest_pair_dis:
|
if abs(point[0] - points_sorted_on_x[mid][0]) < closest_pair_dis:
|
||||||
cross_strip.append(point)
|
cross_strip.append(point)
|
||||||
|
|
||||||
cross_strip = column_based_sort(cross_strip, 1)
|
|
||||||
closest_in_strip = dis_between_closest_in_strip(cross_strip,
|
closest_in_strip = dis_between_closest_in_strip(cross_strip,
|
||||||
len(cross_strip), closest_pair_dis)
|
len(cross_strip), closest_pair_dis)
|
||||||
return min(closest_pair_dis, closest_in_strip)
|
return min(closest_pair_dis, closest_in_strip)
|
||||||
|
|
||||||
|
|
||||||
def closest_pair_of_points(points, points_counts):
|
def closest_pair_of_points(points, points_counts):
|
||||||
return math.sqrt(closest_pair_of_points_sqr(points, points_counts))
|
points_sorted_on_x = column_based_sort(points, column = 0)
|
||||||
|
points_sorted_on_y = column_based_sort(points, column = 1)
|
||||||
|
return (closest_pair_of_points_sqr(points_sorted_on_x,
|
||||||
|
points_sorted_on_y,
|
||||||
|
points_counts)) ** 0.5
|
||||||
|
|
||||||
|
|
||||||
points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (0, 2), (5, 6), (1, 2)]
|
if __name__ == "__main__":
|
||||||
points = column_based_sort(points)
|
points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)]
|
||||||
print("Distance:", closest_pair_of_points(points, len(points)))
|
print("Distance:", closest_pair_of_points(points, len(points)))
|
||||||
|
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user