1818
1919Time complexity: O(n * log n)
2020"""
21+ from __future__ import annotations
2122
23+ from typing import Any
2224
23- def euclidean_distance_sqr (point1 , point2 ):
25+
26+ def euclidean_distance_sqr (
27+ point1 : list [Any ] | tuple [Any , ...], point2 : list [Any ] | tuple [Any , ...]
28+ ) -> float :
2429 """
25- >>> euclidean_distance_sqr([1,2],[2,4])
30+ >>> euclidean_distance_sqr([1, 2], [2, 4])
2631 5
2732 """
2833 return (point1 [0 ] - point2 [0 ]) ** 2 + (point1 [1 ] - point2 [1 ]) ** 2
2934
3035
31- def column_based_sort (array , column = 0 ) :
36+ def column_based_sort (array : list [ Any ] , column : int = 0 ) -> list [ Any ] :
3237 """
3338 >>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1)
3439 [(3, 0), (5, 1), (4, 2)]
3540 """
3641 return sorted (array , key = lambda x : x [column ])
3742
3843
39- def dis_between_closest_pair (points , points_counts , min_dis = float ("inf" )):
44+ def dis_between_closest_pair (
45+ points : list [Any ], points_counts : int , min_dis : float = float ("inf" )
46+ ) -> float :
4047 """
4148 brute force approach to find distance between closest pair points
4249
@@ -46,7 +53,7 @@ def dis_between_closest_pair(points, points_counts, min_dis=float("inf")):
4653 Returns :
4754 min_dis (float): distance between closest pair of points
4855
49- >>> dis_between_closest_pair([[1,2],[2,4],[5,7],[8,9],[11,0]],5)
56+ >>> dis_between_closest_pair([[1, 2], [2, 4], [5, 7], [8, 9], [11, 0]], 5)
5057 5
5158
5259 """
@@ -58,7 +65,9 @@ def dis_between_closest_pair(points, points_counts, min_dis=float("inf")):
5865 return min_dis
5966
6067
61- def dis_between_closest_in_strip (points , points_counts , min_dis = float ("inf" )):
68+ def dis_between_closest_in_strip (
69+ points : list [Any ], points_counts : int , min_dis : float = float ("inf" )
70+ ) -> float :
6271 """
6372 closest pair of points in strip
6473
@@ -68,18 +77,20 @@ def dis_between_closest_in_strip(points, points_counts, min_dis=float("inf")):
6877 Returns :
6978 min_dis (float): distance btw closest pair of points in the strip (< min_dis)
7079
71- >>> dis_between_closest_in_strip([[1,2],[2,4],[5,7],[8,9],[11,0]],5)
72- 85
80+ >>> dis_between_closest_in_strip([[1, 2], [2, 4], [5, 7], [8, 9], [11, 0]], 5)
81+ 5
7382 """
7483
75- for i in range (min ( 6 , points_counts - 1 ), points_counts ):
76- for j in range (max ( 0 , i - 6 ), i ):
84+ for i in range (points_counts - 1 ):
85+ for j in range (i + 1 , min ( i + 6 , points_counts ) ):
7786 current_dis = euclidean_distance_sqr (points [i ], points [j ])
7887 min_dis = min (min_dis , current_dis )
7988 return min_dis
8089
8190
82- def closest_pair_of_points_sqr (points_sorted_on_x , points_sorted_on_y , points_counts ):
91+ def closest_pair_of_points_sqr (
92+ points_sorted_on_x : list [Any ], points_sorted_on_y : list [Any ], points_counts : int
93+ ) -> float :
8394 """divide and conquer approach
8495
8596 Parameters :
@@ -88,7 +99,7 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
8899 Returns :
89100 (float): distance btw closest pair of points
90101
91- >>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(5, 6 ), (7, 8 )], 2)
102+ >>> closest_pair_of_points_sqr([(1, 2), (3, 4)], [(1, 2 ), (3, 4 )], 2)
92103 8
93104 """
94105
@@ -99,10 +110,10 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
99110 # recursion
100111 mid = points_counts // 2
101112 closest_in_left = closest_pair_of_points_sqr (
102- points_sorted_on_x , points_sorted_on_y [:mid ], mid
113+ points_sorted_on_x [:mid ], points_sorted_on_y , mid
103114 )
104115 closest_in_right = closest_pair_of_points_sqr (
105- points_sorted_on_y , points_sorted_on_y [mid :], points_counts - mid
116+ points_sorted_on_x [mid :], points_sorted_on_y , points_counts - mid
106117 )
107118 closest_pair_dis = min (closest_in_left , closest_in_right )
108119
@@ -112,7 +123,7 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
112123 """
113124
114125 cross_strip = []
115- for point in points_sorted_on_x :
126+ for point in points_sorted_on_y :
116127 if abs (point [0 ] - points_sorted_on_x [mid ][0 ]) < closest_pair_dis :
117128 cross_strip .append (point )
118129
@@ -122,9 +133,9 @@ def closest_pair_of_points_sqr(points_sorted_on_x, points_sorted_on_y, points_co
122133 return min (closest_pair_dis , closest_in_strip )
123134
124135
125- def closest_pair_of_points (points , points_counts ) :
136+ def closest_pair_of_points (points : list [ Any ] , points_counts : int ) -> float :
126137 """
127- >>> closest_pair_of_points([(2, 3), (12, 30)], len([(2, 3), (12, 30)]) )
138+ >>> closest_pair_of_points([(2, 3), (12, 30)], 2 )
128139 28.792360097775937
129140 """
130141 points_sorted_on_x = column_based_sort (points , column = 0 )
0 commit comments