0
0
Data Structures Theoryknowledge~6 mins

Collision handling (open addressing) in Data Structures Theory - Full Explanation

Choose your learning style9 modes available
Introduction
Imagine you have a box with many compartments to store your keys, but sometimes two keys want to go into the same compartment. This problem is called a collision. We need a way to find another empty compartment quickly without losing any keys.
Explanation
What is a collision?
A collision happens when two different items are assigned to the same spot in a storage system, like a hash table. Since each spot can hold only one item, we must find a way to handle this conflict.
A collision means two items want the same storage spot.
Open addressing method
Open addressing handles collisions by looking for another empty spot in the storage. Instead of using extra space, it searches through the storage itself to find a free compartment for the new item.
Open addressing finds a new spot inside the storage when a collision occurs.
Linear probing
Linear probing checks the next compartments one by one until it finds an empty one. If the first spot is taken, it tries the next, then the next, and so on, wrapping around to the start if needed.
Linear probing searches sequentially for the next free spot.
Quadratic probing
Quadratic probing jumps through compartments using a formula that squares the step number. This means it checks spots farther away in a pattern like 1, 4, 9 steps away, reducing clustering compared to linear probing.
Quadratic probing uses squared steps to spread out searches for free spots.
Double hashing
Double hashing uses a second formula to decide how far to jump when searching for an empty spot. This makes the search pattern less predictable and helps avoid clusters of filled spots.
Double hashing uses two formulas to find empty spots and reduce clustering.
Limitations of open addressing
Open addressing works well when the storage is not too full. If it gets crowded, finding empty spots takes longer, and performance drops. Also, deleting items can be tricky because empty spots might break the search chain.
Open addressing slows down when storage is nearly full and deletion is complex.
Real World Analogy

Imagine a parking lot where each car has a preferred parking spot. If that spot is taken, the driver looks for the next available spot nearby, either by checking spots one by one, jumping farther away in a pattern, or using a special rule to find a free space.

Collision → Two cars arriving at the same parking spot at the same time
Open addressing → Driver searching for another empty parking spot within the lot
Linear probing → Driver checking each parking spot one by one until finding an empty one
Quadratic probing → Driver skipping spots in a pattern like 1, 4, 9 spots away to find a free space
Double hashing → Driver using a second rule to decide how far to jump to find an empty spot
Limitations of open addressing → Parking lot getting full, making it harder and slower to find empty spots
Diagram
Diagram
┌───────────────┐
│ Hash Table    │
├───────────────┤
│ [0] Item A    │
│ [1] Item B    │
│ [2] Collision │
│ [3] Empty     │
│ [4] Empty     │
│ [5] Empty     │
└───────────────┘

Collision at [2] → check next spots:
[3] empty → place item here (linear probing)

Search pattern:
[2] → [3] → [4] → [5] ...
This diagram shows a hash table with a collision at position 2 and how linear probing finds the next empty spot at position 3.
Key Facts
CollisionWhen two items are assigned to the same storage location.
Open addressingA collision handling method that searches for empty spots within the storage.
Linear probingA technique that checks the next slots one by one to resolve collisions.
Quadratic probingA technique that checks slots at increasing squared distances to reduce clustering.
Double hashingA technique that uses a second hash function to determine jump steps for collision resolution.
Code Example
Data Structures Theory
class OpenAddressingHashTable:
    def __init__(self, size):
        self.size = size
        self.table = [None] * size

    def hash(self, key):
        return key % self.size

    def linear_probe(self, key):
        index = self.hash(key)
        start_index = index
        while self.table[index] is not None:
            index = (index + 1) % self.size
            if index == start_index:
                raise Exception('Hash table is full')
        return index

    def insert(self, key):
        index = self.hash(key)
        if self.table[index] is None:
            self.table[index] = key
        else:
            new_index = self.linear_probe(key)
            self.table[new_index] = key

    def __str__(self):
        return str(self.table)

# Example usage
ht = OpenAddressingHashTable(5)
ht.insert(1)
ht.insert(6)  # Collision with 1, linear probe to next spot
ht.insert(11) # Collision again, linear probe further
print(ht)
OutputSuccess
Common Confusions
Believing open addressing uses extra storage space to handle collisions.
Believing open addressing uses extra storage space to handle collisions. Open addressing resolves collisions by searching within the existing storage, not by using extra space like separate chaining.
Thinking linear probing always finds an empty spot quickly regardless of table fullness.
Thinking linear probing always finds an empty spot quickly regardless of table fullness. Linear probing slows down significantly as the table gets full because it may need to check many spots sequentially.
Assuming deletion in open addressing is as simple as clearing a spot.
Assuming deletion in open addressing is as simple as clearing a spot. Deleting items requires special handling to avoid breaking the search chain, often using markers instead of clearing spots.
Summary
Collisions happen when two items want the same storage spot, and open addressing solves this by searching for empty spots inside the storage.
Linear probing checks spots one by one, quadratic probing uses squared steps, and double hashing uses a second formula to find free spots.
Open addressing works best when the storage is not too full, and deleting items requires careful handling to keep searches working.