Hash Tables
Hash Tables
A N I M P L E M E N TAT I O N O F H A S H I N G A L G O R I T H M
Division Remainder
Method
Sum of ASCII
String/Char Key
Values
In this concept we will deeply explain this
data structure, the algorithm behind it,
different collision techniques, and some No-Collision
WORKING OF
use case examples including different HASH TABLE HASHING
Separate Chaining
Linear Probing
(Open Hashing)
scenarios thus explaining hashing ALGORITHM
technique for each of them. Collision
Open Addressing
Quadratic Probing
(Closed Hashing)
Insert
Double Hashing
Operations Find
Delete
What is
Hash Table?
Hash Tables:
• They are widely used in many kinds of computer
software, particularly for associative arrays, database
indexing, caches and sets.
• Compilers use Hash Tables to keep track of declared
variables.
• Used for checking circularity of array.
• In most of the programming languages, there are
built-in data types or data structures in the standard
library that are based on hash tables e.g., dictionary in
Python, or HashMap in Java
Hash Function/
Hashing Algorithm:
• Hashing algorithms are functions that generate a fixed What is Collision?
A hash collision occurs when a hash algorithm produces
result (the hash value) from a given input. the same hash value for two different input values
• Hash function is used to map data of arbitrary size to
fixed-size values.
• The values returned by a hash function are called hash
values, hash codes, digests, or simply hashes.
• The values are used to index a fixed-size table called a
hash table.
Hash Function/
Hashing Algorithm:
• A Hash function can be categorized as:
• Good Hash Function: (Minimum or No Collisions) Figure 1. Example of a Good Hash Function
It returns same
hash value for
the same
character either
upper or lower
case
Collisions This is an example of a bad hash function
Example: resulting in collisions.
Let's give it an input of a student whose
number we want to store against it.
(Student Name -> KEY, Marks -> Value)
String key Hash Function:
Let name be MAHAD it produces sum
475 whose modulus can be taken to
produce and index.
Let another name be AHMAD it will also
produce its sum 475 and value of both
keys will be placed at same index which
will cause collision.
Implementing a simple Hash
Table:
(Considering no collisions)
Hash Function
Running Time
BEST: O(1)
insert WORST
O(n)
:
BEST: O(1)
find WORST
O(n)
:
BEST: O(1)
delete WORST (if insertions
O(n) are always at
: the end of the
linked list)
Performance of
Separate
Chaining:
n = Number of keys to be inserted in hash table
c = Number of slots in hash table
Example:
length = 10
keys/values are
43, 135, 72, 23, 99, 19, 82
Advantage: No Extra Space
Disadvantage: Search Time O(n), Deletion Difficult,
Clustering (primary)
Open addressing is a
(Open Addressing collision resolution strategy
where collisions are resolved
Methods) by storing
(2) Quadratic the colliding key in a
different location when the
Probing: natural choice is full.
Example:
length = 10
keys/values are
42, 16, 91, 33, 18, 27, 36, 62
Advantage: No Extra Space, Clustering Resolved
Disadvantage: Search Time O(n), No Guarantee of
finding free slot.
Example
Question:
(Open Addressing
Methods)
(3) Double
Hashing:
• Double hashing is a collision resolving
technique in Open Addressed Hash tables.
Simple Hash Function:
• Double hashing uses the idea of applying a
H1(k) = k mod length
second hash function to key when a
Default Hash function when there is
collision occurs
no collision.
Double Hashing Function:
H’(k,i) = [ H1(k) + ( i x H2(k) ] % length
This is only used when collision
occurs
Where H2(k) = prime – ( k % prime)
prime can be any prime number integer or any
other number relatively prime with length &
prime<length
(Open Addressing Advantage: No Extra Space,
No Clustering
Methods) Disadvantage: Search Time O(n),
Double Hashing
Example:
(Open Addressing
Methods)
Double Hashing
Example:
Thank You!
ANY QUESTIONS ?