Skip to main content
CSCE 3110
Data Structures & Algorithm
Analysis
Rada Mihalcea
http://www.cs.unt.edu/~rada/CSCE3110
Hashing
Reading: Chap.5, Weiss
Dictionaries stores elements so that they can be
located quickly using keys.
For eg
A Dictionary may hold bank accounts.
In which key will be account number.
And each account may stores many additional
information.
Dictionaries
How to Implement a Dictionary?
Different data structure to realize a key
Array , Linked list
Binary tree
Hash table
Red/Black tree
AVL Tree
B-Tree
Why Hashing??
The sequential search algorithm takes time
proportional to the data size, i.e, O(n).
Binary search improves on liner search reducing
the search time to O(log n).
With a BST, an O(log n) search efficiency can
be obtained; but the worst-case complexity is
O(n).
To guarantee the O(log n) search time, BST
height balancing is required ( i.e., AVL trees).
Why Hashing?? (Cntd.)
Suppose that we want to store 10,000 students records
(each with a 5-digit ID) in a given container.
· A linked list implementation would take O(n) time.
· A height balanced tree would give O(log n)
access time.
· Using an array of size 100,000 would give O(1)
access time but will lead to a lot of space wastage.
Why Hashing?? (Cntd.)
Is there some way that we could get O(1) access
without wasting a lot of space?
The answer is hashing.
Hashing
Another important and widely useful
technique for implementing dictionaries
Constant time per operation (on the average)
Like an array, come up with a function to map
the large range into one which we can
manage.
Basic Idea
Use hash function to map keys into positions
in a hash table
Ideally
If Student A has ID(Key) k and h is hash
function, then A’s Details is stored in position
h(k) of table
To search for A, compute h(k) to locate
position. If no element, dictionary does not
contain A.
Example
Let keys be ID of 100 students
And ID in form of like 345610.
Now, we decided to take A[100]
And, Hash function is , say , LAST TWO DIGIT
So, 103062 will go to location 62
And same if some one have 113062
Then again goes to the location 62
THIS EVENT IS CALLED COLLISION
Collision Resolution
Chaining
Linear Probe
Double hashing
Chaining
Hash Functions
A Good Hash function is one which distribute keys
evenly among the slots.
And It is said that Hash Function is more art than a
science. Becoz it need to analyze the data.
Key
Hash
Function Slot
Hash Function(cntd.)
Need of choose a good Hash function
Quick Compute.
Distributes keys in uniform manner throughout the
table.
How to deal with Hashing non integer Key???
1.Find some way of turning keys into integer.
eg if key is in character then convert it into integer using
ASCII
2.Then use standard Hash Function on the integer.
Hash Function (contd.)
Hash code map
Keys Integer
Compression map
Integer A[0….m-1]
The Mapping of keys to indices of a hash table is called a
hash function.
The Hash Function is ussually the composition of two
maps:
Collision Resolution (contd.)
Now, there is two more techniques to deal
with collision
Linear Probing
Double Hashing
Linear probe
Linearprobeinsert(k)
If(table is full)
{error}
probe =h(k)
while(table[probe] is occupied)
{probe = (probe + 1) % m //m is no. of slots
}
Table[probe]=k
Linear Probe(contd.)
If the current location is used, Try the next
table Location.
Used less memory than chaining as one does
not have to store all those link(i.e. address of
others).
Slower than chaining as one might have to
walk along the table for a long time.
Linear Probe (contd.)
Linear probe (contd.)
Deletion in Linear probe
Double Hashing
h1(k) - Position in the table where we first
check for the key
h2(k) – Determine offset when h1(k) is
already occupied
In Linear probing offset is always 1.
Double Hashing (contd.)
Doublehashing insert(k)
If (table is full)
{error
}
Probe=h1(k); offset=h2(k);
While (table[probe] is occupied)
{probe=(probe + offset)%m
}
table[probe]=k;
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Double Hashing(contd.)
Thank
U 