Static and Dynamic Hashing
Static and Dynamic Hashing
• In static Hashing , dictionary pairs are stored in a table ht, called the hash table.
• Hash table is partitioned into b buckets ht[0],…ht[b-1].
• Each bucket is capable of holding s dictionary pairs.
• Bucket consist of s slots each slot being large enough to hold one dictionary pair.
• Usually s=1, and each bucket can hold exactly one pair
• The address or location of a pair whose key is k is determined by a hash function h- which maps keys into
buckets. Thus for any key k, h(k) is an integer in the range 0- (b-1)
• Key density of a hash table is the ratio n/T, where n- no of pairs in the table, T- total no of possible keys
• The loading density or loading factor of a hash table is = n/(sb)
• Key density of a hash table is the ratio n/T,
• where n- no of pairs in the table ,T- total no of possible keys.
• The loading density or loading factor of a hash table is = n/(sb)
Example: f the keys are at most 6 characters long, where each character may be a decimal digit or an uppercase
letter and the first char is a letter No of possible keys T= 26X36i >1.6 X109
Example
• Consider a hash table ht with b=26 buckets and s=2. we have n=10 distinct identifiers each representing C library
function.
• Load factor=10/52=0.19
• The hash function must map each of the possible identifiers onto one of the nos 0-25
• Construct a simple hash function by associating letters a-z with no’s 0-25 respectively and then defining hash function
f(x) as first character of x
In the above example, if the next identifier clock hashes into bucket ht[2].
• Since bucket is full, we have an overflow and we need to resolve it When no overflow occurs, time required to
insert, delete or search using hashing depends only on time required to compute hash function and time to search
one bucket.
• Hence insert, delete and search times are independent of n, no of entries in Dictionary Since the bucket size s is
small search within a bucket is carried out sequentially
• The above hash function is not well suited because of large no of collisions and overflow.
• Choose a hash function that is both easy to compute and results in very few collisions
k h(k)
A0 100 000
A1 100 001
B0 101 000
B1 101 001
C1 110 001
In this example
keys are 2 C2 110 010 characters
each char C3 110 011 transforms
into 3 bit sequence
A-100
C4 110 101 B-101
C-110
Each did=git 0-7 is represented by 3 bit sequence H(A0,1)=0=0
H(A1,3)=001=1 H(B1,4)=1001=9