Professional Documents
Culture Documents
Hash tables are extremely effective and practical way of implementing dictionaries.
A hash table or hash map is a data structure that uses a hash function to map
identifying values, known as keys (e.g., a person's name), to their
associated values (e.g., their telephone number).
The hash function is used to transform the key into the index of the slot or bucket
where the corresponding value is to be stored.
Hashing:
The process of mapping the keys to their respective positions is called as hashing.
1. Division method
2. Multiplication method
3. Universal method
Division Method:
Example:
Note:
Disadvantage:
Collision:
If two or more keys demand for the same slot in the hash table then collision occurs.
.i.e, if h(k1)=h(k2). (Shown in above example as a Note.)
To resolve collision we are having 2 types of collision resolution techniques.
1. Open hasing.
2. Closed hashing.
Choosing a hash function that minimizes the number of collisions and also
hashes uniformly is another critical issue.
1) Open hashing (Separate Chaining):
In the strategy known as separate chaining, direct chaining, or simply chaining, each
slot of the bucket is a pointer to a linked list that contains the key-value pairs that
hashed to the same location.
Insertion requires adding a new entry record or adding to the end of the list belonging
to the hashed slot (index of the table).
In hash table, The next value(pointer) of the last key in the bucket or slot is kept to null.
Example:
Keys : 0,1,4,2,16,25,36,49,64,81,100
Table size m= 7
In closed hashing all elements are stored in the hash table itself.
Here two keys that hash to the same bucket cannot be placed there.
The key which gets the bucket first will be there and the other must be placed in an
empty bucket below the home bucket.
Note: Here the searching for an next bucket will be done through the hash table in a
circular manner.
Example:
Table size, m =7
42
22
20
10
17
34
Again for 20, as the last slot is already filled, we go through the search in a circular
manner and place it where ever we find an empty slot.
Disadvantage:
Rehashing methods:
Collisions are handled by generating a sequence of rehash values.
1. Linear probing
h(k,i)=(h(k)+i) mod m
2. Quadratic probing
h(k,i)=(h(k)+c1i+c2 i^2) mod m
where c1, c2 are constants c2 !=0.
Table size is 7.
Now see,
In this manner we fill up the hash table using linear probing technique.
Quadratic Probing:
Table size is 7
Probes are the number of attempts made for inserting a key.
Now,
In this manner we fill up the hash table using Quadratic probing technique.
If size is prime, then quadratic probing will find an empty slot in size/2 probes or
fewer.
If i is larger thansize/2, then quadratic probing may fail to find an empty slot.
Table size is 7.
Double hashing:
Now after getting the g(k) value, we should place the key g(k) buckets or slots after the
home bucket (h(k) ) proceeding in a circular manner through the hash table.
Example:
Here the keys(k) are 76,93, 40, 47,10,55
Table size is 7.
Now , 47 should be placed 3 buckets next to the home bucket proceeding in a circular
manner in the hash table.
Now , 55 should be placed 5 buckets next to the home bucket proceeding in a circular
manner in the hash table.
This is done by increasing the table size. If m be the actual size of the hash table, then
m is replaced with R.
R>2m.
After reconstructuring the hash table, the values which are already mapped should be
mapped once again with respect to the new hash table.
Example:
42
22
10
33
For new hash table table size will be 17. (17 is the 1 st prime number after 14).
42
10
33