Transcript 제13주

Relations (2)
Rosen 6th ed., ch. 8
Closures of Properties
• Let R be a relation on a set A. R may or may not
have some property P, such as reflexivity,
symmetry, or transitivity. If there is a relation S
with property P containing R such that S is a
subset of every relation with property P
containing R, then S is called the closure of R with
respect to P.
– For any property X, the “X closure” of a set A is defined
as the “smallest” subset of A that has the given
property.
Reflexive Closure
• Reflective closure of R is R  Δ, where Δ = {(a, a)
| a  A} is the diagonal relation on A.
• EXAMPLE: What is the reflexive closure of the
relation R = {(1, 1), (1, 2), (2, 1), (3, 2)} on the set
A = {1, 2, 3}?
Solution: R  Δ = {(1, 1), (1, 2), (2, 1), (3, 2)}  {(1,
1), (2, 2), (3, 3)} = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 2),
(3, 3)}
Reflexive Closure Cont.
• EXAMPLE: What is the reflexive closure of the
relation R = {(a, b) | a < b} on the set of integers?
Solution: R  Δ = {(a, b) | a < b}  {(a, a) | a  Z}
= {(a, b) | a ≤ b}.
Symmetric Closure
• R  R- is the symmetric closure of R, where R- =
{(b, a) | (a, b)  R}.
• EXAMPLE: What is the symmetric closure of the
relation R = {(a, b) | a > b} on the set of positive
integers?
Solution: R  R- = {(a, b) | a > b}  {(b, a) | a < b}
= {(a, b) | a ≠ b}.
A case of transitivity closure
• Consider the relation R = {(1, 3), (1, 4), (2, 1), (3,
2)} on the set {1, 2, 3, 4}. This relation is not
transitive since (1, 2), (2, 3), (2, 4), and (3, 1) is
not in R.
• Adding (1, 2), (2, 3), (2, 4), and (3, 1), We get a
relation {{(1, 3), (1, 2), (1, 4), (2, 1), (2, 3), (2, 4), (3,
1), (3, 2)}. Is this relation transitive? => NO.
• The relation contains (3, 1) and (1, 4) but does not
contain (3, 4)
– Constructing the transitive closure of a relation is more
complicate than constructing either the reflective or
symmetric closure.
Paths in Directed Graphs
• DEFINITION: A path from a to b in the directed
graph G is a sequence of edges (x0, x1), (x1, x2), (x2,
x3),…, (xn-1, xn) in G, where n is a nonnegative
integer, and x0 = a and xn = b, that is, a sequence
of edges where the terminal vertex of an edge is
the same as the initial vertex in the next edge in
the path.
• This path is denoted by x0, x1, x2, … , xn-1, xn and
has length n. We view the empty set of edges as a
path from a to a. A path of length n ≥ 1 that
begins and ends at the same vertex is called a
circuit or cycle.
Paths in Directed Graphs Cont.
• Which of the following are paths in the directed
graph shown below: a, b, e, d; a, e, c, d, b; b, a, c,
b, a, a, b; d, c; c, b, a; e, b, a, b, a, b, e? What are
the lengths of those that are paths? Which are
circuit?
– Paths: a, b, e, d(l=3); b, a, c, b, a, a, b(l=6); d, c(l=1);
c, b, a(l=2); e, b, a, b, a, b, e(l=6)
– Circuits: b, a, c, b, a, a, b;
a
b
e, b, a, b, a, b, e
d
e
c
Paths in Directed Graphs Cont.
• THEOREM: Let R be a relation on a set A. There is
a path of length n, where n is a positive integer,
from a to b if and only if (a, b)  Rn.
R
R
R2
1
1
1
1
1
2
2
2
2
2
3
3
3
3
3
4
4
4
4
4
Transitive Closure
• DEFINITION: Let R be a relation on a set A. The
connectivity relation R* consists of the pairs (a, b)
such that there is a path of length at least one
from a to b in R.
• Because Rn consists of the pairs (a, b) such that
there is a path of length n from a to b, it follows
that R* is the union of all the sets Rn. In other
words,
R 
*
R
nZ 
n
Transitive Closure Cont.
• EXAMPLE: R이 전 세계의 모든 사람들의 집합에 대한
관계이고 a가 b를 만난 적이 있으면 (a, b)를 포함한다고
하자. Rn (n은 1보다 큰 양의 정수)은 무엇을 의미하는가?
또한 R*는 무엇을 의미하는가?
– 관계 R2는 (a, c)  R이고 (c, b)  R 인 사람 c가 있으면,
즉, a가 c를 만난 적이 있고 c가 b를 만난 적이 있는
사람 c가 존재하면, (a, b)를 포함한다.
– 비슷하게, Rn은 a가 x1을 만난 적이 있고, x1이 x2를 만
난 적이 있고, … xn-1이 b를 만난 적이 있는 사람들 x1,
x2, …, xn-1이 존재하는 (a, b)들로 구성된다.
– 관계 R*는 어떤 순차(sequence) 내의 각 사람이 순차
내의 다음 사람을 만난 적이 있는, a에서 시작하여 b
로 끝나는 사람들의 순차가 존재하면 (a, b)를 포함한
다.
Transitive Closure Cont.
• THEOREM: The transitive closure of a relation R
equals the connectivity relation R*.
• THEOREM: Let MR be the zero-one matrix of the
relation R on a set with n elements. Then the
zero-one matrix of the transitive closure R* is
M R*  M R  M
[ 2]
R
M
[3]
R
 ... M
[ n]
R
Example of Transitive Closure.
• EXAMPLE: Find the zero-one matrix of the
transitive closure of the relation R where
1 0 1 
M R  0 1 0
1 1 0
Example Cont.
• Because
1 1 1 
0 1 0 
= 

1 1 1 
M R[ 2]
1 0 1  1 0 1 
 0 1 0 ⊙ 0 1 0
1 1 0  1 1 0 
M R[3]
1 1 1  1 0 1 
1 1 1 
 0 1 0⊙ 0 1 0 = 0 1 0
1 1 1 
1 1 1  1 1 0 
M R*
and
1 0 1 1 1 1 1 1 1 1 1 1








 0 1 0   0 1 0   0 1 0   0 1 0 
1 1 0 1 1 1 1 1 1 1 1 1
Simple Transitive Closure Alg.
A procedure to compute R* with 0-1 matrices.
procedure transClosure(MR: n x n 0-1 matrix)
A := B := MR;
for i := 2 to n
begin
{note A represents Ri}
A := A⊙MR; B := B  A {join}
end
return B {Alg. takes Θ(n4) time}
Warshall’s Algorithm
• 앞서 설명한 R*알고리즘의 시간 복잡도는 O(n4)이다. 따라
서 좀 더 빠른 시간 안에 R*를 구할 수 있는 알고리즘이 필
요했으며, 1960년에 Warshall이 새로운 알고리즘을 발표한
다.
• 내부 정점(internal vertex)
– 만약 a, x1, x2, …, xm-1, b가 경로(path)이면, 내부 정점은 x1,
x2, …, xm-1이다. 즉, 맨 앞과 맨 뒤가 아닌 경로 상의 임의
의 위치에 놓인 정점이 내부 정점이다.
Warshall’s Algorithm
• Warshall의 알고리즘은 일련의 0-1 행렬을 구하는 것에 기
초한다. 이들 행렬은 W0, W1, … Wn이며 W0은 MR(주어진 관
[k ]
[k ]
w
계 R의 0-1 행렬)이고, Wk  [wij ] 에서 ij 는 모든 내부 정
점이 집합 {v1, v2, … vk} (앞 부분 k개의 정점)의 원소인 vi에서
vj로의 경로가 존재하면 1이고, 그렇지 않으면 0이다.
• 이렇게 해서 Wn(n은 관계 R이 정의된 집합의 원소의 수)을
구하면 Wn = MR*이다.
Warshall’s Algorithm
• EXAMPLE: Let R be the relation with directed graph
shown below. Let a, b, c, d be a listing of the
elements of the set. Find the matrices W0, W1, W2,
W3, and W4. The matrix W4 is the transitive closure of
R.
Solution: v1 = a, v 2 = b, v3 = c, v4 = d라 하자. W0 는 주
어진 관계의 행렬이므로,
0
W0 = 1

1

0
0
0
0
0
0
1
0
1
1
0.
1

0
a
d
b
c
Warshall’s Algorithm
• W1 은 내부 정점으로 v1 = a만을 포함하는 vi에서 vj로의 경
로가 있으면 (i, j) 위치를 1로 한다. 길이 1인 이전의 경로들
은 내부 정점이 없으므로 여기서도 모두 1이다.
• 이제, b에서 d로 가는 경로 b, a, d가 허용되므로,
0
1
W1 = 
1

0
0
0
0
0
0
1
0
1
1
1
.
1

0
(이전 단계에서 (i, a) 위치와 (a, j)가 모두
1이었던 (i, j)들의 위치를 1로 한다.)
a
d
b
c
Warshall’s Algorithm
• W2 는 내부 정점으로 v1 = a와 v2 = b 만을 포함하는 vi에서 vj
로의 경로가 있으면 (i, j) 위치를 1로 한다. b로 향하는 간선
(edge)이 없으므로, 변화가 없다.
a
0
W2 = 
1
1

0
0
0
0
0
0
1
0
1
1
.

1
1

0
d
b
c
Warshall’s Algorithm
• W3 은 내부 정점으로 v1 = a, v2 = b와 v3 = c 만을 포함하는 vi
에서 vj로의 경로가 있으면 (i, j) 위치를 1로 한다.
• d에서 a로 가는 경로 d, c, a와 d에서 d로 가는 경로 d, c, d가
허용되므로,
0
1
W3 = 
1

1
0
0
0
0
0
1
0
1
1
1
.
1

1
(이전 단계에서 (i, c) 위치와 (c, j)가 모두
1이었던 (i, j)들의 위치를 1로 한다.)
a
d
b
c
Warshall’s Algorithm
• W4 는 d까지 포함하여 모든 정점을 내부 정점으로 허용한
다. 따라서, vi에서 vj로의 경로가 있는 모든 (i, j) 위치를 1로
한다
a
1
W4 = 1

1

1
0
0
0
0
1
1
1
1
1
1 .
1

1
d
b
c
Warshall’s Algorithm
• Uses only Θ(n3) operations!
Procedure Warshall(MR : rank-n 0-1 matrix)
W := MR
for k := 1 to n
for i := 1 to n
for j := 1 to n
wij := wij  (wik  wkj)
return W {this represents R*}
wij = 1 means there is a path from i to j going only through nodes ≤k
Equivalence Relations
• An equivalence relation (e.r.) on a set A is
simply any binary relation on A that is
reflexive, symmetric, and transitive.
– E.g., = itself is an equivalence relation.
Equivalence Relation Examples
• “Strings a and b are the same length.”
• “Integers a and b have the same absolute
value.”
• “Real numbers a and b have the same
fractional part (i.e., a − b  Z).”
• “Integers a and b have the same residue
modulo m.” (for a given m>1)
Equivalence Classes
• Let R be any equiv. rel. on a set A.
• The equivalence class of a,
[a]R :≡ { b | aRb }
(optional subscript R)
– It is the set of all elements of A that are “equivalent” to a
according to the eq.rel. R.
– Each such b (including a itself) is called a representative of
[a]R.
Equivalence Class Examples
• “Strings a and b are the same length.”
– [a] = the set of all strings of the same length as a.
• “Integers a and b have the same absolute value.”
– [a] = the set {a, −a}
• “Real numbers a and b have the same fractional part
(i.e., a − b  Z).”
– [a] = the set {…, a−2, a−1, a, a+1, a+2, …}
• “Integers a and b have the same residue modulo m.”
(for a given m>1)
– [a] = the set {…, a−2m, a−m, a, a+m, a+2m, …}
Partitions
• A partition of a set A is the set of all the
equivalence classes {A1, A2, … } for some e.r.
on A.
• The Ai’s are all disjoint and their union = A.
• They “partition” the set into pieces. Within
each piece, all members of the set are
equivalent to each other.
An example of Partition
• EXAMPLE: What are the partition of the
integers arising from congruence modulo 4?
Solution: [0]4 = {…, -8, -4, 0, 4, 8, …}
[1]4 = {…, -7, -3, 1, 5, 9, …}
[2]4 = {…, -6, -2, 2, 6, 10, …}
[3]4 = {…, -5, -1, 3, 7, 11, …}
These classes are disjoint, and every integer is
in exactly one of them. They form a partition.