Hash Tables - PowerPoint PPT Presentation

About This Presentation
Title:

Hash Tables

Description:

Computer Science and Engineering. Chapter 8 of Goodrich and Tomassia's Text. 12/23/09 ... Maintain a set of 50 employee data for a 'temp agency' so that ... – PowerPoint PPT presentation

Number of Views:90
Avg rating:3.0/5.0
Slides: 21
Provided by: cseBu
Learn more at: https://cse.buffalo.edu
Category:

less

Transcript and Presenter's Notes

Title: Hash Tables


1
Hash Tables
  • Computer Science and Engineering
  • Chapter 8 of Goodrich and Tomassias Text

2
Topics
  • Hashing
  • Hash functions
  • Hash Tables
  • Collision
  • Collision resolution
  • Chaining, linear probing, quadratic probing,
    double hashing
  • Java Hash table
  • Example

3
Hashing Concept
  • Another approach for storing and searching
    elements.
  • Used in applications where add and delete,
    besides search, are used.
  • Worst case linear but can get O(1) best case.

4
Hash Function
  • A hash function h maps keys of a given type into
    an integer interval 0, N-1
  • A simple function
  • h(x) x mod N
  • A good hash function will uniformly disperse the
    keys in the range 0, N-1

5
Hash Table
  • Hash table ADT
  • Has a hash function h
  • Array of size N
  • Collision occurs when two keys map to the same
    array index.
  • Two major collision resolution schemes are
    chaining and open addressing

6
Example
  • Item(name, ssn) where ssn is 9 digit positive
    integer.
  • Hash table N 10000.
  • Hash function last four digits of of x
  • Use chaining to handle collision.

7
Hash Table Example
0 1 2 3
045-34-0002
. . . .
9996 9997 9998 9999
567-34-9996
045-35-9996
8
Hash Functions
  • Usually specified as two components
  • Component1 called the hash code map, collects the
    parts of the data and maps them to integers
    (numeric data)
  • H1 keys ? integers
  • Component2 called the compression map, takes the
    integers and maps them to 0 to N-1.
  • H2 integers? 0, N-1

9
Hash Code Maps
  • Memory address of data
  • Integer cast of non-numeric data bit/byte
    pattern of data
  • Sum of all components add the ascii values of
    your last name
  • Polynomials of various parts of data.

10
Compression maps
  • Reminder of division (mod)
  • h(y) y mod N
  • Multiple, Add and Divide (MAD)
  • h(y) (ay b) mod N where
  • a mod N ltgt 0 (otherwise everything will map to
    b!)

11
Linear Probing
  • Linear probing resolves collision by placing the
    colliding item in the next available empty cell.
  • Each entry inspected is referred to as the
    probe
  • Example h(x) x mod 13
  • Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in
    that order

12
Search with linear probing find(k)
  • j h(k) probe 0
  • while (p lt N)
  • 2.1 c Aj
  • 2.2 if (c null) return NOT_FOUND
  • else if (c.key() k)
  • return c.element()
  • else j (j1) mod N
  • p p 1
  • 3. return NOT_FOUND

13
Double Hashing
  • h1 primary hash function
  • If it results in collision, resolve by applying
    another hash function, secondary hash function
    here is an example of such a function.
  • d(k) q k mod q
  • Where q lt N,
  • Possible values of d(k) are 1,2,3,..q
  • It cannot be 0
  • When collision occurs (h(k)jd(k)) mod N
  • j 0, 1, ..N-1

14
Example
  • h(k) k mod 13
  • d(k) 7 k mod 7
  • Insert 18, 41, 22, 44, 59, 32, 31, 73.

15
Java support for Hashing
  • Java1.4 API
  • Hashtable since jdk1.0
  • HashMap since jdk1.2

16
Hashtable/HashMap
  • Array of Entry
  • Chaining is the collision resolution policy
  • Entry is an inner class Entry is a list
  • Entry holds a key, value and a link to next Entry
  • Key Object should implement hashcode() and
    equals() methods
  • hashcode() of the Key class is the hash code map
    that converts key attributes to an integer
  • Compression map/function is hashcode mod
    array.length
  • Loadfactor (how full it can get before
    expanding), initial capacity can be specified

17
Hashtable vs HashMap
AbstractMap
extends
Hashtable Map, Cloneable,
Serailizable
implements
18
Hashtable vs HashMap
  • Hashtable implements Dictionary which is
    deprecated and has been superceded by AbstractMap
  • Hashtable implementation is a synchronized for
    concurrent access. HashMap leaves synchronization
    to the application.
  • Hashtable is slower (due to synchronized
    implementation)

19
HashMap
20
Using HashMap
  • Problem An employee data is made up of Name
    (first, last), employee number (Integer), and
    salary. Maintain a set of 50 employee data for a
    temp agency so that employees can be added,
    removed and searched easily.
  • Solution Choice of data structure HashMap
  • Key? We will consider name as the key
  • Name is a class of two attributes first, last
Write a Comment
User Comments (0)
About PowerShow.com