0% found this document useful (0 votes)
52 views5 pages

Hashing With Chaining

Design and analysis of Algorithms : Hash functions Hashing with Chaining - choice of Hash functions

Uploaded by

s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
52 views5 pages

Hashing With Chaining

Design and analysis of Algorithms : Hash functions Hashing with Chaining - choice of Hash functions

Uploaded by

s
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOC, PDF, TXT or read online on Scribd
You are on page 1/ 5

Hashing with chaining

Hashing with chaining is an application of linked lists and gives an approach to collision
resolution. In hashing with chaining, the hash table contains linked lists of elements or
pointers to elements (Figure 12.1). The lists are referred to as chains, and the technique is
called chaining. This is a common technique where a straightforward implementation is
desired and maximum efficiency isn't required. Each linked list contains all the elements
whose keys hash to the same index. Using chains minimizes search by dividing the set to
be searched into lots of smaller pieces. There's nothing inherently wrong with linear
search with small sequences; it's just that it gets slower as the sequences to be searched
get longer. In this approach to resolving collisions, each sequence of elements whose
keys hash to the same value will stay relatively short, so linear search is adequate.

This Hash Table is an array [0 .. m-1] of linked_list.


The table entries are called buckets or slots, and the linked lists are called chains.

x is placed in linked_list #h[x] (number of hash function).

Choice of Hash Function

Choice of h: h[x]

 must be simple
 must distributes the data evenly
Choice of m: m approximates n (= 1 item/linked list) n = input size.
e.g.
1. MOD FUNCTION.

Choice of three digit for school phone number e.g.398-3738

x is an interger value.
H[x] = x mod m.

Choising last three digit(738) is more appropriate than the first three digits (398)
as they distribute the data more evenly.
To do this take mod function:
x mod m:
h[x] = x mod 10^k: It gives last k digits
h[x] = x mod 2^k: It gives last k bits
2. MIDDLE DIGITS OF AN INTEGER

This often yields unpredictable (and thus good) distributions of the data.
Assume that you wish to take the two digits three positions from the right of x.

If x = 539 872 178


then h[x] = 72

This is obtained by
H[x] = (x/1000) mod 100
Where (/1000) drops three digits and (x/1000) mod 100 keeps two digits.

3. ORDER - PRESERVING HASH FUNCTIO


o x < y applies h[x]<= h[y] (pointer reserving by hash)

(Application: Sorting)
4. PERFECT HASH FUNCTION
o A perfect hashing function is one that causes no collisions.
o Perfect hashing functions can be found only under certain conditons.
o One application of the perfect hash function is static dictionary.
o h[x] is designed after having peeked at the data.

Analysis
Here we analyse the number of probes in a table for doing "search". A probe is a check of
a table entry. All other components of the algorithm are ignored, but as they are
collectively bounded by the number of probes, it suffices to just consider probes.

n = input size, m= table size, and a= LOAD FACTOR = n /m

-WORST CASE : (Number of probes for search /insert)

T= Theta(n) (all elements are in 1 linked list)


-AVERAGE CASE : Assume h[key[x]] spreads input independently and randomly over
m buckets.
1. "UNSUCCESSFUL SEARCH"

When the element searched is not in the table.


Number of probes = length of the linked list h[k].
Expected time = n/m = a(+1).

Where 'a' is n/m and '+1' is to check NIl.


And k is uniformly and randomly distributed over all values in the table.

2. SPACE

Space is relative to input size.


SPACE/Input size = (m+n)/n = 1 + 1/a
3. "SUCCESSFUL SEARCH"

When the element searched figures in the table


x1, x2, ..., xn: input
ti = Expected time
Number of probes =
1+(i-l)/m

Where (i-1)/m comes from the time needed to insert the i-th element in a
table with i-1 element. By the unsucceful search part, the expected time is
(i-1)/m.

On average, the time to find an element in the table is 1/n (1+)i+1)/m)=


1 +n(n-1)/2nm <= 1 + a/2.

You might also like