30
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 implemenati on by link list

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,

Embed Size (px)

Citation preview

Page 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,

Hash Tables

1

01234 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

Page 2: 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,

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(k1, v1), (k2, v2), (k3, v3), …

Page 3: 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,

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 N10,000 and the hash functionh(k)last four digits of k

3

01234

999799989999

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)

hash table

Page 4: 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,

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, N1]

i h(k)

Page 5: 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,

5

Page 6: 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,

Hash Functions• A hash function is usually

specified as the composition of two functions:

Hash code: h1: keys integers

Compression function: h2: integers [0, N1]

• 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

6

y = h1(key)

h2(y)

i h(k)

Page 7: 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,

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);}

Page 8: 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,

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) x0 x1 … xk1

– We evaluate the polynomialp(a) x0 ak1 x1 ak

… xk-2 a xk1

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) timep0(a) x

pi (a) xi api1(a) (i 1, 2, …, k 1)

• We have p(a) pk1(a)

8

TomMaryHelenJohn

‘T’(33)2+’o’(33)1+’m’

Page 9: 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,

Cyclic Shift Hash Codes

9

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

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.

Page 10: 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,

10

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

5-bit

Page 11: 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,

Compression Functions

• 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

11

y = h1(key)

h2(y)

Page 12: 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,

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

12

01234

025-612-0001

451-229-0004

981-101-0004 h(k2)

h(k1)

Page 13: 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,

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

Page 14: 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,

Map Methods with Separate Chaining used for CollisionsAlgorithm 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 mapreturn 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 nullt = 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 + 1return 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 mapt = 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 - 1return t

14

Page 15: 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,

Linear Probing

15

h(k) = k mod 11k = 15

Page 16: 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,

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 ofA[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

0 1 2 3 4 5 6 7 8 9 10 11 12

41 18445932223173 0 1 2 3 4 5 6 7 8 9 10 11 12

Page 17: 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,

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 0repeat

c A[i]if c

return null else if c.key () k

return c.element()else

i (i 1) mod Np p 1

until p Nreturn null

Page 18: 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,

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 0 1 2 3 4 5 6 7 8 9 10 11 12

remove(22)

Page 19: 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,

Quadratic Probing• Iteratively trying the buskets

A[(i+f(j)) mod N], for j = 0, 1, 2, …, where f(j) = j2

19

0 1 2 3 4 5 6 7 8 9 10 11 12

3141 184459 7322 32 0 1 2 3 4 5 6 7 8 9 10 11 12

• Example:– h(k) k mod 13

– Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order

quadratic probing

linear probing

Page 20: 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,

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)

Page 21: 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,

Example of Double Hashing

• Consider a hash table storing integer keys that handles collision with double hashing– N13 – 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 0 1 2 3 4 5 6 7 8 9 10 11 12

0 1 2 3 4 5 6 7 8 9 10 11 12

k h (k ) h' (k ) Probes18 5 3 541 2 1 222 9 6 944 5 5 5 1059 7 4 732 6 3 631 5 4 5 9 073 8 4 8

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

Page 22: 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,

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 nN 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

nN Hashin

g

Page 23: 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,

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<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); } }

Page 24: 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,

24

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 1023. */ 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); }

Page 25: 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,

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<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; }

Page 26: 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,

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<K,V> e = bucket[i]; if ( e == null) {

if (avail < 0) avail = i; // key is not in tablebreak;

} 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)

Page 27: 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,

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<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 }

put(15, Value)

Page 28: 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,

28

/** 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 = - 1 - 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; }}

Page 29: 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,

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<String,Integer> h = new HashTableMap<String,Integer>(); String word; Integer count;

Entry: (word, count)

Page 30: 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,

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<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 + "."); }}