Data+Structures+and+Algorithms+Bootcamp+in+Python+slides+Remaster (1) - Part-3
Data+Structures+and+Algorithms+Bootcamp+in+Python+slides+Remaster (1) - Part-3
Hashing is a method of sorting and indexing data. The idea behind hashing is to allow large
amounts of data to be indexed using keys commonly created by formulas
Magic function
Apple 18
Application 20
Appmillers 22
0 1 .. 18 19 20 21 22 23
.. Apple Application Appmillers
AppMillers
www.appmillers.com
Why Hashing?
It is time e cient in case of SEARCH Operation
Tree O(logN)
AppMillers
www.appmillers.com
ffi
Hashing Terminology
Hash function : It is a function that can be used to map of arbitrary size to data of xed size.
Key : Input data by a user
Hash value : A value that is returned by Hash Function
Hash Table : It is a data structure which implements an associative array abstract data type, a
structure that can map keys to values
Collision : A collision occurs when two di erent keys to a hash function produce the same
output.
Hash Function
Apple 18
0 1 .. 18 19 20 21 22 23
Hash function
ABCD 20
ABCDEF 20
ABCDEF
Collision
0 1 ..
..
18 19 💥
20
ABCD
21 22 23
AppMillers
www.appmillers.com
ff
fi
Hash Functions
Mod function
mod(400, 24) 16
mod(700, 24) 4
0 1 .. 4 5 .. 16 .. 23
.. 700 .. 400 ..
AppMillers
www.appmillers.com
Hash Functions
ASCII function
modASCII("ABC", 24) 6
A 65 65+66+67 = 198 24
192 8
B 66
6
C 67
0 1 .. 6 7 .. 16 .. 23
.. ABC .. ..
AppMillers
www.appmillers.com
Hash Functions
Hash function
ABCD 20
ABCDEF 20
ABCDEF
Collision
0 1 ..
..
18 19 💥
20
ABCD
21 22 23
AppMillers
www.appmillers.com
Hash Functions
ABCDEF
Hash function
ABC 18
Collision
0 1 ..
..
💥
18
ABCD
19 20 21 22 23
AppMillers
www.appmillers.com
Collision Resolution Techniques
Hash function
ABCD 0
2
💥
Collision
1
EFGH 2
2 ABCD EFGH
IJKLM 3
4
5
6
7
8
9
10
11
12
13
14
15
AppMillers
www.appmillers.com
Collision Resolution Techniques
Resolution Techniques
Linear Probing
Quadratic Probing
Double Hashing
AppMillers
www.appmillers.com
Collision Resolution Techniques
Direct Chaining : Implements the buckets as linked list. Colliding elements are stored in this lists
0
1
Hash function 2 111 ABCD 222
Null EFGH 333
Null IJKLM Null
ABCD 2 4
5
EFGH 2
6
IJKLM 2
7 444 Miller Null
Miller 7 444
8
9
10
11
12
13
14
15
AppMillers
www.appmillers.com
Collision Resolution Techniques
Open Addressing: Colliding elements are stored in other vacant buckets. During storage and
lookup these are found through so called probing.
Linear probing : It places new key into closest following empty cell
0
1
Hash function 2 ABCD
3 EFGH
ABCD 2 4 KLM
5
EFGH 2
6
IJKLM 2
7
8
9
10
11
12
13
14 AppMillers
www.appmillers.com
15
IJ
Collision Resolution Techniques
Open Addressing: Colliding elements are stored in other vacant buckets. During storage and
lookup these are found through so called probing.
Quadratic probing : Adding arbitrary quadratic polynomial to the index until an empty cell is found
0
1
Hash function 2 ABCD
3 2 + 4= 6
ABCD 2 4
2 + 4= 6
5
EFGH 2
6 EFGH 2 + (2*4) = 8
IJKLM 2
7
8 KLM
9
Hash 2
10
11
EFGH 4 12
13
IJKLM 4
14 AppMillers
www.appmillers.com
15
IJ
Hash Table is Full
Direct Chaining
This situation will never arise.
Hash function
222 555
IJKLM 3 2 333 ABCD Null
NOPQ 0 333
3 444 IJKLM
RSTU 1 Null
444
AppMillers
www.appmillers.com
Hash Table is Full
Open addressing
Create 2X size of current Hash Table and recall hashing for current keys
Hash function
0 NOPQ 0 NOPQ
ABCD 2
EFGH 1
1 EFGH 1 EFGH
AppMillers
www.appmillers.com
Pros and Cons of Collision resolution techniques
Direct chaining
- Hash table never gets full
- Huge Linked List causes performance leaks (Time complexity for search operation becomes O(n).)
Open addressing
- Easy Implementation
- When Hash Table is full, creation of new Hash table a ects performance (Time complexity for
search operation becomes O(n).)
AppMillers
www.appmillers.com
ff
Pros and Cons of Collision resolution techniques
Hash function
AppMillers
www.appmillers.com
Personal Computer
Practical Use of Hashing
Login : [email protected]
Password: 123456
Google Servers
Hash value: *&71283*a12
AppMillers
www.appmillers.com
fi
Practical Use of Hashing
AppMillers
www.appmillers.com
fi
Practical Use of Hashing
File system : File path is mapped to physical location on disk
Path: /Documents/Files/hashing.txt
1 /Documents/
Files/hashing.txt Physical location: sector 4
3
AppMillers
www.appmillers.com
Pros and Cons of Hashing
x When Hash function is not good enough Insertion/Deletion/Search operations take O(n) time
AppMillers
www.appmillers.com