100_lecture31

100_lecture31 - Introduction to Computation and Problem...

Info iconThis 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
Background image of page 1

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

View Full DocumentRight 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
Background 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
Background image of page 3

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

View Full DocumentRight 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
Background image of page 4
9 Collisions "car" and "color" hash to the same value using as "random" as it should be. But if
Background image of page 5

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

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

Page1 / 21

100_lecture31 - Introduction to Computation and Problem...

This preview shows document pages 1 - 6. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online