Relations
A relation records which ordered pairs are connected by a rule. Functions are special relations, but relations are more flexible: one input may be related to many outputs, to no outputs, or to itself. They model databases, directed graphs, modular congruence, ordering, reachability, and equivalence.
Relations are the common language behind many discrete structures. A directed graph is a relation on vertices. A database table is a finite relation among attribute domains. A partial order is a relation with reflexivity, antisymmetry, and transitivity. Because of this, learning relation properties pays off repeatedly.
Definitions
A binary relation from to is a subset of . A relation on is a subset of . If , write .
Important properties for a relation on :
- Reflexive: .
- Irreflexive: .
- Symmetric: .
- Antisymmetric: .
- Asymmetric: .
- Transitive: .
A relation from to can be represented by a zero-one matrix , where entry exactly when . A relation on a set can also be represented by a directed graph.
An -ary relation is a set of ordered -tuples. Relational databases are built from finite -ary relations, with columns as attributes and rows as records. A primary key is a set of attributes whose values identify each tuple uniquely.
The inverse relation from to is
Key results
The number of relations on an -element set is
Reason: has ordered pairs. A relation is any subset of that Cartesian product, and a set with elements has subsets.
Composition of relations works like relational chaining. If and , then
For zero-one matrices, composition corresponds to Boolean matrix multiplication:
The transitive closure of a relation is the smallest transitive relation containing . In graph terms, it connects to exactly when there is a directed path from to .
Closures add the fewest pairs needed to force a property. The reflexive closure adds missing pairs. The symmetric closure adds whenever is present. The transitive closure adds reachability pairs.
Visual
| Property | Matrix sign | Directed-graph sign |
|---|---|---|
| reflexive | all diagonal entries are | every vertex has a loop |
| irreflexive | all diagonal entries are | no vertex has a loop |
| symmetric | matrix equals its transpose | every arrow has a reverse arrow |
| antisymmetric | off-diagonal pairs do not both occur | no two-way arrows between distinct vertices |
| transitive | Boolean square entries are already present | every directed two-step path has a shortcut |
Worked example 1: Analyze a divisibility relation
Problem. Let and define on by if . List the relation and determine whether it is reflexive, symmetric, antisymmetric, and transitive.
Method.
- List all pairs where the first number divides the second:
- Reflexive: every divides itself, so every appears.
- Symmetric: false. For example, but .
- Antisymmetric: true. If and for positive integers, then .
- Transitive: true. If and , then and , so and .
Checked answer. The relation is reflexive, antisymmetric, and transitive, but not symmetric. It is therefore a partial order.
Worked example 2: Compute a composition of relations
Problem. Let
and
Find .
Method.
- A pair is in when there is a middle element with and .
- From and , get .
- From and , get and .
- From and , get and .
- From and , get .
- Remove duplicates.
Checked answer.
The duplicate appears through two middle elements, but relations are sets, so it is listed once.
Code
def compose(R, S):
return {
(a, c)
for (a, b) in R
for (b2, c) in S
if b == b2
}
def transitive_closure(relation):
closure = set(relation)
changed = True
while changed:
changed = False
new_pairs = compose(closure, closure) - closure
if new_pairs:
closure |= new_pairs
changed = True
return closure
R = {(1, "a"), (1, "b"), (2, "b"), (3, "c")}
S = {("a", "x"), ("b", "x"), ("b", "y"), ("c", "z")}
print(compose(R, S))
print(transitive_closure({(1, 2), (2, 3)}))
The transitive closure loop repeatedly adds pairs created by two-step paths until no new reachability pairs appear.
Common pitfalls
- Forgetting that a relation is a set, so duplicate ordered pairs do not count twice.
- Confusing symmetric and antisymmetric. A relation can be neither; equality is both symmetric and antisymmetric.
- Assuming antisymmetric means "not symmetric." That is false.
- Reversing the order in relation composition. In , apply first and then .
- Omitting diagonal entries when checking reflexivity.
- Treating transitive closure as the same as symmetric closure. They add different kinds of pairs.
When checking relation properties, use both positive and negative evidence. To prove reflexivity, verify every diagonal pair. To disprove it, give one missing diagonal pair. To prove symmetry, take an arbitrary pair in the relation and show is also in it. To disprove symmetry, one counterexample pair is enough. Transitivity is usually the most demanding because it concerns every compatible pair of pairs.
Matrix representations make some errors easier to see. Reflexivity is a diagonal condition. Symmetry is equality with the transpose. Antisymmetry means that outside the diagonal, the matrix cannot have s in both positions and . Boolean matrix multiplication helps detect transitivity: if a two-step path exists from to , then transitivity requires the direct pair .
Closures should be minimal. A reflexive closure does not add every possible pair; it adds only missing diagonal pairs. A symmetric closure does not add all pairs in both directions; it adds just the reverse of each existing pair. A transitive closure may require repeated additions because one new reachability pair can combine with another pair to force still more pairs.
For database-style relations, remember that rows are tuples and attributes have domains. A relation with columns (student_id, course, grade) is not a function unless one set of attributes uniquely determines the others. A primary key is a uniqueness claim, so it must be checked against all tuples and against possible future tuples if the database is meant to enforce a rule.
Relations also clarify why functions are special. A function from to is a relation in which every appears as first coordinate exactly once. If an input appears twice with different outputs, the relation is not a function. If an input is missing, it is not a total function from .
As a final check, translate each relation into at least two representations when the set is finite. List the ordered pairs, draw the directed graph, or write the zero-one matrix. A property that is hard to see in one representation is often obvious in another: symmetry appears as paired arrows, reflexivity as loops, and transitivity as shortcuts for directed two-step paths.
Also separate relation properties from relation names. "Divides" on positive integers is a partial order because it has the right properties; "congruence modulo " is an equivalence relation because it has a different set of properties. The words are not labels to memorize. They are conclusions justified by checking definitions.
When a relation is defined by a formula, test boundary and diagonal cases first. For if , the diagonal immediately shows it is not reflexive. For if , the diagonal works, but symmetry fails. These quick checks help decide which full proofs or counterexamples are still needed.
For finite relations, count possible ordered pairs before counting relations. If and , then has pairs, so there are relations from to . This simple count reinforces that a relation is a subset, not a rule that must connect every input.
That subset viewpoint also explains why empty and universal relations are legitimate relations.
Both are useful edge cases.
Connections
- Sets and set operations supplies Cartesian products and subsets.
- Functions, sequences, and sums treats functions as special relations.
- Equivalence relations and partial orders studies two major relation types.
- Graphs basics represents relations as directed graphs.
- Modular arithmetic and cryptography uses congruence as an equivalence relation.