Chapter 5 Hashing
Total Page:16
File Type:pdf, Size:1020Kb
Introduction hashing performs basic operations, such as insertion, Chapter 5 deletion, and finds in average time Hashing 2 Hashing Hashing Functions a hash table is merely an of some fixed size let be the set of search keys hashing converts into locations in a hash hash functions map into the set of in the table hash table searching on the key becomes something like array lookup ideally, distributes over the slots of the hash table, to minimize collisions hashing is typically a many-to-one map: multiple keys are if we are hashing items, we want the number of items mapped to the same array index hashed to each location to be close to mapping multiple keys to the same position results in a example: Library of Congress Classification System ____________ that must be resolved hash function if we look at the first part of the call numbers two parts to hashing: (e.g., E470, PN1995) a hash function, which transforms keys into array indices collision resolution involves going to the stacks and a collision resolution procedure looking through the books almost all of CS is hashed to QA75 and QA76 (BAD) 3 4 Hashing Functions Hashing Functions suppose we are storing a set of nonnegative integers we can also use the hash function below for floating point given , we can obtain hash values between 0 and 1 with the numbers if we interpret the bits as an ______________ hash function two ways to do this in C, assuming long int and double when is divided by types have the same length fast operation, but we need to be careful when choosing first method uses C to accomplish this task example: if , is just the lowest-order bits of are all the hash values equally likely? choosing to be a not too close to a power of 2 works well in practice 5 6 Hashing Functions Hashing Functions we can also use the hash function below for floating point we can hash strings by combining a hash of each _________ numbers if we interpret the bits as an integer (cont.) second uses a , which is a variable that can hold objects of different types and sizes is an additional parameter we get to choose if is larger than any character value, then this approach is what you would obtain if you treated the string as a base- ______________ 7 8 Hashing Functions Hashing Functions K&R suggest a slightly simpler hash function, corresponding we can use the idea for strings if our search key has to _____________ parts, say, street, city, state: hash = ((street * R + city) % M) * R + state) % M; same ideas apply to hashing vectors Weiss suggests 9 10 Hash Functions Hash Functions the choice of parameters can have a ____________ effect example: on the results of hashing good: words are __________________________ compare the text's string hashing algorithm for different pairs of and plot _______________ of the number of words hashed to each hash table location; we use the American dictionary from the aspell program as data (305,089 words) 11 12 Hash Functions Hash Functions example: example: very bad better 13 14 Hash Functions Collision Resolution example: hash table collision bad occurs when elements hash to the __________________ in the table various for dealing with collision separate chaining open addressing linear probing other methods 15 16 Separate Chaining Separate Chaining separate chaining insert, search, delete in lists keep a list of all elements that to the same location all proportional to of linked list each location in the hash table is a _________________ insert example: first 10 squares new elements can be inserted at of list duplicates can increment ______________ other structures could be used instead of lists binary search tree another hash table linked lists good if table is and hash function is good 17 18 Separate Chaining Separate Chaining how long are the linked lists in a hash table? observations value: where is the number of keys more important than table size and is the size of the table general rule: make the table as large as the number of is it reasonable to assume the hash table would exhibit elements to be stored, this behavior? keep table size prime to ensure good ________________ load factor average length of a list time to search: time to evaluate the hash function + time to the list unsuccessful search: successful search: 19 20 Separate Chaining Separate Chaining declaration of hash structure hash member function 21 22 Separate Chaining Separate Chaining routines for separate chaining routines for separate chaining 23 24 Open Addressing Linear Probing linked lists incur extra costs linear probing insert operation time to for new cells when is hashed, if slot is open, place there effort and complexity of defining second data structure if there is a collision, then start looking for an empty slot a different collision strategy involves placing colliding keys starting with location in the hash table, and in nearby slots proceed through , , 0, 1, 2, if a collision occurs, try cells until an empty wrapping around the hash table, looking for one is found an empty slot bigger table size needed with search operation is similar load factor should be below checking whether a table entry is vacant (or is one we three common strategies seek) is called a ___________ linear probing quadratic probing double hashing 25 26 Linear Probing Linear Probing example: add 89, 18, 49, 58, 69 with and as long as the table is , a vacant cell can be found but time to locate an empty cell can become large blocks of occupied cells results in primary _____________ deleting entries leaves __________ some entries may no longer be found may require moving many other entries expected number of probes for search hits: for insertion and search misses: for , these values are and , respectively 27 28 Linear Probing Quadratic Probing performance of linear probing (dashed) vs. more random quadratic probing collision resolution eliminates _______________________ adequate up to collision function is quadratic Successful, Unsuccessful, Insertion example: add 89, 18, 49, 58, 69 with and 29 30 Quadratic Probing Quadratic Probing in linear probing, letting table get nearly greatly quadratic probing hurts performance collisions will probe the same alternative cells quadratic probing clustering no of finding an empty cell once the causes less than half an extra probe per search table gets larger than half full at most, of the table can be used to resolve collisions if table is half empty and the table size is prime, then we are always guaranteed to accommodate a new element could end up with situation where all keys map to the same table location 31 32 Double Hashing Double Hashing double hashing double hashing example with apply a second hash function to and probe across is a prime smaller than table size ___________________________ insert 89, 18, 49, 58, 69 function must never evaluate to ____ make sure all cells can be probed 33 34 Double Hashing Rehashing double hashing example (cont.) table may get _______________ note here that the size of the table (10) is not prime run time of operations may take too long if 23 inserted in the table, it would collide with 58 insertions may for quadratic resolution since and the table size is 10, too many removals may be intermixed with insertions only one alternative location, which is taken solution: build a new table (with a new hash function) go through original hash table to compute a hash value for each (non-deleted) element insert it into the new table 35 36 Rehashing Rehashing example: insert 13, 15, 24, 6 into a hash table of size 7 example (cont.) with insert 23 table will be over 70% full; therefore, a new table is created 37 38 Rehashing Rehashing example (cont.) rehashing run time since elements and to rehash new table is size 17 the entire table of size roughly new hash function must have been insertions since last rehash all old elements are inserted into new rehashing may run OK if in ________________ table if interactive session, rehashing operation could produce a slowdown rehashing can be implemented with _________________ could rehash as soon as the table is half full could rehash only when an insertion fails could rehash only when a certain ______ ___ is reached may be best, as performance degrades as load factor increases 39 40 Hash Tables with Worst-Case Access Hash Tables with Worst-Case Access hash tables so far perfect hashing average case for insertions, searches, and assume all items known _________________ deletions separate chaining separate chaining: worst case if the number of lists continually increases, the lists will become shorter and shorter some queries will take nearly logarithmic time with enough lists, high probability of ______________ worst-case time would be better two problems important for applications such as lookup tables for number of lists might be unreasonably __________ routers and memory caches the hashing might still be unfortunate if is known in advance, and elements can be _______________, worst-case time is can be made large enough to have probability achievable of no collisions if collision detected, clear table and try again with a different hash function (at most done 2 times) 41 42 Hash Tables with Worst-Case Access Hash Tables with Worst-Case Access perfect hashing (cont.) perfect hashing (cont.) how large must be? example: slots 1, 3, 5, 7 empty; slots 0, 4, 8 have 1 theoretically, should be , which is ____________ element each; slots 2, 6 have 2 elements each; slot 9 solution: use lists has 3 elements resolve collisions by using hash tables instead of linked lists each of these lists can