820 likes | 1.19k Views
Chapter 8. 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. 8.1 Relations and their properties. Def. 1: A, B: two sets.
E N D
Chapter 8 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
8.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, ...
Relations on a set Ex3: A = {1,2,3,4}; B={a,b} ==> {(1,a),(1,b),(2a),(3,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)?
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 = ____
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.
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. ?
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 ?
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!)
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} Notes: 1. The textbook use SR (less popular) instead of R·S to denote the composition of R and S. 2. R·S can be abbreviated as RS.
1 0 1 2 2 1 3 2 3 4 R S C A B Examples of relation compositions Ex20: 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= {(1,0),(1,2),(2,1),(2,2),(3,0),(3,2) } Fact: Relation composition is associative (i.e., R·(S·T) = (R·S)·T ) Hence we can write RST w/t parentheses.
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 }. Lemma 1: T Í AxB, S Í BxA are two relations . Then 1. IA·T = T; 2. S·IA = S. • Pf of 1: For all (x,z)ÎAxB, • (x,z) ∈ IA·T iff ($yÎA, (x,y)ÎIA and (y,z)ÎT) • iff (y = x and (y,z) ÎT) iff (x,z) ÎT. Hence R1 = R R0 = R IA = R. R2 = RR1=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 • = Rb -- by lemma 1 • = R (0+b). • Ind. step : assume RaRb= Ra+b => • Ra+1Rb = (RRa)Rb = R(RaRb) --- (Assoc of Relations) • = RRa+b = Ra+1+b. Corollary: RRk = Rk R. // = R k+1
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 2: If Rn+1 = Rn then Rk+1 = Rk for all k n. Ex: If R6 = R5 => R10 = R6R4 = R5R4 = R9 Pf: By ind on (k-n). Basis: k-n = 0. Then Rk+1 = Rn+1 = Rn = Rk. Inductive step. : k-n = t+1 >0. By IH, we have R(n+t)+1 = R n+t. Then. Rk+1 = R(n+t+1)+1 = R R(n+t+1) = RR(n+t) = R(n+t)+1 = Rk Corollary 3: If Rn+1 = Rn => Rk = Rn for all k > n. Ex: R6 = R5 => R10 = R9 = R8 = R7 = R6 = R5. Pf: Rk = Rk-1 = R(k-1)-1 = Rk-2 = ...=Rn+1 = Rn.
Lemm3: R is transitive iff R2 Í R. Pf: (=>): Assume R is transitive. Then (x,z)R2 =>y with (x,y) R /\ (y,z) R => (x,z) R --- by transtivity of R (<=): Assume R2Í R. Then for all x,y,z: If (x,y) R /\ (y,z) R => (x,z) R2 => (x,z) R --- by premise Hence R is transitive. Theorem 1: Relation R on a set A is transitive iff (RnÍR for all n>0). Pf: (=>) : Basis: n = 1 => Rn = R1 = R ⊆ R. Ind. step: for any pair (x,z), (x,z) Î Rn+1 => $y s.t. (x,y) Î R /\ (y,z) Î Rn. But, by ind. hyp., both (x,y) and (y,z) Î R. And by transtivity of R, (x,z) Î R. (<=) : Direct from Lemma3. QED
8.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.
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 or view 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.
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.
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 ...
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,...)
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) }
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)}
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) = ?
A B A->B 1 1 a a 2 2 b b 3 3 8.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,a),(3,a),(3,b)) possibly ordered by fields. 2. Matrix (table): 3. Digraph: A\B a b 1 0 0 or 2 1 0 3 1 1
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
Notes • If R ∈ AxA with |A| = n, ==> MR is a nxn 0-1 matrix. • R is ref. (resp. irref. ) iff mii = 1 (0) for every 1i n. • R is symmetric (asym) iff for all i, j mij = mji (mij /\ mji ≠ 1) • R is tansitive iff for all i,j,k if mij=mjk=1 => mik = 1.
0-1 matrix operation • Boolean operation on {0,1} • x ∨ y = max(x,y) • x ∧ y = min(x,y) • ~x = 1-x • Boolean operations 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.
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, and MRk = (MR)k for any k 0.
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 = ?
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 541)
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 ?
8.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 relation R on A, CLP(R) is the smallest of all relations on A which include R and satisfy the propertyP. (i.e., CLp(R) is the relation C on A s.t. • 1. R Í C --- including R • 2. P(C) is true --- satisfying P • 3. For any relation S on A, if it satisfies (1) and (2), • then C Í S. --- smallest • CLp(R) is called the P closure of R.
Theorem: CLP(R), if existing, is unique. pf: Let S and T be any two P-closures of R. ==> By minimality (3) of closure : S ÍT and T Í S, hence S = T. 3. Problem: Does CLP(R) always exist ? No ! Ex: R = { (a,a), (a,b) } has no irreflexive closure. Why ? Ans: Since the “irreflexive” property and R are contradictory. (I.e., No superset of R could be irreflexive. )
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).
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.
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 = ? =[100,010,001] 2. MRref = ? =[100,101,000]\/[100,010,001] =[100,111,001] 3. MR-1 = ? = [110,000,010] 4. MRsym = ? = [110,000,010]\/[100,101,000]=[110,101,010] pf: Let Tref = {S| S is a relation on A and is ref and R Í S. } ==> 1. (1) RUIAÎTref (2)SÎTref =>RUIAÍS. Hence R U IA = CLref(R). 2. Let Tsym = {S| S is a relation on A and is sym and R Í S. } ==> 2. (1) RUR-1ÎTsym . ( (x,y)Î RUR-1 => (y,x) ÎR-1UR = RUR-1.) (2) SÎTsym => RUR-1 Í S. (R-1 Í S : (x,y)ÎR-1=>(y,x)ÎR =>(y,x)ÎS =>(x,y)ÎS.) Hence RUR-1 = CLsym(R).
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.
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)(4,5)} ==> "1,2,3,4,2" is a path of length ? from ? to ?. 5 4 3 2 1
Correspondence b/t relation and digraph Theorem 1: |A|= m; R is a binary relation on A; (Hence G=(A,R) is a digraph) ==> 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
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 k steps." 2. (a,b) ∈ R* means " b is reachable from a" or means " b is connected to a".
Proof of Theorem 2: pf: (1) left to the students. (2). Properties needed to be proved: 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.Smallest of R* Let T be any ref. and tran. relation including R. We will show by induction on n that Rn ÍT for all n 0. Hence R* = R0 U R U R2 U... Í 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., Rk Í T. => if (x,y) Î Rn => $z s.t. (x,z)ÎR and (z,y) Î Rk => (x,z)Î T and by ind. hyp., and (z,y)ÎT => By transitivity of T, (x,y)Î T.
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 1 p <m from a to b. Ex: In the Figure, there is a path 1 2 1 2 3 4 2 3 4 5 of length 9 > 5. By this lemma, we should have another one shorter than it. (ex: 1 2 1 2 3 4 5) 2. If there is a path of length > 0 from a to b then there is a path of length 1 p n from a to b. 5 4 3 2 1
x1 x0 (台北) xi (台中) xi +1 (南投) xj (台中) xj+1 (彰化) xm (高雄) The proof • Let a=x0, x1,x2,..., xm-1,xm=b be the path of length m > 1 from a to b. ==> {x1,x2,..., xm-1,xm } A. Since m > n = |A|, by pigeonhole principle, there are 1 i<j m s.t. xi = xj. ==> The path x0,x1,..,xi, xj+1,...,xm is a path from a to b with shorter length m + (j-i) < m.
2. Let a0 be any path of length > 0 from a to b. If | a0 | ≤ n, we are done. O/w, by 1. we can construct a path a1 with length 0 < | 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 | > ... >0 But since | a0 | is finite, we will eventually find a path ak with length ≤ n . QED
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: (Same as Lem1 except that now x0 is taken into account when applying pigeonhole principle ) 1. Let a=x0, x1,x2,..., xm-1,xm=b be the path of length m from a to b. Since m ≥ n = |A|, and {x0, x1,x2,..., xm-1,xm} A, by pigeonhole principle, there is xi = xj for some 0 i<j m. ==> The path x0,x1,..,xi, xj+1,...,xm is a path from a to b with length m + (j-i) < m. 2. same as Lem 1.
Corollaries lemma: If a ¹ b and there is a path of length m n from a to b, then there is a path of length 0 p < n from a to b. pf: similar to previous Lemma (1.1+1.2) with the note that, m n =|A|, x0 xm, and {x0, x1,x2,..., xm-1,xm} A. By pigeonhole principle, there is 0 i<j m s.t. xi = xj where (i,j) (0,m). Hence 0 < (j-i) < m and the resulting path has shorter size 0< m –(j-i) < m. Corollary: |A| = n > 0. 1. R+ = Uk= 1.. n Rk. Pf: (1.) It suffices to show that ∀m > n, RmÍ RUR2U...URn. (x,y) Î Rm => $ a path from x to y of length m > n > 0 => $ a path from x to y of length 0 < p n => (x,y) Î Rp => (x,y) Î RUR2U...URn.
Corollary: 2. R* = Uk=0..n-1 Rk. Pf: Same as 1 but using Lem1’.2. i.e., It suffices to show that ∀m ≥ n, RmÍ R0UR1U...URn-1. Theorem: 1. MR+ = MR \/ MR2 \/...\/MRn. 2. MR* = MR0 \/ MR \/ MR2 \/...\/MRn-1.
Algorithm for transitive closure • TR(MR: a 0-1 nxn matrix) • 1. A := MR; • 2. B := A • 3. for i = 2 to n do // A = MRi-1 ; B = Uk=1,i-1 MRk. • 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 algorithm for finding MR*. • Ans: Step 2: B = A U IA step 3: for I = 2 to n-1 • Complexity: • Each execution of step 3.1 takes time O(n3) • Total execution time = O(n4). • ==> more efficient algorithm ( O(n3) ) is possible!!