Presentation is loading. Please wait.

Presentation is loading. Please wait.

Dr. Eng. Farag Elnagahy Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222.

Similar presentations


Presentation on theme: "Dr. Eng. Farag Elnagahy Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222."— Presentation transcript:

1 Dr. Eng. Farag Elnagahy farahelnagahy@hotmail.com Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222 Discrete Structures IRelations

2 2 Relations Functions as Relations Let A and B be nonempty sets. functionf A function f from A to B is an assignment of exactly one element of B to each element of A. We write f(a) = b if b is the unique element of B assigned by the function f to the element a of A. If f is a function from A to B, f : A → B we write f : A → B Relations are a generalization of function

3 3 Relations binary relation Let A, B be sets, a binary relation R from A to B, is a subset of A×B. R  AxB A binary relation from A to B is a set R of ordered pairs where the first element of each ordered pairs comes from A and the second element comes from B. R:A×B, or R:A,B is a subset of the set A×B. The notation a R b means that (a,b)  R. The notation a R b means that (a,b)  R. When (a,b) belongs to R, a is said to be related to b by relation R.

4 4 Relations binary relation Example Let A be the set of students in your school and let B be set of courses, and let R be the relation that consists of those pairs (a,b), where a is a student enrolled in course b. (a,b), where a is a student enrolled in course b.  If Ahmed, Ali, and Mohamed are enrolled in CP223 and Ahmed, Ali, and Osman are enrolled in CS313 and Ahmed, Ali, and Osman are enrolled in CS313  Then the pairs (Ahmed,CP223), (Ali, CP223), (Mohamed, CP223), (Ahmed, CS313), (Ali, CS313 ), and (Osman, CS313) belong to (are in) R.  The pair (Osman, CP223) is not in R.

5 5 Relations Representation of relation (Arrow diagram & table) Example Let A ={0,1,2} and B={a,b} and the relation R from A to B is {(0,a),(0,b),(1,a),(2,b)}. 0 a1 b2 Arrow diagram table 0 R a 0 R b 1 R a 2 R b 1 R b 2 R a Rab 0xx 1x 2x

6 6 Relations Representation of relation (digraphs) A directed graph, or digraph consists of a set V of vertices (or nodes) together with a set E of ordered pairs of elements of V called edges (or arcs). The vertex a is called the initial vertex of the edge (a,b), and vertex b is called the terminal vertex of this edge. a b a b An edge of the form (a,a) is represented by an arc from the vertex a back to itself and it is called a loop. a edge or arc

7 7 Relations Representation of relation (digraphs) ExampleR={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)}loop vertex(node) vertex(node) edge(arc) edge(arc) A directed graph (digraph) A directed graph (digraph) 1 2 3 4

8 8 Relations Representation of relation (matrix) A relation between finite sets can be represented using a zero-one matrix. Suppose that R is a relation from A={a 1,a 2,…,a m ) to B={b 1,b 2,….,b n }. This relation can be represented by the matrix M R =[m ij ], where: [m ij ]= 1 if (a i,b j )  R [m ij ]= 1 if (a i,b j )  R 0 if (a i,b j )  R 0 if (a i,b j )  R Example Let A ={0,1,2} and B={a,b} and the relation R from A to B is {(0,a),(0,b),(1,a),(2,b)}. 11 10 01 MR=MR=MR=MR= ab 01 2

9 9 Relations on a Set  A (binary) relation from a set A to itself is called a relation on the set A. Example Let A={1,2,3,4} which ordered pairs are in the R={(a,b) | a divides b}. 1,2,3,4 are positive integer, max is 4 R= {(1,1),(1,2),(1,3),(1,4),(2,2),(2,4), (3,3),(4,4)} (3,3),(4,4)} Draw the arrow diagram, digraph, and matrix? R1234 1xxxx 2xx 3x 4x

10 10 Relations on a Set Example Consider these relations on the set of integers R 1 ={(a,b) | a  b} R 2 ={(a,b) | a  b} R 3 ={(a,b) | a=b or a=-b} R 4 ={(a,b) | a=b} R 5 ={(a,b) | a=b+1} R 6 ={(a,b) | a+b  3} Which of these relations contain each of the pairs (1,1), (1,2), (2,1), (1,-1), and (2,2) ? The pair (1,1) is in ….. …..

11 11 Relations on a Set How many relations are there on a set with n elements? A relation on a set A is a subset of AxA. AxA has n 2 elements when A has n elements, and a set with m elements has 2 m subsets, there are 2 n 2 subsets of AxA. Thus there are 2 n 2 relations on a set with n elements. For example there are 2 3 2 = 2 9 =512 relations on the set {a,b,c}

12 12 Properties of Relations There are several properties that are used to classify relations on a set. In some relations an element is always related to itself. For example, let R be the relation on the set of all people consisting of pairs (x,y) where x and y has the same father and the same mother. Then xRx for every person x.

13 13 Properties of Relations A relation R on a set A is called reflexive if (a,a)  R for every element a  A (  a  A), aRa. (a,a)  R for every element a  A (  a  A), aRa. –E.g., the relation ≥ : ≡ {(a,b) | a≥b} is reflexive  A relation R on the set A is reflexive if  a((a,a)  R) when the universe of discourse is the set of all elements in A.  a((a,a)  R) when the universe of discourse is the set of all elements in A. Reflexive means that every member is related to itself.  A relation R on a set A is called irreflexive if (a,a)  R for every element in A (a,a)  R for every element in A There is no element in A is related to itself There is no element in A is related to itself

14 14 Properties of Relations Example Consider the following relations on the {1,2,3,4} R 1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)} R 2 ={(1,1),(1,2),(2,1)} R 3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)} R 4 ={(2,1),(3,1),(3,2),(4,1),(3,4)} R 5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)} R 6 ={(3,4)} Which of these relations are reflexive? The relations R 3 and R 5 are reflexive because they both contain all pairs of the form (a,a). irreflexive ?

15 15 Properties of Relations Example Consider the following relations on the set of integers R 1 ={(a,b) | a  b} R 2 ={(a,b) | a  b} R 3 ={(a,b) | a=b or a=-b} R 4 ={(a,b) | a=b} R 5 ={(a,b) | a=b+1} R 6 ={(a,b) | a+b  3} Which of these relations are reflexive? The relations R 1, R 3 and R 4 are reflexive because they both contain all pairs of the form (a,a). irreflexive ?

16 16 Properties of Relations Example  Is the “divides” relation on the set of positive integers reflexive?  Is the “divides” relation on the set of integers reflexive? Note that 0 does not divide 0.

17 17 Properties of Relations A relation R on a set A is called reflexive if and only if there is a loop at every vertex of the directed graph. R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)} 1 2 3 4 irreflexive ?

18 18 Properties of Relations A relation R on a set A is called reflexive if and only if (a i,a i )  R this means that m ii =1 for i=1,2,.,n All the elements on the main diagonal of M R are equal to 1 R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)} 1111 0111 0011 0001 MR=MR=MR=MR= irreflexive ?

19 19 Properties of Relations  A relation R on a set A is symmetric if (b,a)  R whenever (a,b)  R for all a,b  A if (b,a)  R whenever (a,b)  R for all a,b  A  a  b((a,b)  R → (b,a)  R )  a  b((a,b)  R → (b,a)  R )  A relation R on a set A is antisymmetric if (a,b)  R and (b,a)  R then a=b for all a,b  A if (a,b)  R and (b,a)  R then a=b for all a,b  A  a  b((a,b)  R  (b,a)  R → (a=b) )  a  b((a,b)  R  (b,a)  R → (a=b) ) Note that “the term symmetric and antisymmetric are not opposites, the relation can have both of these properties or may lack both of them”

20 20 Properties of Relations A relation cannot be both symmetric and antisymmetric if it contains some pair of the form (a,b), where a≠b example Let R be the following relation defined on the set {a, b, c, d}: R = {(a, a), (a, c), (a, d), (b, a), (b, b), (b, c), (b, d), (c, b), (c, c), (d, b), (d, d)}. Determine whether R is: (a) reflexive. Yes (b) symmetric. No there is no (c,a) for example (c) antisymmetric. No b  c b  d

21 21 Properties of Relations A relation cannot be both symmetric and antisymmetric if it contains some pair of the form (a,b), where a≠b example Let R be the following relation defined on the set {a, b, c, d}: R = {(a, a), (b, b), (c, c), (d, d)}. Determine whether R is: (a) reflexive. Yes (b) symmetric. yes (c) antisymmetric. yes

22 22 Properties of Relations Example Consider the following relations on the {1,2,3,4} R 1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)} R 2 ={(1,1),(1,2),(2,1)} R 3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)} R 4 ={(2,1),(3,1),(3,2),(4,1),(3,4)} R 5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)} R 6 ={(3,4)} Which of these relations are symmetric and which are antisymmetric ? R 2 and R 3 are symmetric because in each case (b,a) belongs to the relation whenever (a,b) does.

23 23 Properties of Relations Example Consider the following relations on the set of integers R 1 ={(a,b) | a  b} R 2 ={(a,b) | a  b} R 3 ={(a,b) | a=b or a=-b} R 4 ={(a,b) | a=b} R 5 ={(a,b) | a=b+1} R 6 ={(a,b) | a+b  3} Which of these relations are symmetric and which are antisymmetric ? R 3, R 4,and R 6 are symmetric because in each case (b,a) belongs to the relation whenever (a,b) does.

24 24 Properties of Relations Example Consider the following relations on the set of integers R 1 ={(a,b) | a  b} a  b and b  a imply that a=b R 2 ={(a,b) | a  b} R 3 ={(a,b) | a=b or a=-b} R 4 ={(a,b) | a=b} R 5 ={(a,b) | a=b+1} R 6 ={(a,b) | a+b  3} R 1, R 2, R 4, R 5 are antisymmetric R 2 is antisymmetric it is impossible for a>b and b>a R 5 is antisymmetric it is impossible for a=b+1 and b=a+1

25 25 Properties of Relations Example Is the “divides” relation on the set of positive integers symmetric? Is it antisymmetric ? This relation is not symmetric because 1|2, but 2|1. It is antisymmetric because a|b, and b|a then a=b.

26 26 Properties of Relations A relation R on a set A is called symmetric if and only if for every edge between distinct vertices in its directed graph there is an edge in the opposite direction. R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)} 1 2 3 4 Not symmetric

27 27 Properties of Relations A relation R on a set A is called antisymmetric if and only if there are never two edges in the opposite direction between distinct vertices in its directed graph 1 2 3 4 Antisymmetric Not reflexive Not symmetric

28 28 Properties of Relations A relation R on a set A is called symmetric if and only if m ij =m ji of M R for i=1,2,.,n j=1,2,.,n R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)} 1111 0111 0011 0001 MR=MR=MR=MR=1234 12 3 4 (a,b)(a,b)(a,b)(a,b) Antisymmetric

29 29 Properties of Relations A relation R on a set A is called symmetric if and only if m ij =m ji of M R for i=1,2,.,n j=1,2,.,n R={(1,1),(1,2),(1,3),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)} 1110 0111 0011 0001 MR=MR=MR=MR=1234 12 3 4 (a,b)(a,b)(a,b)(a,b) Antisymmetric

30 30 Properties of Relations Suppose that the relation R on a set A is represented by the matrix 110 111 011 MR=MR=MR=MR= A relation R is reflexive iff (a i,a i )  R this means that m ii =1 for i=1,2,.,n A relation R is symmetric if (a,b)  R ↔ (b,a)  R if (a,b)  R ↔ (b,a)  R this means that m ij =m ji for i=1,2,.,n 110111 011 MR=MR=MR=MR=

31 31 Properties of Relations Suppose that the relation R on a set A is represented by the matrix 110 011 010 MR=MR=MR=MR= This relation is reflexive symmetric antisymmetric antisymmetric This relation is reflexive symmetric antisymmetric antisymmetric000111 101 MR=MR=MR=MR=

32 32 Properties of Relations Suppose that the relation R on a set A is represented by the matrix 110 111 010 MR=MR=MR=MR= This relation is reflexive symmetric antisymmetric antisymmetric This relation is reflexive symmetric antisymmetric antisymmetric110110 001 MR=MR=MR=MR=

33 33 Properties of Relations Let R be the relation consisting of all pairs (x,y) of students at your school, where x has taken more credits than y. Suppose that x is related to y and y related to z. This means that x has taken more credits than y and x has taken more credits than y and y has taken more credits than z y has taken more credits than z We can conclude that x has taken more credits than z, so that x is related to z. x has taken more credits than z, so that x is related to z. The relation R has the transitive property.

34 34 Properties of Relations A relation R on a set A is called transitive if whenever (a,b)  R and (b,c)  R then (a,c)  R, for all a, b, c  A  a  b  c(( (a,b)  R  (b,c)  R) → (a,c)  R)

35 35 Properties of Relations Consider the following relations on the {1,2,3,4} R 1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)} R 2 ={(1,1),(1,2),(2,1)} R 3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)} R 4 ={(2,1),(3,1),(3,2),(4,1),(4,2),((4,3)} R 5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} (3,4),(4,4)} R 6 ={(3,4)} Which of these relations are transitive ?  The relation is transitive If (a,b) and (b,c) belong to the relation If (a,b) and (b,c) belong to the relation then (a,c) also does. then (a,c) also does. R 4 (3,2),(2,1),(3,1) (4,2) (2,1),(4,1) (4,3) (3,1),(4,1) (4,3) (3,2),(4,2) (4,3) (3,1),(4,1) (4,3) (3,2),(4,2)

36 36 Properties of Relations Consider the following relations on the set of integers R 1 ={(a,b) | a  b} R 2 ={(a,b) | a  b} R 3 ={(a,b) | a=b or a=-b} R 4 ={(a,b) | a=b} R 5 ={(a,b) | a=b+1} R 6 ={(a,b) | a+b  3} Which of these relations are transitive ?  The relation is transitive If (a,b) and (b,c) belong to the relation If (a,b) and (b,c) belong to the relation then (a,c) also does. then (a,c) also does.

37 37 Properties of Relations Is the “divides” relation on the set of positive integers transitive? Suppose that a divides b and b divides c. Then there are positive integers k and l such that b=ak and c=bl. Hence, c=a(kl), so a divides c. It follows that the relation is transitive

38 38 Properties of Relations A relation is transitive if and only if whenever there is an edge from a vertex x to a vertex y and an edge from a vertex y to a vertex z, there is an edge from a vertex x to a vertex z completing a triangle where each side is a directed edge with the correct direction. 1 2 3 4

39 39 Properties of Relations Exercises PP.542-544 2-313-1422-2832

40 40 Combining Relations Let A={1,2,3} and B={1,2,3,4} The relation R 1 ={(1,1),(2,2),(3,3)} R 2 ={(1,1),(1,2),(1,3),(1,4)} R 1  R 2 = {(1,1),(1,2),(1,3),(1,4),(2,2),(3,3)} R 1  R 2 = {(1,1)} R 1 - R 2 = {(2,2),(3,3)} R 2 - R 1 = {(1,2),(1,3),(1,4)} R 1  R 2 = R 2  R 1 = R 1  R 2 - R 1  R 2 = {(1,2),(1,3),(1,4),(2,2),(3,3)} = {(1,2),(1,3),(1,4),(2,2),(3,3)} Read examples 18,19 PP. 525-526

41 41 Combining Relations Let A={1,2,3} and B={1,2,3,4} The relation R 1 ={(1,1),(2,2),(3,3)} R 2 ={(1,1),(1,2),(1,3),(1,4)} Construct M R 1 and M R 2 R 1  R 2 = M R 1  R 2 = M R 1  M R 2 R 1  R 2 = M R 1  R 2 = M R 1  M R 2

42 42 Compositions of Relations Let R be a relation from a set A to a set B and S a relation from B to a set C. The composite of R and S is the relation consisting of ordered pairs (a,c), where a  A, c  C, and for which there exists an element b  B such that (a,b)  R and (b,c)  S. we denote the composite of R and S by S  R Example R is the relation from {1,2,3} to {1,2,3,4} S is the relation from {1,2,3,4} to {0,1,2} R = {(1,1),(1,4),(2,3),(3,1),(3,4)} S = {(1,0),(2,0),(3,1),(3,2),(4,1)} S  R={(1,0),(1,1),(2,1),(2,2),(3,0),(3,1)}} T/F

43 43 Compositions of Relations To find the matrix representing the relation S  R (composite of R and S) Construct M R and M s Then calculate the Boolean product ( ⊙ ) of the matrix M R and M s M S  R = M R ⊙ M s M S  R = M R ⊙ M s

44 44 Compositions of Relations The n th power R n of a relation R on a set A can be defined recursively by: R 1 = R R n+1 = R n  R for all n>0.The n th power R n of a relation R on a set A can be defined recursively by: R 1 = R R n+1 = R n  R for all n>0. R 2 = R  R, R 3 = R 2  R = (R  R)  R Example R = {(1,1),(2,1),(3,2),(4,3)}, find the powers R n,n=2,3,4,…. R 2 = R  R= {(1,1),(2,1),(3,1),(4,2)} R 3 = R 2  R= {(1,1),(2,1),(3,1),(4,1)} R 4 = R 3  R= {(1,1),(2,1),(3,1),(4,1)}= R 3 R n = R 3

45 45 Compositions of Relations Let R be a relation from a set A to a set B, Let R be a relation from a set A to a set B,  The inverse relation (R -1 ) from B to A is the set of ordered pairs { (b,a) | (a,b)  R }  The complement relation R is the set of ordered pairs { (a,b) | (a,b)  R } pairs { (a,b) | (a,b)  R } Exercises PP. 527-529 1-7, 24-25, 32, 54

46 46 Closures of Relations Consider relation R={(1,2),(2,2),(3,3)} on the set A = {1,2,3,4}. Is R reflexive? No What can we add to R to make it reflexive? (1,1), (4,4) R’ = R U {(1,1),(4,4)} is called the reflexive closure of R.

47 47 Closures of Relations In general Let R be a relation on a set A R may or may not have some property P such as: Reflexivity – Symmetry – Transitivity The closure of relation R on set A with respect to property P is the relation R’ with  R  R’  R’ has property P R’ is called the closure of R with respect to P

48 48 Closures of Relations Let R be the relation on {1, 2, 3, 4} such that R = {(1, 1), (1, 4), (2, 3), (3, 1), (3, 3), (4, 4)}. Find: (a) the reflexive closure of R. (b) the symmetric closure of R. (b) the symmetric closure of R. (c) the transitive closure of R. (c) the transitive closure of R. (a) {(1,1), (1,4), (2,2), (2,3), (3,1), (3,3), (4,4)}. (b) {(1,1), (1,3), (1,4), (2,3), (3,1), (3,2), (3,3), (4,1), (4,4)}. (c) {(1,1), (1,4), (2,1), (2,3), (2,4), (3,1), (3,3), (3,4), (4,4)}. Read examples 1 and 2 PP. 454

49 49 Equivalence Relations A relation on a set A is called equivalence relation if it is reflexive, symmetric, and transitive. ~ b Two elements a and b that related by an equivalence relation are called equivalent. a ~ b Is R is equivalence relation? R={(a,b) | a=b or a=-b} r,s, and t Read examples 2,4,5,6,7 PP. 556-557 Exercises PP.553-554 1,3, 5-7 Exercises PP.562-563 1-2

50 50 Equivalence Relations Congruence Modulo m Let m be a positive integer m>1. Show that the following relation is an equivalence relation on the set of integers. R={ (a,b) | a  b(mod m) } Note that a  b(mod m) Means m divides a-b  a-a=0 and is divisible by m ( R is reflexive )  a  b(mod m) then a-b=km where k is an integer It follows that b-a=(-k)m means b  a(mod m) ( R is symmetric )  suppose that a  b(mod m) and b  c(mod m) a-b=km and b-c=lm add both equations we get: a-b+ b-c= km+ lm=(k+l)m a-c=(k+l)m I.e a  c(mod m) ( R is transitive ) R is equivalence relation

51 51 Equivalence Classes Let R be an equivalence relation on S. The set of all elements that are related to an element a of S is called equivalence class of a. the equivalence class of a with respect to R is denoted by [a] R. a  S, [a] R, is [a] R = {s|(a,s)  R} or [a] R = {s: aRs} If b  [a] R b is called a representative of this equivalence relation  Any element of a class can be used as a representative of this class.

52 52 Equivalence Classes Example What is equivalence class of an integer for the following equivalence relation? R={(a,b) | a=b or a=-b} In this equivalence relation the integer is related to itself and its negative, so : [a] R ={-a,a} or [a] ={-a,a} [7] ={-7,7} [5] ={-5,5} [0] ={0}

53 53 Equivalence Classes Example What is equivalence class of 0 and 1 for the Congruence Modulo 4? The equivalence class of 0 contains all integers a such that a  0(mod 4) [0] ={…………,-8,-4,0,4,8,……………….} The equivalence class of 1 contains all integers a such that a  1(mod 4) [1] ={…………,-7,-3,1,5,9,……………….} Congruence classes modulo m [a] m ={……………,a-2m,a-m,a,a+m,a+2m,……………..}

54 54 Equivalence Classes Example Let n be a positive integer and S a set of strings. R n is the relation on S such that sR n t iff s=t or both s and t have at least n characters and the first n characters of s and t are the same. sR 3 t 01 R 3 01 00111 R 3 00101 01 R 3 11 00111 R 3 01101 01 R 3 11 00111 R 3 01101 What is equivalence class of the string 0111 with respect to the R 3 ? [011] R3 ={ 011, 0110,0111,01100,01101,01110, 01111,………} 01111,………}

55 55 Equivalence Classes and Partitions Let n be a positive integer and S a set of strings. R n is the relation on S such that sR n t iff s=t or both s and t have at least n characters and the first n characters of s and t are the same. sR 3 t [ ] R3 ={ } [0] R3 ={0} [1] R3 ={1} [00] R3 ={00} [01] R3 ={01} [10] R3 ={10} [11] R3 ={11}

56 56 Equivalence Classes and Partitions [000] R3 ={000,0000,0001,00000,00001,00011,………} [001] R3 ={001,0010,0011,00100,00101,00111,………} [010] R3 ={010,0100,0101,01000,01001,01011,………} [011] R3 ={011,0110,0111,01100,01101,01111,………} [100] R3 ={100,1000,1001,10000,10001,10011,………} [101] R3 ={101,1010,1011,10100,10101,10111,………} [110] R3 ={110,1100,1101,11000,11001,11011,………} [111] R3 ={111, 1110,1111,11100,11101,11111,………} These 15 equivalence classes are disjoint and every bit string is in exactly one of them. These equivalence classes partition the set of all bit strings.

57 57 Partial Orderings Let R be a relation on a set S, then R is a Partially Ordered Set (POSet) if it is  Reflexive - aRa,  a  Transitive - aRb  bRc  aRc,  a,b,c  Antisymmetric - aRb  bRa  a=b,  a,b and denoted by (R,S) R={(a,b) | a  b}  a  a Reflexive  a  b and b  a implies a=b Antisymmetric  a  b and b  c implies a  c Transitive  is is a partial ordering on Z, and (Z,  ) is poset

58 58 Partial Orderings Example (Z +, | ), the relation “divides” on positive integers. Reflexive? a|a since a=1a (k=1)Antisymmetric? a|b means b=ak, b|a means a=bj. But b = bjk this means jk=1. jk=1 means j=k=1, and we have b=a1, or b=aTransitive? a|b means b=ak, b|c means c=bj. c = bj = akj =am where m=kj then a|c | is is a partial ordering on Z +, and (Z +,|) is poset

59 59 Partial Orderings Example Show that the inclusion relation  is a partial ordering on the power set of a set S? Reflexive? A  A Antisymmetric? A  B and B  A then A=B Transitive? A  B and B  C then A  C  is is a partial ordering on P(s), and (P(s),  ) is poset

60 60 Partial Orderings Different symbols such , , and | are used for a partial ordering. The general symbol ≼ is used for a partial ordering. a ≼ b means (a,b)  R in an arbitrary poset (S,R).  The elements a and b of a poset (S, ≼ ) are called comparable if either a ≼ b or b ≼ a.  when a and b are elements of S such that neither a ≼ b nor b ≼ a, a and b are called incomparable.

61 61 Partial Orderings Example In the poset (Z +,|), are the integers 3 and 9 comparable? are the integers 5 and 7 comparable? 3|9 comparable 5|7 7|5 incomparable  The adjective “partial” is used to describe partial orderings because pairs of elements may be incomparable.  When every two elements in the set are comparable, the relation is called total ordering.

62 62 Partial Orderings If (S, ≼ ) is a poset and every two elements of S are comparable, S is called a totally ordered or linear ordered set (chain). And ≼ is called a total order or a linear order. Examples  The poset (Z,  ) is totally ordered a  b or b  a.  The poset (Z +,|) is not totally ordered ex. 5,7

63 63 Hasse Diagrams Hasse diagrams are a special kind of graphs used to describe posets. Ex. In poset ({1,2,3,4},  ), we can draw the following directed graph, or digraph to describe the relation. 1 2 3 4

64 64 Hasse Diagrams Hasse diagrams are a special kind of graphs used to describe posets. 1.Draw edge (a,b) if a  b 2.Don’t draw self loops 3.Don’t draw transitive edges 4.Don’t draw up arrows 1 2 3 4

65 65 Hasse Diagrams Hasse diagrams are a special kind of graphs used to describe posets. 1.Draw edge (a,b) if a  b 2.Don’t draw self loops 3.Don’t draw transitive edges 4.Don’t draw up arrows 1 2 3 4

66 66 Hasse Diagrams Hasse diagrams are a special kind of graphs used to describe posets. 1.Draw edge (a,b) if a  b 2.Don’t draw self loops 3.Don’t draw transitive edges 4.Don’t draw up arrows 1 2 3 4 The poset (Z,  ) is totally ordered (chain) a  b or b  a.

67 67 Hasse Diagrams  is is a partial ordering on P(s), and (P(s),  ) is poset The hasse digram of (P({a,b,c}),  ) {a,b,c} or 111 {a,b} or 110{a,c} or 101{b,c} or 011 {a} or 100{b} or 010{c} or 001 {} or 000

68 68 Hasse Diagrams Maximal and Minimal Elements oAn element in the poset is called maximal if it is not less than any elements of the poset. oAn element in the poset is called minimal if it is not greater than any elements of the poset. Reds are maximal. whites are minimal.

69 69 Hasse Diagrams Which elements of the poset ({2,4,5,10,12,20,25),|) Are maximal, and which are minimal? Maximal elements are 12,20,25 minimal elements are 2,5 2 4 12 5 102025 Note that: 25 is the greatest element and 2 is the least element.

70 70 Hasse Diagrams Which elements of the poset ( {1,2,3,4},  ), Are maximal, and which are minimal? Maximal element is 4 minimal element is 1 1 2 34 Note that: 4 is the greatest element and 1 is the least element.

71 71

72 72

73 73

74 74

75 75

76 76 N-ary Relations and Their Applications The relationships among elements from more than two sets are called n-ary relations. The relationships among elements from more than two sets are called n-ary relations. Let A 1, A 2, …., A n be sets, an n-ary relations on these sets is a subset of A 1 xA 2 x…..xA n. The sets A 1, A 2, …., A n are called the domains of the relation, and n is called its degree. Example Let R be the relation on NxNxN consisting of triples (a,b,c), where a, b, and c are integers with a<b<c. (1,2,3)  R (2,4,3)  R The degree of this relation is 3 Its domains are equal to the sets of natural numbers.

77 77 N-ary Relations and Their Applications Example Let R be the relation on ZxZxZ consisting of triples (a,b,c), where a, b, and c are integers with b-a=k and c-b=k, where k (common difference) is an integer. This relation is called arithmetic progression (a,a+k,a+2k). (1,2,3), (1,3,5)  R, (2,4,3), (2,5,9)  R The degree of this relation is 3 Its domains are equal to the sets of integers.

78 78 N-ary Relations and Their Applications Example Let R be the relation on ZxZxZ consisting of triples (a,b,c), where a, b, and c are integers with b/a=k and c/b=k, where k (common ratio) is an integer. This relation is called geometric progression (a,ak,ak 2 ). (1,3,9), (1,4,16)  R, (2,4,3), (2,5,9)  R The degree of this relation is 3 Its domains are equal to the sets of integers.

79 79 N-ary Relations and Their Applications Example Let R be the relation on ZxZxZ + consisting of triples (a,b,m), where a, b, and m are integers with m  1 and a  b(mod m). (8,2,3), (-1,9,5),(14,0,7)  R, (7,2,3), (-2,-8,5), (11,0,6)  R The degree of this relation is 3 Its first two domains are the sets all of integers. And its third domain is the set of all positive integers. Congruence Modulo m m>0 a  b(mod m). Means m divides a-b

80 80 N-ary Relations and Their Applications Example Let R be the relation consisting of 5-tuples (A,N,S,D,T) representing airplane flights, where A is the airline, N is the flight number, S is the starting point, D is the destination, and T is the departure time. (Saudi Arabian Airlines,304,Cairo,Jeddah,15:00)  R The degree of this relation is 5 Its domains are the set of all airlines, the set of flight numbers, the set of cities, the set of cites, and the set of times.

81 81 Databases and Relations Relational Databases A relational database is essentially just an n-ary relation R. A database consists of records, which are n-tuples, made up of fields. These fields are the entire of the n-tuples. Relations used to represent databases are called tables. Each column of the table corresponds to an attribute of the database.

82 82 Databases and Relations A domain of an n-ary relation is called a primary key when the value of the n-tuple from this domain determines the n-tuple. That is,a domain is primary key when no two n-tuples in the relation have the same value from this domain. Student_nameID_numberMajorGPA Ahmed Ali 0612345CS3.88 Ashraf Sami 0412364Physics3.65 Waleed Tarek 0512432Math2.88 Tarek Morad 0723465CS3.65

83 83 Databases and Relations Records are often added to or deleted from databases. Thus, the primary key should be chosen that remains one whenever the database is changed. The current collection of the n-tuples in a relation is called the extension of the relation. The more permanent part of a database, including the name and attributes of the database is called the intension. Selecting the primary key depends on the possible extensions of the database.

84 84 Databases and Relations Combinations of domains can also uniquely identify n-tuples in n-ary database. The Cartesian product of these domains is called a composite key A composite key for the database is a set of domains {A i, A j, …} such that R contains at most 1 n-tuple (…,a i,…,a j,…) for each composite value (a i, a j,…)  A i ×A j ×… See student relation Is (Major x GPA) a composite key for the n-ary relation ? Assuming that no n-tuples are ever added

85 85 Operations on n-ary Relations Selection Operator Let R be an n-ary relation and C a condition that elements in R may satisfy. Then the selection operator s C maps the n-ary relation R to the n-ary relation of all n-tuples from R that satisfy the condition C.

86 86 Operations on n-ary Relations selection operator s C1 where c 1 is the condition major=“CS“ The result is the two 4-tuples. (Ahmed Ali, 0612345, CS, 3.88) (Tarek Morad, 0723465, CS, 3.65) s C2 GPA >3.5 s C3 (Major=“CS“  GPA >3.5 ) Student_nameID_numberMajorGPA Ahmed Ali 0612345CS3.88 Ashraf Sami 0412364Physics3.65 Waleed Tarek 0512432Math2.88 Tarek Morad 0723465CS3.65

87 87 Operations on n-ary Relations Projection Operators The projection P i1,i2,….im where i 1 <i 2 <….i m, maps the n-tuple (a 1,a 2,….,a n ) to the m-tuple (a i1,a i2,…a im ), where m ≤ n The projection P i1,i2,….im deletes n-m of the components of an n-tuple, leaving the i 1 th, i 2 th,….,i m th components P 1,3 is applied to the 4-tuples (2,3,0,4),(Tarek Morad, 0723465, CS, 3.65) (2,0), (Tarek Morad, CS)

88 88 Operations on n-ary Relations P 1,4 is applied to the relation in the table Student_nameID_numberMajorGPA Ahmed Ali 0612345CS3.88 Ashraf Sami 0412364Physics3.65 Waleed Tarek 0512432Math2.88 Tarek Morad 0723465CS3.65 Student_nameGPA Ahmed Ali 3.88 Ashraf Sami 3.65 Waleed Tarek 2.88 Tarek Morad 3.65 New relation is produced using projection

89 89 Operations on n-ary Relations Join Operator Puts two relations together to form a sort of combined relation.Puts two relations together to form a sort of combined relation. If the tuple (A,B) appears in R 1, and the tuple (B,C) appears in R 2, then the tuple (A,B,C) appears in the join J(R 1,R 2 ).If the tuple (A,B) appears in R 1, and the tuple (B,C) appears in R 2, then the tuple (A,B,C) appears in the join J(R 1,R 2 ). –A, B, and C here can also be sequences of elements (across multiple fields), not just single elements

90 90 Operations on n-ary Relations Join Operator example Suppose R 1 is a teaching assignment table, relating Professors to Courses. Suppose R 2 is a room assignment table relating Courses to Rooms,Times. Then J(R 1,R 2 ) is like your class schedule, listing (professor,course,room,time). Exercises PP.536-537 1-17


Download ppt "Dr. Eng. Farag Elnagahy Office Phone: 67967 King ABDUL AZIZ University Faculty Of Computing and Information Technology CPCS 222."

Similar presentations


Ads by Google