\(\) \(%\newcommand{\CLRS}{\href{https://mitpress.mit.edu/books/introduction-algorithms}{Cormen et al}}\) \(\newcommand{\CLRS}{\href{https://mitpress.mit.edu/books/introduction-algorithms}{Introduction to Algorithms, 3rd ed.} (\href{http://libproxy.aalto.fi/login?url=http://site.ebrary.com/lib/aalto/Doc?id=10397652}{online via Aalto lib})}\) \(\newcommand{\SW}{\href{http://algs4.cs.princeton.edu/home/}{Algorithms, 4th ed.}}\) \(%\newcommand{\SW}{\href{http://algs4.cs.princeton.edu/home/}{Sedgewick and Wayne}}\) \(\) \(\newcommand{\Java}{\href{http://java.com/en/}{Java}}\) \(\newcommand{\Python}{\href{https://www.python.org/}{Python}}\) \(\newcommand{\CPP}{\href{http://www.cplusplus.com/}{C++}}\) \(\newcommand{\ST}[1]{{\Blue{\textsf{#1}}}}\) \(\newcommand{\PseudoCode}[1]{{\color{blue}\textsf{#1}}}\) \(\) \(%\newcommand{\subheading}[1]{\textbf{\large\color{aaltodgreen}#1}}\) \(\newcommand{\subheading}[1]{\large{\usebeamercolor[fg]{frametitle} #1}}\) \(\) \(\newcommand{\Blue}[1]{{\color{flagblue}#1}}\) \(\newcommand{\Red}[1]{{\color{aaltored}#1}}\) \(\newcommand{\Emph}[1]{\emph{\color{flagblue}#1}}\) \(\) \(\newcommand{\Engl}[1]{({\em engl.}\ #1)}\) \(\) \(\newcommand{\Pointer}{\raisebox{-1ex}{\huge\ding{43}}\ }\) \(\) \(\newcommand{\Set}[1]{\{#1\}}\) \(\newcommand{\Setdef}[2]{\{{#1}\mid{#2}\}}\) \(\newcommand{\PSet}[1]{\mathcal{P}(#1)}\) \(\newcommand{\Card}[1]{{\vert{#1}\vert}}\) \(\newcommand{\Tuple}[1]{(#1)}\) \(\newcommand{\Implies}{\Rightarrow}\) \(\newcommand{\Reals}{\mathbb{R}}\) \(\newcommand{\Seq}[1]{(#1)}\) \(\newcommand{\Arr}[1]{[#1]}\) \(\newcommand{\Floor}[1]{{\lfloor{#1}\rfloor}}\) \(\newcommand{\Ceil}[1]{{\lceil{#1}\rceil}}\) \(\newcommand{\Path}[1]{(#1)}\) \(\) \(%\newcommand{\Lg}{\lg}\) \(\newcommand{\Lg}{\log_2}\) \(\) \(\newcommand{\BigOh}{O}\) \(%\newcommand{\BigOh}{\mathcal{O}}\) \(\newcommand{\Oh}[1]{\BigOh(#1)}\) \(\) \(\newcommand{\todo}[1]{\Red{\textbf{TO DO: #1}}}\) \(\) \(\newcommand{\NULL}{\textsf{null}}\) \(\) \(\newcommand{\Insert}{\ensuremath{\textsc{insert}}}\) \(\newcommand{\Search}{\ensuremath{\textsc{search}}}\) \(\newcommand{\Delete}{\ensuremath{\textsc{delete}}}\) \(\newcommand{\Remove}{\ensuremath{\textsc{remove}}}\) \(\) \(\newcommand{\Parent}[1]{\mathop{parent}(#1)}\) \(\) \(\newcommand{\ALengthOf}[1]{{#1}.\textit{length}}\) \(\) \(\newcommand{\TRootOf}[1]{{#1}.\textit{root}}\) \(\newcommand{\TLChildOf}[1]{{#1}.\textit{leftChild}}\) \(\newcommand{\TRChildOf}[1]{{#1}.\textit{rightChild}}\) \(\newcommand{\TNode}{x}\) \(\newcommand{\TNodeI}{y}\) \(\newcommand{\TKeyOf}[1]{{#1}.\textit{key}}\) \(\) \(\newcommand{\PEnqueue}[2]{{#1}.\textsf{enqueue}(#2)}\) \(\newcommand{\PDequeue}[1]{{#1}.\textsf{dequeue}()}\) \(\) \(\newcommand{\Def}{\mathrel{:=}}\) \(\) \(\newcommand{\Eq}{\mathrel{=}}\) \(\newcommand{\Asgn}{\mathrel{\leftarrow}}\) \(%\newcommand{\Asgn}{\mathrel{:=}}\) \(\) \(%\) \(% Heaps\) \(%\) \(\newcommand{\Downheap}{\textsc{downheap}}\) \(\newcommand{\Upheap}{\textsc{upheap}}\) \(\newcommand{\Makeheap}{\textsc{makeheap}}\) \(\) \(%\) \(% Dynamic sets\) \(%\) \(\newcommand{\SInsert}[1]{\textsc{insert}(#1)}\) \(\newcommand{\SSearch}[1]{\textsc{search}(#1)}\) \(\newcommand{\SDelete}[1]{\textsc{delete}(#1)}\) \(\newcommand{\SMin}{\textsc{min}()}\) \(\newcommand{\SMax}{\textsc{max}()}\) \(\newcommand{\SPredecessor}[1]{\textsc{predecessor}(#1)}\) \(\newcommand{\SSuccessor}[1]{\textsc{successor}(#1)}\) \(\) \(%\) \(% Union-find\) \(%\) \(\newcommand{\UFMS}[1]{\textsc{make-set}(#1)}\) \(\newcommand{\UFFS}[1]{\textsc{find-set}(#1)}\) \(\newcommand{\UFCompress}[1]{\textsc{find-and-compress}(#1)}\) \(\newcommand{\UFUnion}[2]{\textsc{union}(#1,#2)}\) \(\) \(\) \(%\) \(% Graphs\) \(%\) \(\newcommand{\Verts}{V}\) \(\newcommand{\Vtx}{v}\) \(\newcommand{\VtxA}{v_1}\) \(\newcommand{\VtxB}{v_2}\) \(\newcommand{\VertsA}{V_\textup{A}}\) \(\newcommand{\VertsB}{V_\textup{B}}\) \(\newcommand{\Edges}{E}\) \(\newcommand{\Edge}{e}\) \(\newcommand{\NofV}{\Card{V}}\) \(\newcommand{\NofE}{\Card{E}}\) \(\newcommand{\Graph}{G}\) \(\) \(\newcommand{\SCC}{C}\) \(\newcommand{\GraphSCC}{G^\text{SCC}}\) \(\newcommand{\VertsSCC}{V^\text{SCC}}\) \(\newcommand{\EdgesSCC}{E^\text{SCC}}\) \(\) \(\newcommand{\GraphT}{G^\text{T}}\) \(%\newcommand{\VertsT}{V^\textup{T}}\) \(\newcommand{\EdgesT}{E^\text{T}}\) \(\) \(%\) \(% NP-completeness etc\) \(%\) \(\newcommand{\Poly}{\textbf{P}}\) \(\newcommand{\NP}{\textbf{NP}}\) \(\newcommand{\PSPACE}{\textbf{PSPACE}}\) \(\newcommand{\EXPTIME}{\textbf{EXPTIME}}\)

Introduction: sets and maps

In this and the next round we are interested in the abstract data types for dynamic sets and maps. Here “dynamic” means that the keys, or (key,value) associations in maps, are inserted, searched for and removed all the time in an online fashion.

Sets

A basic API for a set abstract data type could be

  • insert(k) for inserting the key k in the set,

  • search(k) for finding whether the key k is in the set, and

  • remove(k) for deleting the key k from the set.

The figure below shows the set diagrams for two sets of strings, where the latter is obtained by inserting a new string in it.

_images/sets-insert.png

In Scala, this API corresponds to the collection.mutable.Set trait, which is implemented, among others, in the mutable TreeSet and HashSet classes. The methods corresponding to the ones in the asbtract API are

  • s.add(k) or s += k for inserting key k into the set s,

  • s.contains(k) or s(k) for checking whether the set s contains the key k, and

  • s.remove(k) or s -= k for removing the key k from the set s.

In Java, the counterpart is the Set interface. In C++, the standard library contains two classes, set and unordered_set, implementing a variant of the API (the difference is explained below).

Maps

For maps, also called dictionaries in some languages, the API also allows associating a value to each key.

_images/maps.png

In Scala mutable map trait the methods are called

  • m.update(k,v) or simply m(k) = v for setting the value of the key k to v in the map m,

  • m.apply(k) or simply m(k) for getting the value associated with the key k in the map m, and

  • m.remove(k) for removing the key k from the map m.

In the Java 8 Map interface the methods are called put, get, and remove.

In the C++ standard library map and unordered_map classes, the methods are called

  • insert, or simply m[k] = v, for a map m, key k, and value v,

  • m.at(k), or simply m[k], and

  • m.erase(k).

Ordered sets and maps

In this round, we will in fact consider ordered sets and ordered maps that

  • ​ assume an ordering between the keys, and

  • ​ allow efficient searching for the smallest, next smallest, etc keys.

_images/maps-ordered.png

The abstract API of ordered sets extends the basic API with

  1. min for getting the smallest key in the set,

  2. max for getting the largest key in the set,

  3. predecessor(k) for getting the largest key that is smaller than k, and

  4. successor(k) for getting the smallest key that is larger than k.

Our goal is to have data structures and algorithms allowing the operations to be done in logarithmic time in the size of the set. Furthermore, the keys can be listed in ascending order in linear time. Note the difference to the priority queue API of the previous round (Section Priority queues with binary heaps): removing arbitrary keys as well as finding the minimum and successor are now also supported.

  • ​ In Scala, the TreeSet class implements a variant of the API allowing ordered iteration over the keys in the set.

    In the class, firstKey gives the smallest key while lastKey finds the largest key. Note: min and max are slow, linear time generic operations, do not use them.

    Note 2: in the current Scala version, val s = collection.mutable.Set() creates a new “hash set” (covered in the next round) in which the operations above take linear time in the worst case. Use val s = collection.mutable.TreeSet() to create an ordered set.

  • ​ In Java, the TreeSet API is close to the abstract one:

    • first gives the smallest key,

    • last gives the largest key,

    • higher(k) gives the smallest key that is larger than k, and

    • lower(k) gives the largest key that is smaller than k.

  • ​ The C++ standard library has the set class.

  • ​ For maps, the corresponding classes are TreeMap in Scala, TreeMap in Java, and map in the C++ standard library.