Unit 6
Unit 6
6
HASHING
Topic Name
Hashing, Hash Table
By,
Prof. Dipika Birari
Content
Topics to cover:
Hashing Introduction
Hash Table
Bucket
Bucket Hashing
Hashing: Introduction
Hashing:
Hashing is a search strategy based on the value of the key on which the
search is based.
Here,
Hash key: Value gives the index value where the actual data is likely to
store in the data structure.
Hash Table
Hash Table:
Hash table used to store data. All the data values are inserted into the
hash table based on the hash key value.
Hash key value is used to map the data with index in the hash table.
And the hash key is generated for every data using a hash function.
Hash Table
“ Hash table is an array which maps a key (data) into the data structure
with the help of hash function such that insertion, deletion and search
operations can be performed with constant time complexity (i.e. O(1)).”
Hash Table
Applications of Hash Table
Applications of Hash Table:
Database Systems
Symbol Tables
Data Dictionaries
The M slots of the hash table are divided into B buckets, with each bucket
consisting of M/B slots.
If desired key value is not found & the bucket is still has free slot
Then the search is complete.
It has many of the same applications as other hash functions, but with
the advantage that no collision resolution has to be implemented.
Load Density
The loading density or loading factor of a hash table is
a = n/(sb)
Where,
s is the number of slots
The entire linked list at that index is first searched for the presence of
the K already.
If found, it’s value is updated and
Otherwise, the K-V pair is stored as a new node in the list.
Rehashing
Complexity and Load Factor:
For the first step, time taken depends on the K and the hash
function.
example, if the key is a string “abcdef”, then it’s hash function may
depend on the length of the string.
The worst case = all the n entries are at the same index.
But hash functions uniformly distribute the keys in the array so this
almost never happens.
Rehashing
Complexity and Load Factor:
So, on an average,
if n = entries and
b = size of the array
n/b = entries on each index.
This value n/b is called the load factor
This Load Factor needs to be kept low, so that number of entries at one
index is less and so is the complexity almost constant,
i.e., O(1)
Rehashing
“Rehashing means hashing again.”
Basically,
increases
when the load factor > its pre-defined value of load factor
(default value =0.75),
Then complexity increases
If it’s greater than its pre-defined value (or default value = 0.75)
Then Rehash
For Rehash,
make a new array of double the previous size and
make it the new bucket array.
Then traverse to each element in the old bucket Array and call the
insert() for each
So as to insert it into the new larger bucket array.
Issues in Hashing
Hash tables are extremely useful data structures as lookups take
expected O(1) time on average,
i.e. the amount of work that a hash table does to perform a lookup is
at most some constant.
Hash Collision
A situation when the resultant hashes for two or more data elements
in the data set U, maps to the same location in the hash table, is called
a hash collision.
Collision Resolution Strategies
Hash Collision
In such situation two or more data elements would qualify to be stored
or mapped to the same location in the hash table.
Chaining: Stores colliding keys in Linked List at the same table index.
It is technique in which the data is not directly stored at the hash key
index(k) of hash table.
Rather the data at the key index (k) in the hash table is pointer to the
head of the data structure where the data is actually stored.
0
1
2
3
4
5
6
Initial Empty
Table
Open Hashing/Separate Chaining
Example:
Consider a simple ash function a “ Key mod 7” and sequence of keys as
50, 700, 76, 85, 92, 73, 101.
0 0
1 1 To insert 50,
2 2 Index = Key % 7
3 3 = 50 % 7
Index = 1
4 4
5 5
6 6
Initial Empty
Table
Open Hashing/Separate Chaining
Example:
Consider a simple ash function a “ Key mod 7” and sequence of keys as
50, 700, 76, 85, 92, 73, 101.
0 0
1 1 50 To insert 50,
2 2 Index = Key % 7
3 3 = 50 % 7
Index = 1
4 4
5 5
6 6
To insert 700,
0 0
1 1 50 Index = Key % 7
2 2 = 700 % 7
Index = 0
3 3
4 4 To insert 76,
5 5
Index = Key % 7
6 6 = 76 % 7
Index = 6
Initial Empty
Table
Open Hashing/Separate Chaining
Example:
Consider a simple ash function a “ Key mod 7” and sequence of keys as
50, 700, 76, 85, 92, 73, 101.
To insert 700,
0 0 700
1 1 50 Index = Key % 7
2 2 = 700 % 7
Index = 0
3 3
4 4 To insert 76,
5 5
Index = Key % 7
6 6 76 = 76 % 7
Index = 6
Initial Empty Insert
Table 700 & 76
Open Hashing/Separate Chaining
Example:
Consider a simple ash function a “ Key mod 7” and sequence of keys as
50, 700, 76, 85, 92, 73, 101.
To insert 85,
0 0 700
1 1 50 Index = Key % 7
2 2 = 85 % 7
Index = 1
3 3
4 4
5 5 Collision Occurs
6 6 76 Add to chain
Initial Empty
Table
Open Hashing/Separate Chaining
Example:
Consider a simple ash function a “ Key mod 7” and sequence of keys as
50, 700, 76, 85, 92, 73, 101.
To insert 85,
0 0 700
1 1 50 85 Index = Key % 7
2 2 = 85 % 7
Index = 1
3 3
4 4
5 5 Collision Occurs
6 6 76 Add to chain
Initial Empty
Table
Open Hashing/Separate Chaining
Example:
Consider a simple ash function a “ Key mod 7” and sequence of keys as
50, 700, 76, 85, 92, 73, 101.
To insert 92,
0 0 700
1 1 50 85 Index = Key % 7
2 2 = 92 % 7
Index = 1
3 3
4 4
5 5 Collision Occurs
6 6 76 Add to chain
To insert 92,
0 0 700
1 1 50 85 92 Index = Key % 7
2 2 = 92 % 7
Index = 1
3 3
4 4
5 5 Collision Occurs
Add to chain
6 6 76
To insert 73,
0 0 700
1 1 50 85 92 Index = Key % 7
2 2 = 73 % 7
Index = 3
3 3
4 4
5 5
6 6 76
Initial Empty
Table
Open Hashing/Separate Chaining
Example:
Consider a simple ash function a “ Key mod 7” and sequence of keys as
50, 700, 76, 85, 92, 73, 101.
To insert 73,
0 0 700
1 1 50 85 92 Index = Key % 7
2 2 = 73 % 7
Index = 3
3 3 73
4 4
5 5
6 6 76
To insert 101,
0 0 700
1 1 50 85 92 Index = Key % 7
2 2 = 101 % 7
Index = 3
3 3 73
4 4
5 5 Collision Occurs
Add to chain
6 6 76
Initial Empty
Table
Open Hashing/Separate Chaining
Example:
Consider a simple ash function a “ Key mod 7” and sequence of keys as
50, 700, 76, 85, 92, 73, 101.
To insert 101,
0 0 700
1 1 50 85 92 Index = Key % 7
2 2 = 101 % 7
Index = 3
3 3 73 101
4 4
5 5 Collision Occurs
Add to chain
6 6 76
Disadvantages:
Wastage of space (Some part of hash table are never used.)
Linked lists could get long. Longer linked lists could negatively
impact performance
If the chain becomes long; then search time become 0(n) in worst
case.
Closed Hashing/Open Addressing
Open addressing / probing is carried out for insertion into fixed size
hash tables (hash tables with 1 or more buckets).
If the index given by the hash function is occupied, then there is need
to find another bucket for the element to be stored. Then increment
the table position by some number.
Linear Probing
Quadratic Probing
Double Hashing
Closed Hashing/Open Addressing
Linear Probing:
It uses a technique “Probing”.
The next slot for the collided key is found in this method by
using a technique called "Probing".
Suppose that a key hashes into a position that has been already
occupied.
The simplest strategy is to look for the next available position to place
the item.
Closed Hashing/Open Addressing
Linear Probing:
In linear probing, the rehashing process is linear.
Insert 89
Closed Hashing/Open Addressing
Insert 18
Closed Hashing/Open Addressing
Insert 49
Closed Hashing/Open Addressing
Insert 58
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Insert 9
Closed Hashing/Open Addressing
Linear Probing: Example
{89, 18, 49, 58, 9}
0 49
To find element 79, 1 58
2 9
Compute hash code of 79 =9 3
Search at A[9] 4
Search at A[(9+1 %10]=A[0] 5
Search at A[(9+2 %10]=A[1] 6
Search at A[(9+3 %10]=A[2] 7
Search at A[(9+4 %10]=A[3] 8 18
And so on 9 89
Insert 9
Closed Hashing/Open Addressing
Table Size=10
Slot per Bucket=1
Total no. of Comparisons=???
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1
2
3
4
5
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1
2
3
4
5 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1
2
3 3
4
5 25 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21
2
3 3 3
4
5 25 25 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21
2
3 3 3 3
4
5 25 25 25 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21
2
3 3 3 3
4 13
5 25 25 25 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21
2
3 3 3 3 3
4 13 13
5 25 25 25 25 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21
2 1
3 3 3 3 3
4 13 13
5 25 25 25 25 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21
2 1 1
3 3 3 3 3 3
4 13 13 13
5 25 25 25 25 25 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21
2 1 2
3 3 3 3 3 3
4 13 13 13
5 25 25 25 25 25 25
6
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21
2 1 2
3 3 3 3 3 3
4 13 13 13
5 25 25 25 25 25 25
6 1
7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21 21
2 1 2 2
3 3 3 3 3 3 3
4 13 13 13 13
5 25 25 25 25 25 25 25
6 1 1
7 7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21 21 21
2 1 2 2 2
3 3 3 3 3 3 3 3
4 13 13 13 13 13
5 25 25 25 25 25 25 25 25
6 1 1 1
7 7 7
8
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21 21 21
2 1 2 2 2
3 3 3 3 3 3 3 3
4 13 13 13 13 13
5 25 25 25 25 25 25 25 25
6 1 1 1
7 7 7
8 12
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21 21 21 21
2 1 2 2 2 2
3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 13
5 25 25 25 25 25 25 25 25 25
6 1 1 1 1
7 7 7 7
8 12 12
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21 21 21 21
2 1 2 2 2 2
3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4
5 25 25 25 25 25 25 25 25 25
6 1 1 1 1
7 7 7 7
8 12 12
9
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21 21 21 21
2 1 2 2 2 2
3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4
5 25 25 25 25 25 25 25 25 25
6 1 1 1 1
7 7 7 7
8 12 12
9 13
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21 21 21 21 21
2 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4 4
5 25 25 25 25 25 25 25 25 25 25
6 1 1 1 1 1
7 7 7 7 7
8 12 12 12
9 13 13
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0
1 21 21 21 21 21 21 21 21
2 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4 4
5 25 25 25 25 25 25 25 25 25 25
6 1 1 1 1 1
7 7 7 7 7
8 12 12 8
9 13 13
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0 12
1 21 21 21 21 21 21 21 21
2 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4 4
5 25 25 25 25 25 25 25 25 25 25
6 1 1 1 1 1
7 7 7 7 7
8 12 12 8
9 13 13
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0 12
1 21 21 21 21 21 21 21 21
2 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4 4
5 25 25 25 25 25 25 25 25 25 25
6 1 1 1 1 1
7 7 7 7 7
8 12 12 8
9 13 13
Compr 0 0 0
Chaining with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0 12
1 21 21 21 21 21 21 21 21
2 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4 4
5 25 25 25 25 25 25 25 25 25 25
6 1 1 1 1 1
7 7 7 7 7
8 12 12 8
9 13 13
Compr 0 0 0 1 1
Linear Probing with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0 12
1 21 21 21 21 21 21 21 21
2 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4 4
5 25 25 25 25 25 25 25 25 25 25
6 1 1 1 1 1
7 7 7 7 7
8 12 12 8
9 13 13
Compr 0 0 0 1 1 4
Linear Probing with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0 12
1 21 21 21 21 21 21 21 21
2 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4 4
5 25 25 25 25 25 25 25 25 25 25
6 1 1 1 1 1
7 7 7 7 7
8 12 12 8
9 13 13
Compr 0 0 0 1 1 4 0 6
Linear Probing with Replacement: Example
Data: {25, 3, 21, 13, 1, 2, 7, 12, 4, 8}
Bucket 25 3 21 13 1 2 7 12 4 8
0 12
1 21 21 21 21 21 21 21 21
2 1 2 2 2 2 2
3 3 3 3 3 3 3 3 3 3
4 13 13 13 13 13 4 4
5 25 25 25 25 25 25 25 25 25 25
6 1 1 1 1 1
7 7 7 7 7
8 12 12 8
9 13 13
Compr 0 0 0 1 1 4 0 6 5 4
Collision Resolution Strategies
Chaining: Stores colliding keys in Linked List at the same table index.
If the index given by the hash function is occupied, then there is need
to find another bucket for the element to be stored. Then increment
the table position by some number.
Linear Probing
Quadratic Probing
Double Hashing
Closed Hashing/Open Addressing
Linear Probing:
It uses a technique “Probing”.
The next slot for the collided key is found in this method by
using a technique called "Probing".
Suppose that a key hashes into a position that has been already
occupied.
The simplest strategy is to look for the next available position to place
the item.
Closed Hashing/Open Addressing
Linear Probing:
In linear probing, the rehashing process is linear.
Table Size=10
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1
2
3
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1
2
3
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1 11
2
3
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1 11 11
2
3
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1 11 11
2
3 33
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1 11 11 11
2
3 33 33
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20
1 11 11 11
2
3 33 33
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20
1 11 11 11 11
2
3 33 33 33
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20
1 11 11 11 11
2
3 33 33 33
4
5
6
7
8 88
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20
1 11 11 11 11 11
2
3 33 33 33 33
4
5
6
7
8 88 88
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20
1 11 11 11 11 11
2
3 33 33 33 33
4
5
6
7
8 88 88
9 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20
1 11 11 11 11 11 11
2
3 33 33 33 33 33
4
5
6
7
8 88 88 88
9 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20
1 11 11 11 11 11 11
2
3 33 33 33 33 33
4
5
6
7
8 88 88 88
9 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20
1 11 11 11 11 11 11
2 98
3 33 33 33 33 33
4
5
6
7
8 88 88 88
9 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20
1 11 11 11 11 11 11 11
2 98 98
3 33 33 33 33 33 33
4
5
6
7
8 88 88 88 88
9 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20
1 11 11 11 11 11 11 11
2 98 98
3 33 33 33 33 33 33
4 44
5
6
7
8 88 88 88 88
9 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11
2 98 98 98
3 33 33 33 33 33 33 33
4 44 44
5
6
7
8 88 88 88 88 88
9 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11
2 98 98 98
3 33 33 33 33 33 33 33
4 44 44
5
6
7
8 88 88 88 88 88
9 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11
2 98 98 98
3 33 33 33 33 33 33 33
4 44 44
5 68
6
7
8 88 88 88 88 88
9 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11
2 98 98 98 98
3 33 33 33 33 33 33 33 33
4 44 44 44
5 68 68
6
7
8 88 88 88 88 88 88
9 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11
2 98 98 98 98
3 33 33 33 33 33 33 33 33
4 44 44 44
5 68 68
6 66
7
8 88 88 88 88 88 88
9 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11 11
2 98 98 98 98 98
3 33 33 33 33 33 33 33 33 33
4 44 44 44 44
5 68 68 68
6 66 66
7
8 88 88 88 88 88 88 88
9 79 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11 11
2 98 98 98 98 98
3 33 33 33 33 33 33 33 33 33
4 44 44 44 44
5 68 68 68
6 66 66
7
8 88 88 88 88 88 88 88
9 79 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11 11
2 98 98 98 98 98
3 33 33 33 33 33 33 33 33 33
4 44 44 44 44
5 68 68 68
6 66 66
7 22
8 88 88 88 88 88 88 88
9 79 79 79 79 79 79
Chaining with Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1 11
2
3
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1 11 11
2
3
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1 11 11
2
3 33
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0
1 11 11 11
2
3 33 33
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20
1 11 11 11
2
3 33 33
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20
1 11 11 11 11
2
3 33 33 33
4
5
6
7
8
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20
1 11 11 11 11
2
3 33 33 33
4
5
6
7
8 88
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20
1 11 11 11 11 11
2
3 33 33 33 33
4
5
6
7
8 88 88
9
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20
1 11 11 11 11 11
2
3 33 33 33 33
4
5
6
7
8 88 88
9 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20
1 11 11 11 11 11 11
2
3 33 33 33 33 33
4
5
6
7
8 88 88 88
9 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20
1 11 11 11 11 11 11
2
3 33 33 33 33 33
4
5
6
7
8 88 88 88
9 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20
1 11 11 11 11 11 11
2 98
3 33 33 33 33 33
4
5
6
7
8 88 88 88
9 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20
1 11 11 11 11 11 11 11
2 98 98
3 33 33 33 33 33 33
4
5
6
7
8 88 88 88 88
9 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20
1 11 11 11 11 11 11 11
2 98 98
3 33 33 33 33 33 33
4 44
5
6
7
8 88 88 88 88
9 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11
2 98 98 98
3 33 33 33 33 33 33 33
4 44 44
5
6
7
8 88 88 88 88 88
9 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11
2 98 98 98
3 33 33 33 33 33 33 33
4 44 44
5
6
7
8 88 88 88 88 88
9 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11
2 98 98 98
3 33 33 33 33 33 33 33
4 44 44
5 68
6
7
8 88 88 88 88 88
9 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11
2 98 98 98 98
3 33 33 33 33 33 33 33 33
4 44 44 44
5 68 68
6
7
8 88 88 88 88 88 88
9 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11
2 98 98 98 98
3 33 33 33 33 33 33 33 33
4 44 44 44
5 68 68
6 66
7
8 88 88 88 88 88 88
9 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11 11
2 98 98 98 98 98
3 33 33 33 33 33 33 33 33 33
4 44 44 44 44
5 68 68 68
6 66 66
7
8 88 88 88 88 88 88 88
9 79 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11 11
2 98 98 98 98 98
3 33 33 33 33 33 33 33 33 33
4 44 44 44 44
5 68 68 68
6 66 66
7
8 88 88 88 88 88 88 88
9 79 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11 11
2 98 98 98 98 22
3 33 33 33 33 33 33 33 33 33
4 44 44 44 44
5 68 68 68
6 66 66
7
8 88 88 88 88 88 88 88
9 79 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11 11
2 98 98 98 98 22
3 33 33 33 33 33 33 33 33 33
4 44 44 44 44
5 68 68 68
6 66 66
7 98
8 88 88 88 88 88 88 88
9 79 79 79 79 79 79
Chaining without Replacement: Example
Data: {11, 33, 20, 88, 79, 98, 44, 68, 66, 22}
Bucket 11 33 20 88 79 98 44 68 66 22
0 20 20 20 20 20 20 20 20
1 11 11 11 11 11 11 11 11 11 11
2 98 98 98 98 22
3 33 33 33 33 33 33 33 33 33
4 44 44 44 44
5 68 68 68
6 66 66
7 98
8 88 88 88 88 88 88 88
9 79 79 79 79 79 79
Closed Hashing/Open Addressing
Quadratic Probing:
Although linear probing is a simple process where it is easy to
compute the next available location, linear probing also leads to
some clustering when keys are computed to closer values.
Quadratic Probing:
Therefore we define a new process of Quadratic probing that
provides a better distribution of keys when collisions occur.
where K= 1, 2, 3, ……
Quadratic Probing:
where K= 1, 2, 3, ……
If the hash value = k
Then the next location = k+1
Quadratic Probing:
Closed Hashing/Open Addressing
Double Hashing:
It is best open addressing technique to overcome clustering chances.
Here, we increment the probing length based on another hash
function.
Primary hash function=h1
Secondary hash function=h2
Double Hashing:
f(key) = h1(key) + k * h2(key)
Where h1 != h2
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
0
1
2
3
4
5
6
7
8
9
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3 0
123 % 10 = 3 1
2
3
4
5
6
7
8
9
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location ??? 0
123 % 10 = 3 1
2
3 123
4
5
6
7
8
9
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3 0
Insert 124 at location ??? 1
124 % 10 = 4 2
3 123
4
5
6
7
8
9
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3 0
Insert 124 at location 4 1
124 % 10 = 4 2
3 123
4 124
5
6
7
8
9
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3 0
Insert 124 at location 4 1
Insert 333 at location ??? 2
333 % 10 = 3 3 123
Location 3 is already occupied 4 124
Next Location = 3 + 1. digits(333) =3+3 = 6 5
Insert 333 at location 6
6
7
8
9
f(key) = h1(key) + k * h2(key)
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3 0
Insert 124 at location 4 1
Insert 333 at location ??? 2
333 % 10 = 3 3 123
Location 3 is already occupied 4 124
Next Location = 3 + 1. digits(333) =3+3 = 6 5
Insert 333 at location 6
6 333
7
8
9
f(key) = h1(key) + k * h2(key)
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3 0
Insert 124 at location 4 1
Insert 333 at location 6 2
Insert 4679 at location ??? 3 123
4679 % 10 = 9 4 124
5
6 333
7
8
9
f(key) = h1(key) + k * h2(key)
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3 0
Insert 124 at location 4 1
Insert 333 at location 6 2
Insert 4679 at location ??? 3 123
4679 % 10 = 9 4 124
5
6 333
7
8
9 4679
f(key) = h1(key) + k * h2(key)
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3
Insert 124 at location 4 0
Insert 333 at location 6 1
Insert 4679 at location 9
2
Insert 983 at location ???
3 123
983 % 10 = 3
Location 3 is already occupied 4 124
Next Location = 3 + 1*digits(983) =3+3 = 6 is already occupied 5
Next Location = 3 + 2* digits(983) =3+2*3 = 9 is already occupied
Next Location = 3 + 3 *digits(983) =3+3*3 = 12 6 333
12 % 10 =2 7
Insert 983 at location 2
8
9 4679
f(key) = h1(key) + k * h2(key)
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3
Insert 124 at location 4 0
Insert 333 at location 6 1
Insert 4679 at location 9
2 983
Insert 983 at location ???
3 123
983 % 10 = 3
Location 3 is already occupied 4 124
Next Location = 3 + 1*digits(983) =3+3 = 6 is already occupied 5
Next Location = 3 + 2* digits(983) =3+2*3 = 9 is already occupied
Next Location = 3 + 3 *digits(983) =3+3*3 = 12 6 333
12 % 10 =2 7
Insert 983 at location 2
8
9 4679
f(key) = h1(key) + k * h2(key)
Closed Hashing/Open Addressing
Double Hashing:
{ 123, 124, 333, 4679, 983} and Table_ Size=10
Insert 123 at location 3 0
Insert 124 at location 4 1
Insert 333 at location 6 2 983
Insert 4679 at location 9 3 123
Insert 983 at location 2 4 124
5
6 333
7
8
9 4679
Closed Hashing/Open Addressing
Double Hashing:
Advantages
Clustering chances very less.
Disadvantages
A new hashing function is overhead and take more time tan those
two.