DSA_M5
DSA_M5
https://www.geeksforgeeks.org/hashing-data-structure/
stores and retrieves data in a way that allows for quick access.
on average.
Introduction
Applications
Double Hashing
What is Hashing?
a Hash Function and the value obtained from the hash function
Now the question arises if Array was already there, what was
the need for a new data structure! The answer to this is in the
be small, but for a large data set, it can cause a lot of problems
So now we are looking for a data structure that can store the
time as well.
Components of Hashing
There are majorly three components of hashing:
in a data structure.
index .
index.
How does Hashing work?
strings can act as a key and the string itself will act as the value
key?
Step 1: We know that hash functions (which is some
o “a” = 1,
“ab” = 1 + 2 = 3,
“cd” = 3 + 4 = 7 ,
“efg” = 5 + 6 + 7 = 18
o “ab” in 3 mod 7 = 3,
o “cd” in 7 mod 7 = 0, and
o “efg” in 18 mod 7 = 4.
given string by using a simple hash function and rapidly find the
hashing seems like a great way to store (key, value) pairs of the
data in a table.
original.
For example: Consider an array as a Map where the key is the
index and the value is the value at that index. So for an array A
https://www.geeksforgeeks.org/hash-functions-and-list-
types-of-hash-functions/
1. Division Method.
2. Multiplication Method
3. Mid-Square Method
4. Folding Method
6. Universal Hashing
7. Perfect Hashing
1. Division Method
The division method involves dividing the key by a prime
h(k)=k mod m
Advantages:
Simple to implement.
Disadvantages:
2. Multiplication Method
h(k)=⌊m(kAmod1)⌋
Advantages:
Disadvantages:
Steps:
Advantages:
Disadvantages:
4. Folding Method
The folding method involves dividing the key into equal parts,
summing the parts, and then taking the modulo with respect to
𝑚m .
Steps:
Advantages:
Simple and easy to implement.
Disadvantages:
SHA-256.
Characteristics:
Pre-image resistance.
Collision resistance.
Advantages:
High security.
Disadvantages:
Computationally intensive.
6. Universal Hashing
h(k)=((a⋅k+b)modp)modm
Where a and b are randomly chosen constants, p is a prime
Advantages:
Disadvantages:
7. Perfect Hashing
Types:
Advantages:
No collisions.
Disadvantages:
Complex to construct.
Conclusion
store and find data quickly. Knowing the different types of hash
hash function for the job, developers can greatly improve the
A hash function that maps every item into its own unique slot is
hash function if we know the items and the collection will never
possibilities is large.
So, We can construct our hash function to do the same but the
hash function.
1. Efficiently computable.
function
function.
For example: {“ab”, “ba”} both have the same hash value, and
string {“cd”,”be”} also generate the same hash value, etc. This is
What is Collision?
handling technology.
1. Separate Chaining
2. Open Addressing
1) Separate Chaining
The idea is to make each cell of the hash table point to a linked
list of records that have the same hash function value. Chaining
problem:
2) Open Addressing
next location.
Algorithm:
and a sequence of keys that are to be inserted are 50, 70, 76,
85, 93.
2.b) Quadratic Probing
H+1 2 ,H +2 2 ,H +3 2 ,H +4 2 …………………. H + k 2
this method we look for i 2 ‘th probe (slot) in i’th iteration and
Let hash(x) be the slot index computed using the hash function
n.
n.
https://www.geeksforgeeks.org/introduction-to-hashing-2/
data.
string.
structures.
data structures
of possible keys.
Explore our latest online courses and learn new skills at your own pace. Enroll and become
a certified expert to boost your career.
Applications of Hashing
Password verification
Associating filename with their paths in operating systems
Data Structures, where a key-value pair is created in which the key is a unique value,
whereas the value associated with the keys can be either same or different for
different keys.
Board games such as Chess, tic-tac-toe, etc.
Graphics processing, where a large amount of data needs to be matched and fetched.
Read through this article to find out more about Hashing and specifically the
difference between two important hashing techniques − static
hashing and dynamic hashing.
Delete − Search a record address and delete a record at the same address or delete a
chunk of records from records for that address in memory.
Insertion − While entering a new record using static hashing, the hash function (h)
calculates bucket address "h(K)" for the search key (k), where the record is going to
be stored.
Search − A record can be obtained using a hash function by locating the address of
the bucket where the data is stored.
Update − It supports updating a record once it is traced in the data bucket.
Delete − Locate the desired location and support deleting data (or a chunk of data) at
that location.
Insertion − Support inserting new data into the data bucket if there is a space
available in the data bucket.
Query − Perform querying to compute the bucket address.
Update − Perform a query to update the data.
The location of the data in memory keeps changing according to the bucket size. Hence
if there is a phenomenal increase in data, then maintaining the bucket address table
becomes a challenge.
Here are some prominent differences by which Static Hashing is different than
Dynamic Hashing −
When a search key is specified in a static hash, the hashing algorithm always
returns the same address. For example, if you take the mod-4 hash function,
only 5 values will be produced. For this to work, the output address must
always be the same. The number of buckets at any given time is constant.
The data bucket address obtained by static hashing will always be the same. So,
if we use the mod(5) hash function to get the address of EmpId = 103, we always
get the same data bucket address 3. In this case, the data bucket position
remains unchanged. Therefore, all existing data buckets in memory remain
unchanged while the entire hashing process remains the same. In this case,
there are five partitions in the memory used to hold data.
Searching the data: When data is needed, the same hash function
is used to get the address of the packet where the data is stored.
Inserting Data: When new data is entered into the table, the hash
key is used to create the address of the new data and place the
data there.
from memory.
Updating a Record: To update the info, we will first find it using
the hash function and then change the profile info. If we want to
add data to the file, but the address of the dataset created by
There are many ways to solve bucket overflow problem. Here are
Open Hashing
after input is R4, the hash function will produce 112 as the
the system selects 113 as the next available packet and assigns it
R4.
Close Hashing
When the data group is full, a new bucket will be created for the
same hash result and added after the old group. This method is
called overflow chaining. For example, if the new address to be
added to the file is R4, the hash function will give it address 112.
But the bucket is too full to hold any more information. In this
case, a new bucket will be placed and tied to the end of the 112
bucket.
DBMS system.
hashing techniques.
Conclusion
show that data is not stored sequentially and also provides the
correct memory address for each value in the data. Unlike other
hashes, static hash methods can be used for static results without
the DBMS.
FAQs
A hash collision occurs when the hash values of two or more files
in the dataset are not assigned to the same location in the hash
table.
There are two methods that can be used to avoid hash collisions
whose keys have the same value. This method should have an
The Modulo Hash Function calculates the hash of key data and
https://quescol.com/data-structure/linear-probing
https://databasetown.com/types-of-hashing-in-dbms-static-
dynamic-hashing/
https://pediaa.com/what-is-the-difference-between-static-
and-dynamic-hashing/