DSA Unit VI Hashing and File Organization
DSA Unit VI Hashing and File Organization
1
Unit VI - Syllabus
Hashing: Hash tables and scattered tables:
Basic concepts, hash function, characteristics
of good hash function, Different key-to-address
transformations techniques, synonyms or
collisions, collision resolution techniques- linear
probing, quadratic probing, rehashing, chaining
with and without replacement.
File:Concept of File, File types and file
organization (sequential, index sequential and
Direct Access), Comparison of different file
organizations.
2
Hash tables and scattered tables
3
Hash Table
⚫Hash table is a data structure that is used to
store <key : value> pairs
4
Introduction
⚫Hashing is finding an address where the data is to be
stored as well as located using a key with the help of the a
function.
⚫Hashing is a method of directly computing the address of
the record with the help of a key by using a suitable
mathematical function called the hash function
⚫The resulting address is used as the basis for storing and
retrieving records and this address is called as home
address of the record
⚫For array to store a record in a hash table, hash function
is applied to the key of the record being stored, returning
an index within the range of the hash table
⚫The item is then stored in the table of that index position
Introduction
6
Hash Function
⚫A hash function is a mathematical function that
converts a numerical input value into another
compressed numerical value.
7
Hash Function
9
Collision
⚫The result of two keys hashing into the same
address is called collision
1
0
Synonym
1
1
Overflow
1
2
Load density and Load factor
1
3
Features of a good hash function
⚫The average performance of hashing depends on
how the
hash function distributes the set of keys among
the slots
⚫Division Method
⚫Multiplication Method
⚫Extraction Method
⚫Mid-Square Hashing
⚫Folding Technique
⚫Rotation
⚫Universal Hashing
Division method
⚫One of the required features of the hash function is
that
the resultant index must be within the table index
range
⚫Hash(Key) = Key % M
Multiplication method
⚫The multiplication method works as:
1.Multiply the key ‘Key’ by a constant A in the
range 0 < A < 1 and extract the fractional part of
Key ´ A
3
1
Scattered
Tables
32
Open addressing
⚫In open addressing, when collision
occurs, it is resolved by finding an
available empty location other than
the home address
⚫If Hash(Key) is not empty, the
positions are probed in the following
sequence until an empty location is
found.
⚫When we reach the end of table, the
search is wrapped around to start and
the search continues till the current
collide location
3
3
Linear Probing
⚫A hash table in which a collision is resolved
by putting the item in the next empty place
in following the occupied place is called
linear probing
⚫This strategy looks for the next free
location until it is found is called as Probing.
⚫The function that we can use for probing
linearly from the next location is as follows:
❖
⚫(Hash(x) + p(i)) MOD Max
❖
⚫As p(i) = i for linear probing, the function
❖
❖ becomes (Hash(x)+ i) MOD Max
⚫Initially i = 1, if the location is not empty
then it becomes 2, 3, 4, …, and so on till 3
4
Linear Probing
index key
43,135,72,2 0 H(k) = k
3,99,19,82. 1 mod M
H’(k) = (h(k)+i )
89 2 mod M
3
4
5
6
7
8
35 9
Disadvantages of Linear Probing
36
Variants of Linear Probing
❖ With replacement
❖ Without replacement
3
7
Linear Probing With replacement
3
8
Linear Probing Without
replacement
⚫Problem statement
⚫Insert Ki at first free location from (u+i
2)
MOD M where i=0 to (M-1)
⚫KEYS : 3,2,9,6,11,13,7,12
⚫H(k) = 2k+3
⚫M=10
⚫Use modulo-division method and quadratic
probing to store these keys.
42
Example - Quadratic Probing
Index Keys Key Location no. of
0 probe(s)
1
2
3
4
5
6
7
8
43 9
Concept of File
Hashing: Hash tables and scattered tables:
Basic concepts, hash function, characteristics
of good hash function, Different key-to-address
transformations techniques, synonyms or
collisions, collision resolution techniques- linear
probing, quadratic probing, rehashing, chaining
with and without replacement.
File:Concept of File, File types and file
organization (sequential, index sequential and
Direct Access), Comparison of different file
organizations.
44
Concept of File
45
Concept of File
46
Concept of File
47
Concept of File
48
file organization
49
file organization
50
file organization
51
file organization
52
file organization
53
file organization
54
file organization
55
file organization
56