# Sets and sequences

## Sets and multisets

1. In a proper set (or set, for short), each element occurs at most once, that is, no duplications of elements within a set.
2. In contrast, in a multiset, an element may occur more than once. The multiplicity of an element in a multiset is the number of its occurrences in that multiset.

Remarks:

1. In computer science, as opposed to math, sets and multisets are usually dynamic, that is, elements may be inserted and removed.
2. Uniting two multisets amounts to adding the multiplicities. To intersect two multisets, take the minimum of the multiplicities. (Multi)set difference is realized by subtracting the multiplicities in the subtrahend from the multiplicities in the minuend.

## Maximal and minimal sets

Let $\mathcal{S}$ be a set of (multi)sets.

1. A set $S\in\mathcal{S}$ is inclusion-minimal (resp., inclusion-maximal) in $\mathcal{S}$ if $S'\subsetneq S$ (resp., $S'\supsetneq S$) for no $S'\in\mathcal{S}\setminus\{S\}$.
2. A set $S\in\mathcal{S}$ is cardinality-minimal (resp., cardinality-maximal) in $\mathcal{S}$ if $|S'|\lt |S|$ (resp., $|S'|\gt |S|$) for no $S'\in\mathcal{S}\setminus\{S\}$.

Remark: Typically, $\mathcal{S}$ is given as all subsets of a ground set that fulfill a certain property.

## Ordered and sorted sequences

An ordered sequence (or sequence, for short) is given by

1. a number $n \in \N_{0}$, its length,
2. some component type $C$, and
3. a mapping $\pi : \{1,...,n\} \rightarrow C$.

We say that $1,...,n$ are the positions in the sequence (a.k.a. the indexes). The element $\pi (i)$ of sequence $S$ at position $i$ is denoted by $S[i]$. The element $S[1]$ is the head of $S$, and the element $S[n]$ is the tail of $S$.

Consider a comparison $c$ on $C$. Then a sequence $S$ of length $n$ is sorted with respect to $c$, if $S[i] \le S[i+1]$ for all $i \in \{1,...,n-1\}$.

Remarks:

1. Note that the first position is $1$, not $0$, as opposed to array indexes in many popular programming languages such as C, C++, and Java.
2. Like sets and multisets, sequences are dynamic.

A set, multiset or sequence with only one, two, three, or four elements is called a singleton, pair, triple, and quadruple, respectively.

## Stacks and queues

1. A stack (a.k.a. LIFO queue, LIFO = last-in-first-out) is a dynamic ordered sequence such that new elements may only be inserted before the head (a.k.a. top), and no element except for the current head may be accessed and removed.
2. A queue (a.k.a. FIFO queue, FIFO = first-in-first-out) is a dynamic ordered sequence such that new elements may only be inserted at the end (that is, after the current tail), and no element except for the head may be accessed and removed.

Remark: For fast access, an implementation of a FIFO queue should maintain an additional pointer to the tail. To update this pointer after an append, simply move it one step forward in the sequence.

## Maps

A map is given by

1. a number $n \in \N_{0}$, its size,
2. a key type $\mathcal{K}$ and a value type $\mathcal{V}$,
3. a finite subset (a proper set) $K \subseteq \mathcal{K}$, the map's keys, and
4. a mapping $K \rightarrow \mathcal{V}$, which assigns a value to each key in the map.

Like sets, multisets and sequences, maps are dynamic in computer science.

Remark: A sequence may also implement a map, namely if its component type is $\mathcal{K} \times \mathcal{V}$ and each value of $\mathcal{K}$ occurs at most once.

## Partitions

Let $I$ be a finite or infinite set of positive integral numbers.

1. A partition of a finite or infinite set $S$ is a family $\{S_i|i\in I\}$ of subsets of $S$ such that
1. $V_i\cap V_j=\emptyset$ for all $i,j\in I$, $i\neq j$, and
2. $\bigcup_{i\in I}S_i=S$.
2. If $I$ is finite, we speak of a $k$-partition, where $k=|I|$.
3. In particular, if $|I|=2$, we speak of a bipartition.