lec7 - October 3, 2005 Copyright 2001-5 by Erik D. Demaine...

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: October 3, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.1 Introduction to Algorithms 6.046J/18.401J L ECTURE 7 Hashing I Direct-access tables Resolving collisions by chaining Choosing hash functions Open addressing Prof. Charles E. Leiserson October 3, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.2 Symbol-table problem Symbol table S holding n records : record x key [ x ] key [ x ] Other fields containing satellite data Operations on S : I NSERT ( S , x ) D ELETE ( S , x ) S EARCH ( S , k ) How should the data structure S be organized? October 3, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.3 Direct-access table I DEA : Suppose that the keys are drawn from the set U {0, 1, , m 1} , and keys are distinct. Set up an array T [0 . . m 1] : T [ k ] = x if x K and key [ x ] = k , NIL otherwise. Then, operations take (1) time. Problem: The range of keys can be large: 64-bit numbers (which represent 18,446,744,073,709,551,616 different keys), character strings (even larger!). October 3, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.4 Hash functions Solution: Use a hash function h to map the universe U of all keys into {0, 1, , m 1} : U S k 1 k 2 k 3 k 4 k 5 m 1 h ( k 1 ) h ( k 4 ) h ( k 2 ) h ( k 3 ) T = h ( k 5 ) As each key is inserted, h maps it to a slot of T . When a record to be inserted maps to an already occupied slot in T , a collision occurs. October 3, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.5 Resolving collisions by chaining Link records in the same slot into a list. h (49) = h (86) = h (52) = i T 49 49 86 86 52 52 Worst case: Every key hashes to the same slot. Access time = ( n ) if | S | = n i October 3, 2005 Copyright 2001-5 by Erik D. Demaine and Charles E. Leiserson L7.6 Average-case analysis of chaining We make the assumption of simple uniform hashing: Each key k S is equally likely to be hashed to any slot of table T , independent of where other keys are hashed. Let n be the number of keys in the table, and let m be the number of slots....
View Full Document

Page1 / 26

lec7 - October 3, 2005 Copyright 2001-5 by Erik D. Demaine...

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

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