# Lecture 6: relations

• reading: MCS 4.4, (see 10.11 for defintions used in 10.10)
• finishing up countability:
• $$ℚ$$ is countable, $$ℝ$$ is uncountable
• relations
• binary relations, reflexivity, symmetry, transitivity, equivalence relations

## Countability

We started with a coda on countability.

### $$ℚ$$ is countable

Claim: the set of rational numbers (i.e. fractions, written $$ℚ$$) is countable.

Proof: In fact, we will show that the set of positive rationals, $$ℚ^+$$, is countable. The countability of $$ℚ$$ follows without too much more effort.

We can draw a table containing all of the rational numbers:

1 2 3 4 $$\cdots$$
1 1/1 2/1 3/1 4/1 $$\cdots$$
2 1/2 2/2 3/2 4/2 $$\cdots$$
3 1/3 2/3 3/3 4/3 $$\cdots$$
$$\vdots$$ $$\ddots$$

Note that every positive rational number is contained at least once in the table.

Now we can define a surjection $$f$$ from $$ℕ$$ to $$ℚ^+$$ by tracing each lower-left to upper-right diagonal, and listing the elements as we pass them. For example, the first few rational numbers would be:

n f(n)
0 1/1
1 1/2
2 2/1
3 1/3
4 2/2
5 3/1
6 1/4

For any rational number $$y$$, $$y$$ appears somewhere in the table, which means it lies on some diagonal, which means that eventually it will be output by $$f$$. In other words, there exists some $$n$$ with $$f(n) = y$$; so $$f$$ is surjective. Therefore $$|ℕ| \geq |ℚ|$$, so $$ℚ^+$$ is countable.

Note: this technique is (confusingly) not called diagonalization. Diagonalization refers to the technique we use in the following proof:

### $$ℝ$$ is uncountable

Claim: The set of real numbers $$ℝ$$ is uncountable.

Proof: in fact, we will show that the set of real numbers between 0 and 1 is uncountable; since this is a subset of $$ℝ$$, the uncountability of $$ℝ$$ follows immediately.

We proceed by contradiction. Suppose that $$ℝ$$ were countable. Then there would exist a surjection $$f : ℕ → ℝ$$. We could expand the digits of $$f$$ in a table; for example, if $$f(0) = 0$$, $$f(1) = 1/2$$, $$f(2) = π - 3$$, $$f(3) = φ - 1$$, then the table would look as follows:

$$n$$ $$f(n)$$ digits of $$f(n)$$
0 0 $$0.00000\cdots$$
1 1/2 $$0.50000\cdots$$
2 $$π - 3$$ $$0.14159\cdots$$
3 $$φ - 1$$ $$0.61803\cdots$$

Given such a table, we can form a real number $$x_D$$ that is not in the table by changing the $$i$$th digit of the $$i$$th number; perhaps by adding 5 (wrapping around, so that 7 + 5 = 2, for example).

In the example, $$x_D = 0.5565\cdots$$.

Now $$x_D$$ cannot be in the table, because $$x_D$$ differs from $$f(i)$$ in the $$i$$th digit. But this contradicts the fact that $$f$$ is surjective, thus completing the proof.

Note: this technique is called diagonalization.

### What good is countability?

• the existence different sizes of infinity is pretty neat.

• diagonalization is used to prove that there are specifications with no program that implements them. One such problem is determining whether a program crashes or not. It would be nice to have a compiler that guarantees that your program never crashes. However, diagonalization can be used to show that no such program exists. We may discuss this more when we talk about computability; it is also discussed in MCS.

• this proof is also quite closely related to notions of truth and provability, which we will discuss later in the course.

## Relations

### Definitions

Definition: a relation on the sets $$A_1, A_2, \dots, A_n$$ is a subset of $$A_1 \times A_2 \times \cdots \times A_n$$. In other words, it is a set of $$n$$-tuples. A binary relation on A is a subset of $$A \times A$$.

Notation: if $$R$$ is a binary relation on $$A$$, and $$x$$ and $$y$$ are in $$A$$, then "$$x R y$$" means $$(x,y) \in R$$.

### Examples

• "≥" and "$$\lt$$" are binary relations on $$ℕ$$. "≥" = $$\{(0,0), (1,0), (1,1), (2,0), (2,1), (2,2), \dots\}$$.

• "=" is a binary relation on any given set. For example, on $$ℕ$$, we have "=" = $$\{(0,0), (1,1), (2,2), (3,3), \dots\}$$.

• "is a brother of" and "is a sibling of" are binary relationships on the set of people.

• "_ got _ on _" is a ternary relation on the sets of students, grades, and problem sets.

### Properties

If $$R$$ is a binary relation on a set $$A$$, then $$R$$ is

• reflexive if for all $$x \in A$$, $$x R x$$
• symmetric if for all $$x, y \in A$$, if $$xRy$$ then $$yRx$$.
• transitive if for all $$x, y, and z \in A$$, if $$xRy$$ and $$yRz$$ then $$xRz$$.
• an equivalence relation if it is reflexive, symmetric and transitive

These properties describe the properties people implicitly assume when working with equality. They are important to check whenever redefining equality. For example, many programming languages let you define an "equals" function (__eq__ in python, .equals() in Java, (=) in OCaml); if you ever do this, you should check that your definition has these properties. Otherwise, your program will almost certainly develop bugs, because you will use the properties without thinking about them. It is actually very hard to get this right when redefining equals in the presence of subclasses.

Here are some examples of the properties; you should check these for yourself:

• $$≥$$ is reflexive and transitive but not symmetric; $$\lt$$ is transitive but not symmetric or reflexive

• $$=$$ is an equivalence relation (which is good since the whole point of equivalence relations is to abstract the properties of "="!)

• "is a brother of" is neither reflexive, symmetric, nor transitive. "is a sibling of" is symmetric, but not reflexive or transitive.

• "is related to" is an equivalence relation on the set of people, if we define it such that everyone is related to themselves.

• the empty relation (in which nothing is related to anything) is symmetric and transitive but not reflexive.