9 DictionaryandHashing-1
9 DictionaryandHashing-1
find(k):
if the dictionary has an item with key k, returns the position of this item, else, returns a null position.
insertItem(k, o):
removeElement(k):
removes the item with key k from the dictionary. Exception of no such element.
Other functions
– size(), isEmpty()
– keys(), Elements()
Dictionary
Types
– Ordered Dictionaries
A total order relation is defined on the keys
– Unordered Dictionaries
No order relation is assumed on the keys
Only equality testing between keys is used
Associative Stores
– When keys are unique, keys are like addresses to the location where the element
is stored
Dictionary : Using Direct Addressing
index = hash(key)
Hashing : An Example
Telephone directory
Hashing Function
key k
integer
index
Hash Code Maps
combine in some way the high-order and low-order portions of a 64-bit key to form a 32-
bit hash code,
summation of bits
A variant of the polynomial hash code replaces multiplication by a with a cyclic shift of a
partial sum by a certain number of bits.
HashCode
Memory address:
Casting to an Integer
object x whose binary representation can be viewed as an n-tuple (x0,x1, . . . ,xn−1) of 32-
bit integers
Summing the Components
POTS - 80 + 79 + 84 + 83 = 326
Polynomial Accumulation
x0an−1 +x1an−2+···+xn−2a+xn−1
}
POTS - 2705107
Cyclic-Shift Hash Codes
Example:
STOP - 2804276
POTS - 2705107
Hash code Excerise
break it into two groups of 3 letters, two groups of four digits and one group of
three digits. five numbers or components (x0, x1, x2, x3, x4)
- whose maximum values are 17575 (263-1), 17575 (263-1), 9999, 9999 and 999,
respectively. For the size of our hash table, we can choose a prime number near
Division Method
i mod N
Separate Chaining
Open Addressing
- linear probing
- quadratic probing
- double hashing
- Using pseudorandom number generator
Separate Chaining
• Good hash function, the
core map operations run
in O(⌈n/N⌉)
Where,
|λ= n/N, called the load
factor of the hash table
Draw the 11-entry hash table that results from using the
hash function, h(i) =(3i+5) mod 11, to hash the keys 12, 44,
13, 88, 23, 94, 11, 39, 20, 16, and 5, assuming collisions
are handled by chaining.