kymberly fergusson cse1303 part a data structures and algorithms summer semester 2003 lecture a14...
TRANSCRIPT
![Page 1: Kymberly Fergusson CSE1303 Part A Data Structures and Algorithms Summer Semester 2003 Lecture A14 – Hash Tables](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/1.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/2.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/3.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/4.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/5.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/6.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/7.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/8.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/9.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/10.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/11.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/12.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/13.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/14.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/15.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/16.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/17.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/18.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/19.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/20.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/21.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/22.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/23.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/24.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/25.jpg)
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](https://reader035.vdocuments.net/reader035/viewer/2022062619/5519b2785503467a578b466e/html5/thumbnails/26.jpg)
26
Revision: ReadingRevision: Reading• Kruse 8
• Standish 11
• Langsam 7.4
PreparationPreparationNext lecture: Hash Tables: Collision Detection
• Read Chapter 8 in Kruse et al.