Chap. 4 - Sun Yat

Download Report

Transcript Chap. 4 - Sun Yat

Chapter 4 Relations and Digraphs
Weiqi Luo (骆伟祺)
School of Software
Sun Yat-Sen University
Email:[email protected] Office:A309
Chapter Four: Relations and Digraphs








4.1 Product Sets and Partitions
4.2 Relations and Digraphs
4.3 Paths in Relations and Digraphs
4.4 Properties of Relations
4.5 Equivalence Relations
4.6 Data structures for Relations and Digraphs
4.7 Operations on Relations
4.8 Transitive Closure and Warshall’s Algorithm
2
School of Software
4.1. Product Sets and Partitions
 Ordered pair
An order pair (a, b) is a listing of objects a and b in a prescribed
order, which a appearing first and b appearing second
(a,b) = (c,d) <=> a=c and b=d
 Product set
If A and B are two nonempty sets, we define the product set or
Cartesian product A × B as the set of all ordered pairs (a, b)
with a ∈ A and b ∈ B. Thus
A × B = {(a, b) | a ∈ A and b ∈ B}
3
School of Software
4.1. Product Sets and Partitions
 Example 1 & 2
Let A={1, 2, 3} and B={r, s}
then
A × B = {(1, r), (1, s), (2, r), (2, s), (3, r), (3, s)}
B × A = {(r, 1), (s, 1), (r, 2), (s, 2), (r, 3), (s, 3)}
Note: A × B is not equal to B × A
4
School of Software
4.1. Product Sets and Partitions
 Theorem 1: For any two finite, nonempty sets A
and B, |A×B| = |A||B|
Proof:
Suppose |A| = m, |B|=n. To form an ordered pair (a, b), a
∈ A, and b∈B, we must perform two successive tasks:
1) choose a first element from A
(m ways)
2) choose a second element from B (n ways)
By multiplication principle (Section 3.1). There are
totally m×n ways to form an ordered pair (a, b), which
means that |A×B| = |A||B| = m×n
5
School of Software
4.1. Product Sets and Partitions
 Example 3
If A=B=R, the set of all real numbers, then R×R, also
denoted by R2, is the set of all points in the plane. The
ordered pair (a, b) gives the coordinates of a point in the
plane
Y
(a, b)
O
X
6
School of Software
4.1. Product Sets and Partitions
 Cartesian product
A1 × A2 × … × Am = {(a1,a2, …,am) | ai ∈ Ai}
where A1, A2 , … , Am are non-empty sets.
 | A1 × A2 × … × Am | = |A1| |A2| … |Am|
where A1, A2 , … , Am are finite, nonempty sets
7
School of Software
4.1. Product Sets and Partitions
 Example 5
A manufacture offers the following options for its
refrigerators
Doors:
side-by-side(s) over-under(u) , three (t)
Icemaker: freezer(f), door(d)
Finish:
standard(r), metallic(m), custom(c)
Let D={s,u,t} I={f,d} and F={r, m, c}
then the Cartesian product D ×I ×F contains all the
categories that describe refigerator options. There are 3 ×2
×3=18 categories.
8
School of Software
4.1. Product Sets and Partitions
 Employees
Employee ID
Last Name
Department
Years with
company
8341
Croft
Front Office
2
7984
Cottongim
Sales
2
2086
King
Human Resources
4
0340
Boswell
Research
3
Harris
Sales
4
…
…
3465
 Select D[ai1=required1, ai2=required2,…, aik=requiredk]
9
School of Software
4.1. Product Sets and Partitions
 Partition
A partition or quotient set of a nonempty set A is a
collection B of nonempty subsets of A such that
1. Each element of A belongs to one of the set in B
2. If A1 and A2 are distinct elements of B, then
A1 ∩ A2 = { }
A1
A2
A3
A4
A6
A
B={A1, A2, A3, A4, A5, A6}
A5
Blocks or cells
10
School of Software
4.1. Product Sets and Partitions
 Example 6
Let A={a, b, c, d, e, f, g, h}, consider the following
subsets of A
A1= {a, b, c, d} A2={a, c, e, f, g, h}
A3= {a, c, e, g} A4={b, d} A5={f, h}
Then {A1, A2} is not a partition, since A1 ∩ A2 is not an
empty set. Also {A1 , A5} is not a partition since e is not
in A1 and A5 . The collection B={A3 A4 A5} is a
partition of A
11
School of Software
4.1. Product Sets and Partitions
 Homework
Ex. 2, Ex. 10, Ex. 17, Ex. 32, Ex. 36, Ex. 37
12
School of Software
4.2 Relations and Digraphs
 Relation
Let A and B be nonempty sets. A relation R from A to
B is a subset of A × B. If R ⊆ A× B and (a, b) ∈ R,
we say that a is related to b by R, write a R b, if a is not
related to b by R, we write a b.
If A and B are equal. We say that If R ⊆ A× A is a
related on A, instead of a relation from A to A.
13
School of Software
4.2 Relations and Digraphs
 Example 1
Let A ={1, 2, 3} and B={r, s}. Then we define R={(1,
r), (2, s), (3, r)} is a relation from A to B.
 Example 2
Let A and B be sets of real numbers. We define the
following relation R(equals) from A to B
Y
O
X
14
School of Software
4.2 Relations and Digraphs
 Example 3
Let A={1, 2, 3, 4}. Define the following relation R (less
than) on A
a R b if and only if a<b
Then R= { (1,2) (1,3) (1,4) (2, 3) (2,4) (3,4) }
 Example 4
Let A=Z+, the set of all positive integers. Define the
following relation R on A:
a R b if and only if a divides b
Then 4 R 12, 6 R 30, but 5 7 .
15
School of Software
4.2 Relations and Digraphs
 Example 5
Let A be the set of all people in the world. We define the
following relation R on A: a R b if any only if there is a sequence
a0, a1, a2, …,an of people such that a0=a, an=b and ai-1 knows ai,
i=1,2,…,n (n will depend on a and b)
Small-world network
http://en.wikipedia.org/wiki/Small-world_network
16
School of Software
4.2 Relations and Digraphs
 Example 6
Let A=R, the set of real numbers. We define the
following relation R on A
x R y if and only if x and y satisfy the equation
x2/4+y2/9=1
(0,3)
Y
O
(2,0)
17
X
School of Software
4.2 Relations and Digraphs
 Example 9
C1
C1
C2
C3
C4
C5
140
100
150
200
200
160
220
190
250
C2
190
C3
110
180
C4
190
200
120
C5
200
100
200
150
150
Ci R Cj if and only if the cost of going from Ci to Cj
defined and less than or equal to $180.
18
School of Software
4.2 Relations and Digraphs
 Sets arising from relations
Let R ⊆ A× B be a relation from A to B.
a) The domain of R
Denoted by Dom(R), is the set of all first elements in
the pairs that make up R. (Dom(R) ⊆ A )
b) The range of R
Denoted by Ran(R), is the set of all second elements in
the pairs that make up R. (Ran(R) ⊆ B )
19
School of Software
4.2 Relations and Digraphs
 Example 12
Let R be the relation of Example 6
(0,3)
Y
O
(2,0)
then
Dom(R) = [-2, +2]
X
Ran(R) = [-3, +3]
20
School of Software
4.2 Relations and Digraphs
 R-relative set of x
If R is a relation from A to B and x ∈ A, we define
R(x), the R-relative set of x, to be the set of all y in B
with the property that x is R-related to y.
R(x) = {y ∈ B | x R y}
If A1 ⊆A, then R(A1), the R-relative set of A1, is the set
of all y in B with the property that x is R-related to y for
some x in A1.
R(A1) = {y ∈ B | x R y for some x in A1}
21
School of Software
4.2 Relations and Digraphs
 Example 13
Let A={a, b, c, d} and let R={(a,a), (a,b), (b,c),
(c,a), (d,c), (c,b)}. Then
R(a) = {a, b} R(b)={c}
If A1={c, d}, Then
R(A1)= {a, b, c}
22
School of Software
4.2 Relations and Digraphs
 Let R be the relation of Example 6
A1=[-∞, -2) U (+2, +∞], A2=[-2, +2] then
R(A1)= ф , R(A2)= [-3, +3]
R(-2)={0}, R(+2)={0} ,
R(0) = {-3, +3}
(0,3)
Y
O
(2,0)
23
X
School of Software
4.2 Relations and Digraphs
 Theorem 1
Let R be a relation from A to B, and let A1 and A2 be
subsets of A, then
(a) If A1 ⊆ A2, then R(A1) ⊆ R(A2)
(b) R(A1 U A2 ) = R(A1) U R(A2)
(c) R (A1 ∩ A2 ) ⊆ R(A1 ) ∩ R(A2)
24
School of Software
4.2 Relations and Digraphs
 (a) If A1 ⊆ A2, then R(A1) ⊆ R(A2)
Proof:
If y ∈ R(A1), then x R y for some x in A1, since
A1 ⊆ A2, x ∈A2, thus y ∈ R(A2) and therefore
R(A1) ⊆ R(A2)
25
School of Software
4.2 Relations and Digraphs
 (b) R(A1 U A2 ) = R(A1) U R(A2)
Proof :
(1) R(A1 U A2 ) ⊆ R(A1) U R(A2)
If y ∈ R(A1 U A2 ), then x R y for some x in A1 U A2, then x in
A1 or A2. If x in A1 , y ∈ R(A1); if x in A2 . y ∈ R(A2). In either
cases, y ∈ R(A1) U R(A2) , therefore
R(A1 U A2 ) ⊆ R(A1) U R(A2)
(2) R(A1) U R(A2) ⊆ R(A1 U A2 )
A1 ⊆ A1 U A2 , then R(A1) ⊆ R(A1 U A2 )
A2 ⊆ A1 U A2 , then R(A2) ⊆ R(A1 U A2 )
Thus R(A1) U R(A2) ⊆ R(A1 U A2 )
Therefore, we obtain R(A1 U A2 ) = R(A1) U R(A2)
26
School of Software
4.2 Relations and Digraphs
 (c) R (A1 ∩ A2) ⊆ R(A1) ∩ R(A2)
Proof:
If y ∈ R (A1 ∩ A2 ) , then x R y for some x in A1 ∩ A2,
since x is in both A1 and A2 , it follows that y is in both
R(A1) and R(A2) ; that is, y ∈ R(A1 ) ∩ R(A2).
Therefore
R (A1 ∩ A2) ⊆ R(A1) ∩ R(A2)
Note: R(A1) ∩ R(A2) ⊆ R (A1 ∩ A2)
27
School of Software
4.2 Relations and Digraphs
 Example 15
Let A=Z, R be “≤” A1={0,1,2} and A2={9,13}. Then R(A1)
consists of all integers n such that 0 ≤n, or 1 ≤n, or 2 ≤ n,
thus
R(A1) ={0, 1, 2,…}.
Similarly R(A2)={9, 10, 11,…}, so
R(A1) ∩ R(A2)= R(A2)={9,10,11,…}
On the other hand, A1 ∩ A2 = ф , therefore,
R (A1 ∩ A2 ) = ф
This shows that
R(A1 ) ∩ R(A2) ⊆ R (A1 ∩ A2 )
28
School of Software
4.2 Relations and Digraphs
 Theorem 2
Let R and S be relations from A to B, If R(a) =S(a) for
all a in A, then R=S
Proof:
If a R b, then b ∈ R(a). Therefore, b ∈ S(a) and a S b. A
completely similar argument shows that, if a S b, then a
R b. Thus R=S.
29
School of Software
4.2 Relations and Digraphs
 The matrix of a Relation
If A={a1,a2, …am} and B={b1,b2,…bn} are finite sets
containing m and n elements, respectively, and R is a
relation from A to B, we represent R by the m×n
matrix MR =[mij], which is defined by
mij = 1
if (ai bj) ∈ R
=0
otherwise
The matrix MR is called the matrix of R.
30
School of Software
4.2 Relations and Digraphs
 Example 17
Let A ={1, 2, 3} and B={r, s}. Then we define
R={(1, r), (2, s), (3, r)} is a relation from A to B.
Then the matrix of R is
31
School of Software
4.2 Relations and Digraphs
 Consider the matrix
1 0 0 1
M   0 1 1 0 
1 0 1 0 
Since M is 3×4, we let
A={a1,a2,a3} and B={b1,b2,b3,b4}
Then (ai,bj) in R if and only if mij=1, thus
R={(a1,b1),(a1,b4),(a2,b2),(a2,b3),(a3,b1),(a3,b3}
32
School of Software
4.2 Relations and Digraphs
 The Digraph of a Relation
If A is a finite set, R is a relation on A (from A to A)
1) Draw a small circle for each element of A and label the
circle with the corresponding element of A (Vertices)
2) Draw an arrow from vertex ai to vertex aj if and only if
ai R aj (Edge)
The resulting pictorial representation of R is called a
directed graph or digraphy of R.
33
School of Software
4.2 Relations and Digraphs
 Example 19
Let A={1, 2, 3, 4}
R={(1,1), (1,2), (2,1), (2,2), (2,3), (2,4), (3,4), (4,1)}
34
School of Software
4.2 Relations and Digraphs
 Example 20
Find the relation determined by the following Figure.
R={ (1,1), (1,3), (2,3), (3,3), (3,2), (4,3) }
35
School of Software
4.2 Relations and Digraphs
 In-/Out- Degree
If R is a relation on a set A, and a in A, then
The in-degree of a relative to the relation R is the
number of b in A such that (b,a) in R.
The out-degree of a is the number of b in A such that
(a,b) in R
36
School of Software
4.2 Relations and Digraphs
 Example 22
Let A={a, b, c, d}, and R be the relation on A that has the matrix
Construct the digraph of R, and list the in-degrees and outdegrees of all vertices
1
0
MR  
1

0
0 0 0
1 0 0 
1 1 0 

1 0 1
a
b
c
d
In-degree
2
3
1
1
Out-degree
1
1
3
2
37
School of Software
4.2 Relations and Digraphs
 If R is a relation on a set A, and B is a subset of A, the
restriction of R to B is
R ∩ (B×B)
 Example 24
Let A={a,b,c,d,e,f}, R={(a,a), (a,c), (b,c), (a,e), (b,e), (c,e)}.
Let B={a,b,c}, then B×B={(a,a), (a,b), (a,c), (b,a), (b,b),
(b,c), (c,a), (c,b), (c,c) }
and the restriction of R to B is
{(a, a), (a, c), (b, c)}
38
School of Software
4.2 Relations and Digraphs
 Homework
Ex. 2, Ex. 8, Ex.12, Ex.20
Ex.24, Ex. 28, Ex. 32
39
School of Software
4.3 Paths in Relations and Digraphs
 Suppose that R is a relation on a set A. A path of
length n in R from a to b is a finite sequence
п : a, x1, x2, …, xn-1, b
beginning with a and ending with b, such that
a R x1, x1 R x2, …,xn-1 R b
Note: A path of length n involves n+1 elements
of A, although they are not necessarily distinct.
40
School of Software
4.3 Paths in Relations and Digraphs
 Example 1
Consider the digraph in the following figure.
Then
п1: 1, 2, 5, 4, 3 is a path of
length 4 from vertex 1 to 3
п2: 1, 2, 5, 1 is a path of
length 3 from vertex 1 to itself
п3: 2, 2 is a path of length 1 from
vertex 2 to itself
Cycle: a path that begins and ends at the same vertex (п2 п3 )
41
School of Software
4.3 Paths in Relations and Digraphs
 If n is a fixed positive integer, we define a relation Rn
on A as follows: x Rn y means that there is a path of
length n from x to y in R
 Define a relation R∞ on A, by letting x R y mean that
there is some path in R from x to y. The length of such
a path will depend on x and y. The relation R∞ is
sometimes called the connectivity relation for R.
 Rn(x) consists of all verities that can be reached form x
by means of a path in R of length n.
The set R∞ (x) consists of all vertices that can be
reached from x by some path in R
42
School of Software
4.3 Paths in Relations and Digraphs
 Example 2
Let A be the set of all living human beings
R be the relation of mutual acquaintance
a R b means that a and b know one another
a R2 b means that a and b have an acquaintance in common
a Rn b if a knows someone x1, who knows x2, …, who knows
xn-1, who knows b.
a R∞ b means that some chain of acquaintances exists that
begins at a and ends at b.
Q: It is interesting (and unknown) whether every two
Americans, say, are related by R∞
43
School of Software
4.3 Paths in Relations and Digraphs
 Example 3
Let A be a set of cities
x R y if there is a direct flight from x to y on at least
one airline.
x Rn y if one can book a flight from x to y having
exactly n-1 intermediate stops
x R∞ y if one can get from x to y by plane
44
School of Software
4.3 Paths in Relations and Digraphs
 Example 5
Let A={a,b,c,d,e} and R={(a,a), (a,b), (b,c), (c,e), (c,d),
(d,e)} Compute (a) R2 (b) R ∞
R
45
School of Software
4.3 Paths in Relations and Digraphs
 Compute R2
a R2 a since a R a and a R a
a R2 b since a R a and a R b
a R2 c since a R b and b R c
b R2 e since b R c and c R e
b R2 d since b R c and c R d
c R2 e since c R d and d R e
Hence
R2 ={(a,a), (a,b), (a,c), (b,e), (b,d), (c,e)}
46
School of Software
4.3 Paths in Relations and Digraphs
 Compute R ∞
To compute R ∞ , we need all ordered pairs of vertices
for which there is a path of any length from the first
vertex to the second. We can see that from the figure
R∞ = { (a,a), (a,b), (a,c), (a,d), (a,e),
(b,c),(b,d), (b,e), (c,d), (c,e), (d,e) }.
R
Note: If |R| is large, it can be tedious
and perhaps difficult to compute R ∞
47
School of Software
4.3 Paths in Relations and Digraphs
 Boolean Matrix (p.37)
A Boolean Matrix is an m×n matrix whose entries are
either 0 or 1.
Let A=[aij] and B=[bij] be m×n matrix Boolean matrix.
The Join of A and B : A V B = C = [cij]
cij=1
if aij=1 or bij=1
cij=0
otherwise
The meet of A and B: A ^ B = D = [dij]
dij=1
if aij and bij are both 1
dij=0
otherwise
48
School of Software
4.3 Paths in Relations and Digraphs
 Boolean Product (p.38)
A = [aij] is an m×p Boolean matrix and
B = [bij] is a p×n Boolean matrix.
The Boolean product of A and B, denoted A⊙B, is the
m ×n Boolean matrix C=[cij] defined by
1 if aik  1 and bkj  1 for some k , 1  k  p
cij  
0 otherwise
49
School of Software
4.3 Paths in Relations and Digraphs
 Example
Let
1 1 0 
0 1 0 

A
1 1 0 


0 0 1
then
A
1 1 0 
0 1 0 

B
1 1 0 


0
0
1


1 0 0 0 
B   0 1 1 0 
1 0 1 1 
1 1 1 0 
1 0 0 0  

0
1
1
0
0 1 1 0  


 1 1 1 0 
1 0 1 1  

1
0
1
1


50
School of Software
4.3 Paths in Relations and Digraphs
 Theorem 5 (p. 39)
1. (a) A V B= B V A (b) A ^ B = B ^ A
2. (a) (A V B) V C = A V (B V C)
(b) (A ^ B) ^ C = A ^ (B ^ C)
3. (a) A ^ ( B V C) = (A ^ B) V (A ^ C)
(b) A V (B ^ C) = (A V B) ^ (A V C)
4. (A ⊙ B) ⊙C = A ⊙ (B ⊙C)
51
School of Software
4.3 Paths in Relations and Digraphs
 Theorem 1
If R is a relation on A={a1,a2,…an}, then
Proof:
M R2  M R
MR
Let MR=[mij] and MR2 =[nij]. By definition, the i, j-th element
of MR ⊙ MR is equal to 1 if only if row i of MR and column j of
MR has a 1 in the same relative position, say position k. This
means that mik=1 and mkj=1 for some k, 1 ≤ k ≤ n.
By definition of matrix MR, the preceding conditions mean
that ai R ak, and ak R aj. Thus ai R2 aj, and so nij=1.
Therefore, position i, j of MR ⊙ MR is 1 if and only if nij=1.
52
School of Software
4.3 Paths in Relations and Digraphs
 Example 6
Let A and R be as in Example 5. Then
1 1 0 0 0 
0 0 1 0 0 


M R  0 0 0 1 1 


0
0
0
0
1


0 0 0 0 0 
MR
1 1 0 0 0 
0 0 1 0 0 


M R  0 0 0 1 1 


0 0 0 0 1 
0 0 0 0 0 
1 1 0 0 0  1 1 1 0 0 
0 0 1 0 0  0 0 0 1 1 

 

0 0 0 1 1   0 0 0 0 1 

 

0 0 0 0 1  0 0 0 0 0 
0 0 0 0 0  0 0 0 0 0 
53
School of Software
4.3 Paths in Relations and Digraphs
 Theorem 2
For n ≥ 2 and R is a relation on a finite set A, we have
M Rn  M R
MR
M R ...
M R (n factors)
Proof by Mathematical Induction
(refer to p.138-139 for more details)
54
School of Software
4.3 Paths in Relations and Digraphs
 If R and S are relations on A. the relation R U S is
defined by x (R U S) y if and only if x R y or x R y. It is
easy to verify that MRUS =MR V MS


R =R  R 2  R 3  ..   R n
n 1
M R  =M R  M R 2  M R 3  ...
 M R  (M R )  (M R )  ...
2
55
3
School of Software
4.3 Paths in Relations and Digraphs
 The Reachability relation R* of a relation on a set A
that has n elements is defined as follows:
x R* y means that x=y or x R∞ y
M R* = I n  M R 
 I n  M R  (M R )  (M R )  ...
2
56
3
School of Software
4.3 Paths in Relations and Digraphs
 Composition
Let п1 = a, x1, x2, …, xn-1,b be a path in relation R of
length n form a to b,
and п2 = b, y1, y2, …, ym-1,c be a path in relation R of
length m form b to c,
then the composition of п1 and п2 is the path
a, x1, x2, …, xn-1,b y1, y2, …, ym-1,c of length n+m
denoted by п2 O п1
57
School of Software
4.3 Paths in Relations and Digraphs
 Consider the relation whose digraph is given in the
following figure and the paths
п1 = 1, 2, 3 and п2 = 3, 5, 6, 2, 4
Then the composition of п1 and п2 is the path
п2 O п1 : 1, 2, 3, 5, 6, 2, 4 from 1 to 4 of length 6
58
School of Software
4.3 Paths in Relations and Digraphs
 Homework
Ex. 2, Ex. 6, Ex. 12, Ex. 18, Ex. 20, Ex. 26
59
School of Software
4.4 Properties of Relations
 Reflexive & Irreflexive
A relation R on a set A is reflexive if (a, a)∈ R for all a
∈A. A relation R on a set A is irreflexive if a R a for all
a∈ A
 1 * ... * 


*
1
...
*



... 


 * ... 1 
 0 * ... * 


*
0
...
*



... 


 * ... 0 
Dom(R)
=Ran(R)
=A
reflexive
irreflexive
60
School of Software
4.4 Properties of Relations
 Example 1
(a) Δ = {(a,a) | a ∈A}
Reflexive
(b) R = {(a,b)∈A×A | a ≠ b}
Irreflexive
(c) A={1,2,3} and R={(1,1), (1,2)}
not Reflexive ((2,2) not in R), not Irreflexive ((1,1) in R)
(d) A is a nonempty set. R=Ø ⊆ A×A, the empty
relation.
not Reflexive, Irreflexive
61
School of Software
4.4 Properties of Relations
 Symmetric, Asymmetric & Antisymmetric
Symmetric:
if a R b, then b R a
Asymmetric:
if a R b, then b R a (must be irreflexive)
Antisymmetric: if a ≠ b, a R b or b R a
( if a R b and b R a, then a=b )
not Symmetric: some a and b with a R b, but b R a
not Asymmetric: some a and b with a R b, but b R a
not Antisymmetric :
some a and b with a ≠ b, but both a R b and b R a
62
School of Software
4.4 Properties of Relations
 Example 2
Let A=Z, the set of integers, and let
R={ (a,b) ∈A×A | a < b }
Is R symmetric, asymmetric, or antisymmetric?
Solution:
not symmetric: if a<b then b<a is not true.
asymmetric: if a<b, then b<a must be true
Antisymmetric: if a ≠ b, then a<b or b<a must be true
63
School of Software
4.4 Properties of Relations
 Example 4
Let A={1,2,3,4} and let
R={(1,2), (2,2), (3,4), (4,1)}
R is not symmetric, since (1,2) ∈ R, but (2,1) ∈ R.
R is not asymmetric, since (2,2) ∈ R
R is antisymmeric, since if a ≠ b, either (a, b) ∈ R or
(b,a) ∈ R
64
School of Software
4.4 Properties of Relations
 Example 5
Let A=Z+, the set of positive integers, and let
R={(a,b) ∈A×A | a divides b }
Is R symmetric, asymmetric, or antisymmetric?
Solution:
not symmetric: 3 | 9 but 9 | 3
not asymmetric: 2 | 2
antisymmetric: if a | b and b | a, then a=b (Section 1.4)
65
School of Software
4.4 Properties of Relations
 The properties of symmetric matrix
The matrix MR=[mij] of a symmetric relation on a finite
set A= {a1,a2, …,an} satisfies the following property
if mij=1 (ai R aj) then mji=1 (aj R ai)
Moreover,
if mij=0 (ai R aj) then mji=0 (ai R aj)
Therefore, we have
MR  M
T
R
66
School of Software
4.4 Properties of Relations
 The properties of asymmetric matrix
The matrix MR=[mij] of an asymmetric relation on a
finite set A= {a1,a2, …,an} satisfies the following
property :
if mij=1 (ai R aj) then mji=0 (aj R ai)
Moreover,
if mii=0 (ai R ai) for all i=1,2, …,n
67
School of Software
4.4 Properties of Relations
 The properties of antisymmetric matrix
The matrix MR=[mij] of an antisymmetric relation on a
finite set A= {a1,a2, …,an} satisfies the following
property
if i ≠ j, ( ai ≠ aj )
then mij=0 (ai R aj ) or mji=0 (aj R ai )
68
School of Software
4.4 Properties of Relations
 Example 6
1 0 1


0 0 1
1 1 1 


Symmetric
Not Asymmetic
Not Antisymmetric
1 1 1 0 


1
1
0
0


1 0 1 1 


 0 0 1 1
Symmetric
Not Asymmetic
Not Antisymmetric
69
1 1 1 


0 1 0
0 0 0


Not Symmetric
Not Asymmetic
Antisymmetric
School of Software
4.4 Properties of Relations
 Example 6
0 0 1 1


0
0
1
0


0 0 0 1


1 0 0 0 
Not Symmetric
Not Asymmetic
Not Antisymmetric
1 0 0 1 


0
1
1
1


0 0 1 0


0 0 0 1
0

0
0

0
Not Symmetric
Not Asymmetic
Antisymmetric
Not Symmetric
Asymmetic
Antisymmetric
70
1
0
0
0
1
1
0
0
1

0
1

0
School of Software
4.4 Properties of Relations
 The digraphs of asymmetric relation
The digraphs cannot simultaneously have an edge from
vertex i to vertex j and an edge from vertex j to vertex i.
This is true even i equals j (no cycles). All edges are
“one-way streets”
 The digraphs of antisymmetric relation
For different vertices i and j, there cannot be an edge
from vertex i to vertex j and edge from vertex j to
vertex i. When i=j, no condition is imposed. Thus there
may be cycles of length 1, but again all edges are “one
way”.
71
School of Software
4.4 Properties of Relations
 The digraphs of symmetric relation
If there is an edge from vertex i to vertex j, then there is
an edge from vertex j to vertex i. Thus, if two vertex are
connected by an edge, they must always be connected
in both directions.
We keep the vertices as they appear in the digraph, but
if two vertices a and b are connected by edges in each
direction, we replace these two edges with one
undirected edge, “two-way street ” (Graph of the
symmetric relation)
72
School of Software
4.4 Properties of Relations
 Example 7
Let A={a,b,c,d,e} and R be the symmetric relation
given by R={(a,b), (b,a), (a,c), (c,a), (b,c), (c,b), (b,e),
(e,b), (e,d), (d,e), (c,d), (d,c)}
Undirected edge
Adjacent vertices
digraphs
graphs
73
School of Software
4.4 Properties of Relations
 Transitive Relations
A relation R on a set A is transitive if whenever a R b
and b R c, then a R c.
For instance: (Ex. 8)
the relation “<” on the set of integers
If a < b and b < c, then we have a < c
Note: not Transitive if there exist a, b and c in A so that a
R b and b R c, but a R c.
74
School of Software
4.4 Properties of Relations
 Example 9
Let A=Z+, and R={(a,b) ∈A×A | a divides b }
Is R transitive?
Solution:
Suppose that a R b and b R c, so that a | b and b | c. It
then does follow that a | c (See Theorem 2 of Section
1.4). Thus R is transitive.
75
School of Software
4.4 Properties of Relations
 Example 10
Let A ={1,2,3,4} and let R={(1,2),(1,3),(4,2)}
Is R transitive?
Solution:
Since there are no elements a, b and c in A such that a R
b and b R c, but a R c, R is transitive.
76
School of Software
4.4 Properties of Relations
 A sufficient condition of transitive
A relation R is transitive if and only if its matrix
MR=[mij] has the property
if mij=1 and mjk=1, then mik=1.
the left-hand side means that (M R )2 has a 1 in position
(i,k). Thus the transitivity of R means that if (M R )2 has
a 1 in any position, then MR must have a 1 in the same
position.
Thus, in particular, if (M R )2  M R , then R is transitive.
77
School of Software
4.4 Properties of Relations
 Example 11
Let A={1,2,3} and let R be the relation on A whose
matrix is
111 

MR  
001



 001

Show that R is transitive.
Solution:
By direct computation, (M R )2  M R ,
transitive.
78
therefore, R is
School of Software
4.4 Properties of Relations
 If R is transitive, (M R )2 may not equal to MR
(Verify the Example 10)
MR
MR
 0110 
 0000 


 0000 


 0100 
 0110   0000 
 0000   0000 


  MR
 0000   0000 

 

 0100   0000 
79
School of Software
4.4 Properties of Relations
 If R is a transitive relation, and a R b and b R c mean
that there is a path of length 2 in R from a to c, namely
a R2 c. Therefore, we may rephrase the definition of
transitivity as follows:
If a R2 c, then a R c, that is R2 ⊆ R
80
School of Software
4.4 Properties of Relations
 Theorem 1
A relation R is transitive if and only if it satisfies the
following property:
If there is a path of length greater than 1 from vertex a
to vertex b, there is a path of length 1 from a to b (that
is, a is related to b).
Algebraically stated, R is transitive if and only if Rn ⊆
R for all n >=1
How to Proof the Theorem?
81
School of Software
4.4 Properties of Relations
 Theorem 2
Let R be a relation on a set A, then
(a) Reflexivity of R means that a∈R(a) for all a in A.
(b) Symmetry of R means that a∈R(b) iff b∈R(a).
(c) Transitivity of R means that if b∈R(a) and c∈R(b),
then c∈R(a).
82
School of Software
4.4 Properties of Relations
 Homework
Ex. 2, Ex. 14, Ex. 18, Ex. 26, Ex. 32, Ex. 38
83
School of Software
4.5 Equivalence Relations
 Equivalence Relation
A relation R on a set A is called an equivalence relation
if it is reflexive, symmetric, and transitive.
Example 1:
Let A be the set of all triangles in the plane, and let R
be the relation on A defined as follow
R= { (a, b) ∈A×A | a is congruent to b}
Therefore, R is an equivalence relation.
Q: R= { (a, b) ∈A×A | a is similar to b} ?
84
School of Software
4.5 Equivalence Relations
 Example 2
Let A={1,2,3,4} and let
R={(1,1),(1,2), (2,1), (2,2), (3,4), (4,3), (3,3), (4,4)}
R is an equivalence relation.
MR
1100 
1100 


 0011


 0011
85
School of Software
4.5 Equivalence Relations
 Example 3
Let A=Z, the set of integers, and let R be defined by a R
b if and only if a<=b, is R an equivalence relation?
Reflexive? Yes, since a<=a for all a in A
Symmetric? No, (1, 2) in R but (2,1) is not in R
Transitive?
Yes, since a<=b and b<=c, then we have a<=c
Therefore, R is not an equivalence relation.
86
School of Software
4.5 Equivalence Relations
 Example 4
Let A=Z and let R= { (a, b) ∈A×A | a and b yield the
same remainder when divided by 2}.
In this case, we call 2 the modulus and write
a  b(mod 2) read “a is congruent to b mod 2”
Reflexive: a  a(mod 2)
Symmetric: if a  b(mod 2) , then a and b yield the
same remainder when divided by 2, thus b  a(mod 2)
Transitive: if a  b(mod 2) & b  c(mod 2) , then a, b
and c yield the same remainder when divided by 2.
Thus a  c(mod 2)
87
School of Software
4.5 Equivalence Relations
 Theorem 1
Let P be a partition on a set A. Recall that the sets in P are
called the blocks of P. Define the relation A as follows:
a R b if and only if a and b are member of the same block
Then R is an equivalence relation on A . Proof:
(a) If a in A, then a is in the same block as itself, so a R a
(b) If a R b, then a and b are in the same block, so b R a
(c) If a R b, and b R c, then a , b and c must all lie in the
same block of P, thus a R c.
Note: If P is a partition of a set A, then P can be used to
construct an equivalence on A
88
School of Software
4.5 Equivalence Relations
 Example 6
Let A={1,2,3,4} and consider the partition
P={{1,2,3},{4}} of A. Find the equivalence relation R
on A determined by P.
Solution:
Based on the Theorem 1, each element in a block is
related to every other element in the same block and
only to those elements. Thus,
R={(1,1), (1,2), (1,3), (2,1), (2,2), (2,3), (3,1), (3,2), (3,3), (4,4)}
89
School of Software
4.5 Equivalence Relations
 P is a partition of A and R is the equivalence relation
determined by P
A1
If a in Ai, i=1,2…6
then Ai=R(a)
A2
A3
Lemma 1*:
a R b iff R(a)=R(b)
A4
A6
A5
90
School of Software
4.5 Equivalence Relations
 Lemma 1
(Note: Lemma is a theorem whose main purpose
is to aid in proving some other theorem)
Let R be an equivalence relation on a set A, and let a ∈
A , b ∈ A. Then
a R b if and only if R(a)=R(b)
Proof:
First, suppose that R(a)=R(b).
Since R is reflexive, b ∈ R(b);
therefore, b in R(a), so a R b
91
School of Software
4.5 Equivalence Relations
Conversely, suppose that a R b, then
(1) b in R(a) by definition. Therefore, since R is symmetric
(2) a in R(b) by theorem 2 (b) of Section 4.4.
To prove R(a)=R(b)
First, we choose x in R(b), since R is transitive, the fact x in
R(b) with b in R(a) (1) , implies by Theorem 2(c) of Section
4.4 that x in R(a). Thus, R(b) ⊆ R(a)
Second, support y in R(a). This fact and a in R(b) (2) imply.
As before, that y in R(b). Thus R(a) ⊆ R(b)
Therefore, we have R(a) = R(b)
The Lemma proved.
92
School of Software
4.5 Equivalence Relations
 Theorem 2
Let R be an equivalence relation on A, and let P be the
collection of all distinct relative sets R(a) for a in A. Then P
is a partition of A, and R is the equivalence relation
determined by P.
Proof:
According to the definition of a partition, we should show
the two following properties
(a) Every element of A belongs to some relative set
(b) If R(a) and R(b) are not identical, then R(a) ∩ R(b) = ф
93
School of Software
4.5 Equivalence Relations
 Property (a) is true, since a ∈ R(a) (reflexivity)
 Property (b) is equivalent to the following statement
If R(a) ∩ R(b) ≠ ф, then R(a) = R(b) (p59 Theorem 2 b)
Assume c∈ R(a) ∩ R(b), then a R c, b R c.
then we have c R b (symmetric)
a R c, c R b  a R b (transitivity)
Therefore, R(a) = R(b) (by lemma 1)
94
School of Software
4.5 Equivalence Relations
 Equivalence classes
If R is an equivalence relation on A, then the sets R(a)
(or [a]) are traditionally called equivalence classes of R.
The partition P constructed in Theorem 2 consists of all
equivalence classes of R, and this partition will be
denoted by A/R. (the quotients set of A)
95
School of Software
4.5 Equivalence Relations
 Example 7
Let A={1,2,3,4} and let
R={(1,1),(1,2), (2,1), (2,2), (3,4), (4,3), (3,3), (4,4)}
Determine A/R
Solution:
R(1) = {1, 2} = R(2) ,
R(3) = {3, 4} = R(4)
Hence
A/R = { {1,2} , {3,4}}
96
School of Software
4.5 Equivalence Relations
 Example 8
Let A=Z and let R= { (a, b) ∈A×A | a and b yield the
same remainder when divided by 2}.
Solution:
First R(0)={…,-4,-2,0,+2,+4,…}, the set of even
integers, since the remainder is zero when each of these
numbers is divided by 2.
R(1) = {…, -3,-1,0,+1,+3,… }, the set of odd integers,
since the remainder is 1 when divided by 2. Hence,
A/R consists of the even integers and the set of odd
integers.
97
School of Software
4.5 Equivalence Relations
 The procedure of determining A/R is as follows:
Support A={a1,a2,…} (finite or countable)
Step 1: i=0; j=0;
Step 2: i=i+1;
Step 3: if ai ∈ A, then j=j+1; bj=ai
and compute the equivalence class R(bj ), A= A- R(bj)
Step 4: if A become an empty set, then we obtain the
equivalence classes R(b1), R(b2), … R(bj) (Note: j may be
finite or infinite)
otherwise repeat step 2 ~ 4
98
School of Software
4.5 Equivalence Relations
 Homework
Ex. 4, Ex. 12, Ex. 14, Ex. 20, Ex. 23, Ex. 28
99
School of Software
4.7 Operations on Relations
 Let R and S be relations from a set A to a set B
R and S are subsets of A×B. We can use set
operations on the relations R and S
Relations: (three representations)
1. the set of ordered pairs (finite or infinite)
2. digraph (finite)
3. matrix (finite)
100
School of Software
4.7 Operations on Relations
 Complementary relation

a R b if and only if a R b
 The intersection R ∩ S
a (R ∩ S) b means that a R b and a S b
 The union R U S
a (R U S) b means that a R b or a S b
101
School of Software
4.7 Operations on Relations
 Inverse
Let R be a relation from A to B, the relation R-1 is a
relation from B to A (reverse order from R) denoted by
b R-1 a if and only if a R b
Note: (R-1) -1=R
Dom(R-1) = Ran (R)
Ran(R-1) = Dom(R)
102
School of Software
4.7 Operations on Relations
 Example 1
Let A={1,2,3,4} and B= {a,b,c}. We Let
R={(1,a), (1,b), (2,b),(2,c),(3,b),(4,a)}
and S= {(1,b),(2,c),(3,b),(4,b)}

Compute: (a) R (b) R ∩ S (c) R U S (d) R-1
Solution:
A×B = { (1,a), (1,b), (1,c), (2,a), (2,b), (2,c), (3,a), (3,b),
(3,c), (4,a), (4,b), (4,c) }

R  A B  R
 { (1,c), (2,a), (3,a), (3,c), (4,b), (4,c) }
103
School of Software
4.7 Operations on Relations
 Example 1
R∩S = { (1,b), (3,b), (2,c) }
R∪S = { (1,a), (1,b), (2,b), (2,c), (3,b), (4,a), (4,b) }
R-1= { (a,1), (b,1), (b,2), (c,2), (b,3), (a,4) }
104
School of Software
4.7 Operations on Relations
 Example 2
Let A=R. Let R be the relation ≤ on A and let S be ≥
Then the complement of R is the relation >, since a ≤ b
means a>b
Similarly, the complement of S is the relation <.
On the other hand, R-1=S, since for any number a and b
a R-1 b if and only if b R a if and only if b ≤ a
if and only if a ≥ b if and only if a S b
R ∩ S : “=”
R U S : A×A
105
School of Software
4.7 Operations on Relations
 Example 3
R={ (a,a), (b,b), (a,c), (b,a), (c,b), (c,d), (c,e), (c,a),
(b,d), (d,a), (d,e), (e,b), (e,a), (e,d), (e,c) }
R-1= { (b,a), (e,b), (c,c), (c,d), (d,d), (d,b), (c,b), (d,a),
(e,e), (e,a) }
R∩S = {(a b), (b e) (c c) }
106
School of Software
4.7 Operations on Relations
 Example 4
101 
 011 
M R   011  M S  110 
 000 
 010 
 001 
 010 
100 
 010 




M

M _  100  M R 1   010 
R S


R
 000 
111 
110 
107
M R S
111 
 111 
 010 
School of Software
4.7 Operations on Relations
 Operations on Boolean Matrix
M RS  M R  M S
M RS  M R  M S
M R1  ( M R )
T
_
M_  MR
R
108
School of Software
4.7 Operations on Relations
 Theorem 1
Suppose that R and S are relations from A to B
(a) If R ⊆ S, then R-1 ⊆ S-1.
(b) If R ⊆ S, then S ⊆ R
(c) (R∩S) -1 =R -1 ∩ S -1 and (R U S) -1 = R -1 U S -1
(d) (R∩S) = R U S
and R U S = R ∩ S
109
School of Software
4.7 Operations on Relations
 Theorem 2
Let R and S be relations on a set A
(a) If R is reflexive, so is R-1
(b) If R and S are reflexive, then so are R∩S and R U S
(c) R is reflexive if and only if R is irrflexive
110
School of Software
4.7 Operations on Relations
 Example 5
Let A={1,2,3} and consider the two reflexive relations
R = { (1,1), (1,2), (1,3), (2,2), (3,3) }
S = { (1,1), (1,2), (2,2), (3,2), (3,3) }
then R-1 = { (1,1), (2,1), (3,1), (2,2), (3,3) }. R and R-1
are both reflexive.
R= { (2,1), (2,3), (3,1), (3,2) } is irreflexive while R is
reflexive.
R∩S = {(1,1), (1,2), (2,2), (3,3) } (reflexive)
R∪S = { (1,1), (1,2), (1,3), (2,2), (3,2), (3,3) } (reflexive)
111
School of Software
4.7 Operations on Relations
 Theorem 3 (Homework Ex. 37)
Let R be a relation on a set A. Then
(a) R is symmetric if and only if R=R-1
(b) R is antisymmetric if and only if R ∩ R -1 ⊆ Δ
(c) R is asymmetric if and only if R ∩ R -1 = Ø
112
School of Software
4.7 Operations on Relations
 Theorem 4
Let R and S be relations on A
(a) If R is symmetric, so are R-1 and R
(b) If R and S are symmetric, So R ∩ S and R U S
113
School of Software
4.7 Operations on Relations
 Example 6
Let A={1,2,3} and consider the symmetric relations
R = { (1,1), (1,2), (2,1), (1,3), (3,1) }
S = { (1,1), (1,2), (2,1), (2,2), (3,3) }
then
(a) R-1 = { (1,1), (2,1), (1,2), (3,1), (1,3) } (symmetric)
R = { (2,2), (2,3), (3,2), (3,3) } (symmetric)
(b) R∩S = { (1,1), (1,2), (2,1) } (symmetric)
R∪S = { (1,1), (1,2), (1,3), (2,1), (2,2), (3,1), (3,3) }
(symmetric)
114
School of Software
4.7 Operations on Relations
 Theorem 5
Let R and S be relations on A
(a) (R ∩ S )2 ⊆ R2 ∩ S2
(b) If R and S are transitive, so is R ∩ S
(c) If R and S are equivalence relations, so is R ∩ S
115
School of Software
4.7 Operations on Relations
 Closure
If R is a relation on a set A, and R lacks some relational
properties (e.g. reflexivity, symmetry and transitivity) .
We try to add as few new pairs as possible to R, so that
the resulting relation R1 has the property we desired.
If R1 exists, we call it the closure of R with the respect
to the property in question.
116
School of Software
4.7 Operations on Relations
 Example 8
Support that R is a relation on A, and R is not reflexive.
R1=R∪Δ is the smallest reflexive relation on A containing
R. The reflexive closure of R is R∪Δ.
 Example 9
Support that R is a relation on A, and R is not symmetric.
R1 =R∪R-1 is the smallest symmetric relation containing R.
(Note: (R∪R-1)-1 = R-1∪R )
117
School of Software
4.7 Operations on Relations
 The symmetric closure of R
R= {(a,b), (b,c), (a,c), (c,d)}
R∪R-1= {(a,b), (b,a) ,(b,c), (c,b), (a,c), (c,a), (c,d), (d,c)}
118
School of Software
4.7 Operations on Relations
 Composition
Support A, B and C are sets, R is a relation from A to
B, and S is a relation from B to C, the composition of R
and S, written SoR is a relation from A to C
If a is in A and c is in C, then a SoR c if and only if for
some b in B, we have a R b, and b S c.
R
A
S
B
C
SoR
119
School of Software
4.7 Operations on Relations
 Example 10
Let A= {1,2,3,4} and R={(1,2), (1,1), (1,3), (2,4),(3,2)}
and S={(1,4), (1,3), (2,3),(3,1), (4,1)}.
Since (1,2) in R, (2,3) in S, we must have (1,3) in SoR
Similarly, we have
SoR = {(1,4), (1,3), (1,1),(2,1), (3,3)}
120
School of Software
4.7 Operations on Relations
 Theorem 6
Let R be a relation from A to B and let S be a relation
from B to C. Then if A1 is any subset of A, we have
(SoR)(A1)= S(R(A1) )
Proof: by the definition of composition and relative set
121
School of Software
4.7 Operations on Relations
 Theorem 7
Let A, B, C and D be sets, R a relation from A to B, S a
relation from B to C and T a relation from C to D. Then
T o (S o R) = (T o S) o R
Proof: by the definition of composition
122
School of Software
4.7 Operations on Relations
 Example 13
Let A={a, b}, R={(a, a), (b, a), (b,b)} and S={ (a, b),
(b, a), (b, b)}. Then
So R={(a,b), (b,a), (b,b)}
Ro S={(a,a), (a,b), (b,a),(b,b)}
In general, So R does not equal to Ro S
123
School of Software
4.7 Operations on Relations
 Theorem 8
Let A, B and C be sets, R a relation from A to B, and S
a relation from B to C. Then
(S o R) -1= R-1 o S-1
Proof: by the definition of composition and inverse
124
School of Software
4.7 Operations on Relations
 Homework
Ex. 5, Ex. 12, Ex. 16, Ex. 22 Ex. 25, Ex. 29, Ex. 37
125
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Theorem 1
Let R be a relation on a set A. Then R ∞ is the transitive
closure of R
Proof
(a)R ∞ is transitive. a R ∞ b , b R ∞ c, then a R ∞ c
(b)R ∞ is the smallest transitive relation containing R.
Support S is any transitive relation on A, and R ⊆ S ,
S is transitive  Sn ⊆ S, for all n (P.146 Theorem 1)
then S ∞ =S1 U S2… ⊆ S
R ∞ ⊆ S ∞ ⊆ S (why?)
This means that R ∞ is the smallest one.
126
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Example 1
Let A={1,2,3,4} and let R ={(1,2), (2,3), (3,4), (2,1)}. Find
the transitive closure of R
Method # 1:
1  1, 2, 3, 4
2  1, 2, 3, 4
3 4
4 ф
R ∞= {(1,1) , (1,2), (1,3), (1,4), (2,1) , (2,2), (2,3), (2,4), (3,4)}
127
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
1010 
 Example 1
1010 
3

Method # 2
(M R )  
0000 


0100 
1010 
0000 

1010 
0101 
1010


2




MR 
(M R ) 
0101 
0001 
0000 
4



(
M
)



R
0000 
0000 
0000 


1111 
0000 


M R
1111 

 M R  (M R )  (M R ) 
0001 


0000 
2
3
128
Note : (M R )2  (M R )4
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Theorem 2
Let A be a set with |A|=n, and let R be a relation on A. Then
R  R  R2  ...Rn
Proof:
If x0 R ∞ xm, then there is a path from x0 to xm with length m
x0, x1, x2, …, xm
case #1: if 1≤ m≤ n, then x0 Rm xm
129
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Theorem 2
case #2: if m>n, there must have some vertex appearing
two times in the path (why?), saying xi = xj, where i<j
x0, x1, …, xi, …, xj, xj+1 … xm
there must exists another path from x0 to xm with length less
than m (why?),
x0, x1, …, xi, xj+1 … xm
Therefore, if x0 R ∞ xm , we can find the shortest path from
x0 to xm with length less than or equal to n (why?) , namely
x0 R k xm 1≤ k≤ n
Therefore, the theorem has been proofed.
130
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Let R be a relation on a set A={a1,a2,…,an}.
Interior vertices
If x1,x2,…xm is a path in R, then any vertices other than x1
and xm are called interior vertices of the path.
Boolean matrix Wk (n matrices with size n-by-n)
For 1 ≤ k ≤n, Wk has a 1 in position i , j if and only if there
is a path from ai to aj in R whose interior vertices, if any,
come from the set {a1,a2, …, ak}
Wn=M R ∞
Since any vertex must come from the set {a1,a2,…,an}, it
follows that the matrix Wn has a 1 in position i , j if some
path in R connects ai with aj
131
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 How to compute Wk k=1,2,…n
Let W0=MR
Support Wk-1=[sij] is known, let Wk=[tij] then
if tij =1, then there must be a path from ai to aj whose
interior vertices come from the set {a1,a2, …,ak}.
Case #1: If ak is not an interior vertex of this path, then all interior
vertices must actually come from the set {a1,a2,…ak-1}, so sij=1.
Case #2: If ak is an interior vertex of this path, then sik=1 and
skj=1 (refer to the proof of Theorem 2)
P1
ai
ak
P2
aj
P1, P2 are subpaths with all interior
vertices from {a1,a2, …, ak-1}
132
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Warshall’s Algorithm
Warshall’s Algorithm is a more efficient algorithm for
computing transitive closure, the procedure of the algorithm
is as follows:
Step1: First transfer Wk all 1’s in Wk-1
Step 2: List the locations p1,p2,… in column k of Wk-1,
where the entry is 1 and the locations q1,q2,… in row k of
Wk-1, where the entry is 1
Step 3: Put 1’s in all the positions pi, qj of Wk
133
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Example 2
Consider the relation R defined in Example 1. Then
0100 
 0100 
1110 
1010 
1110 
1110 
 W1  
 W2  

W0  M R  
0001 
 0001 
0001






0000 
 0000 
0000
pi:
qj:
2
2
pi: 1, 2
qj: 1, 2, 3
pi:
qj:
1, 2
4
1111 
1111 

W3  
 0001 


 0000 
pi: 1, 2
qj: ф
M R  W4  W3
134
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Algorithm WARSHALL
1. CLOSURE  MAT
2. FOR K=1 THRU N
a. FOR I=1 THRU N
1. FOR J=1 THRU N
a. CLOSURE[I,J]  CLOSURE[I,J]
V (CLOSURE[I,K] ^ CLOSURE[K,J] )
Warshall’s Algorithm:
O(N3)
Boolean Matrix operations: O(N4)
135
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Theorem 3
If R and S are equivalence relations on a set A, then the
smallest equivalence relation containing both R and S is
(R U S) ∞
Proof:
1. Reflexive. △ ⊆ R, △ ⊆ S, △ ⊆ R U S ⊆ (R U S) ∞
2. Symmetric. R=R-1, S=S-1, (RUS)-1=R-1 U S-1=R U S
all paths in RUS are two-way streets, and it follows from
the definitions that (R U S) ∞ must be symmetric (why?)
3. Transitive. (R U S) ∞ is the transitive closure of R U S
(Theorem 1). (the smallest)
136
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Example 3
Let A={1,2,3,4,5}, R={(1,1), (1,2), (2,1), (2,2), (3,3),
(3,4), (4,3), (4,4), (5,5)}, and S= { (1,1), (2,2), (3,3),
(4,4), (4,5), (5,4), (5,5) }.
Find the smallest equivalence relation containing R and S,
and compute the partition of A that it produces
S
R
12 34 5
12 34 5
137
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
11000 
10000 
11000 
11000 
 01000 
11000 






M R   00110  M S   00100  M R  S   00110 






 00110 
 00011 
 00110 



 00001 

 00011 

 00011 

11000  (RUS) ∞
11000 
W3  W2


 W1  W0 W5  W4   00111
12 34 5


00111



 00111

(R∪S)∞ = { (1,1), (1,2), (2,1), (2,2), (3,3), (3,4), (3,5), (4,3), (4,4), (4,5), (5,3), (5,4), (5,5 }
138
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
 Homework
Ex. 1, Ex. 2, Ex. 7, Ex. 13, Ex. 18, Ex. 25
139
School of Software