SlideShare a Scribd company logo
1 of 134
WHAT IS
HASH
TABLE
Reporter: Joshua Mangrobang
● What is a HASH TABLE?
● What is a HASH FUNCTION?
● What is a HASH COLLISION?
● Implementation of a Hash Table
● Discussion on collision resolution methods in particular SEPARATE CHAINING and OPEN
ADDRESSING
● Separate Chaining Implementation
Open Addressing Implementation
● Linear Probing
● Quadratic Probing
● Double Hashing
Outline
Hash Tables are a data structure
that allow you to create a list of
paired values. You can then
retrieve a certain value by using
the key for that value, which you
put into the table beforehand.
What is a Hash
Table
Hash Tables are a data structure
that allow you to create a list of
paired values. You can then
retrieve a certain value by using
the key for that value, which you
put into the table beforehand.
What is a Hash
Table
A Hash Table transforms a key
into an integer index using a
hash function, and the index will
decide where to store the
key/value pair in memory.
What is Hash Function:
A hash function is used to transform a given key into a specific slot
index. it is used to map each and every possible key into a unique slot
index. If every key is mapped into a unique slot index, then the hash
function is known as a perfect hash function.
What is Hash Function:
A hash function is used to transform a given key into a specific slot
index. it is used to map each and every possible key into a unique slot
index. If every key is mapped into a unique slot index, then the hash
function is known as a perfect hash function.
A hash function receives the input keys and returns the index of an
element in an array called a hash table. The index is known as hash
index.
What is Hash Function:
A hash function is used to
transform a given key into a
specific slot index. it is used to
map each and every possible
key into a unique slot index. If
every key is mapped into a
unique slot index, then the
hash function is known as a
perfect hash function.
A hash function receives the
input keys and returns the
index of an element in an array
called a hash table. The index
is known as hash index.
What is Hash Function:
By hashing a password, the company protects user information. Even
if a hacker breaks into the system, they won’t have access to actual
passwords, just the hashes.
Whenever you log in, your email provider doesn’t store the plain text
password, all they need is the hash.
When you enter your password, it is run through the hash function.
The output is matched against the hash that is saved in the database.
If the hash values are the same, the password is correct.
What is Collision:
In hashing. A collision happens when the hash function generates
same hash (output) for different set of keys (inputs).
A collision occurs when more than one value to be hashed by a
particular hash function hash to the same slot in the table or data
structure (hash table) being generated by the hash function.
Collision Example:
1 7 18 30
Index Value
0
1
2
3
4
10
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
10
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
10 % 5 = 0
10
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
10 % 5 = 0
10
1
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
1 % 5 = 1
10
1
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
1 % 5 =1
10
1
7
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
7 % 5 =2
10
1
7
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
7 % 5 =2
10
1
7
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
18 % 5 = 3
10
1
7
18
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
18 % 5 = 3
10
1
7
18
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
30 % 5 = 0
10
1
7
18
30
Collision Example:
1
7
18
30
H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
30 % 5 = 0
10
1
7
18
30 Collision
Reporter: Joshua Mangrobang
COLLISION
CONTROL
The process of finding alternate index position in the Hash
Table.
Collision Control
Open Hashing
Close Hashing
COLLISION
CONTROL
The process of finding alternate index position in the Hash
Table.
Collision Control
Open Hashing
(Separate Chaining)
Close Hashing
(Open Adrressing)
COLLISION
CONTROL
The process of finding alternate index position in the Hash
Table.
Collision Control
Open Hashing
Separate Chaining
Close Hashing
(Open Adrressing)
• Linear Probing
COLLISION
CONTROL
The process of finding alternate index position in the Hash
Table.
Collision Control
Open Hashing
Separate Chaining
Close Hashing
(Open Adrressing)
• Linear Probing
• Quadratic Probing
COLLISION
CONTROL
The process of finding alternate index position in the Hash
Table.
Collision Control
Open Hashing
Separate Chaining
Close Hashing
(Open Adrressing)
• Linear Probing
• Quadratic Probing
• Double Hashing Reporter: Joshua Mangrobang
COLLISION
CONTROL
Open Hashing
(Separate Chaining)
COLLISION
CONTROL
Open Hashing
Separate Chaining
Separate Chaining is a technique which is uses linked list data structures known as a chain.
This is one of the most popular and commonly used techniques in order to handle collision.
COLLISION
CONTROL
Open Hashing
Separate Chaining
Separate Chaining is a technique which is uses linked list data structures known as a chain.
This is one of the most popular and commonly used techniques in order to handle collision.
When multiple elements are hashed into the same slot index, then these elements are
inserted into a singly linked list which is known as chain.
COLLISION
CONTROL
Open Hashing
Separate Chaining
Separate Chaining is a technique which is uses linked list data structures known as a chain.
This is one of the most popular and commonly used techniques in order to handle collision.
When multiple elements are hashed into the same slot index, then these elements are
inserted into a singly linked list which is known as chain.
Stores data input values in linked lists identified by the index keys created by the hashing
functions. The table cells don't hold the input values as in open addressing but rather store
the index keys created by the hashing function.
1
7
18
30 H(k)= key % 5
Index Value
0
1
2
3
4
10
Key
Hash Function
Open Hashing (Separate
Chaining)
23
40
3
23
40
3
18
7
1
10 30
Hash Table
Open Hashing (Separate Chaining)
ADVANTAGES
DISADVANTAGES
Open Hashing (Separate Chaining)
ADVANTAGES
DISADVANTAGES
Simple to Implement
Open Hashing (Separate Chaining)
ADVANTAGES
DISADVANTAGES
Simple to Implement
Hash Table never fills up, we can always add more elements to the chain
Open Hashing (Separate Chaining)
ADVANTAGES
DISADVANTAGES
Simple to Implement
Hash Table never fills up, we can always add more elements to the chain
Mostly used when its known how many and how frequenly keys may be
inserted or deleted.
Open Hashing (Separate Chaining)
DISADVANTAGES
Open Hashing (Separate Chaining)
DISADVANTAGES
Wastage of space (Some Parts of the hash table are never used)
Open Hashing (Separate Chaining)
DISADVANTAGES
Wastage of space (Some Parts of the hash table are never used)
If the chain become long, then search time can become O(n)
Open Hashing (Separate Chaining)
DISADVANTAGES
Wastage of space (Some Parts of the hash table are never used)
If the chain become long, then search time can become O(n)
It use extra space for links
COLLISION
CONTROL
Closed Hashing
(Open Addressing)
In Open Addressing all elements are stored in the hash table itself, So at any point, the size
of the table must be greater than or equal to the total number of keys.
is a method used in hash tables for resolving collisions. In hash tables, collisions occur
when two different keys hash to the same index in the underlying array.
Is an alternative method to resolve hash collisions. Unlike separate chaining - there are no
linked lists. Each item is placed in the hash table by searching, (or probing as we'll call it),
for an open bucket to place it.
COLLISION
CONTROL
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
COLLISION
CONTROL
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing
COLLISION
CONTROL
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing
Quadratic Probing
COLLISION
CONTROL
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing
Quadratic Probing
Double Hashing
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing Quadratic Probing Double Hashing
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing
Quadratic Probing Double Hashing
In Linear Probing, the hash table is search sequentially that starts from the original
location of the hash. If in case the location that we get is already occupied, then we
check for the next location.
The function used for rehashing is as follows: rehash (key) = (n+1) % table-size
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & I be the probe count.
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & I be the probe count.
If slot h(k) % S is full, Then
we try h(k) + % S
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & I be the probe count.
If slot h(k) % S is full, Then
we try h(k) + % S
If h(k) % S is also full, Then
we try h(k) + 2 % S
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & I be the probe count.
If slot h(k) % S is full, Then
we try h(k) + % S
If h(k) % S is also full, Then
we try h(k) + 2 % S
If h(k) % S is also full, Then
we try h(k) + 3 % S
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
If slot h(k) % S is full, Then
we try h(k) + i % S
If h(k) % S is also full, Then
we try h(k) + 2 % S
If h(k) % S is also full, Then
we try h(k) + 3 % S
Generalized function
h(k) + i % S
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
If slot h(k) % S is full, Then
we try h(k) + i % S
If h(k) % S is also full, Then
we try h(k) + 2 % S
If h(k) % S is also full, Then
we try h(k) + 3 % S
Generalized function
h(k) + i % S
Example: Store these 7 elements in hash table
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
If slot h(k) % S is full, Then
we try h(k) + i % S
If h(k) % S is also full, Then
we try h(k) + 2 % S
If h(k) % S is also full, Then
we try h(k) + 3 % S
Generalized function
h(k) + i % S
Example: Store these 7 elements in hash table
5 7 25 84 32 61 3
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 5 % 5
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 5 % 5
H(k) = 5 % 5 = 0
5
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 5 % 5
H(k) = 5 % 5 = 0
5
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
5
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 7 % 5
5
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 7 % 5
H(k) = 7 % 5 = 2
5
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 7 % 5
H(k) = 7 % 5 = 2
5
7
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
5
7
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 25 % 5
5
7
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 25 % 5
H(k) = 25 % 5 =
0
5
7
25
Solution:
S = 7
i = n
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
5
7
25
Solution:
S = 7
i = n
h(k) = ((key %
5) + 1) % 7
h(k) = ((25 % 5)
+ 1) % 7
= (0 + 1) % 7
= 1 % 7 =1
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 84 % 5
H(k) = 84 % 5 =
4
5
7
25
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
5
7
25
84
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 32 % 5
H(k) = 32 % 5 =
2
5
7
25
84
32
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 32 % 5
H(k) = 32 % 5 =
2
5
7
25
Solution:
S = 7
i = n
h(k) = ((key %
5) + 1) % 7
h(k) = ((32 % 5)
+ 1) % 7
= (2 + 1) % 7
= 3 % 7 = 3
84
32
Reporter: Joshua Mangrobang
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 61 % 5
5
7
25
84
32
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 61 % 5
H(k) = 61 % 5 =
1
5
7
25
Solution:
S = 7
i = 1
84
32
61
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 61 % 5
H(k) = 61 % 5 =
1
5
7
25
Solution:
S = 7
i = 2
84
32
61
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 61 % 5
H(k) = 61 % 5 =
1
5
7
25
Solution:
S = 7
i = 3
84
32
61
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 61 % 5
H(k) = 61 % 5 =
1
5
7
25
Solution:
S = 7
i = 4
h(k) = ((key %
5) + 1) % 7
h(k) = ((61 % 5)
+ 4) % 7
= (1 + 4) % 7
= 5 % 7 = 5
84
32
61
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
5
7
25
Solution:
S = 7
i = 4
h(k) = ((key %
5) + 1) % 7
h(k) = ((61 % 5)
+ 4) % 7
= (1 + 4) % 7
= 5 % 7 = 5
84
32
61
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 3 % 5
5
7
25
Solution:
S = 7
i = n
84
32
61
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 3 % 5
H(k) = 3 % 5 = 3
5
7
25
Solution:
S = 7
i = 1
84
32
61
3
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 3 % 5
H(k) = 3 % 5 = 3
5
7
25
Solution:
S = 7
i = 2
84
32
61
3
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 3 % 5
H(k) = 3 % 5 = 3
5
7
25
Solution:
S = 7
i = 3
84
32
61
3
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 3 % 5
H(k) = 3 % 5 = 3
5
7
25
Solution:
S = 7
i = 3
h(k) = ((key %
5) + 1) % 7
h(k) = ((3 % 5)
+ 3) % 7
= (3 + 3) % 7
= 6 % 7 = 6
84
32
61
3
Linear Probing
Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count.
Generalized function
h(k) + i % S
Keys
5
7
25
84
32
61
3
Hash Function
H(k) = key % 5
Index Value
0
1
2
3
4
5
6
Hash Table
H(k) = 3 % 5
H(k) = 3 % 5 = 3
5
7
25
84
32
61
3
ADVANTAGES
DISADVANTAGES
Linear Probing
Simple to Implement
Hash Table entirely fills up
Very Efficient
ADVANTAGES
DISADVANTAGES
Linear Probing ADVANTAGES
DISADVANTAGES
Linear Probing
Clustering Issues – Primary and Secondary
Primary Clustering – Many consecutive elements form groups and it starts taking time to find
a free slot or to search an element
Secondary Clustering – Two Records only have the same collision chain if their initial
position is the same.
ADVANTAGES
DISADVANTAGES
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing
Quadratic Probing
Double Hashing
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing
Quadratic Probing
Double Hashing
In Quadratic Probing, the algorithm searches for slots in a more spaced out manner.
This is done to eliminate the drawback of clustering faced in linear probing. When a
collision occurs the algorithm looks for the next slot using an equation that involves
the original hash value and a quadratic function. If that slot is also occupied the
algorithms increment the value of the quadratic function and tries again .
This process is repeated until an empty slot is found.
Quadratic Probing
Let hash(k) be the slot index computed using hash function, S be the sized of the table &
I be the probe count. Let f(i) = i^2
Quadratic Probing
Let hash(k) be the slot index computed using hash function, S be the sized of the table &
I be the probe count. Let f(i) = i^2
If slot hash(k) % S is full, then we try (hash (k)+1 *1) % S
Quadratic Probing
Let hash(k) be the slot index computed using hash function, S be the sized of the table &
I be the probe count. Let f(i) = i^2
If slot hash(k) % S is full, then we try (hash (k)+1 *1) % S
If hash((k) +1*1) % S is full, then we try (hash (k)+1*2) % S
Quadratic Probing
Let hash(k) be the slot index computed using hash function, S be the sized of the table &
I be the probe count. Let f(i) = i^2
If slot hash(k) % S is full, then we try (hash (k)+1 *1) % S
If hash((k) +1*1) % S is full, then we try (hash (k)+1*2) % S
If hash((k) +2*2) % S is full, then we try (hash (k)+3*3) % S
F(i) = i^2
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9 19 29 32 17
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
H(k) = 9 % 5 = 4
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17 9
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
H(k) = 19 % 5 =
4
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17 9
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
Solution:
S = 5
i = 1
(h(k) + i^2 ) %
= (4 + 1) % 5
= 5 % 5 = 0
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
19
9
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
H(k) = 29 % 5 =
4
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
19
9
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
Solution:
S = 5
i = 2
(h(k) + i^2 ) %
= (4 + 4) % 5
= 8 % 5 = 3
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
19
9
29
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
H(k) = 32 % 5 =
2
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
19
9
29
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
19
9
29
32
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
H(k) = 17 % 5 =
2
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
19
9
29
32
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
Solution:
S = 5
i = 1
(h(k) + i^2 ) %
= (2 + 1) % 5
= 3 % 5 = 3
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
19
9
29
32
Keys
Hash Function
h(k) = key % 5
Index Value
0
1
2
3
4
Hash Table
Solution:
S = 5
i = 2
(h(k) + i^2 ) %
= (2 + 4) % 5
= 6 % 5 = 1
Quadratic Probing F(i) = i^2
Example: Store these 5 elements in Hash Table
9
19
29
32
17
19
9
29
32
17
Reporter: Joshua Mangrobang
ADVANTAGES
DISADVANTAGES
Quadratic Probing
Simple To Implement
Hash Table Entirely Fills up
Solves the primary issue of
Clustering face in linear probing
ADVANTAGES
DISADVANTAGES
Quadratic Probing
Simple To Implement
Hash Table Entirely Fills up
Solves the primary issue of
Clustering face in linear probing
One drawback of quadratic probing is that the
probe will not cover all memory slots
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing Quadratic Probing
Double Hashing
Closed Hashing
(Open Addressing)
3 Type of Closed
Hashing Techniques
Linear Probing Quadratic Probing
Double Hashing
In Double Hashing, we make use of two functions. The first hash function is h1(k).
This function takes in our key and gives out a location on the hash table. If the new
location is empty, we can easily place our key in there without ever using the
secondary hash function
However in case of collision we need to use secondary hash function h2(k) in
combination with the first hash function h1(k) to find a new location to a hash table.
Let h1(k) be the first hash function & h2(k), S be the size of the table & i be the probe
count.
If slot h1(k) % S is full, then we try (h1 (k)+1 * (h2(k)) % S
If (h1((k) +1* (h2(k))) % S is full, then we try (h1(k)+ 2* (h2(k))) % S
If (h1((k) +2* (h2(k))) % S is full, then we try (h1(k)+ 3* (h2(k))) % S
(h1(k) + i* (h2(k))) % S
Double Hashing
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7 25 83 32 61
5 4
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
H1(k) = 5 % 5 =
0
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
H1(k) = 5 % 5 =
0
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
5
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
H1(k) = 7 % 5 =
2
5
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
5
7
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
H1(k) = 25 % 5 =
0
5
7
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
Solution:
S = 5
i = 1
(h1(k) + i * (h2(k)))
% S
= 0 + 1 * 4 % 7
= 4 % 7 = 4
5
7
25
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
H1(k) = 83 % 5 =
3
5
7
25
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
5
7
25
83
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
H1(k) = 32 % 5 =
2
5
7
25
83
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
Solution:
S = 5
i = 1
(h1(k) + i * (h2(k)))
% S
= 2 + 1 * 4 % 7
= 6 % 7 = 6
H1(k) = 32 % 5 =
2
5
7
H2(k) = 25 % 7 =
4
25
83
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
Solution:
S = 5
i = 1
(h1(k) + i * (h2(k)))
% S
= 2 + 1 * 4 % 7
= 6 % 7 = 6
5
7
25
83
32
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
H1(k) = 61 % 5 =
1
5
7
25
83
32
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
H1(k) = 4 % 5 =
4
5
7
25
83
32
61
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
Solution:
S = 5
i = 1
(h1(k) + i * (h2(k)))
% S
= 4 + 1 * 4 % 7
= 8 % 7 = 1
5
7
25
83
32
61
Reporter: Joshua Mangrobang
(h1(k) + i* (h2(k))) % S
Example: Store these 7 elements in hash table
Double Hashing
7
25
83
32
61
5
4
Keys
Hash Function
h1(k) = key % 5
h2(k) = key % 7
Index Value
0
1
2
3
4
5
6
Hash Table
Solution:
S = 7
i = 2
(h1(k) + i * (h2(k)))
% S
= 4 + 2 * 4 % 7
= 12 % 7 = 5
5
7
25
83
32
61
4
ADVANTAGES
DISADVANTAGES
No Clustering
Produce Uniform
Distribution of records
More effective than linear or
quadratic probing
Double Hashing
ADVANTAGES
DISADVANTAGES
No Clustering
Produce Uniform
Distribution of records
More effective than linear or
quadratic probing
Double Hashing
Collision may still occur with double hashing & can cause thrashing
Code
Index Value
0
1
2
3
4
5
6
7
8
9
Code
Index Value
0
1
2
3
4
5
6
7
8
9
Reporter: Joshua Mangrobang

More Related Content

Similar to Data Structure and Algorithms: What is Hash Table ppt

Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11Bianca Teşilă
 
hashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javaishasharma835109
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing TablesChinmaya M. N
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptxkratika64
 
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfAssignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfkksrivastava1
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)Home
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Kuntal Bhowmick
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptxskilljiolms
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashingchidabdu
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithmfarhankhan89766
 
Hash table in java
Hash table in javaHash table in java
Hash table in javasiriindian
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxmy6305874
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingHaripritha
 
computer notes - Data Structures - 37
computer notes - Data Structures - 37computer notes - Data Structures - 37
computer notes - Data Structures - 37ecomputernotes
 

Similar to Data Structure and Algorithms: What is Hash Table ppt (20)

Data structures and algorithms lab11
Data structures and algorithms lab11Data structures and algorithms lab11
Data structures and algorithms lab11
 
hashing in data strutures advanced in languae java
hashing in data strutures advanced in languae javahashing in data strutures advanced in languae java
hashing in data strutures advanced in languae java
 
Hashing And Hashing Tables
Hashing And Hashing TablesHashing And Hashing Tables
Hashing And Hashing Tables
 
Hashing.pptx
Hashing.pptxHashing.pptx
Hashing.pptx
 
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdfAssignment4 Assignment 4 Hashtables In this assignment we w.pdf
Assignment4 Assignment 4 Hashtables In this assignment we w.pdf
 
Hashing
HashingHashing
Hashing
 
Data Structures : hashing (1)
Data Structures : hashing (1)Data Structures : hashing (1)
Data Structures : hashing (1)
 
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)Hashing notes data structures (HASHING AND HASH FUNCTIONS)
Hashing notes data structures (HASHING AND HASH FUNCTIONS)
 
Hash table (2)
Hash table (2)Hash table (2)
Hash table (2)
 
Ch17 Hashing
Ch17 HashingCh17 Hashing
Ch17 Hashing
 
VCE Unit 04vv.pptx
VCE Unit 04vv.pptxVCE Unit 04vv.pptx
VCE Unit 04vv.pptx
 
Sienna 9 hashing
Sienna 9 hashingSienna 9 hashing
Sienna 9 hashing
 
11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm11_hashtable-1.ppt. Data structure algorithm
11_hashtable-1.ppt. Data structure algorithm
 
Hash table in java
Hash table in javaHash table in java
Hash table in java
 
Hash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptxHash in datastructures by using the c language.pptx
Hash in datastructures by using the c language.pptx
 
Chapter 12 ds
Chapter 12 dsChapter 12 ds
Chapter 12 ds
 
Open addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashingOpen addressing &amp rehashing,extendable hashing
Open addressing &amp rehashing,extendable hashing
 
Hashing
HashingHashing
Hashing
 
asdfew.pptx
asdfew.pptxasdfew.pptx
asdfew.pptx
 
computer notes - Data Structures - 37
computer notes - Data Structures - 37computer notes - Data Structures - 37
computer notes - Data Structures - 37
 

Recently uploaded

Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Zilliz
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingWSO2
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuidePixlogix Infotech
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdfSandro Moreira
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusZilliz
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...caitlingebhard1
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Victor Rentea
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringWSO2
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard37
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAnitaRaj43
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FMESafe Software
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...Zilliz
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAndrey Devyatkin
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Orbitshub
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)Samir Dash
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfdanishmna97
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaWSO2
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformWSO2
 

Recently uploaded (20)

Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)Introduction to Multilingual Retrieval Augmented Generation (RAG)
Introduction to Multilingual Retrieval Augmented Generation (RAG)
 
Quantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation ComputingQuantum Leap in Next-Generation Computing
Quantum Leap in Next-Generation Computing
 
JavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate GuideJavaScript Usage Statistics 2024 - The Ultimate Guide
JavaScript Usage Statistics 2024 - The Ultimate Guide
 
WSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering DevelopersWSO2's API Vision: Unifying Control, Empowering Developers
WSO2's API Vision: Unifying Control, Empowering Developers
 
[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf[BuildWithAI] Introduction to Gemini.pdf
[BuildWithAI] Introduction to Gemini.pdf
 
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
WSO2 Micro Integrator for Enterprise Integration in a Decentralized, Microser...
 
Exploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with MilvusExploring Multimodal Embeddings with Milvus
Exploring Multimodal Embeddings with Milvus
 
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...Stronger Together: Developing an Organizational Strategy for Accessible Desig...
Stronger Together: Developing an Organizational Strategy for Accessible Desig...
 
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024Finding Java's Hidden Performance Traps @ DevoxxUK 2024
Finding Java's Hidden Performance Traps @ DevoxxUK 2024
 
Choreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software EngineeringChoreo: Empowering the Future of Enterprise Software Engineering
Choreo: Empowering the Future of Enterprise Software Engineering
 
JohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptxJohnPollard-hybrid-app-RailsConf2024.pptx
JohnPollard-hybrid-app-RailsConf2024.pptx
 
AI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by AnitarajAI in Action: Real World Use Cases by Anitaraj
AI in Action: Real World Use Cases by Anitaraj
 
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers:  A Deep Dive into Serverless Spatial Data and FMECloud Frontiers:  A Deep Dive into Serverless Spatial Data and FME
Cloud Frontiers: A Deep Dive into Serverless Spatial Data and FME
 
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ..."I see eyes in my soup": How Delivery Hero implemented the safety system for ...
"I see eyes in my soup": How Delivery Hero implemented the safety system for ...
 
AWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of TerraformAWS Community Day CPH - Three problems of Terraform
AWS Community Day CPH - Three problems of Terraform
 
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
Navigating the Deluge_ Dubai Floods and the Resilience of Dubai International...
 
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
AI+A11Y 11MAY2024 HYDERBAD GAAD 2024 - HelloA11Y (11 May 2024)
 
How to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cfHow to Check CNIC Information Online with Pakdata cf
How to Check CNIC Information Online with Pakdata cf
 
Modernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using BallerinaModernizing Legacy Systems Using Ballerina
Modernizing Legacy Systems Using Ballerina
 
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data PlatformLess Is More: Utilizing Ballerina to Architect a Cloud Data Platform
Less Is More: Utilizing Ballerina to Architect a Cloud Data Platform
 

Data Structure and Algorithms: What is Hash Table ppt

  • 2. ● What is a HASH TABLE? ● What is a HASH FUNCTION? ● What is a HASH COLLISION? ● Implementation of a Hash Table ● Discussion on collision resolution methods in particular SEPARATE CHAINING and OPEN ADDRESSING ● Separate Chaining Implementation Open Addressing Implementation ● Linear Probing ● Quadratic Probing ● Double Hashing Outline
  • 3. Hash Tables are a data structure that allow you to create a list of paired values. You can then retrieve a certain value by using the key for that value, which you put into the table beforehand. What is a Hash Table
  • 4. Hash Tables are a data structure that allow you to create a list of paired values. You can then retrieve a certain value by using the key for that value, which you put into the table beforehand. What is a Hash Table A Hash Table transforms a key into an integer index using a hash function, and the index will decide where to store the key/value pair in memory.
  • 5. What is Hash Function: A hash function is used to transform a given key into a specific slot index. it is used to map each and every possible key into a unique slot index. If every key is mapped into a unique slot index, then the hash function is known as a perfect hash function.
  • 6. What is Hash Function: A hash function is used to transform a given key into a specific slot index. it is used to map each and every possible key into a unique slot index. If every key is mapped into a unique slot index, then the hash function is known as a perfect hash function. A hash function receives the input keys and returns the index of an element in an array called a hash table. The index is known as hash index.
  • 7. What is Hash Function: A hash function is used to transform a given key into a specific slot index. it is used to map each and every possible key into a unique slot index. If every key is mapped into a unique slot index, then the hash function is known as a perfect hash function. A hash function receives the input keys and returns the index of an element in an array called a hash table. The index is known as hash index.
  • 8. What is Hash Function:
  • 9.
  • 10. By hashing a password, the company protects user information. Even if a hacker breaks into the system, they won’t have access to actual passwords, just the hashes.
  • 11.
  • 12. Whenever you log in, your email provider doesn’t store the plain text password, all they need is the hash. When you enter your password, it is run through the hash function. The output is matched against the hash that is saved in the database. If the hash values are the same, the password is correct.
  • 13. What is Collision: In hashing. A collision happens when the hash function generates same hash (output) for different set of keys (inputs). A collision occurs when more than one value to be hashed by a particular hash function hash to the same slot in the table or data structure (hash table) being generated by the hash function.
  • 14. Collision Example: 1 7 18 30 Index Value 0 1 2 3 4 10
  • 15. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 10
  • 16. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 10 % 5 = 0 10
  • 17. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 10 % 5 = 0 10 1
  • 18. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 1 % 5 = 1 10 1
  • 19. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 1 % 5 =1 10 1 7
  • 20. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 7 % 5 =2 10 1 7
  • 21. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 7 % 5 =2 10 1 7
  • 22. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 18 % 5 = 3 10 1 7 18
  • 23. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 18 % 5 = 3 10 1 7 18
  • 24. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 30 % 5 = 0 10 1 7 18 30
  • 25. Collision Example: 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function 30 % 5 = 0 10 1 7 18 30 Collision Reporter: Joshua Mangrobang
  • 26. COLLISION CONTROL The process of finding alternate index position in the Hash Table. Collision Control Open Hashing Close Hashing
  • 27. COLLISION CONTROL The process of finding alternate index position in the Hash Table. Collision Control Open Hashing (Separate Chaining) Close Hashing (Open Adrressing)
  • 28. COLLISION CONTROL The process of finding alternate index position in the Hash Table. Collision Control Open Hashing Separate Chaining Close Hashing (Open Adrressing) • Linear Probing
  • 29. COLLISION CONTROL The process of finding alternate index position in the Hash Table. Collision Control Open Hashing Separate Chaining Close Hashing (Open Adrressing) • Linear Probing • Quadratic Probing
  • 30. COLLISION CONTROL The process of finding alternate index position in the Hash Table. Collision Control Open Hashing Separate Chaining Close Hashing (Open Adrressing) • Linear Probing • Quadratic Probing • Double Hashing Reporter: Joshua Mangrobang
  • 32. COLLISION CONTROL Open Hashing Separate Chaining Separate Chaining is a technique which is uses linked list data structures known as a chain. This is one of the most popular and commonly used techniques in order to handle collision.
  • 33. COLLISION CONTROL Open Hashing Separate Chaining Separate Chaining is a technique which is uses linked list data structures known as a chain. This is one of the most popular and commonly used techniques in order to handle collision. When multiple elements are hashed into the same slot index, then these elements are inserted into a singly linked list which is known as chain.
  • 34. COLLISION CONTROL Open Hashing Separate Chaining Separate Chaining is a technique which is uses linked list data structures known as a chain. This is one of the most popular and commonly used techniques in order to handle collision. When multiple elements are hashed into the same slot index, then these elements are inserted into a singly linked list which is known as chain. Stores data input values in linked lists identified by the index keys created by the hashing functions. The table cells don't hold the input values as in open addressing but rather store the index keys created by the hashing function.
  • 35. 1 7 18 30 H(k)= key % 5 Index Value 0 1 2 3 4 10 Key Hash Function Open Hashing (Separate Chaining) 23 40 3 23 40 3 18 7 1 10 30 Hash Table
  • 36. Open Hashing (Separate Chaining) ADVANTAGES DISADVANTAGES
  • 37. Open Hashing (Separate Chaining) ADVANTAGES DISADVANTAGES Simple to Implement
  • 38. Open Hashing (Separate Chaining) ADVANTAGES DISADVANTAGES Simple to Implement Hash Table never fills up, we can always add more elements to the chain
  • 39. Open Hashing (Separate Chaining) ADVANTAGES DISADVANTAGES Simple to Implement Hash Table never fills up, we can always add more elements to the chain Mostly used when its known how many and how frequenly keys may be inserted or deleted.
  • 40. Open Hashing (Separate Chaining) DISADVANTAGES
  • 41. Open Hashing (Separate Chaining) DISADVANTAGES Wastage of space (Some Parts of the hash table are never used)
  • 42. Open Hashing (Separate Chaining) DISADVANTAGES Wastage of space (Some Parts of the hash table are never used) If the chain become long, then search time can become O(n)
  • 43. Open Hashing (Separate Chaining) DISADVANTAGES Wastage of space (Some Parts of the hash table are never used) If the chain become long, then search time can become O(n) It use extra space for links
  • 44. COLLISION CONTROL Closed Hashing (Open Addressing) In Open Addressing all elements are stored in the hash table itself, So at any point, the size of the table must be greater than or equal to the total number of keys. is a method used in hash tables for resolving collisions. In hash tables, collisions occur when two different keys hash to the same index in the underlying array. Is an alternative method to resolve hash collisions. Unlike separate chaining - there are no linked lists. Each item is placed in the hash table by searching, (or probing as we'll call it), for an open bucket to place it.
  • 45. COLLISION CONTROL Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques
  • 46. COLLISION CONTROL Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing
  • 47. COLLISION CONTROL Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing Quadratic Probing
  • 48. COLLISION CONTROL Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing Quadratic Probing Double Hashing
  • 49. Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing Quadratic Probing Double Hashing
  • 50. Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing Quadratic Probing Double Hashing In Linear Probing, the hash table is search sequentially that starts from the original location of the hash. If in case the location that we get is already occupied, then we check for the next location. The function used for rehashing is as follows: rehash (key) = (n+1) % table-size
  • 51. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & I be the probe count.
  • 52. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & I be the probe count. If slot h(k) % S is full, Then we try h(k) + % S
  • 53. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & I be the probe count. If slot h(k) % S is full, Then we try h(k) + % S If h(k) % S is also full, Then we try h(k) + 2 % S
  • 54. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & I be the probe count. If slot h(k) % S is full, Then we try h(k) + % S If h(k) % S is also full, Then we try h(k) + 2 % S If h(k) % S is also full, Then we try h(k) + 3 % S
  • 55. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. If slot h(k) % S is full, Then we try h(k) + i % S If h(k) % S is also full, Then we try h(k) + 2 % S If h(k) % S is also full, Then we try h(k) + 3 % S Generalized function h(k) + i % S
  • 56. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. If slot h(k) % S is full, Then we try h(k) + i % S If h(k) % S is also full, Then we try h(k) + 2 % S If h(k) % S is also full, Then we try h(k) + 3 % S Generalized function h(k) + i % S Example: Store these 7 elements in hash table
  • 57. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. If slot h(k) % S is full, Then we try h(k) + i % S If h(k) % S is also full, Then we try h(k) + 2 % S If h(k) % S is also full, Then we try h(k) + 3 % S Generalized function h(k) + i % S Example: Store these 7 elements in hash table 5 7 25 84 32 61 3
  • 58. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table
  • 59. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table
  • 60. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 5 % 5
  • 61. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 5 % 5 H(k) = 5 % 5 = 0 5
  • 62. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 5 % 5 H(k) = 5 % 5 = 0 5
  • 63. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table 5
  • 64. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 7 % 5 5
  • 65. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 7 % 5 H(k) = 7 % 5 = 2 5
  • 66. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 7 % 5 H(k) = 7 % 5 = 2 5 7
  • 67. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table 5 7
  • 68. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 25 % 5 5 7
  • 69. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 25 % 5 H(k) = 25 % 5 = 0 5 7 25 Solution: S = 7 i = n
  • 70. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table 5 7 25 Solution: S = 7 i = n h(k) = ((key % 5) + 1) % 7 h(k) = ((25 % 5) + 1) % 7 = (0 + 1) % 7 = 1 % 7 =1
  • 71. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 84 % 5 H(k) = 84 % 5 = 4 5 7 25
  • 72. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table 5 7 25 84
  • 73. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 32 % 5 H(k) = 32 % 5 = 2 5 7 25 84 32
  • 74. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 32 % 5 H(k) = 32 % 5 = 2 5 7 25 Solution: S = 7 i = n h(k) = ((key % 5) + 1) % 7 h(k) = ((32 % 5) + 1) % 7 = (2 + 1) % 7 = 3 % 7 = 3 84 32 Reporter: Joshua Mangrobang
  • 75. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 61 % 5 5 7 25 84 32
  • 76. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 61 % 5 H(k) = 61 % 5 = 1 5 7 25 Solution: S = 7 i = 1 84 32 61
  • 77. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 61 % 5 H(k) = 61 % 5 = 1 5 7 25 Solution: S = 7 i = 2 84 32 61
  • 78. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 61 % 5 H(k) = 61 % 5 = 1 5 7 25 Solution: S = 7 i = 3 84 32 61
  • 79. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 61 % 5 H(k) = 61 % 5 = 1 5 7 25 Solution: S = 7 i = 4 h(k) = ((key % 5) + 1) % 7 h(k) = ((61 % 5) + 4) % 7 = (1 + 4) % 7 = 5 % 7 = 5 84 32 61
  • 80. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table 5 7 25 Solution: S = 7 i = 4 h(k) = ((key % 5) + 1) % 7 h(k) = ((61 % 5) + 4) % 7 = (1 + 4) % 7 = 5 % 7 = 5 84 32 61
  • 81. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 3 % 5 5 7 25 Solution: S = 7 i = n 84 32 61
  • 82. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 3 % 5 H(k) = 3 % 5 = 3 5 7 25 Solution: S = 7 i = 1 84 32 61 3
  • 83. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 3 % 5 H(k) = 3 % 5 = 3 5 7 25 Solution: S = 7 i = 2 84 32 61 3
  • 84. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 3 % 5 H(k) = 3 % 5 = 3 5 7 25 Solution: S = 7 i = 3 84 32 61 3
  • 85. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 3 % 5 H(k) = 3 % 5 = 3 5 7 25 Solution: S = 7 i = 3 h(k) = ((key % 5) + 1) % 7 h(k) = ((3 % 5) + 3) % 7 = (3 + 3) % 7 = 6 % 7 = 6 84 32 61 3
  • 86. Linear Probing Let h(k) be the slot index computed using a hash function, S be the table of size & i be the probe count. Generalized function h(k) + i % S Keys 5 7 25 84 32 61 3 Hash Function H(k) = key % 5 Index Value 0 1 2 3 4 5 6 Hash Table H(k) = 3 % 5 H(k) = 3 % 5 = 3 5 7 25 84 32 61 3 ADVANTAGES DISADVANTAGES
  • 87. Linear Probing Simple to Implement Hash Table entirely fills up Very Efficient ADVANTAGES DISADVANTAGES
  • 89. Linear Probing Clustering Issues – Primary and Secondary Primary Clustering – Many consecutive elements form groups and it starts taking time to find a free slot or to search an element Secondary Clustering – Two Records only have the same collision chain if their initial position is the same. ADVANTAGES DISADVANTAGES
  • 90. Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing Quadratic Probing Double Hashing
  • 91. Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing Quadratic Probing Double Hashing In Quadratic Probing, the algorithm searches for slots in a more spaced out manner. This is done to eliminate the drawback of clustering faced in linear probing. When a collision occurs the algorithm looks for the next slot using an equation that involves the original hash value and a quadratic function. If that slot is also occupied the algorithms increment the value of the quadratic function and tries again . This process is repeated until an empty slot is found.
  • 92. Quadratic Probing Let hash(k) be the slot index computed using hash function, S be the sized of the table & I be the probe count. Let f(i) = i^2
  • 93. Quadratic Probing Let hash(k) be the slot index computed using hash function, S be the sized of the table & I be the probe count. Let f(i) = i^2 If slot hash(k) % S is full, then we try (hash (k)+1 *1) % S
  • 94. Quadratic Probing Let hash(k) be the slot index computed using hash function, S be the sized of the table & I be the probe count. Let f(i) = i^2 If slot hash(k) % S is full, then we try (hash (k)+1 *1) % S If hash((k) +1*1) % S is full, then we try (hash (k)+1*2) % S
  • 95. Quadratic Probing Let hash(k) be the slot index computed using hash function, S be the sized of the table & I be the probe count. Let f(i) = i^2 If slot hash(k) % S is full, then we try (hash (k)+1 *1) % S If hash((k) +1*1) % S is full, then we try (hash (k)+1*2) % S If hash((k) +2*2) % S is full, then we try (hash (k)+3*3) % S F(i) = i^2
  • 96. Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17
  • 97. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17
  • 98. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table H(k) = 9 % 5 = 4 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17
  • 99. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 9
  • 100. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table H(k) = 19 % 5 = 4 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 9
  • 101. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table Solution: S = 5 i = 1 (h(k) + i^2 ) % = (4 + 1) % 5 = 5 % 5 = 0 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 19 9
  • 102. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table H(k) = 29 % 5 = 4 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 19 9
  • 103. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table Solution: S = 5 i = 2 (h(k) + i^2 ) % = (4 + 4) % 5 = 8 % 5 = 3 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 19 9 29
  • 104. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table H(k) = 32 % 5 = 2 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 19 9 29
  • 105. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 19 9 29 32
  • 106. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table H(k) = 17 % 5 = 2 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 19 9 29 32
  • 107. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table Solution: S = 5 i = 1 (h(k) + i^2 ) % = (2 + 1) % 5 = 3 % 5 = 3 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 19 9 29 32
  • 108. Keys Hash Function h(k) = key % 5 Index Value 0 1 2 3 4 Hash Table Solution: S = 5 i = 2 (h(k) + i^2 ) % = (2 + 4) % 5 = 6 % 5 = 1 Quadratic Probing F(i) = i^2 Example: Store these 5 elements in Hash Table 9 19 29 32 17 19 9 29 32 17 Reporter: Joshua Mangrobang
  • 109. ADVANTAGES DISADVANTAGES Quadratic Probing Simple To Implement Hash Table Entirely Fills up Solves the primary issue of Clustering face in linear probing
  • 110. ADVANTAGES DISADVANTAGES Quadratic Probing Simple To Implement Hash Table Entirely Fills up Solves the primary issue of Clustering face in linear probing One drawback of quadratic probing is that the probe will not cover all memory slots
  • 111. Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing Quadratic Probing Double Hashing
  • 112. Closed Hashing (Open Addressing) 3 Type of Closed Hashing Techniques Linear Probing Quadratic Probing Double Hashing In Double Hashing, we make use of two functions. The first hash function is h1(k). This function takes in our key and gives out a location on the hash table. If the new location is empty, we can easily place our key in there without ever using the secondary hash function However in case of collision we need to use secondary hash function h2(k) in combination with the first hash function h1(k) to find a new location to a hash table.
  • 113. Let h1(k) be the first hash function & h2(k), S be the size of the table & i be the probe count. If slot h1(k) % S is full, then we try (h1 (k)+1 * (h2(k)) % S If (h1((k) +1* (h2(k))) % S is full, then we try (h1(k)+ 2* (h2(k))) % S If (h1((k) +2* (h2(k))) % S is full, then we try (h1(k)+ 3* (h2(k))) % S (h1(k) + i* (h2(k))) % S Double Hashing
  • 114. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4
  • 115. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table H1(k) = 5 % 5 = 0
  • 116. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table H1(k) = 5 % 5 = 0
  • 117. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table 5
  • 118. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table H1(k) = 7 % 5 = 2 5
  • 119. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table 5 7
  • 120. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table H1(k) = 25 % 5 = 0 5 7
  • 121. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table Solution: S = 5 i = 1 (h1(k) + i * (h2(k))) % S = 0 + 1 * 4 % 7 = 4 % 7 = 4 5 7 25
  • 122. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table H1(k) = 83 % 5 = 3 5 7 25
  • 123. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table 5 7 25 83
  • 124. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table H1(k) = 32 % 5 = 2 5 7 25 83
  • 125. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table Solution: S = 5 i = 1 (h1(k) + i * (h2(k))) % S = 2 + 1 * 4 % 7 = 6 % 7 = 6 H1(k) = 32 % 5 = 2 5 7 H2(k) = 25 % 7 = 4 25 83
  • 126. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table Solution: S = 5 i = 1 (h1(k) + i * (h2(k))) % S = 2 + 1 * 4 % 7 = 6 % 7 = 6 5 7 25 83 32
  • 127. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table H1(k) = 61 % 5 = 1 5 7 25 83 32
  • 128. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table H1(k) = 4 % 5 = 4 5 7 25 83 32 61
  • 129. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table Solution: S = 5 i = 1 (h1(k) + i * (h2(k))) % S = 4 + 1 * 4 % 7 = 8 % 7 = 1 5 7 25 83 32 61 Reporter: Joshua Mangrobang
  • 130. (h1(k) + i* (h2(k))) % S Example: Store these 7 elements in hash table Double Hashing 7 25 83 32 61 5 4 Keys Hash Function h1(k) = key % 5 h2(k) = key % 7 Index Value 0 1 2 3 4 5 6 Hash Table Solution: S = 7 i = 2 (h1(k) + i * (h2(k))) % S = 4 + 2 * 4 % 7 = 12 % 7 = 5 5 7 25 83 32 61 4
  • 131. ADVANTAGES DISADVANTAGES No Clustering Produce Uniform Distribution of records More effective than linear or quadratic probing Double Hashing
  • 132. ADVANTAGES DISADVANTAGES No Clustering Produce Uniform Distribution of records More effective than linear or quadratic probing Double Hashing Collision may still occur with double hashing & can cause thrashing