Skip to content
Home Β» Open Addressing

Open Addressing


πŸ“˜ Open Addressing (Collision Resolution Technique)

Open Addressing is a collision-handling technique used in hash tables in which all key-value pairs are stored directly inside the hash table (i.e., no linked lists, no external structure).

When a collision occurs (two keys hash to the same index), the hash table looks for another empty slot inside the table according to a probing sequence.


πŸ“Œ Key Idea

  • Store every element inside the table (size = m slots).
  • If the desired slot is occupied β†’ probe (search) to find the next empty slot.
  • Searching, inserting, deleting β†’ O(1) average, O(n) worst-case.

πŸ“˜ Core Formula

For a key k and a probing number i:

[
h(k, i) = (h(k) + f(i)) \mod m
]

Where:

  • h(k) = primary hash function
  • f(i) = probing function
  • i = number of collisions so far

πŸ“Œ Requirements of Open Addressing

  1. Load factor must be < 1
    [
    \alpha = \frac{n}{m} < 1
    ]
  2. Table must have at least one empty slot.
  3. Deletions require special handling (“lazy delete” markers).

πŸ“˜ Types of Open Addressing

There are three main types:


1. Linear Probing

[
h(k, i) = (h(k) + i) \mod m
]

Sequence:
h(k), h(k)+1, h(k)+2, …

βœ” Advantages

  • Simple
  • Cache-friendly (array locality)

✘ Disadvantages

  • Primary clustering
    • Long blocks of filled cells form
    • Increases search/insert time

2. Quadratic Probing

[
h(k, i) = (h(k) + i^2) \mod m
]

Sequence:
h(k), h(k)+1Β², h(k)+2Β², h(k)+3Β² …

βœ” Advantages

  • Reduces primary clustering
  • Better than linear probing

✘ Disadvantages

  • Secondary clustering
    • Keys with the same h(k) follow same probe path
  • Must choose m and coefficients carefully to ensure full table coverage

3. Double Hashing (Best Technique)

[
h(k, i) = (h_1(k) + i \cdot h_2(k)) \mod m
]

Where:

  • h1(k) is main hash
  • h2(k) is secondary hash
  • h2(k) β‰  0

βœ” Advantages

  • Almost eliminates clustering
  • Best distribution
  • Most efficient open-addressing technique

✘ Disadvantages

  • Slightly more complex
  • Requires good second hash function

πŸ“˜ Example of Probing (Linear Probing)

Table size = 7
Hash function: ( h(k) = k \mod 7 )

Insert keys: 10, 20, 30

  • 10 β†’ 10 % 7 = 3 β†’ slot 3
  • 20 β†’ 20 % 7 = 6 β†’ slot 6
  • 30 β†’ 30 % 7 = 2 β†’ slot 2
    Now insert 17:
  • 17 % 7 = 3 β†’ occupied
  • Try 4 β†’ empty
    β†’ Insert at slot 4

πŸ“˜ Deletion in Open Addressing

Removing directly will break probe chains.

Solution β†’ Lazy deletion

  • Mark cell as β€œdeleted”
  • Future searches continue probing
  • Insertions may reuse deleted slots

πŸ“˜ Performance of Open Addressing

MetricAverageWorst Case
SearchO(1)O(n)
InsertO(1)O(n)
DeleteO(1)O(n)

Performance depends on:

  • load factor (Ξ±)
  • Probing technique
  • Quality of hash function

To maintain O(1) efficiency:
[
\alpha \le 0.5 \text{ to } 0.7
]

When Ξ± becomes large β†’ rehashing needed.


πŸ“˜ Comparison of Probing Methods

MethodPrimary ClusteringSecondary ClusteringComplexity
Linear ProbingHigh ❌MediumHigh
Quadratic ProbingMediumHigh ❌Medium
Double HashingVery Low βœ”Very Low βœ”Best

Double Hashing is considered the best form of open addressing.


πŸ“˜ Advantages of Open Addressing

  • No extra memory for linked lists
  • Cache-friendly (array-based)
  • Good average-case performance
  • Simple structure
  • Used in many modern hash tables

πŸ“˜ Disadvantages

  • Performance drops sharply at high load factors
  • Deletion is complicated (requires lazy delete)
  • Cannot store more than m keys
  • Probing sequences can become long (clustering)

πŸ“˜ Summary (Exam Notes)

  • Open addressing stores all keys inside the hash table
  • Uses probing to find empty slots
  • Types:
    βœ” Linear Probing
    βœ” Quadratic Probing
    βœ” Double Hashing (best)
  • Needs load factor Ξ± < 1
  • Uses lazy deletion
  • Average O(1), worst O(n)
  • Preferred method: Double Hashing