{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

100_lecture31

# 100_lecture31 - Introduction to Computation and Problem...

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

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

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

View Full Document
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
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

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

View Full Document
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
9 Collisions "car" and "color" hash to the same value using as "random" as it should be.

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