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.
h(k)=k mod m
Where:
- k
→ Key
- m → Hash table size
- h(k) → Hash index
- 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
|
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.
- Static
Hashing
- Dynamic
Hashing
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.
- Separate Chaining
- Linear Probing (Open Addressing)
- Quadratic Probing (Open Addressing)
- Double Hashing (Open Addressing)
- 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 |
|
Index |
Values |
|
0 |
— |
|
1 |
29 |
|
2 |
— |
|
3 |
— |
|
4 |
11 → 18 → 25 |
|
5 |
33 |
|
6 |
20 |
- 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
|
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 |
|
Index |
Value |
|
0 |
25 |
|
1 |
29 |
|
2 |
33 |
|
3 |
— |
|
4 |
11 |
|
5 |
18 |
|
6 |
20 |
- Quadratic probing is an open-addressing collision resolution technique.
- When a collision occurs, positions are checked using square increments.
|
Key |
Hash
Calculation |
Insert
Position |
|
11 |
11 mod 7 = 4 |
4 |
|
18 |
18 mod 7 = 4 →
4 + 1² = 5 |
5 |
|
20 |
20 mod 7 = 6 |
6 |
|
25 |
25 mod 7 = 4 →
4 + 1² = 5 → 4 + 2² = 8 mod 7 = 1
|
1 |
|
29 |
29 mod 7 = 1 →
1 + 1² = 2
|
2 |
|
33 |
33 mod 7 = 5 → 5+ 1² = 6 → 5+ 2² = 11 mod 7=4 → 5+ 3² = 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.
|
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 |
|
Index |
Value |
|
0 |
29 |
|
1 |
25 |
|
2 |
33 |
|
3 |
— |
|
4 |
11 |
|
5 |
18 |
|
6 |
20 |
Comments
Post a Comment