{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

100_lecture31 - Introduction to Computation and Problem...

Info icon This preview shows pages 1–6. Sign up to view the full content.

View Full Document Right Arrow Icon
Introduction to Computation and Problem Solving Prof. Steven R. Lerman and Dr. V. Judson Harward Class 31: Class 31: Lab: Hashing Lab: Hashing 2 Motivation Can we search in better than O( lg n ) time? The operation of a computer memory does considerably better than this. A computer memory takes a key (the memory address) to insert or retrieve a data item (the memory word) in constant ( O( 1 ) ) time. Access times for computer memory do not go up as the size of the computer memory or the proportion used increases. 1
Image of page 1

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

View Full Document Right Arrow Icon
3 Direct Addressing technique called direct addressing Data storage in arrays is another example of the role of key. Computer memory access is a special case of a in which the key leads directly to the data item. direct addressing, where the array index plays The problem with direct addressing schemes is that they require storage equal to the range of all possible keys rather than proportional to the number of items actually stored. 4 Direct Addressing Example entries. Let's use the example of social security numbers. A direct addressing scheme to store income information on US tax payers would require a table of 1,000,000,000 entries since a social security number has 9 digits. It doesn't matter whether we expect to store data on 100 tax payers or 100,000,000. A direct addressing scheme will still require a table that can accommodate all 1 billion potential 2
Image of page 2
5 Hashing Hashing is a technique that provides speed comparable to direct addressing ( O(1) ( , n random hash code uses this hash code (~direct address) to index . ) with far more manageable memory requirements O(n) where is the number of entries actually stored in the table). Hashing uses a function to generate a pseudo- from the object key and then into the hash table 6 Hashing Example Suppose that we want a small hash table with a capacity of 16 entries to store English words. Then we will need a hash function that will map English words to the integers 0, 1, ..., 15. We usually divide the task of creating a hash function into two parts: 1. Map the key into an integer. 2. Map the integer "randomly" or in a well distributed way to the range of integers ( { 0, ..., m-1 } , where m is the capacity or number of entries) that will be used to index into the hash table. 3
Image of page 3

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

View Full Document Right Arrow Icon
7 Hash Code Example As an example, consider the hash code that takes step 1 ), then takes the remainder mod 16 ( step 2 ). (99 + 114) mod 16 = 5 . the numeric value of the first character of the word and adds it to the numeric value of the last character of the word ( For instance, the numeric value of a "c" is 99 and of a "r" is 114. So, "car" would hash to 8 Hash Code Diagram of Keys car house dorm color 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 Universe 4
Image of page 4
9 Collisions "car" and "color" hash to the same value using as "random" as it should be.
Image of page 5

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

View Full Document Right Arrow Icon
Image of page 6
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

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