1 / 60

Hash Tables

Hash Tables. Asst. Prof. Dr. İlker Kocabaş. Overview. Hash Tables Collisions Linear Probing Problems with Linear Pr ob Chaining. Information Retrieval Binary Search Trees Hashing. Applications. Example. Hash Functions. Example: Bibliography.

Download Presentation

Hash Tables

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Hash Tables Asst. Prof. Dr. İlker Kocabaş

  2. Overview Hash Tables Collisions Linear Probing Problems with Linear Prob Chaining • Information Retrieval • Binary Search Trees • Hashing. • Applications. • Example. • Hash Functions.

  3. Example: Bibliography • 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.

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

  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

  6. Complexity • Inserting • Balanced Trees O(log(n)) • Unbalanced Trees O(n) • Searching • Balanced Trees O(log(n)) • Unbalanced Trees O(n)

  7. Hashing hash table 0 hash function 1 key pos 2 3 : : TABLESIZE - 1

  8. Example: hash table 0 hash function 1 “Kruse” 5 2 3 4 Kruse 5 6

  9. Hashing • Each item has a unique key. • Use a large array called a Hash Table. • Use a Hash Function.

  10. Applications • Databases. • Spell checkers. • Computer chess games. • Compilers.

  11. Operations • Initialize • all locations in Hash Table are empty. • Insert • Search • Delete

  12. Hash Function • Mapskeys to positions in the Hash Table. • Be easy to calculate. • Use all of the key. • Spread the keys uniformly.

  13. Example: Hash Function #1 unsigned hash(char* s) { int i = 0; unsigned value = 0; while (s[i] != ‘\0’) { value = (s[i] + 31*value) % 101; i++; } return value; }

  14. value = (s[i] + 31*value) % 101; Example: Hash Function #1 • 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

  15. Example: Hash Function #1 value = (s[i] + 31*value) % 101; Hash Key Value Aho 49 Kruse 95 Standish 60 Horowitz 28 Langsam 21 Sedgewick 24 Knuth 44 resulting table is “sparse”

  16. value = (s[i] + 1024*value) % 128; Example: Hash Function #2 Hash Key Value Aho 111 Kruse 101 Standish 104 Horowitz 122 Langsam 109 Sedgewick 107 Knuth 104 likely to result in “clustering”

  17. Example: Hash Function #3 value = (s[i] + 3*value) % 7; Hash Key Value Aho 0 Kruse 5 Standish 1 Horowitz 5 Langsam 5 Sedgewick 2 Knuth 1 “collisions”

  18. Insert • Apply hash function to get a position. • Try to insert key at this position. • Deal with collision.

  19. Example: Insert Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function 1 Aho 0 2 3 4 5 6

  20. Example: Insert Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function 1 Kruse 5 2 3 4 Kruse 5 6

  21. Example: Insert Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function Standish 1 Standish 1 2 3 4 Kruse 5 6

  22. Search • Apply hash function to get a position. • Look in that position. • Deal with collision.

  23. Example:Search Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function Standish 1 Kruse 5 2 3 4 Kruse 5 found. 6

  24. Example:Search Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function Standish 1 Sedgwick 2 2 3 4 Kruse 5 Not found. 6

  25. Hash Tables:Collision Resolution

  26. Hashing hash table 0 hash function 1 key pos 2 3 : : TABLESIZE - 1

  27. Example: hash table 0 hash function 1 “Kruse” 5 2 3 4 Kruse 5 6

  28. Hash Function • Maps keys to positions in the Hash Table. • Be easy to calculate. • Use all of the key. • Spread the keys uniformly. Hashing • Each item has a unique key. • Uses a large array called a Hash Table. • Uses a Hash Function.

  29. Hash Table Operations • Initialize • all locations in Hash Table are empty. • Insert • Search • Delete

  30. Example: Hash Function #3 value = (s[i] + 3*value) % 7; Hash Key Value Aho 0 Kruse 5 Standish 1 Horowitz 5 Langsam 5 Sedgewick 2 Knuth 1 “collisions”

  31. Birthdays Number of People Probability 10 20 30 40 50 60 70 0.1169 0.4114 0.7063 0.8912 0.9704 0.9941 0.9992 Collision • When two keys are mapped to the same position. • Very likely.

  32. Collision Resolution • Two methods are commonly used: • Linear Probing. • Chaining.

  33. Linear Probing • Linear search in the array from the position where collision occurred.

  34. Insert with Linear Probing • Apply hash function to get a position. • Try to insert key at this position. • Deal with collision. • Must also deal with a full table!

  35. Example: Insert with Linear Probing Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function 1 Aho 0 2 3 4 5 6

  36. Example: Insert with Linear Probing Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function 1 Kruse 5 2 3 4 Kruse 5 6

  37. Example: Insert with Linear Probing Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function Standish 1 Standish 1 2 3 4 Kruse 5 6

  38. Example: Insert with Linear Probing Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function Standish 1 Horowitz 5 2 3 4 Kruse 5 Horowitz 6

  39. Example: Insert with Linear Probing Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function Standish 1 Langsam Langsam 5 2 3 4 Kruse 5 Horowitz 6

  40. module linearProbe(item) { position = hash(key of item) count = 0 loop { if (count == hashTableSize) then { output “Table is full” exit loop } if (hashTable[position] is empty) then { hashTable[position] = item exit loop } position = (position + 1) % hashTableSize count++ } }

  41. Search with Linear Probing • Apply hash function to get a position. • Look in that position. • Deal with collision.

  42. Example:Search with Linear Probing Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth found. hash table Aho 0 Hash Function Standish 1 Langsam Langsam 5 2 3 4 Kruse 5 Horowitz 6

  43. Example:Search with Linear Probing Aho, Kruse, Standish, Horowiz, Langsam, Sedgewick, Knuth hash table Aho 0 Hash Function Standish 1 Langsam Knuth 1 2 3 4 Kruse 5 not found. Horowitz 6

  44. module search(target) { count = 0 position = hash(key of target) loop { if (count == hashTableSize) then { output “Target is not in Hash Table” return -1. } else if (hashTable[position] is empty) then { output “Item is not in Hash Table” return -1. } else if (hashTable[position].key == target) then { return position. } position = (position + 1) % hashTableSize count++ } }

  45. Delete with Linear Probing • Use the search function to find the item • If found check that items after that also don’t hash to the item’s position • If items after do hash to that position, move them back in the hash table and delete the item. Very difficult and time/resource consuming!

  46. Linear Probing: Problems • Speed. • Tendency for clustering to occur as the table becomes half full. • Deletion of records is very difficult. • If implemented in arrays – table may become full fairly quickly, resizing is time and resource consuming

  47. Chaining • Uses a Linked List at each position in the Hash Table. • Linked list at a position contains all the items that ‘hash’ to that position. • May keep linked lists sorted or not.

  48. hash table 0 1 2 3 : :

  49. Example: Chaining Aho, Kruse, Standish, Horowiz, Langsam, Sedgwick, Knuth 0, 5, 1, 5, 5, 2, 1 Aho 1 0 2 Knuth Standish 1 1 2 Sedgewick 0 3 0 4 3 Horowitz Langsam Kruse 5 0 6

  50. Hashtable with Chaining • At each position in the array you have a list: List hashTable[MAXTABLE]; • You must initialise each list in the table. 1 0 2 1 2 1 :

More Related