TExcelTAD
Home › Discrete Mathematics › Binary Relations

Binary Relations

A binary relation RR from a set AA to a set BB is a subset of A×BA \times B. If (a,b)∈R(a, b) \in R we write aRba \mathrel{R} b and say aa is related to bb. "Is enrolled in" between students and courses, "follows" between users, and ≤\le between numbers are all relations. Functions are special relations, and the vocabulary of this lesson (images, inverses, the arrow properties) is what the counting rules rest on.

Relations as data

A relation is just a set of pairs, which is exactly a two-column table in a database.

python · try it yourself
enrolled = {("ann", "math"), ("ann", "cs"), ("bob", "cs"), ("cid", "art"), ("dee", "cs")}

def image(R, X):
    """R(X): everything related to some element of X."""
    return sorted({b for a, b in R if a in X})

def inverse(R):
    return {(b, a) for a, b in R}

print("courses of ann:", image(enrolled, {"ann"}))
print("courses of ann or cid:", image(enrolled, {"ann", "cid"}))
print("students in cs:", image(inverse(enrolled), {"cs"}))
courses of ann: ['cs', 'math']
courses of ann or cid: ['art', 'cs', 'math']
students in cs: ['ann', 'bob', 'dee']

The image R(X)R(X) of a set X⊆AX \subseteq A is the set of elements of BB related to some element of XX. The inverse relation R−1R^{-1} reverses every pair. In SQL, the image is a SELECT course FROM enrolled WHERE student IN (…).

Arrow properties

Picture each pair as an arrow from aa to bb. Four properties count arrows:

Property Arrows Name when it holds
≤1\le 1 arrow out of each aa function-like a (partial) function
≥1\ge 1 arrow out of each aa defined everywhere total
≤1\le 1 arrow into each bb no sharing injective
≥1\ge 1 arrow into each bb nothing missed surjective

A total function has exactly 1 arrow out of every element of AA; a bijection is a total function with exactly 1 arrow into every element of BB.

python · try it yourself
from collections import Counter

def arrows(R, A, B):
    out = Counter(a for a, _ in R)
    into = Counter(b for _, b in R)
    return {
        "function": all(out[a] <= 1 for a in A),
        "total": all(out[a] >= 1 for a in A),
        "injective": all(into[b] <= 1 for b in B),
        "surjective": all(into[b] >= 1 for b in B),
    }

A, B = ["ann", "bob", "cid", "dee"], ["math", "cs", "art"]
enrolled = {("ann", "math"), ("ann", "cs"), ("bob", "cs"), ("cid", "art"), ("dee", "cs")}
advisor = {("ann", "math"), ("bob", "cs"), ("cid", "art"), ("dee", "cs")}
print("enrolled:", arrows(enrolled, A, B))
print("advisor: ", arrows(advisor, A, B))
enrolled: {'function': False, 'total': True, 'injective': False, 'surjective': True}
advisor:  {'function': True, 'total': True, 'injective': False, 'surjective': True}

"Enrolled" is not a function (ann has two courses). "Advisor department" is a total surjective function, not injective (bob and dee share cs).

Composing relations

If R⊆A×BR \subseteq A \times B and S⊆B×CS \subseteq B \times C, the composition S∘RS \circ R relates aa to cc when there is some bb with aRba \mathrel{R} b and bScb \mathrel{S} c. "Student takes a course taught by professor" is the composition of "enrolled" and "taught by". In a database, composing relations is a join on the shared column.

Relations on one set

A relation on a single set AA (a subset of A×AA \times A) is the same thing as a directed graph with vertex set AA. Properties such as reflexive, symmetric and transitive classify these relations; they lead to partial orders and equivalence relations.

In software

Many-to-many relationships in a database (students and courses, tags and posts) are stored as a join table, which is literally the set of pairs. An ORM's "has one", "belongs to" and "has many" are the arrow properties: "each order belongs to exactly one customer" says the relation from orders to customers is a total function.

Exercises

Exercise 1: Arrow properties

Let A={1,2,3}A = \{1, 2, 3\}, B={x,y}B = \{x, y\}, R={(1,x),(2,x),(3,y)}R = \{(1, x), (2, x), (3, y)\}. Is RR a function? Total? Injective? Surjective?

Answer

A total function, surjective, not injective.

Solution

Each of 1, 2, 3 has exactly one arrow out, so RR is a total function. xx has two arrows in, so it is not injective. Both xx and yy are hit, so it is surjective.

Exercise 2: Image and inverse

With RR from Exercise 1, find R({1,2})R(\{1, 2\}) and R−1({x})R^{-1}(\{x\}).

Answer

R({1,2})={x}R(\{1, 2\}) = \{x\}; R−1({x})={1,2}R^{-1}(\{x\}) = \{1, 2\}.

Solution

1 and 2 are both related only to xx. Reversing the arrows, xx is related to 1 and 2.

Exercise 3: Compose with a join (code)

Given takes = {(ann, cs), (bob, art)} and taught_by = {(cs, kay), (cs, lee), (art, mo)}, compute the composition "student is taught by professor".

Answer

{(ann,kay),(ann,lee),(bob,mo)}\{(\text{ann}, \text{kay}), (\text{ann}, \text{lee}), (\text{bob}, \text{mo})\}.

Solution
python · try it yourself
takes = {("ann", "cs"), ("bob", "art")}
taught_by = {("cs", "kay"), ("cs", "lee"), ("art", "mo")}
comp = sorted((s, p) for s, c in takes for c2, p in taught_by if c == c2)
print(comp)
[('ann', 'kay'), ('ann', 'lee'), ('bob', 'mo')]

Test yourself

Formally, a binary relation from AA to BB is…

  1. a function
  2. a subset of A×BA \times B
  3. an element of AA
  4. a bijection

A relation with at most one arrow into each element of BB is…

  1. total
  2. surjective
  3. injective
  4. a function

Composing two relations corresponds to which database operation?

  1. UNION
  2. a join on the shared column
  3. DELETE
  4. ORDER BY