Lecture 12. Hashing
Lecture 12. Hashing
CT065-3-3
Hashing
Level 3 Computing (Software Engineering)
Prepared by: Tan Choon Ling First Prepared on: 09-10-06 Last Modified on: 09-10-06
Quality checked by:
Copyright 2006 Asia Pacific Institute of Information Technology
Topic & Structure of Lesson
Hashing
Hashing Strategies
Truncation
Folding
Modular Arithmetic
Collisions
Open Addressing using Linear Probing
Chaining
Module Code and Module Title Title of Slides Slide 2 (of 22)
Learning Outcomes
Module Code and Module Title Title of Slides Slide 3 (of 22)
Searching
Module Code and Module Title Title of Slides Slide 4 (of 22)
Hashing
Module Code and Module Title Title of Slides Slide 6 (of 22)
Hashing
Terminology
h is a hash function
k hashes to slot h(k)
the hash value of k is h(k)
Module Code and Module Title Title of Slides Slide 7 (of 22)
Hashing Properties
Module Code and Module Title Title of Slides Slide 8 (of 22)
Hashing Properties
Module Code and Module Title Title of Slides Slide 9 (of 22)
Hashing Strategies
Truncation
Ignore part of the key and use the remaining
part directly as the index
eg.: if the keys are 8-digit numbers and the
hash table has 1000 entries, then the first,
fourth and eighth digit could make the hash
function
21296876 maps to 296
Module Code and Module Title Title of Slides Slide 10 (of 22)
Hashing Strategies
Folding
Break up the key in parts and combine them
in some way
eg.: if the keys are 8-digit numbers and the
hash table has 1000 entries, break up a key
into three, three and two digits, add them up
and, if necessary, truncate them
21296876 maps to 212 + 968 + 76 = 1256
and then mod to 256
Module Code and Module Title Title of Slides Slide 11 (of 22)
Hashing Strategies
Modular Arithmetic
Convert the key to an integer, and then mod
that integer by the size of the table
eg.: 21296876 maps to 876
Module Code and Module Title Title of Slides Slide 12 (of 22)
Collisions
Module Code and Module Title Title of Slides Slide 13 (of 22)
Handling Collisions
Open addressing
Idea:
Store all elements in the hash table itself.
If a collision occurs, find another slot. (How?)
When searching for an element examine slots until
the element is found or it is clear that it is not in the
table.
The sequence of slots to be examined (probed) is
computed in a systematic way.
It is possible to fill up the table so that you cant insert
any more elements.
idea: extendible hash tables?
Module Code and Module Title Title of Slides Slide 14 (of 22)
Handling Collisions
Open addressing
Probing must be done in a systematic way
(why?)
There are several ways to determine a
probe sequence:
linear probing
quadratic probing
double hashing
random probing
Module Code and Module Title Title of Slides Slide 15 (of 22)
Handling Collisions
Level K +2 Vacant
....... ..
Table Index
Module Code and Module Title Title of Slides Slide 16 (of 22)
Handling Collisions
Module Code and Module Title Title of Slides Slide 17 (of 22)
Handling Collisions
Module Code and Module Title Title of Slides Slide 18 (of 22)
Resolving Collisions
Module Code and Module Title Title of Slides Slide 19 (of 22)
Resolving Collisions
Chaining
Insert : O(1)
worst case
Delete : O(1)
worst case
assuming doubly-linked list
its O(1) after the element has been found
Search : ?
depends on length of chain.
Module Code and Module Title Title of Slides Slide 20 (of 22)
Summary
Hashing
Hashing Strategies
Truncation
Folding
Modular Arithmetic
Collisions
Open Addressing using Linear Probing
Chaining
Module Code and Module Title Title of Slides Slide 21 (of 22)
Next Lesson
Module Code and Module Title Title of Slides Slide 22 (of 22)