Lecture 14 Relations CSCI – 1900 Mathematics for Computer Science Fall 2014 Bill Pine
CSCI 1900 Lecture 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 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 Cartesian Product of m Sets If A 1, A 2, …, A m are nonempty sets, then the Cartesian Product of them is the set of all ordered m-tuples (a 1, a 2, …, a m ), where a i A i, i = 1, 2, … m Denoted A 1 A 2 … A m = { (a 1, a 2, …, a m ) | a i A i, i = 1, 2, … m }
CSCI 1900 Lecture 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 Cardinality of Cartesian Product The Cardinality of the Cartesian Product equals the product of the cardinality of all sets involved sets: | A 1 A 2 … A m | = | A 1 | | A 2 | … | A m |
CSCI 1900 Lecture 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} –A 1 ={1, 3, 4} A 2 ={2, 6} A 3 = {5} –P = {A 1, A 2, A 3 } is a partition of A
CSCI 1900 Lecture Additional Partition Example Let A={a, b, c, d, e, f, g, h} A 1 ={a, b, c, d} A 2 ={a, c, e, f, g, h} A 3 = {a, c, e, g} A 4 ={b, d} A 5 ={f, h} –Is P = {A 1, A 2 } a partition of A? –Is P = {A 1, A 5 } a partition of A? –Is P = {A 3, A 4, A 5 } a partition of A?
CSCI 1900 Lecture 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 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 y x R y
CSCI 1900 Lecture 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 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 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 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 Represent A Relation As A Matrix Let: A = { a 1, a 2, a 3, …, a m } with |A| = m B = { b 1, b 2, b 3, …, a n } with |B| = n With Q a relation from A to B Represent Q as an m x n matrix, M Q, with m i j = 1 if (a i, b j ) Q = 0 if (a i, b j ) Q
CSCI 1900 Lecture 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 =
CSCI 1900 Lecture 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 a i is related to a j, then draw an arrow from the vertex a i to the vertex a j –These arrows are called Arcs or Edges The resulting pictorial is called a digraph (directed graph)
CSCI 1900 Lecture 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 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) } Node In Degree Out Degree
CSCI 1900 Lecture 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 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 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) }
CSCI 1900 Lecture Reflexive( cont) A relation Q on set A, is irreflexive if no element relates to itself
CSCI 1900 Lecture 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) }
CSCI 1900 Lecture Symmetric ( cont) A relation Q on set A, is asymmetric if whenever (a, b) Q, (b, a) Q
CSCI 1900 Lecture Transitive A relation Q on set A, is transitive if (a, b) Q and (b, c) Q then (a, c) Q Example: Let A={ 1, 2, 3 } R={ (1, 2), (2, 3), (1, 3) }
CSCI 1900 Lecture Equivalence Relations A relation Q on a set A is an equivalence relation if the relation is –Reflexive –Symmetric –Transitive
CSCI 1900 Lecture Key Concepts Summary Cartesian Product Partitioning Relations Representing Relations as a Matrix Digraph Properties of Relations Equivalence Relations