0% found this document useful (0 votes)
3 views24 pages

Hashing

The document provides an overview of hashing, a technique for efficient data storage and retrieval using hash functions. It covers various hash functions such as the Division Method, Multiplication Method, Mid Square Method, and Folding Method, along with collision resolution techniques like chaining and open addressing. The document emphasizes the benefits of hashing, including fast lookups and efficient storage, and explains how hash tables work to map keys to values for quick access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views24 pages

Hashing

The document provides an overview of hashing, a technique for efficient data storage and retrieval using hash functions. It covers various hash functions such as the Division Method, Multiplication Method, Mid Square Method, and Folding Method, along with collision resolution techniques like chaining and open addressing. The document emphasizes the benefits of hashing, including fast lookups and efficient storage, and explains how hash tables work to map keys to values for quick access.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 24

UNIT – IV

HASHING
Syllabus
1) Introduction to Hashing
2) Hash functions
a) Division Method
b) Multiplication Method
c) Mid Square Method
d) Folding Method
3) Collision resolution techniques
a) Chaining
b) Open addressing.
Hashing …?
Hashing is a technique used to store data in a way that
allows for efficient retrieval.
Hashing is a searching process that does not require
unnecessary comparison and directly find the location of the
key by using hash function.
Note: A hash is a fixed-size numerical or alphanumeric
value that is generated from input data (such as a string,
number, or any type of data) through a hash function.
Purpose:
It helps to quickly map data to a specific location in a hash
table using a hash function.
The transformation of a key to the corresponding value is
done using a Hash Function and the value obtained from the
hash function is called Hash Code.

Example: Imagine a list of names where we use a hash


function to quickly locate a name.
Why Use Hashing?
• Fast Lookups: Searching for data is quick (O(1) time
complexity in ideal cases).

• Efficient Storage: Reduces the need for searching


through entire data sets.

• Common Applications: Databases, caches,


dictionaries/maps.
How Hashing Works?
• Step 1: The data (like a string or number) is passed through a
hash function.
• Step 2: The function converts the data into a hash value (a
number).
• Step 3: The hash value is used as an index to place the data
into a hash table (an array).
• Step 4: To retrieve the data, we run the same hash function
and quickly find the data in the hash table.
What is hash Table?
• Hash table is just an array which maps a key (data) into the
data structure with the help of hash function such that
insertion, deletion and search operations are performed with
constant time complexity (i.e. O(1)).
What is Hash Function?
• A hash function is a mathematical function that converts
input (like a string or number) into a fixed-size numerical
value (called a hash).
• which is used to map a given value with a particular key for
faster access of elements.
• The result of the hash function is referred to as a hash value
or hash.
• The efficiency of mapping depends of the efficiency of the
hash function used.
Types of Hash Functions:
There are many hash functions that use numeric or
alphanumeric keys. Some of those are:
a) Division Method.
b) Multiplication Method
c) Mid Square Method
d) Folding Method.
a) Division Method
• The division method in hashing, also known as modular
hashing.
• This method divides the input number by a table size and takes
the remainder.
• Function: hash(key) = key % table_size
• If the key is 12 and the table size is 10, the hash is 12 % 10 =
2.
Find the index values for the list of elements [ 42, 10, 56, 37, 74 ] table size=
10. Map a key k into one of the m slots by taking the remainder of k divided
by m. That is, h(k) = k mod m.
Simple String Hashing (For words or text)
Here, you can add up the ASCII values of the letters in the word and use
that sum.
For "cat", ASCII values are:
'c' = 99 , 'a' = 97 , 't' = 116
Add them: 99 + 97 + 116 = 312
Then, you can take the remainder when dividing by the table size, say 5:
312 % 5 = 2
So, "cat" would be stored at position 2 in the table.
b) Multiplication Method
a) The Multiplication Method is another way to create a hash
function for storing data in a hash table.
b) It works by multiplying the input value (the key) by a constant and
then using a portion of the result to compute the hash code.
How the Multiplication Method works
1.Multiply the key by a constant A (where 0 < A < 1)
2.Take the fractional part of the result (everything after the
decimal)
3.Multiply that fractional part by the table size (the no of available
spots in the hash table)
4.Take the floor (round down) of the result to get the hash value
5.Formula:
h(k)=⌊m(kA mod 1)⌋
hash(key)=floor(table_size×(key×A%1))
c) Mid-Square Method
The Mid-Square Method is another technique used in hashing to
generate hash values for keys.
It is one of the oldest and simplest methods for generating hash
values.
The basic idea behind this method is to take a key, square it, and then
extract a portion of the resulting number to serve as the hash value
Steps for the Mid-Square Method:
1. Square the key k, i.e., k^2.
2. Take the middle part of the resulting number (a specified number of
digits).
3. Use the modulus operation to map the middle portion to the range of
the hash table size mmm.
d) Folding Method
• The Folding Method is a hashing technique where a key (usually an
integer) is split into smaller parts, and these parts are combined (or
"folded") together to generate a hash value and then taking the modulo
with respect to m.
• It is designed to improve the distribution of keys in the hash table and
reduce the chance of collisions by breaking the key into parts and then
combining those parts.
Steps for folding method:
Step 1: Divide the key into n parts, where each part has approximately
the same number of digits (or bits).

Step 2: Add these parts together (fold them).

table size 𝑚.
Step 3: Apply the modulus operation to ensure the result fits into the

You might also like