19_Hashing.pptx - Hash Tables 1 Direct Array Storage •...

This preview shows page 1 - 10 out of 36 pages.

1 Hash Tables
Image of page 1

Subscribe to view the full document.

2 Direct Array Storage Given: records with unique keys and that the keys are integers in the range 0 to 99 Use an array of length 100 to hold the records Place the record with key value K at index K of the array This makes insertion, deletion and searching very efficient: constant time
Image of page 2
3 Sparse Array Sparse array situation: Number of possible keys is very large, but expect to store only a limited number of records Example: keys are identifiers of length 8. Then there are 26 8 possible character strings too many for a direct array implementation. In any program, we would expect only a limited number of identifiers Hashing and hash tables are an attempt to retain some of the advantages of direct indexing for “sparse arrays”
Image of page 3

Subscribe to view the full document.

4 Hashing We select a table size HTSize ; and a function h that maps from all possible key values into the integers in the range 0..HTSize-1 Given key k , h(k) is the index of the first possible place to insert/find the object with key k Distinct keys X 1 and X 2 , are said to collide if h ( X 1 ) = h(X 2 )
Image of page 4
5 Example Desirable hash function properties: the set of values that will be stored in the table are spread as evenly as possible over the array indices (minimize collisions) the hash function computation has fast execution time.
Image of page 5

Subscribe to view the full document.

6 Hash Functions A hash function is usually specified as the composition of two functions: Conversion function : h 1 : keys integers Compression function : h 2 : integers [0, N - 1] The conversion function is applied first, and the compression function is applied next: h ( x ) = h 2 ( h 1 ( x )) The goal of the hash function is to “disperse” the keys in an apparently random way
Image of page 6
7 Conversion Function Example View the string as an integer represented in base 128. Return the corresponding integer
Image of page 7

Subscribe to view the full document.

8 Compression Function: Truncation Truncation Here we ignore part of the key and use the remaining digits as the index Very fast, but often fails to distribute keys evenly Example : key an 8 digit number select the 1 st , 4 th and 7 th digits from the right yield a number between 0 and 999 Example: 6 2 53 8 19 4 284
Image of page 8
9 Compression Function: Folding Folding Partition the key into several parts and combine arithmetically (addition or multiplication), then truncate. Example : If the key is a 8 digit number, then we might extract three integers from the key by taking the leftmost three digits, then ]the next three digits and finally the remaining two digits.
Image of page 9

Subscribe to view the full document.

Image of page 10
  • Fall '19
  • hash function, Hash Table Organization

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

Ask Expert Tutors You can ask 0 bonus questions You can ask 0 questions (0 expire soon) You can ask 0 questions (will expire )
Answers in as fast as 15 minutes