Comp106 2 Relations
Comp106 2 Relations
Relations
Relations are discrete structures that are used to represent relationships between
elements of sets.
• between a business and its telephone number
• between an employee and his or her salary
• between a program and a variable it uses
Relations can be used to solve various problems in computer science such as:
Determining which pairs of cities are linked by airline flights in a network
Computing the distance between a pair of registered Facebook users
Finding an efficient order for different phases of a complicated project
Producing a useful way to store information in computer databases
9.1 Relations and Their Properties
a1
b1
a2
b2
a3
A B
R1
Some relations make sense:
e.g.
A: set of cities
B: set of countries
R: (a,b)R if city a is in country b.
(Izmir, Turkey) R
(Paris, France) R
(Paris, Turkey) R
A function ƒ from A to B can be thought of as the set of ordered pairs (a,b) s.t. b = ƒ(a).
0 a
a b
1 b
0 ×
2
1 ×
A B 2 ×
Relations defined on a single set:
This reads like “R is the set of all pairs
A relation on a set A is a relation from A to A. (a,b) such that a divides b, where a and b
are both elements of A”.
e.g.
A = {1, 2, 3, 4}
R = {(a,b) | a | b, (a,b) A×A}
= {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 4), (3, 3), (4, 4)}
e.g.
How many relations are there on a set with n elements?
|A×A| = n2
2
2𝑛 (# of subsets of A×A)
Properties of Relations defined on a set:
e.g.
A= {1, 2, 3}
e.g.
R: The set of pairs of people having the same eye color (reflexive)
e.g. on integers
R1 = {(a, b) | a ≤ b} ??
R2 = {(a, b) | a > b} ??
R3 = {(a, b) | a = b a = -b} ??
R4 = {(a, b) | a = b} ??
R5 = {(a, b) | a = b + 1} ??
R6 = {(a, b) | a + b ≤ 3} ??
Definition:
A relation R on a set A is called
e.g.
Rt = {(a,b) | a is taller than b} transitive
e.g.
A= {1, 2, 3}
If R is reflexive, then:
there are n pairs such that (a,a)R (this is a requirement for reflexivity),
and there exist n(n–1) pairs such that a b;
any combination of these n(n–1) pairs can also be in R.
# of reflexive relations = 2n(n-1)
e.g.
How many symmetric relations are there on a set with n elements? (Exercise)
Combining relations:
R1 = {(a,1), (b,3)}
R2 = {(a,1), (a,2), (b,1), (b,2)}
R3 = {(b,1), (b,2)}
R4 = {(a,1), (b,2)}
Definition:
Let R be a relation on A.
S○R = {(a1, c4), (a2, c3), (a4, c4), (a4, c1)}
The powers Rn, n = 1, 2, 3, …, are defined by
R1 = R, R2 = R○R,… Rn = Rn-1 ○ R.
Proof:
e.g.
Let R = {(a, b) | a < b} R: A → B.
e.g.
How many relations are there on a set with n elements?
|A×A| = n2
n2
2 (# of subsets of A×A)
e.g.
How many reflexive relations are there on a set with n elements?
If R is reflexive, then:
there are n pairs such that (a, a)R
and n(n–1) pairs such that (a, b)R where a b
# of reflexive relations = 2n(n-1)
9.3 Representing Relations
Definition: A relation R can also be represented by a matrix MR = [mij]:
1 if (ai , bj ) R
mij m11 … mN1
0 if (ai , b j ) R MR = ⋮ ⋮ ⋮
m1K … mNK
K×N
e.g. Let A = {1, 2}, B = {a, b, c} and R: A→B such that
R = {(1,b), (2,a), (2,b), (2,c)}
0 1 0
MR =
1 1 1
e.g.
Let R be a relation defined on A = {1, 2, 3}: R = {(1,2), (2,2), (1,3)}
0 1 1
MR = 0 1 0 Note that we get a square matrix whenever R: A→ A.
0 0 0
Remark: The matrix representation of a relation assumes a certain ordering of the elements as given in the sets. If
you change the order of the elements, the matrix representation changes.
- Reflexive relation R s.t. i (ai, ai)R
i mii = 1
If MR = [mij]K×N then
e.g.
Hence, to obtain cij , we take the ith row of A and jth column of B and then
compute the dot product:
ith row
jth column
MS○R = MR MS
Recall the following example:
MS○R = MR MS
rth Boolean Power:
Let A be a square (M×M) zero-one matrix and r be a positive integer.
Ar = A A … A
r times
A0 = I
Remark: Let R: A→ A
MRn [MR ]
n
Representing Relations Using Graphs:
Pictorial representation.
Definition:
A directed graph (digraph) consists of a set V of vertices (or nodes) along with a set E
of edges (or arcs) which are ordered pairs of vertices.
Edge(a, b): a is initial vertex (node), b is terminal vertex (node)
e.g.
a
loop
Graph
representation
0 1 0 Matrix
b c
𝑀𝑅 = 0 0 1 representation
Set 0 1 1
R = {(a,b), (b,c), (c,b), (c,c)} representation
a . .b
reflexive
c.
Example to graph representation of a relation:
Connectivity problems:
S = {(1, 1), (1, 2), (3, 2), (2, 2), (3, 3)}
The closure S is the smallest possible relation with property P, which contains R.
S = {(1, 1), (1, 2), (3, 2), (2, 2), (3, 3)}
S = Reflexive closure of R = R ,
where = {(a, a) | a A} : diagonal relation
e.g.
R = {(a, b) | a < b}, reflexive closure?
Let R = {(1, 1), (1, 2), (2, 1), (2, 3), (3, 1), (3, 3)} on A = {1, 2, 3}
We should add all ordered pairs (b, a), where (a, b) is in R but (b, a) is not.
Symmetric closure of R = R {(3, 2), (1, 3)}
e.g.
R = {(a, b) | a < b}
Let R = {(1, 3), (1, 4), (2, 1), (3, 2)} on {1, 2, 3, 4}
R is not transitive since there are pairs (a, c)R although (a, b), (b, c)R.
We might repeat step (i) until reaching a transitive relation. But there are better ways.
Remark: The transitive closure of R contains all the pairs (a, b) of R, where a is
directly or indirectly linked to b via transitions
e.g. Draw transitive closure of
a. .b
c. .d
e.g.
a. . b A path:
a, b, d, a, e
e.g.
a. . b A path:
a, b, d, e
c. .d .e
But also (a,e)R6 since there is also another path of length 6 between a and e:
a,b,d,a,c,d,e
Theorem:
Let R be a relation on A. Then n > 0, there is a path of length n from a to b iff (a, b)Rn.
Proof: Use induction, where P(n): There is a path of length n from a to b iff (a, b)Rn
Basis step:
By definition there is a path of length 1 from a to b iff (a, b)R. Hence P(n) is T for n = 1.
Inductive step: Assume P(n) is T for some arbitrary fixed n 1. Show P(n+1) is T.
There is a path of length n+1 from a to b iff
cA s. t. there is a path of length 1 from a to c and a path of length n from c to b
that is, cA such that (a,c)R and (c,b)Rn (by inductive hypothesis)
which means (a, b) Rn+1 = Rn ○ R (by definition of composite relation).
P(n+1) is T.
Connectivity relation R* consists of all those pairs (a, b) s.t. there is a path between a
and b in R.
Since Rn includes all the paths of length n by the previous theorem, There is a path of length n
from a to b iff (a, b)Rn
e.g.
Let R be a relation on the set of people in the world that contains (a,b) if a has met b.
R*: ? (a, b)R* if there is a sequence of people, starting with a and ending with b.
Connectivity Relation = Transitive Closure
Finding transitive closure is equivalent to determining all pairs of vertices that are
connected through a path.
Recall that, to find the transitive closure of a relation, we need to iteratively add to
the relation all the missing transitions.
Theorem:
The transitive closure of a relation R equals to the connectivity relation R*.
Proof:
We must show that,
(i) R* is transitive
(ii) R* is the smallest possible transitive relation that contains R
(any transitive relation that contains R also contains R* )
i. R* is transitive?
If (a, b)R*, there is a path from a to b.
If (b, c)R*, there is a path from b to c.
There is a path from from a to c, which means (a, c)R*, hence R* is transitive.
ii. Let S be any transitive relation that contains R, i.e., R S. Show R* S.
Since S is transitive, S n S for all n (by the theorem in Section 9.1) R on a set A is transitive
iff Rn R for all n
S n S and 𝑆∗ = S* S
Also, since R S (given), R* S * (by definition of connectivity relation)
R* S.
Hence, any transitive relation that contains R also contains R*
|A| = 5
Given R, how can we compute the connectivity relation R*?
Lemma:
Let R be a relation in A and |A| = n. If there is a path from a to b in R, then one can
always find a path from a to b with length not exceeding n.
Hence
|A| = 5
Given R, how can we compute the connectivity relation R*?
Lemma:
Let R be a relation in A and |A| = n. If there is a path from a to b in R, then one can
always find a path from a to b with length not exceeding n.
Proof:
Suppose there is a path x0, x1, …, xm from x0 = a to xm = b with length m.
If m n, then there are at least two vertices on this path, equal to each other xi = xj such
that 0 i j m –1. (by the pigeonhole principle)
e.g.
Let R = {(a, a), (a, c), (b, a), (c, a), (c, c)}. Find R*.
no need to compute
powers beyond n = 3
Note: Although in general S○R ≠ R○S, while computing powers of a relation, the order
of compositions does not matter, hence
Rn+1 = Rn ○ R = R○ Rn
Algorithm for computation of connectivity relation (or transitive closure):
Complexity:
A MR : T(n) = (n 1) (n2(2n 1) + n2)
(n + (n –1))n2 operations = (n 1)(2n3)
BA: T(n) is O(n4). (Polynomial complexity)
n2 operations
e.g. Let (a, b)R if there is a non-stop flight from city a to b. When is (a, b) in
R2?
If c s.t. (a, c) R, (c, b)R.
R3?
If c,d s.t. (a, c) R, (c, d)R, (d, b)R.
R*?
If it is possible to fly from a to b.
R* can be computed using the algorithm of the previous slide given the flight nework.
9.5 Equivalence Relations
e.g. Consider the relation R = {(a, b) a b (mod 4)}
R is symmetric, transitive and reflexive.
Hence we say, R is an equivalence relation.
What matters?
R divides (or partitions) the set of integers into four disjoint subsets:
where any two integers in a given subset are related with R, hence said to
be “equivalent” to each other.
R is an equivalence relation.
R is reflexive
R is symmetric
R is transitive
R is an equivalence relation.
R divides (or partitions) the set of strings into disjoint subsets, where each subset contains all
strings of the same length.
These subsets are equivalence classes. Any two strings in a given subset are equivalent to each
other (with respect to the relation).
e.g.
Relations on a set of people:
a) {(a, b) a and b are at the same age}
b) {(a, b) a and b speak a common language}
e.g.
R = {(a, b) a b (mod m)}
Equivalence classes partition (or divide) a set into disjoint, nonempty subsets.
e.g.
R = {(a, b) a b (mod m)}
m equivalence classes:
[0]R , [1]R, ….., [m 1]R
All are disjoint and form a partition.
Equivalence Classes and Partitions:
Equivalence classes partition (or divide) a set into disjoint, nonempty subsets.
Equivalence classes partition (or divide) a set into disjoint, nonempty subsets.
e.g.
Relation R on a set of people: {(a, b) a and b are at the same age}
R partitions the set of people into equivalence classes (hence into nonempty disjoint
subsets).
Each equivalence class is the set of people who are at the same age, for example [a]R is
the set of people who are 18 years old (if a is 18 years old).
Equivalence Classes and Partitioning:
R is reflexive since a,b ab = ab, which implies that ((a, b), (a, b )) R
R is symmetric since a,b,c,d ((a, b),(c, d )) R ad = bc da = cb
((c, d), (a, b)) R.
R is transitive since
a,b,c,d,e,f ((a, b), (c, d )) R and ((c, d ), (e, f )) R (ad = bc and cf = de)
af = be ((a, b), (e, f )) R
Thus, equivalence classes for this relation partition the set of positive real number pairs
into disjoint nonempty subsets such that
Remark: If the relation R were not defined on a set of positive valued number pairs,
(including 0 in particular), then R would not be an equivalence relation. Why?
e.g. a) Let R be the relation on the set of functions from Z+ to Z+ such that (f, g) belongs to R iff f is Θ(g).
Show that R is an equivalence relation. Use the definition of big-O notation whenever necessary.
b) Describe the equivalence class containing f (n) = n2 for the equivalence class of part (a).
a) We need to show that R is reflexive, symmetric and transitive.
• Any two elements a and b in an equivalence class are then related, hence
“equivalent” with respect to R
9.6 Partial Order Relations
e.g.
R = {(a, b) a | b}
e.g.
Let R = {(1, 1), (2,2), (3, 3), (2, 1), (2, 3), (3, 1)} on S = {1, 2, 3}.
R is reflexive, anti-symmetric and transitive R is a partial order (S, R) is a poset.
1 ≼1 2 ≼2 3 ≼3 2 ≼1 2 ≼3 3 ≼1
e.g.
Poset (Z+, R) with R = {(a, b) b | a}.
1 ≼1 4 ≼2 10 ≼5
e.g. R = {(1, 1), (2,2), (3, 3), (2, 1), (2, 3), (3, 1) } on S = {1, 2, 3}.
R is a total ordering; every two elements are comparable.
{a1 ,a2 ,…,an} is a compatible total ordering of S (compatible with relation R).
Definition:
An element a is maximal in poset (S, ≼) if there is no bS s.t. a ≺b.
An element a is minimal in poset (S, ≼) if there is no bS s.t. b ≺a.
A poset in which every pair of elements has both the least upper bound and the greatest lower
bound is called a lattice.