Professional Documents
Culture Documents
…
key are allowed removeElement(k): if the Our hash table uses an
Applications: dictionary has an item with array of size N = 10,000 and 9997 ∅
address book key k, removes it from the 9998 200-751-9998
the hash function
1
Dictionaries 1/19/2005 11:37 PM
Collision Handling
Hash Code Maps (§2.5.3) (§ 2.5.5)
Memory address: Component sum:
We reinterpret the memory We partition the bits of Collisions occur when 0 ∅
address of the key object as the key into components 1 025-612-0001
an integer (default hash code different elements are 2 ∅
of fixed length (e.g., 16
of all Java objects)
or 32 bits) and we sum mapped to the same 3 ∅
Good in general, except for 4
numeric and string keys
the components cell 451-229-0004 981-101-0004
(ignoring overflows)
Integer cast: Suitable for numeric keys Chaining: let each
We reinterpret the bits of the of fixed length greater cell in the table point Chaining is simple,
key as an integer than or equal to the
Suitable for keys of length to a linked list of but requires
number of bits of the
less than or equal to the integer type (e.g., long elements that map additional memory
number of bits of the integer
type (e.g., byte, short, int
and double in Java) there outside the table
and float in Java)
Dictionaries and Hash Tables 7 Dictionaries and Hash Tables 10
Compression
Maps (§2.5.4) Search with Linear Probing
Consider a hash table A Algorithm findElement(k)
Division: Multiply, Add and that uses linear probing
i ← h(k)
p←0
h2 (y) = y mod N Divide (MAD): findElement(k) repeat
The size N of the h2 (y) = (ay + b) mod N We start at cell h(k) c ← A[i]
hash table is usually if c = ∅
a and b are We probe consecutive
return NO_SUCH_KEY
chosen to be a prime nonnegative integers locations until one of the
else if c.key () = k
following occurs
The reason has to do such that return c.element()
An item with key k is
with number theory a mod N ≠ 0 found, or
else
i ← (i + 1) mod N
and is beyond the Otherwise, every An empty cell is found,
p←p+1
scope of this course integer would map to or
until p = N
N cells have been
the same value b unsuccessfully probed
return NO_SUCH_KEY
2
Dictionaries 1/19/2005 11:37 PM
Performance of
Updates with Linear Probing Hashing
To handle insertions and insert Item(k, o) In the worst case, searches,
deletions, we introduce a insertions and removals on a The expected running
We throw an exception
special object, called
if the table is full hash table take O(n) time time of all the dictionary
AVAILABLE, which replaces We start at cell h(k) The worst case occurs when ADT operations in a
deleted elements all the keys inserted into the hash table is O(1)
We probe consecutive dictionary collide
removeElement(k) cells until one of the In practice, hashing is
The load factor α = n/N
We search for an item with following occurs
affects the performance of a
very fast provided the
key k A cell i is found that is
hash table load factor is not close
either empty or stores
If such an item (k, o) is AVAILABLE, or Assuming that the hash to 100%
found, we replace it with the values are like random Applications of hash
N cells have been
special item AVAILABLE numbers, it can be shown
and we return element o
unsuccessfully probed tables:
We store item (k, o) in that the expected number of
small databases
Else, we return cell i probes for an insertion with
open addressing is compilers
NO_SUCH_KEY
1 / (1 − α) browser caches
Dictionaries and Hash Tables 13 Dictionaries and Hash Tables 16
Universal Hashing
Double Hashing (§ 2.5.6)
Double hashing uses a
secondary hash function Common choice of
d(k) and handles compression map for the A family of hash functions
collisions by placing an secondary hash function: is universal if, for any
item in the first available d2(k) = q − k mod q 0<i,j<M-1,
cell of the series Theorem: The set of
where Pr(h(j)=h(k)) < 1/N.
(i + jd(k)) mod N all functions, h, as
for j = 0, 1, … , N − 1
q<N Choose p as a prime
defined here, is
3
Dictionaries 1/19/2005 11:37 PM