# Sets and sequences

## Contents

## Sets and multisets

- In a
**proper set**(or**set**, for short), each element occurs at most once, that is, no duplications of elements within a set. - 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:**

- In computer science, as opposed to math, sets and multisets are usually dynamic, that is, elements may be inserted and removed.
- 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 be a set of (multi)sets.

- A set is
**inclusion-minimal**(resp.,**inclusion-maximal**) in if (resp., ) for no . - A set is
**cardinality-minimal**(resp.,**cardinality-maximal**) in if (resp., ) for no .

**Remark:**
Typically, 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

- a number , its
**length**, - some
**component type**, and - a mapping .

We say that are the **positions** in the sequence (a.k.a. the **indexes**). The element of sequence at position is denoted by . The element is the **head** of , and the element is the **tail** of .

Consider a comparison on . Then a sequence of length is **sorted** with respect to , if for all .

**Remarks:**

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

## Singleton, pair, triple, quadruple

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

- 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. - 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

- a number , its
**size**, - a
**key type**and a**value type**, - a finite subset (a proper set) , the map's
**keys**, and - a mapping , 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 and each value of occurs at most once.

## Partitions

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

- A
**partition**of a finite or infinite set is a family of subsets of such that- for all , , and
- .

- If is finite, we speak of a
**-partition**, where . - In particular, if , we speak of a
**bipartition**.