Set Operations


10 min.   |   Beginner   |   (Hammack 2013)

Union, Intersection, and Difference

Suppose \mathcal{A} and \mathcal{B} are sets.

A union of \mathcal{A} and \mathcal{B} is the set: \mathcal{A} \cup \mathcal{B} = \{x: x \in \mathcal{A} \text{ or } x \in \mathcal{B} \}

A intersection of \mathcal{A} and \mathcal{B} is the set: \mathcal{A} \cap \mathcal{B} = \{x: x \in \mathcal{A} \text{ and } x \in \mathcal{B} \}

A difference of \mathcal{A} and \mathcal{B} is the set: \mathcal{A} - \mathcal{B} = \{x: x \in \mathcal{A} \text{ and } x \notin \mathcal{B} \}

CautionUnion, Intersection, and Difference: Example 1

Suppose the following sets \mathcal{A} and \mathcal{B}:

\mathcal{A} = \{1,2\}, \ \ \mathcal{B} = \{2,3,4\} \tag{1}

The union \mathcal{A} \cup \mathcal{B} is the set:

\mathcal{A} \cup \mathcal{B} = \{ 1,2,3,4\}

CautionUnion, Intersection, and Difference: Example 2

The intersection \mathcal{A} \cap \mathcal{B} for the sets mentioned above (1) is:

\mathcal{A} \cap \mathcal{B} = \{ 2 \}

CautionUnion, Intersection, and Difference: Example 3

The difference \mathcal{A} - \mathcal{B} for the sets mentioned above (1) is:

\mathcal{A} - \mathcal{B} = \{ 1 \}

Which of the shaded regions matches the following expression?

(\mathcal{A} \cup \mathcal{B}) \cap \mathcal{C}

The correct answer is the third option. We first take the union of sets \mathcal{A} and \mathcal{B}, which includes all elements that are in either set. Then, we find the intersection with set \mathcal{C}, which includes only those elements that are also in set \mathcal{C}.

The LaTeX command \cup for a union between two sets \cup, \cap for a intersection between two sets \cap, and - for the difference between two sets -.

In Python, you can use the methods union, intersection and difference of a set object. Alternatively, you can also use the |, & and - operators.

Complement

Suppose \mathcal{A} is a set.

A universal set \mathcal{U} is a larger set that encompasses other sets.

The complement of \mathcal{A}, denoted \bar{\mathcal{A}} or \mathcal{A}^c, is the set \bar{\mathcal{A}} = \mathcal{U} - \mathcal{A}.

CautionComplement: Example

Another special set is the one that contains prime numbers \mathbb{P}:

\mathbb{P} = \{2, 3, 5, 7, ...\};

its complement, denoted \bar{\mathbb{P}}, is the set of natural numbers that are not prime:

\bar{\mathbb{P}} = \mathbb{N} - \mathbb{P} = \{1, 4, 6, 8, 9, 10, ...\}

Find \bar{\mathcal{A}}, given:

\mathcal{A} = \{1,2,3\}, \ \ \mathcal{U} = \{0,1,2,3,4,5\}

The correct answer is \{0, 4, 5\}. This is the complement of set \mathcal{A} with respect to the universal set \mathcal{U}. The complement \bar{\mathcal{A}} includes all elements in \mathcal{U} that are not in \mathcal{A}.

The LaTeX command \bar{\mathcal{A}} can be used for the bar notation of the complement \bar{\mathcal{A}}, and \mathcal{A}^c for the complement notation \mathcal{A}^c.

Set Operations: Exercises

Tip Set Operations: Exercises 1-5

Given the following sets:

\mathcal{A} = \{ 1,2,3\} \ \ \mathcal{B} = \{ 2,3,4\} \ \ \mathcal{C} = \{ 1,4,5\};

find:

  1. \mathcal{A} \cup \mathcal{B}
  2. \mathcal{A} \cap \mathcal{C}
  3. \mathcal{B} - \mathcal{C}
  4. \mathcal{A} \cup (\mathcal{B} \cap \mathcal{C})
  5. (\mathcal{A} - \mathcal{B}) \cap \mathcal{C}