Professional Documents
Culture Documents
Chapter 9 - Hash
1. Basic concepts
2. Hash functions
3. Collision resolution
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 1 / 44
Outcomes
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 2 / 44
Basic concepts
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 3 / 44
Basic concepts
Search complexity:
erage) Case)
16 4 8 16
50 6 25 50
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 4 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 5 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 5 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 6 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 7 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 8 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 8 / 44
Basic concepts
Ideal hashing:
No location collision
Compact address space
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 8 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 9 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 10 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 11 / 44
Basic concepts
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 12 / 44
Hash functions
Hash functions
Direct hashing
Modulo division
Digit extraction
Mid-square
Folding
Rotation
Pseudo-random
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 13 / 44
Direct Hashing
hash(Key) = Key
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 14 / 44
Direct Hashing
space.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 15 / 44
Modulo division
Example:
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 16 / 44
Digit extraction
379452→394
121267→112
378845→388
160252→102
045128→051
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 17 / 44
Mid-square
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 18 / 44
Mid-square
Example:
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 19 / 44
Folding
The key is divided into parts whose size matches the address size.
Example:
Key = 123|456|789
fold shift
123 + 456 + 789 = 1368
→ 368
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 20 / 44
Folding
The key is divided into parts whose size matches the address size.
Example:
Key = 123|456|789
fold shift
123 + 456 + 789 = 1368
→ 368
fold boundary
321 + 456 + 987 = 1764
→ 764
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 20 / 44
Rotation
Hashing keys that are identical except for the last character may
create synonyms.
600101 160010
600102 260010
600103 360010
600104 460010
600105 560010
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 21 / 44
Rotation
600101 → 62 160010 → 26
600102 → 63 260010 → 36
600103 → 64 360010 → 46
600104 → 65 460010 → 56
600105 → 66 560010 → 66
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 22 / 44
Pseudo-random
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 23 / 44
Pseudo-random
Example:
Key = 121267
a = 17
c = 7
listSize = 307
= 41
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 24 / 44
Collision resolution
Collision resolution
Except for the direct hashing, none of the others are one-to-one
mapping
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 25 / 44
Collision resolution
Open addressing
Bucket hashing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 26 / 44
Open addressing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 27 / 44
Open addressing
Hash function:
h : U → {0, 1, 2, ..., m − 1}
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 28 / 44
Open addressing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 29 / 44
Open Addressing
i = 0
while i<m do
j = hp(k, i)
return j
else
i = i + 1
end
end
End hashInsert
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 30 / 44
Open Addressing
i = 0
while i<m do
j = hp(k, i)
if T[j] = k then
return j
else
i = i + 1
end
end
return nil
End hashSearch
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 31 / 44
Open Addressing
Linear probing
Quadratic probing
Double hashing
Key oset
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 32 / 44
Linear Probing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 33 / 44
Linear Probing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 33 / 44
Linear Probing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 34 / 44
Linear Probing
Advantages:
Disadvantages:
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 35 / 44
Quadratic Probing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 36 / 44
Quadratic Probing
Advantages:
Disadvantages:
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 37 / 44
Double Hashing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 38 / 44
Key Oset
The new address is a function of the collision address and the key.
of f set = [key/listSize]
newAddress = (collisionAddress + of f set) mod listSize
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 39 / 44
Key Oset
The new address is a function of the collision address and the key.
of f set = [key/listSize]
newAddress = (collisionAddress + of f set) mod listSize
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 39 / 44
Open addressing
{0, 1, . . . , m − 1}
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 40 / 44
Linked List Resolution
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 41 / 44
Linked list resolution
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 42 / 44
Bucket hashing
Each bucket has an address and collisions are postponed until the
bucket is full.
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 43 / 44
Bucket hashing
Lecturer: Vuong Ba Thinh Contact: vbthinh@hcmut.edu.vn Data Structure and Algorithms [CO2003] 44 / 44