Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14...

26
Kymberly Fergusson Kymberly Fergusson CSE1303 Part A CSE1303 Part A Data Structures and Algorithms Data Structures and Algorithms Summer Semester 2003 Summer Semester 2003 Lecture A14 – Hash Tables Lecture A14 – Hash Tables

Transcript of Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14...

Page 1: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

Kymberly FergussonKymberly Fergusson

CSE1303 Part ACSE1303 Part AData Structures and AlgorithmsData Structures and Algorithms

Summer Semester 2003Summer Semester 2003

Lecture A14 – Hash TablesLecture A14 – Hash Tables

Page 2: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

2

OverviewOverview

• Information Retrieval

• Review: Binary Search Trees

• Hashing.

• Applications.

• Example.

• Hash Functions.

Page 3: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

4

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

Page 5: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

6

ComplexityComplexity

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

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

Page 7: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

7

HashingHashing

key hash function

0

1

2

3

TABLESIZE - 1

:

:

hash table

pos

Page 8: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

8

“Kruse”

0

1

2

3

6

4

5

hash table

Example:

5

Kruse

hash function

Page 9: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

9

HashingHashing

• Each item has a unique key.

• Use a large array called a Hash Table.

• Use a Hash Function.

Page 10: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

10

ApplicationsApplications

• Databases.

• Spell checkers.

• Computer chess games.

• Compilers.

Page 11: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

11

OperationsOperations

• Initialize– all locations in Hash Table are empty.

• Insert

• Search

• Delete

Page 12: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

18

InsertInsert

• Apply hash function to get a position.

• Try to insert key at this position.

• Deal with collision.

Page 19: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

22

SearchSearch

• Apply hash function to get a position.

• Look in that position.

• Deal with collision.

Page 23: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

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.

Page 25: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

25

RevisionRevision• Hash Tables

– Hash Functions– Insert, Search

Page 26: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables.

26

Revision: ReadingRevision: Reading• Kruse 8

• Standish 11

• Langsam 7.4

PreparationPreparationNext lecture: Hash Tables: Collision Detection

• Read Chapter 8 in Kruse et al.