Hashing
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.
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 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.
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
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