Seminar 5
Seminar 5
SEMINAR
CS3353-C PROGRAMMING AND DATA STRUCTURES
Open Addressing Collision Handling technique in Hashing
Insert(k): Keep probing until an empty slot is found. Once an empty slot is
found, insert k.
Search(k): Keep probing until the slot’s key doesn’t become equal to k or an
empty slot is reached.
Delete(k): Delete operation is interesting. If we simply delete a key, then the
search may fail. So slots of deleted keys are marked specially as “deleted”.
The insert can insert an item in a deleted slot, but the search doesn’t stop at
a deleted slot.
1. Linear Probing:
In linear probing, the hash table is searched sequentially that starts from
the original location of the hash. If in case the location that we get is already
occupied, then we check for the next location.
The function used for rehashing is as follows: rehash(key) =
(n+1)%table-size.
For example, The typical gap between two probes is 1 as seen in the
example below:
Let hash(x) be the slot index computed using a hash function and S be the table
size
If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
Example: Let us consider a simple hash function as “key mod 5” and a
sequence of keys that are to be inserted are 50, 70, 76, 85, 93.
Implementation :
Program to implement Hash Table using Open Addressing
The task is to design a general Hash Table data structure with Collision
case handled and that supports the Insert(), Find(), and Delete() functions.
Examples:
Suppose the operations are performed on an array of pairs, {{1, 5}, {2, 15},
{3, 20}, {4, 7}}. And an array of capacity 20 is used as a Hash Table:
1. Insert(1, 5): Assign the pair {1, 5} at the index (1%20 =1) in the Hash
Table.
2. Insert(2, 15): Assign the pair {2, 15} at the index (2%20 =2) in the Hash
Table.
3. Insert(3, 20): Assign the pair {3, 20} at the index (3%20 =3) in the Hash
Table.
4. Insert(4, 7): Assign the pair {4, 7} at the index (4%20 =4) in the Hash
Table.
5. Find(4): The key 4 is stored at the index (4%20 = 4). Therefore, print the 7
as it is the value of the key, 4, at index 4 of the Hash Table.
6. Delete(4): The key 4 is stored at the index (4%20 = 4). After deleting Key
4, the Hash Table has keys {1, 2, 3}.
7. Find(4): Print -1, as the key 4 does not exist in the Hash Table.
2. Quadratic Probing
If you observe carefully, then you will understand that the interval between
probes will increase proportionally to the hash value. Quadratic probing is a
method with the help of which we can solve the problem of clustering that was
discussed above. This method is also known as the mid-square method. In this
method, we look for the i2‘th slot in the ith iteration. We always start from the
original hash location. If only the location is occupied then we check the other
slots.
let hash(x) be the slot index computed using hash function.
If slot hash(x) % S is full, then we try (hash(x) + 1*1) % S
If (hash(x) + 1*1) % S is also full, then we try (hash(x) + 2*2) % S
If (hash(x) + 2*2) % S is also full, then we try (hash(x) + 3*3) % S
The intervals that lie between probes are computed by another hash function.
Double hashing is a technique that reduces clustering in an optimized way. In
this technique, the increments for the probing sequence are computed by using
another hash function. We use another hash function hash2(x) and look for the
i*hash2(x) slot in the ith rotation.
let hash(x) be the slot index computed using hash function.
If slot hash(x) % S is full, then we try (hash(x) + 1*hash2(x)) % S
If (hash(x) + 1*hash2(x)) % S is also full, then we try (hash(x) + 2*hash2(x)) %
S
If (hash(x) + 2*hash2(x)) % S is also full, then we try (hash(x) + 3*hash2(x)) %
S
Example: Insert the keys 27, 43, 692, 72 into the Hash Table of size 7. where
first hash-function is h1(k) = k mod 7 and second hash-function is h2(k) = 1 +
(k mod 5)
S.No
. Separate Chaining Open Addressing