Lecture 20

# Introduction to Algorithms, Second Edition

• Notes
• davidvictor
• 7

This preview shows pages 1–3. Sign up to view the full content.

The University of Texas at Austin Hashing Department of Computer Sciences Professor Vijaya Ramachandran Lecture 20 CS357: ALGORITHMS, Spring 2006 1 Hashing Hashing is a widely-used class of data structures that support the operations of insert, delete and search on a dynamic set of keys S . The keys are drawn from a universe U ( | U | >> | S | ), which for our purposes will be a set of positive integers. These keys are mapped into a hash table using a hash function ; the size of the hash table is usually within a constant factor of the number of elements in the current set S . There are two main methods used to implement hashing: hashing with chain- ing and hashing with open addressing . We will study hashing with chaining here. 1.1 Hashing with Chaining In hashing with chaining , the elements in S are stored in a hash table T [0 .. m 1] of size m , where m is somewhat larger than n , the size of S . The hash table is said to have m slots . Associated with the hashing scheme is a hash function h which is a mapping from U to { 0 , · · · , m 1 } . Each key k S is stored in location T [ h ( k )], and we say that key k is hashed into slot h ( k ). If more than one key in S hashes into the same slot, then we have a collision . In such a case, all keys that hash into the same slot are placed in a linked list associated with that slot; this linked list is called the chain at that slot. The load factor of a hash table is defined to be α = n/m ; it represents the average number of keys per slot. We typically operate in the range m = Θ( n ) so α is usually a constant (usually α < 1). If a large number of insertions or deletions destroys this property, a more suitable value of m is chosen and the keys are re-hashed into a new table with a new hash function.

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

In simple uniform hashing we assume that we have a ‘good’ hash function h such that any element in U is equally likely to hash into any of the m slots in T , independent of the other keys in the table. We also assume that h ( k ) can be computed in constant time. In hashing with chaining, inserts and deletes can be performed in constant time with a suitable linked list representation for the chains. In the worst case, a search operation can take as long as n steps if all keys hash into the same slot. However, if we assume simple uniform hashing then the expected
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern