29
Relations (2) Relations (2) Rosen 6 th ed., ch. 8

Relations (2) 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,

Embed Size (px)

DESCRIPTION

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)}

Citation preview

Page 1: Relations (2) 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,

Relations (2)Relations (2)

Rosen 6th ed., ch. 8

Page 2: Relations (2) 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,

Closures of PropertiesClosures 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.

Page 3: Relations (2) 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,

Reflexive ClosureReflexive 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)}

Page 4: Relations (2) 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,

Reflexive Closure Cont.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}.

Page 5: Relations (2) 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,

Symmetric ClosureSymmetric 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}.

Page 6: Relations (2) 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,

A case of transitivity closureA 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.

Page 7: Relations (2) 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,

• 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 GraphsPaths in Directed Graphs

Page 8: Relations (2) 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,

• 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; e, b, a, b, a, b, e

Paths in Directed Graphs Cont.Paths in Directed Graphs Cont.

e

a b

d

c

Page 9: Relations (2) 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,

• 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.

Paths in Directed Graphs Cont.Paths in Directed Graphs Cont.

1

2

3

4

1

2

3

4

R1

2

3

4

R1

2

3

4

1

2

3

4

R2

Page 10: Relations (2) 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,

• 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,

Transitive ClosureTransitive Closure

Zn

nRR*

Page 11: Relations (2) 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,

• 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.Transitive Closure Cont.

Page 12: Relations (2) 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,

• 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

Transitive Closure Cont.Transitive Closure Cont.

][]3[]2[* ... n

RRRRR MMMMM

Page 13: Relations (2) 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,

• EXAMPLE: Find the zero-one matrix of the transitive closure of the relation R where

Example of Transitive Closure.Example of Transitive Closure.

011010101

RM

Page 14: Relations (2) 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,

• Because

⊙ = and

⊙ =

Example Cont.Example Cont.

]2[RM

111010111

011010101

011010101

]3[RM

111010111

011010101

111010111

111010111

111010111

111010111

011010101

*RM

Page 15: Relations (2) 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,

Simple Transitive Closure Alg.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

A := A M⊙ R; B := B A {join}endreturn B {Alg. takes Θ(n4) time}

{note A represents Ri}

Page 16: Relations (2) 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,

Warshall’s AlgorithmWarshall’s Algorithm

• 앞서 설명한 R* 알고리즘의 시간 복잡도는 O(n4) 이다 . 따라서 좀 더 빠른 시간 안에 R* 를 구할 수 있는 알고리즘이 필요했으며 , 1960 년에 Warshall 이 새로운 알고리즘을 발표한다 .

• 내부 정점 (internal vertex)– 만약 a, x1, x2, …, xm-1, b 가 경로 (path) 이면 , 내부

정점은 x1, x2, …, xm-1 이다 . 즉 , 맨 앞과 맨 뒤가 아닌

경로 상의 임의의 위치에 놓인 정점이 내부 정점이다 .

Page 17: Relations (2) 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,

Warshall’s AlgorithmWarshall’s Algorithm

• Warshall 의 알고리즘은 일련의 0-1 행렬을 구하는 것에 기초한다 . 이들 행렬은 W0, W1, … Wn 이며 W0 은 MR(주어진 관계 R 의 0-1 행렬 ) 이고 , 에서 는 모든 내부 정점이 집합 {v1, v2, … vk} ( 앞 부분 k 개의 정점 ) 의 원소인 vi 에서 vj 로의 경로가 존재하면 1 이고 , 그렇지 않으면 0 이다 .

• 이렇게 해서 Wn(n 은 관계 R 이 정의된 집합의 원소의 수 ) 을 구하면 Wn = MR* 이다 .

][ ][kijk wW ][k

ijw

Page 18: Relations (2) 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,

Warshall’s AlgorithmWarshall’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 는 주어진 관계의 행렬이므로 ,

W0 = .

0100100101011000

c

a b

d

Page 19: Relations (2) 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,

Warshall’s AlgorithmWarshall’s Algorithm

• W1 은 내부 정점으로 v1 = a 만을 포함하는 vi 에서 vj 로의 경로가 있으면 (i, j) 위치를 1 로 한다 . 길이 1 인 이전의 경로들은 내부 정점이 없으므로 여기서도 모두 1 이다 .

• 이제 , b 에서 d 로 가는 경로 b, a, d 가 허용되므로 ,

W1 = .

( 이전 단계에서 (i, a) 위치와 (a, j) 가 모두 1 이었던 (i, j) 들의 위치를 1 로 한다 .)

0100100111011000

c

a b

d

Page 20: Relations (2) 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,

Warshall’s AlgorithmWarshall’s Algorithm

• W2 는 내부 정점으로 v1 = a 와 v2 = b 만을 포함하는 vi

에서 vj 로의 경로가 있으면 (i, j) 위치를 1 로 한다 . b 로 향하는 간선 (edge) 이 없으므로 , 변화가 없다 .

W2 = .

0100100111011000

c

a b

d

Page 21: Relations (2) 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,

Warshall’s AlgorithmWarshall’s Algorithm

• W3 은 내부 정점으로 v1 = a, v2 = b 와 v3 = c 만을 포함하는 vi 에서 vj 로의 경로가 있으면 (i, j) 위치를 1 로 한다 .

• d 에서 a 로 가는 경로 d, c, a 와 d 에서 d 로 가는 경로 d, c, d 가허용되므로 ,

W3 = .

( 이전 단계에서 (i, c) 위치와 (c, j) 가 모두 1 이었던 (i, j) 들의 위치를 1 로 한다 .)

1101100111011000

c

a b

d

Page 22: Relations (2) 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,

Warshall’s AlgorithmWarshall’s Algorithm

• W4 는 d 까지 포함하여 모든 정점을 내부 정점으로 허용한다 . 따라서 , vi 에서 vj 로의 경로가 있는 모든 (i, j) 위치를 1 로 한다

W4 = .

1101110111011101

c

a b

d

Page 23: Relations (2) 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,

Warshall’s AlgorithmWarshall’s Algorithm

• Uses only Θ(n3) operations!Procedure Warshall(MR : rank-n 0-1 matrix)

W := MR

for k := 1 to nfor i := 1 to n

for j := 1 to nwij := wij (wik wkj)

return W {this represents R*}

wij = 1 means there is a path from i to j going only through nodes ≤k

Page 24: Relations (2) 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,

Equivalence RelationsEquivalence 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.

Page 25: Relations (2) 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,

Equivalence Relation ExamplesEquivalence 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)

Page 26: Relations (2) 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,

Equivalence ClassesEquivalence 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.

Page 27: Relations (2) 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,

Equivalence Class ExamplesEquivalence 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, …}

Page 28: Relations (2) 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,

PartitionsPartitions

• 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.

Page 29: Relations (2) 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,

An example of PartitionAn 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.