Professional Documents
Culture Documents
Hash Tables
COMP2015 HKBU/CS/JF/2023 1
Motivation
n Binary search tree and AVL tree can be used to implement
Insert and Find operations
q The premise is that the data should be sorted in advance
n Objective:
q Deletions
q Finds
COMP2015 HKBU/CS/JF/2023 2
Hash Table
n A hash table is a kind of symbol table.
COMP2015 HKBU/CS/JF/2023 3
Symbol Table
Key Value
23123456 Chan Yat, Male, 96868686
23234567 Cheung Wai Man, Male, 62323232
23345678 Lee Wing Man, Female, 94343434
23456789 Wong Siu Ming, Male, 61700800
… ...
COMP2015 HKBU/CS/JF/2023 4
Hash Function
n Hash Function: a function h that maps a search key to an
integer index.
q e.g., h(“06053344”) = 36
“06053344”
(a search key)
q A hash function:
n Should be simple to compute;
COMP2015 HKBU/CS/JF/2023 5
Hash Table
n A symbol table that uses a hash function is called a hash
table.
“06053344”
h(“06053344”)
COMP2015 HKBU/CS/JF/2023 6
Collision in Hashing
n A hash function can sometimes return the same index
(called hash value) for two different keys.
h(“06053344”) = 36
h(“06059999”) = 36
…
n Having two or more keys hash to the same index is called
collision.
COMP2015 HKBU/CS/JF/2023 7
Hash Function
Usually, m << N.
COMP2015 HKBU/CS/JF/2023 8
Hash Function
COMP2015 HKBU/CS/JF/2023 9
Hash Function
n Method 1
q Add up the ASCII values of the characters in the string
(72+111+110+103+75+111+110+103)
q Problems:
n Different permutations of the same set of characters
would have the same hash value
n If the table size is large, the keys are not distributed well.
n Method 2
Represent the first three characters s1s2s3 in the string by the
ASCII code, choose a number r, the integer value for the
string is
ASCII (s1) + ASCII(s2)r + ASCII(s3)r2
COMP2015 HKBU/CS/JF/2023 11
Collision Handling: Separate Chaining
h(K) = K mod 10
COMP2015 HKBU/CS/JF/2023 12
Separate Chaining
n To insert a key K
q Compute h(K) to determine which list to traverse, then
insert in the linked list
n To delete a key K
q Compute h(K), then search for K within the list. Delete K
if it is found.
COMP2015 HKBU/CS/JF/2023 13
Collision Handling: Open Addressing
n Open addressing:
q Relocate the key K to be inserted if it collides with an
existing key. That is, we store K at a different entry.
q Quadratic probing
q Double hashing
COMP2015 HKBU/CS/JF/2023 14
Open Addressing
COMP2015 HKBU/CS/JF/2023 15
Linear Probing
n f(i) = i
q cells are probed sequentially (with wraparound)
n Insertion:
q Let K be the new key to be inserted. We compute hash(K)
q For i = 0 to m-1
COMP2015 HKBU/CS/JF/2023 16
Linear Probing
Question: Insert the following values into the hash table using
hash(K)=K mod 10 (table size) and linear probing to resolve
collisions
1, 5, 11, 7, 12, 17, 6, 25
0 1 2 3 4 5 6 7 8 9
111 12 25
5 6 17
7
COMP2015 HKBU/CS/JF/2023 17
Linear Probing
COMP2015 HKBU/CS/JF/2023 18
Primary Clustering
COMP2015 HKBU/CS/JF/2023 19
Primary Clustering in Linear
Probing
COMP2015 HKBU/CS/JF/2023 20
Quadratic Probing
n f(i) = i2
n hi(K) = ( hash(K) + i2 ) mod m
n Example: inserting keys 89, 18, 49, 58, 69 with hash(K) = K
mod 10
COMP2015 HKBU/CS/JF/2023 21
Quadratic Probing
COMP2015 HKBU/CS/JF/2023 22
Quadratic Probing
n If the table size is a prime of the form 4k+3, the entire table
can be probed.
COMP2015 HKBU/CS/JF/2023 23
Quadratic Probing
n Secondary clustering
q Keys that hash to the same home position will probe the
same alternative cells
COMP2015 HKBU/CS/JF/2023 24
Tutorial
Insert the following keys into the hash table using hash(K)=K
mod m (table size) and linear/quadratic probing to resolve
collisions
19, 39, 29, 9, 79, 49, 69
0 1 2 3 4 5 6 7 8 9
COMP2015 HKBU/CS/JF/2023 25
Double Hashing
n The idea is that even if two keys hash to the same value
using hash(), they will have different values using hash2().
n f(i) = i * hash2(K)
q e.g., hash2(K) = R - (K mod R), where R is a prime
number smaller than m
q hash2 should return a nonzero integer.
COMP2015 HKBU/CS/JF/2023 26
Double Hashing
n hi(K) = ( hash(K) + f(i) ) mod m; hash(K) = K mod m
n f(i) = i * hash2(K); hash2(K) = R - (K mod R),
n Example: m=10, R = 7 and insert keys 89, 18, 49, 58, 69
To insert 49,
hash2(49)=7, 2nd
probe is T[(9+7)
mod 10]
To insert 58,
hash2(58)=5, 2nd
probe is T[(8+5)
mod 10]
To insert 69,
hash2(69)=1, 2nd
probe is T[(9+1)
mod 10]
COMP2015 HKBU/CS/JF/2023 27
Rehashing
COMP2015 HKBU/CS/JF/2023 28
Example Applications
COMP2015 HKBU/CS/JF/2023 29
Issues
n What do we lose?
q Operations that require ordering are inefficient
q FindMax
q FindMin
q PrintSorted
n What do we gain?
q Insert: O(1)
q Delete: O(1)
q Find: O(1)
COMP2015 HKBU/CS/JF/2023 30
Tutorial
v [Hash Table – More Collision Resolution] Suppose you
have a hash table of size 10, with initial hash function h(x) =
2x. Insert the following keys into the hash table:
2, 12, 3, 10, 5, 7
v (a) Have your hash table using linear probing to resolve
collisions.
v (b) Have your hash table using quadratic probing to resolve
collisions.
v (c) Have your hash table using double hashing, with second
hash function f(x) = x + 1.
COMP2015 HKBU/CS/JF/2023 31