2081 8

CSC211 ยท 2081 ยท 5 marks

What is hashing? how do you apply linear probing and rehashing explain with example.

What is hashing? how do you apply linear probing and rehashing explain with example. [5]

Data Structures and AlgorithmsChapter 7: Searching and HashingHashing

Quick answer

Hashing is a technique used to map data (keys) to specific locations (indices) in a hash table using a hash function. The hash function converts a key into an index within the range of the table size.

Hashing, Linear Probing, and Rehashing

What is Hashing?

General form:

h(x) = x % table_size

The main advantage of hashing is that it allows O(1) average time for insertion, deletion, and search operations.


Hash Collision

A collision occurs when two distinct keys produce the same hash value (i.e., map to the same index). Collision resolution is necessary for correct operation.


Linear Probing

Linear probing is an open addressing collision resolution technique. When a collision occurs at index h(x), the algorithm searches sequentially for the next empty slot in the table.

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 -- a group of consecutive occupied slots forms, slowing down future insertions.


Example of Linear Probing

Insert keys: {89, 49, 18, 58} into a hash table of size 10.

Hash function: h(x) = x % 10

StepKeyh(x) = x%10Action
18989%10 = 9Index 9 is empty, insert at 9
24949%10 = 9Collision! Index 9 is full. Try (9+1)%10 = 0 -- empty, insert at 0
31818%10 = 8Index 8 is empty, insert at 8
45858%10 = 8Collision! Index 8 is full. Try (8+1)%10 = 9 -- full. Try (8+2)%10 = 0 -- full. Try (8+3)%10 = 1 -- empty, insert at 1

Final Hash Table:

Index0123456789
Key4958------1889

Rehashing

Rehashing is a collision resolution technique where, upon collision, a new hash function is applied to the key to find another location.

Formula:

h_i(x) = (h(x) + i * h'(x)) % table_size

Or simply, a second independent hash function is used:

h2(x) = R - (x % R)

where R is a prime number smaller than the table size.

Rehashing is also performed when the load factor (ratio of filled slots to table size) becomes too high -- in that case, the table size is doubled and all keys are re-inserted using the new hash function.

Steps in Rehashing:

  1. Apply the primary hash function h(x).
  2. If collision occurs, apply a secondary hash function h2(x).
  3. Probe at positions: (h(x) + i * h2(x)) % table_size for i = 1, 2, 3, ...

Example of Rehashing (Double Hashing)

Insert keys: {89, 49}, table size = 10, R = 7

  • h(x) = x % 10
  • h2(x) = 7 - (x % 7)
Keyh(x)Collision?h2(x)New index
899No--Insert at 9
499Yes7-(49%7) = 7-0 = 7(9 + 1*7)%10 = 6, Insert at 6

Final Table:

Index0123456789
Key------49--89

Summary

TechniqueCollision Resolution MethodProblem
Linear ProbingNext sequential empty slotPrimary clustering
RehashingApply a new/second hash functionMore computation needed

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