-
Notifications
You must be signed in to change notification settings - Fork 0
/
research.py
388 lines (362 loc) · 14.4 KB
/
research.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
import time
import sys
import graphviz
import os
import gc
import copy
import hash_table
import rb_tree
import random
import matplotlib.pyplot as plt
import numpy
def clear_all_directory():
dir = 'result'
for f in os.listdir(dir): #clear previous info
os.remove(os.path.join(dir, f))
def rb_tree_manual_input(): #пользовательская проверка работы дерева и корректности реализации rb-tree
clear_all_directory()
print('Enter the count of pair (key,value) for add in rb-tree')
count = int(input())
print('Enter the pairs (key,value) to add in rb-tree')
nodes_to_add = []
for i in range(count):
node = list(map(int, input().split()))
nodes_to_add.append(node)
tree = rb_tree.RBTree()
for node in nodes_to_add:
tree.insert(node[0], node[1])
tree.print('rb_tree_graphviz')
print('Enter the key to delete')
keys_to_delete = list(map(int, input().split()))
for keys in keys_to_delete:
tree.delete(keys)
tree.print('rb_tree_after_delete')
def hash_table_manual_input(): #пользовательская проверка работы хеш-таблицы и корректности ее реализации
print('Enter the count of pair (key,value) for add in hash-table')
count = int(input())
print('Enter the pairs (key,value) to add in hash-table')
nodes_to_add = []
for i in range(count):
node = list(map(int, input().split()))
nodes_to_add.append(node)
table = hash_table.HashTable(100)
for node in nodes_to_add:
table.insert(node[0], node[1])
print(table)
print('Enter the key to delete')
keys_to_delete = list(map(int, input().split()))
for keys in keys_to_delete:
table.delete(keys)
print('Table after delete')
print(table)
def create_graphics_with_log(n, times, title_name): #создать график на основе кол-ва элементов и массива times
plt.figure()
plt.title(title_name)
plt.xlabel("n, count")
plt.ylabel("t, ns")
plt.scatter(n, times, s=0.5, color='blue')
plt.plot(n, numpy.log2(n) * 10**3, color='green')
plt.savefig("result/{}.png".format(title_name))
plt.show()
def create_graphics(n, times, title_name):
plt.figure()
plt.title(title_name)
plt.xlabel("n, count")
plt.ylabel("t, ns")
plt.scatter(n, times, s=0.2, color='orange')
plt.savefig("result/{}.png".format(title_name))
plt.show()
def create_compared_graphics(n, times_tree, times_table, title_name):
plt.figure()
plt.title(title_name)
plt.xlabel("n, count")
plt.ylabel("t, ns")
plt.scatter(n, times_tree, s=0.2, color='blue', label='rb-tree')
plt.scatter(n, times_table, s=0.2, color='orange', label='hash-table')
plt.legend(loc="best")
plt.savefig("result/{}.png".format(title_name))
plt.show()
def average_case_rb_tree_insert():
print('Average-case for RB-tree inserting...')
count = 10001
research_count = 100
n = [i for i in range(1, count)]
insert_time = [0 for _ in range(1, count)]
for i in range(research_count):
nodes_to_add = [[random.randint(0, int(2*count)), random.randint(0, 1000)] for i in range(1, count)]
random.shuffle(nodes_to_add)
tree = rb_tree.RBTree()
for idx, elem in enumerate(nodes_to_add):
gc.disable() #отключение сборщика мусора
start = time.perf_counter_ns()
tree.insert(elem[0], elem[1])
end = time.perf_counter_ns() - start
gc.enable()
insert_time[idx] += end
del tree
for i in range(len(insert_time)):
insert_time[i] /= research_count
create_graphics_with_log(n, insert_time, "RB-tree, average-case for insert")
def average_case_rb_tree_delete():
print('Average-case for RB-tree deleting...')
count = 10001
research_count = 100
n = [i for i in range(1, count)]
delete_time = [0 for i in range(1, count)]
for i in range(research_count):
nodes_to_add = [[random(count*3, count*5), random.randint(0, 1000)] for i in range(1, count)]
random.shuffle(nodes_to_add)
tree = rb_tree.RBTree()
for elem in nodes_to_add:
tree.insert(elem[0], elem[1])
for j in range(1, count):
idx = random.randint(0, len(nodes_to_add) - 1)
key_to_delete = nodes_to_add[idx][0]
gc.disable()
start = time.perf_counter_ns()
tree.delete(key_to_delete)
end = time.perf_counter_ns() - start
gc.enable()
delete_time[j-1] += end
nodes_to_add.pop(idx)
del tree
delete_time.reverse()
for i in range(len(delete_time)):
delete_time[i] /= research_count
create_graphics_with_log(n, delete_time, "RB-tree, average-case for delete")
def average_case_rb_tree_search():
print('Average-case for RB-tree searching...')
count = 10001
research_count = 100
n = [i for i in range(1, count)]
search_time = [0 for i in range(1, count)]
for i in range(research_count):
nodes_to_add = [[i, random.randint(0, 1000)] for i in range(1, count)]
random.shuffle(nodes_to_add)
tree = rb_tree.RBTree()
for elem in nodes_to_add:
tree.insert(elem[0], elem[1])
for j in range(1, count):
idx = random.randint(0, len(nodes_to_add) - 1)
key_to_search = nodes_to_add[idx][0]
gc.disable()
start = time.perf_counter_ns()
tree.search(key_to_search)
end = time.perf_counter_ns() - start
gc.enable()
search_time[j-1] += end
tree.delete(key_to_search)
nodes_to_add.pop(idx)
search_time.reverse()
for i in range(len(search_time)):
search_time[i] /= research_count
create_graphics_with_log(n, search_time, "RB-tree, average-case for search")
def average_case_hash_table_insert(hash_type):
print('Hash-table inserting...')
count = 10001
research_count = 100
n = [i for i in range(1, count)]
insert_time = [0 for _ in range(1, count)]
for i in range(research_count):
nodes_to_add = [[random.randint(0, count), random.randint(0, 1000)] for i in range(1, count)]
times = []
table = hash_table.HashTable(count * 3, hash_type)
for idx, node in enumerate(nodes_to_add):
gc.disable() #отключение сборщика мусора
start = time.perf_counter_ns()
table.insert(node[0], node[1])
end = time.perf_counter_ns() - start
gc.enable()
insert_time[idx] += end
del table
for i in range(len(insert_time)):
insert_time[i] /= research_count
create_graphics(n, insert_time, "Hash-table, average insert, {}".format(hash_type))
def average_case_hash_table_delete(hash_type):
print('Average-case for Hash-table deleting...')
count = 10001
research_count = 100
n = [i for i in range(1, count)]
delete_time = [0 for i in range(1, count)]
for i in range(research_count):
nodes_to_add = [[random.randint(0, count*2), random.randint(0, 1000)] for i in range(1, count)]
table = hash_table.HashTable(count * 3, hash_type)
for node in nodes_to_add:
table.insert(node[0], node[1])
for j in range(1, count):
idx = random.randint(0, len(nodes_to_add) - 1)
key_to_delete = nodes_to_add[idx][0]
gc.disable()
start = time.perf_counter_ns()
table.delete(key_to_delete)
end = time.perf_counter_ns() - start
gc.enable()
delete_time[j - 1] += end
nodes_to_add.pop(idx)
del table
delete_time.reverse()
for i in range(len(delete_time)):
delete_time[i] /= research_count
create_graphics(n, delete_time, "Hash-table, average-case for delete, {}".format(hash_type))
def average_case_hash_table_search(hash_type):
print('Average-case for Hash-table searching...')
count = 10001
research_count = 1
n = [i for i in range(1, count)]
search_time = [0 for i in range(1, count)]
for i in range(research_count):
nodes_to_add = [[random.randint(0, count*2), random.randint(0, 1000)] for i in range(1, count)]
table = hash_table.HashTable(count * 3, hash_type)
for node in nodes_to_add:
table.insert(node[0], node[1])
for j in range(1, count):
idx = random.randint(0, len(nodes_to_add) - 1)
key_to_search = nodes_to_add[idx][0]
gc.disable()
start = time.perf_counter_ns()
table.search(key_to_search)
end = time.perf_counter_ns() - start
gc.enable()
search_time[j - 1] += end
table.delete(key_to_search)
nodes_to_add.pop(idx)
del table
search_time.reverse()
for i in range(len(search_time)):
search_time[i] /= research_count
create_graphics(n, search_time, "Hash-table, average-case for search, {}".format(hash_type))
def compare_insert(hash_type):
print('RB-tree Vs Hash-table inserting...')
count = 10001
research_count = 100
n = [i for i in range(1, count)]
insert_time_tree = [0 for _ in range(1, count)]
insert_time_table = [0 for _ in range(1, count)]
for i in range(research_count):
nodes_to_add = [[q, random.randint(0, 1000)] for q in range(1, count)]
random.shuffle(nodes_to_add)
tree = rb_tree.RBTree()
table = hash_table.HashTable(count * 2, hash_type)
for idx, elem in enumerate(nodes_to_add):
gc.disable() # отключение сборщика мусора
start = time.perf_counter_ns()
tree.insert(elem[0], elem[1])
end = time.perf_counter_ns() - start
gc.enable()
insert_time_tree[idx] += end
gc.disable() # отключение сборщика мусора
start = time.perf_counter_ns()
table.insert(elem[0], elem[1])
end = time.perf_counter_ns() - start
gc.enable()
insert_time_table[idx] += end
del tree
del table
for i in range(len(n)):
insert_time_tree[i] /= research_count
insert_time_table[i] /= research_count
create_compared_graphics(n, insert_time_tree, insert_time_table, "RB-tree Vs Hash-table ({}) insert".format(hash_type))
def compare_delete(hash_type):
print('RB-tree Vs Hash-table deleting...')
count = 10001
research_count = 100
n = [i for i in range(1, count)]
delete_time_tree = [0 for i in range(1, count)]
delete_time_table = [0 for i in range(1, count)]
for i in range(research_count):
nodes_to_add = [[q, random.randint(0, 1000)] for q in range(1, count)]
random.shuffle(nodes_to_add)
tree = rb_tree.RBTree()
table = hash_table.HashTable(count * 2, hash_type)
for elem in nodes_to_add:
tree.insert(elem[0], elem[1])
table.insert(elem[0], elem[1])
for j in range(1, count):
idx = random.randint(0, len(nodes_to_add) - 1)
key_to_delete = nodes_to_add[idx][0]
gc.disable()
start = time.perf_counter_ns()
tree.delete(key_to_delete)
end = time.perf_counter_ns() - start
gc.enable()
delete_time_tree[j - 1] += end
gc.disable()
start = time.perf_counter_ns()
table.delete(key_to_delete)
end = time.perf_counter_ns() - start
gc.enable()
delete_time_table[j - 1] += end
nodes_to_add.pop(idx)
del tree
del table
delete_time_tree.reverse()
delete_time_table.reverse()
for i in range(len(n)):
delete_time_tree[i] /= research_count
delete_time_table[i] /= research_count
create_compared_graphics(n, delete_time_tree, delete_time_table, "RB-tree Vs Hash-table ({}) delete".format(hash_type))
def compare_search(hash_type):
print('RB-tree Vs Hash-table searching...')
count = 10001
research_count = 100
n = [i for i in range(1, count)]
search_time_tree = [0 for i in range(1, count)]
search_time_table = [0 for i in range(1, count)]
for i in range(research_count):
nodes_to_add = [[q, random.randint(0, 1000)] for q in range(1, count)]
random.shuffle(nodes_to_add)
tree = rb_tree.RBTree()
table = hash_table.HashTable(count * 2, hash_type)
for elem in nodes_to_add:
tree.insert(elem[0], elem[1])
table.insert(elem[0], elem[1])
for j in range(1, count):
idx = random.randint(0, len(nodes_to_add) - 1)
key_to_search = nodes_to_add[idx][0]
gc.disable()
start = time.perf_counter_ns()
tree.search(key_to_search)
end = time.perf_counter_ns() - start
gc.enable()
search_time_tree[j - 1] += end
gc.disable()
start = time.perf_counter_ns()
table.search(key_to_search)
end = time.perf_counter_ns() - start
gc.enable()
search_time_table[j - 1] += end
table.delete(key_to_search)
tree.delete(key_to_search)
nodes_to_add.pop(idx)
del tree
del table
search_time_tree.reverse()
search_time_table.reverse()
for i in range(len(n)):
search_time_tree[i] /= research_count
search_time_table[i] /= research_count
create_compared_graphics(n, search_time_tree, search_time_table, "RB-tree Vs Hash-table ({}) search".format(hash_type))
def check_rb_tree():
average_case_rb_tree_insert()
average_case_rb_tree_delete()
average_case_rb_tree_search()
def check_hash_table():
print('Choose hash-type: linear, quadratic, double')
hash_type = input()
average_case_hash_table_insert(hash_type)
average_case_hash_table_delete(hash_type)
average_case_hash_table_search(hash_type)
def compare_structure():
print('Choose hash-type: linear, quadratic, double')
hash_type = input()
compare_insert(hash_type)
compare_delete(hash_type)
compare_search(hash_type)
if __name__ == '__main__':
clear_all_directory()
rb_tree_manual_input()
hash_table_manual_input()
check_rb_tree()
check_hash_table()
compare_structure()
print('Research is complete! Check */result folder')