Skip to content
DoingCamp CS

Make it comfy

Lesson style

Quick mode shows the key picture, a summary and the practice. Good for a refresher.

Theme
Font

Both fonts are designed to be easy to read. Some readers with dyslexia prefer Lexend.

Text size
Spacing
Animations

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

The story

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.

Play with 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.

Name it

The real words

  • A relation RR on a set AA is a subset of A×AA \times A, the set of all ordered pairs. Write a∼ba \sim b (or aRba \mathrel{R} b) when (a,b)∈R(a, b) \in R.
  • The reflexive, symmetric, transitive properties:
    • Reflexive: a∼aa \sim a for every aa. (“Same birthday as”: yes. “Less than”: no.)
    • Symmetric: if a∼ba \sim b then b∼ab \sim a. (“Friends with”: yes. “Follows”: no.)
    • Transitive: if a∼ba \sim b and b∼cb \sim c then a∼ca \sim c. (“Less than”: yes. “Friends with”: no, a friend of a friend may be a stranger.)
    • Each property is a ∀\forall 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: {…,0,3,6,…}\{\ldots, 0, 3, 6, \ldots\}, {…,1,4,7,…}\{\ldots, 1, 4, 7, \ldots\} and {…,2,5,8,…}\{\ldots, 2, 5, 8, \ldots\}. This is the heart of modular arithmetic (lesson 17).
  • A partial order is reflexive, antisymmetric (if a∼ba \sim b and b∼ab \sim a then a=ba = b) and transitive, like ≤\le on numbers or ⊆\subseteq on sets. “Partial” because some pairs may not be comparable at all: neither task has to come before the other.
Build it

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).

Challenge

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.

Remember it

'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?