Presentation is loading. Please wait.

Presentation is loading. Please wait.

Relations and Their Properties

Similar presentations


Presentation on theme: "Relations and Their Properties"— Presentation transcript:

1 Relations and Their Properties
Sunday, April 23, 2017 Relations and Their Properties Section 8.1 © by A-H. Esfahanian. All Rights Reserved.

2 Review: Cartesian Products
Sunday, April 23, 2017 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 © by A-H. Esfahanian. All Rights Reserved.

3 Sunday, April 23, 2017 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 Ai for i =1, 2, …, n. A1×A2× …×An = {(a1, a2, …, an) | aiAi for i=1, 2, …, n} © by A-H. Esfahanian. All Rights Reserved.

4 Sunday, April 23, 2017 Binary Relations Definition: Let A and B be sets. A binary relation R from A to B is a subset of the Cartesian product AB. Notation: xRy means (x,y)R, and x is said to be related to y under R. xRy denotes (x,y)R. © by A-H. Esfahanian. All Rights Reserved.

5 Sunday, April 23, 2017 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 R from A to B by: (a,b)R if 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), … © by A-H. Esfahanian. All Rights Reserved.

6 Another Example A= {a, b, c, d } B = {1, 2, 3}
Sunday, April 23, 2017 Another Example c b d 2 3 1 a A= {a, b, c, d } B = {1, 2, 3} R = {(a,2), (b,1), (b, 2), (c,3), (d,2)}. R 1 2 3 a x b c d Is this relation a function? © by A-H. Esfahanian. All Rights Reserved.

7 Functions As Relations
Sunday, April 23, 2017 Functions As Relations Recall that: Let f: A → B be a function. The graph of f is 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. © by A-H. Esfahanian. All Rights Reserved.

8 Sunday, April 23, 2017 Relations on a Set Definition: A relation on a set A 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)} © by A-H. Esfahanian. All Rights Reserved.

9 Sunday, April 23, 2017 Example R can be displayed graphically and in tabular form/zero-one matrix form: R 1 x x x x 2 x x x x 3 2 4 1 © by A-H. Esfahanian. All Rights Reserved.

10 Sunday, April 23, 2017 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 2n2 elements. Therefore, there are 2n2 relations on a set with n elements. © by A-H. Esfahanian. All Rights Reserved.

11 Sunday, April 23, 2017 Types of Relations Definition: A relation R on a set A is called reflexive if (a,a)R for every element a  A, that is, Definition: A relation R on a set A is 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. © by A-H. Esfahanian. All Rights Reserved.

12 Sunday, April 23, 2017 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)} R4 = {(1,1), (2,2), (3,3), (4,4)} R5 = {(1,3), (1,4), (2,3), (2,4), (3,1), (3,4)} R6 = {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)} Neither © by A-H. Esfahanian. All Rights Reserved.

13 Sunday, April 23, 2017 Symmetric Relations Definition: A relation R on a set A is called symmetric if (b,a)R whenever (a,b)R for a, b  A, that is, Definition A relation R on a set A is 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. A relation can be both Symmetric and antisymmetric. A relation can be neither symmetric nor antisymmetric. © by A-H. Esfahanian. All Rights Reserved.

14 Sunday, April 23, 2017 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)} 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 R6 = {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)} © by A-H. Esfahanian. All Rights Reserved.

15 Sunday, April 23, 2017 Transitive Relations Definition: A relation R on a set A is called transitive if 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? © by A-H. Esfahanian. All Rights Reserved.

16 Sunday, April 23, 2017 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)} R3 = {(2,4), (4,2)} Not Transitive R4 = {(1,2), (2,3), (3,4)} R5 = {(1,1), (2,2), (3,3), (4,4)} R6 = {(1,3), (1,4), (2,3), (2,4), (3,1), (3,4)} The “divides” relation on the set of positive integers © by A-H. Esfahanian. All Rights Reserved.

17 Sunday, April 23, 2017 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 © by A-H. Esfahanian. All Rights Reserved.

18 Pictorial Representation of Binary Relations on a Finite Set
Sunday, April 23, 2017 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)} 1 4 3 2 © by A-H. Esfahanian. All Rights Reserved.

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)} 1

20 Equivalence Relations
Sunday, April 23, 2017 Equivalence Relations A relation R on 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 } © by A-H. Esfahanian. All Rights Reserved.

21 Sunday, April 23, 2017 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) | x y ≥ 0} R = {(x,y) | x y = 0} R = {(x,y) | x = 1} R = {(x,y) | x = 1 or y = 1} © by A-H. Esfahanian. All Rights Reserved.

22 R = {(x,y) | x + y = 0} Solution:
Sunday, April 23, 2017 R = {(x,y) | x + y = 0} Solution: It is not reflexive since, for example, (1,1)  R. It is not irreflexive since, 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. © by A-H. Esfahanian. All Rights Reserved.

23 R = {(x,y) | x = y  x = – y} Solution:
Sunday, April 23, 2017 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. © by A-H. Esfahanian. All Rights Reserved.

24 R = {(x,y) | x – y is a rational number}
Sunday, April 23, 2017 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 is rational, 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. © by A-H. Esfahanian. All Rights Reserved.

25 R = {(x,y) | x = 2y} Solution:
Sunday, April 23, 2017 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)  R but (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 transitive since, for example, (4,2)  R and (2,1)  R but (4,1)  R. © by A-H. Esfahanian. All Rights Reserved.

26 R = {(x,y) | x y ≥ 0} Solution: It is reflexive since x·x ≥ 0.
Sunday, April 23, 2017 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, but 2 ≠ 3. It is not transitive because, for example, (1,0) and (0, – 2) are both in R but (1, – 2) is not. © by A-H. Esfahanian. All Rights Reserved.

27 R = {(x,y) | x y = 0} Solution: It is not reflexive since (1,1)  R.
Sunday, April 23, 2017 R = {(x,y) | x y = 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 but 2 ≠ 0. It is not transitive because, for example, (1,0) and (0, – 2) are both in R but (1, – 2) is not. © by A-H. Esfahanian. All Rights Reserved.

28 R = {(x,y) | x = 1} Solution: It is not reflexive since (2,2)  R.
Sunday, April 23, 2017 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 symmetric since, 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 is transitive since if (1,y)  R and (y,z)  R, so is (1,z)  R . © by A-H. Esfahanian. All Rights Reserved.

29 R = {(x,y) | x = 1 or y = 1} Solution:
Sunday, April 23, 2017 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 symmetric as the roles of x and y are interchangeable. It is not antisymmetric since, for example, (2,1) and (1,2) are both in R but 2 ≠ 1. It is not transitive since, for example, (3,1) and (1,7) are both in R but (3,7)  R . © by A-H. Esfahanian. All Rights Reserved.

30 Summary Relation R IR S A T E ✓ R = {(x,y) | x + y = 0}
Sunday, April 23, 2017 Summary Relation R IR S A T E R = {(x,y) | x + y = 0} R = {(x,y) | x = ±y} {(x,y) | x – y is a rational number} R = {(x,y) | x = 2y} R = {(x,y) | x y ≥ 0} R = {(x,y) | x y = 0} R = {(x,y) | x = 1} R = {(x,y) | x = 1 or y = 1} © by A-H. Esfahanian. All Rights Reserved.

31 Sunday, April 23, 2017 Combining Relations Since relations are special kind of sets, all of the operators we used for combining sets could be used to combine relations. © by A-H. Esfahanian. All Rights Reserved.

32 Combining Relations – Examples
Sunday, April 23, 2017 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)} © by A-H. Esfahanian. All Rights Reserved.

33 Composition of Relations
Sunday, April 23, 2017 Composition of Relations Definition: Let R be a relation from a set A to a set B, and S be a relation from B to 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. A B c R S S o R © by A-H. Esfahanian. All Rights Reserved.

34 Example Let A={1, 2, 3}, B={x, y, w, z}, C={0, 1, 2},
Sunday, April 23, 2017 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 S a relation from B to C: S = {(x,0), (y,0), (w,1), (w,2), (z,1)} What is S o R? S o R = {(1,0), (1,1), (2,1), (2,2), (3,0), (3,1)} © by A-H. Esfahanian. All Rights Reserved.

35 Sunday, April 23, 2017 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 © by A-H. Esfahanian. All Rights Reserved.

36 Sunday, April 23, 2017 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. © by A-H. Esfahanian. All Rights Reserved.

37 Example Consider the following relations on {1,2,3,4}.
Sunday, April 23, 2017 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)} Is R transitive? Yes R2 = {(2,2), (2,3), (2,4), (3,2), (3,3), (3,4)} © by A-H. Esfahanian. All Rights Reserved.

38 Characterizing Transitive Relations
Sunday, April 23, 2017 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 that if 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 in Rk+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 © by A-H. Esfahanian. All Rights Reserved.

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, , CSE, 3.88), (Adams, , PHY, 3.45), (Chou, , CSE, 3.49), (Gould, , MTH, 3.45), (Rao, , MTH, 3.90), (Stevens, , 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)
Students (ID# is the primary key) Name ID# Major GPA Anderson 231455 CSE 3,88 Adams 888323 PHY 3.45 Chou 102147 3.49 Gould 453876 MTH Rao 678543 3.90 Stevens 786576 PSY 2.99

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)
Name ID# Major GPA Anderson 231455 CSE 3.88 Adams 888323 PHY 3.45 Chou 102147 3.49 Gould 453876 MTH Rao 678543 3.90 Stevens 786576 PSY 2.99 GPAs Name GPA Anderson 3.88 Adams 3.45 Chou 3.49 Gould Rao 3.90 Stevens 2.99 P1,4

45 P1,2 (Enrollments) Majors P1,2 Enrollments Name Major Course Glauser
BIO MS 475 PY 410 MS 511 Marcus MTH MS 603 Miller CSE MS 575 CS 455 Majors Name Major Glauser BIO Marcus MTH Miller CSE P1,2

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

47 SMajor = “CSE” (Enrollments)
Name Major Course Glauser BIO MS 475 PY 410 MS 511 Marcus MTH MS 603 Miller CSE MS 575 CS 455 CSE Enrollments Name Major Course Miller CSE MS 575 CS 455 SMajor = “CSE”

48 SMajor = “CSE”  GPA > 3.5 (Students)
Name ID# Major GPA Anderson 231455 CSE 3.88 Adams 888323 PHY 3.45 Chou 102147 3.49 Gould 453876 MTH Rao 678543 3.90 Stevens 786576 PSY 2.99 SMajor = “CSE”  GPA > 3.5 CSE Students on Dean’s list Name ID# Major GPA Anderson 231455 CSE 3.88

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) …
Teaching Assignments Professor Dept. Course No. Cruz ZOO 335 412 Farber PSY 501 617 Grammer PHY 544 551 Rosen CSE 518 MTH 575 Class Schedule Dept. Course No. Room Time CSE 518 N521 2:00 pm MTH 575 N502 3:00 pm 611 4:00 pm PHY 544 B505 PSY 501 A100 617 A110 11:00 am ZOO 335 9:00 am 412 8:00 am

51 Tables 5 and 6 joined over Dept&Course
Tuples of Table 5: cols 2,3 are matched to tuples of Table 6: cols 1, 2. The joined 5-tuples create a new relation The new relation adds (joins) the teacher information to the Class-schedule information the room and time information to the Teaching_assignment information

52 …yields a new table with 5 columns:
Professor Dept. Course No. Room Time Cruz ZOO 335 A100 9:00 am 412 8:00 am Farber PSY 501 3:00 pm 617 A110 11:00 am Grammer PHY 544 B505 4:00 pm Rosen CSE 518 N521 2:00 pm MTH 575 N502 Pure join: Keep only rows that can be joined (inner join)

53 One useful variation on “pure join”:
Professor Dept. Course No. Room Time Cruz ZOO 335 A100 9:00 am 412 8:00 am Farber PSY 501 3:00 pm 617 A110 11:00 am Grammer PHY 544 B505 4:00 pm 551 ? Rosen CSE 518 N521 2:00 pm MTH 575 N502 611 Outer join: keep tuples that don’t join; fill missing entries with special “undefined” value

54 Table 7 Teaching_schedule
Professor Department Course Number Room Time Cruz Zoology 335 A100 9:00 am 412 8:00 am Farber Psychology 501 3:00 pm 617 A110 11:00 am Grammer Physics 544 B505 4:00 pm Rosen Computer science 518 N521 2:00 pm Mathematics 575 N502

55 SQL: Structured Query Language
Query language references Tables and their Attributes and applies conditions to them SELECT Departure_time FROM Flights WHERE Destination = “Detroit” name of Table or relation project P5 (Flights) adds a select condition

56 Table 8 Flights Flights Airline Flight# Gate Destination Departure
Nadir 122 34 Detroit 08:10 Acme 221 22 Denver 08:17 33 Anchorage 08:22 323 Honolulu 08:30 199 13 08:47 222 09:10 322 09:44 SELECT Departure_time FROM Flights WHERE Destination = “Detroit” Result should be: 08:10 08:47 09:44

57 SQL example SELECT Professor, Time
FROM Teaching_assignments, Class_schedule WHERE Department = ‘Mathematics’ project these columns from the join of these tables provided this condition holds The “answer table” is a single 2-tuple: (Rosen, 3:00 pm)


Download ppt "Relations and Their Properties"

Similar presentations


Ads by Google