lecture07

# lecture07 - Introduction to Algorithms...

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

Introduction to Algorithms 6.046J/18.401J/SMA5503 Lecture 7 Prof. Charles E. Leiserson

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

View Full Document
Introduction to Algorithms Day 11 L7.2 © 2001 by Charles E. Leiserson Symbol-table problem Symbol table T holding n records : 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 Day 11 L7.3 © 2001 by Charles E. Leiserson Direct-access table I DEA : Suppose that the set of keys is K {0, 1, …, m –1} , and keys are distinct. Set up an array T [0 . . m –1] : T [ k ] = x if k 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!).

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

View Full Document
Introduction to Algorithms Day 11 L7.4 © 2001 by Charles E. Leiserson 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 0 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 Day 11 L7.5 © 2001 by Charles E. Leiserson Resolving collisions by chaining Records in the same slot are linked into a list. h (49) = h (86) = h (52) = i T 49 86 52 i

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

View Full Document
Introduction to Algorithms Day 11 L7.6 © 2001 by Charles E. Leiserson Analysis of chaining We make the assumption of 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. 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 Day 11 L7.7 © 2001 by Charles E. Leiserson Search cost Expected time to search for a record with a given key = Θ (1 + α ) .

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

### What students are saying

• As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

Kiran Temple University Fox School of Business ‘17, Course Hero Intern

• I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

Dana University of Pennsylvania ‘17, Course Hero Intern

• The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

Jill Tulane University ‘16, Course Hero Intern