You are on page 1of 7

Hashing, Hashtables

Example
100,000 students with unique roll nos.
(e.g. JEE).
A ommon requirement is !inding all
students satis!"ing a ertain ondition.
(Ex. with entre as #anpur).
$olutions
$tore in an arra"
% &ithout an" organi'ation.
% (ro)en into se*eral sub+sets.
$tore as ,)e", *alue- pairs.
Hashtables
."nami set o! ,)e", *alue- pairs with search,
insert and delete operations.
Ex 1. /ompiler0 1denti!iers in a program
Ex 2. $pell he)ing
3enerali'ation o! an arra". 1n arra", aess, insert,
delete operations 4(1). Aess through an index,
whih ats li)e a )e".
Hashtable0 operations are 4(1) on a*erage.
Aess diretl" through the )e".
1mplementation
$tore in arra" A using A5hash()e")6.
% 7untion hash % hashing !untion i.e. #e"81nteger
mapping.
% hash()1)9hash()2) !or two distint )e"s )1, )2 %
ollision. 1deall" ollision should be 0.
% /olliding )e"s stored as hains (i.e. lists) in the same
loation.
$i'e o! )e" spae.
Ex. 1! )e"s !rom (0, 1, ... m+1) then an be stored
in diret+address+table. :ot possible i! )e" spae
*er" large but onl" small part is atuall" used.
;hen hash !untion used.
hash !untion
hash0 eah )e" should equi+probabl"
hash to an" o! the slots (sa" m slots)
independent o! the hash *alue o! an"
other )e".
#e"s usuall" interpreted as natural
numbers. An" other t"pe (e.g. string)
mapped to natural nos.
.i*ision method
hash())9) mod m, where m is suitabl" hosen.
A*oid m92
<
, sine hash()) is <ust the < lower order
bits o! ). 3ood hoie0 a prime not lose to power
o! 2.
Ex. 2000 elements, with ollision list si'e limited to
=. ;hen m9>01 is a good hoie.
?rime near 2000@= but not lose to power o! 2.
$o hash())9) mod >01.
/an waste spae i! m not suitable.

You might also like