CS 200: Relationsfrance/CS200/Lectures/relations.pdf · Relations on a Set as Graphs Consider the...

31
CS 200: Relations Rosen 8.1 – 8.5

Transcript of CS 200: Relationsfrance/CS200/Lectures/relations.pdf · Relations on a Set as Graphs Consider the...

Page 1: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

CS 200: Relations

Rosen 8.1 – 8.5

Page 2: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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}

Page 3: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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.

Page 4: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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),…}

Page 5: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 6: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 7: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 8: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

Example

Person X Person Y Person Z

Alice Bob DeniseCharles Alice BobCharles Charles CharlesDenise Denise Denise

Relation R: X thinks that Y likes Z

Page 9: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 10: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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)

Page 11: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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}

Page 12: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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?

Page 13: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 14: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 15: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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?

Page 16: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 17: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 18: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

Composite Relations

The powers Rn of relationship R can be defined recursively:

R1 = R and Rn+1 = RnR

Page 19: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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)

Page 20: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 21: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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)

Page 22: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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.

Page 23: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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.

Page 24: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

Equivalence Relations

What about the relation R on real numbers such that xRy iff |x – y| < 1?

Page 25: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 26: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 27: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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,…}}

Page 28: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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])

Page 29: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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.

Page 30: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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

Page 31: CS 200: Relationsfrance/CS200/Lectures/relations.pdf · 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

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.