6. Example 1
• To store the key/values pair, we can use
simple array like data structure where
keys can be used as index to store
values.
Aamir sohail 6
7. Example 2
• We have 10 complaints indexed with a number
ranges from 0 to 9 ( say complaint number)
• We have an array of 10 spaces to store 10
complaints
• Here we can use complaint number as key and
it is nothing but index number in an array.
Aamir sohail 7
8. Example 3
• We have 5 students to store in table (nothing
but a custom array). Student will be assigned
a Roll no, so roll no is the key value to identify
a student.
• Assume that the table (array) has 10 slots
available.
• Also assume that a roll no is a 4 digit number
Aamir sohail 8
9. Terms
• Hash Table
- Mostly it is an array to store dataset.
• Hash Function
- a hash function is any function that can be
used to map dataset.
• Hashing
- in hashing large keys are converted in to
small ones by using hash function and then the
values are stored in hash table.
Aamir sohail 9
10. Hash Functions
• A hash function usually means a function
that compresses. Meaning the output is
shorter than the input.
• A hash function is any function that can
be used to map data of arbitrary size to
data of fixed size.
Aamir sohail 10
11. Parameters of good hash function
• Easy to Compute
• Minimize Collision
Aamir sohail 11
12. Perfect Hashing
• Perfect hashing maps each valid input to
a different hash value ( No Collision )
Aamir sohail 12
14. Hash Function
• A good hash function to use with
integer key values is the mid-square
method
• The mid-square method squares the key
value, and then takes out the middle r
bits of the result.
Aamir sohail 14
15. Collision
• A situation when the resultant hashes
for two or more data elements in data
set, maps to the same location in the
table, is called a hash collision.
• In such situation two or more data
elements would qualify to be
stored/mapped to the same location in
hash table.
Aamir sohail 15
16. Solution for Hash Collision
• Two types of solutions for hash collision
- Open Hashing (chaining)
- Closed Hashing (open Addressing)
• The difference between the two has to do
with
- whether collisions are stored outside the
table (open hashing), or
- whether collisions result in storing one
of the records at another slot in the table
(closed hashing).
Aamir sohail 16
17. Open Hashing
• The simplest form of open hashing
defines each slot in the hash table to be
the head of linked list.
• All records that hash to a particular
slot are placed on that slot’s linked list.
Aamir sohail 17