11 - Hash Tables II Data Structures and Algorithms Andrei...

Info iconThis preview shows pages 1–8. Sign up to view the full content.

View Full Document Right Arrow Icon
Hash Tables II Data Structures and Algorithms Andrei Bulatov
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Hash Tables II 11-2 Hash Tables In case of collision create a list of elements with the same hash value 1 k key article next ) ( 1 k h ) ( 2 k h 2 k 3 k 4 k 6 k 5 k key article next key article next key article next key article next key article next ) ( 3 k h
Background image of page 2
Algorithms – Hash Tables II 11-3 Good Hash Functions Good hash functions are those that are as close to simple uniform hashing as possible It is difficult to achieve, since we do not know the distribution of keys Note, there are two types of hash functions with absolutely different requirements: - hash functions to support data structures - cryptographic hash functions Assumption: All keys are natural numbers
Background image of page 3

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Hash Tables II 11-4 The Division Method Choose m Then h(k) = k mod m Should be careful with some values of m Say, no powers of 2, or powers of 10, or Primes is a good choice, as long as they are not close to a power of 2
Background image of page 4
Algorithms – Hash Tables II 11-5 The Multiplication Method Choose m Choose A with 0 < A < 1 x mod 1 denotes the fractional part of x, that is x – x Then h(k) = m (kA mod 1) is a convenient value If the size of a computer word is w, choose A to be a fraction like for a integer s To compute h(k), multiply k by The result is a 2w-bit value Then h(k) is then the p most significant bits of p m 2 = w s 2 w A s 2 = 0 1 2 r r w + 0 r
Background image of page 5

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Algorithms – Hash Tables II 11-6 Universal Hashing To guarantee hashing even closer to simple uniform, a natural idea is to choose hash function also at random, independent of the keys being hashed We use universal collection of hash functions A collection H of hash functions is called universal, if for each pair of stinct keys k and l, the number of hash functions h such distinct keys H that h(k) = h(l) is no more than |H|/m To construct a hash table we first select h H (randomly!), and then use it
Background image of page 6
Algorithms – Hash Tables II 11-7 Universal Hashing (cntd) Lemma Suppose a hash function is chosen at random from a universal collection and is used to hash n keys into a table of size m. If key
Background image of page 7

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
Image of page 8
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/19/2009 for the course CS CMPT 307 taught by Professor A.bulatov during the Fall '09 term at Simon Fraser.

Page1 / 27

11 - Hash Tables II Data Structures and Algorithms Andrei...

This preview shows document pages 1 - 8. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online