You Never Escape Your…

57
Fall 2002 CMSC 203 - Discrete Structures 1 You Never Escape Your… You Never Escape Your… Relations Relations

description

You Never Escape Your…. Relations. Relations. If we want to describe a relationship between elements of two sets A and B, we can use ordered pairs with their first element taken from A and their second element taken from B. - PowerPoint PPT Presentation

Transcript of You Never Escape Your…

Page 1: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 1

You Never Escape Your…You Never Escape Your…

RelationsRelations

Page 2: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 2

RelationsRelationsIf we want to describe a relationship between If we want to describe a relationship between elements of two sets A and B, we can use elements of two sets A and B, we can use ordered pairsordered pairs with their first element taken with their first element taken from A and their second element taken from B. from A and their second element taken from B. Since this is a relation between Since this is a relation between two setstwo sets, it is , it is called a called a binary relationbinary relation..Definition:Definition: Let A and B be sets. A binary Let A and B be sets. A binary relation from A to B is a subset of Arelation from A to B is a subset of AB.B.In other words, for a binary relation R we have In other words, for a binary relation R we have R R A AB. We use the notation aRb to denote B. We use the notation aRb to denote that (a, b)that (a, b)R and aR and aRb to denote that (a, b)b to denote that (a, b)R.R.

Page 3: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 3

RelationsRelationsWhen (a, b) belongs to R, a is said to be When (a, b) belongs to R, a is said to be relatedrelated to b by R. to b by R.Example:Example: Let P be a set of people, C be a set Let P be a set of people, C be a set of cars, and D be the relation describing which of cars, and D be the relation describing which person drives which car(s).person drives which car(s).P = {Carl, Suzanne, Peter, Carla}, P = {Carl, Suzanne, Peter, Carla}, C = {Mercedes, BMW, tricycle}C = {Mercedes, BMW, tricycle}D = {(Carl, Mercedes), (Suzanne, Mercedes),D = {(Carl, Mercedes), (Suzanne, Mercedes), (Suzanne, BMW), (Peter, tricycle)} (Suzanne, BMW), (Peter, tricycle)}This means that Carl drives a Mercedes, This means that Carl drives a Mercedes, Suzanne drives a Mercedes and a BMW, Peter Suzanne drives a Mercedes and a BMW, Peter drives a tricycle, and Carla does not drive any drives a tricycle, and Carla does not drive any of these vehicles.of these vehicles.

Page 4: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 4

Functions as RelationsFunctions as RelationsYou might remember that a You might remember that a functionfunction f from a f from a set A to a set B assigns a unique element of B set A to a set B assigns a unique element of B to each element of A.to each element of A.The The graphgraph of f is the set of ordered pairs (a, b) of f is the set of ordered pairs (a, b) such that b = f(a).such that b = f(a).Since the graph of f is a subset of ASince the graph of f is a subset of AB, it is a B, it is a relationrelation from A to B. from A to B.Moreover, for each element Moreover, for each element aa of A, there is of A, there is exactly one ordered pair in the graph that has exactly one ordered pair in the graph that has aa as its first element. as its first element.

Page 5: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 5

Functions as RelationsFunctions as RelationsConversely, if R is a relation from A to B such Conversely, if R is a relation from A to B such that every element in A is the first element of that every element in A is the first element of exactly one ordered pair of R, then a function exactly one ordered pair of R, then a function can be defined with R as its graph.can be defined with R as its graph.

This is done by assigning to an element aThis is done by assigning to an element aA A the unique element bthe unique element bB such that (a, b)B such that (a, b)R.R.

Page 6: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 6

Relations on a SetRelations on a SetDefinition:Definition: A relation on the set A is a relation A relation on the set A is a relation from A to A.from A to A.In other words, a relation on the set A is a In other words, a relation on the set A is a subset of Asubset of AA.A.

Example:Example: Let A = {1, 2, 3, 4}. Which ordered Let A = {1, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) | a < b} ?pairs are in the relation R = {(a, b) | a < b} ?

Page 7: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 7

Relations on a SetRelations on a SetSolution:Solution: R = { R = {(1, 2),(1, 2),(1, 3),(1, 3),(1, 4),(1, 4),(2, 3),(2, 3),(2, 4),(2, 4),(3, 4)}(3, 4)}

44332211

44332211RR11 11

22

33

44

22

33

44

XX XX XX

XX XX

XX

Page 8: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 8

Relations on a SetRelations on a SetHow many different relations can we How many different relations can we define on a set A with n elements?define on a set A with n elements?A relation on a set A is a subset of AA relation on a set A is a subset of AA.A.How many elements are in AHow many elements are in AA ?A ?There are nThere are n22 elements in A elements in AA, so how many A, so how many subsets (= relations on A) does Asubsets (= relations on A) does AA have?A have?The number of subsets that we can form out of The number of subsets that we can form out of a set with m elements is 2a set with m elements is 2mm. Therefore, 2. Therefore, 2nn22 subsets can be formed out of Asubsets can be formed out of AA.A.Answer:Answer: We can define 2 We can define 2nn22 different relations different relations on A.on A.

Page 9: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 9

Properties of RelationsProperties of RelationsWe will now look at some useful ways to classify We will now look at some useful ways to classify relations.relations.Definition:Definition: A relation R on a set A is called A relation R on a set A is called reflexivereflexive if (a, a) if (a, a)R for every element aR for every element aA.A.Are the following relations on {1, 2, 3, 4} Are the following relations on {1, 2, 3, 4} reflexive?reflexive?R = {(1, 1), (1, 2), (2, 3), (3, 3), (4, 4)}R = {(1, 1), (1, 2), (2, 3), (3, 3), (4, 4)} No.No.R = {(1, 1), (2, 2), (2, 3), (3, 3), (4, 4)}R = {(1, 1), (2, 2), (2, 3), (3, 3), (4, 4)} Yes.Yes.R = {(1, 1), (2, 2), (3, 3)}R = {(1, 1), (2, 2), (3, 3)} No.No.

Definition:Definition: A relation on a set A is called A relation on a set A is called irreflexiveirreflexive if (a, a) if (a, a)R for every element aR for every element aA.A.

Page 10: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 10

Properties of RelationsProperties of RelationsDefinitions:Definitions: A relation R on a set A is called A relation R on a set A is called symmetricsymmetric if (b, if (b, a)a)R whenever (a, b)R whenever (a, b)R for all a, bR for all a, bA. A. A relation R on a set A is called A relation R on a set A is called antisymmetricantisymmetric if if

a = b whenever (a, b)a = b whenever (a, b)R and (b, a)R and (b, a)R.R.A relation R on a set A is called A relation R on a set A is called asymmetricasymmetric if if (a, b)(a, b)R implies that (b, a)R implies that (b, a)R for all a, bR for all a, bA. A.

Page 11: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 11

Properties of RelationsProperties of RelationsAre the following relations on {1, 2, 3, 4} Are the following relations on {1, 2, 3, 4} symmetric, antisymmetric, or asymmetric?symmetric, antisymmetric, or asymmetric?

R = {(1, 1), (1, 2), (2, 1), (3, 3), (4, 4)}R = {(1, 1), (1, 2), (2, 1), (3, 3), (4, 4)} symmetricsymmetric

R = {(1, 1)}R = {(1, 1)} sym. and sym. and antisym.antisym.

R = {(1, 3), (3, 2), (2, 1)}R = {(1, 3), (3, 2), (2, 1)} antisym. antisym. and and asym.asym.R = {(4, 4), (3, 3), (1, 4)}R = {(4, 4), (3, 3), (1, 4)} antisym.antisym.

Page 12: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 12

Properties of RelationsProperties of RelationsDefinition:Definition: A relation R on a set A is called A relation R on a set A is called transitivetransitive if whenever (a, b) if whenever (a, b)R and (b, c)R and (b, c)R, R, then (a, c)then (a, c)R for a, b, cR for a, b, cA. A. Are the following relations on {1, 2, 3, 4} Are the following relations on {1, 2, 3, 4} transitive?transitive?

R = {(1, 1), (1, 2), (2, 2), (2, 1), (3, 3)}R = {(1, 1), (1, 2), (2, 2), (2, 1), (3, 3)} Yes.Yes.R = {(1, 3), (3, 2), (2, 1)}R = {(1, 3), (3, 2), (2, 1)} No.No.R = {(2, 4), (4, 3), (2, 3), (4, 1)}R = {(2, 4), (4, 3), (2, 3), (4, 1)} No.No.

Page 13: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 13

Counting RelationsCounting RelationsExample:Example: How many different reflexive relations How many different reflexive relations can be defined on a set A containing n elements?can be defined on a set A containing n elements?Solution:Solution: Relations on R are subsets of A Relations on R are subsets of AA, A, which contains nwhich contains n22 elements. elements.Therefore, different relations on A can be Therefore, different relations on A can be generated by choosing different subsets out of generated by choosing different subsets out of these nthese n22 elements, so there are 2 elements, so there are 2nn22 relations. relations.A A reflexivereflexive relation, however, relation, however, mustmust contain the n contain the n elements (a, a) for every aelements (a, a) for every aA.A.Consequently, we can only choose among nConsequently, we can only choose among n22 – n = – n = n(n – 1) elements to generate reflexive relations, n(n – 1) elements to generate reflexive relations, so there are 2so there are 2n(n – 1)n(n – 1) of them. of them.

Page 14: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 14

Combining RelationsCombining RelationsRelations are sets, and therefore, we can apply Relations are sets, and therefore, we can apply the usual the usual set operationsset operations to them. to them.If we have two relations RIf we have two relations R11 and R and R22, and both of , and both of them are from a set A to a set B, then we can them are from a set A to a set B, then we can combine them to Rcombine them to R11 R R22, R, R11 R R22, or R, or R11 – R – R22..In each case, the result will be In each case, the result will be another another relation from A to Brelation from A to B..

Page 15: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 15

Combining RelationsCombining Relations… … and there is another important way to combine and there is another important way to combine relations.relations.Definition:Definition: Let R be a relation from a set A to a set Let R be a relation from a set A to a set B and S a relation from B to a set C. The B and S a relation from B to a set C. The compositecomposite of R and S is the relation consisting of of R and S is the relation consisting of ordered pairs (a, c), where aordered pairs (a, c), where aA, cA, cC, and for which C, and for which there exists an element bthere exists an element bB such that (a, b)B such that (a, b)R and R and

(b, c)(b, c)S. We denote the composite of R and S byS. We denote the composite of R and S bySSRR..In other words, if relation R contains a pair (a, b) In other words, if relation R contains a pair (a, b) and relation S contains a pair (b, c), then Sand relation S contains a pair (b, c), then SR R contains a pair (a, c).contains a pair (a, c).

Page 16: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 16

Combining RelationsCombining RelationsExample:Example: Let D and S be relations on A = {1, 2, Let D and S be relations on A = {1, 2, 3, 4}.3, 4}.D = {(a, b) | b = 5 - a} “b equals (5 – a)”D = {(a, b) | b = 5 - a} “b equals (5 – a)”S = {(a, b) | a < b} “a is smaller than b”S = {(a, b) | a < b} “a is smaller than b”D = {(1, 4), (2, 3), (3, 2), (4, 1)}D = {(1, 4), (2, 3), (3, 2), (4, 1)}S = {(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)}S = {(1, 2), (1, 3), (1, 4), (2, 3), (2, 4), (3, 4)}SSD = {D = {

(2, 4),(2, 4),(3, 3),(3, 3),(3, 4),(3, 4),(4, 2),(4, 2), (4, 3),(4, 3),D maps an element a to the element (5 – a), D maps an element a to the element (5 – a), and afterwards S maps (5 – a) to all elements and afterwards S maps (5 – a) to all elements larger than (5 – a), resulting in larger than (5 – a), resulting in SSD = {(a,b) | b D = {(a,b) | b > 5 – a}> 5 – a} or or SSD = {(a,b) | a + b > 5}.D = {(a,b) | a + b > 5}.

(4, 4)}(4, 4)}

Page 17: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 17

Combining RelationsCombining RelationsWe already know that We already know that functionsfunctions are just are just special casesspecial cases of of relationsrelations (namely those that (namely those that map each element in the domain onto exactly map each element in the domain onto exactly one element in the codomain).one element in the codomain).

If we formally convert two functions into If we formally convert two functions into relations, that is, write them down as sets of relations, that is, write them down as sets of ordered pairs, the composite of these relations ordered pairs, the composite of these relations will be exactly the same as the composite of will be exactly the same as the composite of the functions (as defined earlier).the functions (as defined earlier).

Page 18: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 18

Combining RelationsCombining Relations

Definition:Definition: Let R be a relation on the set A. Let R be a relation on the set A. The powers RThe powers Rnn, n = 1, 2, 3, …, are defined , n = 1, 2, 3, …, are defined inductively byinductively byRR11 = R = RRRn+1n+1 = R = RnnRR

In other words:In other words:RRnn = R = RRR … … R (n times the letter R)R (n times the letter R)

Page 19: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 19

Combining RelationsCombining RelationsTheorem:Theorem: The relation R on a set A is transitive The relation R on a set A is transitive if and only if Rif and only if Rnn R for all positive integers n. R for all positive integers n. Remember the definition of transitivity:Remember the definition of transitivity:Definition:Definition: A relation R on a set A is called A relation R on a set A is called transitive if whenever (a, b)transitive if whenever (a, b)R and (b, c)R and (b, c)R, then R, then (a, c)(a, c)R for a, b, cR for a, b, cA. A. The composite of R with itself contains exactly The composite of R with itself contains exactly these pairs (a, c). these pairs (a, c). Therefore, for a transitive relation R, RTherefore, for a transitive relation R, RR does not R does not contain any pairs that are not in R, so Rcontain any pairs that are not in R, so RR R R. R.Since RSince RR does not introduce any pairs that are R does not introduce any pairs that are not already in R, it must also be true that (Rnot already in R, it must also be true that (RR)R)R R R, and so on, so that R R, and so on, so that Rnn R. R.

Page 20: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 20

n-ary Relationsn-ary RelationsIn order to study an interesting application of In order to study an interesting application of relations, namely relations, namely databasesdatabases, we first need to , we first need to generalize the concept of binary relations to generalize the concept of binary relations to n-n-ary relationsary relations..

Definition:Definition: Let A Let A11, A, A22, …, A, …, Ann be sets. An be sets. An n-ary n-ary relationrelation on these sets is a subset of A on these sets is a subset of A11AA22……AAnn..The sets AThe sets A11, A, A22, …, A, …, Ann are called the are called the domainsdomains of of the relation, and n is called its the relation, and n is called its degreedegree..

Page 21: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 21

n-ary Relationsn-ary RelationsExample:Example: Let R = {(a, b, c) | a = 2b Let R = {(a, b, c) | a = 2b b = 2c with a, b, c b = 2c with a, b, cNN}}What is the degree of R?What is the degree of R?The degree of R is 3, so its elements are triples.The degree of R is 3, so its elements are triples.What are its domains?What are its domains?Its domains are all equal to the set of integers.Its domains are all equal to the set of integers.Is (2, 4, 8) in R?Is (2, 4, 8) in R?No.No.Is (4, 2, 1) in R?Is (4, 2, 1) in R?Yes.Yes.

Page 22: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 22

Databases and RelationsDatabases and RelationsLet us take a look at a type of database Let us take a look at a type of database representation that is based on relations, representation that is based on relations, namely thenamely the relational data model. relational data model.

A database consists of n-tuples called A database consists of n-tuples called recordsrecords, , which are made up of which are made up of fieldsfields..These fields are the These fields are the entriesentries of the n-tuples. of the n-tuples.

The relational data model represents a database The relational data model represents a database as an n-ary relation, that is, a set of records.as an n-ary relation, that is, a set of records.

Page 23: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 23

Databases and RelationsDatabases and RelationsExample:Example: Consider a database of students, whose Consider a database of students, whose records are represented as 4-tuples with the fields records are represented as 4-tuples with the fields Student NameStudent Name, , ID NumberID Number, , MajorMajor, and , and GPAGPA::R = {(Ackermann, 231455, CS, 3.88),R = {(Ackermann, 231455, CS, 3.88), (Adams, 888323, Physics, 3.45), (Adams, 888323, Physics, 3.45), (Chou, 102147, CS, 3.79), (Chou, 102147, CS, 3.79), (Goodfriend, 453876, Math, 3.45), (Goodfriend, 453876, Math, 3.45), (Rao, 678543, Math, 3.90), (Rao, 678543, Math, 3.90), (Stevens, 786576, Psych, 2.99)} (Stevens, 786576, Psych, 2.99)}Relations that represent databases are also called Relations that represent databases are also called tablestables, since they are often displayed as tables., since they are often displayed as tables.

Page 24: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 24

Databases and RelationsDatabases and RelationsA domain of an n-ary relation is called a A domain of an n-ary relation is called a primary keyprimary key if the n-tuples are uniquely if the n-tuples are uniquely determined by their values from this domain.determined by their values from this domain.This means that no two records have the same This means that no two records have the same value from the same primary key.value from the same primary key.In our example, which of the fields In our example, which of the fields Student Student NameName, , ID NumberID Number, , MajorMajor, and , and GPAGPA are are primary keys?primary keys?Student NameStudent Name and and ID NumberID Number are primary are primary keys, because no two students have identical keys, because no two students have identical values in these fields.values in these fields.In a real student database, only In a real student database, only ID NumberID Number would be a primary key.would be a primary key.

Page 25: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 25

Databases and RelationsDatabases and RelationsIn a database, a primary key should remain one In a database, a primary key should remain one even if new records are added.even if new records are added.Therefore, we should use a primary key of the Therefore, we should use a primary key of the intension intension of the database, containing all the n-of the database, containing all the n-tuples that can ever be included in our database.tuples that can ever be included in our database.

Combinations of domainsCombinations of domains can also uniquely can also uniquely identify n-tuples in an n-ary relation.identify n-tuples in an n-ary relation.When the values of a When the values of a set of domainsset of domains determine an n-tuple in a relation, the determine an n-tuple in a relation, the Cartesian productCartesian product of these domains is called a of these domains is called a composite keycomposite key..

Page 26: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 26

Databases and RelationsDatabases and RelationsWe can apply a variety of We can apply a variety of operationsoperations on n-ary on n-ary relations to form new relations.relations to form new relations.Definition:Definition: The The projectionprojection P Pii11, i, i22, …, i, …, imm maps the n- maps the n-tuple (atuple (a11, a, a22, …, a, …, ann) to the m-tuple (a) to the m-tuple (aii11, a, aii22, …, a, …, aiimm), ), where m where m n. n.In other words, a projection PIn other words, a projection Pii11, i, i22, …, i, …, imm keeps the m keeps the m components acomponents aii11, a, aii22, …, a, …, aiim m of an n-tuple and of an n-tuple and deletes its (n – m) other components.deletes its (n – m) other components.Example:Example: What is the result when we apply the What is the result when we apply the projection Pprojection P2,42,4 to the student record (Stevens, to the student record (Stevens, 786576, Psych, 2.99) ?786576, Psych, 2.99) ?Solution:Solution: It is the pair (786576, 2.99). It is the pair (786576, 2.99).

Page 27: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 27

Databases and RelationsDatabases and Relations

In some cases, applying a projection to an In some cases, applying a projection to an entire table may not only result in fewer entire table may not only result in fewer columns, but also in columns, but also in fewer rowsfewer rows..

Why is that?Why is that?

Some records may only have differed in those Some records may only have differed in those fields that were deleted, so they become fields that were deleted, so they become identicalidentical, and there is no need to list identical , and there is no need to list identical records more than once.records more than once.

Page 28: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 28

Databases and RelationsDatabases and RelationsWe can use the We can use the joinjoin operation to combine two operation to combine two tables into one if they share some identical fields.tables into one if they share some identical fields.

Definition:Definition: Let R be a relation of degree m and S Let R be a relation of degree m and S a relation of degree n. The a relation of degree n. The joinjoin J Jpp(R, S), where p (R, S), where p m and p m and p n, is a relation of degree m + n – p n, is a relation of degree m + n – p that consists of all (m + n – p)-tuples that consists of all (m + n – p)-tuples (a(a11, a, a22, …, a, …, am-pm-p, c, c11, c, c22, …, c, …, cpp, b, b11, b, b22, …, b, …, bn-pn-p),),where the m-tuple (awhere the m-tuple (a11, a, a22, …, a, …, am-pm-p, c, c11, c, c22, …, c, …, cpp) ) belongs to R and the n-tuple (cbelongs to R and the n-tuple (c11, c, c22, …, c, …, cpp, b, b11, b, b22, , …, b…, bn-pn-p) belongs to S.) belongs to S.

Page 29: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 29

Databases and RelationsDatabases and Relations

In other words, to generate Jp(R, S), we have to In other words, to generate Jp(R, S), we have to find all the elements in R whose p last find all the elements in R whose p last components match the p first components of components match the p first components of an element in S.an element in S.

The new relation contains exactly these The new relation contains exactly these matches, which are combined to tuples that matches, which are combined to tuples that contain each matching field only once.contain each matching field only once.

Page 30: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 30

Databases and RelationsDatabases and RelationsExample:Example: What is J What is J11(Y, R), where Y contains the (Y, R), where Y contains the fields fields Student NameStudent Name and and Year of BirthYear of Birth,,Y = {(1978, Ackermann),Y = {(1978, Ackermann), (1972, Adams), (1972, Adams), (1917, Chou), (1917, Chou), (1984, Goodfriend), (1984, Goodfriend), (1982, Rao), (1982, Rao), (1970, Stevens)}, (1970, Stevens)},and R contains the student records as defined and R contains the student records as defined before ?before ?

Page 31: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 31

Databases and RelationsDatabases and Relations

Solution:Solution: The resulting relation is: The resulting relation is: {(1978, Ackermann, 231455, CS, 3.88),{(1978, Ackermann, 231455, CS, 3.88), (1972, Adams, 888323, Physics, 3.45), (1972, Adams, 888323, Physics, 3.45), (1917, Chou, 102147, CS, 3.79), (1917, Chou, 102147, CS, 3.79), (1984, Goodfriend, 453876, Math, 3.45), (1984, Goodfriend, 453876, Math, 3.45), (1982, Rao, 678543, Math, 3.90), (1982, Rao, 678543, Math, 3.90), (1970, Stevens, 786576, Psych, 2.99)} (1970, Stevens, 786576, Psych, 2.99)}

Since Y has two fields and R has four, the Since Y has two fields and R has four, the relation Jrelation J11(Y, R) has 2 + 4 – 1 = 5 fields.(Y, R) has 2 + 4 – 1 = 5 fields.

Page 32: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 32

Representing RelationsRepresenting RelationsWe already know different ways of representing We already know different ways of representing relations. We will now take a closer look at two relations. We will now take a closer look at two ways of representation: ways of representation: Zero-one matricesZero-one matrices and and directed graphsdirected graphs..If R is a relation from A = {aIf R is a relation from A = {a11, a, a22, …, a, …, amm} to B = } to B = {b{b11, b, b22, …, b, …, bnn}, then R can be represented by the }, then R can be represented by the zero-one matrix Mzero-one matrix MRR = [m = [mijij] with] withmmijij = 1, if (a = 1, if (aii, b, bjj))R, andR, andmmijij = 0, if (a = 0, if (aii, b, bjj))R.R.Note that for creating this matrix we first need to Note that for creating this matrix we first need to list the elements in A and B in a list the elements in A and B in a particular, but particular, but arbitrary orderarbitrary order..

Page 33: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 33

Representing RelationsRepresenting Relations

Example:Example: How can we represent the relation How can we represent the relation R = {(2, 1), (3, 1), (3, 2)} as a zero-one matrix?R = {(2, 1), (3, 1), (3, 2)} as a zero-one matrix?

Solution:Solution: The matrix M The matrix MRR is given by is given by

110100

RM

Page 34: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 34

Representing RelationsRepresenting RelationsWhat do we know about the matrices representing What do we know about the matrices representing a a relation on a setrelation on a set (a relation from A to A) ? (a relation from A to A) ?They are They are square square matrices.matrices.What do we know about matrices representing What do we know about matrices representing reflexivereflexive relations? relations?All the elements on the All the elements on the diagonaldiagonal of such matrices of such matrices MMrefref must be must be 1s1s..

1.

..

11

refM

Page 35: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 35

Representing RelationsRepresenting RelationsWhat do we know about the matrices What do we know about the matrices representing representing symmetric relationssymmetric relations??These matrices are symmetric, that is, MThese matrices are symmetric, that is, MRR = (M = (MRR))tt..

1101100100101101

RM

symmetric matrix,symmetric matrix,symmetric relation.symmetric relation.

0011001100110011

RM

non-symmetric matrix,non-symmetric matrix,non-symmetric relation.non-symmetric relation.

Page 36: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 36

Representing RelationsRepresenting RelationsThe Boolean operations The Boolean operations joinjoin and and meetmeet (you (you remember?)remember?) can be used to determine the can be used to determine the matrices representing the matrices representing the unionunion and the and the intersectionintersection of two relations, respectively. of two relations, respectively.

To obtain the To obtain the joinjoin of two zero-one matrices, we of two zero-one matrices, we apply the Boolean “or” function to all apply the Boolean “or” function to all corresponding elements in the matrices.corresponding elements in the matrices.

To obtain the To obtain the meetmeet of two zero-one matrices, of two zero-one matrices, we apply the Boolean “and” function to all we apply the Boolean “and” function to all corresponding elements in the matrices.corresponding elements in the matrices.

Page 37: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 37

Representing RelationsRepresenting RelationsExample:Example: Let the relations R and S be Let the relations R and S be represented by the matricesrepresented by the matrices

011111101

SRSR MMM

001110101

SM

What are the matrices representing RWhat are the matrices representing RS and RS and RS?S?Solution:Solution: These matrices are given by These matrices are given by

000000101

SRSR MMM

010001101

RM

Page 38: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 38

Representing Relations Using Representing Relations Using MatricesMatrices

Example:Example: How can we represent the relation How can we represent the relation R = {(2, 1), (3, 1), (3, 2)} as a zero-one matrix?R = {(2, 1), (3, 1), (3, 2)} as a zero-one matrix?

Solution:Solution: The matrix M The matrix MRR is given by is given by

110100

RM

Page 39: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 39

Representing Relations Using Representing Relations Using MatricesMatrices

Example:Example: Let the relations R and S be Let the relations R and S be represented by the matricesrepresented by the matrices

011111101

SRSR MMM

001110101

SM

What are the matrices representing RWhat are the matrices representing RS and RS and RS?S?Solution:Solution: These matrices are given by These matrices are given by

000000101

SRSR MMM

010001101

RM

Page 40: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 40

Representing Relations Using Representing Relations Using MatricesMatrices

Do you remember the Do you remember the Boolean product Boolean product of two of two zero-one matrices?zero-one matrices?Let A = [aLet A = [aijij] be an m] be an mk zero-one matrix and k zero-one matrix and B = [bB = [bijij] be a k] be a kn zero-one matrix.n zero-one matrix.Then the Then the Boolean productBoolean product of A and B, denoted of A and B, denoted by Aby AB, is the mB, is the mn matrix with (i, j)th entry [cn matrix with (i, j)th entry [cijij], ], wherewhereccijij = (a = (ai1i1 b b1j1j) ) (a (ai2i2 b b2i2i) ) … … (a (aikik b bkjkj). ). ccijij = 1 if and only if at least one of the terms = 1 if and only if at least one of the terms(a(ainin b bnjnj) = 1 for some n; otherwise c) = 1 for some n; otherwise cijij = 0. = 0.

Page 41: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 41

Representing Relations Using Representing Relations Using MatricesMatrices

Let us now assume that the zero-one matrices Let us now assume that the zero-one matrices MMA A = [a= [aijij], M], MBB = [b = [bijij] and M] and MCC = [c = [cijij] represent ] represent relations A, B, and C, respectively.relations A, B, and C, respectively.Remember:Remember: For M For MCC = M = MAAMMBB we have: we have:ccijij = 1 if and only if at least one of the terms = 1 if and only if at least one of the terms(a(ainin b bnjnj) = 1 for some n; otherwise c) = 1 for some n; otherwise cijij = 0. = 0.In terms of the In terms of the relationsrelations, this means that C , this means that C contains a pair (xcontains a pair (xii, z, zjj) if and only if there is an ) if and only if there is an element yelement ynn such that (x such that (xii, y, ynn) is in relation A and ) is in relation A and (y(ynn, z, zjj) is in relation B.) is in relation B.Therefore, C = BTherefore, C = BA (A (compositecomposite of A and B). of A and B).

Page 42: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 42

Representing Relations Using Representing Relations Using MatricesMatrices

This gives us the following rule:This gives us the following rule:MMBBAA = M = MAAMMBB

In other words, the matrix representing the In other words, the matrix representing the compositecomposite of relations A and B is the of relations A and B is the Boolean Boolean productproduct of the matrices representing A and B. of the matrices representing A and B.

Analogously, we can find matrices representing Analogously, we can find matrices representing the the powers of relationspowers of relations::MMRRnn = M = MRR

[n][n] (n-th (n-th Boolean powerBoolean power).).

Page 43: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 43

Representing Relations Using Representing Relations Using MatricesMatrices

Example:Example: Find the matrix representing R Find the matrix representing R22, , where the matrix representing R is given bywhere the matrix representing R is given by

001110010

RM

Solution:Solution: The matrix for R The matrix for R22 is given by is given by

010111110

]2[2 RRMM

Page 44: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 44

Representing Relations Using Representing Relations Using DigraphsDigraphs

Definition:Definition: A A directed graphdirected graph, or , or digraphdigraph, , consists of a set V of consists of a set V of verticesvertices (or (or nodesnodes) ) together with a set E of ordered pairs of together with a set E of ordered pairs of elements of V called elements of V called edgesedges (or (or arcsarcs).).The vertex a is called the The vertex a is called the initial vertexinitial vertex of the of the edge (a, b), and the vertex b is called the edge (a, b), and the vertex b is called the terminal vertexterminal vertex of this edge. of this edge.

We can use arrows to display graphs.We can use arrows to display graphs.

Page 45: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 45

Representing Relations Using Representing Relations Using DigraphsDigraphs

Example:Example: Display the digraph with V = {a, b, c, d}, Display the digraph with V = {a, b, c, d}, E = {(a, b), (a, d), (b, b), (b, d), (c, a), (c, b), (d, b)}.E = {(a, b), (a, d), (b, b), (b, d), (c, a), (c, b), (d, b)}.

aa bb

ccdd

An edge of the form (b, b) is called aAn edge of the form (b, b) is called a loop. loop.

Page 46: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 46

Representing Relations Using Representing Relations Using DigraphsDigraphs

Obviously, we can represent any relation R on a Obviously, we can represent any relation R on a set A by the digraph with A as its vertices and set A by the digraph with A as its vertices and all pairs (a, b)all pairs (a, b)R as its edges.R as its edges.

Vice versa, any digraph with vertices V and Vice versa, any digraph with vertices V and edges E can be represented by a relation on V edges E can be represented by a relation on V containing all the pairs in E.containing all the pairs in E.

This This one-to-one correspondenceone-to-one correspondence between between relations and digraphs means that any relations and digraphs means that any statement about relations also applies to statement about relations also applies to digraphs, and vice versa.digraphs, and vice versa.

Page 47: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 47

Equivalence Relations Equivalence Relations

Equivalence relationsEquivalence relations are used to relate are used to relate objects that are similar in some way.objects that are similar in some way.Definition:Definition: A relation on a set A is called an A relation on a set A is called an equivalence relation if it is reflexive, equivalence relation if it is reflexive, symmetric, and transitive.symmetric, and transitive.Two elements that are related by an Two elements that are related by an equivalence relation R are called equivalence relation R are called equivalentequivalent..

Page 48: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 48

Equivalence Relations Equivalence Relations Since R is Since R is symmetricsymmetric, a is equivalent to b , a is equivalent to b whenever b is equivalent to a.whenever b is equivalent to a.Since R is Since R is reflexivereflexive, every element is , every element is equivalent to itself.equivalent to itself.Since R is Since R is transitivetransitive, if a and b are equivalent , if a and b are equivalent and b and c are equivalent, then a and c are and b and c are equivalent, then a and c are equivalent.equivalent.

Obviously, these three properties are necessary Obviously, these three properties are necessary for a reasonable definition of equivalence.for a reasonable definition of equivalence.

Page 49: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 49

Equivalence Relations Equivalence Relations Example:Example: Suppose that R is the relation on the set Suppose that R is the relation on the set of strings that consist of English letters such that of strings that consist of English letters such that aRb if and only if l(a) = l(b), where l(x) is the length aRb if and only if l(a) = l(b), where l(x) is the length of the string x. Is R an equivalence relation?of the string x. Is R an equivalence relation?Solution:Solution: • R is reflexive, because l(a) = l(a) and therefore R is reflexive, because l(a) = l(a) and therefore aRa for any string a. aRa for any string a.• R is symmetric, because if l(a) = l(b) then l(b) = R is symmetric, because if l(a) = l(b) then l(b) = l(a), so if aRb then bRa. l(a), so if aRb then bRa.• R is transitive, because if l(a) = l(b) and l(b) = l(c), R is transitive, because if l(a) = l(b) and l(b) = l(c), then l(a) = l(c), so aRb and bRc implies aRc. then l(a) = l(c), so aRb and bRc implies aRc.R is an equivalence relation.R is an equivalence relation.

Page 50: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 50

Equivalence Classes Equivalence Classes Definition: Definition: Let R be an equivalence relation on Let R be an equivalence relation on a set A. The set of all elements that are related a set A. The set of all elements that are related to an element a of A is called the to an element a of A is called the equivalence equivalence classclass of a. of a. The equivalence class of a with respect to R is The equivalence class of a with respect to R is denoted by denoted by [a][a]RR..When only one relation is under consideration, When only one relation is under consideration, we will delete the subscript R and write we will delete the subscript R and write [a][a] for for this equivalence class.this equivalence class.If bIf b[a][a]RR, b is called a , b is called a representativerepresentative of this of this equivalence class.equivalence class.

Page 51: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 51

Equivalence Classes Equivalence Classes Example: Example: In the previous example (strings of In the previous example (strings of identical length), what is the equivalence class identical length), what is the equivalence class of the word mouse, denoted by [mouse] ?of the word mouse, denoted by [mouse] ?

Solution:Solution: [mouse] is the set of all English [mouse] is the set of all English words containing five letters.words containing five letters.

For example, ‘horse’ would be a representative For example, ‘horse’ would be a representative of this equivalence class.of this equivalence class.

Page 52: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 52

Equivalence Classes Equivalence Classes Theorem: Theorem: Let R be an equivalence relation on a Let R be an equivalence relation on a set A. The following statements are equivalent:set A. The following statements are equivalent:• aRbaRb• [a] = [b][a] = [b]• [a] [a] [b] [b] Definition:Definition: A A partition partition of a set S is a collection of of a set S is a collection of disjoint nonempty subsets of S that have S as their disjoint nonempty subsets of S that have S as their union. In other words, the collection of subsets Aunion. In other words, the collection of subsets Aii, , iiI, forms a partition of S if and only if I, forms a partition of S if and only if (i) A(i) Aii for i for iII• AAii A Ajj = = , if i , if i j j• iiII A Aii = S = S

Page 53: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 53

Equivalence Classes Equivalence Classes Examples: Examples: Let S be the set {u, m, b, r, o, c, k, s}.Let S be the set {u, m, b, r, o, c, k, s}.Do the following collections of sets partition S ?Do the following collections of sets partition S ?

{{m, o, c, k}, {r, u, b, s}}{{m, o, c, k}, {r, u, b, s}} yes.yes.{{c, o, m, b}, {u, s}, {r}}{{c, o, m, b}, {u, s}, {r}} no (k is missing).no (k is missing).{{b, r, o, c, k}, {m, u, s, t}}{{b, r, o, c, k}, {m, u, s, t}} no (t is not in S).no (t is not in S).

{{u, m, b, r, o, c, k, s}}{{u, m, b, r, o, c, k, s}} yes.yes.{{b, o, o, k}, {r, u, m}, {c, s}}{{b, o, o, k}, {r, u, m}, {c, s}} yes ({b,o,o,k} = {b,o,k}).yes ({b,o,o,k} = {b,o,k}).

{{u, m, b}, {r, o, c, k, s}, {{u, m, b}, {r, o, c, k, s}, }} no (no ( not allowed). not allowed).

Page 54: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 54

Equivalence Classes Equivalence Classes

Theorem: Theorem: Let R be an equivalence relation on Let R be an equivalence relation on a a set S. Then the set S. Then the equivalence classesequivalence classes of R form of R form a a partitionpartition of S. Conversely, given a partition of S. Conversely, given a partition {A{Aii | i | iI} of the set S, there is an equivalence I} of the set S, there is an equivalence relation R that has the sets Arelation R that has the sets Aii, i, iI, as its I, as its equivalence classes.equivalence classes.

Page 55: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 55

Equivalence Classes Equivalence Classes Example: Example: Let us assume that Frank, Suzanne and Let us assume that Frank, Suzanne and George live in Boston, Stephanie and Max live in George live in Boston, Stephanie and Max live in Lübeck, and Jennifer lives in Sydney. Lübeck, and Jennifer lives in Sydney. Let R be the Let R be the equivalence relationequivalence relation {(a, b) | a and {(a, b) | a and b live in the same city} on the set P = {Frank, b live in the same city} on the set P = {Frank, Suzanne, George, Stephanie, Max, Jennifer}.Suzanne, George, Stephanie, Max, Jennifer}.Then R = {(Frank, Frank), (Frank, Suzanne),Then R = {(Frank, Frank), (Frank, Suzanne),(Frank, George), (Suzanne, Frank), (Suzanne, (Frank, George), (Suzanne, Frank), (Suzanne, Suzanne), (Suzanne, George), (George, Frank),Suzanne), (Suzanne, George), (George, Frank),(George, Suzanne), (George, George), (Stephanie,(George, Suzanne), (George, George), (Stephanie,Stephanie), (Stephanie, Max), (Max, Stephanie),Stephanie), (Stephanie, Max), (Max, Stephanie),(Max, Max), (Jennifer, Jennifer)}.(Max, Max), (Jennifer, Jennifer)}.

Page 56: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 56

Equivalence Classes Equivalence Classes Then the Then the equivalence classesequivalence classes of R are: of R are:{{Frank, Suzanne, George}, {Stephanie, Max}, {{Frank, Suzanne, George}, {Stephanie, Max}, {Jennifer}}.{Jennifer}}.This is a This is a partition partition of P.of P.

The equivalence classes of any equivalence The equivalence classes of any equivalence relation R defined on a set S constitute a relation R defined on a set S constitute a partition of S, because every element in S is partition of S, because every element in S is assigned to assigned to exactly oneexactly one of the equivalence of the equivalence classes.classes.

Page 57: You Never Escape Your…

Fall 2002 CMSC 203 - Discrete Structures 57

Equivalence Classes Equivalence Classes Another example: Another example: Let R be the relation Let R be the relation {(a, b) | a {(a, b) | a b (mod 3)} on the set of integers. b (mod 3)} on the set of integers.Is R an equivalence relation?Is R an equivalence relation?Yes, R is reflexive, symmetric, and transitive.Yes, R is reflexive, symmetric, and transitive.

What are the equivalence classes of R ?What are the equivalence classes of R ?{{…, -6, -3, 0, 3, 6, …},{{…, -6, -3, 0, 3, 6, …}, {…, -5, -2, 1, 4, 7, …}, {…, -5, -2, 1, 4, 7, …}, {…, -4, -1, 2, 5, 8, …}} {…, -4, -1, 2, 5, 8, …}}