What Is Hashing?
What Is Hashing?
Hashing
Lecture No. 8
Contents
1 What is Hashing?
2 Hash Function
3 Collisions Reduction
7 LOGO
7
1
1/8/23
What is Hashing?
8 LOGO
8
Introduction
v Hashing is a useful searching technique, which can be
used for implementing indexes.
9 LOGO
9
2
1/8/23
What is Hashing?
v The idea is to discover the location of a key by simply
examining the key. For that we need to design a hash
function.
Example
ASCII code
HOME
NAME for first two PRODUCT
ADDRESS
letters
BALL 66 65 66×65=4290 290
11 LOGO
11
3
1/8/23
What is Hashing?
RRN File
000
001
⁞ ⁞
004 LOWELL
⁞ ⁞
290 BALL
⁞ ⁞
888 TREE
⁞ ⁞
999
12 LOGO
12
What is Hashing?
v There is no obvious connection between the key and the
location (randomizing)
13 LOGO
13
4
1/8/23
What is Hashing?
v LOWELL, LOCK, OLIVER, and any word with first two
letters L and O will be mapped to the same address
h(LOWELL)=h(LOCK)=h(OLIVER)=004
14 LOGO
14
Reducing Collisions
1. Spread out the records by choosing a good hash function
15 LOGO
15
5
1/8/23
Hash Function
16 LOGO
16
17 LOGO
17
6
1/8/23
18 LOGO
18
19 LOGO
19
7
1/8/23
21 LOGO
21
8
1/8/23
22 LOGO
22
Randomization
v When there is no natural separation between keys, try
randomization.
23 LOGO
23
9
1/8/23
Randomization
2. Radix transformation:
Transform the number into another base and then
divide by the maximum address
Example:
Addresses from 0 to 99
key = 453 in base 11 = 382
hash address = 382 mod 99 = 85.
24 LOGO
24
Collisions Reduction
25 LOGO
25
10
1/8/23
1. Insertion of key k:
§ Go to the home address of k: h(k)
§ If free, place the key there
§ If occupied, try the next position until an empty
position is found
(the ‘next’ position for the last position is position 0, i.e.
wrap around)
26 LOGO
26
27 LOGO
27
11
1/8/23
28 LOGO
28
29 LOGO
29
12
1/8/23
30 LOGO
30
v Disadvantage:
§ If there are lots of collisions of records, as in the
previous example
31 LOGO
31
13
1/8/23
32 LOGO
32
33 LOGO
33
14
1/8/23
34 LOGO
34
35 LOGO
35
15
1/8/23
36 LOGO
36
37 LOGO
37
16
1/8/23
38 LOGO
38
39 LOGO
39
17
1/8/23
Making Deletions
v Deletions in a hashed file have to be made with care:
⁞ ⁞
4 /////////////////////
Key K Home Address – h(k) 5 ADAMS
ADAMS 5 6 JONES
JONES 6 7 MORRIS
MORRIS 6 8 SMITH
SMITH 5 ⁞ ⁞
40 LOGO
40
⁞ ⁞ ⁞ ⁞
4 ///////////////////// 4 /////////////////////
5 ADAMS 5 ADAMS
Empty Slots
6 JONES 6 JONES
7 MORRIS 7 /////////////////////
8 SMITH 8 SMITH
⁞ ⁞ ⁞ ⁞
41 LOGO
41
18
1/8/23
⁞ ⁞
4 ///////////////////// Empty Slot
5 ADAMS
6 JONES
7 ########### Deleted Slot
8 SMITH you can find ‘SMITH’
⁞ ⁞
43 LOGO
43
19
1/8/23
44 LOGO
44
45 LOGO
45
20
1/8/23
46 LOGO
46
insert k? 4 XXXXXXX
5 XXXXXXX
6 XXXXXXX
v Answer: 6, 9, 1, 4, 7, 10, 2, 5, 8, 0, 3 7 XXXXXXX
8 XXXXXXX
9 XXXXXXX
10 XXXXXXX
47 LOGO
47
21
1/8/23
48 LOGO
48
Chained
Progressive
Key K home address Progressive
Overflow
Overflow
ADAMS 20 1 1
BATES 21 1 1
COLES 20 3 2
DEAN 21 3 2
EVANS 24 1 1
FLI NT 20 6 3
Average Search Length 2.5 1.7
49 LOGO
49
22
1/8/23
50 LOGO
50
51 LOGO
51
23
1/8/23
52 LOGO
52
24