24
Hash Tables Hash Tables

Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

Embed Size (px)

Citation preview

Page 1: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

Hash TablesHash Tables

Page 2: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

2

OverviewOverview

• Information Retrieval

• Review: Binary Search Trees

• Hashing.

• Applications.

• Example.

• Hash Functions.

Page 3: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

3

• R. Kruse, C. Tondo, B. Leung, “Data Structures and Program Design in C”, 1991, Prentice Hall.

• E. Horowitz, S. Salini, S. Anderson-Freed, “Fundamentals of Data Structures in C”, 1993, Computer Science Press.

• R. Sedgewick, “Algorithms in C”, 1990, Addison-Wesley.

• A. Aho, J. Hopcroft, J. Ullman, “Data Structures and Algorithms”, 1983, Addison-Wesley.

• T.A. Standish, “Data Structures, Algorithms & Software Principles in C”, 1995, Addison-Wesley.

• D. Knuth, “The Art of Computer Programming”, 1975, Addison-Wesley.

• Y. Langsam, M. Augenstein, M. Fenenbaum, “Data Structures using C and C++”, 1996, Prentice Hall.

Example: Bibliography

Page 4: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

4

Insert the information into a Binary Search Tree, using the first author’s surname as the key

Page 5: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

5

Kruse

Horowitz Sedgewick

Aho Knuth Langsam Standish

Insert the information into a Binary Search Tree, using the first author’s surname as the key

Kruse Horowitz Sedgewick Aho Knuth Langsam Standish

Page 6: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

6

ComplexityComplexity

• Inserting– Balanced Trees O(log(n))– Unbalanced Trees O(n)

• Searching– Balanced Trees O(log(n))– Unbalanced Trees O(n)

Page 7: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

7

HashingHashing

key hash function

0

1

2

3

TABLESIZE - 1

:

:

hash table

pos

Page 8: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

8

“Kruse”

0

1

2

3

6

4

5

hash table

Example:

5

Kruse

hash function

Page 9: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

9

HashingHashing

• Each item has a unique key.

• Use a large array called a Hash Table.

• Use a Hash Function.

Page 10: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

10

ApplicationsApplications

• Databases.

• Spell checkers.

• Computer chess games.

• Compilers.

Page 11: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

11

OperationsOperations

• Initialize– all locations in Hash Table are empty.

• Insert

• Search

• Delete

Page 12: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

12

Hash FunctionHash Function

• Maps keys to positions in the Hash Table.

• Be easy to calculate.

• Use all of the key.

• Spread the keys uniformly.

Page 13: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

13

unsigned hash(char* s){ int i = 0; unsigned value = 0; while (s[i] != ‘\0’) { value = (s[i] + 31*value) % 101; i++; } return value;}

Example: Hash Function #1

Page 14: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

14

• A. Aho, J. Hopcroft, J. Ullman, “Data Structures and Algorithms”, 1983, Addison-Wesley.

‘A’ = 65 ‘h’ = 104 ‘o’ = 111

value = (65 + 31 * 0) % 101 = 65

value = (104 + 31 * 65) % 101 = 99

value = (111 + 31 * 99) % 101 = 49

Example: Hash Function #1

value = (s[i] + 31*value) % 101;value = (s[i] + 31*value) % 101;

Page 15: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

15

resultingtable is “sparse”

Example: Hash Function #1

value = (s[i] + 31*value) % 101;value = (s[i] + 31*value) % 101;

Hash Key Value

Aho 49Kruse 95Standish 60Horowitz 28Langsam 21Sedgewick 24Knuth 44

Page 16: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

16

value = (s[i] + 1024*value) % 128;value = (s[i] + 1024*value) % 128;

Example: Hash Function #2

likely toresult in

“clustering”

Hash Key Value

Aho 111Kruse 101Standish 104Horowitz 122Langsam 109Sedgewick 107Knuth 104

Page 17: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

17

Example: Hash Function #3

“collisions”

value = (s[i] + 3*value) % 7;

Hash Key Value

Aho 0Kruse 5Standish 1Horowitz 5Langsam 5Sedgewick 2Knuth 1

Page 18: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

18

InsertInsert

• Apply hash function to get a position.

• Try to insert key at this position.

• Deal with collision.

Page 19: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

19

Aho Hash Function

0

1

2

3

6

4

5

hash table

Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth

0

Example: Insert

Aho

Page 20: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

20

Kruse

0

1

2

3

6

4

5

hash table

Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth

5

Example: Insert

Aho

Kruse

Hash Function

Page 21: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

21

Standish

0

1

2

3

6

4

5

hash table

Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth

1

Example: Insert

Aho

Kruse

Standish

Hash Function

Page 22: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

22

SearchSearch

• Apply hash function to get a position.

• Look in that position.

• Deal with collision.

Page 23: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

23

Kruse

Kruse

0

1

2

3

6

4

5

hash table

Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth

5

Example: Search

Aho

Standish

Hash Function

found.

Page 24: Hash Tables. 2 Overview Information Retrieval Review: Binary Search Trees Hashing. Applications. Example. Hash Functions

24

Kruse

Sedgwick

0

1

2

3

6

4

5

hash table

Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth

2

Example: Search

Aho

Standish

Hash Function

Not found.