forked from mingchungx/Hash-Table-Collision-Handling
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHash Table Operationsv2.py
251 lines (203 loc) · 8.56 KB
/
Hash Table Operationsv2.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
#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
import random #For randomization of strings
import string #For random strings
class HashTableXXXX: #Hash Table Specification
def __init__(self):
self.size = XXXX #Load Factor; s = 997, h = 199, l = 109
self.bucket = [XXXX for generation in range(self.size)] #[] for chaining, None for linear probing
def print_bucket(self):
print(self.bucket)
def memory_usage(self):
return 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
------------------------------------------------------------
For Insertion:
hash_table = HashTableXXXX()
def insert(hash_table):
letters = string.ascii_lowercase
for i in range(100): #N = 100
key = "".join(random.choice(letters) for i in range(random.randint(1,26)))
hash_table[key] = False #Generates a random combination of letters from lengths 1-26
initial_time = time.time()
insert(hash_table)
execution_time = time.time() - initial_time
------------------------------------------------------------
For Retrieval:
hash_table = HashTableXXXX()
keys = [] #Store a bunch of keys to randomly retrieve from
def insert(hash_table):
global keys
letters = string.ascii_lowercase
for i in range(100): #N = 100
key = "".join(random.choice(letters) for i in range(random.randint(1,26)))
keys.append(key)
hash_table[key] = False #Generates a random combination of letters from lengths 1-26
insert(hash_table)
def retrieve(hash_table):
global keys
for key in keys:
print(hash_table[key])
initial_time = time.time()
retrieve(hash_table)
execution_time = time.time() - initial_time
------------------------------------------------------------
t_directory = r"XXXX" #XXXX = File Directory
m_directory = r"XXXX"
with open(t_directory,"a") as time_data:
time_data.write(f"\n{execution_time:.20f}")
with open(m_directory, "a") as memory_data:
memory_data.write(f"\n{hash_table.memory_usage()}")
------------------------------------------------------------
for execution in range(100):
exec(open(f"XXXX").read()) #XXXX = File Directory