lecture10hashing - Hashing Hash Functions key to memory...

Info iconThis preview shows pages 1–12. Sign up to view the full content.

View Full Document Right Arrow Icon
Hashing Art Covert 11/03/2005 Hash Functions – key to memory mapping; compute the memory address from the key [without comparing keys] Collision resolution
Background image of page 1

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

View Full DocumentRight Arrow Icon
hashing To look up record with key KEY Compute h(KEY) = ADDRESS Check ADDRESS for KEY O(1) hash Function H(KEY) KEY ADDRESS
Background image of page 2
Motivation So far we have seen structures that can support the following options Insert Delete Search Can we design a data structure with constant time ops? Idea: Given a key, if we compute its address (index) in memory, then search can be done in O(1) by direct access. Can we define the address of a key as a function of the key? This is the idea of hashing.
Background image of page 3

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

View Full DocumentRight Arrow Icon
Motivation Say we have a group of students in a computer science department with unique id's Moe:10001 Larry:10002 Curly: 10003 Is there some way we can store these entry's in an array of fixed size so that we can find them in O(1) time?
Background image of page 4
Inserting – Hash Table Use a simple 'hash' function h(c) = stuid mod ARRAY_SIZE h(<Moe, 10001>) = 10001 % 10 = 1 h(<Larry, 10002>) = 10002 % 10 = 2 h(<Currly, 10003>) = 10003 % 10 = 3 Now when we search for Moe, we can find him simply by using this hash function 0 1 Moe 2 Larry 3 Currly 4 5 6 7 8 9
Background image of page 5

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

View Full DocumentRight Arrow Icon
Inserting Insert a student Art with id 10008 h(<Art, 10008>) = 8 0 1 Moe 2 Larry 3 Currly 4 5 6 7 8 Art 9
Background image of page 6
Collisions: what if H(key1) = H(key2)? Insert a student Art with id 10008 h(<Art, 10008>) = 8 Insert a student Shemp with id 10023 h(<Shemp, 10023>) = 3 This collides with Currly!!! What to do? 0 1 Moe 2 Larry 3 Currly 4 5 6 7 8 Art 9
Background image of page 7

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

View Full DocumentRight Arrow Icon
General possibilities Exactly 1 key has hash(KEY) = ADDRESS No key has hash(KEY) = ADDRESS Multiple keys have hash(KEY) = ADDRESS == compare and done Failed search IF ADDRESS specifies a bin, then search that bin for KEY bin can be list, array, disk track
Background image of page 8
Hash Functions A good Hash function satisfies (approximately) the assumption of simple uniform hashing each key is equally likely to hash to any of the m memory bins. It must be fast , else advantage will be lost Some commonly used hash functions:
Background image of page 9

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

View Full DocumentRight Arrow Icon
Example A company of 100 employees. The access key is a nine-digit number (SSN). The array (hash table) is indexed from 0 to 99. How can we define the hashing function h ? Generally, adding up all digits or characters, called folding , produces BAD hashing because it is NOT uniform.
Background image of page 10
A poorly performing hash function: fast but not uniform     unsigned int HashValue = 0;  // just add up the integer values of all the characters of key      for( int i=0; i<Key.length(); i++ )             { HashValue += int(Key[i]); }       return HashValue % tableSize; 
Background image of page 11

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

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

This note was uploaded on 07/25/2008 for the course CSE 331 taught by Professor M.mccullen during the Spring '08 term at Michigan State University.

Page1 / 41

lecture10hashing - Hashing Hash Functions key to memory...

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

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