11.4 Open Addressing
Separate chaining has the disadvantage of using
linked lists.
Requires the implementation of a second data structure.
In an open addressing hashing system, all
the data go inside the table.
Thus, a bigger table is needed.
Generally the load factor should be below 0.5.
If a collision occurs, alternative cells are tried until an empty cell is found.
0
1
2
3
4
5
6
7
8
9
h(k) = k mod m
m = 10
h(10) = 10 mod 10 = 0
h(20) = 20 mod 10 = 0
h(30) = 30 mod 10 = 0
h(40) = 40 mod 10 = 0
h(50) = 50 mod 10 = 0
0
1
2
3
4
5
6
7
8
9
10 ←-- 20 ←- 30 ←-- 40 ←-- 50
All the nodes are
on the chain of
linked list
Take extra space for
linked list
but
not use available space in
hash table
0 10
1
2
3
4
5
6
7
8
9
h(10) = 10 mod 10 = 0
0 10
1
2
3
4
5
6
7
8
9
h(20) = 20 mod 10 = 0
But there is a collision so we
search the next free space
0 10
1 20
2
3
4
5
6
7
8
9
h(20) = 20 mod 10 = 0
0 10
1 20
2
3
4
5
6
7
8
9
h(30) = 30 mod 10 = 0
Again we search for free space
when collision occur
0 10
1 20
2 30
3
4
5
6
7
8
9
h(30) = 30 mod 10 = 0
0 10
1 20
2 30
3
4
5
6
7
8
9
h(40) = 40 mod 10 = 0
0 10
1 20
2 30
3 40
4
5
6
7
8
9
h(40) = 40 mod 10 = 0
0 10
1 20
2 30
3 40
4
5
6
7
8
9
h(50) = 50 mod 10 = 0
0 10
1 20
2 30
3 40
4 50
5
6
7
8
9
h(50) = 50 mod 10 = 0
h(x)i
= Hash(x) + f(i)
Where f(0) = 0
This is called Linear
probing
In linear probing, collisions
are resolved by sequentially
scanning an array (with
wraparound) until an empty cell is
found.
i.e. f is a linear function of i, typically f(i)= i
But we have an issue on this Linear
Probing is clustering
As long as table is big
enough, a free cell can always
be found, but the time to do
so can get quite large.
Summary

We have solve the issue of extra memory and utilize the avaliable
memory in the hash table

Insertion take more time on to find the free space

Finding the element also following the insertion algorithm, so it
also take too time

Deletion cannot be done in a single shot
Quadratic Probing
“Make it simple with the understanding of
Linear probing”
In Linear Probing
h(x)i
= Hash(x) + f(i)
Where f(i) = i
In Quadratic Probing
h(x)i
= Hash(x) + f(i)
Where f(i) = i2
Now the clustering problem is removed from
this quadratic probing
Try this in your own style! :)
Double Hashing
A second hash function is used to drive the
collision resolution.
h(x)i
= Hash(x) + f(i)
f(i) = i * hash2(x)
This is the second hash function
Our hash2 function is
hash2(x) = R – ( x mod R)
Where R is the prime which R < Table Size (m)
Important! note is the 2nd
hashing
must not be equal to zero
Double Hashing is much faster than others :)
Some Hashing Applications
Compilers use hash tables to implement the symbol table
Game programs use hash tables to keep track of
positions it has encountered
On line spelling checkers
Thank you for watching:)

Open Addressing on Hash Tables

  • 1.
  • 2.
    Separate chaining hasthe disadvantage of using linked lists. Requires the implementation of a second data structure. In an open addressing hashing system, all the data go inside the table. Thus, a bigger table is needed. Generally the load factor should be below 0.5. If a collision occurs, alternative cells are tried until an empty cell is found.
  • 3.
    0 1 2 3 4 5 6 7 8 9 h(k) = kmod m m = 10 h(10) = 10 mod 10 = 0 h(20) = 20 mod 10 = 0 h(30) = 30 mod 10 = 0 h(40) = 40 mod 10 = 0 h(50) = 50 mod 10 = 0
  • 4.
    0 1 2 3 4 5 6 7 8 9 10 ←-- 20←- 30 ←-- 40 ←-- 50 All the nodes are on the chain of linked list Take extra space for linked list but not use available space in hash table
  • 5.
  • 6.
    0 10 1 2 3 4 5 6 7 8 9 h(20) =20 mod 10 = 0 But there is a collision so we search the next free space
  • 7.
  • 8.
    0 10 1 20 2 3 4 5 6 7 8 9 h(30)= 30 mod 10 = 0 Again we search for free space when collision occur
  • 9.
    0 10 1 20 230 3 4 5 6 7 8 9 h(30) = 30 mod 10 = 0
  • 10.
    0 10 1 20 230 3 4 5 6 7 8 9 h(40) = 40 mod 10 = 0
  • 11.
    0 10 1 20 230 3 40 4 5 6 7 8 9 h(40) = 40 mod 10 = 0
  • 12.
    0 10 1 20 230 3 40 4 5 6 7 8 9 h(50) = 50 mod 10 = 0
  • 13.
    0 10 1 20 230 3 40 4 50 5 6 7 8 9 h(50) = 50 mod 10 = 0
  • 14.
    h(x)i = Hash(x) +f(i) Where f(0) = 0
  • 15.
    This is calledLinear probing
  • 16.
    In linear probing,collisions are resolved by sequentially scanning an array (with wraparound) until an empty cell is found. i.e. f is a linear function of i, typically f(i)= i
  • 19.
    But we havean issue on this Linear Probing is clustering As long as table is big enough, a free cell can always be found, but the time to do so can get quite large.
  • 20.
    Summary  We have solvethe issue of extra memory and utilize the avaliable memory in the hash table  Insertion take more time on to find the free space  Finding the element also following the insertion algorithm, so it also take too time  Deletion cannot be done in a single shot
  • 21.
  • 22.
    “Make it simplewith the understanding of Linear probing”
  • 23.
    In Linear Probing h(x)i =Hash(x) + f(i) Where f(i) = i In Quadratic Probing h(x)i = Hash(x) + f(i) Where f(i) = i2
  • 24.
    Now the clusteringproblem is removed from this quadratic probing
  • 25.
    Try this inyour own style! :)
  • 26.
  • 27.
    A second hashfunction is used to drive the collision resolution. h(x)i = Hash(x) + f(i) f(i) = i * hash2(x) This is the second hash function
  • 28.
    Our hash2 functionis hash2(x) = R – ( x mod R) Where R is the prime which R < Table Size (m)
  • 29.
    Important! note isthe 2nd hashing must not be equal to zero
  • 30.
    Double Hashing ismuch faster than others :)
  • 31.
    Some Hashing Applications Compilersuse hash tables to implement the symbol table Game programs use hash tables to keep track of positions it has encountered On line spelling checkers
  • 32.
    Thank you forwatching:)