Docsity

Hash Tables: Data Structure and Implementation, Slides of Java Programming

Toggle sidebar
bg1
Hashtables
Calin Curescu, some reworking of Goodrich,Tamassia
Hashtables
Calin Curescu, some reworking of Goodrich,Tamassia
20 pages
TDDC32 lecture 5, 2006
Hashtables
Calin Curescu, some reworking of Goodrich,Tamassia
2of 20
TDDC32 lecture 5, 2006
The Map/Dictionary ADT
•Map Abstract Data Type (ADT) methods:
– get(k): if the map M has an entry with key k, return its
associated value; else, return null
– put(k, v): insert entry (k, v) into the map M; if key k is not
already in M, then return null; else, return old value
associated with k
–remove(k): if the map M has an entry with key k, remove it
from M and return its associated value; else, return null
–size(), isEmpty()
–keys(): return an iterator of the keys in M
– values(): return an iterator of the values in M
pf3
pf4
pf5
pf8
pf9
pfa

Partial preview of the text

Download Hash Tables: Data Structure and Implementation and more Slides Java Programming in PDF only on Docsity!

Hashtables Calin Curescu, some reworking of Goodrich,Tamassia

Hashtables

Calin Curescu, some reworking of Goodrich,Tamassia

20 pages TDDC32 lecture 5, 2006

Hashtables 2 of 20

The Map/Dictionary ADT

  • Map Abstract Data Type (ADT) methods:
    • get(k): if the map M has an entry with key k, return its

associated value; else, return null

  • put(k, v): insert entry (k, v) into the map M; if key k is not

already in M, then return null; else, return old value

associated with k

  • remove(k): if the map M has an entry with key k, remove it

from M and return its associated value; else, return null

  • size(), isEmpty()
  • keys(): return an iterator of the keys in M
  • values(): return an iterator of the values in M

Hashtables Calin Curescu, some reworking of Goodrich,Tamassia

3 of 20 TDDC32 lecture 5, 2006

Why Hash Tables?

  • Complexity of Sorted Array List: find O(log n), insert O(n), remove O(n)
  • Complexity of Sorted Linked List: find O(n), insert O(n), remove O(n)
  • Complexity of Binary Search Trees: find O(log n), insert O(log n), remove O(log n)
  • Can we do better?
    • What about: find O(1), insert O(1), remove O(1)?
  • What about an array where x is set at a[x]
    • Too big for unknown and sparse values
    • Can we solve that?

Hashtables 4 of 20

  • A hash function h maps keys of a given type to integers in a fixed interval [0, N − 1]
  • Example: h ( x ) = x mod N is a hash function for integer keys
  • The integer h ( x ) is called the hash value of key x
  • A hash table for a given key type consists of
    • Hash function h
    • Array (called table) of size N
  • When implementing a map with a hash table, the goal is to store item ( k , o ) at index i = h ( k )
  • The load factor of a hash table is λ = n / N
    • Where n is the number of elements

Hash Functions and Hash Tables

Hashtables Calin Curescu, some reworking of Goodrich,Tamassia

7 of 20 TDDC32 lecture 5, 2006

  • Memory address:
    • We reinterpret the memory address of the key object as an integer (default hash code of all Java objects)
    • Good in general, except for numeric and string keys
  • Integer cast:
    • We reinterpret the bits of the key as an integer
    • Suitable for keys of length less than or equal to the number of bits of the integer type (e.g., byte, short, int and float in Java) - Component sum: - We partition the bits of the key into components of fixed length (e.g., 16 or 32 bits) and we sum the components (ignoring overflows) - Suitable for numeric keys of fixed length greater than or equal to the number of bits of the integer type (e.g., long and double in Java)

Hash Codes

Hashtables 8 of 20

Hash Codes (cont.)

  • Polynomial accumulation:
    • We partition the bits of the key into a sequence of components of fixed length (e.g., 8, 16 or 32 bits) a 0 a 1 … an − 1
    • We evaluate the polynomial p ( z ) = a 0 + a 1 z + a 2 z^2 + … … + an − 1 zn −^1 at a fixed value z , ignoring overflows
    • Especially suitable for strings (e.g., the choice z = 33 gives at most 6 collisions on a set of 50,000 English words) - Polynomial p ( z ) can be evaluated in O ( n ) time using Horner’s rule: - The following polynomials are successively computed, each from the previous one in O (1) time p 0 ( z ) = an − 1 pi ( z ) = an − i − 1 + zp (^) i − 1 ( z ) ( i = 1, 2, …, n −1) - We have p ( z ) = pn − 1 ( z )

Hashtables Calin Curescu, some reworking of Goodrich,Tamassia

9 of 20 TDDC32 lecture 5, 2006

  • Division:
    • h 2 ( y ) = y mod N
    • The size N of the hash table is usually chosen to be a prime
    • The reason has to do with number theory and is beyond the scope of this course - Multiply, Add and Divide (MAD): - h 2 ( y ) = ( ay + b ) mod N - a and b are nonnegative integers such that a mod N ≠ 0 - Otherwise, every integer would map to the same value b

Compression Functions

Hashtables 10 of 20

  • Collisions occur when

different elements are

mapped to the same cell

  • Separate Chaining: let each

cell in the table point to a

linked list of entries that map

there

• Average list length: λ

  • Expected probes per insertion

(or unsuccessful search): λ

  • Expected probes per successful

search: λ /2 + 1

  • Separate chaining is simple, but requires additional memory outside the table
  • Typical load factor: 1.
  • Each inspection of a cell or

list entry is referred as

probing

4 451-229-0004^ 981-101-

025-612-

Separate Chaining

Hashtables Calin Curescu, some reworking of Goodrich,Tamassia

13 of 20 TDDC32 lecture 5, 2006

Updates with Linear Probing

  • To handle insertions and deletions, we introduce a special object, called AVAILABLE , which replaces deleted elements
  • remove( k )
    • We search for an entry with key k
    • If such an entry ( k, o ) is found, we replace it with the special item AVAILABLE and we return element o
    • Else, we return null
      • put( k, o )
        • We throw an exception if the table is full
        • We start at cell h ( k )
        • We probe consecutive cells until one of the following occurs - A cell i is found that is either empty or stores AVAILABLE , or - N cells have been unsuccessfully probed
        • We store entry ( k, o ) in cell i

Hashtables 14 of 20

Quadratic Probing

  • Collisions are dealt by jumping j 2 spaces away from the cell pointed by h(k) - for j = 0, 1, … , N − 1 and j counts the collisions
  • If the table size N is a prime then a new element can always be inserted if table is half empty
  • Elements that has on the same positions build secondary clusters - Much less problematic than primary clusters

Hashtables Calin Curescu, some reworking of Goodrich,Tamassia

15 of 20 TDDC32 lecture 5, 2006

Double Hashing

  • Double hashing uses a secondary hash function d ( k ) and handles collisions by placing an item in the first available cell of the series ( h(k) + jd ( k )) mod N for j = 0, 1, … , N 1
  • The secondary hash function d ( k ) cannot have zero values
  • The table size N must be a prime to allow probing of all the cells
  • Need the same number of probes as purely random linear probing - Common choice of compression function for the secondary hash function: d 2 ( k ) = q − k mod q where - q < N - q is a prime - The possible values for d 2 ( k ) are 1, 2, … , q

Hashtables 16 of 20

  • Consider a hash table storing integer keys that handles collision with double hashing - N = 13 - h ( k ) = k mod 13 - d ( k ) = 7 − k mod 7
  • Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order

Example of Double Hashing

k h ( k ) d ( k ) Probes 18 5 3 5 41 2 1 2 22 9 6 9 44 5 5 5 10 59 7 4 7 32 6 3 6 31 5 4 5 9 0 73 8 4 8

Hashtables Calin Curescu, some reworking of Goodrich,Tamassia

19 of 20 TDDC32 lecture 5, 2006

Why still use Binary Trees?

  • When we need order information – O(log n)
    • findMax(), removeMax()
    • findMin(), removeMin()
    • findRange(x, 5)
      • Find 5 elements around value x
  • Above require O(n) in hash tables
  • For hash tables is recommended to approximatively know

the size of the input set or be able to perform rehashing

Hashtables 20 of 20

Reading

  • Course book, chapter 9.1 9.
  • Hash tables in compendium by Tommy Olsson