Discrete Mathematics Module

Relations, Equivalence & Partial Orders

Binary relations, reflexivity, symmetry, transitivity, equivalence classes, partitions, partially ordered sets (posets), Hasse diagrams, and Warshall's transitive closure algorithm.

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
+-----------------------------------------------------------------------------------------+
|                                  THE 5 CORE PROPERTIES                                  |
+-----------------------------------------------------------------------------------------+
|  1. REFLEXIVE:       Every element is related to itself.                                |
|                      For all a in A: (a, a) in R.                                       |
|                                                                                         |
|  2. IRREFLEXIVE:     No element is related to itself.                                   |
|                      For all a in A: (a, a) not in R.                                   |
|                                                                                         |
|  3. SYMMETRIC:       Relationships are bidirectional.                                   |
|                      (a, b) in R ===> (b, a) in R.                                      |
|                                                                                         |
|  4. ANTISYMMETRIC:   No two distinct elements relate in both directions.                |
|                      (a, b) in R AND (b, a) in R ===> a = b.                            |
|                                                                                         |
|  5. TRANSITIVE:      Relationships cascade through intermediate steps.                  |
|                      (a, b) in R AND (b, c) in R ===> (a, c) in R.                      |
+-----------------------------------------------------------------------------------------+

Classic Concrete Examples:

Relation on $\mathbb{Z}$Reflexive?Symmetric?Antisymmetric?Transitive?
Equality ($a = b$)YesYesYes (vacuously)Yes
Less than or equal ($a \le b$)YesNo ($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)NoYesYes
Divisibility ($a \mid b$) on $\mathbb{N}$Yes ($a \mid a$)NoYesYes

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:
  1. Reflexive: $a , R , a$ for all $a \in A$.
  2. Symmetric: $a , R , b \implies b , R , a$.
  3. 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$:
  1. Every class is non-empty: $[a] \neq \emptyset$.
  2. Classes are pairwise disjoint: For any $a, b \in A$, either $[a] = [b]$ or $[a] \cap [b] = \emptyset$.
  3. 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
Set A Partitioned into Disjoint Equivalence Classes:
+---------------------------------------------------------+
|                                                         |
|    +-------------+   +-------------+   +-------------+  |
|    | Class [a]   |   | Class [b]   |   | Class [c]   |  |
|    |  * a1  * a2 |   |  * b1  * b2 |   |  * c1  * c2 |  |
|    |    * a3     |   |    * b3     |   |    * c3     |  |
|    +-------------+   +-------------+   +-------------+  |
|                                                         |
+---------------------------------------------------------+
Relational DB Connection: This is EXACTLY what 'GROUP BY' does in SQL!

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:
  1. Reflexive: $a \le a$.
  2. Antisymmetric: $a \le b \land b \le a \implies a = b$.
  3. 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:
  1. If $a \le b$, node $b$ is placed higher than node $a$.
  2. 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$).
  3. All reflexive self-loops ($a \le a$) and transitive shortcuts are omitted for visual clarity.
text
Hasse Diagram for Divisibility on {1, 2, 3, 4, 6, 12}:
               [ 12 ]
              /      \
            [ 4 ]    [ 6 ]
             |      /     \
            [ 2 ] -+     [ 3 ]
             \            /
                \      /
                 [ 1 ]

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
Warshall's Optimization Step:
Can we reach j from i?
YES if:
- We can ALREADY reach j directly: W[i][j] == 1, OR
- We can reach intermediate node k (W[i][k] == 1) AND node k can reach j (W[k][j] == 1).

6. Python Implementation: Warshall's Algorithm & Equivalence Partitions

python
import numpy as np

def warshalls_transitive_closure(adj_matrix: np.ndarray) -> np.ndarray:
    """
    Compute the Transitive Closure of a directed graph using Warshall's Algorithm.
    Time Complexity: O(V^3), Space Complexity: O(V^2).
    """
    closure = adj_matrix.copy().astype(bool)
    n = closure.shape[0]

    for k in range(n):        # Intermediate vertex
        for i in range(n):    # Source vertex
            for j in range(n):# Destination vertex
                closure[i, j] = closure[i, j] or (closure[i, k] and closure[k, j])

    return closure.astype(int)

def verify_equivalence_relation(elements: list, relation_pairs: set) -> bool:
    """Verify if a set of pairs defines a valid equivalence relation on elements."""
    # 1. Reflexive check: (a, a) in R
    for a in elements:
        if (a, a) not in relation_pairs:
            return False

    # 2. Symmetric check: (a, b) in R ==> (b, a) in R
    for (a, b) in relation_pairs:
        if (b, a) not in relation_pairs:
            return False

    # 3. Transitive check: (a, b) in R and (b, c) in R ==> (a, c) in R
    for (a, b) in relation_pairs:
        for (c, d) in relation_pairs:
            if b == c:
                if (a, d) not in relation_pairs:
                    return False

    return True

if __name__ == "__main__":
    # 4-node directed dependency graph (0 -> 1 -> 2 -> 3)
    graph = np.array([
        [0, 1, 0, 0],
        [0, 0, 1, 0],
        [0, 0, 0, 1],
        [0, 0, 0, 0]
    ])

    tc = warshalls_transitive_closure(graph)
    print("=== Warshall's Transitive Closure Matrix ===")
    print(tc)
    # Notice: Node 0 can now reach 1, 2, and 3 (top row = [0, 1, 1, 1])

7. Key Takeaways

  1. 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.
  2. Posets Model Dependencies: Reflexive, antisymmetric, and transitive relations form posets, visualized cleanly via Hasse diagrams.
  3. Warshall's Algorithm: Dynamically computes all-pairs reachability in $O(V^3)$ by testing intermediate paths.