Masterclass: The Cartesian-Product View of Relations
Imagine you are building the backend for a modern social media platform.
On one side, you have a set of Users: Alice and Bob.
On the other side, you have a set of Devices: Phone, Laptop, and Tablet.
Before any user logs into any device, your system needs to understand the universe of all possible logins. What are all the potential pairs that could exist?
This universe of all possible combinations is called the Cartesian Product. Once you pick a specific list of actual active logins from that universe, you have created a Relation.
1. The Universe of Possibilities: Cartesian Product
The Cartesian product of two sets, written as (read: " cross "), is the set of all possible ordered pairs where the first element comes from set , and the second element comes from set .
Formal Definition
Given two sets and :
Notice that order matters! The pair is not the same as . The first slot strictly belongs to , and the second slot strictly belongs to .
Real-World Example
Let:
- Set
(1, 2) - Set
("x", "y")
To find , we systematically pair every single element in with every single element in :
Size of the Universe
If set has elements and set has elements, the total number of pairs in is simply:
In our example, and , so pairs.
2. What is a Relation?
Now that we have built the universe of all possible connections (), what is a Relation?
A binary relation from to is simply any subset of the Cartesian product .
That's it! A relation is just a specific selection of pairs chosen from the master grid of possibilities.
Visualizing Relations with a Graph
Let us map Set (1, 2) to Set ("x", "y").
If we define a relation containing only the pairs (1, "x") and (2, "y"), we can draw it as a directed mapping:
graph LR
subgraph Set_A ["Set A"]
1
2
end
subgraph Set_B ["Set B"]
x
y
end
1 -->|R| x
2 -->|R| y
Key Insight: Relations vs. Functions
In introductory algebra, you learned about functions. A function is a strict rule where every input gets exactly one output.
Relations are much more flexible:
- An element in can connect to multiple elements in .
- An element in can connect to zero elements in .
- An element in can be connected to by multiple elements in , or none at all.
Every function is a relation, but not every relation is a function.
3. How Many Relations Can Exist? (Combinatorics)
Here is a classic computer science interview question:
If Set has 3 elements and Set has 2 elements, how many unique binary relations can be formed from to ?
Let's break down the logic step-by-step:
First, find the total number of pairs in the Cartesian Product :
A relation is formed by deciding, for each of these 6 pairs, whether to include it or exclude it.
Think of each pair as a binary light switch:
- 1 = Pair is included in
- 0 = Pair is excluded from
Since there are 6 independent binary choices, the total number of unique relations is:
4. Code Implementation
Let me demonstrate how this works programmatically using Python. We can generate the Cartesian product and test whether a custom relation is a valid subset.
import itertools# Define our two setsset_A = {1, 2}set_B = {'x', 'y'}# 1. Compute Cartesian Product (A x B)cartesian_product = set(itertools.product(set_A, set_B))print("Cartesian Product (A x B):")print(cartesian_product)# Output: {(1, 'x'), (1, 'y'), (2, 'x'), (2, 'y')}# 2. Define a relation R (a subset of A x B)relation_R = {(1, 'x'), (2, 'y')}# 3. Verify if R is a valid binary relationis_valid_relation = relation_R.issubset(cartesian_product)print("\nIs relation_R a valid relation from A to B?")print(is_valid_relation) # Output: True# 4. Calculate total possible relations from A to Btotal_relations = 2 ** (len(set_A) * len(set_B))print(f"\nTotal possible relations between A and B: {total_relations}")# Output: 165. Mini Practice Challenge
Now it's your turn to test your understanding!
Question
Let and .
- List all elements of the Cartesian product .
- What is the maximum number of unique relations that can be constructed from to ?
- Is a valid binary relation from to ?
Solution Walkthrough
Cartesian Product :
Pair every element in with every element in :Maximum Number of Unique Relations:
and .
Total possible pairs .
Total unique relations .Validity of :
Every element in —namely , , and —is present inside . Therefore, , making a valid binary relation.