Salta al contenuto
Note per Studenti Maps Report

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

  1. 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.
  2. Compilers. Type checking: entry = variable, key = variable name, value = variable type.
  3. 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).
  4. Counting object frequencies. Entry = object and its count, key = object, value = count. Useful, for instance, in the analysis of biological sequences (key = kk-mer, a sequence of kk characters).

Definition and interface

A map is a collection of entries with distinct keys drawn from a domain UU in which equality "==" is defined, supporting the methods get, put, remove.

java
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 (key,x)(key, x) returns xx, otherwise null.
  • put(key, value): if there is an entry (key,x)(key, x) replaces xx with value and returns the old value xx; otherwise inserts the new entry (key,value)(key, value) and returns null.
  • remove(key): if there is an entry (key,x)(key, x) removes it and returns xx, otherwise returns null.
  • keySet(), values(), entrySet(): return Iterables 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 UU can be any domain where equality is defined and is not necessarily ordered.

Maps in Java

In java.util:

Example: most frequent word

D[0..N−1]D[0..N-1] is a document of NN 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

nn = number of entries of the map.

Doubly linked list (unordered entries)

Each node stores an entry ⟨key,value⟩\langle key, value \rangle.

  • 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 Θ(n)\Theta(n): 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 nn distinct keys from empty costs Θ(n2)\Theta(n^2) in the worst case: the ii-th insertion scans the existing i−1i-1 entries, so the cost is O(1+2+⋯+n)=O(n2)O(1 + 2 + \dots + n) = O(n^2) and, for nn distinct keys, Ω(∑i=1n−1i)=Ω(n2)\Omega\left(\sum_{i=1}^{n-1} i\right) = \Omega(n^2).

Array of size ∣U∣|U|

Assume the key universe UU is finite and there is a one-to-one correspondence between keys and array indices in [0,∣U∣)[0, |U|).

  • get, put, remove all cost Θ(1)\Theta(1).
  • But the array uses Θ(∣U∣)\Theta(|U|) space: if ∣U∣≫n|U| \gg n this is highly inefficient.

Example: a map of all citizens of Italy (≈6×107\approx 6 \times 10^7 entries) keyed by fiscal code (16 characters from an alphabet of 36 symbols): ∣U∣=3616≈8×1024|U| = 36^{16} \approx 8 \times 10^{24}. Constant-time operations, but the memory needed makes it unfeasible.

What comes next

Versione ripasso

Teoria collegata