This preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full DocumentThis preview has intentionally blurred sections. Sign up to view the full version.
View Full Document
Unformatted text preview: October 3, 2005 Copyright © 20015 by Erik D. Demaine and Charles E. Leiserson L7.1 Introduction to Algorithms 6.046J/18.401J L ECTURE 7 Hashing I • Directaccess tables • Resolving collisions by chaining • Choosing hash functions • Open addressing Prof. Charles E. Leiserson October 3, 2005 Copyright © 20015 by Erik D. Demaine and Charles E. Leiserson L7.2 Symboltable 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 © 20015 by Erik D. Demaine and Charles E. Leiserson L7.3 Directaccess 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: • 64bit numbers (which represent 18,446,744,073,709,551,616 different keys), • character strings (even larger!). October 3, 2005 Copyright © 20015 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 © 20015 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 © 20015 by Erik D. Demaine and Charles E. Leiserson L7.6 Averagecase 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
This note was uploaded on 10/19/2010 for the course CS 477 taught by Professor Gewali during the Spring '08 term at University of Nevada, Las Vegas.
 Spring '08
 Gewali
 Algorithms

Click to edit the document details