“never doubt that a small group of thoughtful, committed people can change the world. indeed, it...
TRANSCRIPT
![Page 1: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/1.jpg)
“Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.”– Margaret Meade
Thought for the Day
![Page 2: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/2.jpg)
Hash Functions
• Essential ingredient for a successful hash table
• Numeric keys– simple
• Textual (alphanumeric) keys– e.g. 609A1234– Not so simple!
key
Hash Function
index
![Page 3: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/3.jpg)
Collisions
• Prevented by using perfect hashing functions
• But this is not always possible
• Other solutions?
• Two types of hash tables:– Internal hashing– External hashing
![Page 4: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/4.jpg)
Internal Hashing with Open Addressing
• Hash the key to find location
• If it is already occupied: start “probing”– simple approach: try immediately following
positions
programming 59
......
5958 60
programming
words 59 Collision!Collision!
words
Probe
![Page 5: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/5.jpg)
Probing
• Example shows linear probing
• Other possibilities– add constant amount to hash value– add variable amount to hash value– apply second hash function to get probe
“distance”
• Must be consistent
![Page 6: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/6.jpg)
Important Side Effect
• Must be very careful when removing items
......
5958 60
programming words
• Must mark deleted entries
• One of three states:– empty– occupied– deleted
XX
![Page 7: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/7.jpg)
Clustering and Efficiency
• As the hash table fills up collisions become more frequent
• Clustering– primary clustering– secondary clustering
1 2 3 4 5 6
• Efficiency decreases
![Page 8: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/8.jpg)
Clustering: Solutions?
• Make the table bigger than required
• Use external hashing
• Use more sophisticated probing techniques– helps decrease secondary clustering
![Page 9: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/9.jpg)
Deletions
• Decrease efficiency of searching
• If deletions are frequent, need to consider rebuilding the table periodically
![Page 10: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/10.jpg)
External Hashing
• Using the hash function identifies a bucket into which the key should be placed
programming 59
programming
words 59 Collision!
words......
5958 60
![Page 11: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/11.jpg)
External Hashing
• Buckets hold several values
• As the buckets fill up, efficiency decreases– no worse than probing– usually better (no “secondary clustering”)
• Big advantage:– space is not limited by table size
![Page 12: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/12.jpg)
Implementing the Buckets
• Many possible approaches– linked lists– binary search trees– secondary hash tables!
• We will use a simple unordered linked list
![Page 13: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/13.jpg)
The ExternalHashTable Classpublic class ExternalHashTable<K, V> implements Dictionary<K, V> { private static final int DEF_SIZE = 101; private class EntryNode<K, V> extends DictionaryPair<K, V> { EntryNode<K, V> next; } // class EntryNode
private EntryNode<K, V>[] table;
private int hash (K aKey) // Scale hash value for table size { . . . } // hash ... } // class ExternalHashTable
Thebuckets
![Page 14: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/14.jpg)
Hashing Function
• How do we handle the hashing function?
• Java to the rescue!– Object class has hashCode method
public int hashCode ()
![Page 15: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/15.jpg)
Using the hashCode Method
private int hash (K aKey)// Scale hash value for table size { return ((aKey.hashCode() & 0x7FFFFFFF) % table.length); } // hash
• Need to ensure it is positive and in correct range:
![Page 16: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/16.jpg)
The Buckets
table ...
key
value
key
value
key
value
![Page 17: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/17.jpg)
The insert Method
public void insert (K aKey, V aValue)// Insert new element or update existing one { int index = hash(aKey); // Look for aKey in linked list EntryNode<K, V> c; for (c = table[index]; c != null && ! c.getKey().equals(aKey); c = c.next) ; ... } // insert
![Page 18: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/18.jpg)
The insert Method (cont.)
...if (c == null) // Insert new node { EntryNode<K, V> n = new EntryNode<K, V>(aKey, aValue); n.next = table[index]; table[index] = n; }else // Update existing entry c.setValue(aValue);
![Page 19: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/19.jpg)
Other Methods• Quite simple
• Remember structure:– array of linked lists
• Example:
public boolean isEmpty ()// Tell whether hash table is empty { for (int k = 0; k < table.length; k++) if (table[k] != null) return false; // Found at least one entry return true; // Found no entries } // isEmpty
![Page 20: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/20.jpg)
External Hash Table: Iterators
• Slightly more complicated:– need to work through array– and work through linked
lists
table ...
key
value
key
value
key
value
![Page 21: “Never doubt that a small group of thoughtful, committed people can change the world. Indeed, it is the only thing that ever has.” – Margaret Meade Thought](https://reader034.vdocuments.site/reader034/viewer/2022051516/56649f4f5503460f94c70f7c/html5/thumbnails/21.jpg)