-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprojectdsa.py
More file actions
551 lines (303 loc) · 16.5 KB
/
projectdsa.py
File metadata and controls
551 lines (303 loc) · 16.5 KB
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
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
import datetime
from random import randint
import math
print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))
class VebNonode:
def __init__ (self,u):
self.u=u # Maximum Universe size
self.min=None # to store minimum element among the child nodes
self.max=None # to store maximum element among the child nodes
self.summary=None # to store a summary of clusters
self.cluster=[None]*int(math.sqrt(u))
class VebTree:
def __init__(self,u):
self.root=VebNonode(u)
def High(self,u,x):
# Returns a key according to which cluster is to be chosen
return x//(int(math.sqrt(u)))
def Low(self,u,x):
# Returns a value which is to be inserted in corresponding cluster
return x%(int(math.sqrt(u)))
def EmptyInsert(self,universe,x):
universe.min=x
universe.max=x
def Insert(self,universe,x):
if universe.u<=x or x<0:
print ("ERROR! universe size exceeded.")
return
if universe.min==None:
# Node has no child ie. Empty node
self.EmptyInsert(universe,x)
# Filling the min,max value
else:
if universe.min>x:
# Line 29-32
universe.min,x=x,universe.min
# Updating the minimum or maximum
elif universe.max<x:
universe.max,x=x,universe.max
if x !=universe.min and x!= universe.max:
# For other then leaf nodes
high=self.High(universe.u,x)
low=self.Low(universe.u,x)
if universe.cluster[high]==None:
universe.cluster[high]=VebNonode(int(math.sqrt(universe.u))) # making cluster[high] as a VebNode
self.EmptyInsert(universe.cluster[high],low)
if universe.summary==None:
universe.summary=VebNonode(int(math.sqrt(universe.u))) # making summary as a VebNode
self.Insert(universe.summary,high)
# to insert summary of clusters
else:
self.Insert(universe.cluster[high],low) # Recursion
def search(self,universe,x):
if universe==None or universe.min==None:
return False
if x==universe.min or x==universe.max:
return True
elif x<universe.min or x>universe.max:
return False
else:
high=self.High(universe.u,x)
low=self.Low(universe.u,x)
return self.search(universe.cluster[high],low)
def Minimum(self,universe):
if universe==None or universe.max==None:
return None
return universe.min
def Maximum(self,universe):
if universe==None or universe.min==None:
return None
return universe.max
def Index(self,u,x,y): # to returns the original No.
return int(y*int((math.sqrt(u)))+x)
def Successor(self,universe,x):
if universe==None or universe.min==None:
return None
if universe.min>x:
return universe.min
elif x<universe.max:
if universe.u==2:
if universe.max==1 and universe.min==0:
return 1
else:
return None
high=self.High(universe.u,x)
low=self.Low(universe.u,x)
maxinsubcluster=None
if universe.cluster[high] != None:
# line 77-84, To find successor in its own child-cluster
maxinsubcluster=self.Maximum(universe.cluster[high])
if maxinsubcluster!=None and maxinsubcluster>low:
# to check whether Successor can be present in sub-cluster(low=new x)
new_suc=self.Successor(universe.cluster[high],low)
# recursion to find successor in sub-cluster
if new_suc != None:
return self.Index(universe.u,new_suc,high) # To get the original no.
else:
return None
else:
# line 85-94, To find successor of cluster
suc_cluster=self.Successor(universe.summary,high)
if suc_cluster==None:
if universe.max != None and x<universe.max:
return universe.max
else:
return None
else:
new_suc=self.Minimum(universe.cluster[suc_cluster])
return self.Index(universe.u,new_suc,suc_cluster)
else:
return None
def Predecessor(self,universe,x):
if universe==None or x>=universe.u or universe.min==None:
return None
if universe.max<x:
return universe.max
elif universe.min<x:
high=self.High(universe.u,x)
low=self.Low(universe.u,x)
if universe.u==2:
if universe.max==1 and universe.min==0:
return 0
else:
return None
mininsubcluster=None
if universe.cluster[high]!=None:
# line 107-114 to find predecessor in sub-cluster
mininsubcluster=self.Minimum(universe.cluster[high])
if mininsubcluster!=None and low>mininsubcluster: # to check whether predecessor can be present in sub-cluster
new_pred=self.Predecessor(universe.cluster[high],low) # recursion to find predecessor in sub-cluster
if new_pred != None:
# if predecessors present
return self.Index(universe.u,new_pred,high)
# to get the original no.
else:
return None
else:
# line 115-124 To find the Predecessor of cluster cluster
pred_cluster=self.Predecessor(universe.summary,high) # to search predecessor of sub-cluster in summary
if pred_cluster==None:
# if predecessor not found
if universe.min!=None and x>universe.min: # *In case: only min and max are present
return universe.min
else:
return None
# otherwise no prede.
else:
new_pred=self.Maximum(universe.cluster[pred_cluster]) # predecessor=maximum of the predecessor of cluster
return self.Index(universe.u,new_pred,pred_cluster) # to get the original value and return it
else:
return None
def Delete(self,universe,x):
if universe==None or universe.min==None:
return False
if x<universe.min or x>universe.max:
return False
if x==universe.min:
if x==universe.max:
universe.min=None
universe.max=None
return -1
if universe.summary==None:
universe.min=universe.max
return -2
else:
y=self.Successor(universe,x)
high=self.High(universe.u,y)
low=self.Low(universe.u,y)
universe.min=y
delvalue=self.Delete(universe.cluster[high],low)
if delvalue==-1:
if universe.cluster[high].min == None:
universe.cluster[high]=None
if universe.summary.min==universe.summary.max:
universe.summary=None
else:
return self.Delete(universe.summary,high)
elif x==universe.max:
if x==universe.min:
universe.min=None
universe.max=None
return -1
if universe.summary==None:
universe.max=universe.min
return -2
else:
y=self.Predecessor(universe,x)
high=self.High(universe.u,y)
low=self.Low(universe.u,y)
universe.max=y
delvalue=self.Delete(universe.cluster[high],low)
if delvalue==-1:
if universe.cluster[high].min == None:
universe.cluster[high]=None
if universe.summary.min==universe.summary.max:
universe.summary=None
else:
return self.Delete(universe.summary,high)
else:
high=self.High(universe.u,x)
low=self.Low(universe.u,x)
if universe.cluster[high]==None:
return False
delvalue=self.Delete(universe.cluster[high],low)
if delvalue==-1:
if universe.cluster[high].min == None:
universe.cluster[high]=None
if universe.summary.min==universe.summary.max:
universe.summary=None
else:
return self.Delete(universe.summary,high)
def logf(x):
return(int(math.log(x,2)))
def integerGenerator(a,b,c,d):
a='{0:08b}'.format(a)
b='{0:08b}'.format(b)
c='{0:08b}'.format(c)
d='{0:08b}'.format(d)
a+=b
a+=c
a+=d
return(int(a,2))
k=0
print("enter the universe size:")
tempSize=int(input())
if tempSize==1 or tempSize==2:
x=0
else:
universeSize2=logf(logf(tempSize))+1
x=pow(2,pow(2,universeSize2))
print(x)
vt=VebTree(x)
while(k!=1):
print()
print()
print("choose options for VEB Operations")
print("1.Insertion in tree")
print("2.Predecessor in tree")
print("3.Successor in tree")
print("4.Search in tree")
print("5.Delete in tree")
print("6.Application of tree")
print("7.Exit")
choose=int(input())
if choose==1:
print("Enter the no. of elements u want to enter")
n=int(input())
for j in range(n):
print("Enter element "+str(j))
num=int(input())
vt.Insert(vt.root,num)
elif choose==2:
print("Enter the element to find its predecessor")
num=int(input())
print("Predecessor is : "+str(vt.Predecessor(vt.root,num)))
elif choose==3:
print("Enter the element to find its sucessor")
num=int(input())
print("Sucessor is : " +str(vt.Successor(vt.root,num)))
elif choose==5:
print("Enter the element to delete")
num=int(input())
if vt.Delete(vt.root,num)==False:
print("Delete Unsucessful")
else:
print("Delete Sucessful")
elif choose==4:
print("Enter the element to search")
num=int(input())
print("Number is there: "+str(vt.search(vt.root,num)))
elif choose==6:
universeSize=2**32
vt=VebTree(universeSize)
#creating a universe of size 2^32 according to the IV04 with max size of IP address To be 2^32-1
#creating own ranges of IP addresses their corresponding ports through with data is to be sent
# assigning 300 ports to our router
# so no. of ranges will be 300 in count and no ranges are overlapping
# storing only beginning of ranges to our van emde boas tree in the van emde boas tree
#using dictionary adt to store port numbers of various ranges
portN={}
i=0
while i<300:
j=randint(0,universeSize-1)
if vt.search(vt.root,j)==False:
vt.Insert(vt.root,j)
portN[j]=i
i+=1
#inputing any ip address and assigning it a port through searching its nearest range beginning
print("Enter no. of Ip Numbers For Data Transfer")
n=int(input())
for i in range(n):
print("Enter the IP Address for data transfer")
a,b,c,d=input().split('.')
IPinteger=integerGenerator(int(a),int(b),int(c),int(d))
begOfIpRange=vt.Predecessor(vt.root,IPinteger)
if begOfIpRange!=False and vt.Successor(vt.root,IPinteger)!=None:
print("Connection Established data packets transfer through port No. "+str(portN[begOfIpRange]))
else:
print("Connection Unsucessful")
elif choose==7:
k=1
else:
print("Plz Enter correct value")
print('Timestamp: {:%Y-%m-%d %H:%M:%S}'.format(datetime.datetime.now()))