290 likes | 453 Views
ΜΑΘΗΜΑ 10 ο. Πίνακες Κατακερματισμού Υλικό εκτός εξετάσιμης ύλης για την χρονιά 2007-2008. Hashing Lecture 10. hashing methods and their implementation hash tables scatter tables.
E N D
ΜΑΘΗΜΑ 10ο Πίνακες Κατακερματισμού Υλικό εκτός εξετάσιμης ύλης για την χρονιά 2007-2008.
HashingLecture 10 • hashing methods and their implementation • hash tables • scatter tables Το υλικό για αυτή τη διάλεξη είναι από το βιβλίο «Data Structures and Algorithms with Object Oriented Design Patterns in Java”, Bruno R. Preiss, John Willey and Sons.
Rationale for Hashing • many applications: information store & retrieval • consider containers storing {key, value} pairs • hashing-based containers suitable for applications with • frequently executed basic operations [find(key), insert] • items are not required to be ordered • main advantage: find, insert are O(1) (average case)
Hashing Methods Section 8.1-3 • basic idea • hash functions • characteristics • common methods • dealing with arbitrary keys
Array-Based Implementation of the Container • for clarity of presentation, consider the case when the container contains keys only • an array will hold some number of items of a given set of keys K • the position of a key in the array is given by a hashfunctionh(·) • in general |K|is large or even unbounded • the actual number of items stored in the container is typically much less than |K| use an array of size M
Hash Functions • we need a function
Characteristics of a Good Hash Function • good hash function • avoids collisions • spreads keys evenly in the array • its computation is fast
Hashing Methods • division method: h(x) = x mod M • middle-square method W=2word length • multiplication method • Fibonacci hashing
Implementing the Division Method public class DivisionMethod { static final int M = 1031; // a prime public static int h (int x) { return Math.abs (x) % M; } }
Implementing the Multiplication & Fibonacci Methods public class MultiplicationMethod { static final int k = 10; // M==1024 static final int w = 32; static final int a = (int) 2654435769L; public static int h(int x) { return (x * a) >>> (w - k); } }
Dealing With Arbitrary Keys illustration • What if keys are not integers? • function f maps keys into non-negative integers: • function g maps non-negative integers into • hash function h is simply h = f g
Hashing of Strings • view a character string, s, as a sequence of n characters, {s0, s1, . . . , sn-1} • simple (but very poor) hash function is • e.g., for all possible a ASCII strings of length five 0 ≤ f(s) ≤ 640
Better String Hash Function • suppose we known a priori that all strings have length four • let B = 256 • we can compute the 32-bit quantity like this f(s) = s0B3 +s1B2 +s2B +s0 • this function spreads out the values better
Dealing with Arbitrary Length Strings • generalize the preceding approach: • computes a unique integer for every possible string • unfortunately the range of f(s) is unbounded • a simple modification bounds the range:
Hash Tables Section 8.4 • hash tables • main difference: dealing with collisions • separate chaining hash tables [illustration] • running time analysis • worst case, average case • scatter tables [illustration] • chained, open addressing • running time analysis
Hash Tables • a hash table is a searchable container that implements the HashTable interface • the AbstractHashTable class is an abstract class from which various implementations are derived – method getLength (returns M) – methods f, g, and h (h = f g) • the implementation shown uses the division method of hashing
Separate Chaining Hash Table • uses separate chaining to resolve collisions • hash table is implemented as an array of linked lists • linked list into which an item is to b inserted is determined by hashing that item • item is inserted into the linked list by appending it
Example: Keys and their Hash Values Prog 8.3(octal)
ChainedHashTable Running Time (Worst Case) constructor O(M) purge O(M) insert T <hashCode> + O(1) withdraw T <hashCode> + O(n) find O(1) +T<hashCode> +nT <isEQ> +O(n)
Average Case Analysis • consider we have a hash table of size M • let there be exactly n items in the hash table • the quantity λ = n/M is called the load factor • the load factor is the average length of a linked list!
Average Running Times • unsuccessful search O(1)+T<hashCode>+λT <isEQ>+O(λ) • successful search O(1)+T<hashCode>+((λ+1)/2)T<isEQ>+O(λ) • if one could guarantee λ ≤ 1, thenT <hashCode>= O(1) and T <isEQ>= O(1), consequently all operations would be O(1) [average] • to guarantee this, must resize