1 / 57

Relations and Their Properties

Relations and Their Properties. Section 8.1. Review: Cartesian Products. Definition : An ordered n-tuple ( a 1 , a 2 , …, a n ) is the ordered collection that has a 1 as its first element, a 2 as its second element…, and a n as its nth element. 2-tuples are called ordered pairs .

soren
Download Presentation

Relations and Their Properties

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. Relations and Their Properties Section 8.1

  2. Review: Cartesian Products • Definition:An ordered n-tuple (a1, a2, …, an) is the ordered collection that has a1 as its first element, a2 as its second element…, and an as its nth element. • 2-tuples are called ordered pairs. • (a,b) = (c,d)if and only if a=c and b=d • (a,b)≠(b,a)unless a=b

  3. Cartesian Product … • Definition:The Cartesian product of the sets A1, A2,…, An, denoted A1×A2×… ×An, is the set of ordered n-tuples (a1, a2, …, an), where ai belongs to set Aifor i =1, 2, …, n. A1×A2× …×An={(a1, a2, …, an)| aiAifor i=1, 2, …, n}

  4. Binary Relations • Definition:Let A and B be sets. A binary relationR fromAtoB is a subset of the Cartesian product AB. • Notation: xRy means (x,y)R, and x is said to be related toy under R. xRy denotes (x,y)R.

  5. Example • Let A be the set of all cities, and let B be the set of all states in the U.S. Define the relation Rfrom A to B by: (a,b)Rif city a is in state b. • Some ordered pairs belonging to relation R: R= {(East Lansing, Michigan), (Boulder, Colorado), (Chicago, Illinois), (Columbus, Ohio), …} (East Lansing)R(Michigan), (Boulder)R(Colorado), …

  6. a 1 b c 2 d 3 Another Example A= {a, b, c, d } B = {1, 2, 3} R = {(a,2), (b,1), (b, 2), (c,3), (d,2)}. Is this relation a function?

  7. Functions As Relations • Recall that: Let f: A→ B be a function. The graph of fis the set of ordered pairs Gf = {(x, f (x))| x  A}. • The graph of a function from A to B is a relation from A to B. • Conversely, if R is a relation from A to B such that every element in A is the first element of exactly one ordered pair of R, then a function can be defined with R as its graph.

  8. Relations on a Set • Definition: A relation ona setA is a relation from A to A, that is, a subset of AA. • Example: Let A= {1, 2, 3, 4}, and R = {(a,b) | a evenly divides b}. • What are the elements of R? R = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,4), (3,3), (4,4)}

  9. 1 1 2 2 3 3 4 4 Example • R can be displayed graphically and in tabular form/zero-one matrix form: R 1 2 3 4 1 x x x x 1 1 1 1 2 x x 0 1 0 1 3 x 0 0 1 0 4 x 0 0 0 1

  10. Relations on a Set… • Let A be a set with n elements. How many distinct relations are there on A? • Answer: The number of relations is the same as the number of subsets of AA. There are n2 elements in AA. Thus, P(AA), the power set of AA, has 2n2elements. Therefore, there are 2n2relations on a set with n elements.

  11. Types of Relations • Definition:A relation Ron a setAis called reflexive if (a,a)R for every element aA, that is, • Definition:A relation Ron a setAis called irreflexive if (a,a)  R for every element aA, that is, • Note that a relation can be both not reflexive and not irreflexive.

  12. Example • Consider the following relations on {1,2,3,4}. Which are reflexive, irreflexive, neither? • R1 = {(1,1), (1,2), (2,1), (2,2), (3,3), (4,4)} • Reflexive • R2 = {(2,4), (4,2)} • Irreflexive • R3 = {(1,2), (2,3), (3,4)} • Irreflexive • R4 = {(1,1), (2,2), (3,3), (4,4)} • Reflexive • R5 = {(1,3), (1,4), (2,3), (2,4), (3,1), (3,4)} • Irreflexive • R6 = {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)} • Neither

  13. Symmetric Relations • Definition:A relation Ron a setAis called symmetric if (b,a)R whenever (a,b)R for a, bA, that is, • Definition A relation Ron a setAis called antisymmetric if whenever (a,b)R and (b,a)R, then a = b, for a, b in A, that is, • Note that these definitions are not complementary. • Give a relation that is both symmetric and antisymmetric. • Give a relation that is neither symmetrick not antisymmetric.

  14. Example • Consider the following relations on {1,2,3,4}. Which are symmetric, antisymmetric, both, or neither? • R1 = {(1,1), (1,2), (2,1), (2,2), (3,3), (4,4)} • Symmetric • R2 = {(2,4), (4,2)} • Symmetric • R3 = {(1,2), (2,3), (3,4)} • Antisymmetric • R4 = {(1,1), (2,2), (3,3), (4,4)} • Both • R5 = {(1,3), (1,4), (2,3), (2,4), (3,1), (3,4)} • Neither • The“divides” relation on the set of positive integers • Antisymmetric • R6 = {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)} • Neither

  15. Transitive Relations • Definition:A relation Ron a setAis called transitiveif whenever (a,b)R and (b,c)R, then (a,c)R, for a, b, c in A, that is, Is the “football team relationship” transitive? A defeats B and B defeats C implies A defeats C ? Is the “subclass” relation on classes in an OO program transitive?

  16. Example • Consider the following relations on {1,2,3,4}. Which are transitive? • R1 = {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)} • Transitive • R2 = {(1,1), (1,2), (2,1), (2,2), (3,3), (4,4)} • Transitive • R3 = {(2,4), (4,2)} • Not Transitive • R4 = {(1,2), (2,3), (3,4)} • Not Transitive • R5 = {(1,1), (2,2), (3,3), (4,4)} • Transitive • R6 = {(1,3), (1,4), (2,3), (2,4), (3,1), (3,4)} • Not Transitive • The“divides” relation on the set of positive integers • Transitive

  17. Example Summary • Consider the following relations on {1,2,3,4}. Name its properties? • R1 = {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)} • T/A • R2 = {(1,1), (1,2), (2,1), (2,2), (3,3), (4,4)} • R/S/T • R3 = {(2,4), (4,2)} • S • R4 = {(1,2), (2,3), (3,4)} • A • R5 = {(1,1), (2,2), (3,3), (4,4)} • R/S/A/T • R6 = {(1,3), (1,4), (2,3), (2,4), (3,1), (3,4)} • None • The“divides” relation on the set of positive integers • R/A/T • Keys:R = Reflexive, S = Symmetric, A = Antisymmetric, T=Transitive

  18. 2 1 3 4 Pictorial Representation of Binary Relations on a Finite Set • Directed Graphs • Vertices, one for each element in the set • Arcs, one for each ordered-pair in the relation • Example: Set S = {1,2,3,4} and relation R on S R= {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)}

  19. Boolean matrix representation of binary relations on a finite set • Recall: If R is a binary relation over set S • Order the elements of S = { x1, x2, …, xn } • Represent by matrix with a 1 in position (i, j) iff xiRxj • Example: Set S = {1,2,3,4} and relation R on S R= {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)}

  20. Equivalence Relations • A relation Ron a set A is called an equivalence relation if it is reflexive, symmetric and transitive. • In such a relation, for each element aA, the set of all elements related to a under R is called the equivalence class of a, and is denoted by [a]. [a] = { (a,b) | a  A  b  A aRb }

  21. Example • Determine the properties of each of the following relations defined on the set of all real numbers R: • R = {(x,y) | x + y = 0} • R = {(x,y) | x = y x = – y} • R = {(x,y) | x – y is a rational number} • R = {(x,y) | x = 2y} • R = {(x,y) | xy≥ 0} • R = {(x,y) | xy = 0} • R = {(x,y) | x = 1} • R = {(x,y) | x = 1 or y = 1}

  22. R = {(x,y) | x + y = 0} • Solution: • It is not reflexivesince, for example, (1,1)  R. • It is not irreflexivesince, for example, (0,0) R. • Since x + y = y + x, it follows that if x + y = 0 then y + x = 0, so the relation is symmetric. • It is not antisymmetric since, for example, (–1,1) and (1, –1) are both in R, but 1 ≠ –1 • The relation is not transitive since, for example, (1,–1) R and (–1,1) R, but (1,1)  R.

  23. R = {(x,y) | x = y x = – y} • Solution: • Since for each x, xRx then it is reflexive. • Since it is reflexive, it is not irreflexive. • Since x = ±y if and only if y = ±x, then it is symmetric. • It is not antisymmetric since, for example, (1,–1) and (–1,1) are both in R but 1 ≠ –1 • It is also transitive because essentially the product of ±1 and ±1 is ±1.

  24. R = {(x,y) | x – y is a rational number} • Solution: • It is reflexive since, for all x, x – x = 0 is a rational number. • Since it is reflexive, it is not irreflexive. • It is symmetric because, if x – y isrational, then – (x – y) = y – x is also. • It is not antisymmetric because, for example, (1, –1) and (– 1,1) are both in R but 1 ≠ –1. • It is transitive because if x – y is a rational and y – z is a rational, so is x – y + y – z = x – z.

  25. R = {(x,y) | x = 2y} • Solution: • It is not reflexive since, for example, (1,1) R. • It is not irreflexive since, for example, (0,0) R. • It is not symmetric since, for example, (2,1) Rbut (1,2) R. • It is antisymmetric because x = y =0 is the only time that (x,y) and (y,x) are both in R. • It is not transitivesince, for example, (4,2) R and (2,1) R but (4,1) R.

  26. R = {(x,y) | x y ≥ 0} • Solution: • It is reflexive since x·x≥ 0. • Since it is reflexive, it is not irreflexive. • It is symmetric as the role of x and y are interchangeable. • It is not antisymmetric since, for example, (2,3) and (3,2) are both in R, but2 ≠ 3. • It is not transitive because, for example, (1,0) and (0, – 2) are both in R but (1, – 2) is not.

  27. R = {(x,y) | xy = 0} • Solution: • It is not reflexive since (1,1) R. • It is not irreflexive since (0,0) R. • It is symmetric as the role of x and y are interchangeable. • It is not antisymmetric since, for example, (2,0) and (0,2) are both in R but2 ≠ 0. • It is not transitive because, for example, (1,0) and (0, – 2) are both in R but (1, – 2) is not.

  28. R = {(x,y) | x = 1} • Solution: • It is not reflexive since (2,2) R. • It is not irreflexive since (1,1) R. • It is not symmetricsince, for example, (1,2) R but (2,1) R. • It is antisymmetric because if (x,y) R and (y,x) R it means x = y = 1. • It istransitive since if (1,y) R and (y,z) R, so is (1,z) R .

  29. R = {(x,y) | x = 1 or y = 1} • Solution: • It is not reflexive since (2,2) R. • It is not irreflexive since (1,1) R. • It is symmetricas the roles of x and y are interchangeable. • It is not antisymmetric since, for example, (2,1) and (1,2) are both in R but2 ≠ 1. • It isnot transitive since, for example, (3,1) and (1,7) are both in R but (3,7) R.

  30. Summary

  31. Combining Relations • Since relations are special kind of sets, all of the operators we used for combining sets could be used to combine relations.

  32. Combining Relations – Examples • Let A={1, 2, 3} and B={1, 2, 3, 4} • Let R1 and R2 be relations from A to B • R1 = {(1,1), (2,2), (3,3)} • R2 = {(1,1), (1,2), (1,3), (1,4)} • R1R2 = {(1,1), (2,2), (3,3), (1,2), (1,3), (1,4)} • R1R2 = {(1,1)} • R1R2 = {(2,2), (3,3)}

  33. S oR R S B c A Composition of Relations • Definition: Let R be a relation from a setA to a set B, and S be a relation from Bto a set C. The composition of R and S, denoted by S o R, is the relation consisting of ordered pairs (a,c), where: • aA,cC, and • bB | (a,b)R and (b,c)S.

  34. Example • Let A={1, 2, 3}, B={x, y, w, z}, C={0, 1, 2}, R a relation from A to B: • R= {(1, x), (1, z), (2, w), (3, x), (3, z)}, and Sa relation from B to C: • S= {(x,0), (y,0), (w,1), (w,2), (z,1)} • What is So R? • So R= {(1,0), (1,1), (2,1), (2,2), (3,0), (3,1)}

  35. Powers of Relations • Definition:Let R be a relation on the set A. The powers Rn, n = 1, 2, 3,… are defined recursively by: • R1 = R, • Rn+1=Rn o R, for n  1. • So: • R2 = RoR, • R3 = R2oR = (RoR) o R • …

  36. Rn+1 Rn+1=Rn o R R Rn A A A In the digraph representation, Rn consists of all the order pairs (x,y) where y is reachable from x using a directed path of length n.

  37. Example • Consider the following relations on {1,2,3,4}. • R= {(1,2), (1,3), (3,4)} • Is R transitive? • No • Find R2=R o R • R2 = {(1,4)} • Consider the following relation: • R= {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)} • IsR transitive? • Yes • Find R2=R o R • R2 = {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)}

  38. Characterizing Transitive Relations • Theorem: The relation R on a set A is transitive if and only if Rn  R for n = 1, 2, 3, ... • If Part: If Rn  R for n = 1, 2, 3, ... then R is transitive • See exercise 6.1 • Only If Part: If R is transitive then Rn  R for n = 1,2,3, ... • Proof By induction • Basis: If R is transitive then R1 R (trivially true) • Hypothesis: Assume true for n = 1, 2, 3, ... , k • Inductive: Given thatif R is transitive then Rk R, we want to show that if R is transitive then Rk+1 R • Proof: Assume that R is transitive, and let (a,b) be inRk+1. So xA | (a,x) R and (x,b)  Rk,by definition of Rk+1. Since R is transitive then Rk R by inductive hypothesis. So (x,b) R. Since R is transitive, then (a,b)  R. So Rk+1 R

  39. Applications in Relational Databases Section 8.2

  40. Records as n-tuples (with fields) • Record is an n-tuple of fields • e.g., student record might be 4-tuple of the form: (name, ID#, Major, GPA) • Relational Data Base: collection of records • e.g. student database { (Anderson, 231455, CSE, 3.88), (Adams, 888323, PHY, 3.45), (Chou, 102147, CSE, 3.49), (Gould, 453876, MTH, 3.45), (Rao, 678543, MTH, 3.90), (Stevens, 786576, PSY, 2.99) } • Field associates the attribute (name, ID#, etc.) with a value from a domain (set of student names, set of ID#s, etc.)

  41. Table 1 Student Data:(Student_Name, PID, Major, GPA)

  42. Operations on relations (tables) • Projection : projects a table on one or more attributes (columns) • Selection : selects records (rows) that satisfy a condition • Join : combines two tables into one based on shared fields • Of course, you can also use the “usual” operations on relations (sets) • , , –, …

  43. Projection of Students: P1,4 (Students) • Columns 1 and 4 are taken from Students • Columns 2 and 3 are ignored • Name is paired with GPA (perhaps so a department chair can send congratulations or condemnations to students with special GPAs) • Beware: the key ID# is lost so there may be some ambiguity • Perhaps there were 2 students “Smith”

  44. Projection of Students: P1,4 (Students) P1,4

  45. P1,2 (Enrollments) P1,2

  46. Selection: SC • Select all records (rows) that satisfy the condition C

  47. SMajor = “CSE” (Enrollments) SMajor = “CSE”

  48. SMajor = “CSE”  GPA > 3.5 (Students) SMajor = “CSE”  GPA > 3.5

  49. Join: Jp (R, S) • Joins records (rows) from R and S on the last p fields (columns) of R and the first p fields (columns) of S • To join two records corresponding fields must be identical • The join of (a1, …, am ) and (b1, …, bn ) is (a1, …, am, bp+1 , …, bn ), if am – p + 1 = b1 and am – p + 2 = b2 and . . . and am = bp

  50. Example: Joining two tables (on 2 cols) …

More Related