Efficient Data Storage and Retrieval with Hash Tables at University of Basrah

3 rd grade n.w
1 / 34
Embed
Share

Explore the lecture on data structures focusing on hash tables at the University of Basrah's College of Computer Science and Information Technology. Learn about efficient storage, retrieval, and collision handling techniques while aiming for constant-time operations. Understand the motivations behind using hash tables to store items based on unique keys and values, ensuring quick access in O(1) time with minimal space complexity.

  • Data Structures
  • Hash Tables
  • University of Basrah
  • Computer Science
  • Information Technology

Uploaded on | 0 Views


Download Presentation

Please find below an Image/Link to download the presentation.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author. If you encounter any issues during the download, it is possible that the publisher has removed the file from their server.

You are allowed to download the files provided on this website for personal or commercial use, subject to the condition that they are used lawfully. All files are the property of their respective owners.

The content on the website is provided AS IS for your information and personal use only. It may not be sold, licensed, or shared on other websites without obtaining consent from the author.

E N D

Presentation Transcript


  1. 3rd Grade College of Computer Science and Information Technology Department of Computer Science Data Structures Lecture 9 hash table Instructor: Ghazwan Abdulnabi Al-Ali University of Basrah, Iraq

  2. Data Structures University of Basrah Example Hash Tables index 0 1 2 3 4 5 value 1 5 0 33 3 index 0 1 2 3 4 5 value 2

  3. Data Structures University of Basrah Example Hash Tables Cont. index 0 1 2 3 4 5 value 1 5 0 33 3 index 0 1 2 3 4 5 value 1 3

  4. Data Structures University of Basrah Example Hash Tables Cont. index 0 1 2 3 4 5 value 1 5 0 33 3 index 0 1 2 3 4 5 value 1 5 4

  5. Data Structures University of Basrah Example Hash Tables Cont. Aim for constant-- time (i.e., O(1)) find, insert, and delete. Ex: search(5) Hash Tables [5]=5. index 0 1 2 3 4 5 value 1 5 0 33 3 index 0 1 2 3 4 5 value 0 1 5 5

  6. Data Structures University of Basrah Example Hash Tables Cont. hash function Number mod size=Hash index 33 mod 6=3 index 0 1 2 3 4 5 value 1 5 0 33 3 index 0 1 2 3 4 5 value 0 1 5 6

  7. Data Structures University of Basrah Example Hash Tables Cont. hash function Number mod size=Hash index 33 mod 6=3 index 0 1 2 3 4 5 value 1 5 0 33 3 index 0 1 2 3 4 5 value 0 1 33 5 7

  8. Data Structures University of Basrah Example Hash Tables Cont. Number mod size=Hash index 3 mod 6=3 index 0 1 2 3 4 5 value 1 5 0 33 3 Collision index 0 1 2 3 4 5 value 0 1 33 5 8

  9. Data Structures University of Basrah Motivations of Hash Tables We have n items, each contains a key and value (k, value). The key uniquely determines the item. Each key could be anything, e.g., a number in [0, 232], a string of length 32, array of numbers, etc. How to store the n items such that given the key k, we can find the position of the item with key= k in O(1) time. Another constraint: space required is O(n). Linked list? Space O(n) and Time O(n). Array? Time O(1) and space: too big, e.g., oIf the key is an integer in [0, 2 32], then the space required is 2 32. oif the key is a string of length 30, the space required is 26 30. Hash Table: space O(n) and time O(1). 9

  10. Data Structures University of Basrah Basic ideas of Hash Tables A hash function h maps keys of a given type with a wide range to integers in a fixed interval [0, N 1], where N is the size of the hash table such that Problem: It is hard to design a function h such that (1) holds. What we can do: We can design a function h so that with high chance, (1) holds. i.e., (1) may not always holds, but (1) holds for most of the n keys. 10

  11. Data Structures University of Basrah Hash Functions An ideal hash function: Fast to compute Rarely hashes two used keys to the same index Often impossible in theory but easy in practice Will handle collisions later hashtable 0 hashfunction: index =h(key) TableSize 1 key space (e.g., integers, strings) 11

  12. Data Structures University of Basrah Using hash functions for Hash Tables Aim for constant- time (i.e., O(1)) find, insert, and delete On average under some often- reasonable assumptions A hash table is an array of some fixed size Hash table 0 Basicidea: hashfunction: index =h(key) key space (e.g., integers, strings) TableSize 1 12

  13. Data Structures University of Basrah Example of Hash Table used for Dictionary of phone numbers 13

  14. Data Structures University of Basrah Example: Simple Integer Hash Function collision: When hash function maps 2 values to same index. h(k) = k % tableSize; set.add(11); index value 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 14

  15. Data Structures University of Basrah Example: Simple Integer Hash Function Cont. collision: When hash function maps 2 values to same index. h(k) = k % tableSize; set.add(11); set.add(49); index value 0 0 1 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 11 15

  16. Data Structures University of Basrah Example: Simple Integer Hash Function Cont. collision: When hash function maps 2 values to same index. h(k) = k % tableSize; set.add(11); set.add(49); set.add(24); index value 0 0 1 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 11 49 16

  17. Data Structures University of Basrah Example: Simple Integer Hash Function Cont. collision: When hash function maps 2 values to same index. h(k) = k % tableSize; set.add(11); set.add(49); set.add(24); set.add(7); index value 0 0 1 2 0 3 0 4 5 0 6 0 7 0 8 0 9 11 24 49 17

  18. Data Structures University of Basrah Example: Simple Integer Hash Function Cont. collision: When hash function maps 2 values to same index. h(k) = k % tableSize; set.add(11); set.add(49); set.add(24); set.add(7); set.add(54); index value 0 0 1 2 0 3 0 4 5 0 6 0 7 7 8 0 9 11 24 49 18

  19. Data Structures University of Basrah Example: Simple Integer Hash Function Cont. collision: When hash function maps 2 values to same index. h(k) = k % tableSize; set.add(11); set.add(49); set.add(24); set.add(7); set.add(54); // collides with 24! index value 0 0 1 2 0 3 0 4 5 0 6 0 7 7 8 0 9 11 54 49 X 19

  20. Data Structures University of Basrah Collision Handling Collisions occur when different elements are mapped to the same cell Separate Chaining: let each cell in the table point to a linked list of entries that map there Separate chaining is simple, but requires additional memory outside the table 0 1 2 3 4 025-612-0001 451-229-0004 981-101-0004 20

  21. Data Structures University of Basrah Methods of Resolution Chaining: Store all elements that hash to the same slot in a linked list. Store a pointer to the head of the linked list in the hash table slot. Open Addressing: All elements stored in hash table itself. When collisions occur, use a systematic (consistent) procedure to store elements in free slots of the table. 0 k1 k4 k5 k2 k6 k7 k8 k3 m 1 21

  22. Data Structures University of Basrah Collision Resolution by Chaining 0 U (universe of keys) k1 k4 k1 k4 K (actual keys) k2 k6 k5 k2 k6 k5 k7 k8 k3 k7 k3 k8 m 1 22

  23. Data Structures University of Basrah EX: Collision Resolution by Chaining 23

  24. 24

  25. Data Structures University of Basrah Open Addressing (closed hashing) We will use just 1D array. Elements either Null or has a pair (key, value) When add entry, check if its hash is empty or not If empty, then add it. If not empty, use some magic (probing) to determine another cell to set the pair. If not repeat until finding cell or declare full table. Load factor: n/N, wheren is the number of items to store and N the size of the hash table = average keys per slot. n/N 1. To get a reasonable performance, n/N<0.5. 25

  26. Data Structures University of Basrah Linear Probing Linear probing handles collisions by placing the colliding item in the next (circularly) available table cell Each table cell inspected is referred to as a probe Colliding items lump together, causing future collisions to cause a longer sequence of probes Example: h(x) = x mod13 Insert keys 18, 41, 22, 44, 59, 32, 31, 73, in this order h(x)=5, 2, 9, 5, 7, 6, 5, 8 0 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 59 32 22 31 73 Hash Tables 0 1 2 3 4 5 6 7 8 9 10 11 12 26

  27. Data Structures University of Basrah Search with Linear Probing Algorithmget(k) i h(k) p 0 repeat c A[i] if c= returnnull else if c.key () =k returnc.element() else i (i+1)mod N p p+1 untilp=N returnnull Consider a hash table A that uses linear probing get(k) We start at cell h(k) We probe consecutive locations until one of the following occurs An item with key k is found, or An empty cell is found, or N cells have been unsuccessfully probed To ensure the efficiency, if k is not in the table, we want to find an empty cell as soon as possible. The load factor can NOT be close to 1. 27

  28. Data Structures University of Basrah Example Search Search for 73: h(73) = Yes, value array = 73 41 18 44 59 32 22 31 73 0 1 2 3 4 5 6 7 8 9 10 11 12 28

  29. Data Structures University of Basrah Example Search Cont. Search for 35: h(35) = Empty Cell! Not found 0 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 59 32 22 31 73 0 1 2 3 4 5 6 7 8 9 10 11 12 29

  30. Data Structures University of Basrah Updates with Linear Probing put(k, o) We throw an exception if the table is full We start at cell h(k) We probe consecutive cells until one of the following occurs A cell i is found that is either empty or stores AVAILABLE, or N cells have been unsuccessfully probed We store entry (k, o) in cell i To handle insertions and deletions, we introduce a special object, called AVAILABLE, which replaces deleted elements remove(k) We search for an entry with key k If such an entry (k, o) is found, we replace it with the special item AVAILABLE and we return element o Else, we return null 30

  31. Data Structures University of Basrah Example Remove Remove 31 Get Mod Search for 31 If found Mark it deleted 41 18 44 59 32 22 31 73 0 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 59 32 22 X 73 0 1 2 3 4 5 6 7 8 9 10 11 12 31

  32. Data Structures University of Basrah Example Remove Cont. Remove 59 Get Mod Search for 59 If found Mark it deleted 41 18 44 59 32 22 X 73 0 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 X 32 22 X 73 0 1 2 3 4 5 6 7 8 9 10 11 12 32

  33. Data Structures University of Basrah Example Insert Insert 57 H(57) = 57%13 = 5 First Empty Slot 41 18 44 X 32 22 X 73 0 1 2 3 4 5 6 7 8 9 10 11 12 41 18 44 57 32 22 X 73 0 1 2 3 4 5 6 7 8 9 10 11 12 33

  34. Data Structures University of Basrah Thank You 34

Related


More Related Content