Hashing in Data Structure | Static Hashing | Collision Resolution Techniques | Separate Chaining | Linear Probing |Quadratic Probing| Double Hashing| Hashing in DBMS

 

Hashing in DBMS

Hashing is a technique used to map data elements (keys) to specific locations in a hash table using a hash function.

It allows insertion and retrieval of data efficiently
  

Hash Function

A hash function converts a key into a hash code (index), which is used to store and retrieve the data from a hash table.

                               h(k)=k mod m

Where:

  • k → Key
  • m → Hash table size
  • h(k) → Hash index 

Hash Table
  • A hash table is a data structure that stores data in an array format.
  • Each position in the table is called a bucket, and the position is accessed using the hash function.

Example

Consider

  • Keys                   (k) = 11, 18, 20, 25, 29, 33
  • Hash table size (m) = 7
Hash function:   h(k)=k mod m
                           h(k)=k mod 7

Key

Hash Calculation

Index

11

11 mod 7

4

18

18 mod 7

4

20

20 mod 7

6

25

25 mod 7

4

29

29 mod 7

1

33

33 mod 7

5

Here, 18 and 25 also map to index 4, creating a collision.



Types of Hashing
Hashing is mainly classified into:

  1. Static Hashing
  2. Dynamic Hashing


Static Hashing:
                       Static hashing uses a fixed-size hash table.

Characteristics

  • The size of the hash table remains fixed.
  • The number of buckets does not change during execution.
  • The hash function remains constant.
  • As data increases, collisions and overflow may occur.
  • Collision resolution techniques are used to handle collisions. 

Collision Resolution Techniques in Static Hashing
The main techniques are:
  1. Separate Chaining
  2. Linear Probing (Open Addressing)
  3. Quadratic Probing (Open Addressing)
  4. Double Hashing (Open Addressing)


Separate Chaining
Separate chaining is a collision resolution technique that uses linked lists.

Working
  • Each hash table index stores a list of elements.
  • Keys that produce the same index are stored in the same linked list.
  • It avoids overflow in the table itself but requires additional memory.

Example

  • Keys                   (k) = 11, 18, 20, 25, 29, 33
  • Hash table size (m) = 7

Key

Hash Calculation

Index(mod remaining value)

11

11 mod 7

4

18

18 mod 7

4

20

20 mod 7

6

25

25 mod 7

4

29

29 mod 7

1

33

33 mod 7

5


For the keys 11, 18 and 25:
11 mod 7=4
18 mod 7=4
25 mod 7=4

Therefore, they are stored in the same chain(Linked List):
Index 4 → 11 → 18 → 25

Index

Values

0

1

29

2

3

4

11 → 18 → 25

5

33

6

20

 


Linear Probing
  • Linear probing is a collision resolution technique based on open addressing.
  • When a collision occurs, the next sequential position is checked until an empty position is found.

Formula
                    
h(k,i)=(h(k)+i) mod m
                                                     where i = 0, 1, 2, ...

Key

Hash Calculation

Insert Position

11

11 mod 7 = 4

4

18

18 mod 7 = 4 → next 5
(Index 4 already occupied, so check  
Next empty  position)

5

20

20 mod 7 = 6

6

25

25 mod 7 = 4 → 5 → 6 → 0

0

29

29 mod 7 = 1

1

33

33 mod 7 = 5 → 6 → 0 → 1 → 2

2

 Final Hash Table

Index

Value

0

25

1

29

2

33

3

4

11

5

18

6

20




Quadratic Probing
  • Quadratic probing is an open-addressing collision resolution technique.
  • When a collision occurs, positions are checked using square increments.
Formula
                    h(k,i)=(h(k)+i2) mod m

Key

Hash Calculation

Insert Position

11

11 mod 7 = 4

4

18

18 mod 7 = 4 → 4 + = 5

5

20

20 mod 7 = 6

6

25

25 mod 7 = 4 
→ 4 + = 5
→ 4 + = 8 mod 7 = 1

1

29

29 mod 7 = 1 

→ 1 + = 2

2

33

33 mod 7 = 5 

→ 5+ = 6
→ 5+ = 11 mod 7=4
→ 5+ = 14 mod 7=0

0

Final Hash Table

Index

Value

0

33

1

25

2

29

3

4

11

5

18

6

20



Double Hashing

  • Double hashing is an open-addressing collision resolution technique.
  • It uses two hash functions to determine the position of a key.

First Hash Function
                                        h1(k)=k mod 7

Second Hash Function
                                         h2(k)=1+(k mod 6)

Final Formula
                                  h(k,i)=(h1(k)+i×h2(k)) mod 7


Key

Hash Calculation

Insert Position

11

h₁ = 11 mod 7 = 4

4

18

h₁ = 18 mod 7 = 4;

h₂ = 1+(18 mod 6)=1;
(4+1×1) mod 7 = 5

5

20

h₁ = 20 mod 7 = 6

6

25

h₁ = 25 mod 7=4;

h₂ = 1+(25 mod 6)=2;
(4+1×2) mod 7 = 6 → full;
(4+2×2) mod 7 = 1

1

29

h₁ = 29 mod 7= 1;

h₂ = 1+(29 mod 6)= 6;
(1+1×6) mod 7 = 0

0

33

h₁ = 33 mod 7= 5;

 h₂ = 1+(33 mod 6)= 4;
(5+1×4) mod 7 = 2

2


Final Hash Table

Index

Value

0

29

1

25

2

33

3

4

11

5

18

6

20


Handwritten Notes- Download Complete Handwritten Notes PDF

Video Explanation- https://youtu.be/IX5jxVn_Qgk

Comments

Popular posts from this blog

Queue ADT in Data structure | Enqueue and Dequeue operation

Normalization in DBMS,(1NF,2NF,3NF,BCNF,4NF,5NF)

Entity-Relationship(ER) Model in DBMS