relations and their properties
DESCRIPTION
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 . - PowerPoint PPT PresentationTRANSCRIPT
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 Ai for i =1, 2, …, n.
A1×A2× …×An = {(a1, a2, …, an) | aiAi for i=1, 2, …, n}
4
Binary Relations Definition: Let A and B be sets. A binary
relation R from A to B is a subset of the Cartesian product AB.
Notation: xRy means (x,y)R, and x is said to be related to y
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 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), …
6
Another Example
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 x x
c x
d x
cb
d23
1a
Is this relation a function?
7
Functions As Relations Recall that: Let f: A → B be a function.
The graph of f is the set of ordered pairsGf = {(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 on a set A is a
relation from A to A, that is, a subset of AA.
Example: Let A = {1, 2, 3, 4}, andR = {(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
Example R can be displayed graphically and in
tabular form/zero-one matrix form:R 1 2 3 41 x x x x 1
1 1 12 x x 0
1 0 13 x
0 0 1 04 x
0 0 0 1
3
2
4
3
4
1
2
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 AA. There are n2 elements in AA. Thus, P(AA), the power set of AA, has 2n2 elements. Therefore, there are 2n2 relations on a set with n elements.
11
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.
( ( , ) )a a A a a R
( ( , ) )a a A a a R
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 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 b a A b A aRb bRa
( )a b a A b A aRb bRa a b
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 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,
( )a b c a A b A c A aRb bRc aRc
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
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
43
2
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)}0 0 0 0
0 1 1 1
0 1 1 1
0 0 0 0
20
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 }
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) | x y ≥ 0} R = {(x,y) | x y = 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 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.
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 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.
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)
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.
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, but 2 ≠ 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) | 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.
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 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 .
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 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 .
30
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} ✓
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
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:
aA, cC, and bB | (a,b)R and (b,c)S.
A B c
R S
S S oo RR
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
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)}
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
A A A
R Rn
Rn+1=Rn o R
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)}
Is R 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 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
Applications in Relational Databases
Section 8.2
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.)
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 CSE 3.49
Gould 453876 MTH 3.45
Rao 678543 MTH 3.90
Stevens 786576 PSY 2.99
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)
, , –, …
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”
Projection of Students: P1,4
(Students) Students
Name ID# Major GPA
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
GPAsName GP
A
Anderson 3.88
Adams 3.45
Chou 3.49
Gould 3.45
Rao 3.90
Stevens 2.99
P1,4
P1,2 (Enrollments)
Enrollments
Name Major
Course
Glauser BIO MS 475
Glauser BIO PY 410
Glauser BIO MS 511
Marcus MTH MS 603
Miller CSE MS 575
Miller CSE CS 455
MajorsName Major
Glauser BIO
Marcus MTH
Miller CSE
P1,2
Selection: SC
Select all records (rows) that satisfy the condition C
SMajor = “CSE” (Enrollments)
Enrollments
Name Major
Course
Glauser BIO MS 475
Glauser BIO PY 410
Glauser BIO MS 511
Marcus MTH MS 603
Miller CSE MS 575
Miller CSE CS 455
SMajor = “CSE”
CSE Enrollments
Name Major
Course
Miller CSE MS 575
Miller CSE CS 455
SMajor = “CSE” GPA > 3.5 (Students)
SMajor = “CSE” GPA > 3.5
Students
Name ID# Major GPA
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
CSE Students on Dean’s list
Name ID# Major GPA
Anderson 231455 CSE 3.88
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
Example: Joining two tables (on 2 cols) …
Teaching Assignments
Professor
Dept.
Course No.
Cruz ZOO 335
Cruz ZOO 412
Farber PSY 501
Farber PSY 617
Grammer PHY 544
Grammer PHY 551
Rosen CSE 518
Rosen MTH 575
Class Schedule
Dept.
Course No.
Room
Time
CSE 518 N5212:00 pm
MTH 575 N5023:00 pm
MTH 611 N5214:00 pm
PHY 544 B5054:00 pm
PSY 501 A1003:00 pm
PSY 617 A11011:00 am
ZOO 335 A1009:00 am
ZOO 412 A1008:00 am
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
…yields a new table with 5 columns:
Professor
Dept.
Course No.
Room Time
Cruz ZOO 335 A1009:00 am
Cruz ZOO 412 A1008:00 am
Farber PSY 501 A1003:00 pm
Farber PSY 617 A11011:00 am
Grammer PHY 544 B5054:00 pm
Rosen CSE 518 N5212:00 pm
Rosen MTH 575 N5023:00 pm
Pure join:
Keep only
rows that can
be joined
(inner join)
One useful variation on “pure join”:
Professor
Dept.
Course No.
Room Time
Cruz ZOO 335 A1009:00 am
Cruz ZOO 412 A1008:00 am
Farber PSY 501 A1003:00 pm
Farber PSY 617 A11011:00 am
Grammer PHY 544 B5054:00 pm
Grammer PHY 551 ? ?
Rosen CSE 518 N5212:00 pm
Rosen MTH 575 N5023:00 pm
? MTH 611 N5214:00 pm
Outer join:
keep tuples
that don’t join;
fill missing
entries with
special
“undefined”
value
Table 7 Teaching_scheduleProfessor Departme
ntCourseNumber
Room Time
Cruz Zoology 335 A100 9:00 am
Cruz Zoology 412 A100 8:00 am
Farber Psychology 501 A100 3:00 pm
Farber Psychology 617 A110 11:00 am
Grammer Physics 544 B505 4:00 pm
Rosen Computer science
518 N521 2:00 pm
Rosen Mathematics
575 N502 3:00 pm
SQL: Structured Query Language
Query language references Tables and their Attributes and applies conditions to them
SELECT Departure_time FROM Flights WHERE Destination = “Detroit”
project P5
(Flights)
name of Table or relation
adds a select condition
Table 8 FlightsFlights
Airline Flight#
Gate
Destination Departure
Nadir 122 34 Detroit 08:10
Acme 221 22 Denver 08:17
Acme 122 33 Anchorage 08:22
Acme 323 34 Honolulu 08:30
Nadir 199 13 Detroit 08:47
Acme 222 22 Denver 09:10
Nadir 322 34 Detroit 09:44
Result should be: 08:10 08:47 09:44
SELECT Departure_time FROM Flights WHERE Destination = “Detroit”
SQL example
SELECT Professor, Time
FROM Teaching_assignments, Class_schedule
WHERE Department = ‘Mathematics’
project these columns
from the join of these tables
The “answer table” is a single 2-tuple: (Rosen, 3:00 pm)
provided this condition holds