|
| 1 | +""" |
| 2 | +The algorithm finds distance btw closest pair of points in the given n points. |
| 3 | +Approach used -> Divide and conquer |
| 4 | +The points are sorted based on Xco-ords |
| 5 | +& by applying divide and conquer approach, |
| 6 | +minimum distance is obtained recursively. |
| 7 | +
|
| 8 | +>> closest points lie on different sides of partition |
| 9 | +This case handled by forming a strip of points |
| 10 | +whose Xco-ords distance is less than closest_pair_dis |
| 11 | +from mid-point's Xco-ords. |
| 12 | +Closest pair distance is found in the strip of points. (closest_in_strip) |
| 13 | +
|
| 14 | +min(closest_pair_dis, closest_in_strip) would be the final answer. |
| 15 | + |
| 16 | +Time complexity: O(n * (logn)^2) |
| 17 | +""" |
| 18 | + |
| 19 | + |
| 20 | +import math |
| 21 | + |
| 22 | + |
| 23 | +def euclidean_distance_sqr(point1, point2): |
| 24 | + return pow(point1[0] - point2[0], 2) + pow(point1[1] - point2[1], 2) |
| 25 | + |
| 26 | + |
| 27 | +def column_based_sort(array, column = 0): |
| 28 | + return sorted(array, key = lambda x: x[column]) |
| 29 | + |
| 30 | + |
| 31 | +def dis_between_closest_pair(points, points_counts, min_dis = float("inf")): |
| 32 | + """ brute force approach to find distance between closest pair points |
| 33 | +
|
| 34 | + Parameters : |
| 35 | + points, points_count, min_dis (list(tuple(int, int)), int, int) |
| 36 | + |
| 37 | + Returns : |
| 38 | + min_dis (float): distance between closest pair of points |
| 39 | +
|
| 40 | + """ |
| 41 | + |
| 42 | + for i in range(points_counts - 1): |
| 43 | + for j in range(i+1, points_counts): |
| 44 | + current_dis = euclidean_distance_sqr(points[i], points[j]) |
| 45 | + if current_dis < min_dis: |
| 46 | + min_dis = current_dis |
| 47 | + return min_dis |
| 48 | + |
| 49 | + |
| 50 | +def dis_between_closest_in_strip(points, points_counts, min_dis = float("inf")): |
| 51 | + """ closest pair of points in strip |
| 52 | +
|
| 53 | + Parameters : |
| 54 | + points, points_count, min_dis (list(tuple(int, int)), int, int) |
| 55 | + |
| 56 | + Returns : |
| 57 | + min_dis (float): distance btw closest pair of points in the strip (< min_dis) |
| 58 | +
|
| 59 | + """ |
| 60 | + |
| 61 | + for i in range(min(6, points_counts - 1), points_counts): |
| 62 | + for j in range(max(0, i-6), i): |
| 63 | + current_dis = euclidean_distance_sqr(points[i], points[j]) |
| 64 | + if current_dis < min_dis: |
| 65 | + min_dis = current_dis |
| 66 | + return min_dis |
| 67 | + |
| 68 | + |
| 69 | +def closest_pair_of_points_sqr(points, points_counts): |
| 70 | + """ divide and conquer approach |
| 71 | +
|
| 72 | + Parameters : |
| 73 | + points, points_count (list(tuple(int, int)), int) |
| 74 | + |
| 75 | + Returns : |
| 76 | + (float): distance btw closest pair of points |
| 77 | +
|
| 78 | + """ |
| 79 | + |
| 80 | + # base case |
| 81 | + if points_counts <= 3: |
| 82 | + return dis_between_closest_pair(points, points_counts) |
| 83 | + |
| 84 | + # recursion |
| 85 | + mid = points_counts//2 |
| 86 | + closest_in_left = closest_pair_of_points(points[:mid], mid) |
| 87 | + closest_in_right = closest_pair_of_points(points[mid:], points_counts - mid) |
| 88 | + closest_pair_dis = min(closest_in_left, closest_in_right) |
| 89 | + |
| 90 | + """ cross_strip contains the points, whose Xcoords are at a |
| 91 | + distance(< closest_pair_dis) from mid's Xcoord |
| 92 | + """ |
| 93 | + |
| 94 | + cross_strip = [] |
| 95 | + for point in points: |
| 96 | + if abs(point[0] - points[mid][0]) < closest_pair_dis: |
| 97 | + cross_strip.append(point) |
| 98 | + |
| 99 | + cross_strip = column_based_sort(cross_strip, 1) |
| 100 | + closest_in_strip = dis_between_closest_in_strip(cross_strip, |
| 101 | + len(cross_strip), closest_pair_dis) |
| 102 | + return min(closest_pair_dis, closest_in_strip) |
| 103 | + |
| 104 | + |
| 105 | +def closest_pair_of_points(points, points_counts): |
| 106 | + return math.sqrt(closest_pair_of_points_sqr(points, points_counts)) |
| 107 | + |
| 108 | + |
| 109 | +points = [(2, 3), (12, 30), (40, 50), (5, 1), (12, 10), (0, 2), (5, 6), (1, 2)] |
| 110 | +points = column_based_sort(points) |
| 111 | +print("Distance:", closest_pair_of_points(points, len(points))) |
| 112 | + |
| 113 | + |
0 commit comments