Lesson 5 of 15

Relations

Binary Relations

A binary relation RR on a set AA is a subset of A×AA \times A. We write aRbaRb (or (a,b)R(a,b) \in R) when aa is related to bb.

Key Properties

PropertyDefinitionExample
Reflexivea, (a,a)R\forall a,\ (a,a) \in R\leq on Z\mathbb{Z}
Symmetric(a,b)R(b,a)R(a,b) \in R \Rightarrow (b,a) \in R== on Z\mathbb{Z}
Antisymmetric(a,b),(b,a)Ra=b(a,b),(b,a) \in R \Rightarrow a=b\leq on Z\mathbb{Z}
Transitive(a,b),(b,c)R(a,c)R(a,b),(b,c) \in R \Rightarrow (a,c) \in R<< on Z\mathbb{Z}

Equivalence Relations

A relation that is reflexive, symmetric, and transitive is an equivalence relation. It partitions the domain into disjoint equivalence classes [a]={b:aRb}[a] = \{b : aRb\}.

Example: "same remainder mod 3" is an equivalence relation on Z\mathbb{Z}, with classes [0],[1],[2][0], [1], [2].

def is_equivalence(relation, domain):
    return (is_reflexive(relation, domain) and
            is_symmetric(relation) and
            is_transitive(relation))

# R = {(1,1),(2,2),(3,3),(1,2),(2,1)} — two classes: {1,2} and {3}
R = {(1,1),(2,2),(3,3),(1,2),(2,1)}
print(is_equivalence(R, {1,2,3}))  # True

Your Task

Implement is_reflexive, is_symmetric, is_transitive, and is_equivalence.

Pyodide loading...
Loading...
Click "Run" to execute your code.