Download presentation

Presentation is loading. Please wait.

Published byFrank Hadley Modified over 3 years ago

1
Dictionaries 4/1/2017 2:36 PM Hash Tables 1 2 3 4 … 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 Map ADT methods:**

Dictionaries Recall the Map ADT 4/1/2017 2:36 PM 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 (k1, v1), (k2, v2), (k3, v3), …

3
**To store and retrieve entries:**

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 1 2 3 4 9997 9998 9999 … To store and retrieve entries: … h(k) hash table

4
**Hash Functions and Hash Tables**

Dictionaries Hash Functions and Hash Tables 4/1/2017 2:36 PM A hash function h maps keys of a given type to integers in a fixed interval [0, N - 1] i = h(k) 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)

5
Dictionaries 4/1/2017 2:36 PM

6
**Hash Functions Hash code: h1: keys integers**

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

7
**Various Hash Codes Memory address: Integer cast: Component sum:**

Dictionaries Various Hash Codes 4/1/2017 2:36 PM 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) static int hashCode(long i) {return (int)((i >> 32) + (int) i);}

8
**Polynomial Hash Codes Polynomial accumulation:**

Dictionaries Polynomial Hash Codes 4/1/2017 2:36 PM Polynomial accumulation: We partition the bits of the key into a sequence of components of fixed length (e.g., 8, 16 or 32 bits) x0 x1 … xk-1 We evaluate the polynomial p(a) = x0 ak-1+ x1 ak-2 + … xk-2 a + xk-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 Horner’s rule: The following polynomials are successively computed, each from the previous one in O(1) time p0(a) = x0 pi (a) = xi + api-1(a) (i = 1, 2, …, k -1) We have p(a) = pk-1(a) Tom Mary Helen John ‘T’(33)2+’o’(33)1+’m’

9
**Cyclic Shift Hash Codes**

Dictionaries Cyclic Shift Hash Codes 4/1/2017 2:36 PM A variant of the polynomial hash code replaces multiplication by a with a cyclic shift of a partial sum by a certain number of bits. static int hashCode(String s) { int h=0; for (int i=0; i<s.length(); i++) { h = (h << 5) | (h >>> 27); // 5-bit cyclic shift of the running sum h += (int) s.charAt(i); // add in next character } return h; Note: “<<” cyclic left shift “>>>” shift bits right, filling in with zeros

10
Dictionaries 4/1/2017 2:36 PM Note: 5-bit cyclic shift has the best performance for a list over 25,000 English words 5-bit

11
**Compression Functions**

Dictionaries 4/1/2017 2:36 PM Compression Functions y = h1(key) Division: h2 (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): h2 (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, p1], with a > 0 h2(y)

12
Collision Handling Collisions occur when different elements are mapped to the same cell, i.e., h(k1) = h(k2) Solutions separate chaining open addressing (i.e., find a different cell) linear probing quadratic probling double hashing 1 2 3 4 h(k1) h(k2)

13
Dictionaries Separate Chaining 4/1/2017 2:36 PM 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**

Dictionaries 4/1/2017 2:36 PM 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

15
Dictionaries Linear Probing 4/1/2017 2:36 PM h(k) = k mod 11 k = 15

16
**Linear Probing Example:**

Dictionaries Linear Probing 4/1/2017 2:36 PM 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 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 59 32 22 31 73

17
**Search with Linear Probing**

Dictionaries Search with Linear Probing 4/1/2017 2:36 PM 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 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

18
**Updates with Linear Probing**

Dictionaries Updates with Linear Probing 4/1/2017 2:36 PM 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 remove(22) 41 18 44 59 22 1 2 3 4 5 6 7 8 9 10 11 12

19
**Quadratic Probing Iteratively trying the buskets**

Dictionaries Quadratic Probing 4/1/2017 2:36 PM Iteratively trying the buskets A[(i+f(j)) mod N], for j = 0, 1, 2, …, where f(j) = j2 Example: h(k) = k mod 13 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order 1 2 3 4 5 6 7 8 9 10 11 12 31 41 18 44 59 73 22 32 quadratic probing linear probing

20
**A[(i + f(j)) mod N] for j = 1, 2, 3, …, where f(j) = j*h’(k).**

Dictionaries Double Hashing 4/1/2017 2:36 PM 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 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**

Dictionaries Example of Double Hashing 4/1/2017 2:36 PM 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 1 2 3 4 5 6 7 8 9 10 11 12 31 41 18 32 59 73 22 44 1 2 3 4 5 6 7 8 9 10 11 12 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**

Dictionaries Performance of Hashing 4/1/2017 2:36 PM 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 1 … … n = n/N Hashing

23
**9.2.6 A Java Hash Table Implementation**

Dictionaries 9.2.6 A Java Hash Table Implementation 4/1/2017 2:36 PM /** A hash table with linear probing and the MAD hash function */ import java.util.Iterator; public class HashTableMap<K,V> implements Map<K,V> { public static class HashEntry<K,V> implements Entry<K,V> { 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<K,V> ent; try { ent = (HashEntry<K,V>) o; } catch (ClassCastException ex) { return false; } return (ent.getKey() == key) && (ent.getValue() == value);

24
**protected int n = 0; // number of entries in the dictionary **

Dictionaries 4/1/2017 2:36 PM protected Entry<K,V> AVAILABLE = new HashEntry<K,V>(null, null); // marker protected int n = 0; // number of entries in the dictionary protected int capacity; // capacity of the bucket array protected Entry<K,V>[] bucket; // bucket array protected int scale, shift; // the shift and scaling factors /** Creates a hash table with initial capacity */ public HashTableMap() { this(1023); } /** Creates a hash table with the given capacity. */ public HashTableMap(int cap) { capacity = cap; bucket = (Entry<K,V>[]) 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
**/** Returns the number of entries in the hash table. */ **

Dictionaries 4/1/2017 2:36 PM /** 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<K> keys() { PositionList<K> keys = new NodePositionList<K>(); for (int i=0; i<capacity; i++) if ((bucket[i] != null) && (bucket[i] != AVAILABLE)) keys.addLast(bucket[i].getKey()); return keys; }

26
Dictionaries /** 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<K,V> 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 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 4/1/2017 2:36 PM findEntry(15)

27
**/** Returns the value associated with a key. */ **

Dictionaries /** 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<K,V>) 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<K,V>(key, value); // convert to proper index n++; return null; // there was no previous value 4/1/2017 2:36 PM put(15, Value)

28
**/** Removes the key-value pair with a specified key. */**

Dictionaries 4/1/2017 2:36 PM /** Doubles the size of the hash table and rehashes all the entries. */ protected void rehash() { capacity = 2*capacity; Entry<K,V>[] old = bucket; bucket = (Entry<K,V>[]) 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<old.length; i++) { Entry<K,V> e = old[i]; if ((e != null) && (e != AVAILABLE)) { // a valid entry int j = findEntry(e.getKey()); bucket[j] = e; } /** Removes the key-value pair with a specified key. */ public V remove (K key) throws InvalidKeyException { int i = findEntry(key); // find this key first if (i < 0) return null; // nothing to remove V toReturn = bucket[i].getValue(); bucket[i] = AVAILABLE; // mark this slot as deactivated n--; return toReturn;

29
**9.2.8 Applications: Counting Word Frequencies in a Document**

Dictionaries 4/1/2017 2:36 PM 9.2.8 Applications: Counting Word Frequencies in a Document 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<String,Integer> h = new HashTableMap<String,Integer>(); String word; Integer count; Entry: (word, count)

30
**9.2.8 Applications: Counting Word Frequencies**

Dictionaries 9.2.8 Applications: Counting Word Frequencies 4/1/2017 2:36 PM 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<String,Integer> 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

Presentation is loading. Please wait....

OK

Hashing.

Hashing.

© 2018 SlidePlayer.com Inc.

All rights reserved.

Ads by Google

Ppt on blood stain pattern analysis definition Download ppt on multimedia and animation Ppt on semiconductor diodes Ppt on standing order act registration Ppt on magnetohydrodynamic power generation How to present a seminar ppt on 4g Ppt on diode as rectifier regulator Ppt on history of atoms Upload and view ppt online shopping Ppt on satellite orbital slots