2019-07-04 07:49:14 +00:00
|
|
|
"""
|
2019-08-06 19:31:03 +00:00
|
|
|
The algorithm finds distance between closest pair of points
|
2019-07-09 15:20:43 +00:00
|
|
|
in the given n points.
|
2019-08-06 19:31:03 +00:00
|
|
|
Approach used -> Divide and conquer
|
|
|
|
The points are sorted based on Xco-ords and
|
2019-07-09 15:20:43 +00:00
|
|
|
then based on Yco-ords separately.
|
2019-08-06 19:31:03 +00:00
|
|
|
And by applying divide and conquer approach,
|
2019-07-04 07:49:14 +00:00
|
|
|
minimum distance is obtained recursively.
|
|
|
|
|
2019-07-09 15:20:43 +00:00
|
|
|
>> Closest points can lie on different sides of partition.
|
2019-08-06 19:31:03 +00:00
|
|
|
This case handled by forming a strip of points
|
2019-07-04 07:49:14 +00:00
|
|
|
whose Xco-ords distance is less than closest_pair_dis
|
2019-08-06 19:31:03 +00:00
|
|
|
from mid-point's Xco-ords. Points sorted based on Yco-ords
|
2019-07-09 15:20:43 +00:00
|
|
|
are used in this step to reduce sorting time.
|
2019-07-04 07:49:14 +00:00
|
|
|
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.
|
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
Time complexity: O(n * log n)
|
2019-08-06 09:17:17 +00:00
|
|
|
"""
|
|
|
|
|
2019-07-04 07:49:14 +00:00
|
|
|
|
|
|
|
def euclidean_distance_sqr(point1, point2):
|
2019-08-06 19:31:03 +00:00
|
|
|
"""
|
|
|
|
>>> euclidean_distance_sqr([1,2],[2,4])
|
|
|
|
5
|
|
|
|
"""
|
2019-07-09 15:20:43 +00:00
|
|
|
return (point1[0] - point2[0]) ** 2 + (point1[1] - point2[1]) ** 2
|
2019-07-04 07:49:14 +00:00
|
|
|
|
|
|
|
|
|
|
|
def column_based_sort(array, column = 0):
|
2019-08-06 19:31:03 +00:00
|
|
|
"""
|
|
|
|
>>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1)
|
|
|
|
[(3, 0), (5, 1), (4, 2)]
|
|
|
|
"""
|
2019-07-04 07:49:14 +00:00
|
|
|
return sorted(array, key = lambda x: x[column])
|
2019-08-06 19:31:03 +00:00
|
|
|
|
2019-07-04 07:49:14 +00:00
|
|
|
|
|
|
|
def dis_between_closest_pair(points, points_counts, min_dis = float("inf")):
|
2019-08-06 19:31:03 +00:00
|
|
|
"""
|
|
|
|
brute force approach to find distance between closest pair points
|
|
|
|
|
|
|
|
Parameters :
|
|
|
|
points, points_count, min_dis (list(tuple(int, int)), int, int)
|
2019-07-04 07:49:14 +00:00
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
Returns :
|
2019-07-04 07:49:14 +00:00
|
|
|
min_dis (float): distance between closest pair of points
|
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
>>> dis_between_closest_pair([[1,2],[2,4],[5,7],[8,9],[11,0]],5)
|
|
|
|
5
|
|
|
|
|
2019-07-04 07:49:14 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
for i in range(points_counts - 1):
|
|
|
|
for j in range(i+1, points_counts):
|
|
|
|
current_dis = euclidean_distance_sqr(points[i], points[j])
|
|
|
|
if current_dis < min_dis:
|
|
|
|
min_dis = current_dis
|
|
|
|
return min_dis
|
|
|
|
|
|
|
|
|
|
|
|
def dis_between_closest_in_strip(points, points_counts, min_dis = float("inf")):
|
2019-08-06 19:31:03 +00:00
|
|
|
"""
|
|
|
|
closest pair of points in strip
|
|
|
|
|
|
|
|
Parameters :
|
|
|
|
points, points_count, min_dis (list(tuple(int, int)), int, int)
|
2019-07-04 07:49:14 +00:00
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
Returns :
|
2019-07-04 07:49:14 +00:00
|
|
|
min_dis (float): distance btw closest pair of points in the strip (< min_dis)
|
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
>>> dis_between_closest_in_strip([[1,2],[2,4],[5,7],[8,9],[11,0]],5)
|
|
|
|
85
|
2019-07-04 07:49:14 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
for i in range(min(6, points_counts - 1), points_counts):
|
|
|
|
for j in range(max(0, i-6), i):
|
|
|
|
current_dis = euclidean_distance_sqr(points[i], points[j])
|
|
|
|
if current_dis < min_dis:
|
|
|
|
min_dis = current_dis
|
|
|
|
return min_dis
|
|
|
|
|
|
|
|
|
2019-07-09 15:20:43 +00:00
|
|
|
def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_counts):
|
2019-07-04 07:49:14 +00:00
|
|
|
""" divide and conquer approach
|
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
Parameters :
|
|
|
|
points, points_count (list(tuple(int, int)), int)
|
|
|
|
|
|
|
|
Returns :
|
|
|
|
(float): distance btw closest pair of points
|
2019-07-04 07:49:14 +00:00
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
>>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(5, 6), (7, 8)], 2)
|
|
|
|
8
|
2019-07-04 07:49:14 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
# base case
|
|
|
|
if points_counts <= 3:
|
2019-07-09 15:20:43 +00:00
|
|
|
return dis_between_closest_pair(points_sorted_on_x, points_counts)
|
2019-08-06 19:31:03 +00:00
|
|
|
|
2019-07-04 07:49:14 +00:00
|
|
|
# recursion
|
|
|
|
mid = points_counts//2
|
2019-08-06 19:31:03 +00:00
|
|
|
closest_in_left = closest_pair_of_points_sqr(points_sorted_on_x,
|
|
|
|
points_sorted_on_y[:mid],
|
2019-07-09 15:20:43 +00:00
|
|
|
mid)
|
2019-08-06 19:31:03 +00:00
|
|
|
closest_in_right = closest_pair_of_points_sqr(points_sorted_on_y,
|
|
|
|
points_sorted_on_y[mid:],
|
2019-07-09 15:20:43 +00:00
|
|
|
points_counts - mid)
|
2019-07-04 07:49:14 +00:00
|
|
|
closest_pair_dis = min(closest_in_left, closest_in_right)
|
2019-08-06 19:31:03 +00:00
|
|
|
|
|
|
|
"""
|
|
|
|
cross_strip contains the points, whose Xcoords are at a
|
2019-07-04 07:49:14 +00:00
|
|
|
distance(< closest_pair_dis) from mid's Xcoord
|
|
|
|
"""
|
|
|
|
|
|
|
|
cross_strip = []
|
2019-07-09 15:20:43 +00:00
|
|
|
for point in points_sorted_on_x:
|
|
|
|
if abs(point[0] - points_sorted_on_x[mid][0]) < closest_pair_dis:
|
2019-07-04 07:49:14 +00:00
|
|
|
cross_strip.append(point)
|
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
closest_in_strip = dis_between_closest_in_strip(cross_strip,
|
2019-07-04 07:49:14 +00:00
|
|
|
len(cross_strip), closest_pair_dis)
|
|
|
|
return min(closest_pair_dis, closest_in_strip)
|
|
|
|
|
2019-08-06 19:31:03 +00:00
|
|
|
|
2019-07-04 07:49:14 +00:00
|
|
|
def closest_pair_of_points(points, points_counts):
|
2019-08-06 19:31:03 +00:00
|
|
|
"""
|
|
|
|
>>> closest_pair_of_points([(2, 3), (12, 30)], len([(2, 3), (12, 30)]))
|
|
|
|
28.792360097775937
|
|
|
|
"""
|
2019-07-09 15:20:43 +00:00
|
|
|
points_sorted_on_x = column_based_sort(points, column = 0)
|
|
|
|
points_sorted_on_y = column_based_sort(points, column = 1)
|
2019-08-06 19:31:03 +00:00
|
|
|
return (closest_pair_of_points_sqr(points_sorted_on_x,
|
|
|
|
points_sorted_on_y,
|
2019-07-09 15:20:43 +00:00
|
|
|
points_counts)) ** 0.5
|
2019-07-04 07:49:14 +00:00
|
|
|
|
|
|
|
|
2019-07-09 15:20:43 +00:00
|
|
|
if __name__ == "__main__":
|
2019-08-06 19:31:03 +00:00
|
|
|
points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (3, 4)]
|
2019-07-09 15:20:43 +00:00
|
|
|
print("Distance:", closest_pair_of_points(points, len(points)))
|