chapter 4 relations and digraphs weiqi luo ( 骆伟祺 ) school of software sun yat-sen university...
TRANSCRIPT
Chapter 4 Relations and Digraphs
Weiqi Luo (骆伟祺 )School of Software
Sun Yat-Sen UniversityEmail : [email protected] Office : A309
School of Software
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
Chapter Four: Relations and Digraphs
School of Software
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}
4.1. Product Sets and Partitions
3
School of Software
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.1. Product Sets and Partitions
4
School of Software
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
4.1. Product Sets and Partitions
5
School of Software
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
4.1. Product Sets and Partitions
6
O X
Y
(a, b)
School of Software
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
4.1. Product Sets and Partitions
7
School of Software
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.
4.1. Product Sets and Partitions
8
School of Software
Employees
Select D[ai1=required1, ai2=required2,…, aik=requiredk]
4.1. Product Sets and Partitions
9
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
…
…
3465 Harris Sales 4
School of Software
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 = { }
A
B={A1, A2, A3, A4, A5, A6}
4.1. Product Sets and Partitions
10
A1 A2
A3
A4
A5
A6
Blocks or cells
School of Software
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
4.1. Product Sets and Partitions
11
School of Software
Homework
Ex. 2, Ex. 10, Ex. 17, Ex. 32, Ex. 36, Ex. 37
4.1. Product Sets and Partitions
12
School of Software
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.
4.2 Relations and Digraphs
13
School of Software
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
4.2 Relations and Digraphs
14
Y
O X
School of Software
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 .
4.2 Relations and Digraphs
15
School of Software
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)
4.2 Relations and Digraphs
16
Small-world network http://en.wikipedia.org/wiki/Small-world_network
School of Software
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
4.2 Relations and Digraphs
17
O
X
Y(0,3)
(2,0)
School of Software
Example 9
Ci R Cj if and only if the cost of going from Ci to Cj defined and less than or equal to $180.
4.2 Relations and Digraphs
18
C1 C2 C3 C4 C5
C1 140 100 150 200
C2 190 200 160 220
C3 110 180 190 250
C4 190 200 120 150
C5 200 100 200 150
School of Software
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 )
4.2 Relations and Digraphs
19
School of Software
Example 12
Let R be the relation of Example 6
then
Dom(R) = [-2, +2] Ran(R) = [-3, +3]
4.2 Relations and Digraphs
20
O
X
Y(0,3)
(2,0)
School of Software
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 A∈ 1}
4.2 Relations and Digraphs
21
School of Software
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}
4.2 Relations and Digraphs
22
School of Software
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}
4.2 Relations and Digraphs
23
O
X
Y(0,3)
(2,0)
School of Software
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)
4.2 Relations and Digraphs
24
School of Software
(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)
4.2 Relations and Digraphs
25
School of Software
(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)
4.2 Relations and Digraphs
26
School of Software
(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)
4.2 Relations and Digraphs
27
School of Software
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 )
4.2 Relations and Digraphs
28
School of Software
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.
4.2 Relations and Digraphs
29
School of Software
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.
4.2 Relations and Digraphs
30
School of Software
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
4.2 Relations and Digraphs
31
School of Software
Consider the matrix
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}
4.2 Relations and Digraphs
32
1 0 0 1
0 1 1 0
1 0 1 0
M
School of Software
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.
4.2 Relations and Digraphs
33
School of Software
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)}
4.2 Relations and Digraphs
34
School of Software
Example 20 Find the relation determined by the following Figure.
R={ (1,1), (1,3), (2,3), (3,3), (3,2), (4,3) }
4.2 Relations and Digraphs
35
School of Software
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
4.2 Relations and Digraphs
36
School of Software
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 out-degrees of all vertices
4.2 Relations and Digraphs
37
1 0 0 0
0 1 0 0
1 1 1 0
0 1 0 1
RM
a b c d
2 3 1 1
1 1 3 2
In-degree
Out-degree
School of Software
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)}
4.2 Relations and Digraphs
38
School of Software
Homework
Ex. 2, Ex. 8, Ex.12, Ex.20
Ex.24, Ex. 28, Ex. 32
4.2 Relations and Digraphs
39
School of Software
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.
4.3 Paths in Relations and Digraphs
40
School of Software
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 )
4.3 Paths in Relations and Digraphs
41
School of Software
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
4.3 Paths in Relations and Digraphs
42
School of Software
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∞
4.3 Paths in Relations and Digraphs
43
School of Software
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
4.3 Paths in Relations and Digraphs
44
School of Software
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 ∞
4.3 Paths in Relations and Digraphs
45
R
School of Software
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)}
4.3 Paths in Relations and Digraphs
46
School of Software
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
4.3 Paths in Relations and Digraphs
47
R
R∞ = { (a,a), (a,b), (a,c), (a,d), (a,e), (b,c),(b,d), (b,e), (c,d), (c,e), (d,e) }.
Note: If |R| is large, it can be tedious and perhaps difficult to compute R ∞
School of Software
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
4.3 Paths in Relations and Digraphs
48
School of Software
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
4.3 Paths in Relations and Digraphs
49
1 1 1 , 1
0
ik kj
ij
if a and b for some k k pc
otherwise
School of Software
Example
Let
then
4.3 Paths in Relations and Digraphs
50
1 1 0
0 1 0
1 1 0
0 0 1
A
1 0 0 0
0 1 1 0
1 0 1 1
B
1 1 0 1 1 1 01 0 0 0
0 1 0 0 1 1 00 1 1 0
1 1 0 1 1 1 01 0 1 1
0 0 1 1 0 1 1
A B
School of Software
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)⊙ ⊙ ⊙ ⊙
4.3 Paths in Relations and Digraphs
51
School of Software
Theorem 1 If R is a relation on A={a1,a2,…an}, then
Proof:
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 m ik=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 M⊙ R is 1 if and only if nij=1.
4.3 Paths in Relations and Digraphs
52
2 R RRM M M
School of Software
Example 6 Let A and R be as in Example 5. Then
4.3 Paths in Relations and Digraphs
53
1 1 0 0 0
0 0 1 0 0
0 0 0 1 1
0 0 0 0 1
0 0 0 0 0
RM
1 1 0 0 0 1 1 0 0 0 1 1 1 0 0
0 0 1 0 0 0 0 1 0 0 0 0 0 1 1
0 0 0 1 1 0 0 0 1 1 0 0 0 0 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 0 0 0 0 0
R RM M
School of Software
Theorem 2 For n ≥ 2 and R is a relation on a finite set A, we have
Proof by Mathematical Induction
(refer to p.138-139 for more details)
4.3 Paths in Relations and Digraphs
54
... ( )n R R R RRM M M M M n factors
School of Software
4.3 Paths in Relations and Digraphs
55
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
2 3 n1
R =R R R .. Rn
2 3RR R R
2 3R R R
M =M ...
M (M ) (M ) ...
M M
School of Software
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
4.3 Paths in Relations and Digraphs
56
* nR R
2 3n R R R
M = I M
I M (M ) (M ) ...
School of Software
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
4.3 Paths in Relations and Digraphs
57
School of Software
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
4.3 Paths in Relations and Digraphs
58
School of Software
Homework
Ex. 2, Ex. 6, Ex. 12, Ex. 18, Ex. 20, Ex. 26
4.3 Paths in Relations and Digraphs
59
School of Software
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
4.4 Properties of Relations
60
1 * ... *
* 1 ... *
...
* ... 1
reflexive
0 * ... *
* 0 ... *
...
* ... 0
irreflexive
Dom(R)=Ran(R)=A
School of Software
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
4.4 Properties of Relations
61
School of Software
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
4.4 Properties of Relations
62
School of Software
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
4.4 Properties of Relations
63
School of Software
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
4.4 Properties of Relations
64
School of Software
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)
4.4 Properties of Relations
65
School of Software
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
4.4 Properties of Relations
66
TR RM M
School of Software
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
4.4 Properties of Relations
67
School of Software
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 )
4.4 Properties of Relations
68
School of Software
Example 6
4.4 Properties of Relations
69
1 0 1
0 0 1
1 1 1
1 1 1 0
1 1 0 0
1 0 1 1
0 0 1 1
1 1 1
0 1 0
0 0 0
SymmetricNot AsymmeticNot Antisymmetric
SymmetricNot AsymmeticNot Antisymmetric
Not SymmetricNot AsymmeticAntisymmetric
School of Software
Example 6
4.4 Properties of Relations
70
0 0 1 1
0 0 1 0
0 0 0 1
1 0 0 0
1 0 0 1
0 1 1 1
0 0 1 0
0 0 0 1
0 1 1 1
0 0 1 0
0 0 0 1
0 0 0 0
Not SymmetricNot AsymmeticNot Antisymmetric
Not SymmetricNot AsymmeticAntisymmetric
Not SymmetricAsymmeticAntisymmetric
School of Software
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”.
4.4 Properties of Relations
71
School of Software
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)
4.4 Properties of Relations
72
School of Software
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)}
4.4 Properties of Relations
73
digraphs graphs
Undirected edge
Adjacent vertices
School of Software
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.
4.4 Properties of Relations
74
School of Software
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.
4.4 Properties of Relations
75
School of Software
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.
4.4 Properties of Relations
76
School of Software
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 has a 1 in position (i,k). Thus the transitivity of R means that if has a 1 in any position, then MR must have a 1 in the same position.
Thus, in particular, if , then R is transitive.
4.4 Properties of Relations
77
2( )RM 2( )RM
2( )R RM M
School of Software
Example 11 Let A={1,2,3} and let R be the relation on A whose matrix is
Show that R is transitive.
Solution:
By direct computation, , therefore, R is transitive.
4.4 Properties of Relations
78
111
001
001RM
2( )R RM M
School of Software
If R is transitive, may not equal to MR (Verify the Example 10)
4.4 Properties of Relations
79
2( )RM
0110 0110 0000
0000 0000 0000
0000 0000 0000
0100 0100 0000
R R RM M M
School of Software
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
4.4 Properties of Relations
80
School of Software
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?
4.4 Properties of Relations
81
School of Software
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).∈
4.4 Properties of Relations
82
School of Software
Homework
Ex. 2, Ex. 14, Ex. 18, Ex. 26, Ex. 32, Ex. 38
4.4 Properties of Relations
83
School of Software
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} ?
4.5 Equivalence Relations
84
School of Software
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.
4.5 Equivalence Relations
85
1100
1100
0011
0011
RM
School of Software
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.
4.5 Equivalence Relations
86
School of Software
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
read “a is congruent to b mod 2”
Reflexive:
Symmetric: if , then a and b yield the same remainder when divided by 2, thus
Transitive: if , then a, b and c yield the same remainder when divided by 2. Thus
4.5 Equivalence Relations
87
(mod 2)a b(mod 2)a a
(mod 2)a b(mod 2)b a
(mod 2) & (mod 2)a b b c
(mod 2)a c
School of Software
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
4.5 Equivalence Relations
88
School of Software
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)}
4.5 Equivalence Relations
89
School of Software
P is a partition of A and R is the equivalence relation determined by P
If a in Ai, i=1,2…6
then Ai=R(a)
Lemma 1*:
a R b iff R(a)=R(b)
4.5 Equivalence Relations
90
A1 A2
A3
A4
A5
A6
School of Software
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
4.5 Equivalence Relations
91
School of Software
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.
4.5 Equivalence Relations
92
School of Software
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) = ф
4.5 Equivalence Relations
93
School of Software
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)
4.5 Equivalence Relations
94
School of Software
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)
4.5 Equivalence Relations
95
School of Software
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}}
4.5 Equivalence Relations
96
School of Software
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.
4.5 Equivalence Relations
97
School of Software
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
4.5 Equivalence Relations
98
School of Software
Homework
Ex. 4, Ex. 12, Ex. 14, Ex. 20, Ex. 23, Ex. 28
4.5 Equivalence Relations
99
School of Software
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)
4.7 Operations on Relations
100
School of Software
Complementary relation
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
4.7 Operations on Relations
101
a Rb
School of Software
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)
4.7 Operations on Relations
102
School of Software
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) (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) }
4.7 Operations on Relations
103
R
{ (1,c), (2,a), (3,a), (3,c), (4,b), (4,c) }
R A B R
School of Software
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) }
4.7 Operations on Relations
104
School of Software
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
4.7 Operations on Relations
105
School of Software
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) }
4.7 Operations on Relations
106
School of Software
Example 4
4.7 Operations on Relations
107
101
011
000RM
011
110
010SM
_
010
100
111R
M
1
100
010
110R
M
S
001
010
000RM
S
111
111
010RM
School of Software
Operations on Boolean Matrix
4.7 Operations on Relations
108
1 ( )
R S R S
R S R S
TRR
M M M
M M M
M M
_
_
RR
M M
School of Software
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
4.7 Operations on Relations
109
School of Software
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
4.7 Operations on Relations
110
School of Software
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)
4.7 Operations on Relations
111
School of Software
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 = Ø
4.7 Operations on Relations
112
School of Software
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
4.7 Operations on Relations
113
School of Software
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)
4.7 Operations on Relations
114
School of Software
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
4.7 Operations on Relations
115
School of Software
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.
4.7 Operations on Relations
116
School of Software
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 ) ∪
4.7 Operations on Relations
117
School of Software
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)}
4.7 Operations on Relations
118
School of Software
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.
4.7 Operations on Relations
119
A B CR S
SoR
School of Software
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)}
4.7 Operations on Relations
120
School of Software
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
4.7 Operations on Relations
121
School of Software
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
4.7 Operations on Relations
122
School of Software
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
4.7 Operations on Relations
123
School of Software
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
4.7 Operations on Relations
124
School of Software
Homework
Ex. 5, Ex. 12, Ex. 16, Ex. 22 Ex. 25, Ex. 29, Ex. 37
4.7 Operations on Relations
125
School of Software
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.
4.8 Transitive Closure and Warshall’s Algorithm
126
School of Software
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)}
4.8 Transitive Closure and Warshall’s Algorithm
127
School of Software
Example 1 Method # 2
4.8 Transitive Closure and Warshall’s Algorithm
128
0100
1010
0001
0000
RM
2 3
1111
1111( ) ( )
0001
0000
R R R RM M M M
2
1010
0101( )
0000
0000
RM
3
1010
1010( )
0000
0000
RM
4
1010
0101( )
0000
0000
RM
2 4: ( ) ( )R RNote M M
School of Software
Theorem 2 Let A be a set with |A|=n, and let R be a relation on A. Then
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
4.8 Transitive Closure and Warshall’s Algorithm
129
2 ... nR R R R
School of Software
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.
4.8 Transitive Closure and Warshall’s Algorithm
130
School of Software
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
4.8 Transitive Closure and Warshall’s Algorithm
131
School of Software
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)
4.8 Transitive Closure and Warshall’s Algorithm
132
P1, P2 are subpaths with all interior vertices from {a1,a2, …, ak-1}
ak
ai aj
P1
P2
School of Software
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
4.8 Transitive Closure and Warshall’s Algorithm
133
School of Software
Example 2 Consider the relation R defined in Example 1. Then
4.8 Transitive Closure and Warshall’s Algorithm
134
0
0100
1010
0001
0000
RW M
1
0100
1110
0001
0000
W
2
1110
1110
0001
0000
W
3
1111
1111
0001
0000
W
pi: 2qj: 2
pi: 1, 2qj: 1, 2, 3
pi: 1, 2qj: 4
pi: 1, 2qj: ф
4 3RM W W
School of Software
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)
4.8 Transitive Closure and Warshall’s Algorithm
135
School of Software
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)
4.8 Transitive Closure and Warshall’s Algorithm
136
School of Software
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
4.8 Transitive Closure and Warshall’s Algorithm
137
1 2 3 4 5
R
1 2 3 4 5
S
School of Software
4.8 Transitive Closure and Warshall’s Algorithm
138
11000
11000
00110
00110
00001
RM
10000
01000
00100
00011
00011
SM
11000
11000
00110
00110
00011
R SM
3 2
1 0
W W
W W
5 4
11000
11000
00111
00111
00111
W W
(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 }∪
1 2 3 4 5
(RUS) ∞
School of Software
Homework
Ex. 1, Ex. 2, Ex. 7, Ex. 13, Ex. 18, Ex. 25
4.8 Transitive Closure and Warshall’s Algorithm
139