32
11.4 Open Addressing

Open Addressing on Hash Tables

Embed Size (px)

Citation preview

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 mm = 10

h(10) = 10 mod 10 = 0h(20) = 20 mod 10 = 0h(30) = 30 mod 10 = 0h(40) = 40 mod 10 = 0h(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 listTake 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 ApplicationsCompilers 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:)