Upcoming SlideShare
×

# Ch17 Hashing

10,560 views

Published on

1 Comment
3 Likes
Statistics
Notes
• Full Name
Comment goes here.

Are you sure you want to Yes No
• gud work

Are you sure you want to  Yes  No
Views
Total views
10,560
On SlideShare
0
From Embeds
0
Number of Embeds
39
Actions
Shares
0
348
1
Likes
3
Embeds 0
No embeds

No notes for slide

### Ch17 Hashing

1. 1. Chapter 17 Hashing
2. 2. Chapter Objectives <ul><li>Define hashing </li></ul><ul><li>Examine various hashing functions </li></ul><ul><li>Examine the problem of collisions in hash tables </li></ul><ul><li>Explore the Java Collections API implementations of hashing </li></ul>
3. 3. Hashing <ul><li>In the collections we have discussed thus far, we have made one of three assumptions regarding order: </li></ul><ul><ul><li>Order is unimportant </li></ul></ul><ul><ul><li>Order is determined by the way elements are added </li></ul></ul><ul><ul><li>Order is determined by comparing the values of the elements </li></ul></ul>
4. 4. Hashing <ul><li>Hashing is the concept that order is determined by some function of the value of the element to be stored </li></ul><ul><ul><li>More precisely, the location within the collection is determined by some function of the value of the element to be stored </li></ul></ul>
5. 5. Hashing <ul><li>In hashing, elements are stored in a hash table with their location in the table determined by a hashing function </li></ul><ul><li>Each location in the hash table is called a cell or a bucket </li></ul>
6. 6. Hashing <ul><li>Consider a simple example where we create an array that will hold 26 elements </li></ul><ul><li>Wishing to store names in our array, we create a simple hashing function that uses the first letter of each name </li></ul>
7. 7. FIGURE 17.1 A simple hashing example
8. 8. Hashing <ul><li>Notice that using a hashing approach results in the access time to a particular element being independent of the number of elements in the table </li></ul><ul><li>This means that all of the operations on an element of a hash table should be O(1) </li></ul><ul><li>However, this efficiency is only realized if each element maps to a unique position in the table </li></ul>
9. 9. Hashing <ul><li>A collision occurs when two or more elements map to the same location </li></ul><ul><li>A hashing function that maps each element to a unique location is called perfect hashing function </li></ul><ul><li>While it is possible to develop a perfect hashing function, a function that does a good job of distributing the elements in the table will still result in O(1) operations </li></ul>
10. 10. Hashing <ul><li>Another issue surrounding hashing is how large the table should be </li></ul><ul><li>If we are assured of a dataset of size n and a perfect hashing function, then the table should be of size n </li></ul><ul><li>If a perfect hashing function is not available but we know the size of the dataset (n), then a good rule of thumb is to make the table 150% the size of the dataset </li></ul>
11. 11. Hashing <ul><li>The third case is the case where we do not know the size of the dataset </li></ul><ul><li>In this case, we depend upon dynamic resizing </li></ul><ul><li>Dynamic resizing of a hash table involves creating a new, larger, hash table and then inserting all of the elements of the old table into the new one </li></ul>
12. 12. Hashing <ul><li>Deciding when to resize is the key to dynamic resizing </li></ul><ul><li>One possibility is simply to resize when the table is full </li></ul><ul><ul><li>However, it is the nature of hash tables that their performance seriously degrades as they become full </li></ul></ul><ul><li>A better approach is to use a load factor </li></ul><ul><li>The load factor of a hash table is the percentage occupancy of the table at which the table will be resized </li></ul>
13. 13. Hashing Functions - Extraction <ul><li>There are a wide variety of hashing functions that provide good distribution of various types of data </li></ul><ul><li>The method we used in our earlier example is called extraction </li></ul><ul><li>Extraction involves using only a part of an element’s value or key to compute the location at which to store the element </li></ul><ul><li>In our previous example, we simply extracted the first letter of a string and computed its value relative to the letter A </li></ul>
14. 14. Hashing Functions - Division <ul><li>Creating a hashing function by division simply means we will use the remainder of the key divided by some positive integer p </li></ul><ul><ul><li>Hashcode(key) = Math.abs(key)%p </li></ul></ul><ul><li>This function will yield a result in the range 0 to p-1 </li></ul><ul><li>If we use our table size as p, we then have an index that maps directly to the table </li></ul>
15. 15. Hashing Functions - Folding <ul><li>In the folding method , the key is divided into two parts that are then combined or folded together to create an index into the table </li></ul><ul><li>This is done by first dividing the key into parts where each of the parts of the key will be the same length as the desired key </li></ul><ul><li>In the shift folding method , these parts are then added together to create the index </li></ul><ul><ul><li>Using the SSN 987-65-4321 we divide into three parts 987, 654, 321, and then add these together to get 1962 </li></ul></ul><ul><ul><li>We would then use either division or extraction to get a three digit index </li></ul></ul>
16. 16. Hashing Functions - Folding <ul><li>In the boundary folding method, some parts of the key are reversed before adding </li></ul><ul><li>Using the same example of a SSN 987-65-4321 </li></ul><ul><ul><li>divide the SSN into parts 987, 654, 321 </li></ul></ul><ul><ul><li>reverse the middle element 987, 456, 321 </li></ul></ul><ul><ul><li>add yielding 1764 </li></ul></ul><ul><ul><li>then use either division or extraction to get a three digit index </li></ul></ul><ul><li>Folding may also be used on strings </li></ul>
17. 17. Hashing Functions - Mid-square <ul><li>In the mid-square method, the key is multiplied by itself and then the extraction method is used to extract the needed number of digits from the middle of the result </li></ul><ul><li>For example, if our key is 4321 </li></ul><ul><ul><li>Multiply the key by itself yielding 18671041 </li></ul></ul><ul><ul><li>Extract the needed three digits </li></ul></ul><ul><li>It is critical that the same three digits by extract each time </li></ul><ul><li>We may also extract bits and then reconstruct an index from the bits </li></ul>
18. 18. Hashing Functions - Radix Transformation <ul><li>In the radix transformation method, the key is transformed into another numeric base </li></ul><ul><li>For example, if our key is 23 in base 10, we might convert it to 32 in base 7 </li></ul><ul><li>We then use the division method and divide the converted key by the table size and use the remainder as our index </li></ul>
19. 19. Hashing Functions - Digit Analysis <ul><li>In the digit analysis method , the index is formed by extracting, and then manipulating specific digits from the key </li></ul><ul><li>For example, if our key is 1234567, we might select the digits in positions 2 through 4 yielding 234 </li></ul><ul><li>The manipulation can then take many forms </li></ul><ul><ul><li>Reversing the digits (432) </li></ul></ul><ul><ul><li>Performing a circular shift to the right (423) </li></ul></ul><ul><ul><li>Performing a circular shift to the left (342) </li></ul></ul><ul><ul><li>Swapping each pair of digits (324) </li></ul></ul>
20. 20. Hashing Functions - Length-Dependent <ul><li>In the length-dependent method, the key and the length of the key are combined in some way to form either the index itself or an intermediate version </li></ul><ul><li>For example, if our key is 8765, we might multiply the first two digits by the length and then divide by the last digit yielding 69 </li></ul><ul><li>If our table size is 43, we would then use the division method resulting in an index of 26 </li></ul>
21. 21. Hashing Functions in the Java Language <ul><li>The java.lang.Object class defines a method called hashcode that returns an integer based on the memory location of the object </li></ul><ul><li>This is generally not very useful </li></ul><ul><li>Class that are derived from Object often override the inherited definition of hashcode to provide their own version </li></ul><ul><li>For example, the String and Integer classes define their own hashcode methods </li></ul><ul><li>These more specific hashcode functions are more effective </li></ul>
22. 22. Hashing - Resolving Collisions <ul><li>If we are able to develop a perfect hashing function, then we do not need to be concerned about collisions or table size </li></ul><ul><li>However, often we do not know the size of the dataset and are not able to develop a perfect hashing function </li></ul><ul><li>In these cases, we must choose a method for handling collisions </li></ul>
23. 23. Resolving Collisions - Chaining <ul><li>The chaining method simply treats the hash table conceptually as a table of collections rather than a table of individual elements </li></ul><ul><li>Thus each cell is a pointer to the collection associated with that location in the table </li></ul><ul><li>Usually, this internal collection is either an unordered list or an ordered list </li></ul><ul><li>Chaining can be accomplished using a linked structure or an array based structure with an overflow area </li></ul>
24. 24. FIGURE 17.2 The chaining method of collision handling
25. 25. FIGURE 17.3 Chaining using an overflow area
26. 26. Resolving Collisions - Open Addressing <ul><li>The open addressing method looks for another open position in the table other than the one to which the element is originally hashed </li></ul><ul><li>The simplest of these methods is linear probing </li></ul><ul><li>In linear probing, if an element hashes to position p and that position is occupied we simply try position (p+1)%s where s is the size of the table </li></ul><ul><li>One problem with linear probing is the development of clusters of occupied cells called primary clusters </li></ul>
27. 27. FIGURE 17.4 Open addressing using linear probing
28. 28. Resolving Collisions - Open Addressing <ul><li>A second form of open addressing is quadratic probing </li></ul><ul><li>In this method, once we have a collision, the next index to try is computed by </li></ul><ul><ul><li>newhashcode(x) = hashcode(x) + (-1) i-1 ((i+1)/2) 2 </li></ul></ul><ul><li>Quadratic probing helps to eliminate the problem of primary clusters </li></ul>
30. 30. Resolving Collisons - Open Addressing <ul><li>A third form of the open addressing method is double hashing </li></ul><ul><li>In this method, collisions are resolved by supplying a secondary hashing function </li></ul><ul><li>For example, if a key x hashes to a position p that is already occupied, then the next position p’ will be </li></ul><ul><ul><li>p’ = p + secondaryhashcode(x) </li></ul></ul><ul><li>Given the same example, if we use a secondary hashing function that uses the length of the string, we get the following result </li></ul>
31. 31. FIGURE 17.6 Open addressing using double hashing
32. 32. Deleting Elements from a Hash Table <ul><li>Removing an element from a chained implementation falls into one of five cases </li></ul><ul><li>If the element we are attempting to remove is the only one mapped to the location, we simply remove the element by setting the table position to null </li></ul>
33. 33. Deleting Elements from a Hash Table <ul><li>If the element we are attempting to remove is stored in the table but has an index into the overflow area </li></ul><ul><ul><li>Replace the element and the next index value in the table with the element and next index value of the array position pointed to by the element to be removed </li></ul></ul><ul><ul><li>We then must also set the position in the overflow area to null and add it back to our list of free overflow cells </li></ul></ul>
34. 34. Deleting Elements from a Hash Table <ul><li>If the element we are attempting to remove is at the end of the list of elements stored at that location the table </li></ul><ul><ul><li>Set its position in the table to null </li></ul></ul><ul><ul><li>Set the next pointer of the previous element to null </li></ul></ul><ul><ul><li>Add that position to the list of free overflow cells </li></ul></ul>
35. 35. Deleting Elements from a Hash Table <ul><li>If the element we are attempting to remove is in the middle of the list of elements stored at that location </li></ul><ul><ul><li>Set its position in the overflow area to null </li></ul></ul><ul><ul><li>Set the next pointer of the previous element to point to the next element of the element being removed </li></ul></ul><ul><ul><li>Add the position back to the list of free overflow cells </li></ul></ul><ul><li>If the element we are attempting to remove is not in the table, then we must throw an exception </li></ul>
36. 36. Deleting Elements from a Hash Table <ul><li>If we have chosen to implement our table using open addressing, the deletion creates more of a challenge </li></ul><ul><li>This is because we cannot remove a deleted item with possibly affecting our ability to find items that collided with it on entry </li></ul><ul><li>The solution to this problem is to mark items as deleted but not actually remove them from the table until some future point when either the deleted item is overwritten or the entire table is rehashed </li></ul>
37. 37. FIGURE 17.7 Open addressing and deletion
38. 38. Hash Tables in the Java Collections API <ul><li>The Java Collections API provides seven implementations of hashing: </li></ul><ul><ul><li>Hashtable </li></ul></ul><ul><ul><li>HashMap </li></ul></ul><ul><ul><li>HashSet </li></ul></ul><ul><ul><li>IdentityHashMap </li></ul></ul><ul><ul><li>LinkedHashSet </li></ul></ul><ul><ul><li>LinkedHashMap </li></ul></ul><ul><ul><li>WeakHashMap </li></ul></ul>
39. 39. TABLE 17.1 Operations on the Hashtable class
40. 40. TABLE 17.1 Operations on the Hashtable class (cont.)
41. 41. TABLE 17.2 Operations on the HashSet class
42. 42. TABLE 17.3 Operations on the HashMap class
43. 43. TABLE 17.4 Operations on the IdentityHashMap class
44. 44. TABLE 17.5 Operations on the WeakHashMap class