-
Notifications
You must be signed in to change notification settings - Fork 8
/
hash search.jl
357 lines (249 loc) · 8.12 KB
/
hash search.jl
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
# coding: utf-8
# In[1]:
#i would recommend you to read algorithm and data structure before messing with hashtable
# http://interactivepython.org/runestone/static/pythonds/SortSearch/Hashing.html
#this script features the hash function described in the book, which is mod eleven %11
#we apply hash function to all the values in a given list
#we shall obtain the output value for each element
#and assuming this is a perfect hash function
#each value from the list should have a unique hash value
#we can create a dictionary based on the unique hash value
#and assign all the values from the list into dictionary
#when we wanna search for something
#we just need to apply hash function to the target
#and make query on the dictionary for that particular hash value
#O(1), simple AF, not really...
#we could encounter the same hash value for different values from the list
#this is so called hash collision
#there are three ways of solving hash collision in the book
#which are chaining, linear probing and quadratic probing
#lets deliberately create an imperfect hash function to see how it goes
# In[2]:
arr=[21,55,89,67,12,12]
# In[3]:
#hash chaining
#the easiest one
#basically stack all the collision together
#form a list under that hash value
#does it make search easier?
#not really, if the list under one hash value gets too large
#it would slow down the search
#the good thing about this method is that you always have all the values in the dictionary
#for other methods, you either have to increase hash table size or drop values
# In[4]:
#the first function is to create a dictionary
#assign values from the list to the dictionary based on hash value
function create_hashtable(arr,default=11)
#create hashtable
hashtable=Dict()
#initialize
for i in 0:(default-1)
hashtable[i]=[]
end
#add value into hashtable
#using folding method
for i in arr
push!(hashtable[i%default],i)
end
return hashtable
end
# In[5]:
#now its the search part
#we just apply hash function on target and get hash value
#we look up the hash value in dictionary
function hash_search(target,arr,default=11)
#get hashtable
hashtable=create_hashtable(arr,default)
#find value through hash value
if target in hashtable[target%default]
return true
else
return false
end
end
# In[6]:
hash_search(12,arr)
# In[7]:
#linear probing
#when collision occurs, we try to find the next empty slot to store it
#it sounds reasonable but it is also trouble
#what if we run through the whole dictionary
#and there is no slot?
#we can choose to drop the values
#or we can reset the hash function or expand the dictionary
#in the best case, it is faster than chaining
#in the worst case, it is slower
# In[8]:
#the first function is to create a dictionary
#assign values from the list to the dictionary based on hash value
function create_hashtable(arr,default=11)
#create hashtable
hashtable=Dict()
#unwanted is a temporary list to append collision items
unwanted=[]
#keep track of values with no allocation
badhash=[]
#initialize
for i in 0:(default-1)
hashtable[i]=""
end
#add value into hashtable
for i in arr
if hashtable[i%default]==""
hashtable[i%default]=i
else
#make sure every collision will be tracked
push!(unwanted,i)
end
end
for i in unwanted
#c is a counter
#in the inner loop
#c is to determine whether we have gone through the entire list
c=0
modnumber=i%default
stop=false
while !stop && c<=default-1
#when modnumber exceeds ten, we return it to 0
#alternatively we can use mod eleven %11
modnumber+=1
if modnumber>default-1
modnumber=0
end
#when the next slot isnt empty, we keep iterating
if hashtable[modnumber]==""
hashtable[modnumber]=i
stop=true
end
c+=1
end
#make sure that we will print out those items which didnt get assigned
if !stop
push!(badhash,i)
end
end
#if the hashing is imperfect, we print out badhash list
if !isempty(badhash)
println(badhash)
end
return hashtable
end
# In[9]:
#the search part is very similar to the chaining one
function hash_search(target,arr,default=11)
#get hashtable
hashtable=create_hashtable(arr,default)
#find value through hash value
modnumber=target%default
if target in hashtable[modnumber]
return true
else
#when we cannot find the value at hash value
#we begin our linear probing
#its the same process as the hash function
#except we only need to return T/F
counter=0
while counter<=default-1
modnumber+=1
if modnumber>default-1
modnumber=0
end
if target==hashtable[modnumber]
return true
end
counter+=1
end
end
return false
end
# In[10]:
hash_search(89,arr)
# In[11]:
#quadratic probing
#it sounds math intensive with the word quadratic
#as a matter of fact, it is simple AF
#we just replace the add one method with add quadratic values
# In[12]:
#the first function is to create a dictionary
#assign values from the list to the dictionary based on hash value
function create_hashtable(arr,default=11)
#create hashtable
hashtable=Dict()
unwanted=[]
badhash=[]
#initialize
for i in 0:(default-1)
hashtable[i]=""
end
#add value into hashtable
for i in arr
if hashtable[i%default]==""
hashtable[i%default]=i
else
push!(unwanted,i)
end
end
for i in unwanted
c=1
modnumber=i%default
stop=false
#the loop is basically the same as linear probing
#except we add quadratic value
#note that its quite difficult
#to determine whether we have been through the entire list
#so i still set counter limit at 10
while !stop && c<=default
modnumber+=c^2
#note that i use mod eleven %11 when iteration exceeds hash table size
if modnumber>default-1
modnumber=modnumber%default
end
if hashtable[modnumber]==""
hashtable[modnumber]=i
stop=true
end
c+=1
end
if !stop
push!(badhash,i)
end
end
if !isempty(badhash)
println(badhash)
end
return hashtable
end
# In[13]:
#the search is basically the same as linear probing
#except linear part is substituted with quadratic
function hash_search(target,arr,default=11)
#get hashtable
hashtable=create_hashtable(arr,default)
#find value through hash value
modnumber=target%default
if target in hashtable[modnumber]
return true
else
counter=1
while counter<=default
modnumber+=counter^2
if modnumber>default-1
modnumber=modnumber%default
end
if target==hashtable[modnumber]
return true
end
counter+=1
end
end
return false
end
# In[14]:
hash_search(78,[21,55,89,67,12,12,12,12,12,12,12,12,12,12,78])
# In[15]:
#we get False in the end
#its quite interesting that for the same hash value 67,12,78
#we can store 67 in hash table but not 78
#67 and 12 are processed earlier than 78
#quadratic probing doesnt iterate through all slots
#all empty slots we can iterate have been occupied by the time we reach 78