Set Notation

$$ \def\hl\{\bbox[yellow]} \def\constant\{\enclose{circle}[mathcolor="blue"]} \def\term\{\enclose{circle}[mathcolor="green"]} \def\U\{\mathbb{U}} \def\N\{\mathbb{N}} \def\Z\{\mathbb{Z}} \def\Q\{\mathbb{Q}} \def\R\{\mathbb{R}} \newcommand{\green}[1]{\textcolor{green}{#1}} \newcommand{\blue}[1]{\textcolor{blue}{#1}} $$
graph LR
    classDef currentPage stroke:#333,stroke-width:4px

	ALG(["fas:fa-trophy Algorithmis fas:fa-trophy "])

	ASY_ANA(["fas:fa-check Asymptotic Analysis#160;"])
    click ASY_ANA "./math-asymptotic-analysis"

    

	MAT_NOT(["fas:fa-check Mathematical Notation#160;"])
    click MAT_NOT "./math-notation"

    

	POL(["fas:fa-check Polynomials #160;"])
    click POL "./math-polynomials"

    

	MAT_FUN(["fas:fa-check Math Functions#160;"])
    click MAT_FUN "./math-functions"

    

	LOG(["fas:fa-check Logarithms#160;"])
    click LOG "./math-logarithms"

    

	COM(["fas:fa-check Combinatorics#160;"])
    click COM "./math-combinatorics"

    

	SET_NOT(["fas:fa-check Set Notation#160;"])
    click SET_NOT "./math-set-notation"

    
      class SET_NOT currentPage
    

	GRA(["fas:fa-check Graphing#160;"])
    click GRA "./math-graphing"

    

	ASY_ANA-->ALG
    COM & GRA & SET_NOT-->ASY_ANA
    MAT_NOT--> SET_NOT
    POL & LOG--> MAT_FUN
    MAT_FUN--> GRA

Set theory is an absolutely fascinating branch of mathematics. It’s intuitively simple on the surface; however, it’s incredibly deep and has far-reaching implications. “The axioms of set theory imply the existence of a set-theoretic universe so rich that all mathematical objects can be construed as sets”1. Fortunately or perhaps unfortunately depending on your perspective, the portion of set theory that applies to algorithms is diminutive albeit ubiquitous. This page focuses on the set of concepts (pun intended) that apply to algorithm design and analysis.

This section has an ancillary purpose as it also serves as an introduction to mathematical notation in general. It’s important to become fluent in written math for two reasons. The first is that reading and writing proofs is requisite for algorithm mastery. The second is that it is an efficient medium for communicating dense concepts. The concise symbols that comprise the language of math is far more expressive and precise than natural languages such as English, Spanish, etc…

Those in the software field often struggle with mathematical notation because written math, while far more precise than natural language, is not nearly as Draconian as typical programming languages. Miswritten syntax in math has no impact (assuming it’s still comprehensible). A syntax error while programming results in a compiler error. Remember that mathematical notation is a matter of convention and authors tend to interpret the convention loosely. There are several correct ways to write the same thing. For instance, $A^\prime$, $A^\complement$, and $A^\sim$ are all equivalent. What’s worse, it’s also common to see the same symbol with a different meaning depending on the context. For instance, the symbol $\Sigma$ denotes a summation; however, it’s not uncommon to define it to be an arbitrary variable. Deriving meaning from mathematical notation takes practice. Do not be discouraged if it isn’t painfully obvious at first. Keeping all this in mind, understand that every attempt is made to showcase the most common representations; however, it’s highly likely that you will encounter variations.

PRO TIP: Bookmark this page in the event that you encounter a symbol you don’t recognize.

History

Georg Cantor introduced set theory in his 1874 paper entitled On a Property of the Collection of All Real Algebraic Numbers. Although controversial at the time, set theory was a major breakthrough in mathematics. Georg’s paper essentially proved that there are multiple sizes of infinity. The concept is so powerful that it is used as a foundation for all of mathematics.

Set theory sustained a crippling blow in the early 1900s when Bertrand Russell formulated what is know as Russell’s Paradox. Mr. Russell wrote about it in a book that he co-authored with Alfred North-Whitehead entitled Principia Mathmatica2. The book was published in 1903. In a nutshell, a logical inconstancy arises when trying to formulate the set of all sets that are not members of themselves. The said set is only a member of itself if it’s not a member of itself. Luckily, set theory was so incredibly useful that it was not abandoned. Ernet Zermelo and Abraham Fraenkel reformulated set theory to alleviate the paradox. Georg’s original theory is now referred to as naive set theory and the more complete theory is called axiomatic or Zermelo-Fraenkel set theory.

In the domain of computer science, naive set theory is more than adequate. All of the relevant concept are covered here. Although the wider topic of set theory is not directly applicable to algorithms, it’s an absolutely fascinating topic that anyone will enjoy exploring.

What is a Set?

Set theory concerns grouping objects, known as members or elements, into well-defined unordered sets. All the items within a set are unique; that is, duplicates are not allowed. This is a familiar concept as it’s taught in grade school. As an example, consider the Kardashians; each family member (Kim, Khloe, Kourtney, Rob, etc…) is an element in the Kardashian set. A more formal example is the classification of the animal kingdom into sets known as phyla, class, order, family, and genus. Before continuing on, take a minute and try to identify a few well-defined sets.

Sets are depicted as objects inside curly braces and are denoted by capital letters. For instance, is the mathematical representation of the set $A$ with the members $1$, $2$, and $3$. Sets can contain other sets such as . The symbol $\emptyset$, alternatively , represents the empty set (set with no objects). Conversely, $\U$ is the universe (aka universal set or universe of discourse) which contains all possible elements in an area of interest. The number of items included in a set, represented by surrounding vertical bars, is called the cardinality3 of the set. $\vert A \vert$ means the “cardinality of $A$”. As $A$ is defined in this paragraph, $\vert A \vert=3$.

Summary

Symbol Description
Set containing the members $1$ and $2$
$\emptyset$ Empty set — set with no members
$\U$ Universe — set with all members in the area of interest
$\vert A\vert$ Cardinality (number of items in set) of set $A$

$\U$ Number Sets

It’s common to constrain the types of numbers that an algorithm accepts or generates. Set theory defines several sets of numbers based on their properties. See the table below.

Symbol Name Description
$\N$ Natural (aka Counting) All positive whole numbers including 04
$\Z$5 Integers $\N$ + negative whole numbers
$\Q$ Rational (Quotient) $\Z$ + numbers representable as fractions $\frac{1}{2}$ or $\frac{3}{4}$
$\R$ Real $\Q$ + irrational numbers including algebraic numbers (e.g. $\sqrt{2}$) and transcendental numbers (e.g. $\pi$ and $e$)

There are many more well-defined numbers sets6; however, the above is sufficient for the purposes at hand. The astute reader may have noticed that each successive set encompasses the previous set. This is by design. The Venn diagram below represents the relationship between the number sets.

Number Sets

Another way to express the diagram above is $\N \subset \Z \subset \Q \subset \R$. The next section elaborates on the $\subset$ symbol.

When a set in comprised of elements from a larger set, it is said to be a subset of that set. From the opposite perspective, a larger set that contains all items in a smaller set is a superset. This is depicted graphically below.

Subset

The $\subset$ symbol indicates a subset relationship and a $\supset$ symbol denotes a superset relationship as shown in mathematical notation below.

Strictly speaking, what’s shown above are proper subsets and supersets. The proper distinction means that the subset is not equal to the superset or vice versa. If the possibility exists that the two sets are equals the symbols $\subseteq$ and $\supseteq$ are used as shown below.

Stated differently, if $\alpha \subseteq \sigma$ then $\sigma$ contains every element that $\alpha$ contains. If $\alpha \subset \sigma$ then $\sigma$ contains every element that $\alpha$ contains and some additional elements. Although it may seem like a pedantic distinction, it’s valuable in practice.

There is also convenient notation for indicating the lack of a relationship. A $/$ through a symbol negates the relationship. Each of the set symbols shown above has an equivalent negated symbol: $\not\subseteq$, $\not\subset$, $\not\supseteq$, and $\not\supset$. See the mathematical statements below.

A final important relationship is a set’s complement. The complement of a set is all the items in $\U$ that are not in the set. See the image below. The complement of $A$ in mathematical notation is $A^\prime$. It is also common to see $A^\complement$, $\overline{A}$, or $A^\sim$.

Complement

As is evident by now, there are many symbols. Don’t be alarmed if they are difficult to remember. They are all summarized below for easy referral.

Summary

Symbol Name Example Negation
$\subseteq$ Subset $\not\subseteq$
$\subset$ Proper Subset $\not\subset$
$\supseteq$ Superset $\not\supseteq$
$\supset$ Proper Superset $\not\supset$
$^\prime$ Complement  

Building a Set

The elements belonging to a well-defined set share certain properties as defined by the set. This section describes the notation for describing those properties. The first task is to denote set membership. The symbol $\in$, read as “is a member of”, indicates that an element belongs to a set. For instance, $\sigma \in A$ indicates that the element $\sigma$ is included in the set $A$. Read aloud, it says “$\sigma$ is a member of $A$”. Contrarily, the symbol $\notin$ indicates the opposite. $\sigma \notin A$ is read as “$\sigma$ is not a member of $A$”. See the examples below.

As has already been demonstrated, one way of defining a set is to list all the members inside s (e.g. ). Inconveniently, many sets are so large that listing all the elements isn’t a possibility. One way to overcome this limitation is to use a $\ldots$ symbol to denote a sequence. For instance, the notation for a set of whole numbers between $1$ and $100$ is:

Omitting the ending or beginning number indicates that the sequence extends through infinity as illustrated below.

Strictly speaking, the remaining symbols in this section are predicate logic symbols rather than set notation. However, this is a pedantic distinction. It makes sense to introduce them here because they are often seen together.

Not all sets conform to a sequence. In these cases, the $\vert$ symbol combined with a predicate suffices. $\vert$ is the such as symbol. A predicate is an expression that accepts a single argument and returns a true or false value. Any value that satisfies the predicate (yields a true result) is a member of the set. This is best demonstrated with an example. The statement below reads: the set of all elements from the real numbers set such that $x \lt 0$. Stated differently, the set of all negative real numbers.

It’s also possible to express set assertions in mathematical notation. The $\forall$ symbol, read as for all, represents all members in a set. The following statement reads: for all members in the $\N$ set, the member is $\geq 0$. Alternatively, every member in $\N$ is greater than or equal to zero.

Another common symbol is $\exists$ which is known as the existential qualification. It asserts that at least one item exists that satisfies the specified predicate. The statement below reads, there exists at least one element in $\N$ that is even.

The notation introduced up to this point concerns defining sets and their elements. The following section outlines set operations.

Summary

Symbol Name Example
$\in$ is a member of
$\notin$ is not a member of
$\ldots$ sequence
$\vert$ such that
$\forall$ for all
$\exists$ there exists

Set Operations

Similar to numbers, sets support basic arithmetic operations. This section outlines five such operations. The first is the set union represented by the $\cup$ symbol. A result of a union operation on two sets is a new set with all the distinct elements from both sets combined. Notice the word distinct; if an item exists in both sets, it’s not duplicated. See the example below.

The $\cap$ symbol represents a set intersection. The intersection of two sets is the items that both sets have in common as shown below.

Taking the difference of two sets works the same way as it does with numbers. Subtracting $A$ from $B$ results in a set with all the items in $B$ that are not in $A$. The set difference symbol ($-$) is even the same as the symbol used for subtracting numbers. See the example below.

The three operations outlined above are fairly innocuous. However, the multiplication operation can seem a bit strange on first inspection. Multiplying two sets generates the Cartesian product of the sets. The Cartesian product is the set of ordered pairs (2-tuples7) with the first element from the first set and the second from the second set. This is easier to explain using set builder notation as shown below.

Stated differently, the Cartesian product of $A$ and $B$ is the set of all ordered pairs $(a, b)$ where $a$ is in $A$ and $b$ is in $B$. See the graphical depiction below.

The depiction above is meant to provide an illustration of what actually occurs when multiplying two sets. The statement below is the proper notation.

The final operation is flatten or flat map, represented by the $\bigcup$ symbol, and it works on sets of sets. It takes all the sets and flattens them into a single flat set containing the distinct items from all the sets. See the example below.

Notice how, just like the union operation, the result is the distinct items from all the sets. Thus concludes the demonstration of the five set operations.

Summary

Quick Review

A quick review of a few mathematical properties.

  • Commutative property: Changing the order of the operands does not change the result: Assuming $\sigma$ is some arbitrary operation, $\sigma$ is commutative if $3 \sigma 4 = 4 \sigma 3$
  • Associative property: The order in which the operations are preformed does not matter. Assuming $\sigma$ is some arbitrary operation, $\sigma$ is associative if $(2 \sigma 3) \sigma 4 = 2 \sigma (3 \sigma 4)$
Symbol Name Example Properties Identity
$\cup$ Union Commutative, Associative $A \cup \emptyset = A$
$\cap$ Intersection Commutative, Associative $A \cap \emptyset = \emptyset$, $A \cap \U = A$
$-$ Difference   $A - \emptyset = \emptyset - A = \emptyset$
$\times$ Cartesian Product   $A \times \emptyset = \emptyset \times A = \emptyset$
$\bigcup$ Flatten N/A $\bigcup{\emptyset} = \emptyset$

Exercises

  1. Specify if each statement is true of false.
    a. $-1 \in \N$
    b. $\forall x \in \N, x \gt 0$
    c. $\exists \sigma \in \Z, \sigma \bmod 2 = 0$
    d.
    e.
    f.

    Answers (click to expand)
    1. false - Negative numbers are not part of the $\N$ set.
    2. false - zero is included in $\N$ and it is not greater than itself.
    3. true - there is at least even number is the $\Z$ set
    4. false - $a$ _is_ in the set
    5. false - $\Q$ has far more numbers than all the positive whole number including zero.
    6. true - the complement of set $A$ is all the items in $\U$ that are not in $A$
  2. Assuming and , calculate each set operation below:
    a. $A \cup B$
    b. $A \cap B$
    c. $A - B$
    d. $B - A$
    e. $A \times B$

    Answers (click to expand)
    1. $\{1,2,3,a,b,c\}$
    2. $\{1,a\}$.
    3. $\{2,3\}$.
    4. $\{b,c\}$.
    5. $\{(1,1),(1,a),(1,b),(1,c),(2,1),(2,a),(2,b),(2,c),(3,1),(3,a),(3,b),(3,c),(a,1),(a,a),(a,b),(a,c)\}$.
  3. With regard to Cartesian products, answer the following questions.

    1. Regarding $A \times B = \{(a,b) \vert a \in A \; \text{and} \; b \in B\}$. Is $(a,b)$ equivalent to $(b,a)$?
    2. Is $A \times B$ equivalent to $B \times A$.
    Answers (click to expand)
    1. No, $(a,b)$ is NOT equivalent to $(b,a)$. Recall that the output of a multiplication operation is ordered pairs meaning that the order has meaning. Changing the order of the pair changes it's identity.
    2. No, unlike numbers, multiplication of sets is not commutative.
      $A = \{1,2\} \\ B = \{3,4\} \\ A \times B = \{(1,3),(1,4),(2,3),(2,4)\} \\ B \times A = \{(3,1),(3,2),(4,1),(4,2)\} $
  1. https://plato.stanford.edu/entries/set-theory/ 

  2. Ironically, much like Russel’s paradox invalidated naive set theory, Kurt Gödel’s incompleteness theorem invalided the system of logic set forth in the Principia Mathmatica. As with almost everything, XKCD has a great comic about it https://xkcd.com/468/

  3. The term cardinality, coined by the father of set theory Georg Cantor, comes from “cardinal numbers”. That is, numbers indicating quantity. This is useful because numbers are an abstract concept that can mean more than one thing. A number could indicate the position of an element in a sequence as well as the number of elements in a set. 

  4. Some fields of mathematics define $\N$ to include $0$ and others do not. It is also common to encounter the set “Counting Numbers” which comprises all the whole positive numbers excluding $0$ and the set “Whole Numbers” that includes “Counting Numbers” and $0$. Alternatively, $\N_0$ is sometimes used to denote the set “Whole Numbers” and $\N_1$ to represent the set “Counting Numbers”. 

  5. Z came from the first letter of the German word Zahlen, which means number. 

  6. $\mathbb{P}$ prime numbers, $\mathbb{I}$ irrational numbers, $\mathbb{C}$ complex numbers, $\mathbb{H}$ quaternions, $\mathbb{O}$ octonions, $\mathbb{S}$ sedenions to name a few 

  7. A tuple is a finite ordered list of elements. Prefixing a non-negative integer indicates the number of items in the tuple. e.g. 2-tuple is a tuple with two elements.