Backend | Solutions to Hash Collision
by Botao Xiao
Here I listed several methods to solve the problem of Hash collision.
Open address Method
The open address method will assign each key to a bucket.
Linear probing rehashing
- Formula: Hi=(H(key + di)) MOD m di=1,2,…,k(k<=m-1).
- di is linear, di = 1, 2, …, m - 1.
- if H(key), we try H(key + i), i = 1, 2, …, M-1
Second order probing rehashing
- Formula: Hi=(H(key + di)) MOD m di = -1, 1, -4, 4, .., ((m - 1) / 2) ^2
- di is in second order.
Fake random probing rehashing
- Formula: Hi=(H(key + di)) MOD m, di is a fake random number.
- This method is like hashhash.
- seed to random must be controllable.
Hashhash
Hashhash means when we find a collison, we can apply hash calculation again:
- Formula: hash(hash…hash(key)), until we find a empty bucket.
- Pros: Tracable.
- Cons: Require too many calculation.
Chaining Method
- HashMap here is a K-V map where V here is a List holding the objects.
- If there happens a hash collision, we need to append the object to the end of the list.
- Pros:
- Implement some algorithms like ip-defreagment.
- Space saving
- Cons: If the size of the hashmap is small, it will create some more space.
Pulic collision Area
If a hash collision happends, we save it to a public collision area.
Reference
Subscribe via RSS