Relasi (MIT)
Relasi (MIT)
Relations
So, R is a set of ordered pairs. We often write a ∼R b or aRb to mean that (a, b) ∈ R.
Functions, for example, are a type of relation. The abstract notion of a relation is useful both in
mathematics and in practice for modeling many different sorts of relationships. It’s the basis of
the relational database model, the standard data model for practical data processing systems.
Many times we will talk about a “relation on the set A”, which means that the relation is a subset
of A × A. We can also define a ternary relation on A as a subset R ⊆ A3 or, in general, an n-ary
relation as a subset R ⊆ An , or R ⊆ A1 × A2 × · · · × An if the sets Ai are different. In this class, we
will focus only on binary relations. Here are some examples:
1. The relation “is taking class” as a subset of {students at MIT} × {classes at MIT}. A relation
from students to classes.
2. The relation “has lecture in” as a subset of {classes at MIT} × {rooms at MIT}. A relation
from classes to rooms.
3. The relation “is living in the same room” as a subset of {students at MIT}×{students at MIT}.
A relation on students.
4. The relation “can drive from first to second city”. (Not necessarily directly—just some way,
on some roads.)
7. “likes”
1 Properties of Relations
Once we have modeled something abstractly as a relation, we can talk about its properties without
referring to the original problem domain. For a relation on a set A there are several standard
properties of relations that occur commonly. Later on we will use these properties to classify
different types of relations.
The difference between antisymmetric and asymmetric relations is that antisymmetric relations
may contain pairs (a, a), i.e., elements can be in relations with themselves, while in an asymmetric
relation this is not allowed. Clearly, any asymmetric relation is also antisymmetric, but not vice
versa.
Among our relations from Example :
• Relation 4 is reflexive, transitive. Not necessarily symmetric, since roads could be one-way
(consider Boston), but in actuality . . . . But definitely not antisymmetric.
• Relation 6 likewise.
• Relation 7 is (unfortunately) not symmetric. Not antisymmetric. Not transitive. Not even
reflexive!
2 Representation
There are many different ways of representing relations. One way is to describe them by proper-
ties, as we did above. For infinite sets, that’s about all we can do. But for finite sets, we usually
use some method that explicitly enumerates all the elements of the relation. Some alternatives
are lists, matrices and graphs. Why do we have so many different ways to represent relations?
Different representations may be more efficient for encoding different problems and also tend to
highlight different properties of the relation.
2.0.1 Lists
A finite relation from set A to set B can be represented by a list of all the pairs.
Example 2.1. The relation from {0, 1, 2, 3} to {a, b, c} defined by the list:
{(0, a), (0, c), (1, c), (2, b), (1, a)}.
Example 2.2. The divisibility relation on natural numbers {1, . . . , 12} is represented by the list:
{(1, 1), (1, 2), (1, 3), (1, 4), (1, 5), (1, 6), (1, 7), (1, 8), (1, 9), (1, 10), (1, 11), (1, 12), (2, 2), (2, 4),
(2, 6), (2, 8), (2, 10), (2, 12), (3, 3), (3, 6), (3, 9), (3, 12), (4, 4), (4, 8), (4, 12), (5, 5), (6, 6),
(6, 12), (7, 7), (8, 8), (9, 9), (10, 10), (11, 11), (12, 12)}.
Boolean matrices are a convenient representation for representing relations in computer programs.
The rows are for elements of A, columns for B, and for every entry there is a 1 if the pair is in the
relation, 0 otherwise.
Example 2.3. The relation from Example 2.1 is represented by the matrix
a b c
0 1 0 1
1 1 0 1
2 0 1 0
3 0 0 0
4 Course Notes 3: Relations
Example 2.4. The divisibility relation over {1, 2, . . . , 12} is represented by the enormous matrix
1 2 3 4 5 6 7 8 9 10 11 12
1 1 1 1 1 1 1 1 1 1 1 1 1
2 0 1 0 1 0 1 0 1 0 1 0 1
3 0 0 1 0 0 1 0 0 1 0 0 1
4 0 0 0 1 0 0 0 1 0 0 0 1
5 0 0 0 0 1 0 0 0 0 1 0 0
6 0 0 0 0 0 1 0 0 0 0 0 1
7 0 0 0 0 0 0 1 0 0 0 0 0
8 0 0 0 0 0 0 0 1 0 0 0 0
9 0 0 0 0 0 0 0 0 1 0 0 0
10 0 0 0 0 0 0 0 0 0 1 0 0
11 0 0 0 0 0 0 0 0 0 0 1 0
12 0 0 0 0 0 0 0 0 0 0 0 1
Symmetry: the matrix is clearly not symmetric across the major diagonal.
2.0.3 Digraphs
We can draw a picture of a relation R ⊆ A × B by drawing a dot for every element of A, a dot for
every element of B, and an arrow from a ∈ A to b ∈ B iff aRb. Such a picture is called a directed
graph, or digraph for short.
Example 2.5. The relation from Example 2.1 is represented by the digraph
0
a
1
b
2
c
3
Digraphs are mainly used for relations where A = B, i.e., for relations on a finite set A. To
represent such a relation as a digraph we draw a dot (vertex) for each element of A, and draw
an arrow from first element to second element of each pair in the relation. The digraph may
contain self-loops, i.e. arrows from a dot to itself, associated to the elements a such that (a, a) is in
the relation.
Course Notes 3: Relations 5
Example 2.6. The divisibility relation over {1, 2, . . . , 12} is represented by the digraph
4 2 8 10
12 6 1
3 9 11
3 Operations on Relations
3.1 Inverse
If R is a relation on A × B, then R−1 is a relation on B × A given by R−1 = {(b, a) | (a, b) ∈ R}. It’s
just the relation “turned backwards.”
Example 3.1. Inverse of “is taking class” (Relation 1 in Example ) is the relation “has as a student”
on the set {classes at MIT} × {students at MIT}; a relation from classes to students.
We can translate the inverse operation on relation to operations on the various representations of
a relation. Given the matrix for R, we can get the matrix for R−1 by transposing the matrix for R
(note that the inverse of a relation is not the same thing as the inverse of the matrix representation).
Given a digraph for R, we get the graph for R−1 by reversing every edge in the original digraph.
3.2 Composition
In words, the pair (a, c) is in R2 ◦ R1 if there exists an element b such that the pair (a, b) is in R1
and the pair (b, c) is in R2 . Another way of thinking about this is that a “path” exists from element
a to c via some element in the set B 1 .
1
Notice that R2 ◦ R1 and R1 ◦ R2 are different. The symbol ◦ is a source of eternal confusion in mathematics—if
you read a book you should always check how the authors define ◦—some of them define composition the other way
around.
6 Course Notes 3: Relations
Example 3.2. The composition of the relation “is taking class” (Relation 1 in Example ) with the
relation “has lecture in” (Relation 2 in Example ) is the relation “should go to lecture in”, a relation
from {students at MIT} to {rooms at MIT}.
Example 3.3. Composition of the parent-of relation with itself gives grandparent-of. Composition
of the child-of relation with the parent-of relation gives the sibling-of relation. (Here we relax
the meaning of sibling to include that a person is the sibling of him/herself.) Does composition
of parent-of with child-of give married-to/domestic partners? No, because that misses childless
couples.
Example 3.4. Let B be the set of boys, G be the set of girls, R1 ⊆ B × G consist of all pairs (b, g)
such that b is madly in love with g, and R2 ⊆ G × B consist of all pairs (g, b) such that g is madly
in love with b. What are the relations R2 ◦ R1 and R1 ◦ R2 , respectively?
If we represent the relations as matrices, then we can compute the composition by a form of
“boolean” matrix multiplication, where + is replaced by ∨ (Boolean OR) and × is replaced by
∧ (Boolean AND).
Example 3.5. Let R1 be the relation from Example 2.1:
a b c
0 1 0 1
1 1 0 1
2 0 1 0
3 0 0 0
d e f
a 1 1 1
b 0 1 0
c 0 0 1
Then R2 ◦ R1 = {(0, d), (0, e), (0, f ), (1, d), (1, e), (1, f ), (2, e)}, that is,
d e f
0 1 1 1
1 1 1 1
2 0 1 0
3 0 0 0
A relation on a set A can be composed with itself. The composition R ◦ R of R with itself is written
R2 . Similarly Rn denotes R composed with itself n times. Rn can be recursively defined: R1 = R,
Rn = R ◦ Rn−1 .
Course Notes 3: Relations 7
Example 3.6. Consider the relation R = {(a, b), (a, c), (b, d), (d, e)} or:
a b c d e
a 0 1 1 0 0 b d
b 0 0 0 1 0
c 0 0 0 0 0 e
d 0 0 0 0 1
e 0 0 0 0 0 a c
R2 will be:
a b c d e
a 0 0 0 1 0 b d
b 0 0 0 0 1
c 0 0 0 0 0 e
d 0 0 0 0 0
e 0 0 0 0 0 a c
R3 will be:
a b c d e
a 0 0 0 0 1 b d
b 0 0 0 0 0
c 0 0 0 0 0 e
d 0 0 0 0 0
e 0 0 0 0 0 a c
Definition 3.7. A path in a relation R is a sequence a0 , . . . , ak with k ≥ 0 such that (ai , ai+1 ) ∈ R
for every i < k. We call k the length of the path.
In the digraph model, a path is something you can trace out by following arrows from vertex to
vertex, without lifting your pen. Note that a singleton vertex is a length 0 path (this is just for
convenience). A simple path is a path with no repeated vertices.
The base case is clear. There is exactly one edge from a to b for every (a, b) ∈ R. And there is
exactly one pair (a, b) ∈ R for every edge from a to b, P (1) is true. Note that since the induction
hypothesis is an equality we have to prove both sides.
For the inductive step, suppose P (n) is true.
First consider a path a0 , . . . , an+1 in R. This is a path a0 , . . . , an in R followed by a pair (an , an+1 )
of R. By the inductive hypothesis, we can assume that (a0 , an ) ∈ Rn . And we have already
8 Course Notes 3: Relations
mentioned that (an , an+1 ) ∈ R. Therefore (a0 , an+1 ) ∈ Rn+1 by the definition of composition.
Thus, every path of length n + 1 corresponds to a relation in Rn+1 .
Now consider a pair (a, b) ∈ Rn+1 . By the definition of composition, there exists a c such that
(a, c) ∈ Rn and (c, b) ∈ R. By the inductive hypothesis, we can assume that (a, c) corresponds to a
length n path from a to c, and since (c, b) ∈ R there is an edge from c to b. Thus, there is a length
n + 1 path from a to b. To conclude, P (n) −→ P (n + 1).
3.3 Closure
A closure “extends” a relation to satisfy some property. But extends it as little as possible.
Definition 3.9. The closure of relation R with respect to property P is the relation S that
(i) contains R,
(ii) has property P , and
(iii) is contained in any relation satisfying (i) and (ii).
That is, S is the “smallest” relation satisfying (i) and (ii).
As a general principle, there are two ways to construct a closure of R with respect to property P :
We can either start with R and add as few pairs as possible until the new relation has property
P ; or we can start with the largest possible relation (which is A × A for a relation on A) and then
remove as many not-in-R pairs as possible while preserving the property P .
Lemma 3.10. Let R be a relation on the set A. The reflexive closure of R is S = R ∪ {(a, a), ∀a ∈ A}.
Proof. It contains R and is reflexive by design. Furthermore (by definition) any relation satisfying
(i) must contain R, and any satisfying (ii) must contain the pairs (a, a), so any relation satisfying
both (i) and (ii) must contain S.
Example 3.11. Let R = {(a, b)(a, c)(b, d)(d, e)}, then the reflexive closure of R is
{(a, b)(a, c)(b, d)(d, e)(a, a)(b, b)(c, c)(d, d)(e, e)} .
Lemma 3.12. Let R be a relation on the set A. The symmetric closure of R is S = R ∪ R−1 .
Proof. This relation is symmetric and contains R. It is also the smallest such. For suppose we have
some symmetric relation T with R ⊆ T . Consider (a, b) ∈ R. Then (a, b) ∈ T so by symmetry
(b, a) ∈ T . It follows that R−1 ⊆ T . So S = R ∪ R−1 ⊆ T .
Example 3.13. Let R = {(a, b)(a, c)(b, d)(d, e)}, then the symmetric closure of R is
The transitive closure is a bit more complicated than the closures above.
Lemma 3.14. Let R be a relation on the set A. The transitive closure of a relation R is the set
Proof. Obviously, R ⊆ S. Next, we show that S is transitive. Suppose (a, b) ∈ S and (b, c) ∈ S.
This means that there is an (a, b) path and a (b, c) path in R. If we “concatenate” them (attach the
end of the (a, b) path to the start of the (b, c) path, we get an (a, c) path. So (a, c) ∈ S. So S is
transitive.
Finally, we need to show that S is the smallest transitive relation containing R. So consider any
transitive relation T containing R. We have to show that S ⊆ T . Assume for contradiction that
S � T . This means that some pair (a, b) ∈ S but (a, b) ∈ / T . In other words, there is a path
a0 , . . . , ak = b in R where k ≥ 1 and (a0 , ak ) ∈
/ T . Call this a missing path. Now let M be the set of
missing paths.
Now we use well-ordering. We have just claimed that the set M of missing paths is nonempty.
So consider a shortest missing path s0 , . . . , sm . We will derive a contradiction to this being the
shortest missing path.
Case 2: m > 1. Then s1 , . . . , sm−1 is a path in R (m − 1 > 0). But it is shorter than our original
shortest missing path, so cannot be missing. Thus (s1 , sm−1 ) ∈ T . Also we have (sm−1 , sm ) ∈ T
since R ⊆ T . Thus by transitivity of T , (s1 , sm ) ∈ T , a contradiction.
We get a contradiction either way, so our assumption (that S � T ) is false. This completes the
proof.
Wait a minute. Well-ordering is applied to sets of numbers; we applied it to a set of paths! How?
Well, look at the set of “lengths of missing paths”. It is nonempty, so has a smallest element. There
is path that has this length—so it is a shortest path.
10 Course Notes 3: Relations
a b c d e
a 0 1 1 0 0 b d
b 0 0 0 1 0
c 0 0 0 0 0 e
d 0 0 0 0 1
e 0 0 0 0 0 a c
is
a b c d e
a 0 1 1 1 1 b d
b 0 0 0 1 1
c 0 0 0 0 0 e
d 0 0 0 0 1
e 0 0 0 0 0 a c
With reflexive and symmetric closure, it is pretty clear how to actually build them. But for transi
tive closure, how do we actually find all the paths we need?
Let’s start by finding paths of a given length. Recall the definition of Rn of R composed with itself
n times. We proved that Rn = {(a, b)giventhere is a length n path from a to b in R}. This means
we can write the transitive closure of R as R ∪ R2 ∪ R3 ∪ · · · . Better—since we know how to do
composition—but still a problem: there are infinitely many terms!
Lemma 3.16. Suppose A has n elements and that R is a relation on A. Let a and b be elements of A and
suppose that there is a path from a to b in R. Then, there is a path of length at most n from a to b in R.
Proof. We’ll use well-ordering (again). Consider the shortest path a = a0 , a1 , . . . , ak from a to b (we
know one exists, so by well-ordering there is a shortest one). Suppose k > n. Then some element
of A appears twice in the list (with more than n list entries, one must be a repeat). This means the
path is at some point circling back to where it was before. We can cut out this cycle from the path
and get a shorter path. This contradicts that we had a shortest path. So we cannot have k > n.
So we don’t need infinitely many terms. It is enough to take paths of length at most n, namely
R1 ∪ R2 ∪ · · · ∪ Rn .
We can use properties of relations to classify them into different types. We will be considering two
important types of relations, equivalence relations and partial orders.
Course Notes 3: Relations 11
Definition 4.1. An equivalence relation is a relation that is reflexive, symmetric and transitive.
For example, the “roommates” relation is an equivalence relation. So is “same size as”, and “on
same Ethernet hub”. A trivial example is the = relation on natural numbers. The hallmark of
equivalence relation is the word same. It provides a way to hide unimportant differences. Using
an equivalence relation we can actually partition the universe into subsets of things that are the
“same”, in a natural way.
Definition 4.2. A partition of a set A is a collection of subsets {A1 , . . . , Ak } such that any two of
them are disjoint (for any i �= j, Ai ∩ Aj = ∅) and such that their union is A.
Let R be an equivalence relation on the set A. For an element a ∈ A, let [a] denote the set {b ∈
A given a ∼R b}. We call this set the equivalence class of a under R. We call a a representative of [a].
Lemma 4.3. The sets [a] for a ∈ A constitute a partition of A. That is, for every a, b ∈ A, either [a] = [b]
or [a] ∩ [b] = ∅.
Proof. Consider some arbitrary a, b ∈ A. If either [a] = [b] or [a] ∩ [b] = ∅ then we are done, so
suppose not. Let c be any element that is in one of the sets but not the other. Without loss of
generality we can assume that c ∈ [b] − [a]. (We know that either c ∈ [b] − [a] or c ∈ [a] − [b]. In
the latter case we can simply swap a and b and reduce to the first case.) Let d be any element in
d ∈ [a] ∩ [b]. We will get a contradiction by showing that a ∼R c and therefore that c ∈ [a]. First,
a ∼R d because d ∈ [a] (note that d = a is a possibility but this is ok because R is reflexive). Second,
d ∼R b and b ∼R c because both c, d ∈ [b] and R is symmetric. This implies, by transitivity, that
d ∼R c. Finally, by transitivity, a ∼R c because a ∼R d and d ∼R c.
Note that all three properties of equivalence relations were used in this proof. Checking that the
proof uses all available assumptions if usually a good sanity check when writing proofs—if one of
the properties you assumed were not needed, you have either made a mistake or proven a much
more strong theorem than you thought—e.g., if you didn’t use transitivity anywhere in the proof
of Lemma 4.3, you would be proving that any reflexive symmetric relation produces a partition,
which is false.
Lemma 4.4. Any partition {A1 , . . . , Ak } of A defines an equivalence relation by letting a ∼R b iff a and
b are in the same Ai .
Proof. Reflexivity: for all a we know a ∈ Ai for some i, by definition of partition. Clearly a and a
are in the same Ai , i.e., a ∼R a.
Symmetry: Assume a ∼R b, that is a and b are in the same Ai . Also b and a are in the same Ai and
therefore b ∼R a.
Transitivity: Assume a ∼R b and b ∼R c. By definition of ∼R , a and b are in the same Ai for some i,
and b and c are in the same Aj for some j. But by definition of partition b cannot be in two different
Ai ’s. So, it must be Ai = Aj and a and c are in the same Ai , proving a ∼R c.
Therefore, we can look at partitions and at equivalence relations as the same thing.
12 Course Notes 3: Relations
A familiar and important equivalence relation on integers (positive, negative and 0) is:
Definition 4.5. If a and b are integers, then we say that a ≡ b (mod m) if m | (a − b).
Proof. We need to show that the relation is reflexive, symmetric, and transitive.
Reflexive: Clearly m | (a − a) = 0, so a ≡ a (mod m).
Symmetric: If a = b + km then b = a + (−k)m.
Transitive: Suppose a ≡ b (mod m) and b ≡ c (mod m). Then m | (a − b) and m | (b − c). So
(a − b) = k1 m and (b − c) = k2 m. So (a − c) = (a − b) + (b − c) = (k1 + k2 )m. Therefore
m | (a − c).
The equivalence class of a is the set [a] = {b ∈ Z | a ≡ b (mod m)}, or {km + a | k ∈ Z}.
It turns out that we can extend a lot of standard arithmetic to work modulo m. In fact, we can
define notions of sum and product for the equivalence classes mod m. For example, we define
[a] + [b], given two equivalence classes mod m, to be the equivalence class [a + b]. This is not as
obvious as it seems: notice that the result is given in terms of a and b, two selected representatives
from the equivalence classes, but that it is supposed to apply to the equivalence classes themselves.
To prove that this works, we have to show that it doesn’t matter which representatives of the
equivalence classes we choose:
It follows that if we are interested only the result of the addition modulo m—which is the case,
for example, in the RSA cryptosystem—then we can at any time replace a given number with
a different number equivalent to it, without changing the value (equivalence class) of the final
answer. The same fact can be proven for multiplication.
5 Partial Orders
Partial orders are another type of binary relation that is very important in computer science. They
have applications to task scheduling, database concurrency control, and logical time in distributed
computing,
Recall that antisymmetric mean aRb ∧ bRa ⇒ a = b, or ∀a �= b, aRb ⇒ ¬bRa. In other words
this relation is never symmetric! This single property is what distinguishes it from an equivalence
relation. The reflexivity, antisymmetry and transitivity properties are abstract properties that gen
erally describe “ordering” relationships.
For a partial order relation we often write an ordering-style symbol like �, instead of just a letter
like R, for a partial order relation. This lets us use notation similar to ≤. For example, we write
a � b if a � b and a �= b. Similarly, we write b � a as equivalent to a � b. But this could be
misleading, note that ≥ is a partial order on natural numbers, as well as ≤. If we use the � symbol
for ≥, things look really funny. In cases like this it is better to use R.
A partial order is always defined on some set A. The set together with the partial order is called a
“poset”:
Definition 5.2. A set A together with a partial order � is called a poset (A, �).
• A = N, R =≥, same.
• A = “set of all computers in the world”, R = “is (directly or indirectly) connected to”. not
a partial order because it is not true that aRb ∧ bRa ⇒ a = b. In fact, it is symmetric and
transitive. Equivalence relation.
• A = “set of all propositions”, R =⇒, not because it’s not antisymmetric. Not symmetric
either, so not equivalence relation.
14 Course Notes 3: Relations
A common source of partial orders in computer science is in “task graphs”. You have a set of tasks
A, and a relation R in which aRb means “b cannot be done until a is finished”. Implicitly, “if all
the things that point at b are done, I can do b.” This can be nicely drawn as a graph. We draw an
arrow from a to b if aRb. For example, below is a graphs that describes the order in which one
would put on clothes. The set is of clothes, and the edges say what should be put on before what.
underwear shirt
The “depends on” graph imposes an ordering on tasks. But what if I add a relation edge from
belt to underwear? In that case my dependency graph stops making sense: there is no way to get
dressed! What goes wrong? A cyclic dependency.
Definition 5.4. A cycle is a path that ends where it started (i.e., the last vertex equals the first).
Definition 5.5. A directed acyclic graph (DAG) is a directed graph with no cycles.
Proof. Suppose the graph representation of a partial order � has a cycle a1 , . . . , ak , a1 . Then by
transitivity of � (with an induction hiding inside) we have a1 � ak . We also have ak � a1 . This
violates the antisymmetry of �, a contradiction.
But is it a partial order? No, because it isn’t reflexive or transitive. But there is a natural extension:
the reflexive transitive closure is a partial order. It gives the relation “must be done before.”
Proof. Let the DAG be R and its transitive reflexive closure S. S is transitive and reflexive; we just
need to prove that it is antisymmetric. We do so by contradiction. Suppose that there exists some
a, b such that a �= b, a ∼S b, and b ∼S a. In other words, there is a path from a to b and a path from
b to a in R. If we attach these two paths, we get a path from a to a in R, i.e., R contains a cycle.
This contradicts the assumption that R is acyclic.
Course Notes 3: Relations 15
A partial order is called partial because it is not necessary that an ordering exists between every
pair of elements in the set.
Example 5.8. Lshoe and Rshoe have no prescribed ordering between them.
Example 5.9. For two sets, it’s not necessary that either be a subset of the other.
When there is no prescribed order between two elements we say that they are “incomparable”.
Definition 5.10. We say that a and b are incomparable if neither a � b nor b � a, and that they are
comparable if a � b or b � a.
However, a partial order need not have incomparable elements. As a special case, we can have a
partial order in which there is a specified order between every pair of elements.
Sometimes when we have a partial order, e.g., of tasks to be performed, we want to obtain a
consistent total order. That is, an order in which to perform all the tasks, one at a time, so as not
to conflict with the precedence requirements.
The task of finding an ordering that is consistent with a partial order is known as topological sort
ing—probably because the sort is based only on the shape, i.e., topology, of the poset, and not on
the actual values.
Definition 5.13. A topological sort of a finite poset (A, �) is a total ordering of all the elements of
A, a1 , a2 , · · · , an in such a way that for all i < j, either ai � aj or ai and aj are incomparable.
For example, underwear, shirt, Lsock, Rsock, pants, sweater, Lshoe, Rshoe, belt, jacket is a topo
logical sort of how to dress. One of the nice facts about posets is that such an ordering always
exists and is even easy to find:
The basic idea to prove this theorem is to pick off a “first” element and then proceed inductively.
Definition 5.15. A minimal element a in a poset (A, �) is one for which (∀b ∈ A)[a � b∨a and b are incomparable].
Equivalently, it is an element a for which (�b ∈ A)[b � a].
Proof. For every element a ∈ A, let pa = {b ∈ A given b � a}, i.e., pa is the set of predecessors of a
according to the partial order. It is enough to show that there exists an a ∈ A such that pa = ∅; to
accomplish this, we use well-ordering.
Let P = {pa given a ∈ A}. Now let a� be an element corresponding to a set pa� of minimum
cardinality. We now that such an a� exists by applying the well-ordering principle to the subset
{|p| : p ∈ P } of N. There can be several sets in P of minimum cardinality, but that doesn’t matter—
we pick pa� to be one of the sets. We now prove by contradiction that pa� = ∅.
Suppose that |pa� | > 0, i.e., that pa� �= ∅. Then there exists some b� ∈ A such that b� � a� . Consider
the set pb� . We now claim that pb� ⊂ pa� . Since c ∈ pb� implies that c � b� , we obtain, by transitivity,
that c ∈ pb� implies that c � a� , i.e., every element in pb� is also an element of pa� , or, equivalently,
pb� ⊆ pa� . Furthermore, b� ∈ pa� since b� � a� , but b� ∈ / pb� since b� � b� . Thus pb� ⊂ pa� . But this
contradicts our assumption that pa� is a set of minimum cardinality. Thus, pa� = ∅.
Example 5.17. Consider the dressing example. Construct an ordering by picking one item at a
time. At each step, look at the poset formed by the remaining elements. Lsock, shirt, sweater,
Rsock, underwear, pants, Lshoe, belt, jacket, Rshoe
When elements of a poset are tasks that need to be done and the partial order is precedence con
straints, topological sorting provides us with a legal way to execute tasks sequentially, i.e., without
violating any precedence constraints. But what if we have the ability to execute more than one task
at the same time? For example, say tasks are programs, partial order indicates data dependence,
and we have a parallel machine with lots of processors instead of a sequential machine with only
one. How should we schedule the tasks? Our goal should be to minimize the total time to com
plete all the tasks. For simplicity, let’s say all the tasks take the same amount of time and all the
processors are identical.
So, given a finite poset of tasks, how long does it take to do them all, in an optimal parallel sched
ule? We can use partial order concepts to analyze this problem.
On the clothes example, we could do all the minimal elements first (Lsock, Rsock, underwear,
shirt), remove them and repeat. We’d need lots of hands, or maybe dressing servants. We can do
pants and sweater next, and then Lshoe, Rshoe, and belt, and finally jacket.
We can’t do any better, because the sequence underwear, pants, belt, jacket must be done in that
order. A sequence like this is called a chain.
Definition 5.19. A chain in a poset is a sequence of elements of the domain, each of which is
smaller than the next in the partial order (� and �=). The length of a chain is the number of elements
in the chain.
Clearly, the parallel time is at least length of any chain. For if we used less time, then two tasks
in the chain would have to be done at the same time. (This is “obvious,” but is formalized as an
application of the “pigeonhole principle” we will study shortly.) But by definition of chains this
violates precedence constraints. A longest chain is also known as a critical path. So we need at least
t steps, where t is the length of the longest chain. Fortunately, it is always possible to use only t:
Theorem 5.20. Given any finite poset (A, �) for which the longest chain has length t, it is possible to
partition A into t subsets, A1 , A2 , · · · , At such that
� �
(∀i ∈ {1, 2, . . . , t})(∀a ∈ Ai )(∀b � a) b ∈ A1 ∪ A2 ∪ · · · ∪ Ai−1 .
That is, we can divide up the tasks into t groups so that for each group Ai , all tasks that have to
precede tasks in Ai are in smaller-numbered groups.
Corollary 5.21. For (A, �) and t as above, it is possible to schedule all tasks in t steps.
Proof. For all i, schedule all elements of Ai at time i. This satisfies the precedence requirements,
because all tasks that must precede a task are scheduled at preceding times.
A4 jacket
A2 pants sweater
This gives just t sets, because the longest chain has length t. Also, each a ∈ A belongs to exactly
one Ai . To complete the proof, we also need to show
� �
(∀i ∈ {1, 2, . . . , t})(∀a ∈ Ai )(∀b � a) b ∈ A1 ∪ A2 ∪ · · · ∪ Ai−1 .
The proof is by contradiction. Assume that a ∈ Ai and that there exists a b ∈/ A1 ∪ A2 ∪ · · · ∪ Ai−1
such that b � a. Then there is a chain of length exceeding i − 1 ending in b. This means, since
b � a, that there is a chain of length > i ending in a, which means that a ∈
/ Ai .
So with an unlimited number of processors, the time to complete all the tasks is the length of the
longest chain. It turns out that this theorem is good for more than parallel scheduling. It is usually
stated as follows.
18 Course Notes 3: Relations
Corollary 5.24. If t is the length of the longest chain in a poset (A, �) then A can be partitioned into t
antichains.
Proof. Let the antichains be the sets Ai defined as in the proof of Theorem 5.20. We now claim that
the elements in those sets are incomparable. Suppose that there exists a, b ∈ Ai such that a �= b
and a and b are comparable. Then either a � b or b � a, which—again by the proof of Theorem
5.20—contradicts the assumption that a and b are in the same Ai .
We can use the above corollary to prove a famous result about posets:
Theorem 5.25 (Dilworth). For all t, every poset with n elements must have either a chain of size greater
than t or an antichain of size at least n/t.
Proof. Assume there is no chain of length greater than t. So, longest chain has length at most t.
Then by Corollary 5.24, the n elements can be partitioned into at most t antichains. Let � be the
size of the largest antichain. Since there are at most t antichains, every antichain contains at most
� elements, and an element belongs to exactly one antichain, �t ≥ n. So there is an antichain with
at least n/t elements.
√
Corollary 5.26. Every poset with n elements has a chain of length greater than n or an antichain of size
√
at least n.
√
Proof. Set t = n in Theorem 5.25.
Example 5.27. In the dressing poset, n = 10. Try t = 3. Has a chain of length 4. Try t = 4. Has no
chain of length 5, but has an antichain of size 4 ≥ 10/4.
Posets arise in all sorts of contexts, and when they do, Dilworth’s theorem can have interesting
implications.
Theorem 5.28. In any sequence of n different numbers, there is either an increasing subsequence of length
√ √
greater than n or a decreasing subsequence of length at least n.
Example 5.29. �6, 4, 7, 9, 1, 2, 5, 3, 8� has the decreasing sequence �6, 4, 1� and the increasing sequence
�1, 2, 3, 8�.
We can prove this using Dilworth’s theorem; the trick is to define the appropriate poset. The do-
main is the set of values in the sequence. For the ordering, define a � b if either a = b, or else
(a < b and a comes before b in the sequence). You should check that this is reflexive (stated explic
itly), transitive, and antisymmetric. A chain corresponds to a sequence that increases in value and
moves to the right, that is, an increasing sequence. But what does an antichain correspond to?
Course Notes 3: Relations 19
Lemma 5.30. If a and b are incomparable (under the partial order) and a > b (as numbers) then a precedes
b in the sequence.
Proof. By contradiction. Assume b precedes a in the sequence. Then b < a and b precedes a in
the sequence, so b � a in the partial order. This contradicts our assumption that a and b are
incomparable.
Lemma 5.31. If a1 , a2 , · · · , at are incomparable and a1 > a2 > · · · > at then a1 , a2 , · · · at form a decreas
ing subsequence.
Proof. For all i, the fact that ai > ai+1 implies that ai precedes ai+1 in the sequence, by the previous
lemma.
So given an antichain, arrange the elements so that they are in decreasing order and the “left of”
relation follows, giving a decreasing subsequence. Dilworth’s theorem implies that there is either
√ √
a chain of size greater than n or an antichain of size at most n. By the analysis above, this yields
√
either an increasing sequence of length greater than n or a decreasing subsequence of length at
√
most n.