Question: PLEASE CODE IN PYTHON Implement the algorithm for the closest pair of points (Section 5.3 in the textbook). Your program should input the coordinates of
PLEASE CODE IN PYTHON
Implement the algorithm for the closest pair of points (Section 5.3 in the textbook). Your program should input the coordinates of the points from (each of) the test files uploaded on D2L, and outputs: (1) the coordinates of a closest pair in the point-set, and (2) the distance between the two points in the closest pair.
Given a text file of pairs use the algorithm to find closest pair of points and the distance
textfile name: 10points.txt
Pairs in file like
1574 9098 7156 1322 511 4721 815 17928 1267 5292 666 405 889 14787 1184 2147 2560 18128 337 1320
Algorithm in textbook below
Def closest_pair(p):
n = len(p)
mergeSort(p,1,n)
return rec_cl_pair(p,1,n)
def rec_cl_pair(p,i,j):
if(j-i < 3):
mergeSort(p)
distance = dist(p[i],p[i+1])
closest_pair = (p[i],p[i+1])
if(j - i == 1):
return distance
if(dist(p[i+1], p[i+2]) < distance):
distance = dist(p[i+1], p[i+2])
closest_pair = (p[i+1], p[i+2])
if(dist(p[i], p[i+2]) < distance):
distance = dist(p[i], p[i+2])
closest_pair = (p[i], p[i+2])
return distance, closest_pair
k = (i+j)//2
l = p[k]
distanceL, closest_pairLeft = rec_cl_pair(p,i,k)
distanceR, closest_pairRight = rec_cl_pair(p,k+1,j)
distance = min(distanceL, distanceR)
if(distance == distanceR):
closest_pair = closest_pairRight
else:
closest_pair = closest_pairLeft
merge(p,i,k,j)
t = 0
for k in range(i, j):
if(p[k] > l - distance & p[k] < l + distance):
t += 1
v[t] = p(k)
for k in range(1, t-1):
for s in range(k+1, min(t, k+7)):
distance = min(distance, dist(v[k], v[s]))
return distance
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
