CS 200: Relationsfrance/CS200/Lectures/relations.pdf · Relations on a Set as Graphs Consider the...
Transcript of CS 200: Relationsfrance/CS200/Lectures/relations.pdf · Relations on a Set as Graphs Consider the...
CS 200: Relations
Rosen 8.1 – 8.5
Relations on a Set
A relation on a set A defines a relationship between elements of A
Example: relations on the set of integersR1 = {(a,b) | a ≤ b}R2 = {(a,b) | a > b}R3 = {(a,b) | a = b + 1}
Relations on a Set as Graphs
Consider the relation R on cities:R = {(a,b) | a,b are cities such that the population of a is smaller than that of b}
We can represent R as a directed graph where there is an edge from a to b if (a,b) is in R.
Binary Relations
A – set of students B – set of coursesR – pairs (a,b) such that student a is enrolled in course bR = {(chris, cs200), (mike,cs520),…}
A – set of cities B – set of US statesR – (a,b) such that city a is in state bR = {(Denver, CO), (Laramie, WY),…}
Binary Relations
A binary relation from a set A to a set B is a set R of ordered pairs (a,b) where a ∈ A and b ∈ B.
The notation aRb denotes (a,b) ∈ R Example: A = {0,1,2}, B = {a,b} and
R={(0,a),(0,b),(1,a),(2,b)}
Relations and Cartesian Products
Let A,B be sets The cartesian product of A and B is denoted by
A x B and is equal to:{ (a,b) | a ∈ A and b ∈ B}
A binary relation from A to B is a subset of A x B
Given sets A and B with sizes n and m respectively, the number of elements in A x B is n*m and the number of binary relations from A to B is 2nm
n-ary Relations
Definition: Let A1, A2, … , An be sets. An n-ary relation on these sets is a subset of
A1 x A2 x … x An. The sets A1, A2, … , An are called the domains of the relation, and n is called its degree.
Example: The between relation consisting of triples (a,b,c) where a,b,c are integers such that a < b < c
Example
Person X Person Y Person Z
Alice Bob DeniseCharles Alice BobCharles Charles CharlesDenise Denise Denise
Relation R: X thinks that Y likes Z
Databases and Relations
StudentsStudentName IDnumber Major GPA
AckermannAdamsChouGoodfriendRaoStevens
231455888323102147453876678543786576
Computer SciencePhysicsComputer ScienceMathematicsMathematicsPsychology
3.883.453.493.453.902.99
Databases defined by relations are called relational databases
Tuples
An ordered n-tuple is a sequence of n objects(x1, x2, …, xn)
First component is x1…n-th component is xn
An ordered pair: 2-tuple (x, y) An ordered triple: 3-tuple (x, y, z)
Tuples vs Sets
Two tuples are equal iff they are equal coodinate-wise(x1, x2, …, xn) = (y1, y2, …, yn) iff
x1 = y1, x2 = y2, …, xn = yn
(2, 1) ≠ (1, 2), but {2, 1} = {1, 2} (1, 2, 1) ≠ (2, 1), but {1, 2, 1} = {2, 1}
Functions as Relations
A function f from A to B assigns an element of B to each element of A.
Differences between relations and functions?
Properties of Relations
A – the set of actorsR = {(a, b) : a,b are actors that have played in the same movie}
R has the property that if aRb then bRa. It is a symmetric relation Can be represented as an undirected graph
Properties of Relations
A relation R on a set A is called transitive if whenever aRb and bRc then aRc for all a,b,cin A.
Example: the ancestor relation
A relation R on a set A is called reflexive if aRa for all a in A
Example: the less-or-equal to relation on the positive integers
Composite Relations
Let R be a relation from A to B, and let S be a relation from B to C. The compositeSR of R and S is defined as:
SR = {(a,c) | ∃b: aRb ∧ bSc}
Example: Let R be the relation such that aRbif a is a parent of b. What is the relation RR?
Composite Relations
R2= RR =(a, c) (e, c) (b, d) (d, d) (c, c)
a
bc
d
e
a
b c
d
e
R R2
Composite Relations
Example: Function composition.
SR(a,c) iff ∃b: aRb ∧ bScIf S and R are functions andR(a)=b and S(b)=c then SR(a)=c
Composite Relations
The powers Rn of relationship R can be defined recursively:
R1 = R and Rn+1 = RnR
Paths and Relations
Let R be a relation on a set A. There is a path of length n from a to b in the graph representing R if and only if aRnb
Proof: by induction on n (Rosen p. 547)
Paths and Relations Define:
For a relation over a set with n elements
Let R be the relation between states in the US where aRb if a and b share a common border. What is R*?
What is R* for the parent relation? Example: A = {1, 2, b}. What is R* for:
R = {(1, 1), (b, b)} S = {(1, 2), (2, b), (1, b)} T = {(2, b), (b, 2), (1, 1)}
Transitive Closure The smallest transitive relation on A that
includes R is called the transitive closure of R. R* is the transitive closure of R (Rosen p. 548)
Transitive Closure
Lemma: Let R be a relation on a A set with nelements. If there is a path from a to b in R, then the path length does not exceed n.
Equivalence Relations
A relation R on a set A is an equivalence relation if it is reflexive (aRa for all a), symmetric (if aRb then bRa) and transitive (if aRb and bRc then aRc).
Example: congruence modulo 3 a,b are in the relation if a and b have the same remainder when divided by 3 (or equivalently 3 divides a-b).
Example: A - a set of strings. R – the relation on A that includes all strings that have the same length.
Equivalence Relations
What about the relation R on real numbers such that xRy iff |x – y| < 1?
Equivalence Classes
Let R be an equivalence relation on a set A. The set of elements related to a∈A is the equivalence class of a (denoted by [a]).
[a] = { x | (x,a) ∈ R }.Example: congruence modulo 3 on the integers
[0] = {…,–6,–3,0,3,6,…} [1] = {…,-5,-2,1,4,7,…}
Any element can be used to define an equivalence class
Equivalence Relations Let us look at the equivalence relation:
A = {x | x is a student in cs200} xRy iff “x sits in the same row as y”
Grouping all students that are related:
We have partitioned A in such a way that everyone in the class belongs to exactly one subset.
row-1row-2
row-5row-3
row-4
Equivalence Relations
A partition of a set A is a collection of subsets {Aj} such that - Ai∩Aj = ∅ for all i≠j (no overlap between the Ai’s)- A = ∪j Aj
Example: A = {1, 2, 3, 4} {{1}, {2}, {3}, {4}} is a partition of A {{1, 2}, {3, 4}} a partition of A
Example: The modulo-3 partition of the integers is {{…,–3,0,3,…}, {…,–2,1,4,…}, {…,–1,2,5,…}}
Equivalence Relations
Theorem 1 (Rosen p. 560)Let R be an equivalence relation on a set A. These statements for elements a and b of A are equivalent:(i) aRb (ii) [a] = [b] (iii) [a] ∩ [b] ≠ ∅
Conclusion: The equivalence classes of a relation R on a set A form a partition of A(start with A = ∪a [a])
Partitions and Equivalence Relations
Theorem 2: Given a partition of a set A into sets Aithere is an equivalence relation R that has the sets Ai as its equivalence classes.
Properties of Relations
A relation R is antisymmetric if (a,b)∈R and (b,a)∈R imply a=b (this is not “not symmetric”).
Example: the less-or-equal to relation on the positive integers
Order Relations
A relation R in A is a partial ordering if and only of if it is reflexive (aRa for all a), antisymmetric (if aRb and bRa then a=b) and transitive (if aRb and bRc then aRc).
Examples: ≤ and ⊆
(A,R) is a partially ordered set / poset if R is a partial ordering on A. Think “no loops”.
If a≤b or b≤a, the elements a and b are comparable.If all pairs are comparable, ≤ is a total ordering or chain.