Collisions and Open Addressing
Load Factor
If a hash table has m slots and contains n elements, is its load factor.
When too many elements are stored and the load factor grows too large, lookup efficiency decreases. For open addressing, a practical maximum load factor is generally chosen within . When the load becomes too high, one solution is to double the table size. This process is called rehashing.
Rehashing traverses the original table and hashes every key into a new array. Why must the keys be hashed again? Increasing the length changes the hashing rule. After expansion, a crowded table becomes sparse again, and the existing keys are redistributed as uniformly as possible.
Load factor is also one of the factors influencing collisions. When different keys map to the same hash address, a collision occurs: . A collision-resolution strategy is therefore needed.
For example, consider the 12 objects {18, 23, 11, 20, 2, 7, 27, 30, 42, 15, 34, 35}. The table size is 17, and the hash function is h(key) = key % size.
From hashing to the first collision
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
No collisions occur before the final key, 35, is inserted. But h(35) = 1, and that position is already occupied, causing a collision.
Two common approaches resolve collisions:
-
Open addressing: after a collision, follow a rule to find another empty address. Common methods include linear probing, quadratic probing, and double hashing.
-
Separate chaining: store all keys that collide at the same position in a singly linked list.
KNOWLEDGE QUIZ
Linear Probing
After a collision, probe cyclically with offsets 1, 2, 3, ..., N - 1, where N is the table length.
For the sequence {47, 7, 29, 11, 9, 84, 54, 20, 30}, use a table of length 13, h(key) = key % 11, and linear probing to resolve collisions.
Linear probing: drag, calculate, and insert
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Linear probing
Linear probing has the drawback of forming clusters.
KNOWLEDGE QUIZ
Quadratic Probing
Quadratic probing uses the alternating offsets to probe cyclically for another address.
For the sequence {47, 7, 29, 11, 9, 84, 54, 20, 30}, use a table of length 11, h(key) = key % 11, and quadratic probing to resolve collisions.
Quadratic probing (±i²): drag, calculate, and insert
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Quadratic probing
Does quadratic probing always find an empty position whenever space remains?
For the following table, insert key 11 with h(key) = key % 5 and quadratic probing.
Quadratic probing: explore coverage by dragging
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Quadratic probing for key 11 keeps oscillating among indices 0 and 2 and never reaches the other empty positions.
A theorem shows that if the table length is a prime of the form , this quadratic-probing sequence can visit the entire table.
KNOWLEDGE QUIZ
KNOWLEDGE QUIZ
Double Hashing
Define another hash function and use the probe offsets .
The probe sequence should be able to visit every storage slot. The following form works well:
KNOWLEDGE QUIZ
Double hashing: drag, calculate, and insert
Unlock the complete interactive lab, deeper explanations, and curated reference answers.
Quadratic probing: work out a new sequence
Start with an empty table of length N = 7 and insert 12, 19, 26, 33, 40 in order. Use only quadratic probing: h(key) = key % 7, offsets 0, +1², −1², +2², −2², ….
- Show each initial address, every candidate, and the collision count. Apply offsets to the initial address; normalize modulo results to 0–6.
- Draw the table after each insertion and the final table, then calculate the load factor.