Presentation is loading. Please wait.

Presentation is loading. Please wait.

Computing Fundamentals 1 Lecture 7 Relations Lecturer: Patrick Browne Room K308 Based on Chapter 14. A Logical approach.

Similar presentations


Presentation on theme: "Computing Fundamentals 1 Lecture 7 Relations Lecturer: Patrick Browne Room K308 Based on Chapter 14. A Logical approach."— Presentation transcript:

1 Computing Fundamentals 1 Lecture 7 Relations Lecturer: Patrick Browne http://www.comp.dit.ie/pbrowne/ Room K308 Based on Chapter 14. A Logical approach to Discrete Math By David Gries and Fred B. Schneider

2 The term tuple is used to describe a row in a database. Tuples are record like structure e.g.. Cross products represent the types (or sorts in CafeOBJ) in a tuple e.g. String  Nat Relations describe relationships between objects. Functions (covered in next lecture) are relations with some special properties. Relations and Functions

3 Given expressions b and c then  b,c  is called an ordered pair. Well known ordered pairs are  x,y  coordinates in the plane, or  name,address  in a database. Axiom, Pair equality:  b,c  =  b’,c’   b = b’  c = c’ A 2-tuple is called an ordered pair. Tuples 1

4 Cross Products The Cross product (or Cartesian product) S  T of two sets S and T is the set of all ordered pairs  b,c  such that b is in S and c is in T. Axiom Cross product (used in later proof): S  T = {b,c | b  S  c  T :  b,c  } Cross products can be extended to n sets producing n-tuples.

5 Cross Products A 2-tuple: S = {2,5} T = {1,2,3} S  T = {  2,1 ,  2,2 ,  2,3 ,  5,1 ,  5,2 ,  5,3  }

6 Cross Products Example of a 2-tuple: S = {Mary,John} T = {John,Bill,Brent} S  T = {  Mary, John ,  Mary,Bill ,  Mary,Brent ,  John, John ,  John,Bill ,  John,Brent  }

7 Cross Products Example S  T =/= T  S : S = {Mary,John} T = {John,Bill,Brent} T  S = {,,,,, } S  T = {  Mary, John ,  Mary,Bill ,  Mary,Brent ,  John, John ,  John,Bill ,  John,Brent  }

8 Some Theorems for Cross products Membership:  x,y   S  T  x  S  y  T Distributivity of  over  S  (T  U)  (S  T)  (S  U) (S  T)  U  (S  U)  (T  U) Monotonicity T  U  (S  T)  (S  U)

9 Relations A relation is a subset of a cross product B 1  B 2  …  B n A binary relation over B  C is a subset of B  C. It is possible to have a relation on B  B. Well know relation lessThan(i,j) is difficult to enumerate (or list), so use: {i,j :  | j-i is positive :  i,j  }

10 Relations A relation can be represented by: A Set of ordered pairs A table e.g. a tuple in a database A directed graph A set comprehension; a rule based description similar to the quantifications of Chapter 8 Two notations for membership of relation   b,c    (set of pairs) b  c (infix, conjunctional)

11 Relation as Directed Graph Set = {1,2,3,4,5,6} Relation = {,,,, } 1 5 4 3 2 6 One vertex for each element of the set. There is a directed edge from each vertex b to vertex c iff is in the binary relation.

12 Diagraph of Relations Given the relation R ={,,,, } On the set {1,2,3} Below is the digraph of R. 1 2 3

13 Digraph Relations is a set of ordered pairs: (a)P = {,, } (b)Q = {, } 1 3 4 2 1 4 2 (a) (b)

14 Digraphs Relations as sets of ordered pairs and digraphs: (c) R = {,,, } (d) S = {,, } 1 3 4 2 1 2 3 (c)(d)

15 A set of ordered pairs A Set of Relations (Ordered Pairs) With sets order is not significant {,, } = {,, } With relations order is significant =/= {,, }=/={,, } 1 2 3 1 2 3

16 Relations on Sets The relation R on {1,2,3,4} is defined by  R if x 2  y. The relation R can be written as a set of ordered pairs: R = {,,,,,,,,,,,, }

17 Some Relations The empty relation on S  T is the empty set . The identity relation i B on B is { x | x  B :  x,x  } The relation parent, b is a parent of c. The relation predecessor ( pred(x) ). The relation square root ( see book 1 ). The algorithm relation between input and output state.

18 Relations Lower case Greek letters represent relations. The Greek letter  is pronounced rho. The Greek letter  is pronounced sigma. The domain, Dom. , and range Ran.  of a relation  on B  C are defined as: –Dom.  = { Õ b:B | (  c |: b  c)} –Ran.  = { Õ c:C | (  b |: b  c)}

19 Relations Dom Ran  B C Dom.  = {b:B | (  c|: b  c)} Ran.  ={c:C | (  b|: b  c)}

20 Operations on Relations Let  be a relation on B  C and  be a relation C  D. The product (or composition) of  and  denoted    is defined by:  b,d       (  c | c  C :  b,c      c,d    ) B C D b c d   Alternative notation b(    )d holds iff b  c  d holds for some c.

21 Composing Relations

22 Powers of a relation For example parent  parent can be written parent 2.  0 = i B (the identity relation on B )  n+1 =  n   (for n  0)  n+m =  n   m  n  m = (  n ) m

23 Classes of relations NameProperty Reflexive (  b| b  b) Irreflexive (  b|  (b  b)) Symmetric (  b,c | (b  c)  (c  b))

24 Classes of relations Antisymmetric (  b,c | (b  c)  (c  b)  b=c) Asymmetric ( non-symmetric, see notes section ) (  b,c | (b  c)   (c  b)) Transitive (  b,c,d | (b  c)  (c  d)  b  d)

25 Relations Discussion Common relations

26 Relations Discussion  is a relation is on a set, A = {1,2,3} For  to be either reflexive and irreflexive it must hold for all of A. For  to be reflexive then  must contain the following ordered pairs {,, } The relation < (less than) is irreflexive. The relation = (equal to) is reflexive, transitive, symmetric, and antisymmetric

27 Relations Discussion Consider the relation square b square c iff b = c * c square is not reflexive because it does not contain { } square is not irreflexive because it does contains pair { } Thus square is neither reflexive or irreflexive. A relation that is not reflexive need not be irreflexive.

28 Relations Discussion For symmetric relations  does not have to hold for all of A, (b  c) need only hold where (c  b) holds and visa versa. The relation  on {1,2,3}  = {,, } is neither symmetric nor antisymmetric. It is not symmetric because there is no to match the (1,3). It is not antisymmetric because it has both and, but 1  2. Antisymmetric allows.

29 Relations Discussion The subset (  or  ) relation is antisymmetric. If A and B are two sets, and if A is a subset of B and B is a subset of A then A=B. (A  B)  (B  C)  (A = B) In general, a relation is antisymmetric if there is no pair of distinct elements of X each of which is related by R to each other.

30 Relation Properties S = { 1, 2, 3}. R2 = {,,, }. reflexive, yes symmetric, no, has and not Antisymmetric, yes, because it is not symmetric and has symmetry on. Asymmetric (which precludes ), no, because it is antisymmetric (which allows ).

31 Relation Properties S = { 1, 2, 3}. R1 = {,,, }. reflexive, no because doesn’t hold symmetric, yes for all then asymmetric, no, because it is symmetric. antisymmetric, no because no, with a=b.

32 Relation closure Example Let R = {,, } be a relation on the set A={a,b,c}. The reflexive closure is: r(R) = {,,,, } The symmetric closure is: s(R) = {,,,, }

33 Relation Properties Let X be the set of all four bit strings. Define a relation R on X as  R if a substring of s 1 of length 2 is equal to a substring of s 2 length 2. Examples  R  R Is this relation reflexive, symmetric, antisymmetric, and/or a partial order?

34 Relation Properties Is  R reflexive? Yes. Example:  R Is  R symmetric? Yes. Example:  R   R Is  R antisymmetric? No, because it is symmetric.

35 Relation Properties Is  R transitive? No, here is a counter example: 1000 R 1011 R 0011 Is  R partial order? No, because it is not reflexive, not antisymmetric, and not transitive. Partial order is a property of certain relations, we will look this at later in the lecture.

36 Reflexive or Symmetric? On the set {1, 2, 3, 4} we have relation {(1,1),(1,3),(2,2),(2,4),(3,1),(3,3),(4,2),(4,4)} We can use a matrix to check whether the relation is reflexive and symmetric. Using matrix operations we can identify the presence of many relations (not covered here)

37 Closure The closure of a relation  with respect to some property (e.g. reflexivity) is the smallest relation that both has the property and contains . To construct a closure, add pairs to , but not too many, until it has the property. The less-than relation is not reflexive, but the less-than-or-equal-to relation is. For example, the reflexive closure of < over integers is the relation constructed by adding to the relation all pairs (b,b) for b an integer. Therefore  is the reflexive closure of <.

38 Closures Reflexive closure r(<) of < on integers is . parent is the relation “b is a parent or a child of c”. Symmetric closure s(parent) of parent is (parent  child), since if is in the symmetric closure so is. Transitive closure parent + of parent is ancestor, since if and is in the transitive closure so is. The reflexive transitive closure parent* of parent-or-self.

39 Various Closures(OUT) Reflexive Closure r(  ) –(    0 ) Symmetric s(  ) –(    -1 ) Transitive closure  + – (  1   2   3   4..  n ) Reflexive Transitive closure  * –(  0   1   2   3   4..  n )

40 Sensible Closures(OUT) Let  be a relation on a set. The reflexive (symmetric, transitive) closure of  is the relation  ’ that satisfies: –  ’ is reflexive (symmetric, transitive) –    ’ –If  ’’ is reflexive (symmetric, transitive) and    ’’ then  ’   ’’. (i.e. smallest subset)

41 Closures Let R = {,, } be a relation on the set A={1,2,3}. Calculate the following closures: the reflexive closure, the symmetric closure,

42 Closures Reflexive Closure: r(R) = {,,,, } Symmetric Closure: s(R) = {,,,, } R = {,, } A={1,2,3}.

43 Equivalence Relations A relation is an equivalence relation iff it is reflexive, symmetric and transitive (e.g. =). An equivalence relation  on a set B partitions the set into non-empty disjoint subsets. Elements that are equivalent under  are placed in the same partition. Elements that are not equivalent under  are placed in different partitions. For example:  b,c   sameEye  b and c have same eye colour

44 Equivalence Relations Let  be an equivalence relation on B. Then [b] , the equivalence class of b, is the subset of elements of B that are equivalent (under  ) to b. x  [b]   x  b The sets [b]  form an equivalence  relation on B partition of B. Thus an equivalence relation on B induces a partition of B, where each partition element consists of equivalent elements.

45 Equivalence Relations......... [b] green b [c] green [d] blue c d Objects b and c are in the same partition and [b]=[c]

46 Equivalence Relations [b] green [c] green [d] blue Objects b and c are in the same partition and [b]green=[c]green

47 Equivalence Classes 1 Theorem: The equivalence classes of an equivalence relation R partition the set A into disjoint nonempty subsets whose union is the entire set. This partition is denoted A/R and called, the quotient set, or the partition of A induced by R, or A modulo R. Definition: Let S 1, S 2,..., S n be a collection of subsets of A. Then the collection forms a partition of A if the subsets are nonempty, disjoint and exhaust A : S i   S i  S j =  if i  j  S i = A

48 Equivalence Relations The relation b = 4 c on integers 0..9(must be +) b = 4 c  (b – c) is a multiple of 4 Their difference is a multiple of 4 We have 4 partitions [0] = [4] = [8] = {0,4,8} [1] = [5] = [9] = {1,5,9} [2] = [6] = {2,6} [3] = [7] = {3,7}

49 Equivalence Relations Consider the relation  defined on the set of people by: – (b  c) iff b and c are female or b and c are the same person or b is c’ s sister. Relation  is reflexive, symmetric, and transitive, so it is an equivalence relation. For female b, [b] consists of b and b ’s sisters, while the equivalence for a male c consists of only that male c.

50 Equivalence Relations There is a correspondence between equivalence in relations and partitions in sets.  b,c   sameEye  b and c have same eye colour

51 Equivalence Relations Theorem 1: Let S be a partition of a set X. Define (x R y) to mean that for some S’ in S, both x and y belong to S’. Then R is reflexive, symmetric, and transitive. Stating this another way, a relation that is reflexive, symmetric, and transitive is an equivalence relation.

52 Equivalence Relation(*) Let S be a partition of X which gives rise to an equivalence relation R on X. X = {1,2,3,4,5,6} S = {{1,3,5},{4},{2,6}} partition X The relation R on X is an equivalence relation therefore R is reflexive, symmetric, and transitive. Draw S. List the ordered pairs of the relation R on X. Draw the diagraph of R.

53 Equivalence Relation(*) Draw S = {{1,3,5},{4},{2,6}}...... [2] 2 2 [6] 2 [4] 3 6 4 1 3 5 [1] 1 [3] 1 [5] 1 Venn Diagram The equivalence classes are subscripted according to their respective partitions

54 Equivalence Relation(*) List the ordered pairs of the relation R on X. R = {(1,1), (1,3),(1,5),(2,2),(2,6), (3,1),(3,3),(3,5),(4,4),(5,1), (5,3),(5,5),(6,2),(6,6)}

55 Equivalence Relation(*)

56 Graph of Equivalence Relation R = {(1,1), (1,3),(1,5),(2,2),(2,6), (3,1),(3,3),(3,5),(4,4),(5,1), (5,3),(5,5),(6,2),(6,6)} 1 3 5 6 2 4

57 Order Relations The order relation compares some members of a set. A typical order relation is < on integers. An order relation need not be a comparison of every member of a set, e.g. while A and B may be parents parent(A,B) may not hold.

58 Equivalence Relation(*) Draw P = {{a,c,e},{d},{b,f}}...... [b] 2 b [f] 2 [d] 3 f d a c e [a] 1 [c] 1 [e] 1 Venn Diagram The equivalence classes are subscripted according to their respective partitions

59 Equivalence Relation(*) Draw the diagraph of R. ac e f b d

60 Equivalence Relation(*) List the ordered pairs of the relation R on X. R = {(a,a), (a,c),(a,e),(b,b),(b,e), (c,a),(c,c),(c,e),(d,d),(e,a), (e,c),(e,e),(f,b),(f,f)}

61 Def. Partial Order Relation A binary relation  on a set b is called a partial order on B if it is Reflexive, Antisymmetric, and Transitive. is called a partially ordered set or poset. We will write partial orders using a  b and b  c. This notation differs from the course text book.

62 Partial Order Relation 1 Is this relation reflexive, antisymmetric, and transitive?

63 Hasse Diagrams Hasse diagrams are a form of simplified digraph that allow us to represent partial orders. They are simplified as follows. –Loops may be suppressed –Arrows not necessary, read from bottom to top. –Transitive arrows may be suppressed.

64 Order Relation on Power set. is a poset and  is a partial order on . Let B be a set. Then is a poset and  is a partial order on  B (the power set of B ), since is symmetric, antisymmetric, and transitive. Let C be the set of courses offered at DIT. Define the relation  by c1  c2 if c1=c2 or c1 is a prerequisite of c2. Then is a poset and  is a partial order on C.

65 Order Relation on Power set 1. A partially order set can be represented with using a POSET diagram. The POSET diagram on the right is based on the power set (all possible subsets) of the three element set {a, b, c}. These subsets form a special kind of partial order that is referred to as a lattice

66 Order Relation. The POSET diagram on the right is based on the power set (all possible subsets) of the three element set {1, 2, 3}.

67 Partial Order Relation on Divisibility 1. The set A = { 1, 2, 3, 4, 5, 6, 10, 12, 15, 20, 30, 60 } of all divisors of 60, partially ordered by divisibility.

68 Order Relations In the construction of a house, certain jobs have to be done before other jobs. Let J be the set of jobs and let defined on J be defined by j 1  j 2 if j 1 has to be completed before j 2 can be started. is a poset.

69 Total Order Relations With partial orders because not all elements need to be comparable. But some total relations give rise to “total order”. For example “less than or equal to" is a total relation over the set of real numbers, because for two numbers either the first is less than or equal to the second, or the second is less than or equal to the first. We can define a total order in two ways 1)in terms of membership and 2)in terms of operations on sets.

70 Total Order Definition A partial order  over B is called a total order or linear order if Def 1 (  b,c | : b  c  c  b) Def 2 iff  -1 = B  B is called a linearly ordered set or a chain.

71 Total Orders and are chains Let set S contain more than one element. Then  over  S is not a total order. Because if b and c are distinct elements of S, then neither {b}  {c} nor {c}  {b} holds. Any subset of a totally ordered set, with the restriction of the order on the whole set. Any partially ordered set X where every two elements are comparable (i.e. if a,b are members of X either a≤b or b≤a or both).

72 Partial but not Total Order Let C be the set of courses at DIT. Let b  c mean that b = c or b is a prerequisite for c. The relation is a partial order but not a total order.

73 Total Orders The letters of the alphabet ordered by the standard dictionary order (e.g., A < B < C ) is a partial order.

74 UML Association as a Relation A representation of a UML association as 1)a diagram and 2)a relation

75 UML Aggregation Relation Informal ‘whole-part’ relationships can be modelled using aggregation –a specialized form of an association –can have standard annotations on ends

76 UML: Cyclic Object Structures Aggregation is useful for ruling out invalid cyclic object structures –eg where an assembly contains itself, directly or indirectly

77 UML: Properties of Aggregation Aggregation rules this out because it is –antisymmetric: an object can’t link to itself –transitive: if a links to b and b to c, a links to c part end whole end

78 Meaning of Aggregation Sometimes there is a conflict E.g. people cannot be their own ancestors –this can be specified using aggregation –but a person’s ancestors are not a part of them! –The aggregation does not make sense in this case

79 Meaning (semantics) of Aggregation Relation A hand may be considered as part of a person and a person may be considered as part of an orchestra, but we would not consider hand as part of an orchestra. Heuristic: If the part moves, can one deduce that the whole moves with it in normal circumstances.


Download ppt "Computing Fundamentals 1 Lecture 7 Relations Lecturer: Patrick Browne Room K308 Based on Chapter 14. A Logical approach."

Similar presentations


Ads by Google