25
April 10, 2002 Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Counting Relations Example: Example: How many different reflexive How many different reflexive relations can be defined on a set A containing relations can be defined on a set A containing n elements? n elements? Solution: Solution: Relations on R are subsets of A Relations on R are subsets of A A, A, which contains n which contains n 2 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 n these n 2 elements, so there are 2 elements, so there are 2 n 2 relations. relations. A A reflexive reflexive relation, however, relation, however, must must contain contain the n elements (a, a) for every a the n elements (a, a) for every a A. A. Consequently, we can only choose among n Consequently, we can only choose among n 2 – n = – n = n(n – 1) elements to generate reflexive n(n – 1) elements to generate reflexive relations, so there are 2 relations, so there are 2 n(n – 1) n(n – 1) of them. of them.

April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

Embed Size (px)

Citation preview

Page 1: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

1

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 contain the n elements (a, a) for every an elements (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 2: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

2

Combining RelationsCombining Relations

Relations 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 3: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

3

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 Let R be a relation from a set A to a set B and S a relation from B to a set C. The set 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 4: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

4

Combining RelationsCombining Relations

Example: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 5: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

5

Combining RelationsCombining Relations

We 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 6: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

6

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 by

RR11 = R = R

RRn+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 7: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

7

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, R, then (a, c)then (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 R does not contain any pairs that are not in R, so Rnot contain 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 8: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

8

n-ary Relationsn-ary Relations

In 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 the relation, and n is called its of the relation, and n is called its degreedegree..

Page 9: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

9

n-ary Relationsn-ary Relations

Example: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 10: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

10

Databases and RelationsDatabases and Relations

Let 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 The relational data model represents a database as an n-ary relation, that is, a set of database as an n-ary relation, that is, a set of records.records.

Page 11: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

11

Databases and RelationsDatabases and RelationsExample:Example: Consider a database of students, Consider a database of students, whose records are represented as 4-tuples with whose records are represented as 4-tuples with the fields 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 12: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

12

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 13: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

13

Databases and RelationsDatabases and Relations

In 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 tuples that can ever be included in our database.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 of these domains is called a a composite keycomposite key..

Page 14: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

14

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 15: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

15

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 16: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

16

Databases and RelationsDatabases and Relations

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

Definition:Definition: Let R be a relation of degree m and Let R be a relation of degree m and S a relation of degree n. The S a relation of degree n. The joinjoin J Jpp(R, S), where (R, S), where p p m and p m and p n, is a relation of degree m + n – n, is a relation of degree m + n – p that consists of all (m + n – p)-tuples p 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, , bb22, …, b, …, bn-pn-p) belongs to S.) belongs to S.

Page 17: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

17

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 18: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

18

Databases and RelationsDatabases and Relations

Example: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 19: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

19

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 20: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

20

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 }, then R can be represented by the zero-one matrix Mthe zero-one matrix MRR = [m = [mijij] with] with

mmijij = 1, if (a = 1, if (aii, b, bjj))R, andR, and

mmijij = 0, if (a = 0, if (aii, b, bjj))R.R.

Note that for creating this matrix we first need Note that for creating this matrix we first need to list the elements in A and B in a to list the elements in A and B in a particular, particular, but arbitrary orderbut arbitrary order..

Page 21: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

21

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

11

01

00

RM

Page 22: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

22

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

.

.

.

1

1

refM

Page 23: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

23

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..

1101

1001

0010

1101

RM

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

0011

0011

0011

0011

RM

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

Page 24: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

24

Representing RelationsRepresenting Relations

The 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 25: April 10, 2002Applied Discrete Mathematics Week 10: Relations 1 Counting Relations Example: How many different reflexive relations can be defined on a

April 10, 2002 Applied Discrete MathematicsWeek 10: Relations

25

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

011

111

101

SRSR MMM

001

110

101

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

000

000

101

SRSR MMM

010

001

101

RM