360 likes | 615 Views
Discrete Mathematics 6 th edition, 2005. Chapter 3 Relations. Relations Equivalence Relations Matrices of Relations Relational Database*. 3.1 Relations. Given two sets X and Y its Cartesian product X Y is the set of all ordered pairs ( x , y ) where x X and y Y
E N D
Discrete Mathematics6th edition, 2005 Chapter 3 Relations • Relations • Equivalence Relations • Matrices of Relations • Relational Database*
3.1 Relations • Given two sets X and Y its Cartesian productXY is the set of all ordered pairs (x,y) where xX and yY • In symbols XY = {(x, y) | xX and yY} • A binary relationR from a set X to a set Y is a subset of the Cartesian product XY • Example: X = {1, 2, 3} and Y = {a, b} • R= {(1,a), (1,b), (2,b), (3,a)} is a relation between X and Y
Domain and range Given a relation R from X to Y, • The domain of R is the set • Dom(R) = { xX | (x, y) R for some yY} • The range of R is the set • Rng(R) = { yY | (x, y) R for some x X} • Example: • if X = {1, 2, 3} and Y = {a, b} • R = {(1,a), (1,b), (2,b)} • Then: Dom(R)= {1, 2}, Rng(R) = {a, b}
Example of a relation • Let X = {1, 2, 3} and Y = {a, b, c, d}. • Define R = {(1,a), (1,d), (2,a), (2,b), (2,c)} • The relation can be pictured by a graph:
1 2 3 4 A digraph of a relation R = {(x, y)| x≤y, x, y in X}, X = {1, 2, 3, 4} R = {(1, 1), (1, 2), (1, 3), (1, 4), (2, 2), (2, 3), (2, 4), (3, 3), (3, 4), (4, 4)}
Properties of relations Let R be a relation on a set X i.e. R is a subset of the Cartesian product XX • R is reflexive if (x,x) R for every xX • R is symmetric if for all x, y X such that (x,y) R then (y,x) R • R is transitive if (x,y) R and (y,z) R imply (x,z) R • R is antisymmetric if for all x,yX such that xy, (x,y)R then (y,x)R
Order relations Let X be a set and R a relation on X. R is a partial order on X if R is reflexive, antisymmetric and transitive. Let x,yX • If (x,y) or (y,x) are in R, then x and y are comparable • If (x,y) R and (y,x) R then x and y are incomparable If every pair of elements in X are comparable, then R is atotal order on X Ex) (x, y) in R if x divides y, x and y are positive integers. Ex) (x, y) in R if xy, x and y are positive integers.
Inverse of a relation Given a relation R from X to Y, its inverseR-1 is the relation from Y to X defined by R-1 = { (y,x) | (x,y) R } • Example: if R = {(1,a), (1,d), (2,a), (2,b), (2,c)} then R-1= {(a,1), (d,1), (a,2), (b,2), (c,2)}
Composition of relation • composition • R1 : relation from X to Y • R2 : relation from Y to Z • R2 R1 • composition of R1 and R2 • the relation from X to Z • R2 R1 = {(x,z) | (x,y)R1 and (y,z)R2 for some yY}
3.2 Equivalence relations Let X be a set and R a relation on X • R is an equivalence relation on X R is reflexive, symmetric and transitive. • Example: • Let X = {integers} and R be the relation on X defined by: xRy x - y = 5k, for some integer k • It is easy to show that R is an equivalence relation on the set of integers.
f c d a b h e g i Example • Relation R on XX = {a, b, c, d, e, f, g, h, i}F1 = {a, c, e},F2 = {b},F3={d, f, g, h, i}
Partitions • A partitionS on a set X is a family {A1, A2,…, An} of subsets of X, such that • A1A2A3…An = X • Aj Ak = for every j, k with j k, 1 <j, k<n. • Example: if X = {integers}, E = {even integers) and O = {odd integers}, then S = {E, O} is a partition of X.
Partitions and equivalence relations Theorem 3.2.1: Let S bea partition on a set X. Define a relation R on X by xRy if x, y are in the same set T for T S. Then R is an equivalence relation on X. i.e. an equivalence relation on a set X corresponds to a partition of X and conversely.
Equivalence classes Let X be a set and let R be an equivalence relation on X. Let a X. • Define [a] ={ xX| xRa } • Let S= { [a] | a X } Theorem 3.2.8: S is a partition on X. • The sets [a] are called equivalence classes of X induced by the relation R. • Given a, b X, then [a] = [b] or [a][b] =
[a] [g] f c d b a e g a h e aRe^eRa eRg^gRe g ? i aRg gRa [a] = [g] Example (([a][g]) ([a][g])?) • Equivalence relation R on XX = {a, b, c, d, e, f, g, h, i}F1 = {a, c, e}, F2 = {b}, F3={e, d, f, g, h, i}
Set of equivalence classes If R is an equivalence relation on a set X, define X/R = {[a] | a X }. Theorem 3.2.15: If each equivalence class on a finite set X has k elements, then X/R has |X|/k elements, i.e. |X/R| = |X| / k.
3.3 Matrices of relations • Let X, Y be sets and R a relation from X to Y • Write the matrix A = (aij) of the relation as follows: • Rows of A = elements of X • Columns of A = elements of Y • Element ai,j = 0 • if the element of X in row i and the element of Y in column j are not related • Element ai,j = 1 • if the element of X in row i and the element of Y in column j are related
The matrix of a relation (1) Example: Let X = {1, 2, 3}, Y = {a, b, c, d} Let R = {(1,a), (1,d), (2,a), (2,b), (2,c)} The matrix A of the relation R is A =
The matrix of a relation (2) • If R is a relation from a set X to itself and A is the matrix of R then A is a square matrix. • Example • Let X = {a, b, c, d} and • R = {(a,a), (b,b), (c,c), (d,d), (b,c), (c,b)}. • Then A =
j B m개 m개 A C Ci,j i i j The product of matrices • Let A be the (lm) matrix of R1 and let B be the (mn) matrix of R2 • The ij-th element of the product of these matrices, AB, is computed as • 1 : if • 0 : if
The matrix of a composed relation Theorem 3.3.6: • Let R1 be a relation from X to Y and let R2 be a relation from Y to Z. • Choose orderings of X, Y, and Z. • Let A1 be the matrix of R1 and let A2 be the matrix of R2 with respect to the orderings selected. • The matrix of the relation R2 R1 with respect to the orderings selected is obtained by replacing each nonzero terms in the matrix product A1A2 by 1
The matrix of a relation on a set X Let A be the square matrix of a relation R from X to itself. Let A2 = the matrix product AA. • R is reflexive All terms aii in the main diagonal of A are 1. • R is symmetric aij = aji for all i and j, • i.e. R is a symmetric relation on X if A is a symmetric matrix • R istransitive whenever cij in C = A2 is nonzero then entry aij in A is also nonzero.
3.4 Relational databases • A binaryrelation R is a relation among two sets X and Y, already defined as R X Y. • An n-ary relation R is a relation among n sets X1, X2,…, Xn, i.e. a subset of the Cartesian product, RX1 X2 … Xn. • Thus, R is a set of n-tuples (x1, x2,…, xn) where xk Xk, 1 <k<n.
Databases • A database is a collection of records that are manipulated by a computer. They can be considered as n sets X1 through Xn, each of which contains a list of items with information. • Database management systems are programs that help access and manipulate information stored in databases.
Relational database model • Columns of an n-ary relation are called attributes • Anattribute is akeyif no two entries have the same value • e.g. social security number • A query is a request for information from the database
Relational databases model • Table PLAYER • 4-ary relation X1X2X3X4 ID Number 22012 93831 58199 84341 01180 26710 61049 39826 Name Johnsonbaugh Glover Battey Cage Homer Score Johnsonbaugh Singleton Position c of p c 1b p of 2b Age 22 24 18 30 37 22 30 31 key (uniquely defines an n-tuple)
Operators • The selection operator chooses n-tuples from a relation by giving conditions on the attributes • The projection operator chooses two or more columns and eliminates duplicates • The join operator manipulates two relations
ID Number 22012 93831 58199 84341 01180 26710 61049 39826 Name Johnsonbaugh Glover Battey Cage Homer Score Johnsonbaugh Singleton Position c of p c 1b p of 2b Age 22 24 18 30 37 22 30 31 Operator - select <PLAYER> PLAYER[Position=c] (22012, Johnsonbaugh, c, 22) , (84341, Cage, c, 30)
ID Number 22012 93831 58199 84341 01180 26710 61049 39826 Name Johnsonbaugh Glover Battey Cage Homer Score Johnsonbaugh Singleton Position c of p c 1b p of 2b Age 22 24 18 30 37 22 30 31 Operator - project <PLAYER> PLAYER[Name, Position] (Johnsonbaugh, c), (Glover,of), (Battey, p), (Cage, c), (Homer, 1b), (Score, p), (Johnsonbaugh, of), (Singleton, 2b)
<ASSIGNMENT> PID 39826 26710 58199 01180 Team Blue Sox Mutts Jackalopes Mutts ID Number 22012 93831 58199 84341 01180 26710 61049 39826 Name Johnsonbaugh Glover Battey Cage Homer Score Johnsonbaugh Singleton Position c of p c 1b p of 2b Age 22 24 18 30 37 22 30 31 Operator - join <PLAYER> PLAYER[ID Number = PID] ASSIGNMENT <PLAYER[ID Number = PID] ASSIGNMENT> ID Number 58199 01180 26710 39826 Name Battey Homer Score Singleton Position p 1b p 2b Age 18 37 22 31 Team Jackalopes Mutts Mutts Blue Sox