0% found this document useful (0 votes)
145 views45 pages

Hashing: Dr. Yasir Faheem and M. Salman Niazi

The document discusses hashing techniques including hash functions, insert and retrieve operations, collisions and how they are resolved using linear probing or chaining. It provides examples of inserting elements using linear probing to handle collisions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
145 views45 pages

Hashing: Dr. Yasir Faheem and M. Salman Niazi

The document discusses hashing techniques including hash functions, insert and retrieve operations, collisions and how they are resolved using linear probing or chaining. It provides examples of inserting elements using linear probing to handle collisions.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 45

HASHING

Dr. Yasir Faheem and M. Salman Niazi


Overview
 Why Hashing Technique ?
 What is Hashing Function ?
 InsertElement Operation
 RetrieveElement Operation
 Collisions
 Collision Resolution
 Linear Probing
 Problems with Linear Probing : Clustering
 Chaining
 Types of Hash Function
Complexity
 Inserting
 Balanced Trees O(log2(n))
 Unbalanced Trees O(n)
 Searching
 Balanced Trees O(log2 (n))
 Unbalanced Trees O(n)
Why Hashing Technique
 So far , we have succeeded in bringing down O(N)
Search to O(log2N) by keeping the list ordered
sequentially with respect to the key field.
 Is it possible to design a search of O(1)-that is , one
that takes constant time to find any element in the
list ?
Hashing

 Each item has a unique key.


 Use a large array called a Hash Table.
 Use a Hash Function.
 Search is O(1).
Applications of Hashing
 Compilers use hash tables to keep track of declared
variables
 Game playing programs use hash tables to store seen
positions, thereby saving computation time if the position
is encountered again
 Hash functions can be used to quickly check for
inequality — if two elements hash to different values
they must be different
 Storing sparse data
When are other representations more
suitable than hashing?
 Hash tables are very good if there is a need for many
searches in a reasonably stable table
 Hash tables are not so good if there are many
insertions and deletions, or if table traversals are
needed — in this case, AVL trees are better
 Also, hashing is very slow for any operations which
require the entries to be sorted
 e.g. Find the minimum key
Hashing Function
 The Hashing function tries to establish some
relationship between key of an element and its
address.
 In practice , this relationship is not easy to
establish.
 The elements are ordered with respect to some
function of the key value.
Hashing

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. }

 Here index is basically type int , as given below :


typedef int Index;
RetrieveElement Operation
 We use Hash function to tell us , where to look for a
particular element.
 Our , Hash function assumes that element is there.
1. void RetrieveElement(ElementType *List , Index
KeyVal,ElementType *Element)
2. {
3. Index Location;
4. Location=Hash(KeyVal);
5. *Element=List[Location];
6. }
RetrieveElement Operation
hash table
Found
0 50700,Rizwan
1
Key= Key Mod 100
00 2

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].

[0] [1] [2] [3] [4] [5] [ 700]


Number 281942902 Number 233667136 Number 580625685 Number 506643548 Number 155778322
 There are several popular collision handling
methods :-
 Linear Probing
 Chaining
Linear Probing
 A simple approach to resolve collisions is to store
the colliding elements in the available space.
 This technique is known as linear probing.
 The linear probing is an example of a general
method of resolving hash clashes called rehashing
or open addressing.
Example: Insert with Linear Probing
Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth
hash table
0 Aho

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

Langsam Hash 2 Langsam


5
Function
3

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.

 One problem with linear probing is that it results in


clustering.
Chaining

 Uses a Linked List at each position in the Hash


Table.
hash table
0

:
:
Example: Chaining
Example: Chaining

Aho, Kruse, Standish, Horowitz, Langsam, Sedgwick, Knuth


0, 5, 1, 5, 5, 2, 1

1 Aho
0
2 Standish Knuth
1

2 1 Sedgewick

3 0

4 0
3 Kruse Horowitz Langsam
5
6 0
Hashtable with Chaining

 At each position in the array you have a list:

List hashTable[MAXTABLE];

1
0
2
1

2 1

:
Insert with Chaining

 Apply hash function to get a position in the


array.
 Insert key into the Linked List at this position in
the array.
Insert with Chaining
module InsertChaining(item)
{
posHash = hash(key of item)

InsertList (hashTable[posHash], item);

1 Aho
0
2 Standish Knuth
1

2 1 Sedgewick

:
Search with Chaining

 Apply hash function to get a position in the


array.
 Search the Linked List at this position in the
array.
Search with Chaining
/* module returns NULL if not found, or the address of the
* node if found */

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

 Apply hash function to get a position in the


array.
 Delete the node in the Linked List at this
position in the array.
Delete with Chaining
/* module uses the Linked list delete function to delete an item

inside that list, it does nothing if that item isn’t there. */

module DeleteChaining(item)
{
posHash = hash(key of item)

deleteList (hashTable[posHash], item);


}
1 Aho
0
2 Standish Knuth
1

2 1 Sedgewick

:
Disadvantages of Chaining

 Uses more space.


 More complex to implement.
 A linked list at every element in the array
Advantages of Chaining

 Insertions and Deletions are easy and quick.


 Allows more records to be stored.
 Naturally resizable, allows a varying number of
records to be stored.
The End

You might also like