CS5800 Assignment 6
CS5800 Assignment 6
① h(x) = Xmod 10
,
Insert 7 3 ,
13 , 6 ,
3 ,
27 , 8]
modi
Inert 5 :
h(3) =
3
> 3
5
-
:
·
Insert 15 :
h(13) : 15 mod 10
= 3
"
e
Insert 6 :
h(6) : 6 mod 10
= 6
:
P
Insert 3 : I
h(3) =
3 mod 10
3 >
-
3
= 3 I
5 >
-
3 -> 15
↑
Insert 27 :
h(27) :
27 mod 10 I
=
7 3 >
-
3
"
i
P
Insert 8 : I
h (8) : I mod 10
3 >
-
3
I
= S
> 3 -> 15
F
-
O .
Universal
hashing :
Given :
1 = a
P
Size 20
:
p = 101
a)prove
I :
,
20
b b
a (x1 -
x2) + -
= 0 (modp)
a (xi - *
2) =
0 / mod p)
we know that p 101 and since know it is prime we that
we can
say
:
,
a(X1 -X2) =
up
for some
integerm .
we know that X , X2 and so X1-Xc 0.
a= 0 modp
: P(ha p(X1) ,
=
fra ,
p(x23) =
1
20
Hence ,
the
probability that ha b(X)
,
=
most 120
making the hash family universal.
The
probability that each individual pair collide under the
is
3 +
=
0 .
05-0
universal hash function
From 0 + O
: The expected no ·
of collision =
1225X0 05 .
=
61 25
.
...
Expected no
of collision while
hashing - 61 25.
he
secondary hash function (X) It (x mod 10)
· : =
9) Insert [27 ,
18 ,
29 ,
28 ,
39 ,
13 ,
16]
Insect 27 :
Mi (27) : 27 mod 11 16
= 5 2 13
position 39
so , adding 27 to 5 3
28
4
27
InsertIS (18)
3
-
:
18 mod 1 6 29
=
7 7 18
so ,
adding Is to
position 7 S
Ip
Insert
29 :
h , (29) :
29 mod 11
=
- (collision found)
he (29) : 1 + 129 mod 10)
= 1 + 9
= 10
new
position :
(7 + ixI0) mod 11
i 1 17 mod 11 6
say
: :
,
so ,
adding 29 in position 6
Insert 28 :
-
hi (2) :
as mod 11
=
6
(collision found)
M2 (28) : 1 + 128 mod 10)
= 1 + 8
=
9
16 + ix 9) mod
new
position >
-
11
i 1, 13 mod 11 4
say
: :
--
so adding2s in position 4
.
39
set -
39 mod
=
6 (Collision found)
h 2 (39) :
1 + 139 mod 10)
=
1 - 9
= 10
new position :
(6 + ix10) mod I
say i : 1
,
I mod 11:3 / collision
found
i: 2 26 modi ↑ (collision founds
say ,
:
36 modi
sayi 3 3
: :
,
so , adding 39 in position 3
Insert
13 :
hi (13) : 13 mod 11
= 2
so ,
adding 13 in position 2
cert: 16 mod 11
=
5 (collision found)
he (16) : 1 + 116 mod its
= 1 + 6
= 7
new position :
(3 + ix7) mod
modli
say i 1 12 :
,
so ,
adding 16 in position
,
16
2 13
3 39
28
4
3 27
6 29
7 18
Ip
b) Explain how double
hashing reduces
clustering in
comparison to linear
proving :
Linear
probing :
·
This will lead to a
group of continuous slots being occupied and hence forming
primary clusters
.
Double Hashing :
·
Double hashing reduces
clustering by bringing a second hash .
function
When
·
collision occurs , instead of checking the next consecutive slot like linear
·
This makes the elements in the hash table more dispersed rather than
creating a
continuous cluster
.
ixhe(x)) modn
In double hashing position (h(x)
·
new : +
,
for
where i-no .
Primary
h2(x) -
Eg : Linear
proving Double Hasking
suppose collision happens at position 6 .
be 2 or a etc...
④ Implementation of a hash table using chaining :
import random
class UniversalHash: # Initialize universal hash
function with table size
def __init__(self, table_size, p=101):
self.table_size = table_size
self.p = p # largest prime no when compared to inpot
self.a = random.randint(1, p - 1)
self.b = random.randint(0, p - 1)
def hash(self, key): # Applying universal has function on
key
key_value = sum(ord(char) for char in key)
return ((self.a * key_value + self.b) % self.p) % self.table_size
class ChainedHashTable:
def __init__(self, size):#
Initialize hash table with
empty list
self.size = size
self.table = [[] for _ in range(size)]
self.hash_function = UniversalHash(size)
def set(self, key, value): #
inserting value
for key
index = self.hash_function.hash(key)
for i, (k, v) in enumerate(self.table[index]):
if k == key:
self.table[index][i] = (key, value) # existing key
return self.table[index].append((key, value))
#
inserting new
key details
# creating chaired
hash table
hash_table = ChainedHashTable(size=8)
for name, mail in data: #
inserting data in hash table
hash_table.set(name, mail)
print("Printing the hash table:")
hash_table.display()
length of key
.