560 likes | 834 Views
Relations and Their Properties. Section 9.1 , 9.3 Tuesday, June 17. 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.
E N D
Relations and Their Properties Section 9.1, 9.3 Tuesday, June 17 RELATIONS
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. • ordered 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 RELATIONS
Review: General relation • Definition:The Cartesian product of the sets A1, A2, …, An: A1×A2× …×An={(a1, a2, …, an)| aiAifor i=1, 2, …, n} • Definition: A relation R over the setsA1, A2, …, An is a subset of A1×A2× …×An : R ⊆ A1×A2× …×An • The number n is called the arity of the relation; equivalently, R is called an n-ary relation. RELATIONS
Binary Relations • Definition:Let A and B be sets. A binary relation Rfrom A to B is a subset of the Cartesian product AB. • Notation: xRy means (x,y) R if xRy, we say x is related to y under R. xRy is short for (x,y) R. RELATIONS
Example • Let A be the set of all cities in the U.S., and let B be the set of states in the U.S. • Define the relation S from A to B by: aSb if citya is in state b. • Some ordered pairs belonging to relation S: • (East Lansing, MI) S • (Chicago, IL) S • (Boulder, CO) S • … (East Lansing) S (MI) (Chicago) S (IL) (Boulder) S (CO) RELATIONS
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? What about the relation S that relates a city to the state that the city is in? What about the relation represented by a Python namespace? RELATIONS
Functions and Relations • Recall: If f: A→ B is a function, the graph of f is depicted by plotting the set of ordered pairs Gf = {(x, f (x))| x A}. • More generally, we can graph a binary relation. • Example: Define xRy iff x2 + y2 = 5. • Is this relation a function? How can youtell by looking at the graph? 5 -5 5 -5 RELATIONS
Binary Relations on a Set • Definition: A binary relation R on a set A is a relation from A to A, i.e., R AA. • Example: Let A = {1, 2, 3, 4}, and define D = {(a,b) | ∃m (m ∈A∧b = am)}. • Exercise: What pairs does D contain? D = {(1,1), (1,2), (1,3), (1,4), (2,2), (2,4), (3,3), (4,4)} Thus: 1D1, 1D2, 1D3, 1D4, 2D2, 2D4, 3D3, 4D4 RELATIONS
1 1 2 2 3 3 4 4 Different ways to show a binary relation • D can be displayed graphically, in tabular form, as a zero-one matrix, or as a directed graph: 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 RELATIONS
Directed Graph Representation of a Binary Relation on a Finite Set • Directed Graphs • Vertices, one for each element in the set • Directed edges, one for each ordered-pair in the relation • Example: Let A = {1, 2, 3, 4}, and define aDb ↔ ∃m (m ∈A∧b = am). 1 2 4 3 RELATIONS
Exercise: 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 AA. There are n2 elements in AA. Thus, P(AA), the power set of AA, has 2n2elements. Therefore, there are 2n2relations on a set with n elements. RELATIONS
Types of Relations • Definition:A relation R on a set A is reflexive if (a,a) R for every element aA, that is, ∀a ( aRa ) • Definition:A relation R on a set A is irreflexive if (a,a) R for every element aA, that is, ∀a ( aRa ) RELATIONS
Exercise Reflexive: ∀a ( aRa ) • Consider the following relations on {1,2,3,4}. Classify each as reflexive, irreflexive, both, or neither. • R1 = {(1,1), (1,2), (2,1), (2,2), (3,3), (4,4)} • R2 = {(2,4), (4,2)} • 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)} • Can a relation on a set A be both reflexive and irreflexive? • Is every relation on A either reflexive or irreflexive? Irreflexive:∀a ( aRa ) RELATIONS
Symmetric Relations • Definition:A relation R on a set A is symmetric if (b,a) R whenever (a,b) R, for a, bA; that is, ∀a,b ( aRb → bRa ) • Definition A relationR on a set A is antisymmetric if a = b whenever both (a,b) R and (b, a) R, for a, b in A; that is, ∀a,b ( aRb ∧ bRa → a = b ) RELATIONS
Exercise Symmetric: ∀a,b ( aRb → bRa ) • Classify each relation below as “symmetric,” “antisymmetric,” “both”, or “neither.” • R1 = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 3), (4, 4)} on {1, 2, 3, 4} • R2 = {(2, 4), (4, 2)} on {1, 2, 3, 4} • R3 = {(1, 2), (2, 3), (3, 4)} on {1, 2, 3, 4} • R4 = {(1, 1), (2, 2), (3, 3), (4, 4)} on {1, 2, 3, 4} • R5 = {(1, 3), (1, 4), (2, 3), (2, 4), (3, 1), (3, 4)} on {1, 2, 3, 4} • The“less than or equal to” relation (i.e., “≤” ) on the positive integers • The “less than” relation (i.e., “<“) on the positive integers • These definitions are not complementary. • Give a relation that is both symmetric and antisymmetric. • Give a relation that is neither symmetric not antisymmetric. Antisymmetric: ∀a,b ( aRb ∧ bRa → a = b ) RELATIONS
Transitive Relations • Definition:A relationR on a set A is transitiveif whenever (a, b) R and (b, c) R, then (a, c) R, for a, b, c in A, that is, ∀ a, b, c ( aRb ∧ bRc → aRc ) RELATIONS
Exercise Transitive: ∀ a, b, c ( aRb ∧ bRc → aRc ) • Classify each relation below as “transitive” or “not transitive” • R1 = {(1, 1), (1, 2), (2, 1), (2, 2), (3, 3), (4, 4)} on {1, 2, 3, 4} • R2 = {(2, 4), (4, 2)} on {1, 2, 3, 4} • R3 = {(1, 2), (2, 3), (3, 4)} on {1, 2, 3, 4} • R4 = {(1, 1), (2, 2), (3, 3), (4, 4)} on {1, 2, 3, 4} • The “less than” relation (i.e., “<“) on the positive integers • The “successor” relation S on the integers defined by: nSmiffm = n + 1 • The “subtype” (“subclass”) relation on the set of types (classes) in an OO program. • The “defeated” relation on the set of football teams in the Big 10 RELATIONS
Exercise • Determine which properties hold for the relations defined below 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} RELATIONS
R = {(x, y) | x + y = 0} • Solution: • R is not reflexivesince, for example, 1R1. • R is not irreflexivesince, for example, 0R0. • If x + y = 0 then y + x = 0, so xRy implies yRx, which means R is symmetric. • R is not antisymmetric since, for example, (–1)R1 and 1R(–1) are both in R, but 1 ≠ –1 • R is not transitive since, for example, 1R(–1) and (–1)R1, but 1R1. RELATIONS
R = {(x,y) | x = y x = – y} • Solution: • Since xRx, for each x, relation R is reflexive. • Since R is reflexive, it is not irreflexive. • Since x = ±y if and only if y = ±x, xRy if and only if xRy, for all x and y; thus, R is symmetric. • R is not antisymmetric since, for example, (–1)R1 and 1R(–1) are both in R, but 1 ≠ –1 . • R is also transitive because essentially the product of ±1 and ±1 is ±1. RELATIONS
R = {(x,y) | x – y is a rational number} • Solution: • R is reflexive since, for all x, x – x = 0 is a rational number. • Since R is reflexive, it is not irreflexive. • R is symmetric because, if x – y isrational, then so is – (x – y) = y – x . • R is not antisymmetric because, for example, (–1)R1 and 1R(–1) are both in R, but 1 ≠ –1 . • R is transitive because if x – y is a rational and y – z is a rational, so is x – y + y – z = x – z. RELATIONS
R = {(x,y) | x = 2y} • Solution: • R is not reflexive since, for example, (1,1) R. • R is not irreflexive since, for example, (0,0) R. • R is not symmetric since, for example, (2,1) Rbut (1, 2) R. • R is antisymmetric because x = y = 0 is the only time that (x, y) and (y, x) are both in R. • R is not transitivesince, for example, (4, 2) R and (2, 1) R but (4, 1) R. RELATIONS
R = {(x,y) | x y ≥ 0} • Solution: • R is reflexive since x·x≥ 0. • Since R is reflexive, it is not irreflexive. • R is symmetric as the role of x and y are interchangeable. • R is not antisymmetric since, for example, (2,3) and (3,2) are both in R, but2 ≠ 3. • R is not transitive because, for example, (1,0) and (0, – 2) are both in R but (1, – 2) is not. RELATIONS
R = {(x,y) | xy = 0} • Solution: • R is not reflexive since (1,1) R. • R is not irreflexive since (0,0) R. • R is symmetric as the role of x and y are interchangeable. • R is not antisymmetric since, for example, (2,0) and (0,2) are both in R but2 ≠ 0. • R is not transitive because, for example, (1,0) and (0, – 2) are both in R but (1, – 2) is not. RELATIONS
R = {(x, y) | x = 1} • Solution: • R is not reflexive since (2, 2) R. • R is not irreflexive since (1, 1) R. • R is not symmetricsince, for example, (1, 2) R but (2, 1) R. • R is antisymmetric because (x, y) R and (y, x) R only ifx = y = 1. • R istransitive since (x, y) R and (y, z) R only if x = 1, which implies (x, z) R . RELATIONS
R = {(x, y) | x = 1 or y = 1} • Solution: • R is not reflexive since (2, 2) R. • R is not irreflexive since (1, 1) R. • R is symmetricas the roles of x and y are interchangeable (i.e., x =1 or y =1 iff y =1 or x =1). • R is not antisymmetric since, for example, (2, 1) and (1, 2) are both in R but2 ≠ 1. • R isnot transitive since, for example, (3, 1) and (1, 7) are both in R but (3,7) R. RELATIONS
Summary RELATIONS
Combining Relations • Since relations are special kind of sets, all of the operators we used for combining sets could be used to combine relations. RELATIONS
Combining Relations – Examples • Since relations are sets, all of the operators we used for combining sets can be used to combine relations. • Example: Let R1 and R2 on A = { 1, 2, 3, 4} be defined as follows: • R1 = {(1,1), (2,2), (3,3)} • R2 = {(1,1), (1,2), (1,3), (1,4)} • Then the following are also relations on A • R1R2 = {(1,1), (2,2), (3,3), (1,2), (1,3), (1,4)} • R1R2 = {(1,1)} • R1−R2 = {(2,2), (3,3)} RELATIONS
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 oR, is the relation consisting of ordered pairs (a, c), where: • a A,c C, and • b (b B andaRb and bSc). S oR R S a b c B C A RELATIONS
Exercise • 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 isS oR? • S oR= {(1,0), (1,1), (2,1), (2,2), (3,0), (3,1)} RELATIONS
Powers of Relations • Definition:Let R be a relation on the set A. The powers of R, denoted Rn, for n = 1, 2, 3,…, are defined recursively by: • R1 = R, • Rn+1=RnoR, for n 1. • So: • R2 = R o R, • R3 = R2 oR = (R oR) o R • R4 = R3 oR = ((R oR) o R) o R • … RELATIONS
Exercise • Consider the relation R= {(1, 2), (1, 3), (3, 4)} on{1, 2, 3, 4}. • Is R transitive? • Calculate R2 • Consider R={(2, 2), (2, 3), (2, 4), (3, 2), (3, 3), (3, 4)} on {1, 2, 3, 4} • Is R transitive? • Calculate R2 • Calculate R3 • What will you get if you calculate R153 ? RELATIONS
Rn+1 Rn+1 = Rn o R R Rn x z y A A A In the digraph representation, Rn consists of all the order pairs (x,y) where y is reachable from x by traveling a directed path of length n. RELATIONS
Characterizing Transitive Relations • Theorem: The relation R on a set A is transitive if and only if RnR for n= 1, 2, 3, ... • If Part: If RnR for n= 1, 2, 3, ... then R is transitive • See exercise 6.1 • Only If Part: If R is transitive then RnR for n= 1,2,3, ...(*) We prove (*) inductively. • Basis Step: The basis holds trivially since R1 R, for all relations R. RELATIONS
Characterizing Transitive Relations • Induction Step: Assume k ≥ 1 and that if R is transitive then R kR (IH). We need to show: if R is transitive, then Rk+1R (**). Assume that R is transitive, and let (a,b) Rk+1. By the defn of Rk+1, there is somexA such that (a,x) R and (x,b) Rk. Since R is transitive, the IH implies RkR, which implies (x,b) R. Since R is transitive and (a,x) R, this latter result implies (a,b) R. Thus, Rk+1R RELATIONS
Stopped here (Spring 2012) RELATIONS
Applications in Relational Databases Section 8.2 RELATIONS
Records as n-tuples (with fields) • A record is an n-tuple whose entries, calledfields, associate a value from an attribute domain to each of a fixed set of attributes, where n is the cardinality of the attributed set. • e.g., astudent record might be 4-tuple giving values for the attributes: (name, ID#, Major, GPA) • Relational Data Base: a set of records • e.g. a 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) } • Primary key: attribute whose value uniquely determines a record. RELATIONS
Table 1 Student Data:(Student_Name, PID, Major, GPA) RELATIONS
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) • , , –, … RELATIONS
Projection example: 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” RELATIONS
Projection of Students: P1,4 (Students) P1,4 RELATIONS
P1, 2 (Enrollments) P1,2 RELATIONS
Selection: SC • Select all records (rows) that satisfy the condition C RELATIONS
SMajor = “CSE” (Enrollments) SMajor = “CSE” RELATIONS
SMajor = “CSE” GPA > 3.5 (Students) SMajor = “CSE” GPA > 3.5 RELATIONS
Join: Jp (R, S) • Join 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 RELATIONS
Table 5 Teaching_Assignments RELATIONS
Table 6 Class_schedule RELATIONS