Maps
In questa pagina 5
Map ADTs
| ADT | Distinct keys | Keys from a totally ordered universe |
|---|---|---|
| Map | yes | no |
| Ordered map | yes | yes |
| Multimap (or dictionary) | no | no |
| Ordered multimap (or ordered dictionary) | no | yes |
Structures studied for them: Hash TablesA hash table implements a map by sending each key through a hash function to one of N buckets, handling collisions with separate chaining, and it gives expected O(1 + load factor) time per operation but Theta(n) in the worst case.Hash Tables → implement maps that are not necessarily ordered; binary search trees, multi-way search trees and (2,4)-trees implement ordered maps and dictionaries.
A map is a collection of entries supporting search, insertion and removal based on their keys, which act as indices. Compared to a priority queue (Priority QueuesA priority queue is a collection of (key, value) entries where removal returns the entry with the smallest key (highest priority), and a list implementation makes either insertion or removal cost linear time.Priority Queues →), which gives access only to the entry with the highest priority (e.g. the smallest key), a map gives efficient access to any entry through its key.
Applications
- Databases. In a university system: entry = student, key = student ID (primary index), value = the student's information. Name and surname can act as secondary indices.
- Compilers. Type checking: entry = variable, key = variable name, value = variable type.
- Search engines. Entry = inverted list, key = a word, value = the list of documents containing it. Instead of searching every document for a word, keep a map from words to documents (an inverted index).
- Counting object frequencies. Entry = object and its count, key = object, value = count. Useful, for instance, in the analysis of biological sequences (key = -mer, a sequence of characters).
Definition and interface
A map is a collection of entries with distinct keys drawn from a domain in which equality "" is defined, supporting the methods get, put, remove.
public interface Map<K, V> {
int size();
boolean isEmpty();
V get(K key);
V put(K key, V value);
V remove(K key);
Iterable<K> keySet();
Iterable<V> values();
Iterable<Entry<K, V>> entrySet();
}get(key): if there is an entry returns , otherwisenull.put(key, value): if there is an entry replaces withvalueand returns the old value ; otherwise inserts the new entry and returnsnull.remove(key): if there is an entry removes it and returns , otherwise returnsnull.keySet(),values(),entrySet(): returnIterables with the keys, values, entries of the map, enumerated with iterators.
Inserting an entry with a duplicate key replaces the previous one (keys are distinct).
A key is analogous to an index of a list: it gives direct access to the associated element, but keys need not be numeric, consecutive or ordered. A map is an associative array: the domain can be any domain where equality is defined and is not necessarily ordered.
Maps in Java
In java.util:
- interface
Map<K,V>with the methods above and a static nested interfaceMap.Entry<K,V>(associated with the classMap, usable whereverMapis visible; all nested interfaces in Java are static by default); - class
HashMap<K,V>: a map implemented with a hash table with separate chaining (see Hash TablesA hash table implements a map by sending each key through a hash function to one of N buckets, handling collisions with separate chaining, and it gives expected O(1 + load factor) time per operation but Theta(n) in the worst case.Hash Tables →); - class
TreeMap<K,V>: a map implemented with a red-black tree.
Example: most frequent word
is a document of words (with repetitions). Using a support map only through the Map interface, return a word with the maximum number of occurrences (any one in case of ties). Counting word frequencies is a basic operation of text analysis (document classification, topic modelling). The solution and its analysis are in Hash TablesA hash table implements a map by sending each key through a hash function to one of N buckets, handling collisions with separate chaining, and it gives expected O(1 + load factor) time per operation but Theta(n) in the worst case.Hash Tables →.
Implementations
= number of entries of the map.
Doubly linked list (unordered entries)
Each node stores an entry .
get(key): scan the list for the entry with that key.put(key, value): search for the key; if present update the value, otherwise insert a new entry.remove(key): search for the key; if present remove the entry.
All three are : they all need to search for the key, because of the constraint that keys must be distinct (even put of a new key must check the key is not already there). Inefficient in time.
Building a map of distinct keys from empty costs in the worst case: the -th insertion scans the existing entries, so the cost is and, for distinct keys, .
Array of size
Assume the key universe is finite and there is a one-to-one correspondence between keys and array indices in .
get,put,removeall cost .- But the array uses space: if this is highly inefficient.
Example: a map of all citizens of Italy ( entries) keyed by fiscal code (16 characters from an alphabet of 36 symbols): . Constant-time operations, but the memory needed makes it unfeasible.
What comes next
- If the keys have no order: Hash TablesA hash table implements a map by sending each key through a hash function to one of N buckets, handling collisions with separate chaining, and it gives expected O(1 + load factor) time per operation but Theta(n) in the worst case.Hash Tables → compress the huge universe into a table of size proportional to .
- If the keys come from an ordered universe (ordered map): search trees, with better worst-case guarantees.
- A multimap relaxes the distinct-keys constraint and allows several entries with the same key.
Versione ripasso
- ADTs by (distinct keys, ordered universe): map (yes, no), ordered map (yes, yes), multimap/dictionary (no, no), ordered multimap (no, yes).
- Map: entries with distinct keys from a domain with equality; unlike a priority queue (Priority QueuesA priority queue is a collection of (key, value) entries where removal returns the entry with the smallest key (highest priority), and a list implementation makes either insertion or removal cost linear time.Priority Queues →), any entry is reachable by key. Associative array: keys need not be numeric or ordered.
- Uses: databases (ID student), compilers (name type), search engines (word documents, inverted index), frequency counts (object count).
- Interface:
size,isEmpty,get(k),put(k, v),remove(k),keySet(),values(),entrySet().get: value ornull;put: replaces and returns the old value, or inserts and returnsnull;remove: removes and returns the value, ornull.
- Java:
Map<K,V>withMap.Entry<K,V>;HashMap(hash table, separate chaining);TreeMap(red-black tree). - Doubly linked list:
get,put,removeall (search needed by distinctness); insertions . - Array of size : all but space, unfeasible when (fiscal codes: ).
- Typical mistake: thinking
putof a new key on a list is : it must first check the key is absent. - Next: Hash TablesA hash table implements a map by sending each key through a hash function to one of N buckets, handling collisions with separate chaining, and it gives expected O(1 + load factor) time per operation but Theta(n) in the worst case.Hash Tables → for unordered keys.