Docsity

Hash Table Implementation and Performance: Linear Probing and Synonyms, Slides of Computer Science

Partial preview of the text

Download Hash Table Implementation and Performance: Linear Probing and Synonyms and more Slides Computer Science in PDF only on Docsity!

Overflow Handling

• An overflow occurs when the home bucket for a

new pair (key, element) is full.

• We may handle overflows by:

 Search the hash table in some systematic fashion for a

bucket that is not full.

  • Linear probing (linear open addressing).
  • Quadratic probing.
  • Random probing.

 Eliminate overflows by permitting each bucket to

keep a list of all pairs for which it is the home bucket.

  • Array linear list.
  • Chain.

Linear Probing – Get And Put

  • divisor = b (number of buckets) = 17.
  • Home bucket = key % 17. 0 4 8 12 16
  • Put in pairs whose keys are 6, 12, 34, 29, 28, 11,

Linear Probing – remove(34)

• Search cluster for pair (if any) to fill vacated

bucket.

Linear Probing – remove(29)

• Search cluster for pair (if any) to fill vacated

bucket.

Expected Performance

  • alpha = loading density = (number of pairs)/b.

 alpha = 12/.

  • S n

= expected number of buckets examined in a

successful search when n is large

  • U n

= expected number of buckets examined in a

unsuccessful search when n is large

  • Time to put and remove governed by U n

Expected Performance

  • S n

~ ½(1 + 1/(1 – alpha))

  • U n

~ ½(1 + 1/(1 – alpha)

2

  • Note that 0 <= alpha <= 1.

alpha Sn Un

Alpha <= 0.75 is

recommended.

Hash Table Design

• Dynamic resizing of table.

 Whenever loading density exceeds threshold (4/5 in our

example), rehash into a table of approximately twice the

current size.

• Fixed table size.

 Know maximum number of pairs.

 No more than 1000 pairs.

 Loading density <= 4/5 => b >= 5/4*1000 = 1250.

 Pick b (equal to divisor) to be a prime number or an odd

number with no prime divisors smaller than 20.

Linear List Of Synonyms

• Each bucket keeps a linear list of all pairs

for which it is the home bucket.

• The linear list may or may not be sorted by

key.

• The linear list may be an array linear list or

a chain.

Expected Performance

• Note that alpha >= 0.

• Expected chain length is alpha.

• S

n

~ 1 + alpha/2.

• U

n

<= alpha, when alpha < 1.

• U

n

~ 1 + alpha/2, when alpha >= 1.

java.util.Hashtable

• Unsorted chains.

• Default initial b = divisor = 101

• Default alpha <= 0.

• When loading density exceeds max permissible

density, rehash with newB = 2b+1.