Lecture 14 Relations CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

28
Lecture 14 Relations CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine

Transcript of Lecture 14 Relations CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine.

Lecture 14Relations

CSCI – 1900 Mathematics for Computer Science

Fall 2014

Bill Pine

CSCI 1900 Lecture 14 - 2

Lecture Introduction

• Reading– Rosen - Sections 9.1, 9.2, 9.3

• Cartesian Product• Partitioning• Relations• Representing Relations as a Matrix• Digraph• Properties of Relations• Equivalence Relations

CSCI 1900 Lecture 14 - 3

Cartesian Product for 2 Sets

• If A and B are nonempty sets, the product set or Cartesian Product, A x B is the set of all ordered pairs (a,b) with a A and b B

• The ordered pair (a,b) is called a 2-tuple• Example A={1,2,3} B={r,s}

– A x B={ (1, r), (2, r), (3, r), (1, s), (2, s), (3, s) }• Example Z x Z, set of all discrete points in

the plane• Note that (1,r) (r,1)

CSCI 1900 Lecture 14 - 4

Cartesian Product of m Sets

• If A1, A2, …, Am are nonempty sets, then the Cartesian Product of them is the set of all ordered m-tuples (a1, a2, …, am), where ai Ai, i = 1, 2, … m

• Denoted A1 A2 … Am = { (a1, a2, …, am) | ai Ai, i = 1, 2, … m }

CSCI 1900 Lecture 14 - 5

Cartesian Product Examples

• Let A = {1, 2, 3} and B = {a, b, c},– A B = {(1,a), (1,b), (1,c), (2,a), (2,b), (2,c),

(3,a), (3,b), (3,c)}

• Let A = {1, 2, 3}, B = {a, b, c}, C = {T, F}– A B C= {(1,a,T), (1,b,T), (1,c,T), (2,a,T),

(2,b,T), (2,c,T), (3,a,T), (3,b,T), (3,c,T), (1,a,F), (1,b,F), (1,c,F), (2,a,F), (2,b,F), (2,c,F), (3,a,F), (3,b,F), (3,c,F)}

CSCI 1900 Lecture 14 - 6

Cardinality of Cartesian Product

The Cardinality of the Cartesian Product equals the product of the cardinality of all sets involved sets:

| A1 A2 … Am | = | A1 | | A2 | … | Am |

CSCI 1900 Lecture 14 - 7

Partition

• A Partition P of a nonempty set A, is the set of nonempty subsets such that– Each element in A belongs to one set in P– Sets in P are disjoint

• Example: Let A={1, 2, 3, 4, 5, 6}– A1 ={1, 3, 4} A2 ={2, 6} A3= {5}

–P = {A1 , A2 , A3 } is a partition of A

CSCI 1900 Lecture 14 - 8

Additional Partition Example

• Let A={a, b, c, d, e, f, g, h}A1 ={a, b, c, d} A2 ={a, c, e, f, g, h}

A3= {a, c, e, g} A4 ={b, d} A5 ={f, h}

– Is P = {A1, A2 } a partition of A?

– Is P = {A1, A5 } a partition of A?

– Is P = {A3, A4 , A5} a partition of A?

CSCI 1900 Lecture 14 - 9

Subsets of the Cartesian Product

• Many of the results of operations on sets produce subsets of the Cartesian Product set

• Relational database– Each column in a database table can be considered a set– Each row is an m-tuple of the elements from each

column or set– No two rows should be alike

• Example – Employee ID, Last Name, Department, Salary– Database is a subset of ID x Name x Depart x Salary

CSCI 1900 Lecture 14 - 10

Relations

• A relation, R, is a subset of a Cartesian Product together with a property that defines whether a given m-tuple is a member of the relation or not

• Terminology: Relation R from A to B– R A B– Denoted by

• “x R y” where x A and y B • x has a relation with y

• means: x does not have a relation with yx R y

CSCI 1900 Lecture 14 - 11

Relation Example

• Let– A be the set of all students, and – B be the set of all courses

• A relation R may be defined because certain courses are required by concentration

Burf Snerfle R CSCI 2710

Ginger Ayle R CSCI 2710

CSCI 1900 Lecture 14 - 12

Domain and Range

• Example: A={1, 2, 3, 6} B={1, 3, 4} – Let Q be the relation, a R b if a < b– Q={ (1, 3), (1, 4), (2, 3), (2, 4), (3, 4) }

• Domain of Q, Dom(Q) ={1, 2, 3}– The set of elements in A that are related to some

element in B• Range of Q, Ran(Q) ={3, 4}

– The set of elements in B that are related to some element of A

CSCI 1900 Lecture 14 - 13

Relations Across Same Set

• Relations may be defined upon a single set• Terminology: Relation R on A

R A A• Consider the X-Y Plane you used to plot

points

CSCI 1900 Lecture 14 - 14

Relation on a Single Set Example

• A is the set of all courses• A relation P may be defined as the course is

a prerequisite for• Let P be the relation, C1 R C2

– C1 is a prerequisite for C2

• P = {(CSCI 2150, CSCI 3400), (CSCI 1710, CSCI 2910), (CSCI 2800, CSCI 2910), …}

CSCI 1900 Lecture 14 - 15

Represent A Relation As A Matrix

• Let: A = { a1, a2, a3, …, am} with |A| = m B = { b1, b2, b3, …, an} with |B| = n

• With Q a relation from A to B

• Represent Q as an m x n matrix, MQ , with

mi j = 1 if (ai, bj) Q

= 0 if (ai, bj) Q

CSCI 1900 Lecture 14 - 16

Matrix Example

• Let: A = { 1, 2, 3 } B = { r, s }

• With the relation Q from A to B

Q = { (1, r), (2, s), (3, r) }• Then

1 0

M = 0 1

1 0

CSCI 1900 Lecture 14 - 17

Digraph of a Relation

• Let Q be a relation on A• We can represent Q pictorially as follows

– Each element of A is a circle called a Vertex– If ai is related to aj, then draw an arrow from the

vertex ai to the vertex aj

– These arrows are called Arcs or Edges

• The resulting pictorial is called a digraph (directed graph)

CSCI 1900 Lecture 14 - 18

Digraph of a Relation (cont)

• In-Degree of a vertex– Number of arcs pointing into a vertex

• Out-Degree of a vertex– Number of arcs pointing out from a vertex

CSCI 1900 Lecture 14 - 19

Representing a Relation

Two representations of the same relation Q on A = {1, 2, 3}

Q = { (1, 1), (1, 3), (2, 3), (3, 2), (3, 3) }

1

2

3

NodeInDegree

OutDegree

1 1 2

2 1 1

3 3 2

CSCI 1900 Lecture 14 - 20

Paths in a Digraph

• A walk from one node to the next, following the arrows is a Path– Vertex1, Edge1, Vertex2, Edge2, Vertex3, Edge3,

Vertex4– A vertex may occur multiple times in a path; an edge

may occur only once• Paths start and end at a vertex• The number of edges in the path is its Length

– The above example is length 3 • A path that starts and ends at the same node is a

cycle

CSCI 1900 Lecture 14 - 21

Properties of Relations

• Recall– If Q A x A, then Q is a relation on A

• Relations on a set may have three properties– Reflexive– Symmetric– Transitive

CSCI 1900 Lecture 14 - 22

Reflexive

• A relation Q on set A, is reflexive if for all a A, then (a, a) Q

• Example: Let A={1, 2, 3, 4}

Q={ (1,1), (1,4), (2,1), (2,2),

(2,4), (3,3), (4,3), (4,4) }1

2

3

4

CSCI 1900 Lecture 14 - 23

Reflexive( cont)

• A relation Q on set A, is irreflexive if no element relates to itself

CSCI 1900 Lecture 14 - 24

Symmetric

• A relation Q on set A, is symmetric – If (a, b) Q then (b, a) is also Q

• A = {1, 2, 3} Q = { (1, 1), (1, 2), (2, 1), (2, 3), (3, 2) }

1

2

3

4

CSCI 1900 Lecture 14 - 25

Symmetric ( cont)

• A relation Q on set A, is asymmetric if whenever (a, b) Q, (b, a) Q

CSCI 1900 Lecture 14 - 26

Transitive

• A relation Q on set A, is transitive if (a, b) Q and (b, c) Qthen (a, c) Q

• Example: Let A={ 1, 2, 3 } R={ (1, 2), (2, 3), (1, 3) }

1

2

3

4

CSCI 1900 Lecture 14 - 27

Equivalence Relations

• A relation Q on a set A is an equivalence relation if the relation is– Reflexive– Symmetric– Transitive

CSCI 1900 Lecture 14 - 28

Key Concepts Summary

• Cartesian Product• Partitioning• Relations• Representing Relations as a Matrix• Digraph• Properties of Relations• Equivalence Relations