Вы находитесь на странице: 1из 19

HASHING FUNCTIONS

-YAZHINI.N

HASHING

Process of mapping large amount of data


into smaller table with help of hashing
function.
It is a searching technique
Uses key to determine the memory
address
Done using hashing functions

ADVANTAGES OF HASHING:

Searched in o(1)
Independent of size
Takes constant time

HASHING FUNCTIONS

Mapping between item and slot where that


item belongs in hash table.
REPRESENTED AS:
H:K->L(key determines the memory
addresses)
Make use of hash table(collection of
items: item and its hash value)
Will take item and return integer value
from o to m-1 where m is the size of table

HASHING FUNCTION TECHNIQUES

Direct method
Modulo-division method
Midsquare method
Digit extraction method
Rotation method
Folding method

REMAINDER METHOD

Eg:
m=11 ->0 to 10 slots
none

none

none none none none none none none

6 7

none

none

10

EXAMPLE :

H(k)=k mod m

54,26,93,17,77,31
Item % tablesize ->54%11=10,26%11=4

77

ITEM

HASH VALUE

54

10

26

93

17

77

31

none none

none 26

93

none none

17

31

54

10

LOAD FACTOR:
No of items/table size
->6/11
6 out of 11 slots is occupied.
COLLISION:
If 44 is added,44%11=0.already 77 is mapped in
0.
When 2/more items need to be in same slot
collision occurs.

PERFECT HASH FUNCTION:

If the hash function maps the item into


unique slot without collision it is refered
as perfect hash function.

FOLDING METHOD

Item is divided into equal parts. Then it is


added together to get the hash value.
H(k)=k1+k2+..kr
Eg:
436-555-4601
43-65-55-46-01
43+65+55+46+01=210
210%11=1 ->slot 1

REVERSE FOLDIDNG METHOD

Even numbered parts are


reveresed during addition
eg:

436-555-4601
43-65-55-46-01
43+56+55+64+01=219
219%11=10 ->slot 10

HASH FOLDING TYPES


Example:

MIDSQUARE METHOD

H(k)=l where l is obtained by squaring the


item and deleting digits from both
ends(extracting the portion of the item)
Eg:
44:square of 44 is 1936 ->93
93%11=5 ->slot 5

Eg:

54,26,93,17,77,31
ITEM

HASH VALUE

54

26

93

17

77

31

none
0non
1
e
9 10

none

54

377 4

none

531

26
6

1
7

93

non
e

OTHER HASHING FUNCTIONS:

Direct hashing:

Key is itself treated as address.

Rotation hashing

The key is rotated and then the hashing is


performed.

FUNCTION OF HASHING

Thus
hashing
function
with key
determines
the
address

COLLISION

Collison occurs when 2/more item requires same


slot..Therefore the hashing function must ensure
that there is no collision.
There are several ways to reduce collison such as
linear probing.quadratic probing etc..

Thank
you

Вам также может понравиться