+ All Categories
Home > Documents > Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Date post: 18-Jan-2016
Category:
Upload: malcolm-greer
View: 219 times
Download: 0 times
Share this document with a friend
Popular Tags:
57
Lecture Lecture on on Relations Relations 1 Developed by CSE Dept., CIST Bhopal
Transcript
Page 1: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Lecture Lecture onon

RelationsRelations

1Developed by CSE Dept., CIST Bhopal

Page 2: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

2Developed by CSE Dept., CIST Bhopal

Page 3: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

3Developed by CSE Dept., CIST Bhopal

Page 4: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Functions as RelationsFunctions as Relations

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

4Developed by CSE Dept., CIST Bhopal

Page 5: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Functions as RelationsFunctions as Relations

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

5Developed by CSE Dept., CIST Bhopal

Page 6: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Relations on a SetRelations on a Set

Definition: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} ?

6Developed by CSE Dept., CIST Bhopal

Page 7: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Relations on a SetRelations on a Set

Solution: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)}

44

33

22

11

44332211RR11 11

22

33

44

22

33

44

XX XX XX

XX XX

XX

7Developed by CSE Dept., CIST Bhopal

Page 8: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

8Developed by CSE Dept., CIST Bhopal

Page 9: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Properties of RelationsProperties of RelationsWe will now look at some useful ways to We will now look at some useful ways to classify relations.classify 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), R = {(1, 1), (1, 2), (2, 3), (3, 3), (4, 4)}(4, 4)}

NoNo..R = {(1, 1), (2, 2), (2, 3), (3, 3), (4, R = {(1, 1), (2, 2), (2, 3), (3, 3), (4,

4)}4)}Yes.Yes.

R = {(1, 1), (2, 2), (3, 3)}R = {(1, 1), (2, 2), (3, 3)} NoNo..

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

9Developed by CSE Dept., CIST Bhopal

Page 10: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Properties of RelationsProperties of Relations

Definitions:Definitions:

A relation R on a set A is called A relation R on a set A is called symmetricsymmetric if if (b, a)(b, 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.

10Developed by CSE Dept., CIST Bhopal

Page 11: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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), R = {(1, 1), (1, 2), (2, 1), (3, 3), (4, 4)}(4, 4)}

symmetrisymmetriccR = {(1, 1)}R = {(1, 1)} sym. sym. and and antisymantisym..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.

11Developed by CSE Dept., CIST Bhopal

Page 12: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Properties of RelationsProperties of Relations

Definition: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, R = {(1, 1), (1, 2), (2, 2), (2, 1), (3, 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.

12Developed by CSE Dept., CIST Bhopal

Page 13: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

13Developed by CSE Dept., CIST Bhopal

Page 14: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

14Developed by CSE Dept., CIST Bhopal

Page 15: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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 R and 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).

15Developed by CSE Dept., CIST Bhopal

Page 16: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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)}

16Developed by CSE Dept., CIST Bhopal

Page 17: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

17Developed by CSE Dept., CIST Bhopal

Page 18: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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)

18Developed by CSE Dept., CIST Bhopal

Page 19: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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. 19Developed by CSE Dept., CIST Bhopal

Page 20: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

20Developed by CSE Dept., CIST Bhopal

Page 21: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

21Developed by CSE Dept., CIST Bhopal

Page 22: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

22Developed by CSE Dept., CIST Bhopal

Page 23: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

23Developed by CSE Dept., CIST Bhopal

Page 24: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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. 24Developed by CSE Dept., CIST Bhopal

Page 25: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

25Developed by CSE Dept., CIST Bhopal

Page 26: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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). 26Developed by CSE Dept., CIST Bhopal

Page 27: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

27Developed by CSE Dept., CIST Bhopal

Page 28: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

28Developed by CSE Dept., CIST Bhopal

Page 29: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

29Developed by CSE Dept., CIST Bhopal

Page 30: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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 ?

30Developed by CSE Dept., CIST Bhopal

Page 31: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

31Developed by CSE Dept., CIST Bhopal

Page 32: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

32Developed by CSE Dept., CIST Bhopal

Page 33: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

33Developed by CSE Dept., CIST Bhopal

Page 34: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

34Developed by CSE Dept., CIST Bhopal

Page 35: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

0011

0011

0011

0011

RM

non-symmetric non-symmetric matrix,matrix,non-symmetric non-symmetric relation.relation. 35Developed by CSE Dept., CIST Bhopal

Page 36: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

36Developed by CSE Dept., CIST Bhopal

Page 37: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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 RS and What are the matrices representing RS and RS?RS?

Solution:Solution: These matrices are given by These matrices are given by

000

000

101

SRSR MMM

010

001

101

RM

37Developed by CSE Dept., CIST Bhopal

Page 38: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

11

01

00

RM

38Developed by CSE Dept., CIST Bhopal

Page 39: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

011

111

101

SRSR MMM

001

110

101

SM

What are the matrices representing RS and What are the matrices representing RS and RS?RS?

Solution:Solution: These matrices are given by These matrices are given by

000

000

101

SRSR MMM

010

001

101

RM

39Developed by CSE Dept., CIST Bhopal

Page 40: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Representing Relations Using Representing Relations Using MatricesMatrices

Do you remember the Do you remember the Boolean product Boolean product of of two zero-one matrices?two zero-one matrices?

Let A = [aLet A = [aijij] be an mk zero-one matrix and ] be an mk zero-one matrix and B = [bB = [bijij] be a kn zero-one matrix.] be a kn zero-one matrix.

Then the Then the Boolean productBoolean product of A and B, of A and B, denoted by AB, is the mn matrix with (i, j)th denoted by AB, is the mn matrix with (i, j)th entry [centry [cijij], where], where

ccijij = (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.

40Developed by CSE Dept., CIST Bhopal

Page 41: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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).41Developed by CSE Dept., CIST Bhopal

Page 42: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

42Developed by CSE Dept., CIST Bhopal

Page 43: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

001

110

010

RM

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

010

111

110]2[

2 RRMM

43Developed by CSE Dept., CIST Bhopal

Page 44: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

44Developed by CSE Dept., CIST Bhopal

Page 45: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

aabb

ccdd

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

45Developed by CSE Dept., CIST Bhopal

Page 46: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

46Developed by CSE Dept., CIST Bhopal

Page 47: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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

47Developed by CSE Dept., CIST Bhopal

Page 48: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

48Developed by CSE Dept., CIST Bhopal

Page 49: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

49Developed by CSE Dept., CIST Bhopal

Page 50: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

50Developed by CSE Dept., CIST Bhopal

Page 51: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

51Developed by CSE Dept., CIST Bhopal

Page 52: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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 A ii, , 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 = S52Developed by CSE Dept., CIST Bhopal

Page 53: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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, {{m, o, c, k}, {r, u, b, s}}s}}

yes.yes.

{{c, o, m, b}, {u, s}, {{c, o, m, b}, {u, s}, {r}}{r}}

no (k is missing).no (k is missing).

{{b, r, o, c, k}, {m, u, s, {{b, r, o, c, k}, {m, u, s, t}}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}, {{b, o, o, k}, {r, u, m}, {c, s}}{c, s}}

yes ({b,o,o,k} = yes ({b,o,o,k} = {b,o,k}).{b,o,k}).

{{u, m, b}, {r, o, c, k, s}, }{{u, m, b}, {r, o, c, k, s}, }no ( not allowed).no ( not allowed).53Developed by CSE Dept., CIST Bhopal

Page 54: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

54Developed by CSE Dept., CIST Bhopal

Page 55: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

Equivalence Classes Equivalence Classes Example: Example: Let us assume that Frank, Suzanne Let us assume that Frank, Suzanne and George live in Boston, Stephanie and Max and George live in Boston, Stephanie and Max live in Lübeck, and Jennifer lives in Sydney. live in Lübeck, and Jennifer lives in Sydney.

Let R be the Let R be the equivalence relationequivalence relation {(a, b) | a {(a, b) | a and b live in the same city} on the set P = and b live in the same city} on the set P = {Frank, Suzanne, George, Stephanie, Max, {Frank, Suzanne, George, Stephanie, Max, Jennifer}.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)}.

55Developed by CSE Dept., CIST Bhopal

Page 56: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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.

56Developed by CSE Dept., CIST Bhopal

Page 57: Lecture on Relations 1Developed by CSE Dept., CIST Bhopal.

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, …}}

57Developed by CSE Dept., CIST Bhopal


Recommended