lecture5.ppt - Introduction to Algorithms Lecture 5 Recap • Show that(nlogn is the best possible running time for a sorting algorithm • Design an

lecture5.ppt - Introduction to Algorithms Lecture 5 Recap...

This preview shows page 1 - 14 out of 54 pages.

Introduction to Algorithms Lecture 5
Image of page 1
2 Recap Show that ( n log n ) is the best possible running time for a sorting algorithm. Design an algorithm that sorts in linear time. Order statistics
Image of page 2
3 Today’s topics Direct-accessible table Hash tables Hash functions Universal hashing Perfect Hashing Open addressing
Image of page 3
4 Data Structures Role of data structures: Encapsulate data Support certain operations (e.g., INSERT, DELETE, SEARCH) Our focus: efficiency of the operations Algorithms vs. data structures
Image of page 4
5 Symbol-table problem Symbol table T holding n records : How should the data structure T be organized? record key [ x ] x Other fields containing satellite data Operations on T: INSERT ( T,x ) DELETE ( T,x ) SEARCH ( T,k )
Image of page 5
6 Direct-accessible table IDEA: Suppose that the set of keys is K {0, 1, …, m –1} , and keys are distinct. Set up an array T [0 . . m –1] : 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!). NIL x k T ] [ if k K and keys [ x ] = k otherwise.
Image of page 6
7 Hash functions Solution: Use a hash function h to map the universe U of all keys into {0, 1, …, m –1} : When a record to be inserted maps to an already occupied slot in T , a collision occurs. T 0 h ( k 1 ) h ( k 4 ) h ( k 2 ) = h ( k 5 ) h ( k 3 ) m -1 k 1 k 5 k 4 k 2 k 3 K
Image of page 7
8 Resolving collisions by chaining Records in the same slot are linked into a list. T 49 86 52 i h (49) = h (86) = h (52) = i
Image of page 8
9 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.
Image of page 9
10 Search cost Expected time to search for a record with a given key = (1 + ) . Expected search time = (1) if = O (1) , or equivalently, if n = O ( m ) . apply hash function and access slot search the list
Image of page 10
11 Choosing a hash function The assumption of simple uniform hashing is hard to guarantee, but several common techniques tend to work well in practice as long as their deficiencies can be avoided. Desirata: A good hash function should distribute the keys uniformly into the slots of the table. Regularity in the key distribution should not affect this uniformity.
Image of page 11
12 Division method Assume all keys are integers, and define h ( k ) = k mod m . Deficiency: Don’t pick an m that has a small divisor d . A preponderance of keys that are congruent modulo d can adversely affect uniformity.
Image of page 12
13 Division method Extreme deficiency: If m = 2 r , then the hash doesn’t even depend on all the bits of k : If k = 1011000111011010 2 and r = 6 , then h ( k ) = 011010 2 .
Image of page 13
Image of page 14

You've reached the end of your free preview.

Want to read all 54 pages?

  • Fall '05
  • RudolfFleischer

What students are saying

  • Left Quote Icon

    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.

    Student Picture

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

  • Left Quote Icon

    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.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    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.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern

Stuck? We have tutors online 24/7 who can help you get unstuck.
A+ icon
Ask Expert Tutors You can ask You can ask ( soon) You can ask (will expire )
Answers in as fast as 15 minutes
A+ icon
Ask Expert Tutors