Rosen 6 th 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 (x
0 x
, x
1
), (x
1
, x
2
) in G, where n is a nonnegative
), (x
2
3
),…, (x
n-1
, x n integer, and x
0
= a and x n
= 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 x
0
, x
1
, x
2
, … , x
n-1
, x n 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 e, b, a, b, a, b, e b c d e
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)
R n .
R R R 2
3
4
1
2
3
4
1
2
1
2
3
4
3
4
1
2
1
2
3
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 R n 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 R n . In other words,
R
*
n
Z
R n
Transitive Closure Cont.
• EXAMPLE: R 이 전 세계의 모든 사람들의 집합에 대한
관계이고 a
가 b
를 만난 적이 있으면
(a, b)
를 포함한다고
하자
. R n (n
은
1
보다 큰 양의 정수
또한
R*
는 무엇을 의미하는가
?
)
은 무엇을 의미하는가
?
– 관계 R 2 는
(a, c)
R
이고
(c, b)
R
인 사람
즉
, a
가 c
를 만난 적이 있고 c
가 b
를 만난 적이 있는
사람 c
가 존재하면
, (a, b)
를 포함한다
. c
가 있으면
,
– 비슷하게 x
2
, …, x n-1
, R n
은 a
가
난 적이 있고
, … x
n-1 x
이 b
를 만난 적이 있는 사람들 x
이 존재하는
1
을 만난 적이 있고
, x
(a, b)
들로 구성된다
.
1
이 x
2
를 만
1
– 관계
R*
는 어떤 순차
(sequence)
내의 각 사람이 순차
,
내의 다음 사람을 만난 적이 있는
, a
에서 시작하여 b
로 끝나는 사람들의 순차가 존재하면
(a, b)
를 포함한
다
.
Transitive Closure Cont.
• THEOREM: The transitive closure of a relation R equals the connectivity relation R*.
• THEOREM: Let M
R 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
R
[ 2 ]
M
R
[ 3 ]
...
M
R
[ n ]
Example of Transitive Closure.
• EXAMPLE: Find the zero-one matrix of the transitive closure of the relation R where
M
R
1
0
1
0
1
1
1
0
0
Example Cont.
• Because
M
R
[ 2 ]
1
0
1
M
R
[ 3 ]
1
0
1
M
R *
1
0
1
0
1
1
0
1
1
1
0
0
⊙
1
0
1
0
1
1
1
0
0
1
1
1
1
1
1
1
1
0
1
⊙
1
0
1
1
0
0
1
0
1
1
1
1
0
1
1
1
0
0
=
1
0
1
1
0
1
1
0
1
1
1
1
1
1
1
1
1
1
0
1
1
0
1
1
0
1
1
1
1
1
0
1
Simple Transitive Closure Alg.
A procedure to compute R * with 0-1 matrices.
procedure transClosure(M
R
: n x n 0-1 matrix)
A := B := M
R
; for i := 2 to n begin
A := A ⊙ M
R
{note A represents R i }
; B := B
A {join} end return B {Alg. takes Θ(n 4 ) time}
Warshall’s Algorithm
• 앞서 설명한
R*
알고리즘의 시간 복잡도는
O(n 4 )
이다
.
따라
서 좀 더 빠른 시간 안에
R*
를 구할 수 있는 알고리즘이 필
요했으며
, 1960
년에
Warshall
이 새로운 알고리즘을 발표한
다
.
• 내부 정점
(internal vertex)
– 만약
a, x
1 x
2
, …, x m-1
, x
2
, …, x m-1
, b
가 경로
(path)
이면
,
내부 정점은 x
이다
.
즉
,
맨 앞과 맨 뒤가 아닌 경로 상의 임의
의 위치에 놓인 정점이 내부 정점이다
.
1
,
Warshall’s Algorithm
• Warshall
의 알고리즘은 일련의
0-1
행렬을 구하는 것에 기
초한다
.
이들 행렬은
W
계
점이 집합 v j
R
의
0-1
행렬
{v
1
, v
2
)
이고
, … v
W
0 k
, W
[
1
, … W w ] n
이며
W
에서 w
[ k ij
]
0
은
M
R
(
주어진 관
는 모든 내부 정 k
} (
앞 부분 k
개의 정점
)
의 원소인 v
로의 경로가 존재하면
1
이고
,
그렇지 않으면
0
이다
.
i
에서
• 이렇게 해서
W n
(n
은 관계
R
이 정의된 집합의 원소의 수
)
을
구하면
W n
= M
R*
이다
.
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 W
W
R.
3
, and W
4
. The matrix W
4
0
, W
1
, W
2
, is the transitive closure of
Solution: v = a, v = b, v
3
= c, v
4
어진 관계의 행렬이므로
,
= d
라 하자
. W
0
는 주 a b
W
0
0
1
1
0
0
0
0
0
0
1
0
1
1
0
1
0
d c
Warshall’s Algorithm
• W
1
은 내부 정점으로 v
1
= a
만을 포함하는 v i
에서 v
은 내부 정점이 없으므로 여기서도 모두
1
이다
.
j
로의 경
로가 있으면
(i, j)
위치를
1
로 한다
.
길이
1
인 이전의 경로들
• 이제
, b
에서 d
로 가는 경로
b, a, d
가 허용되므로
,
W
1
0
1
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
• W
2
는 내부 정점으로 v
1
(i, j)
= a
와 v
2
= b
만을 포함하는 v i
에서 v j
위치를
(edge)
이 없으므로
,
변화가 없다
.
1
로 한다
. b
로 향하는 간선 a b
W
2
0
1
1
0
0
0
0
0
0
1
0
1
1
1
1
0
d c
Warshall’s Algorithm
• W
에서
은 내부 정점으로 v j v
1
= a, v
2
= b
와 v
3
= c
만을 포함하는
로의 경로가 있으면
(i, j)
위치를
1
로 한다
. v
• d
에서 a
로 가는 경로
d, c, a
와 d
에서 d
로 가는 경로
d, c, d
가
허용되므로
, i
W
3
0
1
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
• W
다
4
.
는 d
까지 포함하여 모든 정점을 내부 정점으로 허용한
따라서
한다
, v i
에서 v j
로의 경로가 있는 모든
(i, j)
위치를
1
로 a b
W
4
1
1
1
1
0
0
0
0
1
1
1
1
1
1
1
1 d c
Warshall’s Algorithm
• Uses only Θ(n 3 ) operations!
Procedure Warshall(M
R
W := M
R
: rank-n 0-1 matrix) for k := 1 to n for i := 1 to n for j := 1 to n w ij
:= w ij
(w ik return W {this represents R * }
w kj
) w ij
= 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 {A
1
, A
2
, … } for some e.r. on A.
• The A i
’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.