Download presentation

Presentation is loading. Please wait.

Published byFrank Hadley Modified over 2 years ago

1
Hash Tables 1 0 1 2 3 4 451-229-0004 981-101-0002 025-612-0001 451-229-0004 981-101-0002 200-751-9998 025-612-0001 … Helen, 40, Taipei, … Gary, 35, Yunlin, … Many, 20, Chiayi, … Tom, 30, Douliou, … … How to store and retrieve a set of key-value pairs? key value implemenation by link list

2
Recall the Map ADT 2 Map ADT methods: – size(), isEmpty() – get(k): if the map M has an entry with key k, return its assoiciated value; else, return null – put(k, v): insert entry (k, v) into the map M; if key k is not already in M, then return null; else, return old value associated with k – remove(k): if the map M has an entry with key k, remove it from M and return its associated value; else, return null – keys(): return an iterator of the keys in M – values(): return an iterator of the values in M Disadvantage with list-based implementation Map: Key-value entries (k 1, v 1 ), (k 2, v 2 ), (k 3, v 3 ), …

3
Example of Hashing We design a hash table for a map storing entries as (SSN, Name), where SSN (social security number) is a nine-digit positive integer Our hash table uses an array of size N 10,000 and the hash function h(k) last four digits of k 3 0 1 2 3 4 9997 9998 9999 … 451-229-0004 981-101-0002 200-751-9998 025-612-0001 451-229-0004 981-101-0002 200-751-9998 025-612-0001 … To store and retrieve entries: h(k)h(k) hash table

4
Hash Functions and Hash Tables Example: h(k) k mod N is a hash function for integer keys The integer h(k) is called the hash value of key k A hash table for a given key type consists of – Array (called table) of size N – Hash function h When implementing a map with a hash table, the goal is to store item (k, o) at index i h(k) 4 A hash function h maps keys of a given type to integers in a fixed interval [0, N 1] i h ( k )

5
5

6
Hash Functions A hash function is usually specified as the composition of two functions: Hash code: h 1 : keys integers Compression function: h 2 : integers [0, N 1] The hash code is applied first, and the compression function is applied next on the result, i.e., h(k) = h 2 (h 1 (k)) The goal of the hash function is to disperse the keys in an apparently random way 6 y = h 1 (key) h2(y)h2(y) i h ( k )

7
Various Hash Codes Memory address: – We reinterpret the memory address of the key object as an integer (default hash code of all Java objects) – Good in general, except for numeric and string keys Integer cast: – We reinterpret the bits of the key as an integer – Suitable for keys of length less than or equal to the number of bits of the integer type (e.g., byte, short, int and float in Java) – E.g. Float.floatToIntBits(x) for a variable x of base type float Component sum: – We partition the bits of the key into components of fixed length (e.g., 16 or 32 bits) and we sum the components (ignoring overflows) – Suitable for numeric keys of fixed length greater than or equal to the number of bits of the integer type (e.g., long and double in Java) 7 static int hashCode(long i) {return (int)((i >> 32) + (int) i);}

8
Polynomial Hash Codes Polynomial accumulation: – We partition the bits of the key into a sequence of components of fixed length (e.g., 8, 16 or 32 bits) x 0 x 1 … x k 1 – We evaluate the polynomial p(a) x 0 a k 1 x 1 a k … x k-2 a x k 1 at a fixed value a, ignoring overflows – Especially suitable for strings (e.g., the choice a 33 gives at most 6 collisions on a set of 50,000 English words) Polynomial p(a) can be evaluated in O(n) time using Horners rule: – The following polynomials are successively computed, each from the previous one in O(1) time p 0 (a) x p i (a) x i ap i 1 (a) (i 1, 2, …, k 1) We have p(a) p k 1 (a) 8 Tom Mary Helen John T(33) 2 +o(33) 1 +m

9
Cyclic Shift Hash Codes 9 static int hashCode(String s) { int h=0; for (int i=0; i

10
10 Note: 5-bit cyclic shift has the best performance for a list over 25,000 English words 5-bit

11
Compression Functions Division: – h 2 (y) y mod N – The size N of the hash table is usually chosen to be a prime – The reason has to do with number theory and is beyond the scope of this course Multiply, Add and Divide (MAD): – h 2 (y) [(ay b) mod p] mod N – p is a prime number larger than N – a and b are integers chosen at random from [0, p 1], with a > 0 11 y = h 1 (key) h2(y)h2(y)

12
Collision Handling Collisions occur when different elements are mapped to the same cell, i.e., h(k 1 ) = h(k 2 ) Solutions – separate chaining – open addressing (i.e., find a different cell) linear probing quadratic probling double hashing 12 0 1 2 3 4 025-612-0001 451-229-0004 981-101-0004 h(k2)h(k2) h(k1)h(k1)

13
Separate Chaining 13 Let each cell in the table point to a linked list of entries that map there Separate chaining is simple, but requires additional memory outside the table h(k) = k mod 13

14
Map Methods with Separate Chaining used for Collisions Algorithm get(k): Output: The value associated with the key k in the map, or null if there is no entry with key equal to k in the map return A[h(k)].get(k) {delegate the get to the list-based map at A[h(k)]} Algorithm put(k,v): Output: If there is an existing entry in our map with key equal to k, then we return its value (replacing it with v); otherwise, we return null t = A[h(k)].put(k,v) {delegate the put to the list-based map at A[h(k)]} if t = null then {k is a new key} n = n + 1 return t Algorithm remove(k): Output: The (removed) value associated with key k in the map, or null if there is no entry with key equal to k in the map t = A[h(k)].remove(k) {delegate the remove to the list-based map at A[h(k)]} if t null then {k was found} n = n - 1 return t 14

15
Linear Probing 15 h ( k ) = k mod 11 k = 15

16
Linear Probing Open addressing: the colliding item is placed in a different cell of the table Linear probing handles collisions by placing the colliding item in the next (circularly) available table cell Each table cell inspected is referred to as a probe Colliding items lump together, causing future collisions to cause a longer sequence of probes; that is, the order of A[ i ], A[( i +1) mod N ], A[( i +2) mod N ], … where i = h ( k ) Example: – h(k) k mod 13 – Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order 16 0123456789101112 41 18445932223173 0123456789101112

17
Search with Linear Probing Consider a hash table A that uses linear probing get (k) – We start at cell h(k) – We probe consecutive locations until one of the following occurs An item with key k is found, or An empty cell is found, or N cells have been unsuccessfully probed 17 Algorithm get(k) i h(k) p 0 repeat c A[i] if c return null else if c.key () k return c.element() else i (i 1) mod N p p 1 until p N return null

18
Updates with Linear Probing To handle insertions and deletions, we introduce a special object, called AVAILABLE, which replaces deleted elements remove (k) – We search for an entry with key k – If such an entry (k, o) is found, we replace it with the special item AVAILABLE and we return element o – Else, we return null put (k, o) – We throw an exception if the table is full – We start at cell h(k) – We probe consecutive cells until one of the following occurs A cell i is found that is either empty or stores AVAILABLE, or N cells have been unsuccessfully probed – We store entry (k, o) in cell i 18 41 184459 22 0123456789101112 remove (22)

19
Quadratic Probing Iteratively trying the buskets A[(i+f(j)) mod N], for j = 0, 1, 2, …, where f(j) = j 2 19 0123456789101112 3141 184459 7322 32 0123456789101112 Example: – h(k) k mod 13 – Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order quadratic probing linear probing

20
Double Hashing If A[ i ], with i = h ( k ) is already occupied, double hashing uses a secondary hash function h(k) and handles collisions by placing an item in the first available cell of the series The secondary hash function h(k) cannot have zero values The table size N must be a prime to allow probing of all the cells Common choice of compression function for the secondary hash function: h ( k ) q (k mod q) where q N and q is a prime The possible values for h(k) are 1, 2, …, q 20 A[(i f(j)) mod N] for j 1, 2, 3, …, where f(j) = j*h(k). Collision handling: A[(i f(j)) mod N] where i = h(k) for j 1, 2, 3, …, where f(j) = j*h(k)

21
Example of Double Hashing Consider a hash table storing integer keys that handles collision with double hashing – N 13 – h(k) k mod 13 – h(k) 7 (k mod 7) Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order 21 31 41 183259732244 0123456789101112 0123456789101112 Collision handling: A[(i f(j)) mod N] where i = h(k) for j 1, 2, 3, …, where f(j) = j*h(k) h ( k ) q (k mod q) where q N and q is a prime

22
Performance of Hashing In the worst case, searches, insertions and removals on a hash table take O(n) time The worst case occurs when all the keys inserted into the map collide The load factor n N affects the performance of a hash table Suggest: < 0.5 for open addressing schemes and < 0.9 for separate chaining If load factor goes significantly above the threshold, then rehashing to a new table is performed Assuming that the hash values are like random numbers, it can be shown that the expected number of probes for an insertion with open addressing is ( 1 ( 1 ) ) The expected running time of all the dictionary ADT operations in a hash table is O(1) In practice, hashing is very fast provided the load factor is not close to 100% Applications of hash tables: – small databases – compilers – browser caches 22 1 … … n n N Hashing

23
9.2.6 A Java Hash Table Implementation 23 /** A hash table with linear probing and the MAD hash function */ import java.util.Iterator; public class HashTableMap implements Map { public static class HashEntry implements Entry { protected K key; protected V value; public HashEntry(K k, V v) { key = k; value = v; } public V getValue() { return value; } public K getKey() { return key; } public V setValue(V val) { V oldValue = value; value = val; return oldValue; } public boolean equals(Object o) { HashEntry ent; try { ent = (HashEntry ) o; } catch (ClassCastException ex) { return false; } return (ent.getKey() == key) && (ent.getValue() == value); }

24
24 protected Entry AVAILABLE = new HashEntry (null, null); // marker protected int n = 0; // number of entries in the dictionary protected int capacity; // capacity of the bucket array protected Entry [] bucket; // bucket array protected int scale, shift; // the shift and scaling factors /** Creates a hash table with initial capacity 1023. */ public HashTableMap() { this(1023); } /** Creates a hash table with the given capacity. */ public HashTableMap(int cap) { capacity = cap; bucket = (Entry []) new Entry[capacity]; // safe cast java.util.Random rand = new java.util.Random(); scale = rand.nextInt(capacity-1) + 1; shift = rand.nextInt(capacity); } /** Determines whether a key is valid. */ protected void checkKey(K k) { if (k == null) throw new InvalidKeyException("Invalid key: null."); } /** Hash function applying MAD method to default hash code. */ public int hashValue(K key) { return (int) ((Math.abs(key.hashCode()*scale + shift) % prime) % capacity); }

25
25 /** Returns the number of entries in the hash table. */ public int size() { return n; } /** Returns whether or not the table is empty. */ public boolean isEmpty() { return (n == 0); } /** Returns an iterable object containing all of the keys. */ public Iterable keys() { PositionList keys = new NodePositionList (); for (int i=0; i

26
26 /** Helper search method - returns index of found key or -(a + 1), * where a is the index of the first empty or available slot found. */ protected int findEntry(K key) throws InvalidKeyException { int avail = -1; checkKey(key); int i = hashValue(key); int j = i; do { Entry e = bucket[i]; if ( e == null) { if (avail < 0) avail = i;// key is not in table break; } if (key.equals(e.getKey())) // we have found our key return i;// key found if (e == AVAILABLE) {// bucket is deactivated if (avail < 0) avail = i;// remember that this slot is available } i = (i + 1) % capacity;// keep looking } while (i != j); return -(avail + 1); // first empty or available slot } findEntry(15 )

27
27 /** Returns the value associated with a key. */ public V get (K key) throws InvalidKeyException { int i = findEntry(key); // helper method for finding a key if (i < 0) return null; // there is no value for this key return bucket[i].getValue(); // return the found value in this case } /** Put a key-value pair in the map, replacing previous one if it exists. */ public V put (K key, V value) throws InvalidKeyException { int i = findEntry(key); //find the appropriate spot for this entry if (i >= 0)// this key has a previous value return ((HashEntry ) bucket[i]).setValue(value); // set new value if (n >= capacity/2) { rehash(); // rehash to keep the load factor <= 0.5 i = findEntry(key); //find again the appropriate spot for this entry } bucket[-i-1] = new HashEntry (key, value); // convert to proper index n++; return null; // there was no previous value } put(15, Value)

28
28 /** Doubles the size of the hash table and rehashes all the entries. */ protected void rehash() { capacity = 2*capacity; Entry [] old = bucket; bucket = (Entry []) new Entry[capacity]; // new bucket is twice as big java.util.Random rand = new java.util.Random(); scale = rand.nextInt(capacity-1) + 1; // new hash scaling factor shift = rand.nextInt(capacity); // new hash shifting factor for (int i=0; i

29
9.2.8 Applications: Counting Word Frequencies in a Document 29 import java.io.*; import java.util.Scanner; import net.datastructures.*; /** A program that counts words in a document, printing the most frequent. */ public class WordCount { public static void main(String[] args) throws IOException { Scanner doc = new Scanner(System.in); doc.useDelimiter("[^a-zA-Z]"); // ignore non-letters; any characters except a-zA-Z HashTableMap h = new HashTableMap (); String word; Integer count ; Entry: (word, count)

30
9.2.8 Applications: Counting Word Frequencies 30 while (doc.hasNext()) { word = doc.next(); if (word.equals("")) continue; // ignore null strings between delimiters word = word.toLowerCase(); // ignore case count = h.get(word); // get the previous count for this word if (count == null) h.put(word, 1); // autoboxing allows this else h.put(word, ++count); // autoboxing/unboxing allows this } int maxCount = 0; String maxWord = "no word"; for (Entry ent : h.entries()) { // find max-count word if (ent.getValue() > maxCount) { maxWord = ent.getKey(); maxCount = ent.getValue(); } System.out.print("The most frequent word is \"" + maxWord); System.out.println(",\" with word-count = " + maxCount + "."); }

Similar presentations

© 2017 SlidePlayer.com Inc.

All rights reserved.

Ads by Google