0% found this document useful (0 votes)
7 views4 pages

Hashing

Hashing is a technique in data structures that uses a hash function to efficiently store and retrieve data by mapping it to a hash table. It involves handling hash collisions through methods like open addressing and closed addressing, with various techniques such as linear and quadratic probing. Hash tables are widely used in applications like databases, caching, and network routing due to their fast search and retrieval capabilities.

Uploaded by

naragola82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
7 views4 pages

Hashing

Hashing is a technique in data structures that uses a hash function to efficiently store and retrieve data by mapping it to a hash table. It involves handling hash collisions through methods like open addressing and closed addressing, with various techniques such as linear and quadratic probing. Hash tables are widely used in applications like databases, caching, and network routing due to their fast search and retrieval capabilities.

Uploaded by

naragola82
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Hashing

Hashing is a fundamental data structure that efficiently stores and retrieves data in a way that
allows for quick access.

It involves mapping data to a specific index in a hash table using a hash function

This method is commonly used in databases, caching systems, and various programming
applications to optimize search and retrieval operations.

What is Hashing in Data Structure?


Hashing is a technique used in data structures to store and retrieve data efficiently.
It involves using a hash function to map data items to a fixed-size array which is called a hash
table.

How it works:
1.Hash Function: You provide your data items into the hash function.
2.Hash Code: The hash function crunches the data and give a unique hash code.
3.Hash Table: The hash code then points you to a specific location within the hash table.

Hash Table in Data Structure


1. A hash table is also known as a hash map.
2. It is a data structure that stores key-value pairs.
3. It uses a hash function to map keys to a fixed-size array, called a hash table.
4. This allows in faster search, insertion, and deletion operations.

Hash Function
The hash function is a function that takes a key and returns an index into the hash table. The
goal of a hash function is to distribute keys evenly across the hash table, minimizing collisions
(when two keys map to the same index).
Common hash functions include:
 Division Method: Key % Hash Table Size
 Multiplication Method: (Key * Constant) % Hash Table Size
 Universal Hashing: A family of hash functions designed to minimize collisions
What is a Hash Collision?
A hash collision occurs when two different keys map to the same index in a hash table.

Causes of Hash Collisions:


 Poor Hash Function: A hash function that does not distribute keys evenly across the hash
table can lead to more collisions.
 High Load Factor: A high load factor (ratio of keys to hash table size) increases the
probability of collisions.
 Similar Keys: Keys that are similar in value or structure are more likely to collide.

Collision Resolution Techniques


There are two types of collision resolution techniques:
1. Open Addressing:
 Linear Probing: Search for an empty slot sequentially
 Quadratic Probing: Search for an empty slot using a quadratic function

Open Addressing / Closed hashing:


Open addressing is a method for handling collisions. In Open Addressing, all elements are stored in
the hash table itself. So at any point, the size of the table must be greater than or equal to the total number
of keys.

Different ways of Open Addressing:


1. Linear Probing:
In linear probing, the hash table is searched sequentially that starts from the original location of the hash. If
in case the location that we get is already occupied, then we check for the next location.
The function used for rehashing is as follows: rehash(key) = (n+1) % table-size.
For example, The typical gap between two probes is 1 as seen in the example below:
Let hash(x) be the slot index computed using a hash function and S be the table size
If slot hash(x) % S is full, then we try (hash(x) + 1) % S
If (hash(x) + 1) % S is also full, then we try (hash(x) + 2) % S
If (hash(x) + 2) % S is also full, then we try (hash(x) + 3) % S
…………………………………………..
…………………………………………..
Let us consider a simple hash function as “key mod 7” and a sequence of keys as
50, 700, 76, 85, 92, 73, 101,
which means hash(key)= key% S, here S=size of the table =7,indexed from 0 to 6.We can define the hash
function as per our choice if we want to create a hash table,although it is fixed internally with a pre-defined
formula.

 In linear probing, the algorithm simply looks for the next available slot in the hash table and places the
collided key there.
If that slot is also occupied, the algorithm continues searching for the next available slot until an empty
slot is found.
This process is repeated until all collided keys have been stored.
Linear probing has the best cache performance but suffers from clustering.
One more advantage of Linear probing is easy to compute.
 In quadratic probing, the algorithm searches for slots in a more spaced-out manner.
When a collision occurs, the algorithm looks for the next slot using an equation that involves the original
hash value and a quadratic function.
If that slot is also occupied, the algorithm increments the value of the quadratic function and tries again.
This process is repeated until an empty slot is found.
Quadratic probing lies between the two in terms of cache performance and clustering.
2. Closed Addressing:
 Chaining: Store colliding keys in a linked list or binary search tree at each index

Closed Addressing / Open Hashing


S.No. ( Separate Chaining ) Open Addressing / Closed Hashing
1 Chaining is Simpler to implement. Open Addressing requires more computation.
2 In chaining, Hash table never fills up In open addressing, table may become full.
Chaining is Less sensitive to the hash Open addressing requires more sensitive to avoid
3 function clustering
Chaining is mostly used when frequency Open addressing is used when the frequency and
4 and no.of keys is unknown. number of keys is known.
Cache performance of chaining is not good Cache performance is good as everything is stored
5 as keys are stored using linked list. in the same table.
6 Wastage of Space No space wastage
7 Chaining uses extra space for links. No links in Open addressing

For More Info : www.geeksforgeeks.ord : data structures

Applications of Hashing
Hash tables are used in a wide variety of applications, including:
 Databases: Storing and retrieving data based on unique keys
 Caching: Storing frequently accessed data for faster retrieval
 Symbol Tables: Mapping identifiers to their values in programming languages
 Network Routing: Determining the best path for data packets

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy