In our first couple of lessons, we've been building a precise language for making logical statements. We started with propositions and connectives, then expanded our reach with quantifiers to talk about properties of entire "domains" of objects. This lesson marks a shift from the language of logic to the objects themselves. We will formalize the concept of a "domain" or "collection" by studying sets.
Your goal for this lesson is to learn the fundamental ways of combining and manipulating sets. Specifically, we will cover how to perform union, intersection, difference, and Cartesian product operations on finite sets. These operations are the absolute bedrock of discrete mathematics and have direct, practical applications in computer science, from database queries to defining user permissions.
Building New Sets: Union, Intersection, and Difference
At their core, the three most basic set operations are direct analogues of the logical connectives "OR", "AND", and "AND NOT" that we've already discussed.
- Union (): The union of two sets A and B contains every element that is in A or in B (or in both).
- Intersection (): The intersection of A and B contains only those elements that are in A and in B.
- Difference ( or ): The difference of A and B contains the elements that are in A and not in B.
The following video provides an excellent visual and spoken-word introduction to these three concepts, using Venn diagrams to help build your intuition.
Discrete Math - 2.2.1 Operations on Sets
Watch the video "Discrete Math - 2.2.1 Operations on Sets" by Kimberly Brehm.
Pay close attention to the first three main sections of the video: The Union of Sets, which she helpfully relates to a cup you pour everything into. The Intersection of Sets, where she explains the concept of "disjoint" sets (sets with no common elements). The Difference of Sets, which is essentially subtraction. After these explanations, follow along as she works through the practice problem to see these operations applied to concrete examples.
Now that you have a strong visual and intuitive grasp, let's look at the formal definitions. These definitions are what we use in mathematical proofs and are crucial for precise communication.
This reading from the "Discrete Mathematics" text on Runestone Academy provides the formal definitions and further examples.
Please read the following parts of the "Set Theory" chapter: In the main text, locate the definitions for Union (Definition 6.1.4), Intersection (Definition 6.1.6), and Set Difference (Definition 6.1.8). Notice how the logical words "or" and "and" map directly to the set operations. Next, review Example 6.1.12 which applies these operations to intervals of real numbers. Finally, work through Activity 6.1.1 which uses the finite integer sets that are the main focus of our learning outcome. You can check your answers in the textbook interface.
From Sets to Ordered Pairs: The Cartesian Product
Union, intersection, and difference all produce a new set containing elements of the same "type" as the original sets. The Cartesian product is different: it takes two sets and creates a new set of ordered pairs.
An ordered pair (a, b) is not the same as the set {a, b}. In the set, order doesn't matter, so {a, b} is identical to {b, a}. In the ordered pair, order is critical, so (a, b) is different from (b, a) unless a = b. You've encountered ordered pairs constantly in algebra when plotting points (x, y) on a graph.
The Cartesian product (read "A cross B") is the set of all possible ordered pairs (a, b) where a is an element of A and b is an element of B.
This concept is fundamental for understanding relations and functions, which we'll see soon. It's also critical in computer science for things like generating all possible test cases (e.g., set of browsers set of operating systems) or in database theory when joining tables.
The following video from TrevTutor provides a very clear explanation of ordered pairs and how to construct Cartesian products.
CARTESIAN PRODUCTS and ORDERED PAIRS - DISCRETE MATHEMATICS
Watch the video "CARTESIAN PRODUCTS and ORDERED PAIRS" to understand this new type of set operation.
Focus on these key segments: The idea of an ordered pair, contrasting it with a set. The definition of the Cartesian product and a step-by-step construction. How A × B differs from B × A, which highlights the importance of order. How to find the size (cardinality) of a Cartesian product. This is a very useful property. The worked exercise at the end provides a great summary.
Set Operations in Python
Given your background in Python, it's useful to see how directly these mathematical concepts map to code. Python's set data type is a direct implementation of the mathematical idea of a finite set.
Let's define two sets:
# A is the set of vowels
A = {'a', 'e', 'i', 'o', 'u'}
# B is the set of letters in 'cybersecurity'
B = set('cybersecurity')
# Python automatically handles duplicates, so B is {'c', 'y', 'b', 'e', 'r', 's', 'u', 'i', 't'}
Now we can perform the operations:
- Union:
A | BorA.union(B)>>> A | B {'o', 'c', 't', 'b', 'y', 'i', 's', 'r', 'a', 'e', 'u'} - Intersection:
A & BorA.intersection(B)>>> A & B {'e', 'i', 'u'} # The vowels present in 'cybersecurity' - Difference:
A - BorA.difference(B)>>> A - B {'a', 'o'} # Vowels that are NOT in 'cybersecurity'
For the Cartesian product, there's no simple operator, but the itertools module in the standard library provides the product function for exactly this purpose.
import itertools
A = {1, 2}
B = {'x', 'y'}
# itertools.product returns an iterator, so we convert it to a list to see the elements
cartesian_product = list(itertools.product(A, B))
print(cartesian_product)
# Output: [(1, 'x'), (1, 'y'), (2, 'x'), (2, 'y')]
This is a great example of how mathematical foundations are built directly into the programming languages you use.
Practice and Consolidation
To solidify your understanding, please return to the "Set Theory" reading and complete the following exercises. These will test your ability to apply all four operations to finite sets of integers.
Now, let's put it all together with some practice problems from the same textbook page.
Work through these exercises on the page. You can input your answers to check them. Start with Reading Question 1. This is a matching exercise covering all the operations we've discussed. Pay special attention to the B x D and B x B questions. Then, complete all parts of Exercise 3, which gives you three sets (A, B, C) and asks you to compute various unions, intersections, and differences.
Conclusion
In this lesson, we have defined and practiced the four fundamental operations for building new sets from existing ones. These tools are essential for working with collections of objects in a structured way.
Key Takeaways:
- Union () combines elements using a logical OR.
- Intersection () selects common elements using a logical AND.
- Difference () removes elements from one set that are present in another.
- Cartesian Product () creates a new set of all possible ordered pairs from two sets.
- These abstract mathematical operations have direct, high-performance implementations in programming languages like Python.
In our next lesson, we will continue our study of sets by formalizing concepts we've already touched upon: membership (), subsets (), and cardinality (). This will give us the vocabulary to make precise statements about the relationships between sets and their elements.