Data Structures and Algorithms: (CS210/ESO207/ESO211)
Data Structures and Algorithms: (CS210/ESO207/ESO211)
(CS210/ESO207/ESO211)
Lecture 38
Hashing (Data structure for searching)
Recursion
1
Data structures for searching
in O(1) time
Problem Description
: {0,1, , } called universe
,
= ,
Aim: A data structure for a given set that can facilitate efficient searching.
A search query: Does ?
Note: can be any element from .
Hash function, hash value, hash table
Hash function:
is a mapping from to {0,1, , } with the
following characteristics.
() is computable in O(1) time in word RAM.
Example: () = mod
Hash value:
() is called hash value of for a given hash
function , and .
Hash Table:
An array [0 ] of pointers storing .
0
1
2
.
.
.
.
.
.
.
.
.
.
.
0
1
.
.
Hash function, hash value, hash table
Hash function:
is a mapping from to {0,1, , } with the
following characteristics.
() is computable in O(1) time in word RAM.
Example: () = mod
Hash value:
() is called hash value of for a given hash
function , and .
Hash Table:
An array [0 ] of pointers storing .
0
1
2
.
.
.
.
.
.
.
.
.
.
.
0
1
.
.
.
.
.
Hash function, hash value, hash table
Question:
How to use (,) for searching an element ?
Answer:
();
Search element in the list [].
Time complexity for searching:
O(length of the longest list in ).
0
1
2
.
.
.
.
.
.
.
.
.
.
.
0
1
.
.
.
.
.
Efficiency of Hashing depends upon hash function
A hash function is good if it can evenly distributes .
Aim: To search for a good hash function for a given set .
Bad news: There can not be any hash function which is good for every .
Pigeon hole principle:
If there are letters to be placed in less than pigeon holes, there must be at least one
hole containing more than 1 letters.
(very simple and yet very powerful principle)
Hash function, hash value, hash table
For every , there exists a subset of