Hashing Continued and Exam 3 - PowerPoint PPT Presentation

About This Presentation
Title:

Hashing Continued and Exam 3

Description:

... any empty space (including one created by a deletion), we will stop searching ... at the hashed position and find a marked deletion, we know to continue searching ... – PowerPoint PPT presentation

Number of Views:31
Avg rating:3.0/5.0
Slides: 12
Provided by: bobwi9
Learn more at: https://www.cs.umb.edu
Category:

less

Transcript and Presenter's Notes

Title: Hashing Continued and Exam 3


1
Hashing (Continued) and Exam 3
  • Hashing (Continued)
  • Deleting elements
  • Dynamic resizing
  • Hashing in the Java Collections API
  • Hashtable Applications
  • Review for Exam 3
  • Open Book / Open Notes

2
Deleting Chained Implementation
  • If we store a reference to another collection in
    each hash table entry, it is easy use that
    collections remove method to delete an element
    in a chained hash implementation
  • Otherwise, we must manipulate references
    appropriately to unlink the object being removed
    and maintain the chain based at the original hash
    table entry

3
Resolving Collisions
  • Open Addressing looks for another open position
    in the table other than the one to which the
    element is originally hashed
  • Three variations of open addressing
  • Linear probing
  • Quadratic probing
  • Double hashing

4
Open Addressing Linear Probing
  • Open Addressing with linear probing means that if
    an element hashes to position p and position p is
    already occupied, we simply try
  • position p (p 1) tableSize
  • If that element is also occupied, we keep adding
    1 modulo table size until we find
  • An empty element and we use it OR
  • We are back at the original position p and the
    table is full (expanding capacity is an option)
  • Problem Tends to create dense occupied clusters
    which affects the efficiency of adds and searches

5
Open Addressing Quadratic Probing
  • Open Addressing with quadratic probing means that
    if an element hashes to position p and position p
    is already occupied, we use a formula such as
  • new hash original hash (-1)i-1((i1)/2)2
    (for i 1, 2, )
  • We must divide the new hash modulo tableSize
  • This tries a sequence of positions such as
  • p, p1, p-1, p4, p-4, p9, p-9,
  • If we get back to the original position, table is
    full (expanding capacity is still an option)
  • This does not have as strong a tendency to create
    dense clusters as linear probing

6
Open Addressing Double Hashing
  • Open Addressing with double hashing means that if
    an element hashes to position p and position p is
    already occupied, we use a formula such as
  • new hash(x) original hash(x) secondary
    hash(x)
  • We must divide the new hash modulo tableSize
  • This tries a sequence of positions that depends
    on the definition of the secondary hash function
  • If we get back to the original position, table is
    full (expanding capacity is still an option)
  • It is somewhat more costly to compute a second
    hash function, but this tends to further reduce
    the density of clustering over quadratic probing

7
Deleting Open Addressing
  • If we actually delete any element from an open
    addressing implementation, we may make it
    impossible to find another entry
  • If we start a search at the hashed position and
    find any empty space (including one created by a
    deletion), we will stop searching
  • Therefore, we remove an element by marking it as
    deleted without actually removing it
  • Then, if we start a search at the hashed position
    and find a marked deletion, we know to continue
    searching
  • We can reuse deleted elements when possible or
    rehash the table to recover wasted space

8
Dynamic Table Resizing
  • To dynamically expand the capacity of the hash
    table, we create a new larger array
  • However, we note that the size of the table is
    used to select locations in the array
  • We cant just copy the smaller array into a
    sub-array area within the new larger array
  • We must take all elements from the smaller array
    and rehash them into the larger array

9
Java Collections API Hash Tables
  • There are seven implementations of hashing
  • Hashtable
  • HashMap
  • HashSet
  • IdentityHashMap
  • LinkedHashSet
  • LinkedHashMap
  • WeakHashMap

10
Hash Table Applications
  • Compilers use a hashtable called a symbol table
    for the variable names in source code
  • Game programs use a hashtable called a
    transposition table for positions previously
    encountered to remember the move made in that
    position
  • An On-line spell checker can pre-hash its
    dictionary of valid words
  • In each case, an O(1) key lookup is achieved

11
Review for Exam 3
  • Practice Exam is on-line
Write a Comment
User Comments (0)
About PowerShow.com