Lesson 3 of 20 · about 14 min
Relations
A relation says which pairs of things are connected, like "is a friend of" or "is less than". You'll learn the three properties that matter most (reflexive, symmetric, transitive), meet equivalence relations that sort things into groups, and order tasks with partial orders.
🧭 What you need first: Functions
On a social network, “follows” and “is friends with” are both connections between people, but they behave differently. If Ana is friends with Ben, Ben is friends with Ana. If Ana follows a celebrity, the celebrity almost certainly doesn’t follow her back.
A relation is any rule that connects pairs of things: “is friends with”, “is taller than”, “has the same birthday as”, “must be done before”. A few simple properties tell you a lot about a relation, and once you can name them, you can predict how it behaves, and which algorithms can use it.
Two kinds come up again and again. Equivalence relations, like “has the same birthday as”, sort things into groups. Orders, like “must be done before”, line things up, which is how your computer decides what to install first or which spreadsheet cell to recalculate first.
Another way: sorting laundry
“Is the same color as” is an equivalence relation. It sorts a laundry pile into separate piles: every sock lands in exactly one pile, and everything in a pile matches everything else in it.
Test some relations
Run this. Each relation is a set of pairs on the numbers 1 to 4. The code checks the three properties. Then try adding or removing a pair and see which properties change.
The real words
- A relation on a set is a subset of , the set of all ordered pairs. Write (or ) when .
- The reflexive, symmetric, transitive properties:
- Reflexive: for every . (“Same birthday as”: yes. “Less than”: no.)
- Symmetric: if then . (“Friends with”: yes. “Follows”: no.)
- Transitive: if and then . (“Less than”: yes. “Friends with”: no, a friend of a friend may be a stranger.)
- Each property is a statement (F05 lesson 6), which is why the code above uses
all().
- An equivalence relation is reflexive, symmetric and transitive. It splits the set into equivalence classes: groups where everything is related to everything else, and the groups don’t overlap.
- “Same remainder when divided by 3” splits the integers into three classes: , and . This is the heart of modular arithmetic (lesson 17).
- A partial order is reflexive, antisymmetric (if and then ) and transitive, like on numbers or on sets. “Partial” because some pairs may not be comparable at all: neither task has to come before the other.
Write the property checkers
R is a set of pairs on the set A. Write is_reflexive(R, A), is_symmetric(R), is_transitive(R) and is_equivalence(R, A).
Sort into classes
Write classes(items, key): group items into equivalence classes, where two items are related when key(item) gives the same answer. Return a list of sets, one per class, in order of first appearance.
'Is a friend of' on a social network is symmetric. Why isn't it transitive?
Got it?
Finish the lesson to color it in on your map and collect its concept cards.
Did this lesson click?