Hashing Notes

Download as pdf or txt
Download as pdf or txt
You are on page 1of 5

Introduction to

Hashing: Introduction, Collision Resolution


Techniques, and Applications
Introduction to Hashing:
1. Hash Functions:
o A hash function is a function that converts
input data (key) into a fixed-size numerical
value (hash code) which typically serves as
an index in a hash table. The main goal is
to distribute the keys uniformly across the
table to minimize collisions.
o Properties of Good Hash Functions:
▪ Uniform Distribution: The hash
function should distribute keys
uniformly across the hash table to
minimize clustering and ensure even
utilization of the table.
▪ Deterministic: The hash function must
produce the same hash code for the
same key every time it is hashed.
▪ Efficient: The function should be
computationally efficient to avoid
significant performance overhead.
▪ Minimize Collisions: While collisions
are inevitable, a good hash function
aims to minimize their occurrence and
impact.
Collision Resolution Techniques:
1. Open Addressing:
o When a collision occurs (i.e., the hash
code maps to an already occupied slot),
open addressing finds another slot within
the hash table to place the key. This is
done using probing techniques.
o Probing Techniques:
▪ Linear Probing: Check the next slot in
a sequential manner (i.e., (hash + i) %
table_size, where i is the probe
number).
▪ Quadratic Probing: Use a quadratic
function to determine the next slot
(i.e., (hash + i^2) % table_size).
▪ Double Hashing: Apply a second hash
function to determine the interval
between probes (i.e., (hash1 + i *
hash2) % table_size).
o Deletion in Open Addressing: Deleting a
key from an open-addressed hash table
requires careful handling to avoid
breaking the probing sequence. Deleted
slots are often marked as "deleted" rather
than empty.
2. Chaining:
o Each slot in the hash table points to a
linked list of entries that hash to the same
index. This method handles collisions by
maintaining a list of all entries that map to
the same hash code.
o Insertion: Add new keys to the linked list
at the appropriate slot.
o Search: Traverse the linked list at the slot
to find the key.
o Deletion: Remove the key from the linked
list. If the list becomes empty, the slot can
be reset.
Applications:
1. Implementing Dictionaries:
o Hashing is commonly used to implement
dictionaries (also known as associative
arrays or hash maps) where keys are
mapped to values. The hash table
provides average-case constant-time
complexity for insertions, deletions, and
lookups, making it efficient for these
operations.
2. Databases:
o Hashing is used in databases for indexing,
which helps to speed up query processing
by allowing quick lookups of records
based on key values. Hash-based indexing
improves the performance of equality
queries and can be combined with other
indexing methods for range queries.

You might also like