In: Computer Science
Assume that we define h1(k) = k mod 13 and h2(k) = 1 + (k mod
11).
For the opening addressing, consider the following methods
Linear Probing
Given an ordinary hash function h’: U {0, 1, 2, …, m-1}, the method
of linear probing
uses the hash function
h(k, i) = (h1(k) + i) mod m for i = 0, 1, 2, …,
m-1.
Quadratic Probing
Uses a hashing function of the form
h(k, i) = (h1(k) + c1i + c2i2 ) mod m,
where h1 is an auxiliary hash function, c1 and c2 0 are auxiliary
constants c1 3 c2 = 5,
and i = 0, 1, 2, …, m-1.
Double hashing
Uses a hashing function of the form
h(k, i) = (h1(k) + i h2(k) ) mod m,
where h1 and h2 are auxiliary hash functions.
The value of h2(k) must never be zero and should be relatively
prime to m for the sequence
to include all possible address.
Given K = {79, 69, 98, 72, 14, 50, 88, 99, 78, 65} and the size
of a table is 13, with indices counting from 0, 1, 2, …, 12. Store
the given K in a table with the size 13 counting the indices from
0, 1, 2, …, 12. Show the resultant table with 10 given keys for
each method applied:
⦁ if linear probing is employed,
⦁ if quadratic probing is employed, and
⦁ if double hashing is employed.
K={79, 69, 98, 72, 14, 50, 88, 99, 78, 65}
BY linear probing we will save the key at the indices which comes as remainder
if collision occurs then will use h(k, i) = (h1(k) + i) mod m this formula to find next empty space
m=12
h1= 79 mod 12 = 7
h2 = 69 mod 12 = 9
h3 = 98 mod 12 = 2
h4= 72 mod 12 = 0
h5 = 14 mod 12 = 2 collision occurs so , will add 1 to hash key then as using linear probing
h5 = (14+1) mod 12 = 3
h6 = 50 mod 12 = 2 collision occurs so , will add 1 to hash key then
h6 = 50 +1 mod 12 = 3 collision
h6 = 51+1 mod 12 = 4
h7= 88 mod 12 = 4 collision occurs, As 4 is already occupied by hash key 50
h7= 88 +1 mod 12 = 5
h8= 99 mod 12 = 3 collision occurs, As 3 is already occupied by hash key 14
h8= 99+1 mod 12 = 4
h8= 100+1 mod 12 = 5
h8= 101 + 1 mod 12 = 6
h9 = 78 mod 12 = 6 collision occurs, As indicie 6 is already occupied by hash key 99
h9 = 78 +1 mod 12 = 7
h9 = 79 + 1 mod 12 = 8
h10= 65 mod 12 = 5 collision occurs, As 5 is already occupied by hash key 88
h10= 65 +1 mod 12 = 6
h10= 66 +1 mod 12 = 7
h10= 67 +1 mod 12 = 8
h10= 68 +1 mod 12 = 9
h10= 69 +1 mod 12 = 10
HASH VALUE | HASH KEY |
---|---|
0 | 72 |
1 | |
2 | 98 |
3 | 14 |
4 | 50 |
5 | 88 |
6 | 99 |
7 | 79 |
8 | 78 |
9 | 69 |
10 | 65 |
11 | |
12 |
BY Quadratic probing we will save the key at the indices which comes as remainder
if collision occurs then will use h(k, i) = (h1(k) + c1i + c2i2 ) mod m this formula to find next empty space
m=12 ,c1 =3 c2 = 5
h1= 79 mod 12 = 7
h2 = 69 mod 12 = 9
h3 = 98 mod 12 = 2
h4= 72 mod 12 = 0
h5 = 14 mod 12 = 2 collision occurs so , will add 1 to hash key then
h5 = ( 14 + (3*1)+ (5* 12)) mod 12 = 22 MOD 12 = 10
h6 = 50 mod 12 = 2 collision occurs so , will add 1 to hash key then
h6 = ( 50 + (3*1)+ (5* 12)) mod 12 = 58 MOD 12 = 10 collsion occurs
h6 = ( 50 + (3*2)+ (5* 22)) mod 12 = 76 mod 12 = 4
h7= 88 mod 12 = 4 collision occurs, As 4 is already occupied by hash key 50
h7= ( 88 + (3*1)+ (5* 12)) mod 12 = 96 mod 12 =0 collsion occurs again
h7= ( 88 + (3*2)+ (5* 22)) mod 12 = 114 mod 12 = 6
h8= 99 mod 12 = 3
h9 = 78 mod 12 = 6 collision occurs, As indicie 6 is already occupied by hash key 88
h9 = ( 78+ (3*1)+ (5* 12)) mod 12 = 86 mod 12 =2 collision occurs as 98 is already at indices 2.
h9 = ( 78 + (3*2)+ (5* 22)) mod 12 = 114 mod 12 = 6 collision occurs as 88 is already at indices 6.
h9 = ( 78 + (3*3)+ (5* 32)) mod 12 = 132 mod 12 = 0 collision occurs as 72 is already at indices 0.
h9 = ( 78 + (3*4)+ (5* 42)) mod 12 = 170 mod 12 = 2 collision occurs as 98 is already at indices 2.
h9 = ( 78 + (3*5)+ (5* 52)) mod 12 = 218 mod 12 = 2 collision occurs as 98 is already at indices 2.
h9 = ( 78 + (3*6)+ (5* 62)) mod 12 = 276 mod 12 = 0 collision occurs as 72 is already at indices 0.
h9 = ( 78 + (3*7)+ (5* 72)) mod 12 = 344 mod 12 = 8
h10 = 65 mod 12 = 5
HASH VALUE(Indices) | HASH KEY |
---|---|
0 | 72 |
1 | |
2 | 98 |
3 | 99 |
4 | 50 |
5 | 65 |
6 | 88 |
7 | 79 |
8 | 78 |
9 | 69 |
10 | 14 |
11 | |
12 |
BY Double Hashing we will save the key at the indices which comes as remainder
if collision occurs then will use h(k, i) = (h1(k) + i h2(k) ) mod m, this formula to find next empty space
h2(k) this the second hash which we will find if collision occurs with the use of prime no. and then will put the value of i linearly.
m=12
h1= 79 mod 12 = 7
h2 = 69 mod 12 = 9
h3 = 98 mod 12 = 2
h4= 72 mod 12 = 0
h5 = 14 mod 12 = 2 collision occurs so , As 98 is already at that place.
h(k, i) = (h5 + i hash2(k) ) mod m,
hash2(5) = 7 - (14 mod 7) = 7
h5 = (2 + 1* 7) mod 12 = 9 mod 12 =2 collsion as 98 is already there at that place.
h5 = (2 + 2* 7) mod 12 = 16 mod 12 =4
h6 = 50 mod 12 = 2 collision occurs so , As 98 is already at that place
h(k, i) = (h6 + i hash2(k) ) mod m,
hash2(6) = 7 - (50 mod 7) = 6
h6 = (2 + 1* 6) mod 12 = 8
h7= 88 mod 12 = 4 collision occurs, As 4 is already occupied by hash key 50
h(k, i) = (h6 + i hash2(k) ) mod m,
hash2(7) = 7 - (88 mod 7) = 3
h7 = (2 + 1* 3) mod 12 = 5
h8= 99 mod 12 = 3
h9 = 78 mod 12 = 6
h10= 65 mod 12 = 5 collision occurs, As 5 is already occupied by hash key 88
h(k, i) = (h10 + i hash2(k) ) mod m,
hash2(10) = 7 - (65 mod 7) = 5
h10 = (2 + 1* 5) mod 12 = 7 collision occurs, As 7is already occupied by hash key 79
h10 = (2 + 2* 5) mod 12 = 0 collision occurs, As 0 is already occupied by hash key 72
h10 = (2 + 3* 5) mod 12 = 5 collision occurs, As 5 is already occupied by hash key 88
h10 = (2 + 4* 5) mod 12 = 10 it is there.
HASH VALUE | HASH KEY |
---|---|
0 | 72 |
1 | |
2 | 98 |
3 | 99 |
4 | 14 |
5 | 88 |
6 | 78 |
7 | 79 |
8 | 50 |
9 | 69 |
10 | 65 |
11 | |
12 |