Binary Relations
A binary relation from a set to a set is a subset of . If we write and say is related to . "Is enrolled in" between students and courses, "follows" between users, and 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.
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 of a set is the set of elements of related to some element of . The inverse relation 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 to . Four properties count arrows:
| Property | Arrows | Name when it holds |
|---|---|---|
| arrow out of each | function-like | a (partial) function |
| arrow out of each | defined everywhere | total |
| arrow into each | no sharing | injective |
| arrow into each | nothing missed | surjective |
A total function has exactly 1 arrow out of every element of ; a bijection is a total function with exactly 1 arrow into every element of .
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 and , the composition relates to when there is some with and . "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 (a subset of ) is the same thing as a directed graph with vertex set . 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 , , . Is a function? Total? Injective? Surjective?
Answer
A total function, surjective, not injective.
Solution
Each of 1, 2, 3 has exactly one arrow out, so is a total function. has two arrows in, so it is not injective. Both and are hit, so it is surjective.
Exercise 2: Image and inverse
With from Exercise 1, find and .
Answer
; .
Solution
1 and 2 are both related only to . Reversing the arrows, 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
.
Solution
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 to is…
- a function
- a subset of
- an element of
- a bijection
A relation with at most one arrow into each element of is…
- total
- surjective
- injective
- a function
Composing two relations corresponds to which database operation?
UNION- a join on the shared column
DELETEORDER BY