DS-chapter7(Hash)

# DS-chapter7(Hash) - CHAPTER 7 HASHING 7.1 General Idea...

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

7.1 General Idea Symbol Table ( == Dictionary) ::= { < name, attribute > } . Example . In Oxford English dictionary name = since attribute = a list of meanings M[0] = after a date, event, etc. M[1] = seeing that (expressing reason) …… …… This is the worst disaster in California since I was elected. California Governor Pat Brown, discussing a local flood a Example . In a symbol table for a compiler name = identifier (e.g. int ) attribute = a list of lines that use the identifier, and some other fields CHAPTER 7 HASHING

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

View Full Document
Objects : A set of name-attribute pairs, where the names are unique Operations : SymTab Create(TableSize) Boolean IsIn(symtab, name) Attribute Find(symtab, name) SymTab Insert(symtab, name, attr) SymTab Delete(symtab, name) Symbol Table ADT: General Idea
Hash Tables [0] [1] … … [s - 1] … … ht [ 0 ] … … ht [ 1 ] … … ht [b - 1] … … … … b buckets s slots For each identifier x , we define a hash function f ( x ) = position of x in ht[ ] (i.e. the index of the bucket that contains x ) T ::= total number of distinct possible values for x n ::= total number of identifiers in ht[ ] identifier density ::= n / T loading density λ ::= n / ( s b ) General Idea

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

View Full Document
A collision occurs when we hash two nonidentical identifiers into the same bucket, i.e. f ( i 1 ) = f ( i 2 ) when i 1 i 2 . . An overflow occurs when we hash a new identifier into a full bucket. . Example . Mapping n = 10 C library functions into a hash table ht[ ] with b = 26 buckets and s = 2. Slot 1 Slot 0 1 3 4 6 …… 25 Loading density λ = ? 10 / 52 = 0.19 To map the letters a ~ z to 0 ~ 25, we may define f ( x ) = ? x [ 0 ] - a acos acos define define float float exp exp char char atan atan ceil ceil floor floor clock ctime Without overflow , , T T search search = T = T insert insert = T = T delete delete = = O( 1 ) O( 1 ) General Idea
Properties of f : f ( x ) must be easy to compute and minimizes the number of collisions . f ( x ) should be unbiased. That is, for any x and any i , we have that Probability( f ( x ) = i ) = 1 / b . Such kind of a hash function is called a uniform hash function . 7.2 Hash Function f ( x ) = x % TableSize ; /* if x is an integer */ What if TableSize = 10 and x ’s are all end in zero? TableSize = prime number ---- good for random integer keys

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

View Full Document
Hash Function f ( x ) = ( Σ x [ i ]) % TableSize ; /* if x is a string */ . Example . TableSize = 10,007 and string length of x 8.
This is the end of the preview. Sign up to access the rest of the document.

## This note was uploaded on 02/16/2011 for the course CS 135 taught by Professor Yuechen during the Fall '08 term at Zhejiang University.

### Page1 / 19

DS-chapter7(Hash) - CHAPTER 7 HASHING 7.1 General Idea...

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

View Full Document
Ask a homework question - tutors are online