This preview shows pages 1–11. Sign up to view the full content.
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 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: Introduction to Algorithms 6.046J/18.401J Lecture 7 Prof. Piotr Indyk Introduction to Algorithms September 29, 2004 L7.2 Charles Leiserson and Piotr Indyk Data Structures Role of data structures: Encapsulate data Support certain operations (e.g., INSERT , DELETE , SEARCH ) What data structures do we know already ? Yes, heap : INSERT(x) DELETEMIN Introduction to Algorithms September 29, 2004 L7.3 Charles Leiserson and Piotr Indyk Dictionary problem Dictionary T holding n records : key [ x ] key [ x ] record x Other fields containing satellite data Operations on T : I NSERT ( T , x ) D ELETE ( T , x ) S EARCH ( T , k ) How should the data structure T be organized? Introduction to Algorithms September 29, 2004 L7.4 Charles Leiserson and Piotr Indyk Assumptions Assumptions: The set of keys is K U = {0, 1, , u 1} Keys are distinct What can we do ? Introduction to Algorithms September 29, 2004 L7.5 Charles Leiserson and Piotr Indyk Direct access table Create a table T [0 u1]: Benefit: Each operation takes constant time Drawbacks: The range of keys can be large: 64bit numbers (which represent 18,446,744,073,709,551,616 different keys), character strings (even larger!) T [ k ] = x if k K and key [ x ] = k , NIL otherwise. Introduction to Algorithms September 29, 2004 L7.6 Charles Leiserson and Piotr Indyk As each key is inserted, h maps it to a slot of T . Hash functions Solution: Use a hash function h to map the universe U of all keys into {0, 1, , m 1} : U K k 1 k 2 k 3 k 4 k 5 m 1 h ( k 1 ) h ( k 4 ) h ( k 2 ) h ( k 3 ) When a record to be inserted maps to an already occupied slot in T , a collision occurs. T = h ( k 5 ) Introduction to Algorithms September 29, 2004 L7.7 Charles Leiserson and Piotr Indyk Collisions resolution by chaining Records in the same slot are linked into a list. h (49) = h (86) = h (52) = i T 49 49 86 86 52 52 i Introduction to Algorithms September 29, 2004 L7.8 Charles Leiserson and Piotr Indyk Hash functions Designing good functions is quite non trivial For now, we assume they exist. Namely, we assume simple uniform hashing: Each key k K of keys is equally likely to be hashed to any slot of table T , independent of where other keys are hashed Introduction to Algorithms September 29, 2004 L7.9 Charles Leiserson and Piotr Indyk Analysis of chaining Let n be the number of keys in the table, and let m be the number of slots. Define the load factor of T to be = n / m = average number of keys per slot. Introduction to Algorithms September 29, 2004 L7.10 Charles Leiserson and Piotr Indyk Search cost Expected time to search for a record with a given key = apply hash function and access slot search the list Expected search time = (1) if = O (1) , or equivalently, if n = O ( m ) ....
View
Full
Document
This note was uploaded on 04/10/2008 for the course CSE 6.046J/18. taught by Professor Piotrindykandcharlese.leiserson during the Fall '04 term at MIT.
 Fall '04
 PiotrIndykandCharlesE.Leiserson
 Algorithms, Data Structures

Click to edit the document details