This unit introduces the notion of relations. The unit will introduce:
- Motivation for this unit
- Basic relations, creating relations
- Operations on relations
- Properties of relations
Part 0: Unit Introduction
In this unit, we’ll move on and extend Unit 2 on sets and talk about a special type of set, called relations.
As mentioned, one of the biggest reasons for knowing set operations and relations has to do with databases. It is not the only motivation, but in my opinion it is one of the the most immediately motivating topics for why students of computer science should study set theory and relations. Another deeper reason presents itself in distributed systems, and a lot of the language of relations is very useful there as well.
That being said, relations are interesting for other reasons in their own right for discrete mathematics as well. This unit will talk about relations, cover basic terminology and concepts, and show you how they are useful for databases and distributed systems. Again, the unit will start with terminology, and basic concepts, and end on proofs about such concepts.
Motivating Relations
Our starting point is to talk about what a relation is. And there are two separate ways to motivate this. Let’s talk about both ways:
Application 1: Databases
Let’s say that you had to represent some data. We’ve already seen some examples of this at the start and end of Unit 2 on sets, but let’s try to motivate why data is organised that way. (While TCX1004 is not a course on databases, I think all the same it is great to talk about the relational data model here. You won’t learn about databases until TCX2003, but seeing this in advance won’t hurt.)
Let’s say we collected some data about people and their email addresses, perhaps via survey or something. So here’s the example table we had again:
| Name | |
|---|---|
| Jon Snow | jsnow@gmail.com |
| Barry Allen | the_flash@hotmail.com |
| Pikachu | pika@pokemail.com |
One way to see this is that we actually have 3 pairs from a set that looks something like this:
where is a set that is the Cartesian product of two sets, and . Here, let’s pretend that contains all the possible names in the world, and also contains all the possible email addresses we have in the world.
Based on this terminology, we can say something like . Similarly, we can say .
Here in set theory terminology, something like is called a pair. It is also called a 2-tuple.
Importantly, we can call this a relation. Why? We are relating the names to emails—we are establishing an association, or a relationship between someone’s name and their email.
This way of representing data is called the relational model by people who study and use databases. The concept was coined by Dr. Edgar F. Codd. In some sense, he took the concept of relations in mathematics and decided to use it to represent data. You will see why the model is useful in a class on databases. We are just using this real-life example as a motivation on why understand relations. In the previous unit, we talked about set operations, and those are very much applicable in a database setting. Since to (union / intersect / set minus) your data is a very common form of data manipulation. (See the bonus section at the end of Unit 2.)
Application 2: How to represent orderings
Putting the cart before the horse for a little bit (showing you how and why it’s used before showing you the concept), one big application from a mathematical perspective is the ability to talk about orderings among objects. There is probably one you’re quite familiar with: ordering numbers from sets like or using . Perhaps that seems quite uninteresting, but understanding how to “order” or how to “sort” objects is actually quite a huge area of study, and has found its way into computer science as well in the last one or two decades.
If you ever find yourself studying memory models for concurrency-based applications, or designing a distributed systems algorithm, chances are you will have to know a little bit about partial orders (as special type of relation). The vague idea is that when you need to start saying things like “this memory event happened before the other memory event”, that is also a kind of ordering.
Part 1: Basic Relations, Creating Relations
Definition of a Relation
Let’s think a little bit about how to “relate” two objects (again, using the example from earlier and from the previous unit). If we want to relate people to their emails, then we can think of creating pairs to do this. For example, we might represent our data like this:
| Name | |
|---|---|
| Jon Snow | jsnow@gmail.com |
| Barry Allen | the_flash@hotmail.com |
| Pikachu | pika@pokemail.com |
Mathematically, we will see this our table (let’s call it ) as a set of three pairs:
Notice here that if we let be the set of all possible names and let be the set of all possible emails, then . Note that it is not necessarily the case that . here only contains certain pairs from , not all of them!
here is a relation that relates elements from to elements in . A relation is really just a set that tells us how objects and and are associated.
Here’s another example—we could have had a table of student data that includes:
- Their name
- Their year of enrolment
- Their degree
Perhaps something like this:
| Name | Year of Enrolment | Degree |
|---|---|---|
| Simon | 2023 | CS |
| Janice | 2020 | Philo |
| Meg | 2015 | CS |
| Sam | 2016 | Science |
Then we could have represented this using a set like so:
like a set of triples. But what if we wanted two sets and that represented the following relations?
- Set relates the student names to the year of enrolments.
- Set relates the student names to the degree programmes.
Try for yourself to write down what those sets look like, and what they are subsets of. Check it with yourself in the hidden-away spoiler tag. We can let be the set of all possible names, and we can let .
Answer
can be a subset of something like . Based on our data, it can also be a subset of something like . Both answers are viable.
Answer
can be a subset of something like .
Definition: Relations
Let and be two sets.
A set of pairs is called a relation (on sets and ) if it is a subset of the Cartesian product of the sets, i.e., .
Alternatively, any set of pairs is a relation.
Formally, we will consider any set that is a subset of some Cartesian product of sets to be a relation on those two sets. Alternatively, any set of pairs is a relation.
Here are a few more examples:
Example
Let’s say is the set of all possible foods, and is the set of all possible people. Then we can have a set that is a relation between people and the food they enjoy eating.
Notice here that we can relate one person to more than one food. We can also relate more than one person to one food. In general, since is a subset of , it is considered a relation.
Example
Let .
Then relates integers to other integers that divide it. For example , because divides . Also , because does not divide .
Example
Let .
Then relates integers to integers if they have the same divisor when divided by . For example, because both have the remainder when divided by . Similarly, because both have the remainder . Meanwhile, because has remainder , but has remainder .
Question
What about if we wanted to relate three things together? You will see this very commonly in databases. It is called a ternary relation. In general, a relation that relates things is called an -ary relation.
For the purposes of our course, we will focus only on binary relations, i.e., sets of pairs only.
Part 2: Operations on Relations
Just like sets, there are a few common operations that we need to learn for relations. We will cover the two most common ones:
- Relation inversion
- Relation composition
Relation Inversion
Definition: Relation inversion
Given a relation , the inversion of a relation, denoted , is defined by
Basically, it is just saying that a pair is in if and only if is in . Think of as just “reversing” the pairs in .
Let’s see what this means for our previous three examples.
Example
Let’s say is the set of all possible foods, and is the set of all possible people, then we can have a set that is a relation between people and the food they enjoy eating. Previously, we gave an example of such a set.
Then:
Example
Let .
Recall that , because divides . Also , because does not divide .
For example, and .
Example
Let .
Since , this means that . For the same reason, . Also, since , we have that .
Relation Composition
Next is the relation composition operation. This one is slightly involved, so let me start with a few examples.
Example 1
Let’s say we had a set that relates locations via bus routes. Set might relate the stops of the 284 bus service. We will say if bus stop is directly before stop .

For example, looks something like this:
What if we wanted to say that we wanted to take the 284 bus for exactly two stops? We could write this as the composition of with , denoted by . And it is such that:
Notice how the reason was because there was some value , such that and also . (Namely, .)
Example 2
Let’s try another example, this time around let’s make a set that relates any two MRT stations that are connected via the East-West line. This is different from the previous example, but an equally valid way to make a relation. For example, (even if they are not directly next to each other).
Let’s also make another set that relates any two MRT stations that are connected via the North-South line. For example, .
Okay, so this time around, we have two different relations. Think a little bit about what the relation represents.
relates two MRT stations if we can start from a station that is on the North-South line, and is a station that is on the East-West line.
So for example, . But something like . Also, something like . Can you see why?
Pictorially, here’s what’s going on:

We can say something like , because we know is in and is in .

Definition: Relation composition
Let and be two relations. The composition of and , denoted , is defined by:
In English, this is basically just saying:
and are related by if we can find a such that is related to this by relation , and the same is related to by relation .
If no such exists, then and are not related by .
You can mentally picture this as “Taking one step using , and then taking one step using .”
So let’s go back through the examples we had, and see what happens.
Example
Let’s say is the set of all possible foods, and is the set of all possible people, then we can have a set that is a relation between people and the food they enjoy eating. Previously, we gave an example of such a set.
Then,
Example
Let .
For example, since , and , then .
Do you notice something interesting? In general, we can actually notice that if divides , and divides , then divides (this can be proven). So we can actually argue that if then . In other words, .
Example
Let .
Since and , we can say .
Example
Let , and .
Then and , so .
Classic Examples of Relations
Before moving on, we’ve been commonly using some relations that are good examples for the remaining concepts that we want to talk about, so let’s explicitly give them names here first.
Example 1: Divisibility
Let the set be such that:
Then we will call the divisibility relation. Here we will only consider natural numbers. For instance, and , but and .
Example 2: Congruence modulo
Let us fix , and let the set be a relation be such that:
We call the congruence modulo relation. Intuitively, two integers are related by if they share the same remainder when divided by .
Again, are related via relation , but not via relation . On the other hand, is not related via relation , but are related via relation .
Part 3: Properties of Relations
As you might have noticed, relations by themselves as just sets of pairs are nothing special. However, there are certain properties that certain relations might have. In this section, we will go through them. For this section, we shall restrict our attention to relations of the form . In other words, they relate items in the same set . (We will not be considering relations , where .)
Property 1: Reflexivity
Definition: Reflexivity
A relation is reflexive if and only if
Here’s a pictorial example:

Let . Then the relation on the left is . Since every element of is related to itself, i.e., (notice that in the picture there are self-loops from each element of ), the relation on the left is reflexive. Also notice that , but that is irrelevant. We are only concerned with whether every element is related to itself.
On the other hand, the relation on the right is not reflexive. Why? The relation on the right can be written as . Notice that and yet , so we can say that , which as we know, is equivalent to . This again confirms that the relation on the right is not reflexive. The pictorial intuition is that some element is missing a self-loop.
So let’s examine the two special relations from before and see if they are reflexive.
Divisibility is reflexive
Is the divisibility relation reflexive? Yes. After all, every number divides itself. Let’s see a proof of this.
Theorem
Let .
Then is reflexive.
Proof
- Let be arbitrarily chosen.
- [Basic algebra]
- [Basic algebra]
- [Basic algebra]
- [Conjunction of lines 2 and 4]
- [Existential generalisation on lines 2, 3]
- [Conjunction on lines 1 and 6]
- [Definition of ]
- [Universal generalisation on lines 1 and 8]
So the divisibility relation is reflexive!
Congruence is reflexive
What about congruence modulo relation? Fix —our goal statement is to show that .
Theorem
Let .
Then is reflexive.
Proof
- Let be arbitrarily chosen.
- [Basic algebra]
- [Basic algebra]
- [Existential generalisation on lines 3 and 4]
- [Conjunction on lines 1 and 4]
- [Definition of ]
- [Universal generalisation on lines 1 and 6]
So the divisibility relation is also reflexive!
Here’s an example of a relation that is not reflexive. Let . So for example, are related by , but and are not related by . How do we prove this? Our goal statement is to show that .
Proof
- [Basic algebra]
- [Basic algebra]
- [Logically equivalent to line 1]
- [Definition of ]
- [Existential generalisation on lines 2 and 4]
- [Logically equivalent to line 5]
Property 2: Symmetry
Definition: Symmetry
A relation is symmetric if and only if
In English:
For all possible and , if is related to via relation , then must be related to via relation too.
Notice here this means that if we chose some values and such that is not related to , we don’t care whether is related to or not.
Here’s a pictorial example:

Let . Then the relation on the left is . Notice that since is related to , then we must have that is related to in order for this relation to be symmetric. Similarly, since is related to , we must have is related to for this relation to be symmetric. Since both of these are true, we conclude that the relation on the left is indeed symmetric.
On the other hand, the relation on the right is . Notice there that is related to , but is not related to . So now we can say:
which is logically equivalent to:
which is also logically equivalent to:
which means that the relation on the right is not symmetric.
Divisibility is not symmetric
So is the divisibility relation symmetric? Well, we can think about this intuitively first. If we know that some integer divides some integer , does this mean that divides ? Let’s think about what happens when and . Indeed does divide , but does not divide .
Using the similar reasoning as before, this means that the divisibility relation is not symmetric.
Congruence is symmetric
What about the congruence modulo relation ? Intuitively, and are related because they have the same remainder modulo , so of course it doesn’t matter if we said or first.
Let’s look at the formal proof now.
Theorem
Let .
Then is symmetric.
Proof
- Let be arbitrarily chosen.
- Let be arbitrarily chosen.
- Assume .
- [Definition of ]
- Let be such that . [Existential instantiation on line 3.1]
- [Basic algebra]
- [Basic algebra]
- [Existential instantiation on line 3.5]
- [Definition of ]
- [Implication introduction on lines 3 and 3.6]
- [Universal generalisation on lines 2 and 4]
- [Universal generalisation on lines 1 and 5]
Property 3: Anti-Symmetry
Definition: Anti-symmetry
A relation is anti-symmetric if and only if
In English:
For all possible and , if ( is related to via relation , and also is related to via relation ) then .
That’s the standard way that is it written, but I find that confusing for newcomers. We can instead write it as the following (since it is logically equivalent):
which in English:
For all possible and , if , then either is not related to , or is not related to .
This pretty much tells you that for an anti-symmetric relation, the only time that and can be related to each other is when .
Here’s a pictorial example:

A relation on the left is anti-symmetric because the only time and are related are either: when , or when the relation is “one-sided”. For instance, is related to but not the other way around.
On the other hand, for the relation on the right, is related to and is related to , but is not the same as . Hence, the relation on the right is not anti-symmetric.
Divisibility is anti-symmetric
Is the divisibility relation anti-symmetric? Here’s the intuition: If we know that divides , and we also know that divides , then the only possible case is that . Here’s the proof that confirms this:
Theorem
Let .
Then is anti-symmetric.
Proof
- Let be arbitrarily chosen.
- Let be arbitrarily chosen.
- Assume that .
- [Specialisation on line 3]
- [Definition of ]
- Let such that . [Existential instantiation on line 3.2]
- [Specialisation on line 3.3]
- [Specialisation on line 3]
- [Definition of ]
- Let such that . [Existential instantiation on line 3.6]
- [Specialisation on line 3.7]
- [Basic algebra]
- [Basic algebra]
- [Basic algebra, because are natural numbers]
- [Basic algebra]
- [Implication introduction on lines 3, 3.12]
- [Universal generalisation on lines 2 and 4]
- [Universal generalisation on lines 1 and 5]
Congruence is not anti-symmetric
What about the congruence modulo relation ? This one is probably quite straightforward. Let’s give an example: consider and . They are related via (both is related to , and is related to ), but . So is not anti-symmetric. The same idea works for any .
Property 4: Transitivity
Definition: Transitivity
A relation is transitive if and only if
In English:
If is related to , and is related to , then is related to .
The quickest example that demonstrates this idea is the concept of . If we know that , and we know that , then we know that . So is actually a transitive relation.

Pictorially, because both and are related, we must have that are related too, in order for the relation to be transitive. Hence, the relation on the left is transitive. However, on the right side, both and are related, but are not related, so the relation is not transitive.
To be clear, something like the following relations are also transitive:

Let’s end the chapter by proving our two usual examples of relations are both transitive.
Divisibility is transitive
Theorem
Let .
Then is transitive.
Proof
- Let be arbitrarily chosen.
- Let be arbitrarily chosen.
- Let be arbitrarily chosen.
- Assume .
- [Specialisation on line 4]
- [Definition of ]
- Let such that . [Existential instantiation on line 4.2]
- [Specialisation on line 4]
- [Definition of ]
- Let such that . [Existential instantiation on line 4.5]
- [Specialisation on line 4.3]
- [Specialisation on line 4.6]
- [Basic algebra]
- [Basic algebra]
- [Specialisation on line 4.3]
- [Specialisation on line 4.6]
- [Basic algebra]
- [Existential generalisation on line 4.10]
- [Definition of ]
- [Implication introduction on lines 4 and 4.10]
- [Universal generalisation on lines 3 and 5]
- [Universal generalisation on lines 2 and 6]
- [Universal generalisation on 1 and 7]
Congruence is transitive
Theorem
Let .
Then is transitive.
Proof
- Let be arbitrarily chosen.
- Let be arbitrarily chosen.
- Let be arbitrarily chosen.
- Assume .
- [Specialisation on line 4]
- [Definition of ]
- Let such that . [Existential instantiation on line 4.2]
- [Specialisation on line 4]
- [Definition of ]
- Let such that . [Existential instantiation on line 4.2]
- [Basic algebra]
- [Basic algebra]
- [Existential generalisation on lines 4.7 and 4.8]
- [Definition of ]
- [Implication introduction on lines 4 and 4.10]
- [Universal generalisation on lines 3 and 5]
- [Universal generalisation on lines 2 and 6]
- [Universal generalisation on lines 1 and 7]
In summary:
Here’s a table that summarises what we have proven about the two relations we’ve been using:
| Relation | Reflexive? | Symmetric? | Anti-Symmetric? | Transitive? |
|---|---|---|---|---|
| Divisibility, | Yes | No | Yes | Yes |
| Congruence modulo | Yes | Yes | No | Yes |
Bonus: Relations in Computer Science
The four properties we covered aren’t the only possible properties we care about for relations, but they are the main properties that everyone starts with.
In distributed systems, you might see other properties that they care about, stuff like message and program ordering.
Here’s some example slides of relations being used in very high-level computer science: