lec26 - CSE 12 The Map Abstract Data Type Hash table time...

Info iconThis preview shows pages 1–7. Sign up to view the full content.

View Full Document Right Arrow Icon
26 Hash table time costs Hash functions The Map<K,V> interface and implementations CSE 12 The Map Abstract Data Type
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Hashing and hash tables We are considering issues in implementing hash tables We have seen that since the number of possible key values is much larger than the number of buckets in a table, collisions will happen We have considered two collision resolution strategies: Linear probing (a kind of open addressing strategy) Separate chaining (a kind of closed addressing strategy) Now let’s consider cost analysis of the basic algorithms operating on a hash table using those strategies
Background image of page 2
Hash table time costs Suppose a hash table has m buckets, and contains n entries In the worst case, a search for a key takes O( n ) steps This is as bad as a linked list! However, the average case is much better To study this, we consider the load factor λ of the hash table The load factor the number of entries the table contains n , divided by the number of buckets in the table m : λ = n/m In the analysis, we will distinguish between successful (the key is in the table) and unsuccessful (the key is not in the table) search operations Why? Describe the 'worst case' situation.
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Analysis of Linear Probing These are quite good, if λ is not too close to 1 (that is, if the table is not too full). For example if λ =.75, then U n = 8.5 and S n = 2.5 Note that these are O(1), independent of n 2 , 1 1 1 2 1 λ + U m n λ + S m n 1 1 1 2 1 , Unsuccessful search Successful search Using linear probing with load factor λ , the average case number of steps for unsuccessful and successful search are approximately:
Background image of page 4
Analysis of Separate Chaining Average case: The hash function distributes the n keys over the m chains, so the chains will have an average length equal to λ = n/m An unsuccessful search will exhaustively search a chain of length n/m = λ on average A successful search will search a chain that contains the target key, plus on average ( n -1)/ m other keys. On average, half of these other keys will be searched before finding the target key So, in the average case, searching is O(1), independent of n : λ m n U m n = = , 1 2 1 2 1 , + + = λ m n S m n Unsuccessful search Successful search
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Hash Function Design Deterministic – A hash function must always produce the same hash index each time it is given the same key Efficient – Every access to the table requires hashing a key, so it is important to the table’s performance that the hash function be fast to compute Uniform – Avoiding the worst case O(n) access time requires
Background image of page 6
Image of page 7
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/27/2012 for the course CSE 12 taught by Professor Gary during the Fall '08 term at UCSD.

Page1 / 25

lec26 - CSE 12 The Map Abstract Data Type Hash table time...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online