TExcelTAD

Sets

A set is a collection of distinct objects, its elements, with no order and no repetition. Sets are the basic data type of mathematics: numbers, functions, relations and graphs are all defined from them. Python's set type follows the same rules, which makes it easy to experiment.

Notation

Notation Meaning
x∈Ax \in A xx is an element of AA
x∉Ax \notin A xx is not an element of AA
{1,2,3}\{1, 2, 3\} the set listed by its elements (roster notation)
{x∈Z∣x2<10}\{x \in \mathbb{Z} \mid x^2 < 10\} set-builder notation: the integers whose square is below 10
∅\emptyset the empty set
∣A∣\lvert A \rvert the number of elements (cardinality) of a finite set

Standard number sets: N={0,1,2,…}\mathbb{N} = \{0, 1, 2, \ldots\} (this course includes 0, as computer scientists usually do), Z\mathbb{Z} the integers, Q\mathbb{Q} the rationals, R\mathbb{R} the reals, C\mathbb{C} the complex numbers.

Since order and repetition do not matter, {1,2,2,3}={3,2,1}\{1, 2, 2, 3\} = \{3, 2, 1\}: both have exactly the elements 1, 2, 3, and ∣{1,2,2,3}∣=3\lvert \{1, 2, 2, 3\} \rvert = 3.

python · try it yourself
A = {1, 2, 2, 3}
print(A, len(A), A == {3, 2, 1})
B = {x for x in range(-5, 6) if x * x < 10}      # set-builder in Python
print("{x in Z : x^2 < 10} =", sorted(B))
print("3 in B?", 3 in B, "  4 in B?", 4 in B)
print("empty set:", set(), len(set()))
{1, 2, 3} 3 True
{x in Z : x^2 < 10} = [-3, -2, -1, 0, 1, 2, 3]
3 in B? True   4 in B? False
empty set: set() 0

Note: In Python {} is an empty dictionary; the empty set is set().

Subsets and equality

A⊆BA \subseteq B (AA is a subset of BB) means every element of AA is in BB: ∀x. x∈A→x∈B\forall x.\ x \in A \to x \in B. A⊂BA \subset B (a proper subset) means A⊆BA \subseteq B and A≠BA \ne B. Two sets are equal exactly when each is a subset of the other:

A=B  ⟺  A⊆B∧B⊆AA = B \iff A \subseteq B \land B \subseteq A

This gives the standard way to prove two sets are equal: show "⊆\subseteq" in both directions. Every set has ∅\emptyset and itself as subsets (∅⊆A\emptyset \subseteq A is vacuously true).

The power set

The power set P(A)\mathcal{P}(A) is the set of all subsets of AA. If ∣A∣=n\lvert A \rvert = n, then ∣P(A)∣=2n\lvert \mathcal{P}(A) \rvert = 2^n: each element is either in a subset or not, two choices per element. This matches the bitmask trick: a subset of an nn-element list is an nn-bit number.

python · try it yourself
items = ["a", "b", "c"]
n = len(items)
subsets = [{items[i] for i in range(n) if mask >> i & 1} for mask in range(2 ** n)]
for mask, s in enumerate(subsets):
    print(f"{mask:03b} -> {sorted(s)}")
print("|P(A)| =", len(subsets))
000 -> []
001 -> ['a']
010 -> ['b']
011 -> ['a', 'b']
100 -> ['c']
101 -> ['a', 'c']
110 -> ['b', 'c']
111 -> ['a', 'b', 'c']
|P(A)| = 8

Elements of a set can be sets themselves: {∅,{1}}\{\emptyset, \{1\}\} has two elements, and ∅≠{∅}\emptyset \ne \{\emptyset\} (the first has 0 elements, the second has 1).

Russell's paradox

Can any description define a set? Consider R={S∣S∉S}R = \{S \mid S \notin S\}, the set of all sets that are not members of themselves. Is R∈RR \in R? If yes, then by definition R∉RR \notin R; if no, then RR satisfies the condition, so R∈RR \in R. Both answers are contradictory. Modern set theory (ZFC) avoids this by allowing set-builder notation only to select elements from an existing set: {x∈A∣P(x)}\{x \in A \mid P(x)\}.

In software

Sets give O(1)O(1) average membership tests via hashing, so if user_id in seen: with a set is much faster than with a list. Python sets only hold hashable (immutable) values; frozenset is an immutable set that can itself be an element of a set, which is how you build P(A)\mathcal{P}(A) as a real Python set.

Exercises

Exercise 1: Roster form

List the elements of (a) {x∈N∣x<5}\{x \in \mathbb{N} \mid x < 5\}, (b) {x∈Z∣x2=4}\{x \in \mathbb{Z} \mid x^2 = 4\}, (c) {x∈N∣x2=2}\{x \in \mathbb{N} \mid x^2 = 2\}.

Answer

(a) {0,1,2,3,4}\{0, 1, 2, 3, 4\}; (b) {−2,2}\{-2, 2\}; (c) ∅\emptyset.

Solution

(a) uses 0∈N0 \in \mathbb{N}. (b) both 22 and −2-2 square to 4. (c) 2\sqrt{2} is not a natural number, so no element satisfies the condition.

Exercise 2: Count elements

What are ∣{∅}∣\lvert \{\emptyset\} \rvert, ∣{∅,{∅}}∣\lvert \{\emptyset, \{\emptyset\}\} \rvert, ∣P({1,2,3,4})∣\lvert \mathcal{P}(\{1, 2, 3, 4\}) \rvert and ∣P(∅)∣\lvert \mathcal{P}(\emptyset) \rvert?

Answer

1, 2, 16, 1.

Solution

{∅}\{\emptyset\} contains one element, the empty set. {∅,{∅}}\{\emptyset, \{\emptyset\}\} contains two different elements. A 4-element set has 24=162^4 = 16 subsets. The empty set has exactly one subset, itself, so P(∅)={∅}\mathcal{P}(\emptyset) = \{\emptyset\} and 20=12^0 = 1.

Exercise 3: Power set with frozensets (code)

Build P({1,2,3})\mathcal{P}(\{1, 2, 3\}) as a Python set of frozensets and check that it has 8 elements and contains both ∅\emptyset and {1,2,3}\{1, 2, 3\}.

Answer

8, True, True.

Solution
python · try it yourself
from itertools import combinations

A = {1, 2, 3}
P = {frozenset(c) for k in range(len(A) + 1) for c in combinations(A, k)}
print(len(P), frozenset() in P, frozenset(A) in P)
8 True True

Test yourself

What is ∣{a,b,a,c,b}∣\lvert \{a, b, a, c, b\} \rvert?

  1. 5
  2. 3
  3. 2
  4. 4

How many subsets does a 10-element set have?

  1. 10
  2. 100
  3. 1024
  4. 10!10!

Which statement is true for every set AA?

  1. ∅∈A\emptyset \in A
  2. ∅⊆A\emptyset \subseteq A
  3. A∈AA \in A
  4. A⊂AA \subset A