Chapter 9: Maps, Dictionaries, Hashing Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004) http:// parasol.tamu.edu
Chapter 9: Maps, Dictionaries, Hashing. 0. . Nancy Amato Parasol Lab, Dept. CSE, Texas A&M University Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004). 1. 025-612-0001. 2. 981-101-0002. - PowerPoint PPT Presentation
This document is posted to help you gain knowledge. Please leave a comment to let me know what you think about it! Share it to your friends and learn new things together.
Transcript
Chapter 9: Maps, Dictionaries, Hashing
Nancy AmatoParasol Lab, Dept. CSE, Texas A&M University
Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and Mount (Wiley 2004)
cs16.net) to internet addresses (e.g., 128.148.34.101)
• Map ADT methods:• find(k): if M has an entry with
key k, return an iterator p referring to this element, else, return special end iterator.
• put(k, v): if M has no entry with key k, then add entry (k, v) to M, otherwise replace the value of the entry with v; return iterator to the inserted/modified entry
• erase(k) or erase(p): remove from M entry with key k or iterator p; An error occurs if there is no such element.
• A direct address table is a map in which• The keys are in the range {0,1,2,…,N}• Stored in an array of size N - T[0,N]• Item with key k stored in T[k]
• Performance:• insertItem, find, and removeElement all take O(1) time • Space - requires space O(N), independent of n, the number of items
stored in the map
• The direct address table is not space efficient unless the range of the keys is close to the number of elements to be stored in the map, I.e., unless n is close to N.
• A log file is a dictionary implemented by means of an unsorted sequence• We store the items of the dictionary in a sequence (based on a doubly-linked
lists or a circular array), in arbitrary order
• Performance:• insert takes O(1) time since we can insert the new item at the beginning or at
the end of the sequence• find and erase take O(n) time since in the worst case (the item is not found)
we traverse the entire sequence to look for an item with the given key• Space - can be O(n), where n is the number of elements in the dictionary
• The log file is effective only for dictionaries of small size or for dictionaries on which insertions are the most common operations, while searches and removals are rarely performed (e.g., historical record of logins to a workstation)
Map/Dictionary implementations
• n - #elements in map/Dictionary
04/20/23 15:59Vectors 7
Insert Find Space
Log File O(1) O(n) O(n)
Direct Address Table (map only)
O(1) O(1) O(N)
04/20/23 15:598
Ordered Map/Dictionary ADT
• An Ordered Map/Dictionary supports the usual map/dictionary operations, but also maintains an order relation for the keys.
• Naturally supports • Look-Up Tables - store dictionary
in a vector by non-decreasing order of the keys
• Binary Search
• Ordered Map/Dictionary ADT:• In addition to the generic
map/dictionary ADT, supports the following functions:
• closestBefore(k): return the position of an item with the largest key less than or equal to k
• closestAfter(k): return the position of an item with the smallest key greater than or equal to k
04/20/23 15:59Dictionaries 9
Lookup Table
• A lookup table is a dictionary implemented by means of a sorted sequence
• We store the items of the dictionary in an array-based sequence, sorted by key
• We use an external comparator for the keys
• Performance:• find takes O(log n) time, using binary search• insertItem takes O(n) time since in the worst case we have to shift
n2 items to make room for the new item• removeElement take O(n) time since in the worst case we have to
shift n2 items to compact the items after the removal
• The lookup table is effective only for dictionaries of small size or for dictionaries on which searches are the most common operations, while insertions and removals are rarely performed (e.g., credit card authorizations)
04/20/23 15:59Dictionaries 10
Example of Ordered Map: Binary Search
• Binary search performs operation find(k) on a dictionary implemented by means of an array-based sequence, sorted by key
• similar to the high-low game• at each step, the number of candidate items is halved• terminates after a logarithmic number of steps
• Double hashing uses a secondary hash function h2(k) and handles collisions by placing an item in the first available cell of the seriesh(k,i) =(h1(k) ih2(k)) mod N for i 0, 1, … , N 1
• The secondary hash function h2(k) cannot have zero values
• The table size N must be a prime to allow probing of all the cells
• Common choice of compression map for the secondary hash function:
• In the worst case, searches, insertions and removals on a hash table take O(n) time
• The worst case occurs when all the keys inserted into the dictionary collide
• The load factor nN affects the performance of a hash table
• Assuming that the hash values are like random numbers, it can be shown that the expected number of probes for an insertion with open addressing is
1 (1 )
• The expected running time of all the dictionary ADT operations in a hash table is O(1)
• In practice, hashing is very fast provided the load factor is not close to 100%
• Applications of hash tables:• small databases• compilers• browser caches
Dictionaries & Hashing
Uniform Hashing Assumption • The probe sequence of a key k is the sequence of slots
that will be probed when looking for k• In open addressing, the probe sequence is h(k,0), h(k,1), h(k,2),
h(k,3), …
• Uniform Hashing Assumption: Each key is equally likely to have any one of the N! permutations of {0,1, 2, …, N-1} as is probe sequence
• Note: Linear probing and double hashing are far from achieving Uniform Hashing
• Linear probing: N distinct probe sequences• Double Hashing: N2 distinct probe sequences
30 04/20/23 15:59Dictionaries
Dictionaries & Hashing
Performance of Uniform Hashing
• Theorem: Assuming uniform hashing and an open-address hash table with load factor a = n/N < 1, the expected number of probes in an unsuccessful search is at most 1/(1-a).
• Exercise: compute the expected number of probes in an unsuccessful search in an open address hash table with a = ½ , a=3/4, and a = 99/100.