Is there any efficient library function in python to find a pair of points which are closest/farthest? The input is a list of points which lie in a k-dimensional cube of edge size 1. For finding the closest, the following code is taking too much time. TC: O( n**2 * k ) where n is the size of input. In my case, input n is around 4000 and value of k is around 300.
def euclid_dist( p1, p2 ):
val = 0
for i in range(len(p1)):
val += (p1[i]-p2[i])**2
return val
def find_close_cluster( points ):
ans1 = 0
ans2 = 1
min_dist = 1000000000000
for i in range( len(clusters) ):
for j in range( i+1,len(clusters)):
current_dist = euclid_dist(clusters[i],clusters[j])
if( current_dist < min_dist ):
ans1 = i
ans2 = j
min_dist = current_dist
return ( ans1, ans2 )