This unit introduces the notion of relations and is for Week 6. The unit will introduce:
- Basic Relations, Creating Relations
- Relation Composition, Relation Inversion
- Transitivity, Symmetry, Anti-Symmetry, Reflexivity
Unit Introduction
In this unit, we’ll move on and extend Unit 2 on sets and talk about a special type of sets, 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 study set theory and relations for computer science students. Another deeper reason presents itself in distributed systems, and a lot of the language about relations is very useful there as well.
That 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 2 separate ways to motivate this. Let’s talk about both ways:
Application: 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.
So 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, can call this a relation. Why? We are relating the names to emails. We are establishing an association, or a relation 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: 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 in the last 1-2 decades found its way into computer science as well.
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.
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, perhaps 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 3 pairs: |
Notice here that if we let be the set of all possible emails, and be the set of all possible names, 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 , that represented the following relations?
- Set relates the student names to the year of enrollments.
- Set relates the student names to the degree programmes.
Try for yourself to write down what those sets look like, and what are they a subset 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 .
Formally, we will consider any set that is a subset of some cartesian product of sets to be a relation. Alternatively, any set of pairs is 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 . Whereas because has remainder , but has remainder .
Question
What about if we wanted to relate 3 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.
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
Given a relation , the inversion of a relation is written as , and is defined in the following way:
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 3 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 .
So for example . .
Example
Let .
Since , this means . For the same reason, . Also, since , then .
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. We will say if bus stop is directly before stop .
So for example, looks something like this:
What if we wanted to say that we wanted to take the 284 bus for exactly two stops? Well then we could write this as the composition of with , denoted by . And it is such that:
Notice how the reason why 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. So this is different from the previous example, but a perfectly valid way to also make a relation.
So, this means 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. So 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 .
In General:
The general definition of a composition of relations is the following. Let , let , then:
In English, this is basically just saying:
and are related by if we can find a such that is related to this using relation , and the same is related to using 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 . Or 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 a name first here.
The Divisibility Relation
Let the set be such that:
Then we will call the divisibility relation. Here we will only consider natural numbers. So for example . And . But , and .
Congruent Modulo Relation
Let fix , then let the set be a relation be such that:
Then we will 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 they are not related via relation . On the other hand, are not related via relation , but are related via relation .
Properties About 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 these topics, we will restrict our attention to relations of the form . In other words, the relate items in the same set . (We will not be considering relations , where .)
Reflexivity
Consider a relation . We will say is reflexive if the following holds:
Here’s a pictorial example:
Let . Then the relation on the left is . Since (notice that in the picture there are self-loops from each element of ). So the 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 . Which again, confirms that the on the right is not reflexive. The pictorial intuition is that some element is missing a self-loop.
So let’s look at the certain natural mathematical relations 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 , arbitrarily chosen.
- [Basic algebra]
- [Basic algebra]
- [Basic algebra]
- [Conjunction of lines 2, 4]
- [Existential generalisation on lines 2, 3]
- [Conjunction on lines 1, 6]
- [Definition of ]
- [Universal generalisation on lines 1, 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 , arbitrarily chosen.
- [Basic algebra]
- [Basic algebra]
- [Existential generalisation on lines 3, 4]
- [Conjunction on lines 1, 4]
- [Definition of ]
- [Universal generalisation on lines 1, 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, 4]
- [Logically equivalent to line 5]
Symmetry
Consider a relation . We will say is symmetric if the following holds:
In English:
For all possible , and , if is related to via relation , then is related to via relation .
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 . Similarly, since is related to , we must have is related to .
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 it 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 .
So 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 , arbitrarily chosen.
- Let , arbitrarily chosen.
- Assume .
- [Definition of ]
- Let be such that [Existential instantiation of line 3.1]
- [Basic algebra]
- [Basic algebra]
- [Existential instantiation of line 3.5]
- [Definition of ]
- [Introduction implication on lines 3, 3.6]
- [Universal generalisation of lines 2, 4]
- [Universal generalisation of lines 1, 5]
Anti-Symmetry
Consider a relation . We will say is anti-symmetric if the following holds:
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 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”. Like is related to but not the other way around.
On the other hand, on the relation on the right, is related to and is related to but is not the same as . So 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 , arbitrarily chosen.
- Let , arbitrarily chosen.
- Assume that
- [Specialisation on line 3]
- [Definition of ]
- Let such that [Existential instantiation of line 3.2]
- [Specialisation on line 3.3]
- [Specialisation on line 3]
- [Definition of ]
- Let such that [Existential instantiation of 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, 4]
- [Universal generalisation on lines 1, 5]
Congruence is not anti-symmetric
What about the congruence modulo relation ? This one is probably quite straight-forward. 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 .
Transitivity
Consider a relation . We will say is transitive if the following holds:
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 . When 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. On the other hand, in the right side, both and are related, but are not related, so the relation on the right hand side 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 , arbitrarily chosen.
- Let , arbitrarily chosen.
- Let , 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, 4.10]
- [Universal generalisation on lines 3, 5]
- [Universal generalisation on lines 2, 6]
- [Universal generalisation on 1, 7]
Congruence is transitive
Theorem
Let .
Then is transitive.
Proof:
- Let , arbitrarily chosen.
- Let , arbitrarily chosen.
- Let , 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, 4.8]
- [Definition of ]
- [Implication introduction on lines 4, 4.10]
- [Universal generalisation on lines 3, 5]
- [Universal generalisation on lines 2, 6]
- [Universal generalisation on lines 1, 7]
In summary:
Here’s a table that summarises what we have proven about the two relations we’ve been using:
Relation | Reflexivity | Symmetry | Anti-symmetry | Transitivity |
---|---|---|---|---|
Divisibility, | Yes | No | Yes | Yes |
Congruence Modulo | Yes | Yes | No | Yes |
Relations in Computer Science (Bonus)
The 4 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 message and program ordering.
Here’s some example slides of relations being used in very high level computer science: