data-structures

Definition

Hash Table

A hash table is an array of length where each element , , is a singly linked list.

Example:

Probing

There are multiple ways to deal with collisions.

Linear Probing

todo

Problem: Primary clustering todo

Quadratic Probing

Let be a normal hash function. The probing function is then:

where are properly chosen constants.

Problem: Primary clustering is mitigated, but another phenomenon, secondary clustering, could occur.

Double Hashing

Let be two hash functions. The probing function is then:

with .

Choice of : For all keys , the probing sequence must reach all slots . This means that must hold and it must not divide . should be a prime number, , should be chosen independently of .

Brent’s Improvement

Idea: Whenever a key is inserted at a probed slot with (already occupied), set:

If is occupied but is free, then move to to free the position for .

Example:

Benefit: The average-case runtime of a successful search is in worst case .

Algorithm:

def insert_brent(T: HashTable, k: Key) -> None:
	j = h1(k)
	while T[j].status == "used":
		k_prime = T[j].key
		j1 = (j + h2(k)) % m
		j2 = (j + h2(k_prime)) % m
		if T[j1].status != "used" or T[j2].status == "used":
			j = j1
		else:
			T[j] = k
			k = k_prime
			j = j2
	T[j] = k
	T[j].status = "used"