Hashing
🔐 Hashing in Data Structures
✅ What is Hashing?
Hashing is a technique used to uniquely identify and access data using a key. It transforms an input (or key) into a fixed-size value called a hash code or hash value, typically for efficient searching, insertion, and deletion in hash tables.
🧮 Example Scenario
Suppose you want to store student records by roll number:
-
Traditional structures like arrays or linked lists might need linear search.
-
With hashing, you compute a hash index for each roll number and directly access it in O(1) average time.
🔑 What is a Hash Function?
A hash function is a function h(k)
that takes a key k
and returns an index in the hash table.
➕ Example:
Where:
-
k
= key -
m
= size of the hash table
Example:
For key 2025
and table size 10
:
h(2025) = 2025 % 10 = 5
→ Store the item at index 5
.
⭐ Desirable Properties of Hash Functions
-
Deterministic
-
Same input always produces the same output.
-
h(123) = 3
must always return3
.
-
-
Uniformity
-
Distributes keys evenly across the table to avoid clustering.
-
-
Fast Computation
-
Should be computationally simple and quick to evaluate.
-
-
Minimize Collisions
-
A collision occurs when two keys map to the same index.
-
Good hash functions minimize this risk.
-
-
Avalanche Effect (in cryptographic hashing)
-
A small change in input should produce a significantly different hash (not critical in basic DS applications, but important in cryptography).
-
🔄 Handling Collisions
Common methods to resolve hash collisions:
1. Chaining
-
Use a linked list at each index to store multiple keys.
2. Open Addressing
-
Find the next available slot by probing:
-
Linear Probing:
h(k) + i
-
Quadratic Probing:
h(k) + i²
-
Double Hashing: Use a second hash function.
-
📌 Summary Table
Term | Description |
---|---|
Hashing | Technique to map data to a fixed-size table |
Hash Function | Function to compute index from a key |
Collision | Two keys hash to the same index |
Chaining | Use linked lists to resolve collisions |
Open Addressing | Find alternate slots on collision |
Comments
Post a Comment