Hash Tables - PowerPoint PPT Presentation

About This Presentation
Title:

Hash Tables

Description:

Collision occurs when two keys map to the same array index. ... Example: h(x) = x mod 13. Insert keys: 18, 41, 22, 44, 59, 32, 31, 73, in that order ... – PowerPoint PPT presentation

Number of Views:33
Avg rating:3.0/5.0
Slides: 15
Provided by: cseBu
Learn more at: https://cse.buffalo.edu
Category:
Tags: hash | keys | tables

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.
Write a Comment
User Comments (0)
About PowerShow.com