Rosen 6 th ed., 8. hips between elements of sets occur in many contexts Example relationships in everyday life: employee and her salary, person and relative, business and it s phone number, etc. In mathematics we study relationships such as those between a positive integer and one that divides it, etc. 2 The most direct way to express a relationship between elements of two sets is to use ordered pairs (binary relations) A binary relation from A to B is a set R of ordered pairs where the first element of each ordered pair comes from A and the second element comes from B. We use the notation a R b to denote that (a, b) R and a a R b to denote that (a, b) R. In mathematics we study relationships such as those between a positive integer and one that divides it, etc. Recall the definition of the Cartesian (Cross) Product: The Cartesian Product of sets A and B, A x B, is the set A x B = {<x,y> : x A and y B}. A relation is just any subset of the CP!! R AxB Ex: A = students; B = courses. R = {(a,b) student a is enrolled in class b} 3 4 Recall the definition of a function: f = {<a,b> : b = f(a), aa and bb} Is every function a relation? Yes, a function is a special kind of relation on a Set from a set A to itself are of special interest A relation on the set A is a relation from A to A In other words, a relation on a set A is a subset of A x A 5 6 (c)2-22, Michael P. Frank
Properties of Properties of Reflexivity: A relation R on AxA is reflexive if for all a A, (a,a) R. Symmetry: A relation R on AxA is symmetric if (x,y) R implies (y,x) R. Anti-symmetry: A relation on AxA is anti-symmetric if (a,b) R, a b implies (b,a) R. Transitivity: A relation on AxA is transitive if (a,b) R and (b,c) R imply (a,c) R. Examples.. 7 8 7.2: n-ary 8.2: n-ary An n-ary relation R on sets A,,A n, written (with signature) R:A A n or R:A,,A n, is simply a subset R A A n. The sets A i are called the domains of R. The degree of R is n. R is functional in the domain A i if it contains at most one n-tuple (, a i, ) for any value a i within domain A i. 9 Relational Databases A relational database is essentially just an n-ary relation R. A domain A i is a primary key when the value of the n-tuple from this domain determines the n-tuple. That is, a domain is a primary key when no two n-tuples in the relation have the same value from this domain. Relational Databases A composite key for the database is a set of domains {A i, A j, } such that R contains at most n-tuple (,a i,,a j, ) for each composite value (a i, a j, )A i A j 2 (c)2-22, Michael P. Frank 2
Selection Operators Selection Operator Example Let A be any n-ary domain A=A A n, and let C:A {T,F} be any condition (predicate) on elements (n-tuples) of A. Then, the selection operator s C is the operator that maps any (n-ary) relation R on A to the n-ary relation of all n-tuples from R that satisfy C. I.e., RA, s C (R) = {ar s C (a) = T} Suppose we have a domain A = StudentName Standing SocSecNos Suppose we define a certain condition on A, UpperLevel(name,standing,ssn) : [(standing = junior) (standing = senior)] Then, s UpperLevel is the selection operator that takes any relation R on A (database of students) and produces a relation consisting of just the upperlevel classes (juniors and seniors). 3 4 Projection Operators Let A = A A n be any n-ary domain, and let {i k }=(i,,i m ) be a sequence of indices all falling in the range to n, That is, where i k n for all k m. Then the projection operator on n-tuples P{ ik } : A Ai A im is defined by: P a,..., a ) ( a,..., a ) { i }( k n i im Projection Example Suppose we have a ternary (3-ary) domain Cars=Model Year Color. (note n=3). Consider the index sequence {i k }=,3. (m=2) Then the projection P {i simply maps each tuple k } (a,a 2,a 3 ) = (model,year,color) to its image: ( ai, a ) (, ) (, ) i a a3 model color 2 This operator can be usefully applied to a whole relation RCars (a database of cars) to obtain a list of the model/color combinations available. 5 6 Join Operator Puts two relations together to form a sort of combined relation. If the tuple (A,B) appears in R, and the tuple (B,C) appears in R 2, then the tuple (A,B,C) appears in the join J(R,R 2 ). A, B, and C here can also be sequences of elements (across multiple fields), not just single elements. Join Example Suppose R is a teaching assignment table, relating Professors to Courses. Suppose R 2 is a room assignment table relating Courses to Rooms,Times. Then J(R,R 2 ) is like your class schedule, listing (professor,course,room,time). 7 8 (c)2-22, Michael P. Frank 3
Structured Query Language (SQL) Database query language Used to carry out operations that have been described Queries can be made on multiple tables, multiple fields, etc. Note: SQL uses the SELECT command as a projection rather than a selection operator 8.3 Representing 9 2 8.3: Representing Some ways to represent n-ary relations: With an explicit list or table of its tuples. With a function from the domain to {T,F}. Or with an algorithm for computing this function. Some special ways to represent binary relations: With a zero-one matrix. With a directed graph. Using Zero-One Matrices To represent a binary relation R:A B by an A B - matrix M R = [m ij ], let m ij = iff (a i,b j )R. E.g., Suppose Joe likes Susan and Mary, Fred likes Mary, and Mark likes Sally. Then the - matrix Susan Mary Sally representation Joe of the relation Fred Likes:Boys Girls relation is: Mark 2 22 Properties of Zero-One Reflexive, Symmetric Reflexivity: A relation R on A A is reflexive if for all a A, (a,a) R. Symmetry: A relation R on AxA is symmetric if (x,y) R implies (y,x) R. Anti-symmetry: A relation on A A is anti-symmetric if (a,b) R implies (b,a) R. Or a = b. 23 Terms: Reflexive, symmetric, and antisymmetric. These relation characteristics are very easy to recognize by inspection of the zero-one matrix. any-thing any- thing Reflexive: all s on diagonal Symmetric: all identical across diagonal Antisymmetric: all s are across from s 24 (c)2-22, Michael P. Frank 4
Using Directed Graphs Digraph Reflexive, Symmetric A directed graph or digraph G=(V G,E G ) is a set V G of vertices (nodes) with a set E G V G V G of edges (arcs,links). Visually represented using dots for nodes, and arrows for edges. Notice that a relation R:A B can be represented as a graph G R =(V G =AB, E G =R). Matrix representation M R : Graph rep. G R : Mary Joe Fred Mark Susan Joe Fred Mark Sally Edge set E G (blue arrows) Node set V G (black dots) Susan Mary Sally 25 It is extremely easy to recognize the reflexive/irreflexive/ symmetric/antisymmetric properties by graph inspection. Reflexive: Every node has a self-loop Irreflexive: No node links to itself Symmetric: Every link is bidirectional Antisymmetric: No link is bidirectional 26 Properties of Transitivity: A relation on A A is transitive if (a,b) R and (b,c) R imply (a,c) R. Graph?? 8.4 Closures of 27 28 8.4: Closures of Paths in Digraphs/Binary For any property X, the X closure of a set A is defined as the smallest superset of A that has the given property. The reflexive closure of a relation R on A is obtained by adding (a,a) to R for each aa. I.e., it is R I A The symmetric closure of R is obtained by adding (b,a) to R for each (a,b) in R. I.e., it is R R The transitive closure or connectivity relation of R is obtained by repeatedly adding (a,c) to R for each (a,b),(b,c) in R. I.e., it is n R * R nz 29 A path of length n from node a to b in the directed graph G (or the binary relation R) is a sequence (a,x ), (x,x 2 ),, (x n,b) of n ordered pairs in E G (or R). An empty sequence of edges is considered a path of length from a to a. If any path from a to b exists, then we say that a is connected to b. ( You can get there from here. ) A path of length n from a to itself is called a circuit or a cycle. Note that there exists a path of length n from a to b in R if and only if (a,b)r n. 3 (c)2-22, Michael P. Frank 5
Simple Transitive Closure Alg. A procedure to compute R * with - matrices. procedure transclosure(m R :rank-n - mat.) A := B := M R ; for i := 2 to n begin A := A M R ; B := B A {join} end {note A represents R i, B represents the - matrix for R*} return B {Alg. takes Θ(n 4 ) time} 8.5 Equivalence 8.6 Partial Orderings 3 32 8.5: Equivalence An equivalence relation (e.r.) on a set A is simply any binary relation on A that is reflexive, symmetric, and transitive. E.g., = itself is an equivalence relation. For any function f:a B, the relation have the same f value, or = f : {(a,a 2 ) f(a )=f(a 2 )} is an equivalence relation, e.g., let m= mother of then = m = have the same mother is an e.r. Equivalence Relation Examples Strings a and b are the same length. Integers a and b have the same absolute value. Real numbers a and b have the same fractional part. (i.e., a b Z) Integers a and b have the same residue modulo m. (for a given m>) 33 34 Equivalence Classes Let R be any equiv. rel. on a set A. The equivalence class of a, [a] R : { b arb } (optional subscript R) It is the set of all elements of A that are equivalent to a according to the eq.rel. R. Each such b (including a itself) is called a representative of [a] R. Since f(a)=[a] R is a function of a, any equivalence relation R can be defined using arb : a and b have the same f value, given f. Equivalence Class Examples Strings a and b are the same length. [a] = the set of all strings of the same length as a. Integers a and b have the same absolute value. [a] = the set {a, a} Real numbers a and b have the same fractional part (i.e., a b Z). [a] = the set {, a 2, a, a, a+, a+2, } Integers a and b have the same residue modulo m. (for a given m>) [a] = the set {, a 2m, a m, a, a+m, a+2m, } 35 36 (c)2-22, Michael P. Frank 6
Partitions 8.6: Partial Orderings A partition of a set A is the set of all the equivalence classes {A, A 2, } for some equivalence relation on A. The A i s are all disjoint and their union = A. They partition the set into pieces. Within each piece, all members of that set are equivalent to each other. A relation R on A is called a partial ordering or partial order iff it is reflexive, antisymmetric, and transitive. We often use a symbol looking something like (or analogous shapes) for such relations. Examples:, on real numbers,, on sets. Another example: the divides relation / on Z +. Note it is not necessarily the case that either a b or b a. A set A together with a partial order on A is called a partially ordered set or poset and is denoted (A, ). 37 38 (c)2-22, Michael P. Frank 7