hashing

8
Hashing Hashing

Upload: grahamwell

Post on 22-Nov-2014

322 views

Category:

Documents


0 download

DESCRIPTION

 

TRANSCRIPT

Page 1: Hashing

HashingHashing

Page 2: Hashing

What is hashing• Simply, generating a numeric key using an

algorithm (hash function)

• Definition: A function that maps keys to integers, usually to get an even distribution on a smaller set of values.

• The very simplest hash function is to use the modulus operator %

• Input range % key range

Page 3: Hashing

Input and key range• Example. We want to store 7 digit

telephone numbers so that they can be quickly retrieved. – Number of expected entries = 100– Range of telephone numbers = 0 –

9999999Simple hashing algorithm hash = inputNumber % 100What’s the effect?

Page 4: Hashing

Applications of hashing• File management – working out

where to store records• Comparing complex values• Cryptography – creating digital

signatures – eg: md5

Page 5: Hashing

Collisions• Where the hash value returned for

two keys is the same.

• What to do?– Open hashing– Closed hashing– Deleting

• The 2/3rds rule

Page 6: Hashing

Closed Hashing1 23234 33567

32 44 End

Hash table is supplemented by a linked list, which is used to store colliding entries.

Therefore, some values are found outside of the standard hash table (in the linked list)

Page 7: Hashing

‘Open’ Hashing

1 232 323 444 33567

Some strategy is used to fit colliding entries in a predictable way inside the existing table

For this to work, the size of the table needs to be significantly bigger than the total number of records

At least 3:2

Page 8: Hashing

DJB Hash function• “An algorithm produced by Professor Daniel J. Bernstein

and shown first to the world on the usenet newsgroup comp.lang.c. It is one of the most efficient hash functions ever published. “

def DJBHash(key): hash = 5381 for i in range(len(key)): hash = ((hash << 5) + hash) + ord(key[i])

return hash