0% found this document useful (0 votes)
10 views10 pages

CS5800 Assignment 6

Uploaded by

swathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views10 pages

CS5800 Assignment 6

Uploaded by

swathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

1S5800 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
:

mod p) modo 0=b


h(x) (ax + b) P
= <

p = 101

a)prove
I :

family of function is universal if


P(Ma b(X1 hap(x2)) +
the has =

,
20

consider two keys X1 and X2 . To


find the
probability that
they hash to the same

value , the collision condition is

(la . x, + b) modp) mod 20 =


(lax2 + b) modp) mod 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.

is solution for mod p


... There
only one a .

a= 0 modp

There are in total (p-1) =


100 possible choices of a out of which there is
only one value

of a that satisfies the above condition


.

Given the initial


wod p that caused a coliion

: P(ha p(X1) ,
=
fra ,
p(x23) =
1
20

Hence ,
the
probability that ha b(X)
,
=

h(X) for any two distinct


integers X, and X is at

most 120
making the hash family universal.

b suppose we has so elements into the hash table ,


calculate expected number of collisions
in the Mash falls

Given hash elements into table of


that we are
trying to 50 a size so

of ways to choose elements from


The no
50-049-12 -0
2

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.

50 elements to universal hash function


③. Hash table of size 1

Primary ash function


· : h , (x) = X mod I

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
,

Final hash table :

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 :

linear probing when collision happens at a certain index the


In
algorithm checks
·

the next slot until an


empty slot is found.

·
This will lead to a
group of continuous slots being occupied and hence forming
primary clusters
.

Once cluster is formed a


future collision is more frequent in the same
region
·
a ,

Double Hashing :

·
Double hashing reduces
clustering by bringing a second hash .
function

When
·
collision occurs , instead of checking the next consecutive slot like linear

the doubl hashing has .


proving ,
uses a
secondary function

·
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 .

of times search occured


,
that key
h , (x) hash function
-

Primary
h2(x) -

Secondary has function


n-table size .

Eg : Linear
proving Double Hasking
suppose collision happens at position 6 .

Suppose collision happens at


position 6
we
try index 7, 8 , . -- and so -
on we use a
secondary has function to
determine a new
position which may

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

def get(self, key):


# Get values for the
given key
index = self.hash_function.hash(key)
for k, v in self.table[index]:
if k == key:
return v
# when
return None key is not found
def display(self): # Printing Hash table
for i, chain in enumerate(self.table):
print(f"Index {i}: {chain}")
# Testing
data = [
("person1", "[email protected]"),
("person2", "[email protected]"),
("person3", "[email protected]"),
("person4", "[email protected]"),
("person5", "[email protected]"),
("person6", "[email protected]"),

("person7", "[email protected]")
]

# 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()

print("\nFinding the value for keys:")


print(f"Person 1: {hash_table.get('person1')}")
print(f"Person 2: {hash_table.get('person2')}")

print("\nFinding values for non-existing key:")


print(f"Person 8: {hash_table.get('person8')}")
ODPUT :

Printing the hash table ::


Index 0: []
Index 1: [('person1', '[email protected]')]
Index 2: [('person7', '[email protected]')]
Index 3: [('person4', '[email protected]')]
Index 4: [('person3', '[email protected]')]
Index 5: [('person2', '[email protected]')]
Index 6: [('person6', '[email protected]')]
Index 7: [('person5', '[email protected]')]

Finding the value for keys:


Person 1: [email protected]
Person 2: [email protected]

Finding values for non-existent keys:


Person 8: None

Total time complexity of unversal hash


function is O(C) where I is

length of key
.

You might also like