2080 1

CSC211 ยท 2080 ยท 10 marks

Define hash table and hash function. What is collision in hashing? Explain linear probing and quadratic probing with suitable example.

Define hash table and hash function. What is collision in hashing? Explain linear probing and quadratic probing with suitable example.[10]

Data Structures and AlgorithmsChapter 7: Searching and HashingHashing

Quick answer

A hash table is a data structure that stores data in an array format, where each data item is placed at a specific position (index) calculated based on the value of the key. The data is stored in an array called a hash table, and the mapping of keys to indices is done so that equal keys map to equal indices. Hash tables are useful for performing searching, insertion, and deletion at a faster rate.

Hash Table, Hash Function, Collision, Linear Probing and Quadratic Probing


1. Hash Table


2. Hash Function

A hash function is a function that takes a key as input and computes and returns the position (index) of the record in the hash table, instead of performing comparisons. It maps keys to indices of the hash table.

Common hash functions include:

  • Division Method: h(key) = key % table_size
  • Mid-Square Method: Square the key and take the middle digits as the index.
  • Digit Folding: Divide the key into parts and combine them using simple operations.

Example (Division Method): For keys {52, 68, 99, 84} with table size 10:

h(52) = 52 % 10 = 2
h(68) = 68 % 10 = 8
h(99) = 99 % 10 = 9
h(84) = 84 % 10 = 4

3. Collision in Hashing

A collision is a situation that occurs when two distinct keys produce the same hash value, i.e., two different data items are mapped to the same index in the hash table.

Collisions are unavoidable whenever members of a very large set are mapped to a relatively short bit string (Pigeonhole Principle).

Example:

h(89) = 89 % 10 = 9
h(49) = 49 % 10 = 9   <-- Collision! Both map to index 9

Collision Resolution Methods:

  • Open Addressing (Linear Probing, Quadratic Probing, Double Hashing)
  • Chaining
  • Rehashing
  • Hashing using Buckets

4. Linear Probing

Linear probing is an open addressing collision resolution technique in which, when a collision occurs, the algorithm searches for the next empty (vacant) slot sequentially in the array until a free location is found.

Formula:

h(x, i) = (h(x) + i) % table_size

where i = 1, 2, 3, ... is the probe number.

Disadvantage: Linear probing suffers from primary clustering, where a cluster of occupied slots forms, slowing down future insertions and searches.


Example of Linear Probing

Insert keys {89, 49, 18, 58} into a hash table of size 10 using h(x) = x % 10.

Step 1: Insert 89

h(89) = 89 % 10 = 9
Index 9 is empty --> Place 89 at index 9

Step 2: Insert 49

h(49) = 49 % 10 = 9
Index 9 is OCCUPIED (collision!)
Try index (9+1) % 10 = 0 --> Index 0 is empty
Place 49 at index 0

Step 3: Insert 18

h(18) = 18 % 10 = 8
Index 8 is empty --> Place 18 at index 8

Step 4: Insert 58

h(58) = 58 % 10 = 8
Index 8 is OCCUPIED (collision!)
Try index (8+1) % 10 = 9 --> Index 9 is OCCUPIED
Try index (8+2) % 10 = 0 --> Index 0 is OCCUPIED
Try index (8+3) % 10 = 1 --> Index 1 is empty
Place 58 at index 1

Final Hash Table:

Index0123456789
Key49581889

5. Quadratic Probing

Quadratic probing is an open addressing collision resolution technique that eliminates the primary clustering problem of linear probing. Instead of probing linearly, it uses a quadratic function to find the next available slot.

Formula:

h(x, j) = (hash_value + j^2) % table_size

where j = 1, 2, 3, ... is the probe number.

  • 1st collision: (hash_value + 1^2) % table_size
  • 2nd collision: (hash_value + 2^2) % table_size
  • 3rd collision: (hash_value + 3^2) % table_size
  • and so on...

Advantage: Reduces primary clustering compared to linear probing.


Example of Quadratic Probing

Insert keys {89, 49, 58, 79} into a hash table of size 10 using h(x) = x % 10.

Step 1: Insert 89

h(89) = 89 % 10 = 9
Index 9 is empty --> Place 89 at index 9

Step 2: Insert 49

h(49) = 49 % 10 = 9
Index 9 is OCCUPIED (collision!)
Probe 1: (9 + 1^2) % 10 = (9 + 1) % 10 = 0 --> Index 0 is empty
Place 49 at index 0

Step 3: Insert 58

h(58) = 58 % 10 = 8
Index 8 is empty --> Place 58 at index 8

Step 4: Insert 79

h(79) = 79 % 10 = 9
Index 9 is OCCUPIED (collision!)
Probe 1: (9 + 1^2) % 10 = 0  --> Index 0 is OCCUPIED
Probe 2: (9 + 2^2) % 10 = 3  --> Index 3 is empty
Place 79 at index 3

Final Hash Table:

Index0123456789
Key49795889

Notice the difference from linear probing: 79 jumps four slots away to index 3 instead of settling next to the cluster at 0 and 1, so the occupied slots stay spread out.


6. Linear Probing Compared with Quadratic Probing

FeatureLinear ProbingQuadratic Probing
Probe sequence(h(x) + i) % m(h(x) + i^2) % m
ClusteringSuffers from primary clusteringRemoves primary clustering, but secondary clustering remains for keys with the same home slot
Slot coverageEvery slot is eventually examinedOnly about half the slots are guaranteed to be reached, and only when the table size is prime and the load factor stays below 0.5
Cache behaviourGood, probes are adjacentPoorer, probes jump around the table
ImplementationSimplestSlightly more arithmetic per probe

7. Conclusion

A hash table stores records at positions computed by a hash function rather than found by comparison, which is what gives it average constant time insertion, deletion and search. Because a hash function maps a large key space onto a small index range, collisions cannot be avoided, so a resolution strategy is part of every practical hash table. Linear probing resolves a collision by walking to the next free slot, which is simple but builds long runs of occupied slots, while quadratic probing spreads the probes out as squares of the probe number and so breaks up primary clustering. Both remain efficient only while the table is kept well below full, which is why the load factor is watched and the table is rehashed into a larger prime sized array when it grows too high.

Short on time? The 1, 5 and 10 mark versions of this answer, flashcards and practice sets come with a program pass.