1 / 63

Chapter 6

Chapter 6. Relations. Contents. 1. Relations and their properties 2. n-ary relations and their applications 3. Representing relations 4. Closure of relations 5. Equivalence relations 6. Partial orderings. 6.1 Relations and their properties. Def. 1: A, B: two sets.

claral
Télécharger la présentation

Chapter 6

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 6 Relations

  2. Contents 1. Relations and their properties 2. n-ary relations and their applications 3. Representing relations 4. Closure of relations 5. Equivalence relations 6. Partial orderings

  3. 6.1 Relations and their properties Def. 1: A, B: two sets. A (binary) relation from A to B is a subset of AxB. (i.e., any subset of {(x,y) | x Î A and y Î B} ) Notes: • If R Í AxB, we usually use a R b (or not aRb) to denote that (a,b) Î R (or (a,b) Ï R). • Binary relations can be generalized to n-ary ( n > 0) relations Ex1: A = the set of all students; B = the set of all courses let R = {(a,b) | student a is enrolled in course b} Í AxB is a binary relation from A to B. Ex2: A = the set of all cities; B = the set of all nations. Let R = {(x,y) | city x is located in nation y}. ==> (New York, USA) Î R and Tokyo R Japan, ...

  4. Relations on a set Ex3: A = {1,2,3,4}; B={a,b} ==> {(0,a),(0,b),(1,a),(2,b)} is a relation from A to B. ==> Graphical representation = ? ==> table (or matrix) representation = ? Def. 2: A relation R on (or over) a set A is a subset of AxA (= A2). Ex4: A={1,2,3,4}; Let R = {(a,b) Î A2 | a divides b} ==> R= ? ; graphical and table representation = ? Ex 5: Let R1,R2,...,R6 be relations on integers Z defined as R1= {(a,b) | a £ b); R2 = {(a,b) | a > b }; R3 = {(a,b) | a = b or a = -b}; R4={(a,b) | a = b}; R5={(a,b) | a = b+1}; R6={(A,B) | A + B < 4} ==> Which relations contain the pairs: (1,1), (1,2),(2,1),(1,-1),(2,2)?

  5. Examples of relations Ex 6: |A| = n; |B| = m ==> What is the number of relations from A to B ? (i.e., let Rel(A,B)= {R | R is a relation from A to B}, ==> |rel(A,B)| = ? ) Sol: 1. |AxB| = ____. 2. Since Rel(A,B) = 2AxB. Hence |rel(A,B)| = ___ Ex6': |Rel(A,A)| = #relations on A = ____

  6. Properties of (binary) relations on a set Def 3,4,5: R: a binary relation on a set A; • R is reflexive if aRa for every a Î A. • R is irrelexive if (a,a) Ï R for each a Î A. • R is symmetric if for every a,b ÎA, (a,b) Î R => (b,a) ÎR. • R is asymmetric if for every a,b Î A, (a,b) Î R => (b,a) Ï R. • R is antisymmetric if for every a,b Î A, if (a,b) Î R and (b,a) Î R, then a = b. • R is transitive if for every a,b,c Î A, if aRb and bRc then aRc. Def: • R is a partial ordering(p.o.) iff it is ref. sym. and transitive. • R is a strict partial ordering(s.p.o) if it is irref. and trans. • R is a preordering iff it is ref. and transitive. • R is an equivalence relation iff it is ref. sym. and transitive.

  7. Example: • Let B = {1,2,3,4}; • R1= {(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)} • R2={(1,1),(1,2),(2,1)} • R3={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)} • R4={(2,1),(3,1),(3,2),(4,1),(4,2),(4,3)} • R5={((1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),(3,4),(4,4)} • R6={(3,4)} Ex 7: Which of these relations are ref. ? Ex 7': Which of these relations are irref. ? Note: 1. using graphical representations may help determining properties of relations. 2. There are relations which is neither ref. nor irref. Ex 8: Is the "divides" relation on integers reflexive ? Ex 9: which of the relations on Ex5 are ref. ?

  8. Examples Ex10. Which of these relations are symmetric, antisymmetric ? Ex10': Which are asymmetric ? • What is the graphical characteristic of sym., asym. and antisym. relations ? • What is the relation among symmetric, antisymmetric and asymmetric properties ? Ex 12: Is the "divides" relation on integers sym. ? Is it antisym. or asym. ? Ex13: Which of these relations in Ex 7 are transitive. ? Ex13': Which of the relations inEx 7 are preordering, p.o., s.p.o. or equivalence? Ex15: is the "divides" relation transitive ?

  9. Counting the number of relations • |A| = n. 1. # relations on A = ? (2^(n^2)) 2. # reflexive relations on A = ? (2 ^ (n^2 - n)) 3. # irreflexive relation on A = ? (= sol of 2, why?) 4. #irref and symmetric relation on A = ? (2^(n(n-1)/2) ) 5. #symmetric relation on A = ? (2^(n(n+1)/2)) 6. #asymmetric relations on A = ? (3^(n(n-1)/2)) 7. #antisymmetric relations on A = ? (3^(n(n-1)/2) x 2^n) 8. #transitive relations on A = ? (hard!) 9. #equivalence relations on A = ? (hard!) 10. #strict partial ordering on A = ? (hard!) 11. #preordering on A = ? (hard!) 12. #partial ordering on A = ? (hard!)

  10. Combining relations • Relations are sets: • ==> All set operations can be applied to relations • Ex: R1; R2: relations on A; • ==> R1 U R2; R1 ⋂ R2; R1 \ R2 and ~R are defined as usual. • [Binary ] relation compositions: R Í AxB ; S Í BxC; ==> R·S (the composition of R and S) Í AXC is the set: {(x,z) | $y Î B s.t. xRy ∧ ySz} Note: The textbook use S·R (less popular) instead of R·S to denote the composition.

  11. 1 0 1 2 2 1 3 2 3 4 R S C A B Examples of relation compositions Ex19: A={1,2,3}; B={1,2,3,4}; C={0,1,2} R ÍAXB = {(1,1),(1,4),(2,3),(3,1),(3,4)} S Í BXC = {(1,0),(2,0),(3,1),(3,2),(4,1)} => R·S= ? Fact: Relation composition is associative (i.e., R·(S·T) = (R·S)·T ) Hence we can write RST w/t parentheses.

  12. The power of relations Def.7 : R: a relation on A (i.e., a subset of AxA); n : nonnegative integers. The relation Rn is defined inductively as follows: • 1. R0 = IA =def {(x,x) | x ∈ A}. • 2. Rk+1 = RRk = {(x,z) | $y Î R s.t. (x,y) ÎR /\ (y,z) ÎRk }. • Hence R2 = RR; R3=RR2 = R(RR) = (RR)R = R2 R = RRR . • in general we have Rn = RRRR...R. (n R’s) Ex: show that If a, b  0 then RaRb = Ra+b. Pf : by induction on a. • basis: a = 0: => RaRb = IARb = • {(x,z) | $y (x,y) Î IA /\ (y,z) Î Rb } = • {(x,x) | (x,x) Î IA /\ (x,z) Î Rb} = {(x,z) | (x,z) Î Rb } = Rb. • Ind. step : assume RaRb= Ra+b => • Ra+1Rb = (RRa)Rb = R(RaRb) = RRa+b = Ra+1+b. Corollary: RRk = Rk R.

  13. Example Ex20: A= {1,2,3,4}; R={(1,1),(2,1),(3,2),(4,3)} is relation on A. => R0, R1, R2, R3, R4,R5, ...= ____? (use graphical representation) Lemma: If Rn = Rn+1 then Rk = Rk-1 for all k > n. Ex: If R5 = R6 => R10 = R6R4 = R5R4 = R9 Pf: left as an exercise. Corollary: If Rn = Rn+1 ==> Rk = Rn for all k > n. Pf: Rk = Rk-1 = R(k-1)-1 = Rk-2 = ...Rn+1 = Rn. Theorem 1: Relation R on a set A is transitive iff RnÍ R for all n > 0. Pf: (=>) : trivial (by induction on n). Basis: n = 1 (ok). Ind. step: for any (x,z) Î Rn+1 => $z s.t. (x,y) Î Rn /\ (y,z) Î R. By ind. hyp. both (x,y) and (y,z) Î R. By tran. of R, (x,z) Î R. (<=) : for any x,y,z, (x,y) Î R /\ (y,z) Î R => (x,z) Î R2 => (x,y) Î R. QED Corollary: R is transitive iff R2 Í R. (why ?)

  14. 6.2 n-ary relations Def. 1: A1,A2,...,An : n sets. 1. An n-ary relation R on A1,A2,...,An is any subset of A1xA2x...xAn = {(x1,...,xn) | x1Î A1,...,xnÎ An }. 2. A1,..., An are the domains of R 3. n: the degree (or arity) of R. 4. 1-ary (0-ary, 2-ary, 3-ary) relations are called unary (nullary, binary, ternary) relations. Ex: If R Í Z3 (= ZxZxZ) =def {(x,y,z) | x < y < z, x,y,z ∈Z}. Then (1,2,3) Î R but (2,4,3) Ï R.

  15. Relations and databases • Database: • In a database, lots of information about the real world are extracted and stored. • Since the database is a "representation" of the world, if the real world changes, the database should change over time accordingly • Issues in database theory (data model): • How to represent various information (or data) in a database? • (i.e., can we have a unifying method by which we can always (or in most cases) easily organize or arrange all data to be stored in the database ). • Any such method is called a data model.

  16. The relational data model • A database DB (at a time instant, say, time t) consists of a pair <Dom,Rel> where • Dom = {A1,...,An} is a set of sets, each Ai is called a domain of the database. • Rel = {R1,...,Rm} where each Ri Aj1xAj2x...xAjki is a ki-ary relations on domain Aj1,Aj2,...,Ajki. • Each Ki-tuple (a1,a2,...,aki) Ri is called a record (or row) of Ri. • Each Ajs ( 1 <= s < = ki) is called a field (or a column) of the relation Ri (and the record). • Each relation Ri can be represented by a table (w/t duplicate rows) with |Ri| rows and Ki columns.

  17. Example relations Ex: Student records S in a school may be represented by 4-tuples of the form: (student-name, register-id, major, GPA ) Let A = the set of all student [names] B = the set of all student identifier numbers. C = the set of all departments [names] D = the set of all possible GPA numbers ([0,5]). => DB = <{A,B,C,D}, {S}> where S must be a subset of AxBxCxD. A possible value of S may be represented by the table: NAME ID MAJOR GPA ------------------------------------------------------- Allen 23 CS 3 Bob 15 math 4.1 Chen 34 CS 4.0 ...

  18. Operations on n-ary relations • A domain Bj of an n-ary relation R (in database) is called a primary key iff it can be used to uniquely identify records in R at all time. • (in other words, If R is an n-ary relation on domain B1,B2,...,Bn, then for any time t, R(t) must be a subset of B1xB2...xBn. • Bj is a primary key iff for any time t and for any (a1,a2,...,an) and (b1,..,bn) Î R(t), if aj = bj then (a1,...an) = (b1,...,bn). • Note: R(t) changes over time. • Operations on n-ary relations • Selection (or restriction) --- find subset of R with a certain property. • Projections. • Joins --- generalization of binary composition • and other usual set operations (U, /, intersection,...)

  19. relation operations • Restriction (selection): P(x1,...,xn): a statement (or property) on n-tuples (x1,...,xn). R: a relation on A1xA2...,xAn. ==> R|P ( find tuples of R with property P) is the set {(a1,...,an) | (a1,...,an) Î R and P(a1,...,an) is true }. Ex: Let P(x1,x2,x3) = " x1 < x2 < x3 or x3 = 2 " and R = { (1,2,3), (1,3,2), (2,1,3), ==> R|p = {(1,2,3), (1,3,2), (3,1,2) }. (2,3,1), (3,1,2), (3,2,1) }

  20. Projections R : a relation on A1,A2,...,An w= j1j2...jm is a sequence with 0< js< n+1 for all s = 1,..,m. ==> Pw(R) : the projection of R on Aj1Aj2...Ajm is a m-ary relation on Aj1Aj2...Ajm with value: {(xj1,...,xjm) | (x1,...,xn) Î R }. Ex: R= {(1,2,3), (1,3,3), (2,3,1), (2,2,2)} => P1(R) = {1,1,2,2} = {1,2} P2(R) = {2,3} P31(R) = {(3,1),(3,1),(1,2),(2,2)} = {(3,1),(1,2),(2,2)} P313(R) = {(3,1,1), (1,2,1),(2,2,2)}

  21. Joins (combining two relations into one) • R : A1xA2x...xAm-pxAm-p+1x...xAm; S : B1 xB2x..Bpx Bp+1x ..xBm; p ≤ min(m,n). ==> Jp(R,S) = {(a1,...,am-p, c1,c2,...,cp, bp+1,..., bn) | $ (a1,...,am-p, c1,c2,...,cp ) Î R /\ (c1,c2,...,cp, bp+1,..., bn) Î S } Ex: R={(1,2,3), (2,2,3), (3,3,3) (6,1,2)}; S={(2,3,4), (3,3,3), (2,2,2)} ==> J2(R,S) = ? J1(R,S) = ? J3(S,R) = ?

  22. A B A->B 1 1 a a 2 2 b b 3 3 6.3 Relation representation • Three possible ways to represent a finite relation • a list (or array) of ordered pairs. • (0-1) matrix • digraph (directed graph) Ex: A={1,2,3}, B={a,b}, R = {(2,a),(3,a),(3,b)}. 1. list representation: ((2,1),(3,1),(3,2)) possibly ordered by fields. 2. Matrix (table): 3. Digraph: A\B a b 1 0 0 or 2 1 0 3 1 1

  23. Formalization of relation representation • A={a1,a2,..,am}; B = {b1,...,bn}; R: a binary relation from A to B. MR (the matrix representation of R) = (mij) mxn where mij = 1 if (ai,bj) ∈ R and = 0 if (ai,bj) ∉ R. Ex: A={a1,a2,a3}; B = {b1,b2,...,b5} If R1={(a1,b1),(a1,b2),(a3,b2),(a3,b5)} ==> MR1 = ? 01000 If MR2= 10110 ==> R2 = ? 10101 Note: 1. If R ∈ AxA with |A| = n, ==> MR is a nxn 0-1 matrix. 2. R is ref. (resp. irref. ) iff mii = 1 (0) for every 0<i < n+1. 3. R is symmetric (asym) iff for all i, j mij = mji (mij ≠ mji) 4. R is tansitive iff for all i,j,k if mij=mjk=1 => mik = 1.

  24. 0-1 matrix operation • Boolean operation on {0,1} • x ∨ y = max(x,y) • x ∧ y = min(x,y) • ~x = 1-x • Boolean operation on 0-1 matrices: • (M1 \/ M2) ij = M1ij \/ M2ij for all i,j. --- pairwise or • (M1 /\ M2) ij = M1ij /\ M2ij for all i,j. --- pairwise and • (~M1) ij = 1- M1ij for all i,j. -- complement. • (M1x M2)ij = \/k=1,n M1ik /\ M2kj. --- product • M0 = I; Mn+1 = Mn x M. --- power of 0-1 matrix.

  25. Matrix representation for relation operations • R1, R2: two relations from A to B. 1. MR1 UR2 = MR1 ∨ MR2. 2. MR1ÇR2 = MR1 ∧ MR2 3. M~R1 = ~MR1. • R: AXB; S: BxC with A = {a1,...,am}, B = {b1,...,bn} and C= {c1,..,cp}. ==> RS = {(ai,ck) | $bj ∈ B s.t. (ai,bj)  R and (bj,ck)  S } Hence (MRS)ik = 1 iff there is 1  j  n s.t. (aj,bj)  R and (bj,ck)  S iff there is 1  j  n s.t. (MR)ij = 1 /\ (MS)jk = 1 iff \/j=1,n (MR)ij /\ (MS)jk = 1 • Hence MRS = MR x MS. ==> MRk = (MR)k for any k  0.

  26. Examples: Ex3: MR = [110; 111; 011] ==> Is R ref, symmetric or antisymmetric ? Ex4: R1,R2: two relation on A. MR1= [101;100;010]; MR2 = [101;011;100]. ==> MR1UR2 = ? MR1Ç R2 = ? M~R1 = ? Ex5: MR = [101;110;000]; MS = [010; 001;101] => MRS = ? Ex6: MR = [010;011;100] ==> MR2 = (MR)2 = ?

  27. b a d c Representing relations using digraphs • Def 1. A diagraph (or directed graph) is a pair (V,E) where • V is a set, each member is called a node or a vertex. • E is a set of ordered pairs of nodes. (i.e. E Í V2), each member of which is called an edge. • If (a,b)  E is an edge, we say a is the initial (or starting) vertex and b the terminal (or ending) vertex of the edge. • If (a,a) is an edge, we say it is a loop. Ex7: In Fig 3, we have G=(V,E) where V= {a,b,c,d} and E={ (a,b),(a,d),(b,b)(b,d), (c,a),(c,b),(d,b)}. Note: E in fact is a relation on V. Fig 3: (p 377)

  28. Representing relations by digraphs (cont'd) • A = {a1,...,am}, B = {b1,...,bn} R: a binary relation from A to B ==> we can use the digraph G=(V,E) to represent R, where • V= A U B. • E = R. Ex: A = {1,2,3} ; B = {2, 3,4} R = {(x,y) | x > y} ⊆ AXB. ==> R = ? The digraph GR for R = ({1,2,3,4}, R). ==> What is its graphical representation ?

  29. 6.4 closure of relations • R: a relation on A • P(-): a property (or statement or predicate) about relations. eg: 1. P(R) = " R is reflexive" or 2. P(R) = " R is symmetric and transitive" or 3. P(R) = " |R| is not finite" etc. Def: P: a property about relations on A; The operation CLp: 2AxA -> 2AxA is defined to be the function s.t. for each each relation R on A, CLP(R) is the smallest of all relations on A which includes R and satisfies the property P. (i.e., CLp(R) is the relation on A s.t. • 1. R Í CLP(R) --- including R • 2. P(CLP(R)) is true --- satisfying P • 3. For any relation S on A, if it satisfies (1) and (2), • then CLP(R) Í S. --- smallest • CLp(R) is called the P closure of R.

  30. Properties about closure Ex: A = {1,2,3}; R={(1,1),(1,2),(2,1),(3,2)} Let P(S) means " S is reflexive" ==> CLp(R) (reflexive closure of R) = ? (exist ? unique ?) 1. Let TT = { T | T ÍA2 /\ T is ref. and R Í T}. Note: by def. every T ∈ TT satisfies item (1) and(2) of the def. of P-closure. ==> TT = ? 2. Which is the smallest in TT ? (= R U IA !) • Theorem: CLP(R), if existing, is unique. pf: Let S and T be any two P-closures of R. ==> By (3) S ÍT and T Í S, hence S = T. 3. Problem: Does CLP(R) always exist ? (no!) Does the irreflexive closure of R exist in the example ?

  31. closure properties Def: P: a property about relations P is said to be increasing if for each relation R, there exists a relation S satisfying P and including R. P is said to be closed under intersection(or conjunctive) if for any set TT of relations, if all R Î TT satisfies P, then so is their intersection Ç TT. Ex: The following properties are increasing and closed under intersection: • 1. reflexivity; 2. symmetry; 3. transitivity. • Since A2 (the largest relation on A) includes R • and satisfies 1,2,3. • Theorem: If P is increasing and closed under intersection, then CLp(R) always exist for all relations R (on a universe set A).

  32. Example: • Ex: property increasing closed under intersection • ref. yes yes • symmetric yes yes • transitivity yes yes • irref. no(why?) yes(why?) • asymmetric ? ? • antisymmetric ? ? • equivalence ? ? • partial order ? ? • total ordering ? ? • ref or symmetric yes no (why?) Fact: If P is not increasing, then CLp may not exist. If P is not conjunctive,then CLp may not exist.

  33. reflexive and symmetric closures of relations Theorem 1': R: a binary relation on A, then 1. reflexive closure of R (Rref) = R U IA. 2. symmetric closure of R (Rsym) = R U R-1. Ex: if MR = [100,101,000], then 1. IA = ? 2. MRref = ? 3. MR-1 = ? 4. MRsym = ?. pf: Let Tref = {S Í R | S is a relation on A and is ref.} ==> 1. (1) RUIAÎ Tref (2) S Î Tref ==> SÍ R U IA. Hence R U IA = CLref(R). 2. Similar to 1.

  34. 6 2 1 3 5 4 Transitive closure of relations • R: a binary relation on A ==> What is the transitive closure of R (i.e., the smallest transitive relation S on A including R (i.e., if (a,b) and (b,c) Î S, then (a,c) is also Î S)). Ex: If R = {(1,2)(2,6)(6,1)(1,3)(3,4)(4,5)} ==> GR = Fig 1. ==> MR = ? ==> GS = ? ==> MS = ? ==> S = ? • The reflexive and transitive closure of R is the smallest reflexive and transitive relation on A including R.

  35. Path Def. 1: G = (A,R): a digraph; a, b : two vertices in A. 1. A path of length n (n  0) from a to b is a sequence x0,x1,...,xn of vertices s.t. • x0 = a (starting point) and xn = b (ending point) and • (xi, xi+1) Î R for all i = 0,..,n-1. 2. If x0 = xn and n > 0 ==> the path is called a cycle ( or circuit). Ex: If R = {(1,2),(2,1),(2,3)(3,4)(4,2)} ==> "1,2,3,4,5" is a path of length ? from ? to ?.

  36. Correspondence b/t relation and digraph Theorem 1: |A|= m; R is binary relation on A; (Hence G=(A,R) is a digraph) ==> for all n ³ 0, (x,y) Î Rn iff $ a path of length n from x to y. pf: by induction on n. Basis: n = 0 (and R0 = IA = {(x,x) | x Î A}) $ path of length 0 from x to y iff x=y iff (x,y) Î IA. Ind. case: n = k+1. $ a path of length n from x to y <=> (by def) $ a path of length k from x to z and $ a path of length 1 from z to y for some z  A <=> (by ind. hyp.) $ z Î A s.t. (x,z) Î Rk /\ (z,y) Î R <=> (by def) (x,y)  Rn. QED

  37. correspondence b/t relation and digrqph Def. 2: R: a binary relation on A (hence (A,R) is a digraph). R+ =def {(x,y) $ a path of length > 0 from x to y in (A,R)} (i.e., R+ = Uk=1,¥ Rk) R* =def {(x,y) | there is a path of length ³ 0 from x to y} =Uk=0,¥ Rk = R+ U {(x,x) | x in A} = R+ U IA. Theorem 2: 1. R+ is the transitive closure of R. 2. R* is the reflexive and transitive closure of R. Note: If R encodes the property that " (a,b)  R iff b can be reached from a by 1 step" then 1. (a,b) ∈ Rk means "b can be reached from a by n steps." 2. (a,b) ∈ R* means " b is reachable from a" or means " b is connected to a".

  38. Proof of Theorem 2: pf: (1) left to the students. (2). 1. R ÍR* ; 2. R* is ref. and transitive. 3. If T is another relation with property 1 & 2, then R* Í T. 1. R Í R* and R* is ref.: trivial, since R* = IA U R U R2 U ... 2. R* is transitive: (x,y) Î R* and (y,z) Î R* => $m and n ³ 0 s.t. (x,y) Î Rm and (y,z) Î Rn => (x,z)Î Rm+n => (x,z) Î R*. 3. Let T be any ref. and tran. relation including R. we show by induction on n that Rn ÍT for all n  0. Hence R* = R0 U R UR2U... Í T. case 1: n = 0 : R0 = IA Í T (since T is reflexive). case 2 : n = 1 : R Í T (since T includes R) case 3: n = k+1 > 1: by ind.hyp., RJ Í T for all J < n. ==> if (x,y) Î Rn => $z s.t. (x,z) Î Rk and (z,y) Î R => (by ind. hyp.) (x,z)Î T and (z,y)Î T => (by tran. of T) (x,y)Î T.

  39. Some lemmas Lem 1: |A|= n > 0, R: a relation on A. 1. If there is a path of length m > n from a to b, then there is a path of length 0 < p < m from a to b. 2. If there is a path of length > 0 from a to b then there is a path of length 0 < p  n from a to b. pf: 1. Let a=x0, x1,x2,..., xm-1,xm=b be the path of length m from a to b. Since m > n, by pigeonhole principle, there is 1 i<j  m s.t. xi = xj. ==> The path x0,x1,..,xi, xj+1,...,xm is a path from a to b with length m + (j-i) < m. 2. Let a0 be any path from a to b. if | a0 | ≤ n, we are done. O/w, by 1. we can construct a path a1 with length | a1 | <| a0 |. if | a1| ≤ n, then we are done. O/w, repeat the same process, we can obtain a sequence of path a0, a1, a2,... from a to b with | a0 | > | a1 | > ... But since | a0 | is finite, we will eventually find a path ak with length  n . QED

  40. Corollaries lemma: If a ¹ b and there is a path of length m  n, then there is a path of length 0 < p < n from a to b. pf: similar to the previous one. Corollary: |A| = n. 1. R+ = Uk= 1, n Rk. 2. R* = Uk=0,n Rk. Pf: (1.) Since ∀m > n, RmÍ RUR2U...URn. (x,y) Î Rm <=> $ a path from x to y of length m > n => $ a path from x to y of length 0 < p  n <=> (x,y) Î Rp <=> (x,y) Î RUR2U...URn. (2.) Similar to 1. Corollary: R* = Uk=0,n-1 Rk. Pf: Left as an exercise. Hint: RnÍ IA U Uk=1,n-1 Rk. (x,y) ∈ Rn ==> (x=y /\ (x,y) Î IA) or (xy /\ (x,y) Î Uk=1,n-1 Rk). Theorem: 1. MR+ = MR \/ MR2 \/...\/MRn. 2. MR* = MR0 \/ MR \/ MR2 \/...\/MRn-1.

  41. Algorithm for transitive closure • TR(MR: a 0-1 nxn matrix) • 1. A := MR; • 2. B := A • 3. for i = 2 to n do • 3.1 A := A x MR// A = MRi. • 3.2 B := B \/ A // B = Uk=1,i MRk. • 4. return(B) // B = MR+. • Exercise: Give an alg. for finding MR*. • Complexity: • each execution of step 3.1 takes time O(n3) • Total execution time = O(n4). • ==> more efficient algorithm ( O(n3) ) is possible!!

  42. Warshall's algorithm • Applying dynamic programming technique • running time O(n3). • R: a relation on A = {v1,v2,...,vn}. • note: (MR)ij = 1 iff (vi,vj)  R. • W0,W1,...,Wn: a sequence of nxn 0-1 matrices, where • (WK)ij = 1 ( 0 £ k £ n ) iff there is a path of length >0 from vi to vj s.t. all interior vertices of this path are in the set {v1,...,vk}. • (i.e., $ a path vi,u1,u2,...,us,vj (s³ 0) s.t.{u1,...,us}Í {v1,...,vk}. Facts: 1. W0 = MR. [ since (vi,vj) Î R <=> $ a path of length 1 from vi to vj (w/o interior vertex) <=> (i,j) Î W0. ] 2. Wn = MR+. [ Since (vi,vj) Î R+ <=> $ a path of length > 0 from vi to vj <=> $ a path of length > 0 from vi to vj with interior vertices among the set {v1,v2,...,vn} <=> (i,j)Î Wn. ]

  43. Warshall's algorithm (cont'd) Fafct 3: Wk+1 can be defined in terms of Wk. Lem 2: (Wk+1)ij = (Wk)ij \/ ((Wk)i(k+1) /\ (Wk)(k+1)j ) pf: (Wk+1)ij = 1 <=> (by def.) $ a path a=vi,u1,...,us, vj from vi to vj with {u1,...,us} Í{v1,...,vk+1}. <=> two possible cases: case 1: no ui (i= 1,..,s) is vk+1 (i.e., vk+1Ï {u1,...,us}). ==> a is also a path from vi to vj with interior vertices from {v1, ...,vk} ==> (Wk)ij = 1.

  44. Warshall's algorithm (cont'd) case 2:$ a path a = vi,u1,...,us, vj from vi to vj with some ui = vk+1. (i.e., vk+1 ∈ {u1,...,us}). Now let IDX = {i | ui = vk+1} and let p = min(IDX); q = max(IDX). Note that p will equal to q iff |IDX| = 1. Now the path b = v1,u1,...,up-1,up,uq+1,...,us,vj is a path from vi to vjcontaining only one vk+1 = up. ==> v1,u1,..,up ia a path from v1 to vk+1 and up,u q+1,..,vj is a path from vk+1 to vj ==> (Wk)i(k+1) = (Wk)(k+1)j = 1. <=> (Wk)ij \/ ((Wk)i(k+1) /\ (Wk)(k+1)j = 1. QED

  45. Warshall's algorithm (cont'd) EX: Let MR = [0001,1010,1001,0010]. find W0,W1,W2,W3,W4. The Warshall's algorithm: Procedure WS1(MR: nxn 0-1 matrix) 1. W0 := MR; 2. for k := 1 to n do // compute Wk 3. for i := 1 to n do 4. for j = 1 to n do // compute (Wk)ij 5. Wk[i,j] := Wk-1[i,j] \/ (Wk-1[i,k]/\Wk-1[k,j] ); 6. Return(Wn) // Wn = MR+. analysis: 1. The algorithm takes time O(n3). 2. The algorithm require memory space O(n3). ==> can be improved to O(n2) by collapsing all Wk into one W. (and become the final Warshall's algorithm shown in the book). (why? since Wk[i,k]=Wk-1[i,k] /\ Wk[k,j]=Wk-1[k,j] )

  46. 6.5 Equivalence relations Def. 1: R: a relation on a set A. • R is an equivalence relation on A iff it is ref. symmetric and transitive. • x and y in A are said to be equivalent (or R-equivalent) if (x,y)  R. Ex1: R: a relation on the set of all bit strings {0,1}* defined by xRy iff |x| = |y|. ==> 1. R is an equivalence relation on {0,1}* ?. 2. 00110 and 11100 are R-equivalent since both have the same length. Ex 2: R: a relation on integers s.t. xRy iff x=y or x = -y. ==> R is an equivalence relation ? Ex3: R: a relation on real numbers s.t. xRy iff x-y is an integer. ==> R is an equivalence relation ?

  47. More equivalence relations Ex5: [congruence modulo m](共餘) m: a positive integer > 1. ºm: a binary relation on Z s.t. x ºm y iff m divides (x-y). ==> ºm is an equivalence relation on Z. pf: Equivalence classes: Def 2: R: an equivalence relation on A; a : any element of A. ==> the equivalence class of a w.r.t. R, denoted [a]R, is the set of all elements of A equivalent to a, i.e., [a]R = { b | aRb ∧ b Î A}. • If b  [a]R, then say b is a representative of the class [a]R.

  48. Equivalence classes Ex 5: R: a binary relation on Z s.t. xRy iff x = y or x = -y. => 1. R is an equivalence relation. 2. For each x in Z, [x]R = {x, -x}. 3. e.g., [3]R = { y Î Z | 3 = y or 3 = -y} = {3, -3}. Ex6: R Z2 s.t. xRy iff x º y (mod 4). => [0]R = {y ÎZ: 4 | (0-y) } = {..., -8, -4, 0, 4, 8 ,...} [1]R = {y ÎZ: 4 | (1-y) } = {..., -7, -3, 1, 5, 9 ,...}, [2]R = {y ÎZ: 4 | (2-y) } = {..., -6, -2, 2, 6, 10 ,...}, [3]R ={y ÎZ: 4 | (3-y) } = {..., -5, -1, 3, 7,11,...} [4]R = {y ÎZ: 4 | (0-4) } = {..., -8, -4, 0, 4, 8 ,...}= [0]R. [5]R = [1]R, [6]R = [2]R,... => 1. [i]R = [i+4k]R, where i and k are any integers. 2. There are only 4 equivalence classes w.r.t. R. i.e., {[a]R | a Î Z} = {[0]R, [1]R, [2]R, [3]R}= { {...,-4,0,4,..}, {...,-3,1,5,..}, {...,-2,2,6,...}, {...,-1,3,7,...}}.

  49. Congruence relation 3. the class [a]R is called a congruence class (modulo m). 4. For any integer x, xÎ [y]R iff xRy. Quotient set: A: a set; R: an equivalence relation on A. The quotient of A w.r.t R, denoted A/R, is the set of all R-equivalence classes of A. (i.e., A/R = {[x]R | x Î A} ). Ex: The set Z/º4 = {[0]4,[1]4,[2]4,[3]4}. in general we have Z/ºn = {[0]n,[1]n,...,[n-1]n}. Ex: |A| = n; R: an equivalence relation on A s.t. |[a]R| = k for all a Î A. ==> | A/R | = ___

  50. x x1 y y1 Equivalence classes and partitions Lemma 1: R: an equivalence relation on A. Then for any x,y Î A, 1. x R y iff [x]R = [y]R iff [x]RÇ [y]R ¹Æ. 2. not x R y iff [x]R ¹ [y]R iff [x]RÇ [y]R = Æ. 3. Every class [x] is not empty and every x belongs to exactly one class. pf: 1. (=>:) x R y => [x] = {b | xRb} = {b | yRb} = [y] => since [x]=[y] and x Î [x], [x]RÇ [y]R ¹Æ . 1. (<=:) let z in [x]RÇ [y]R ¹Æ. => [x] = {b | xRb} = {b | zRb} = {b |yRb} = [z]. => y Î [x] => xRy. 2,3: direct from 1. not xRy xRy A

More Related