KEMBAR78
Hashing Techniques in Data Structures Part2 | PPTX
Hash Function Examples 
Let h(k) = k % 15. Then, 
if k = 25 129 35 2501 47 36 
h(k) = 10 9 5 11 2 6 
Storing the keys in the array is straightforward: 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
_ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ 
Thus, delete and find can be done in O(1), and 
also insert, except…
Hash Function 
What happens when you try to insert: k = 65 ? 
k = 65 
h(k) = 5 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
_ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ 
65(?) 
This is called a collision.
Handling Collisions 
 Chaining (Hashing with Chaining) 
Open Addressing 
– Linear Probing
Handling Collisions 
Chaining
Separate Chaining 
Let each array element be the head of a chain. 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
      
47 65 36 129 25 2501 
 
35 
Where would you store: 29, 16, 14, 99, 127 ?
Separate Chaining 
Let each array element be the head of a chain: 
Where would you store: 29, 16, 14, 99, 127 ? 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
         
16 47 65 36 127 99 25 2501 14 
   
35 129 29 
New keys go at the front of the relevant chain.
Separate Chaining: Disadvantages 
• Parts of the array might never be used. 
• As chains get longer, search time increases 
to O(n) in the worst case. 
• Constructing new chain nodes is relatively 
expensive . 
• Is there a way to use the “unused” space in 
the array instead of using chains to make 
more space?
Handling Collisions 
Linear Probing
Linear Probing 
Let key k be stored in element h(k)=t of the array 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
47 35 36 129 25 2501 
65(?) 
What do you do in case of a collision? 
If the hash table is not full, attempt to store key in the 
next array element (in this case (t+1)%N, (t+2)%N, 
(t+3)%N …). 
until you find an empty slot.
Linear Probing 
Where do you store 65 ? [Here N is 15]. 
65%15=5 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
47 35 36 65 129 25 2501 
   
attempts 
Where would you store: 29?
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
47 35 36 65 129 25 2501 29 
 
attempts 
Where would you store: 16?
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
[16%15=1] 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
16 47 35 36 65 129 25 2501 29 
 
Where would you store: 14? 
[14%15=14]
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
[14%15=14] 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
14 16 47 35 36 65 129 25 2501 29 
  
attempts 
Where would you store: 99?
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
[99%15=9] 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
14 16 47 35 36 65 129 25 2501 99 29 
    
attempts 
Where would you store: 127 ?
Linear Probing 
If the hash table is not full, attempt to store key 
in array elements (t+1)%N, (t+2)%N, … 
[127%15=7] 
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 
16 47 35 36 65 127 129 25 2501 29 99 14 
  
attempts
Linear Probing 
• Eliminates need for separate data structures 
(chains), and the cost of constructing nodes. 
• Leads to problem of clustering. Elements tend 
to cluster in dense intervals in the array. 
     
• Search efficiency problem remains.

Hashing Techniques in Data Structures Part2

  • 1.
    Hash Function Examples Let h(k) = k % 15. Then, if k = 25 129 35 2501 47 36 h(k) = 10 9 5 11 2 6 Storing the keys in the array is straightforward: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 _ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ Thus, delete and find can be done in O(1), and also insert, except…
  • 2.
    Hash Function Whathappens when you try to insert: k = 65 ? k = 65 h(k) = 5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 _ _ 47 _ _ 35 36 _ _ 129 25 2501 _ _ _ 65(?) This is called a collision.
  • 3.
    Handling Collisions Chaining (Hashing with Chaining) Open Addressing – Linear Probing
  • 4.
  • 5.
    Separate Chaining Leteach array element be the head of a chain. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14       47 65 36 129 25 2501  35 Where would you store: 29, 16, 14, 99, 127 ?
  • 6.
    Separate Chaining Leteach array element be the head of a chain: Where would you store: 29, 16, 14, 99, 127 ? 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14          16 47 65 36 127 99 25 2501 14    35 129 29 New keys go at the front of the relevant chain.
  • 7.
    Separate Chaining: Disadvantages • Parts of the array might never be used. • As chains get longer, search time increases to O(n) in the worst case. • Constructing new chain nodes is relatively expensive . • Is there a way to use the “unused” space in the array instead of using chains to make more space?
  • 8.
  • 9.
    Linear Probing Letkey k be stored in element h(k)=t of the array 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 35 36 129 25 2501 65(?) What do you do in case of a collision? If the hash table is not full, attempt to store key in the next array element (in this case (t+1)%N, (t+2)%N, (t+3)%N …). until you find an empty slot.
  • 10.
    Linear Probing Wheredo you store 65 ? [Here N is 15]. 65%15=5 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 35 36 65 129 25 2501    attempts Where would you store: 29?
  • 11.
    Linear Probing Ifthe hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 47 35 36 65 129 25 2501 29  attempts Where would you store: 16?
  • 12.
    Linear Probing Ifthe hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … [16%15=1] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 47 35 36 65 129 25 2501 29  Where would you store: 14? [14%15=14]
  • 13.
    Linear Probing Ifthe hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … [14%15=14] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 16 47 35 36 65 129 25 2501 29   attempts Where would you store: 99?
  • 14.
    Linear Probing Ifthe hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … [99%15=9] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 14 16 47 35 36 65 129 25 2501 99 29     attempts Where would you store: 127 ?
  • 15.
    Linear Probing Ifthe hash table is not full, attempt to store key in array elements (t+1)%N, (t+2)%N, … [127%15=7] 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 16 47 35 36 65 127 129 25 2501 29 99 14   attempts
  • 16.
    Linear Probing •Eliminates need for separate data structures (chains), and the cost of constructing nodes. • Leads to problem of clustering. Elements tend to cluster in dense intervals in the array.      • Search efficiency problem remains.