1 / 61

12 Hash-Table Data Structures

12 Hash-Table Data Structures. Hash-table principles. Closed-bucket and open-bucket hash tables. Searching, insertion, deletion. Hash-table design. Implementations of sets and maps using hash tables. © 2001, D.A. Watt and D.F. Brown. Hash-table principles (1).

levi
Download Presentation

12 Hash-Table Data Structures

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. 12Hash-Table Data Structures • Hash-table principles. • Closed-bucket and open-bucket hash tables. • Searching, insertion, deletion. • Hash-table design. • Implementations of sets and maps using hash tables. © 2001, D.A. Watt and D.F. Brown

  2. Hash-table principles (1) • If a map’s keys are small integers, we can represent the map by a key-indexed array. Search, insertion, and deletion then have time complexity O(1). • Can we approach this performance with keys of other types? Yes! • Hashing: translate each key to a small integer, and use that integer to index an array. • A hash table is an array of mbuckets, together with a hash functionhash(k) that translates each key k to a bucket index (in the range 0…m–1).

  3. hash table(array of buckets) 0 1 2 3 4 5 m–2 m–1 hashing (translating keys to bucket indices) Hash-table principles (2) • Illustration: collision

  4. Hash-table principles (3) • Each key k has a home bucket in the hash table, namely the bucket with index hash(k). • To insert a new entry with key k into the hash table, assign that entry to k’s home bucket. • To search for an entry with key k in the hash table, look in k’s home bucket. • To delete an entry with key k from the hash table, look in k’s home bucket.

  5. Hash-table principles (4) • The hash function must be consistent: k1 = k2 implies hash(k1) = hash(k2). • In general, the hash function is many-to-one. • Therefore different keys may share the same home bucket: k1k2 but hash(k1) = hash(k2). This is called a collision. • Always prefer a hash function that makes collisions relatively infrequent.

  6. Example 1: a hash function for words • Suppose that the keys are English words. • Possible hash function: m = 26hash(w) = (initial letter of w) – ‘A’ • All words with initial letter ‘A’ share bucket 0;…all words with initial letter ‘Z’ share bucket 25. • This is a convenient choice for illustrative purposes. • This is a poor choice for practical purposes: collisions are likely to be frequent in some buckets.

  7. Hashing in Java (1) • Instance method in class Object: publicint hashCode ();// Translate this object to an integer, such that x.equals(y)// implies x.hashcode()==y.hashcode(). • Note that hashCode is consistent. We can use it to implement a hash function for a hash table with m buckets: int hash (Object k) {return Math.abs(k.hashCode()) % m;} Math.abs returns a nonnegative integer. Modulo-m arithmetic then gives an integer in the range 0…m–1.

  8. Hashing in Java (2) • Each subclass of Object should override hashCode. • Examples:

  9. Closed- vs open-bucket hash tables • Closed-bucket hash table: • Each bucket may be occupied by several entries. • Buckets are completely separate. • Open-bucket hash table: • Each bucket may be occupied by at most one entry. • Whenever there is a collision, displace the new entry to another bucket.

  10. Closed-bucket hash tables (1) • Closed-bucket hash table (CBHT): • Each bucket may be occupied by several entries. • Buckets are completely separate. • Simplest implementation: each bucket is an SLL. • In the following illustrations, keys are names of chemical elements. Assume: m = 26hash(e) = (initial letter of e) – ‘A’

  11. 0 Ar 18 1 Br 35 2 Cl 17 3 4 5 F 9 6 7 I 53 8 9 Kr 36 10 11 12 13 Ne 10 23 Xe 54 24 25 Closed-bucket hash tables (2) • Illustration (with no collisions): is represented by

  12. 0 Be 4 Ba 56 1 Ca 20 Cs 55 2 He 2 H 1 7 8 9 K 19 10 Li 3 11 Mg 12 12 13 Na 11 Rb 37 17 18 Sr 38 25 Closed-bucket hash tables (3) • Illustration (with collisions): is represented by

  13. Closed-bucket hash tables (4) • Java class implementing CBHTs: publicclass CBHT { private BucketNode[] buckets; public CBHT (int m) { buckets = new BucketNode[m]; } … // CBHT methods (see below) privateint hash (Object key) {return Math.abs(key.hashCode()) % buckets.length; }

  14. Closed-bucket hash tables (5) • Java class (continued): //////// Inner class for CBHT nodes //////// privatestaticclass BucketNode { private Object key, value;private BucketNode succ; private BucketNode (Object key, Object val, BucketNode succ) {… } }}

  15. CBHT search (1) • CBHT search algorithm: To find which if any node of a CBHT contains an entry whose key is equal to target-key: 1. Set b to hash(target-key).2. Find which if any node of the SLL of bucket b contains an entry whose key is equal to target-key, and terminate with that node as answer.

  16. CBHT search (2) • Implementation (in class CBHT): public BucketNode search (Object targetKey) {int b = hash(targetKey);for (BucketNode curr = buckets[b]; curr != null; curr = curr.succ) {if (targetKey.equals(curr.key))return curr; }returnnull;}

  17. CBHT insertion (1) • CBHT insertion algorithm: To insert the entry (key, val) into a CBHT: 1. Set b to hash(key).2. Insert the entry (key, val) into the SLL of bucket b, replacing any existing entry whose key is key.3. Terminate.

  18. CBHT insertion (2) • Implementation (in class CBHT): publicvoid insert (Object key, Object val) {int b = hash(key);for (BucketNode curr = buckets[b]; curr != null; curr = curr.succ) {if (key.equals(curr.key)) { curr.value = val; return; } } buckets[b] = new BucketNode(key, val, buckets[b]);}

  19. CBHT deletion (1) • CBHT deletion algorithm: To delete the entry (if any) whose key is equal to key from a CBHT: 1. Set b to hash(key).2. Delete the entry (if any) whose key is equal to key from the SLL of bucket b.3. Terminate.

  20. CBHT deletion (2) • Implementation (in class CBHT): publicvoid delete (Object key) {int b = hash(key);for (BucketNode pred = null, curr = buckets[b]; curr != null; pred = curr, curr = curr.succ) {if (key.equals(curr.key)) {if (pred == null) buckets[b] = curr.succ;else pred.succ = curr.succ;return; } }}

  21. CBHTs: analysis • Analysis of the CBHT search/insertion/deletion algorithms (counting comparisons): Let the number of entries be n. • In the best case, no bucket contains more than (say) 2 entries: Max. no. of comparisons = 2 Best-case time complexity is O(1). • In the worst case, one bucket contains all n entries: Max. no. of comparisons = n Worst-case time complexity is O(n).

  22. CBHTs: design • CBHT design consists of: • choosing the number of buckets m • choosing the hash function hash. • Design aims: • collisions are infrequent • entries are distributed evenly among the buckets, such that few buckets contain more than about 2 entries.

  23. CBHTs: choosing the no. of buckets • The load factor of a hash table is the average number of entries per bucket, n/m. • If n is (roughly) predictable, choose m such that the load factor is likely to be between 0.5 and 0.75. • A low load factor wastes space. • A high load factor tends to cause some buckets to have many entries. • Choose m to be a prime number. • Typically the hash function performs modulo-m arithmetic. If m is prime, the entries are more likely to be distributed evenly over the buckets, regardless of any pattern in the keys.

  24. CBHTs: choosing the hash function • The hash function should be efficient (performing few arithmetic operations). • The hash function should distribute the entries evenly among the buckets, regardless of any patterns in the keys. • Possible trade-off: • Speed up the hash function by using only part of the key. • But beware of any patterns in that part of the key.

  25. Example 2: hash table for English words (1) • Suppose that a hash table will contain about 1000 common English words. • Known patterns in the keys: • Letters vary in frequency: • A, E, I, N, S, T are common • Q, X, Z are uncommon. • Word lengths vary in frequency: • word lengths 4–8 are common • other word lengths are less common.

  26. Example 2 (2) • hash(w) can depend on any of w’s letters and/or length. • Consider m = 20, hash(w) = length of w – 1. – Far too few buckets. Load factor = 1000/20 = 50. – Very uneven distribution. • Consider m = 26, hash(w) = initial letter of w – ‘A’. – Far too few buckets. – Very uneven distribution.

  27. Example 2 (3) • Consider m = 520, hash(w) = 26  (length of w – 1) + (initial letter of w – ‘A’). – Too few buckets. Load factor = 1000/520  1.9. – Very uneven distribution. Since few words have length 0–2, buckets 0–51 will be sparsely populated. Since initial letter Z is uncommon, buckets 25, 51, 77, 103, … will be sparsely populated. And so on. • Consider m = 1499, hash(w) = (weighted sum of letters of w) modulo m i.e., (c1 1st letter of w + c2 2nd letter of w + …) modulo m + Good number of buckets. Load factor  0.67. + Reasonably even distribution.

  28. Open-bucket hash tables (1) • Open-bucket hash table (OBHT): • Each bucket may be occupied by at most one entry. • Whenever there is a collision, displace the new entry to another bucket. • Each bucket has three possible states: • never-occupied (has never contained an entry) • occupied (currently contains an entry) • formerly-occupied (previously contained an entry, which has been deleted and not yet replaced).

  29. Open-bucket hash tables (2) • In the following illustrations, keys are names of chemical elements. Assume: m = 26hash(e) = (initial letter of e) – ‘A’ • On a collision, insert the new entry in the next unoccupied bucket (treating the array as cyclic).

  30. 0 Ar 18 1 Br 35 2 Cl 17 3 4 5 F 9 6 7 8 I 53 9 10 Kr 36 11 12 13 Ne 10 14 22 23 Xe 54 24 25 Open-bucket hash tables (3) • Illustration (with no collisions): occupied never-occupied is represented by

  31. 0 1 Be 4 2 Ca 20 cluster 3 Cs 55 4 Ba 56 5 6 7 H 1 8 He 2 9 10 K 19 11 Li 3 cluster 12 Mg 12 13 Na 11 17 Rb 37 18 Sr 38 25 Open-bucket hash tables (4) • Illustration (with collisions): is represented by

  32. Open-bucket hash tables (5) • Java class implementing OHBTs: publicclass OBHT { private BucketEntry[] buckets; public OBHT (int m) { buckets = new BucketEntry[m]; } … privateint hash (Object k) {return Math.abs(k.hashCode()) % buckets.length; } OBHT methods

  33. Open-bucket hash tables (6) • Java class (continued): //////// Inner class for OBHT entries //////// privatestaticclass BucketEntry { private Object key, value; private BucketEntry (Object k, Object v) {this.key = k; this.value = v; } privatestaticfinal BucketEntry FORMER =new BucketEntry(null, null); }}

  34. 0 1 2 3 4 5 6 7 8 9 10 11 12 13 17 18 25 Example 3: populating an OBHT • Animation: Be 4 Ca 20 Cs 55 Ba 56 H 1 He 2 K 19 Li 3 Mg 12 Na 11 Rb 37 Sr 38

  35. OBHT search (1) • OBHT search algorithm: To find which if any bucket of an OBHT is occupied by an entry whose key is equal to target-key: 1. Set b to hash(target-key).2. Repeat: 2.1. If bucket b is never-occupied: 2.1.1. Terminate with answer none. 2.2. If bucket b is occupied by an entry whose key is equal to target-key: 2.2.1. Terminate with answer b. 2.3. If bucket b is formerly-occupied, or is occupied by an entry whose key is not equal to target-key: 2.3.1. Increment b modulo m.

  36. 0 1 Be 4 2 Ca 20 3 Cs 55 4 Ba 56 5 6 7 H 1 8 He 2 9 10 K 19 11 Li 3 12 Mg 12 13 Na 11 17 Rb 37 18 Sr 38 19 25 OBHT search (2) • Illustrations: Searching for Ba: Searching for He: Searching for Mg: Searching for Ra:

  37. OBHT search (3) • Implementation (in class OBHT): publicint search (Object targetKey) {int b = hash(targetKey);for (;;) { BucketEntry old = buckets[b];if (old == null)return NONE;elseif (old != BucketEntry.FORMER && targetKey.equals(old.key))return b;elseb = (b + 1) % buckets.length; }}

  38. OBHT insertion (1) • OBHT insertion algorithm: To insert the entry (key, val) into an OBHT: 1. Set b to hash(key).2. Repeat: 2.1. If bucket b is never-occupied: 2.1.1. If bucket b is the last never-occupied bucket, treat the OBHT as full. 2.1.2. Make bucket b occupied by (key, val). 2.1.3. Terminate. 2.2. If bucket b is formerly-occupied, or is occupied by an entry whose key is equal to key: 2.2.1. Make bucket b occupied by (key, val). 2.2.2. Terminate. 2.3. …

  39. OBHT insertion (2) • OBHT insertion algorithm (continued): 2.3. If bucket b is occupied by an entry whose key is not equal to key: 2.3.1. Increment b modulo m.

  40. 0 0 0 1 1 1 Be 4 Be 4 Be 4 2 2 2 Ca 20 Ca 20 Ca 20 3 3 3 Cs 55 Cs 55 Cs 55 4 4 4 Ba 56 Ba 56 Ba 56 5 5 5 Fr 87 Fr 87 6 6 6 B 5 7 7 7 H 1 H 1 H 1 8 8 8 He 2 He 2 He 2 9 9 9 10 10 10 K 19 K 19 K 19 11 11 11 Li 3 Li 3 Li 3 12 12 12 Mg 12 Mg 12 Mg 12 13 13 13 Na 11 Na 11 Na 11 17 17 17 Rb 37 Rb 37 Rb 37 18 18 18 Sr 38 Sr 38 Sr 38 25 25 25 OBHT insertion (3) • Illustrations: Inserting(B, 5): Inserting(Fr, 87):

  41. OBHT insertion (4) • Implementation (in class OBHT): privateint load = 0; // no. of occupied or formerly- // occupied buckets in this OBHT publicvoid insert (Object key, Object val) { BucketEntry newest =new BucketEntry(key, val);int b = hash(key);for (;;) { BucketEntry old = buckets[b];if (old == null) {if (++load == buckets.length) …; buckets[b] = newest;return;

  42. OBHT insertion (5) • Implementation (continued): } elseif (old == BucketEntry.FORMER || key.equals(old.key)) { buckets[b] = newest;return; } else b = (b + 1) % buckets.length; }}

  43. OBHT deletion (1) • OBHT deletion algorithm: To delete the entry (if any) whose key is equal to key from an OBHT: 1. Set b to hash(key).2. Repeat: 2.1. If bucket b is never-occupied: 2.1.1. Terminate. 2.2. If bucket b is occupied by an entry whose key is equal to key: 2.2.1. Make bucket b formerly-occupied. 2.2.2. Terminate. 2.3. If bucket b is formerly-occupied, or is occupied by an entry whose key is not equal to key: 2.3.1. Increment b modulo m.

  44. 0 0 0 1 1 1 Be 4 Be 4 Be 4 2 2 2 Ca 20 3 3 3 Cs 55 Cs 55 Cs 55 4 4 4 Ba 56 Ba 56 5 5 5 6 6 6 7 7 7 H 1 H 1 H 1 8 8 8 He 2 He 2 He 2 9 9 9 10 10 10 K 19 K 19 K 19 11 11 11 Li 3 Li 3 Li 3 12 12 12 Mg 12 Mg 12 Mg 12 13 13 13 Na 11 Na 11 Na 11 17 17 17 Rb 37 Rb 37 Rb 37 18 18 18 Sr 38 Sr 38 Sr 38 25 25 25 OBHT deletion (2) • Illustrations: Deleting Ba: Deleting Ca: formerly-occupied

  45. OBHT deletion (3) • Implementation (in class OBHT): publicvoid delete (Object key) {int b = hash(key);for (;;) { BucketEntry old = buckets[b];if (old == null)return;elseif (old != BucketEntry.FORMER && key.equals(old.key)) { buckets[b] = BucketEntry.FORMER;return; } else b = (b + 1) % buckets.length; }}

  46. OBHTs: analysis • Analysis of OBHT search/insertion/deletion algorithm (counting comparisons): Let the number of entries be n. • In the best case, no cluster contains more than (say) 4 entries: Max. no. of comparisons = 4 Best-case time complexity is O(1). • In the worst case, one cluster contains all n entries: Max. no. of comparisons = n Worst-case time complexity is O(n).

  47. OBHTs: design • OBHT design consists of: • choosing the number of buckets m • choosing the hash function hash • choosing the step length s (explained later). • Design aims: • collisions are infrequent • entries are distributed evenly over the hash table, such that few clusters contain more than about 4 entries.

  48. OBHTs: choosing the no. of buckets • Recall: The load factor of a hash table is the average number of entries per bucket, n/m. • If n is (roughly) predictable, choose m such that the load factor is likely to be between 0.5 and 0.75. • A low load factor wastes space. • A high load factor tends to result in long clusters. • Choose m to be a prime number.

  49. OBHTs: choosing the hash function • The hash function should be efficient. • The hash function should distribute the entries evenly over the buckets, with few long clusters. • In an OHBT with s = 1, a cluster will form when several entries fall into the same or adjacent buckets.

  50. OBHTs: choosing the step length • To resolve a collision, the search/insertion/deletion algorithm increments the bucket index and tries again. • The step length, s, is the amount by which the bucket index is incremented. So far we have assumed s = 1. • Alternatively, we can use a fixed s > 1. • Choose m to be prime, and choose s to be in the range 2…m–1. • This ensures that s and m have no common factors. • Otherwise, if (say) m = 10 and s = 2, a typical search path would be 6–8–0–2–4, never reaching the remaining buckets!

More Related