-
Notifications
You must be signed in to change notification settings - Fork 0
/
Closest_Points.py
255 lines (212 loc) · 8.28 KB
/
Closest_Points.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
import math
import sys
import random
import time
# this is my class for the points
class Point:
def __init__(self, x , y):
self.x = x
self.y = y
def stringPoint(self):
string = "(" + str(self.x) + "," + str(self.y) + ")"
return string
def getX(self):
return self.x
def getY(self):
return self.y
# distance formula
def distance(point1 , point2):
return math.sqrt((point2.x - point1.x)**2 + (point2.y - point1.y)**2)
# Global Variables
iterations = 0
closePoint1 = Point(sys.maxsize,0)
closePoint2 = Point(-1 * sys.maxsize,0)
# This algorithm is a very simple brute force algorithm, selects a point, then looks at the distance from that point to
# every other point. It repeats this with every point and returns the 2 closest point with their distance
def bruteForce(listPoints):
check1 = 0 # declarations
check2 = 0
point1 = Point(0,0)
point2 = Point(0,0)
global iterations
dis = sys.maxsize
for i in listPoints: # iterations
for j in listPoints:
iterations += 1
pointDis = distance(i,j)
if pointDis < dis and check1 != check2: # if distance is min, save the distance as new min
point1 = i # if they are not the same point
point2 = j
dis = pointDis
check2 += 1
check2 = 0
check1 += 1
global closePoint1
global closePoint2
if distance(point1,point2) <= distance(closePoint1,closePoint2):
closePoint1 = point1
closePoint2 = point2
return dis
#These are functions are to sort the list in order x and in order y
def mergeSortX(listPoints):
size = len(listPoints)
if size > 1:
middle = size // 2
left = listPoints[:middle]
right = listPoints[middle:]
mergeSortX(left)
mergeSortX(right)
p = 0
q = 0
r = 0
leftSize = len(left)
rightSize = len(right)
while p < leftSize and q < rightSize:
if left[p].getX() < right[q].getX():
listPoints[r] = left[p]
p += 1
else:
listPoints[r] = right[q]
q += 1
r += 1
while p < leftSize:
listPoints[r] = left[p]
p += 1
r += 1
while q < rightSize:
listPoints[r] = right[q]
q += 1
r += 1
return listPoints
def mergeSortY(listPoints):
size = len(listPoints)
if size > 1:
middle = size // 2
left = listPoints[:middle]
right = listPoints[middle:]
mergeSortY(left)
mergeSortY(right)
p = 0
q = 0
r = 0
leftSize = len(left)
rightSize = len(right)
while p < leftSize and q < rightSize:
if left[p].getY() < right[q].getY():
listPoints[r] = left[p]
p += 1
else:
listPoints[r] = right[q]
q += 1
r += 1
while p < leftSize:
listPoints[r] = left[p]
p += 1
r += 1
while q < rightSize:
listPoints[r] = right[q]
q += 1
r += 1
return listPoints
# sorts the lists in x and y and runs the recursive algorithm
def divideAndConquer(listPoints):
return closestPoint(mergeSortX(listPoints),mergeSortY(listPoints))
#For the unlucky case where the closest points are in between of the 2 sorted by x list of points
def stripClosePoints(strip, length , dis):
min_num = dis
for i in range(length):
j = i + 1
while j < length and (strip[j].y - strip[i].y) < min_num:
min_num = distance(strip[i], strip[j])
j += 1
return min_num
# This is my recursive algorithm
def closestPoint(X,Y):
global iterations
iterations += 1
if len(X) <= 3:
return bruteForce(X)
mid = len(X)//2 # split the x sorted list in half
Xleft = X[:mid]
Xright = X[mid:]
dis_left = closestPoint(Xleft , Y) # recursive part, 2 times, once for left side and once for right side
dis_right = closestPoint(Xright , Y)
dis = min(dis_left,dis_right) # after recursion finishes, get the min of left and right
# in case the 2 closest points were both in right and left, check that
S_X = []
S_Y = []
for i in range(len(X)):
if abs(X[i].getX() - Xright[0].getX()) < dis:
S_X.append(X[i])
if abs(Y[i].getX() - Xright[0].getX()) < dis:
S_Y.append(Y[i])
S_X_sorted = mergeSortY(S_X)
return min(dis , stripClosePoints(S_Y , len(S_Y), dis) , stripClosePoints(S_X_sorted , len(S_X), dis))
# For testing if the result is correct
def getInput():
print("Enter P for premade points")
print("Enter C to create new points")
testPoints = [Point(-1230.0,1234.0), Point(234.0,23.0), Point(234.0,432.0), Point(3456235.0,341.0), Point(-1204234.0,-123234.0)]
premade_or_create = input("Enter: ")
if premade_or_create == "C":
testPoints = createPoints()
elif premade_or_create != "P":
print("You know what, you get premade points since you cant follow instructions")
if len(testPoints) > 1:
print("\nEnter D to find the closest points using the Divide and Conquer Algorithm")
print("Enter B to find the closest points using the Brute Force Algorithm")
choice = input("Enter: ")
printPoints = []
for i in testPoints:
printPoints.append(i.stringPoint())
print("Points:" , printPoints)
if choice == "B":
print("Brute Force Algorithm: ")
print("Distance: ",bruteForce(testPoints) , "Points: ", closePoint1.stringPoint() , closePoint2.stringPoint())
print(str(len(testPoints)) + " values given with " + str(iterations) + " iterations, making this a O(n^2) algorithm\n\n" )
elif choice == "D":
print("Divide and Conquer Algorithm: ")
print("Distance: ", divideAndConquer(testPoints) , "Points: ",closePoint1.stringPoint() , closePoint2.stringPoint())
print("Prove O(nlg(n)) using masters theorm: \nRecursive equation: T(n) = 2T(n/2) + n \nValues: a = 2 , b = 2, d = 1 \n(log_2(2)) = 1 = d \nTherefore the run time is O(nlg(n))")
else:
print("It's really hard to find the closest distance between points when you do not have at least 2 points...")
def createPoints():
newPoint = "Y"
result = []
while(newPoint != "N"):
newPoint = input("New Point? (Enter Y/N): ")
if newPoint == "Y":
x = input("Enter x value: ")
y = input("Enter y value: ")
try:
x = float(x)
y = float(y)
except ValueError as e:
print(e)
result.append(Point(x,y))
return result
def Experiment(n):
listpoints = []
for i in range(n):
listpoints.append(Point(random.randint(-100000,100000),random.randint(-100000,100000)))
print("Finished making list")
print("\n\n")
# for the Brute Force Algorithm
tic = time.perf_counter()
fromBF = bruteForce(listpoints)
toc = time.perf_counter()
print("Brute Force Algorithm:")
print("Distance: ", fromBF , "Points: ", closePoint1.stringPoint() , closePoint2.stringPoint())
print(f"This took {toc - tic:0.4f} seconds")
print("\n\n")
# for the Divide and Conquer Algorithm
tic = time.perf_counter()
fromDoC = divideAndConquer(listpoints)
toc = time.perf_counter()
print("Divide and Conquer Algorithm:")
print("Distance: ", fromDoC , "Points: ", closePoint1.stringPoint() , closePoint2.stringPoint())
print(f"This took {toc - tic:0.4f} seconds")
print("\n\n")
n = input("Enter the amount of random points: ")
Experiment(int(n))
input("Press Enter to Exit")