Hashing: Dr. Yasir Faheem and M. Salman Niazi
Hashing: Dr. Yasir Faheem and M. Salman Niazi
hash table
0
1
key hash
pos 2
function
3
:
:
TABLESIZE - 1
Hash Function
Consider a small company , the company has about
100 employees.
The Company uses about 5 digit employee ID as
primary key.
The key values ranges from 00000 to 99999.
For 100 records , we can’t possibly allocate an
array of 100000.
Hash Function
We use an array of 100 elements.
Our hash function in this case , only needs last two
digits of the employee ID.
The has function is :-
hash(Key)= EmployeeID % 100
Hashing
hash table
0
1
Key= Key Mod 100
04 2
50704 3
:
:
TABLESIZE - 1
Hash Function
1. Index Hash(int Id)
2. {
3. return(Id % 100);
4. }
50700 3
4 50704,Ahmad
5
6
RetreiveElement Operation
hash table
0 50700,Rizwan
1
Key= Key Mod 100
04 2
50704 3
4 50704,Ahmad
5
Found
6
InsertElement Operation
To be able to retrieve right record using hash
function, we must put records in their right place
using the same hash function.
Hash function also determines where in the array to
store the element with a specific key.
Here, we present a simple insert function , which
assumes that the array slot at the index returned by
our hash function is free.
InsertElement Operation
1. void InsertElement(ElementType
*List,ElementType NewElement)
2. {
3. Index Location;
4. Location=Hash(NewElement.Id);
5. List[Location]=NewElement;
6. }
InsertElement Operation
Insert (50704,Ahmad)
hash table
1
Key= Key Mod 100
04 2
50704 3
4 50704,Ahmad
5
6
InsertElement Operation
Insert(50700,Rizwan)
hash table
0 50700,Rizwan
1
Key= Key Mod 100
00 2
50700 3
4 50704,Ahmad
5
6
Collisions
Two different keys e.g 01234 , 91234 might hash to
the same address.
This situation is called collision.
The problem of avoiding these collisions is the
biggest challenge in designing a good hash function.
A good hash function minimizes collisions by
spreading the elements throughout the array.
It is very difficult to eliminate collisions entirely.
Number 701466802
Collision
Collision Occurs
Occurs
My hash
value is [2].
1
Aho Hash 2
0
Function
3
6
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1
Kruse Hash 2
5
Function
3
5 Kruse
6
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1 Standish
Standish Hash 2
1
Function
3
5 Kruse
6
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1 Standish
Horowitz Hash 2
5
Function
3
5 Kruse
6 Horowitz
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowitz, Langsam, Sedgewick, Knuth
hash table
0 Aho
1 Standish
5 Kruse
6 Horowitz
Linear Probing
To insert using linear probing , we use the
following algorithm :-
Location = Hash ( Key of NewElement)
While( Key of Info[Location] is not empty )
Location = Next Location
Info[Location]= NewElement
Problems with Linear Probing :
Clustering
Clustering is the tendency of elements to become
unevenly distributed in the hash table, with many
elements clustering around a single hash location.
:
:
Example: Chaining
Example: Chaining
1 Aho
0
2 Standish Knuth
1
2 1 Sedgewick
3 0
4 0
3 Kruse Horowitz Langsam
5
6 0
Hashtable with Chaining
List hashTable[MAXTABLE];
1
0
2
1
2 1
:
Insert with Chaining
1 Aho
0
2 Standish Knuth
1
2 1 Sedgewick
:
Search with Chaining
module SearchChaining(item)
{
posHash = hash(key of item)
Node* found;
found = searchList (hashTable[posHash], item);
return found;
}
1 Aho
0
2 Standish Knuth
1
2 1 Sedgewick
:
Delete with Chaining
module DeleteChaining(item)
{
posHash = hash(key of item)
2 1 Sedgewick
:
Disadvantages of Chaining