Hashing, Hashing Tables - PowerPoint PPT Presentation

About This Presentation
Title:

Hashing, Hashing Tables

Description:

Keys and Hash Functions. Each key is mapped into some number in the range ... An equal number of keys should map into each array position. Ease of Computation ... – PowerPoint PPT presentation

Number of Views:275
Avg rating:3.0/5.0
Slides: 30
Provided by: www2CsU
Category:
Tags: hashing | keys | tables

less

Transcript and Presenter's Notes

Title: Hashing, Hashing Tables


1
Hashing, Hashing Tables
  • Chapter 8

2
Class Hierarchy
3
Introduction
  • Definition
  • Key a key is a field or composite of fields that
    uniquely identifies an entry in a table.

4
Example
  • Table of students in a course sorted by name
  • --------------------------------------------------
    ------------
  • Name Year Mark
  • --------------------------------------------------
    ------------
  • Adams, Keith 3 94
  • Davis, Susan 1 75
  • Jordan, Ann 1 86
  • Patterson, Lynn 4 73
  • Williams, George 1 65

5
Insert Function of ListAsArray
6
Find Function of ListAsArray
7
Insert Function of ListAsLinkedList
8
Find Function of ListAsLinkedList
9
Insert Function of SortedListAsArray
10
Binary Search
11
Hashing
  • The implementation of hash tables is called
    Hashing.
  • Hashing is a technique used for performing
    insertions and finds in constant average time.
  • Efficient removal of items not required

12
The General Idea
  • Array of some fixed size, containing items.

13
Example
14
Keys and Hash Functions
  • Each key is mapped into some number in the range
    0 to TableSize-1 and placed in the appropriate
    cell.
  • The mapping is called a hash function

15
Keys and Hash Functions
  • Characteristics of a good hash function
  • Avoids collisions
  • Spread keys evenly in the array
  • Easy to compute

16
Avoid Collisions
  • Ideal situation
  • Given a set of nltM distinct keys k1,k2,,kn,
    the set of hash values h(k1),h(k2),,h(kn)
    contains no duplicates
  • We can only try to reduce the likelihood of a
    collision using knowledge about the keys
  • E.g. if we know the telephone numbers are all
    from the same district, so the district number
    will have little use in our hash function

17
Spreading Keys Evenly
  • We need to know the distribution of the keys
  • An equal number of keys should map into each
    array position

18
Ease of Computation
  • The running time of the hash function should be
    O(1) (Jumping immediately to the desired record
    is a direct access approach, much like direct
    access of data on a disk)

19
Hashing Methods
  • We are dealing with integer values first, KZ
  • The value of the hash function falls between 0
    and M-1

20
Division Method
  • The simplest method of hashing an integer
  • The division method of hashing
  • h(x) x mod M.

21
Choice of M
  • Generally, any M is good
  • we often choose M to be a prime number

22
Implementation
  • Unsigned int const M 1031 // a prime
  • Unsigned int h(unsigned int x)
  • return xM

23
Middle Square Method
  • Avoid division
  • Making use of the fact that computer does
    finite-precision integer arithmetic
  • All arithmetic is done modulo W, where W2w, w is
    the word size of the computer
  • M2k, W2w
  • Meaning
  • Multiply x by itself, then shift to the right k
    bits.

24
Implementation
  • unsigned int const k 10 // M1024
  • unsigned int const w bitsizeof (unsigned int)
  • unsigned int h (unsigned int x)
  • return (x x) gtgt (w - k)

25
Multiplication Method
  • We multiply the key by a

26
Implementation
  • unsigned int const k 10 // M1024
  • unsigned int const w bitsizeof (unsigned int)
  • unsigned int const a 2654435769U
  • unsigned int h (unsigned int x)
  • return (x a) gtgt (w - k)

27
Hash Tables
28
HashTable Class Definition
29
Separate Chaining
Write a Comment
User Comments (0)
About PowerShow.com