Outline:
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
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.
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.
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
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.
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.
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
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
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
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