I was recently asked ‘how would you implement a hastable’. I know the hashing algorithm is critical as the less collisions the better WRT performance, but what algorithm/data structure should be employed to deliver amortized constant time {O(1)} for insert/delete/lookups?
Share
Hash tables have two main possibilities:
can let your table grow on the fly]. Once a conflict has met – you need to use a second hash function to find the next entree that the element will be mapped to. Note this solution has some troubles [which can be solved] when your hash table also allows deletions. [Special mark for “deleted” entrees]
The important part about hash tables [in both solutions] in order to allow armotorized O(1) insert/del/look up – is allocating a bigger table and rehashing once a pre defined load factor was reached.
EDIT: complexity analsis:
Assume a load factor of
pfor somep < 1.pThus the mean of array accesses is:Sigma(i * p^(i-1) * (1-p)) for each i in [1,n]This gives you:Sigma(i * p^(i-1) * (1-p)) = (1-p) * Sigma(i * p^(i-1)) <= (1-p) * 1/(p-1)^2 = 1-p = CONST. [have a look at the correctness of Sigma(i * p^(i-1)) < 1/(p-1)^2 in wolfram alpha]. Thus resulting on average a constant number of accesses to the array. Also: You might need to rehash all elements after the load factor was reached, resulting inO(n)accesses to the array. This results inn * O(1)ops [adding n elements] and1 * O(n)op [rehashing], so you get:(n * O(1) + 1 * O(n)) / n = O(1)armotorized time.