1. The Structure of Connectivity
In computer science, we rarely deal with independent, isolated values. We model relationships: Does User A follow User B? Does Task 1 depend on Task 2? Is File X equivalent to File Y?
A Relation is the mathematical abstraction that connects pairs of objects.
Formal Definition
Let $A$ and $B$ be sets. A binary relation $R$ from $A$ to $B$ is a subset of the Cartesian product $A \times B$:
$$R \subseteq A \times B$$
If $(a, b) \in R$, we write $a , R , b$ (meaning "$a$ is related to $b$ by $R$").
When $A = B$, we say $R$ is a relation on $A$ ($R \subseteq A \times A$).
2. The Five Fundamental Properties of Relations
Let $R$ be a binary relation on set $A$:
text
Classic Concrete Examples:
| Relation on $\mathbb{Z}$ | Reflexive? | Symmetric? | Antisymmetric? | Transitive? |
|---|---|---|---|---|
| Equality ($a = b$) | Yes | Yes | Yes (vacuously) | Yes |
| Less than or equal ($a \le b$) | Yes | No ($2 \le 3$, but $3 \not\le 2$) | Yes ($a \le b \land b \le a \implies a=b$) | Yes |
| Strictly less than ($a < b$) | No (Irreflexive) | No | Yes | Yes |
| Divisibility ($a \mid b$) on $\mathbb{N}$ | Yes ($a \mid a$) | No | Yes | Yes |
3. Equivalence Relations & Partitions
An Equivalence Relation models the concept of "sameness" or "indistinguishability" under a specific metric.
Definition
A relation $R$ on set $A$ is an equivalence relation if and only if it is:
- Reflexive: $a , R , a$ for all $a \in A$.
- Symmetric: $a , R , b \implies b , R , a$.
- Transitive: $a , R , b \land b , R , c \implies a , R , c$.
3.1 Equivalence Classes
Let $R$ be an equivalence relation on $A$. The equivalence class of an element $a \in A$, denoted $[a]$ or $A/R$, is the set of all elements in $A$ related to $a$:
$$[a] = { x \in A : (x, a) \in R }$$
Any element in an equivalence class is called a representative of that class.
3.2 The Fundamental Theorem of Equivalence Relations
Let $R$ be an equivalence relation on set $A$. The set of all equivalence classes forms a partition of $A$:
- Every class is non-empty: $[a] \neq \emptyset$.
- Classes are pairwise disjoint: For any $a, b \in A$, either $[a] = [b]$ or $[a] \cap [b] = \emptyset$.
- Their union is the entire set: $\bigcup_{a \in A} [a] = A$.
Conversely, every partition of $A$ uniquely defines an equivalence relation on $A$.
text
4. Partial Orders & Hasse Diagrams
A Partial Order models hierarchy, dependency, and precedence (e.g., prerequisite courses, task scheduling in build systems like Make or Bazel).
4.1 Partially Ordered Set (Poset)
A relation $R$ on set $A$ is a partial order (denoted $\le$) if it is:
- Reflexive: $a \le a$.
- Antisymmetric: $a \le b \land b \le a \implies a = b$.
- Transitive: $a \le b \land b \le c \implies a \le c$.
The pair $(A, \le)$ is called a Poset.
- In a Total Order (or Linear Order), every pair of elements is comparable: $\forall a, b$, either $a \le b$ or $b \le a$.
- In a Partial Order, some pairs may be incomparable (e.g., in set inclusion $\subseteq$, ${1}$ and ${2}$ are neither subsets of each other).
4.2 Hasse Diagrams
A Hasse Diagram is a simplified, non-redundant visual representation of a finite poset:
- If $a \le b$, node $b$ is placed higher than node $a$.
- An edge is drawn from $a$ to $b$ if and only if $b$ covers $a$ (meaning $a < b$ and there is no intermediate element $c$ such that $a < c < b$).
- All reflexive self-loops ($a \le a$) and transitive shortcuts are omitted for visual clarity.
text
4.3 Topological Sorting: Linear Extensions of Posets
In compiler build systems and task runners, a DAG (Directed Acyclic Graph) defines dependencies. A Topological Sort finds a total order $(x_1, x_2, \dots, x_n)$ that is compatible with the partial order:
$$\text{If } u \le v, \text{ then } u \text{ appears before } v \text{ in the sequence.}$$
5. Transitive Closure & Warshall's Algorithm
Given a directed graph $G = (V, E)$, the transitive closure is a relation $R^$ such that $(u, v) \in R^$ if and only if there exists a directed path from $u$ to $v$ of length $\ge 1$.
Representing the graph by an $n \times n$ binary adjacency matrix $\mathbf{W}$:
$$W_{ij} = 1 \iff (i, j) \in E$$
Warshall's Dynamic Programming Algorithm
Warshall's algorithm computes the transitive closure in $O(n^3)$ bitwise operations by incrementally allowing paths to pass through intermediate vertices ${1, 2, \dots, k}$:
$$W_{ij}^{(k)} = W_{ij}^{(k-1)} \lor \left( W_{ik}^{(k-1)} \land W_{kj}^{(k-1)} \right)$$
text
6. Python Implementation: Warshall's Algorithm & Equivalence Partitions
python
7. Key Takeaways
- Equivalence Relations Partition: A relation is an equivalence relation if and only if it is reflexive, symmetric, and transitive, partitioning the set into disjoint equivalence classes.
- Posets Model Dependencies: Reflexive, antisymmetric, and transitive relations form posets, visualized cleanly via Hasse diagrams.
- Warshall's Algorithm: Dynamically computes all-pairs reachability in $O(V^3)$ by testing intermediate paths.