forked from mingchungx/Hash-Table-Collision-Handling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHashTable Operations.py
292 lines (246 loc) · 10.1 KB
/
HashTable Operations.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
#IB Extended Essay - Linear Probing vs Chaining Collision Handling Python 3.8.3 64-Bit
------------------------------------------------------------
Prerequisites
import time #Measures execution time in seconds
from sys import getsizeof #Measures memory usage in bytes
from math import floor, modf #For multiplicative Hashing only
class HashTablesXXXX: #Hash Table Specification
def __init__(self):
self.size = XXXX #10000 or 1000000 = Small / Large
self.bucket = [XXXX for generation in range(self.size)] #[] for chaining, False for linear probing collisions, None for linear probing non collisions
self.bucket[655] = None #Only For Linear Probing Collisions! This simulates O(n) collisions as "lpvsch" with hash code 656
def print_bucket(self):
print(self.bucket)
def memory_usage(self):
print(getsizeof(self.bucket))
------------------------------------------------------------
Modular Hash and Chaining
def modular_hash(self, key):
hash_code = 0
for character in key:
hash_code += ord(character)
return hash_code % self.size
def __setitem__(self, key, value):
hash_code = self.modular_hash(key)
found = False
for index, element in enumerate(self.bucket):
if len(element) == 2 and element[0] == key:
self.bucket[hash_code][index] = (key, value)
break
if not found:
self.bucket[hash_code].append((key, value))
def __getitem__(self, key):
hash_code = self.modular_hash(key)
for elements in self.bucket[hash_code]:
if elements[0] == key:
return elements[1]
raise Exception("Key does not exist in Hash Table")
def __delitem__(self, key):
hash_code = self.modular_hash(key)
for index, element in enumerate(self.bucket[hash_code]):
if element[0] == key:
del self.bucket[hash_code][index]
return
raise Exception("Key does not exist in Hash Table")
------------------------------------------------------------
Multiplicative Hash and Chaining
def multiplicative_hash(self, key):
constant = (3^40)/(2^64) #Random real number fitting c = s/2^64, 0 < s < 2^w
hash_code = 0
for character in key:
hash_code += ord(character)
return floor(self.size * (modf(hash_code * constant)[0]))
def __setitem__(self, key, value):
hash_code = self.multiplicative_hash(key)
found = False
for index, element in enumerate(self.bucket):
if len(element) == 2 and element[0] == key:
self.bucket[hash_code][index] = (key, value)
break
if not found:
self.bucket[hash_code].append((key, value))
def __getitem__(self, key):
hash_code = self.multiplicative_hash(key)
for elements in self.bucket[hash_code]:
if elements[0] == key:
return elements[1]
raise Exception("Key does not exist in Hash Table")
def __delitem__(self, key):
hash_code = self.multiplicative_hash(key)
for index, element in enumerate(self.bucket[hash_code]):
if element[0] == key:
del self.bucket[hash_code][index]
return
raise Exception("Key does not exist in Hash Table")
------------------------------------------------------------
Modular Hash and Linear Probing
def probe_range(self, index):
return [*range(index, len(self.bucket))] + [*range(0, index)]
def linear_probe(self, key, index):
for index in self.probe_range(index):
if self.bucket[index] is None:
return index
if type(self.bucket[index]) != bool: #This needs to be checked in case of collision simulation of False filled self.bucket
if self.bucket[index][0] == key:
return index
raise Exception("Hash Table is full")
def modular_hash(self, key):
hash_code = 0
for character in key:
hash_code += ord(character)
return hash_code % self.size
def __setitem__(self, key, value):
hash_code = self.modular_hash(key)
if self.bucket[hash_code] is None:
self.bucket[hash_code] = (key, value)
else:
self.bucket[self.linear_probe(key, hash_code)] = (key, value)
def __getitem__(self, key):
hash_code = self.modular_hash(key)
if self.bucket[hash_code] is None:
return None
for index in self.probe_range(hash_code):
if self.bucket[index] is None:
return None
if type(self.bucket[index]) != bool:
if self.bucket[index][0] == key:
return self.bucket[index][1]
def __delitem__(self, key):
hash_code = self.modular_hash(key)
if self.bucket[hash_code] is None:
return None
for index in self.probe_range(hash_code):
if self.bucket[index] is None:
raise Exception("Key does not exist in Hash Table")
if self.bucket[index][0] == key:
self.bucket[index] = None
break
------------------------------------------------------------
Multiplicative Hash and Linear Probing
def probe_range(self, index):
return [*range(index, len(self.bucket))] + [*range(0, index)]
def linear_probe(self, key, index):
for index in self.probe_range(index):
if self.bucket[index] is None:
return index
if type(self.bucket[index]) != bool:
if self.bucket[index][0] == key:
return index
raise Exception("Hash Table is full")
def multiplicative_hash(self, key):
constant = (3^40)/(2^64)
hash_code = 0
for character in key:
hash_code += ord(character)
return floor(self.size * (modf(hash_code * constant)[0]))
def __setitem__(self, key, value):
hash_code = self.multiplicative_hash(key)
if self.bucket[hash_code] is None:
self.bucket[hash_code] = (key, value)
else:
self.bucket[self.linear_probe(key, hash_code)] = (key, value)
def __getitem__(self, key):
hash_code = self.multiplicative_hash(key)
if self.bucket[hash_code] is None:
return None
for index in self.probe_range(hash_code):
if self.bucket[index] is None:
return None
if type(self.bucket[index]) != bool:
if self.bucket[index][0] == key:
return self.bucket[index][1]
def __delitem__(self, key):
hash_code = self.multiplicative_hash(key)
if self.bucket[hash_code] is None:
return None
for index in self.probe_range(hash_code):
if self.bucket[index] is None:
raise Exception("Key does not exist in Hash Table")
if type(self.bucket[index]) != bool:
if self.bucket[index][0] == key:
self.bucket[index] = None
break
------------------------------------------------------------
#Collisions HC = 656
hash_table["lpvshc"] = False
hash_table["lpvcsh"] = False
hash_table["lpvchs"] = False
hash_table["lpvhsc"] = False
hash_table["lpvhcs"] = False
hash_table["lpsvch"] = False
hash_table["lpsvhc"] = False
hash_table["lpscvh"] = False
hash_table["lpschv"] = False
hash_table["lpshvc"] = False
hash_table["lpshcv"] = False
hash_table["lpcvsh"] = False
hash_table["lpcvhs"] = False
hash_table["lpcsvh"] = False
hash_table["lpcshv"] = False
hash_table["lpchvs"] = False
hash_table["lpchsv"] = False
hash_table["lphvsc"] = False
hash_table["lphvcs"] = False
hash_table["lphsvc"] = False
hash_table["lphscv"] = False
hash_table["lphcvs"] = False
hash_table["lphcsv"] = False
hash_table["lvpsch"] = False
hash_table["lvpshc"] = False
hash_table["lvpcsh"] = False
hash_table["lvpchs"] = False
hash_table["lvphsc"] = False
hash_table["lvphcs"] = False
#Apply Operation on...
hash_table["lpvsch"] = True
#Non-Collisions
hash_table["%$gD"] = True
hash_table["]Sz@#"] = True
hash_table["!"] = True
hash_table["~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~"] = True
hash_table["uvjolx"] = True
hash_table["hajtnulkajsx"] = True
hash_table["djbaffcgtipyyfswqnu"] = True
hash_table["aph"] = True
hash_table["wszajlikf"] = True
hash_table["cnnvsvltndqddogr"] = True
hash_table["lrihcznaxwxshs"] = True
hash_table["kqnzfswojjjhmveoyfqrngitubppwtertuuvxprdywpsha"] = True
hash_table["wngnadicsxqfblkrqfygtrfrix"] = True
hash_table["valnrmpew"] = True
hash_table["fqvsojexjkqlqjtlmzweq"] = True
hash_table["gfkapbqgza"] = True
hash_table["facab"] = True
hash_table["ilffibzd"] = True
hash_table["pdpnykngegzqfz"] = True
hash_table["ltcsbmlgi"] = True
hash_table["zfgcdviookjdefi"] = True
hash_table["prfgquaazjliwsobpdpogbhmhhnhaioivwhzrrfydrngwvlglgjrhlbhrt"] = True
hash_table["gdvub"] = True
hash_table["aqsuwiw n g v a"] = True
hash_table["cwafkhh"] = True
hash_table["fwwmizcazhaapq"] = True
hash_table["zobrutuanrttiwlwlgpdwtjshvnp"] = True
hash_table["taezifxqrmwmun"] = True
hash_table["lmsjwtnqsqwchdushxzauqwkbybpnawfsrvurowzptvhvdwzusizpcbbuzfonqgqhhyfnovtsnaogitluacvx"] = True
#Apply operation on...
hash_table["mxibee"] = True
------------------------------------------------------------
hash_table = HashTableXXXX()
#Prior key insertions here...
initial_time = time.time()
#Operation is set here...
For insertions:
hash_table["XXXX"] = True
For retrieving:
print(XXXX)
For deletion:
del XXXX
execution_time = time.time() - initial_time
directory = r"XXXX" #XXXX = File Directory
with open(directory,"a") as dataset:
dataset.write(f"\n{execution_time:.20f}")
hash_table.memory_usage()
------------------------------------------------------------
for execution in range(100):
exec(open(f"XXXX").read()) #XXXX = File Directory