-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathUnbalncedKD_NearestSearch.py
417 lines (373 loc) · 13.2 KB
/
UnbalncedKD_NearestSearch.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
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
import math,os
from svg import Circle,Rectangle,Scene,Line,Rectangle,Text
def colorstr(rgb): return "#%x%x%x" % (int(rgb[0]/16),int(rgb[1]/16),int(rgb[2]/16))
'''def test(s):
a=s[0]
b=s[1]
scene = Scene('test')
scene.add(Rectangle((100,100),200,200,(255,255,255)))
# scene.add(Line((200,200),(200,300)))
# scene.add(Line((200,200),(300,200)))
# scene.add(Line((200,200),(100,200)))
# scene.add(Line((200,200),(200,100)))
scene.add(Circle((200,200),3,(0,255,0)))
scene.add(Circle((200,300),3,(0,255,0)))
scene.add(Circle((300,200),3,(0,255,0)))
scene.add(Circle((100,200),3,(0,255,0)))
scene.add(Circle((200,100),3,(0,255,0)))
scene.add(Circle((a,b),3,(0,255,255)))
scene.add(Text((50,50),"Testing SVG"))
scene.write_svg()
scene.display()
return'''
class treenode:
def __init__(self):
self.point=[]
self.left=None
self.right=None
self.parent=None
global b
b=treenode()
global c
b.point.append(10000000)
b.point.append(10000000)
c=treenode()
c.point.append(10000000)
c.point.append(10000000)
class kdtree:
def __init__(self):
self.root=None
def newnode(self,a):
temp=treenode()
for i in range(2):
temp.point.append(a[i])
return temp
#this will insert node at root and else part will insert things at the branch
def insert(self,x,y):
a=list()
a.append(x)
a.append(y)
temp=self.newnode(a)
# a.pop()
# a.pop()
a=[]
if self.root==None:
self.root=temp
else:
self.insertbranch(self.root,temp)
# This is the function which is used to calculate the dimension
#like we have consider in 2 dimension so we will find height using it
def findheight(self,node):
count=1
while node.parent:
count+=1
node=node.parent
return count
# Other function to insert in 2 different dimension
def insertbranch(self,node1,node):
height=self.findheight(node1)%2
if height==1: #height==1 corresponds to x dimension and height = 0 corresponds to y direction
if node.point[0]<node1.point[0]: #point[0] as defind in the structure of the node means x coordinate and point[1] corresponds to y coordinate
if node1.left!=None:
self.insertbranch(node1.left,node)
else:
node1.left=node
node.parent=node1
if node.point[0]>=node1.point[0]:
if node1.right!=None:
self.insertbranch(node1.right,node)
else:
node1.right=node
node.parent=node1
elif height==0:
if node.point[1]<node1.point[1] :
if node1.left!=None:
self.insertbranch(node1.left,node)
else:
node1.left=node
node.parent=node1
if node.point[1]>=node1.point[1]:
if node1.right!=None:
self.insertbranch(node1.right,node)
else:
node1.right=node
node.parent=node1
#printing the tree which is created printing is done in inorder traversals
def printkdtree(self,node):
if self.root==None:
print("There is nothing to print")
k=node.point[0]
print(node.point,end=' ')
if node.left !=None:
self.printkdtree(node.left)
if node.right!=None:
self.printkdtree(node.right)
# This code is to remove the node thing when we want to search via recursion and check the points
def searchtree(self,x,y):
if self.root==None:
print("No match found")
elif self.root.point[0]==x and self.root.point[1]==y:
print("Match found")
elif x<self.root.point[0]:
self.searchtree_(x,y,self.root)
elif x>=self.root.point[1]:
self.searchtree_(x,y,self.root)
# Search In KD Trees with the use of 3 parameters one is x coordinatre other is y coordinate and one is node in order to made recurssion
def searchtree_(self,x,y,node):
if node==None:
print()
print("No match found")
elif node!=None:
if node.point[0]==x and node.point[1]==y:
print()
print("Point is present in the tree")
return True
else:
height=self.findheight(node)%2
if height==1:
if x<node.point[0]:
if node.left!=None:
self.searchtree_(x,y,node.left)
else:
print()
print("No match found")
return False
if x>=node.point[0]:
if node.right!=None:
self.searchtree_(x,y,node.right)
else:
print()
print("No match found")
return False
elif height==0:
if y<node.point[1]:
if node.left!=None:
self.searchtree_(x,y,node.left)
else:
print()
print("No match found")
return False
if y>=node.point[1]:
if node.right!=None:
self.searchtree_(x,y,node.right)
else:
print()
print("No match found")
return False
#method to find minimum in both x and y direction we have to start from the root in order to find the minimum
def minimum(self,dimension):
if self.root==None:
print("There is nothing to find minimum")
else:
return self.minimum_(self.root,dimension,1) #this is done in order that the initial height is 1
def minimum_(self,node,dimension,depth):
global b, c
if dimension=='x':
z=0 #x corresponds to the dimension with point index as 0
elif dimension=='y':
z=1 # y corresponds to the dimension with point index as 1
h=depth%2 #checking in which dimension we are currently working
#after this use pycharm debugger to understand what i have done as i cant explain in comments :-p
if h!=z:
if node.left==None:
t=node.point[z]
return node
return self.minimum_(node.left,dimension,depth+1)
a=node
if node.left!=None:
b=self.minimum_(node.left,dimension,depth+1)
if node.right!=None:
c=self.minimum_(node.right,dimension,depth+1)
return self.minnode(a,b,c,z)
def minnode(self,a,b,c,z):
res=a
if b!=None and b.point[z]<res.point[z]:
res=b
if c!=None and c.point[z]<res.point[z]:
res=c
return res
#method to find the maximum of the all
def maximum(self,dimension):
if self.root==None:
print("There is nothing to find minimum")
else:
return self.maximum_(self.root,dimension,1) #this is done in order that the initial height is 1
def maximum_(self,node,dimension,depth):
if dimension=='x':
z=0 #x corresponds to the dimension with point index as 0
elif dimension=='y':
z=1 # y corresponds to the dimension with point index as 1
h=depth%2 #checking in which dimension we are currently working
#after this use pycharm debugger to understand what i have done as i cant explain in comments :-p
if h!=z:
if node.right==None:
t=node.point[z]
return node.point[z]
return self.maximum_(node.right,dimension,depth+1)
a=node.point[z]
if node.left!=None:
b=self.maximum_(node.left,dimension,depth+1)
else:
b=-10000000
if node.right!=None:
c=self.maximum_(node.right,dimension,depth+1)
else:
c=-10000000
return max(a,b,c)
#check whether the points are same or not
def samepoints(self,a,b):
for i in range(len(b)):
if a[i]!=b[i]:
return False
return True
#copy one point to another
def copypoints(self,a,b):
for i in range(len(a)):
a[i]=b[i]
#function of deleting node
def deletenode(self,node,a,height):
if node==None:
return None
flag=0
h=height%2
if h==1:
z=0
k='x'
else:
z=1
k='y'
if self.samepoints(node.point,a):
flag=1
if node.right!=None:
minnode = self.minimum_(node.right,k,height)
self.copypoints(node.point,minnode.point)
node.right=self.deletenode(node.right,minnode.point,height+1)
elif node.left!=None:
minnode=self.minimum_(node.left,k,height)
self.copypoints(node.point,minnode.point)
node.right=self.deletenode(node.left,minnode.point,height+1)
else:
if node.parent.left==node:
node.parent.left=None
else:
node.parent.right=None
node.parent=None
return None
return node
if a[z]<node.point[z]:
node.left=self.deletenode(node.left,a,height+1)
else:
node.right=self.deletenode(node.right,a,height+1)
return node
#wrapping function for deleting node
def deletekdnode(self,a):
return self.deletenode(self.root,a,1)
def search_nearest(self,searchpoint):
m=treenode()
m=closest_point_perfect(self.root,searchpoint,0)
return m.point
def distance(point1,point2):
x1 , y1 = point1
x2 , y2 = point2
dx = x2-x1
dy = y2-y1
d = math.sqrt(dx*dx + dy*dy)
return d
nextBest=treenode()
nextBranch=treenode()
oppositeBranch=treenode()
k=2
def closer(p1,p2,searchpoint):
if p1 is None:
return p2
if p2 is None:
return p1
d1 = distance(p1.point,searchpoint)
d2 = distance(p2.point,searchpoint)
if d1 > d2:
return p2
return p1
def closest_point_perfect(root,searchpoint,depth): # Accurecy : 100%
k=2
if root is None:
return None
axis = depth % k
if searchpoint[axis] < root.point[axis] :
nextBranch=root.left
oppositeBranch=root.right
else:
nextBranch=root.right
oppositeBranch=root.left
best = closer(root,closest_point_perfect(nextBranch,searchpoint,depth+1),searchpoint)
if distance(best.point,searchpoint) > abs(root.point[axis]-searchpoint[axis]):
best = closer(best,closest_point_perfect(oppositeBranch,searchpoint,depth+1),searchpoint)
return best
def closest_point(root,best,searchpoint,depth): #this function has less accurecy
axis = depth % k
if root is None:
return best.point
if best is None or distance(best.point,searchpoint)>distance(searchpoint,root.point):
nextBest = root
else:
nextBest = best
if searchpoint[axis] < root.point[axis]:
nextBranch=root.left
# oppositeBranch=root.right
else:
nextBranch=root.right
# oppositeBranch=root.left
# best=closer(nextBranch,oppositeBranch,searchpoint)
return closest_point(nextBranch,nextBest,searchpoint,depth+1)
def main():
kd=kdtree()
'''kd.insert(6,8)
kd.insert(3,4)
kd.insert(5,6)
kd.insert(4,2)
kd.insert(8,9)
kd.insert(9,4)
kd.insert(9,10)
kd.printkdtree(kd.root)
kd.searchtree(4,2)
print("The minimum in x direction is ",kd.minimum('x').point[0])
print("The minimum in y direction is " , kd.minimum('y').point[1])
print("The maximum in x direction is " , kd.maximum('x'))
print("The maximum in y direction is " , kd.maximum('y'))
a=[5,6]
kd.deletekdnode(a)
kd.printkdtree(kd.root)
a=[3,4]
kd.deletekdnode(a)
print()
kd.printkdtree(kd.root)
print()
a=[100,50]
w=kd.deletekdnode(a)
kd.printkdtree(kd.root)'''
import random
ss=[]
scene = Scene('test')
scene.add(Rectangle((100,100),200,200,(255,255,255)))
for i in range(10):
m=random.randint(100,300)
n=random.randint(100,300)
kd.insert(m,n)
scene.add(Circle((m,n),3,(0,255,0)))
ss.append(distance([m,n],[100,150]))
# kd.printkdtree(kd.root)
ss.sort()
print('List:',ss[0])
p=kd.search_nearest([100,150])
# print(p)
scene.add(Circle((100,150),3,(255,0,255)))
x=distance([100,150],p)
print('Algo:',x)
import time
# time.sleep(1)
scene.add(Circle((p[0],p[1]),3,(0,255,255)))
y='ETA : '+str(round(x/10,2))+' min'
scene.add(Text((50,50),y))
scene.write_svg()
scene.display()
if __name__=='__main__':
main()