Fall 2002 CMSC 203 - Discrete Structures 1
You Never Escape Your…You Never Escape Your…
RelationsRelations
ByBy
Sadat SumonSadat Sumon
Fall 2002 CMSC 203 - Discrete Structures 2
RelationsRelations
If we want to describe a relationship betweenIf we want to describe a relationship between
elements of two sets A and B, we can useelements of two sets A and B, we can use orderedordered
pairspairs with their first element taken from A andwith their first element taken from A and
their second element taken from B.their second element taken from B.
Since this is a relation betweenSince this is a relation between two setstwo sets, it is, it is
called acalled a binary relationbinary relation..
Definition:Definition: Let A and B be sets. A binary relationLet A and B be sets. A binary relation
from A to B is a subset of Afrom A to B is a subset of A××B.B.
In other words, for a binary relation R we haveIn other words, for a binary relation R we have
RR ⊆⊆ AA××B. We use the notation aRb to denote thatB. We use the notation aRb to denote that
(a, b)(a, b)∈∈R and aR and aRb to denote that (a, b)b to denote that (a, b)∉∉R.R.
Fall 2002 CMSC 203 - Discrete Structures 3
RelationsRelations
When (a, b) belongs to R, a is said to beWhen (a, b) belongs to R, a is said to be relatedrelated toto
b by R.b by R.
Example:Example: Let P be a set of people, C be a set ofLet P be a set of people, C be a set of
cars, and D be the relation describing which personcars, and D be the relation describing which person
drives which car(s).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, SuzanneThis means that Carl drives a Mercedes, Suzanne
drives a Mercedes and a BMW, Peter drives adrives a Mercedes and a BMW, Peter drives a
tricycle, and Carla does not drive any of thesetricycle, and Carla does not drive any of these
vehicles.vehicles.
Fall 2002 CMSC 203 - Discrete Structures 4
Functions as RelationsFunctions as Relations
You might remember that aYou might remember that a functionfunction f from a set Af from a set A
to a set B assigns a unique element of B to eachto a set B assigns a unique element of B to each
element of A.element of A.
TheThe 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 A××B, it is aB, it is a
relationrelation from A to B.from A to B.
Moreover, for each elementMoreover, for each element aa of A, there isof A, there is
exactly one ordered pair in the graph that hasexactly one ordered pair in the graph that has aa asas
its first element.its first element.
Fall 2002 CMSC 203 - Discrete Structures 5
Functions as RelationsFunctions as Relations
Conversely, if R is a relation from A to B such thatConversely, if R is a relation from A to B such that
every element in A is the first element of exactlyevery element in A is the first element of exactly
one ordered pair of R, then a function can beone ordered pair of R, then a function can be
defined with R as its graph.defined with R as its graph.
This is done by assigning to an element aThis is done by assigning to an element a∈∈A theA the
unique element bunique element b∈∈B such that (a, b)B such that (a, b)∈∈R.R.
Fall 2002 CMSC 203 - Discrete Structures 6
Relations on a SetRelations on a Set
Definition:Definition: A relation on the set A is a relationA 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 subsetIn other words, a relation on the set A is a subset
of Aof A××A.A.
Example:Example: Let A = {1, 2, 3, 4}. Which ordered pairsLet A = {1, 2, 3, 4}. Which ordered pairs
are in the relation R = {(a, b) | a < b} ?are in the relation R = {(a, b) | a < b} ?
Fall 2002 CMSC 203 - Discrete Structures 7
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
Fall 2002 CMSC 203 - Discrete Structures 8
Relations on a SetRelations on a Set
How many different relations can we define onHow many different relations can we define on
a set A with n elements?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 A××A.A.
How many elements are in AHow many elements are in A××A ?A ?
There are nThere are n22
elements in Aelements in A××A, so how manyA, so how many
subsets (= relations on A) does Asubsets (= relations on A) does A××A have?A have?
The number of subsets that we can form out of aThe number of subsets that we can form out of a
set with m elements is 2set with m elements is 2mm
. Therefore, 2. Therefore, 2nn22
subsetssubsets
can be formed out of Acan be formed out of A××A.A.
Answer:Answer: We can define 2We can define 2nn22
different relationsdifferent relations
on A.on A.
Fall 2002 CMSC 203 - Discrete Structures 9
Properties of RelationsProperties of Relations
We will now look at some useful ways to classifyWe will now look at some useful ways to classify
relations.relations.
Definition:Definition: A relation R on a set A is calledA relation R on a set A is called
reflexivereflexive if (a, a)if (a, a)∈∈R for every element aR for every element a∈∈A.A.
Are the following relations on {1, 2, 3, 4} reflexive?Are the following relations on {1, 2, 3, 4} 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 calledA relation on a set A is called
irreflexiveirreflexive if (a, a)if (a, a)∉∉R for every element aR for every element a∈∈A.A.
Fall 2002 CMSC 203 - Discrete Structures 10
Properties of RelationsProperties of Relations
Definitions:Definitions:
A relation R on a set A is calledA 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, b∈∈A.A.
A relation R on a set A is calledA relation R on a set A is called antisymmetricantisymmetric ifif
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 calledA relation R on a set A is called asymmetricasymmetric ifif
(a, b)(a, b)∈∈R implies that (b, a)R implies that (b, a)∉∉R for all a, bR for all a, b∈∈A.A.
Fall 2002 CMSC 203 - Discrete Structures 11
Properties of RelationsProperties of Relations
Are 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. andsym. and
antisym.antisym.
R = {(1, 3), (3, 2), (2, 1)}R = {(1, 3), (3, 2), (2, 1)} antisym.antisym.
and asym.and asym.
R = {(4, 4), (3, 3), (1, 4)}R = {(4, 4), (3, 3), (1, 4)} antisym.antisym.
Fall 2002 CMSC 203 - Discrete Structures 12
Properties of RelationsProperties of Relations
Definition:Definition: A relation R on a set A is calledA 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, thenR, then
(a, c)(a, c)∈∈R for a, b, cR for a, b, c∈∈A.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.
Fall 2002 CMSC 203 - Discrete Structures 13
Counting RelationsCounting Relations
Example:Example: How many different reflexive relationsHow 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 ARelations on R are subsets of A××A, whichA, which
contains ncontains n22
elements.elements.
Therefore, different relations on A can beTherefore, different relations on A can be
generated by choosing different subsets out ofgenerated by choosing different subsets out of
these nthese n22
elements, so there are 2elements, so there are 2nn22
relations.relations.
AA reflexivereflexive relation, however,relation, however, mustmust contain the ncontain the n
elements (a, a) for every aelements (a, a) for every a∈∈A.A.
Consequently, we can only choose among nConsequently, we can only choose among n22
– n =– n =
n(n – 1) elements to generate reflexive relations, son(n – 1) elements to generate reflexive relations, so
there are 2there are 2n(n – 1)n(n – 1)
of them.of them.
Fall 2002 CMSC 203 - Discrete Structures 14
Combining RelationsCombining Relations
Relations are sets, and therefore, we can apply theRelations are sets, and therefore, we can apply the
usualusual set operationsset operations to them.to them.
If we have two relations RIf we have two relations R11 and Rand R22, and both of, and both of
them are from a set A to a set B, then we canthem are from a set A to a set B, then we can
combine them to Rcombine them to R11 ∪∪ RR22, R, R11 ∩∩ RR22, or R, or R11 – R– R22..
In each case, the result will beIn each case, the result will be another relationanother relation
from A to Bfrom A to B..
Fall 2002 CMSC 203 - Discrete Structures 15
Combining RelationsCombining Relations
…… and there is another important way to combineand there is another important way to combine
relations.relations.
Definition:Definition: Let R be a relation from a set A to aLet R be a relation from a set A to a
set B and S a relation from B to a set C. Theset B and S a relation from B to a set C. The
compositecomposite of R and S is the relation consisting ofof R and S is the relation consisting of
ordered pairs (a, c), where aordered pairs (a, c), where a∈∈A, cA, c∈∈C, and for whichC, and for which
there exists an element bthere exists an element b∈∈B such that (a, b)B such that (a, b)∈∈R andR and
(b, c)(b, c)∈∈S. We denote the composite of R and S byS. We denote the composite of R and S by
SS°° RR..
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 S°° RR
contains a pair (a, c).contains a pair (a, c).
Fall 2002 CMSC 203 - Discrete Structures 16
Combining RelationsCombining Relations
Example:Example: Let D and S be relations on A = {1, 2, 3, 4}.Let D and S be relations on A = {1, 2, 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)}
SS°° D = {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), andD maps an element a to the element (5 – a), and
afterwards S maps (5 – a) to all elements larger thanafterwards S maps (5 – a) to all elements larger than
(5 – a), resulting in(5 – a), resulting in SS°° D = {(a,b) | b > 5 – a}D = {(a,b) | b > 5 – a} oror SS°° DD
= {(a,b) | a + b > 5}.= {(a,b) | a + b > 5}.
(4, 4)}(4, 4)}
Fall 2002 CMSC 203 - Discrete Structures 17
Combining RelationsCombining Relations
We already know thatWe already know that functionsfunctions are justare just specialspecial
casescases ofof relationsrelations (namely those that map each(namely those that map each
element in the domain onto exactly one element inelement in the domain onto exactly one element in
the codomain).the codomain).
If we formally convert two functions into relations,If we formally convert two functions into relations,
that is, write them down as sets of ordered pairs,that is, write them down as sets of ordered pairs,
the composite of these relations will be exactly thethe composite of these relations will be exactly the
same as the composite of the functions (as definedsame as the composite of the functions (as defined
earlier).earlier).
Fall 2002 CMSC 203 - Discrete Structures 18
Combining RelationsCombining Relations
Definition:Definition: Let R be a relation on the set A. TheLet R be a relation on the set A. The
powers Rpowers Rnn
, n = 1, 2, 3, …, are defined inductively by, n = 1, 2, 3, …, are defined inductively by
RR11
= R= R
RRn+1n+1
= R= Rnn
°° RR
In other words:In other words:
RRnn
= R= R°° RR°° …… °° R (n times the letter R)R (n times the letter R)
Fall 2002 CMSC 203 - Discrete Structures 19
Combining RelationsCombining Relations
Theorem:Theorem: The relation R on a set A is transitive ifThe relation R on a set A is transitive if
and only if Rand 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 calledA 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, thenR, then
(a, c)(a, c)∈∈R for a, b, cR for a, b, c∈∈A.A.
The composite of R with itself contains exactlyThe 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, R°° R does notR does not
contain any pairs that are not in R, so Rcontain any pairs that are not in R, so R°° RR ⊆⊆ R.R.
Since RSince R°° R does not introduce any pairs that are notR does not introduce any pairs that are not
already in R, it must also be true that (Ralready in R, it must also be true that (R°° R)R)°° RR ⊆⊆ R,R,
and so on, so that Rand so on, so that Rnn
⊆⊆ R.R.
Fall 2002 CMSC 203 - Discrete Structures 20
n-ary Relationsn-ary Relations
In order to study an interesting application ofIn order to study an interesting application of
relations, namelyrelations, namely databasesdatabases, we first need to, we first need to
generalize the concept of binary relations togeneralize the concept of binary relations to n-aryn-ary
relationsrelations..
Definition:Definition: Let ALet A11, A, A22, …, A, …, Ann be sets. Anbe sets. An n-aryn-ary
relationrelation on these sets is a subset of Aon these sets is a subset of A11××AA22××……××AAnn..
The sets AThe sets A11, A, A22, …, A, …, Ann are called theare called the domainsdomains of theof the
relation, and n is called itsrelation, and n is called its degreedegree..
Fall 2002 CMSC 203 - Discrete Structures 21
n-ary Relationsn-ary Relations
Example:Example:
Let R = {(a, b, c) | a = 2bLet R = {(a, b, c) | a = 2b ∧∧ b = 2c with a, b, cb = 2c with a, b, c∈∈NN}}
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.
Fall 2002 CMSC 203 - Discrete Structures 22
Databases and RelationsDatabases and Relations
Let us take a look at a type of databaseLet us take a look at a type of database
representation that is based on relations, namelyrepresentation that is based on relations, namely
thethe relational data model.relational data model.
A database consists of n-tuples calledA database consists of n-tuples called recordsrecords,,
which are made up ofwhich are made up of fieldsfields..
These fields are theThese fields are the entriesentries of the n-tuples.of the n-tuples.
The relational data model represents a database asThe relational data model represents a database as
an n-ary relation, that is, a set of records.an n-ary relation, that is, a set of records.
Fall 2002 CMSC 203 - Discrete Structures 23
Databases and RelationsDatabases and Relations
Example:Example: Consider a database of students, whoseConsider a database of students, whose
records are represented as 4-tuples with the fieldsrecords 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 calledRelations that represent databases are also called
tablestables, since they are often displayed as tables., since they are often displayed as tables.
Fall 2002 CMSC 203 - Discrete Structures 24
Databases and RelationsDatabases and Relations
A domain of an n-ary relation is called aA domain of an n-ary relation is called a primaryprimary
keykey if the n-tuples are uniquely determined byif the n-tuples are uniquely determined by
their values from this domain.their values from this domain.
This means that no two records have the sameThis 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 fieldsIn our example, which of the fields Student NameStudent Name,,
ID NumberID Number,, MajorMajor, and, and GPAGPA are primary keys?are primary keys?
Student NameStudent Name andand ID NumberID Number are primary keys,are primary keys,
because no two students have identical values inbecause no two students have identical values in
these fields.these fields.
In a real student database, onlyIn a real student database, only ID NumberID Number wouldwould
be a primary key.be a primary key.
Fall 2002 CMSC 203 - Discrete Structures 25
Databases and RelationsDatabases and Relations
In a database, a primary key should remain oneIn 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 theTherefore, we should use a primary key of the
intensionintension 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 identifycan also uniquely identify
n-tuples in an n-ary relation.n-tuples in an n-ary relation.
When the values of aWhen the values of a set of domainsset of domains determine andetermine an
n-tuple in a relation, then-tuple in a relation, the Cartesian productCartesian product ofof
these domains is called athese domains is called a composite keycomposite key..
Fall 2002 CMSC 203 - Discrete Structures 26
Databases and RelationsDatabases and Relations
We can apply a variety ofWe can apply a variety of operationsoperations on n-aryon n-ary
relations to form new relations.relations to form new relations.
Definition:Definition: TheThe projectionprojection PPii11, i, i22, …, i, …, imm
maps the n-tuplemaps the n-tuple
(a(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 mkeeps the m
components acomponents aii11
, a, aii22
, …, a, …, aiimm
of an n-tuple and deletes itsof an n-tuple and deletes its
(n – m) other components.(n – m) other components.
Example:Example: What is the result when we apply theWhat 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).
Fall 2002 CMSC 203 - Discrete Structures 27
Databases and RelationsDatabases and Relations
In some cases, applying a projection to an entireIn some cases, applying a projection to an entire
table may not only result in fewer columns, but alsotable may not only result in fewer columns, but also
inin fewer rowsfewer rows..
Why is that?Why is that?
Some records may only have differed in thoseSome records may only have differed in those
fields that were deleted, so they becomefields that were deleted, so they become identicalidentical,,
and there is no need to list identical records moreand there is no need to list identical records more
than once.than once.
Fall 2002 CMSC 203 - Discrete Structures 28
Databases and RelationsDatabases and Relations
We can use theWe can use the joinjoin operation to combine twooperation 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 aLet R be a relation of degree m and S a
relation of degree n. Therelation of degree n. The joinjoin JJpp(R, S), where p(R, S), where p ≤≤ mm
and pand p ≤≤ n, is a relation of degree m + n – p thatn, is a relation of degree m + n – p that
consists of all (m + n – p)-tuplesconsists 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) belongs
to R and the n-tuple (cto R and the n-tuple (c11, c, c22, …, c, …, cpp, b, b11, b, b22, …, b, …, bn-pn-p))
belongs to S.belongs to S.
Fall 2002 CMSC 203 - Discrete Structures 29
Databases and RelationsDatabases and Relations
In other words, to generate Jp(R, S), we have toIn other words, to generate Jp(R, S), we have to
find all the elements in R whose p last componentsfind all the elements in R whose p last components
match the p first components of an element in S.match the p first components of an element in S.
The new relation contains exactly these matches,The new relation contains exactly these matches,
which are combined to tuples that contain eachwhich are combined to tuples that contain each
matching field only once.matching field only once.
Fall 2002 CMSC 203 - Discrete Structures 30
Databases and RelationsDatabases and Relations
Example:Example: What is JWhat is J11(Y, R), where Y contains the(Y, R), where Y contains the
fieldsfields Student NameStudent Name andand 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 definedand R contains the student records as defined
before ?before ?
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 relationSince Y has two fields and R has four, the relation
JJ11(Y, R) has 2 + 4 – 1 = 5 fields.(Y, R) has 2 + 4 – 1 = 5 fields.
Fall 2002 CMSC 203 - Discrete Structures 32
Representing RelationsRepresenting Relations
We already know different ways of representingWe already know different ways of representing
relations. We will now take a closer look at tworelations. We will now take a closer look at two
ways of representation:ways of representation: Zero-one matricesZero-one matrices andand
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] 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 toNote that for creating this matrix we first need to
list the elements in A and B in alist the elements in A and B in a particular, butparticular, but
arbitrary orderarbitrary order..
Fall 2002 CMSC 203 - Discrete Structures 33
Representing RelationsRepresenting Relations
Example:Example: How can we represent the relationHow 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 MThe matrix MRR is given byis given by










=
11
01
00
RM
Fall 2002 CMSC 203 - Discrete Structures 34
Representing RelationsRepresenting Relations
What do we know about the matrices representingWhat do we know about the matrices representing
aa relation on a setrelation on a set (a relation from A to A) ?(a relation from A to A) ?
They areThey are squaresquare matrices.matrices.
What do we know about matrices representingWhat do we know about matrices representing
reflexivereflexive relations?relations?
All the elements on theAll the elements on the diagonaldiagonal of such matricesof such matrices
MMrefref must bemust be 1s1s..




















=
1
.
.
.
1
1
refM
Fall 2002 CMSC 203 - Discrete Structures 35
Representing RelationsRepresenting Relations
What do we know about the matrices representingWhat do we know about the matrices 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.
Fall 2002 CMSC 203 - Discrete Structures 36
Representing RelationsRepresenting Relations
The Boolean operationsThe Boolean operations joinjoin andand meetmeet (you(you
remember?)remember?) can be used to determine the matricescan be used to determine the matrices
representing therepresenting the unionunion and theand the intersectionintersection of twoof two
relations, respectively.relations, respectively.
To obtain theTo obtain the joinjoin of two zero-one matrices, weof two zero-one matrices, we
apply the Boolean “or” function to all correspondingapply the Boolean “or” function to all corresponding
elements in the matrices.elements in the matrices.
To obtain theTo obtain the meetmeet of two zero-one matrices, weof two zero-one matrices, we
apply the Boolean “and” function to all correspondingapply the Boolean “and” function to all corresponding
elements in the matrices.elements in the matrices.
Fall 2002 CMSC 203 - Discrete Structures 37
Representing RelationsRepresenting Relations
Example:Example: Let the relations R and S be representedLet the relations R and S be represented
by the matricesby the matrices










=∨=∪
011
111
101
SRSR MMM










=
001
110
101
SM
What are the matrices representing RWhat are the matrices representing R∪∪S and RS and R∩∩S?S?
Solution:Solution: These matrices are given byThese matrices are given by










=∧=∩
000
000
101
SRSR MMM










=
010
001
101
RM
Fall 2002 CMSC 203 - Discrete Structures 38
Representing Relations Using MatricesRepresenting Relations Using Matrices
Example:Example: How can we represent the relationHow 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 MThe matrix MRR is given byis given by










=
11
01
00
RM
Fall 2002 CMSC 203 - Discrete Structures 39
Representing Relations Using MatricesRepresenting Relations Using Matrices
Example:Example: Let the relations R and S be representedLet the relations R and S be represented
by the matricesby the matrices










=∨=∪
011
111
101
SRSR MMM










=
001
110
101
SM
What are the matrices representing RWhat are the matrices representing R∪∪S and RS and R∩∩S?S?
Solution:Solution: These matrices are given byThese matrices are given by










=∧=∩
000
000
101
SRSR MMM










=
010
001
101
RM
Fall 2002 CMSC 203 - Discrete Structures 40
Representing Relations Using MatricesRepresenting Relations Using Matrices
Do you remember theDo you remember the Boolean productBoolean product of two zero-of two zero-
one matrices?one matrices?
Let A = [aLet A = [aijij] be an m] be an m××k zero-one matrix andk zero-one matrix and
B = [bB = [bijij] be a k] be a k××n zero-one matrix.n zero-one matrix.
Then theThen the Boolean productBoolean product of A and B, denoted byof A and B, denoted by
AAοοB, is the mB, is the m××n matrix with (i, j)th entry [cn matrix with (i, j)th entry [cijij], where], where
ccijij = (a= (ai1i1 ∧∧ bb1j1j)) ∨∨ (a(ai2i2 ∧∧ bb2i2i)) ∨∨ …… ∨∨ (a(aikik ∧∧ bbkjkj).).
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 ∧∧ bbnjnj) = 1 for some n; otherwise c) = 1 for some n; otherwise cijij = 0.= 0.
Fall 2002 CMSC 203 - Discrete Structures 41
Representing Relations Using MatricesRepresenting Relations Using Matrices
Let us now assume that the zero-one matricesLet us now assume that the zero-one matrices
MMAA = [a= [aijij], M], MBB = [b= [bijij] and M] and MCC = [c= [cijij] represent relations A,] represent relations A,
B, and C, respectively.B, and C, respectively.
Remember:Remember: For MFor MCC = M= MAAοοMMBB 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 ∧∧ bbnjnj) = 1 for some n; otherwise c) = 1 for some n; otherwise cijij = 0.= 0.
In terms of theIn terms of the relationsrelations, this means that C contains a, this means that C contains a
pair (xpair (xii, z, zjj) if and only if there is an element y) if and only if there is an element ynn suchsuch
that (xthat (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 = B°°A (A (compositecomposite of A and B).of A and B).
Fall 2002 CMSC 203 - Discrete Structures 42
Representing Relations Using MatricesRepresenting Relations Using Matrices
This gives us the following rule:This gives us the following rule:
MMBB°°AA = M= MAAοοMMBB
In other words, the matrix representing theIn other words, the matrix representing the
compositecomposite of relations A and B is theof relations A and B is the BooleanBoolean
productproduct of the matrices representing A and B.of the matrices representing A and B.
Analogously, we can find matrices representing theAnalogously, we can find matrices representing the
powers of relationspowers of relations::
MMRRnn = M= MRR
[n][n]
(n-th(n-th Boolean powerBoolean power).).
Fall 2002 CMSC 203 - Discrete Structures 43
Representing Relations Using MatricesRepresenting Relations Using Matrices
Example:Example: Find the matrix representing RFind the matrix representing R22
, where, where
the matrix representing R is given bythe matrix representing R is given by










=
001
110
010
RM
Solution:Solution: The matrix for RThe matrix for R22
is given byis given by










==
010
111
110
]2[
2 RR
MM
Fall 2002 CMSC 203 - Discrete Structures 44
Representing Relations Using DigraphsRepresenting Relations Using Digraphs
Definition:Definition: AA directed graphdirected graph, or, or digraphdigraph, consists, consists
of a set V ofof a set V of verticesvertices (or(or nodesnodes) together with a) together with a
set E of ordered pairs of elements of V calledset E of ordered pairs of elements of V called
edgesedges (or(or arcsarcs).).
The vertex a is called theThe vertex a is called the initial vertexinitial vertex of theof the
edge (a, b), and the vertex b is called theedge (a, b), and the vertex b is called the terminalterminal
vertexvertex of this edge.of this edge.
We can use arrows to display graphs.We can use arrows to display graphs.
Fall 2002 CMSC 203 - Discrete Structures 45
Representing Relations Using DigraphsRepresenting Relations Using Digraphs
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.
Fall 2002 CMSC 203 - Discrete Structures 46
Representing Relations Using DigraphsRepresenting Relations Using Digraphs
Obviously, we can represent any relation R on a setObviously, we can represent any relation R on a set
A by the digraph with A as its vertices and all pairsA by the digraph with A as its vertices and all pairs
(a, b)(a, b)∈∈R as its edges.R as its edges.
Vice versa, any digraph with vertices V and edges EVice versa, any digraph with vertices V and edges E
can be represented by a relation on V containing allcan be represented by a relation on V containing all
the pairs in E.the pairs in E.
ThisThis one-to-one correspondenceone-to-one correspondence betweenbetween
relations and digraphs means that any statementrelations and digraphs means that any statement
about relations also applies to digraphs, and viceabout relations also applies to digraphs, and vice
versa.versa.
Fall 2002 CMSC 203 - Discrete Structures 47
Equivalence RelationsEquivalence Relations
Equivalence relationsEquivalence relations are used to relate objectsare used to relate objects
that are similar in some way.that are similar in some way.
Definition:Definition: A relation on a set A is called anA relation on a set A is called an
equivalence relation if it is reflexive, symmetric,equivalence relation if it is reflexive, symmetric,
and transitive.and transitive.
Two elements that are related by an equivalenceTwo elements that are related by an equivalence
relation R are calledrelation R are called equivalentequivalent..
Fall 2002 CMSC 203 - Discrete Structures 48
Equivalence RelationsEquivalence Relations
Since R isSince R is symmetricsymmetric, a is equivalent to b whenever, a is equivalent to b whenever
b is equivalent to a.b is equivalent to a.
Since R isSince R is reflexivereflexive, every element is equivalent to, every element is equivalent to
itself.itself.
Since R isSince R is transitivetransitive, if a and b are equivalent and b, if a and b are equivalent and b
and c are equivalent, then a and c are equivalent.and c are equivalent, then a and c are equivalent.
Obviously, these three properties are necessaryObviously, these three properties are necessary
for a reasonable definition of equivalence.for a reasonable definition of equivalence.
Fall 2002 CMSC 203 - Discrete Structures 49
Equivalence RelationsEquivalence Relations
Example:Example: Suppose that R is the relation on the setSuppose that R is the relation on the set
of strings that consist of English letters such thatof strings that consist of English letters such that
aRb if and only if l(a) = l(b), where l(x) is the lengthaRb 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 thereforeR 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.
Fall 2002 CMSC 203 - Discrete Structures 50
Equivalence ClassesEquivalence Classes
Definition:Definition: Let R be an equivalence relation on aLet R be an equivalence relation on a
set A. The set of all elements that are related toset A. The set of all elements that are related to
an element a of A is called thean element a of A is called the equivalence classequivalence class
of a.of a.
The equivalence class of a with respect to R isThe equivalence class of a with respect to R is
denoted bydenoted by [a][a]RR..
When only one relation is under consideration, weWhen only one relation is under consideration, we
will delete the subscript R and writewill delete the subscript R and write [a][a] for thisfor this
equivalence class.equivalence class.
If bIf b∈∈[a][a]RR, b is called a, b is called a representativerepresentative of thisof this
equivalence class.equivalence class.
Fall 2002 CMSC 203 - Discrete Structures 51
Equivalence ClassesEquivalence Classes
Example:Example: In the previous example (strings ofIn the previous example (strings of
identical length), what is the equivalence class ofidentical length), what is the equivalence class of
the word mouse, denoted by [mouse] ?the word mouse, denoted by [mouse] ?
Solution:Solution: [mouse] is the set of all English words[mouse] is the set of all English words
containing five letters.containing five letters.
For example, ‘horse’ would be a representative ofFor example, ‘horse’ would be a representative of
this equivalence class.this equivalence class.
Fall 2002 CMSC 203 - Discrete Structures 52
Equivalence ClassesEquivalence Classes
Theorem:Theorem: Let R be an equivalence relation on a setLet R be an equivalence relation on a set
A. The following statements are equivalent:A. The following statements are equivalent:
• aRbaRb
• [a] = [b][a] = [b]
• [a][a] ∩∩ [b][b] ≠≠ ∅∅
Definition:Definition: AA partitionpartition of a set S is a collection ofof a set S is a collection of
disjoint nonempty subsets of S that have S as theirdisjoint 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,,
ii∈∈I, forms a partition of S if and only ifI, forms a partition of S if and only if
(i) A(i) Aii ≠≠ ∅∅ for ifor i∈∈II
• AAii ∩∩ AAjj == ∅∅, if i, if i ≠≠ jj
• ∪∪ii∈∈II AAii = S= S
Fall 2002 CMSC 203 - Discrete Structures 53
Equivalence ClassesEquivalence 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).
Fall 2002 CMSC 203 - Discrete Structures 54
Equivalence ClassesEquivalence Classes
Theorem:Theorem: Let R be an equivalence relation on aLet R be an equivalence relation on a
set S. Then theset S. Then the equivalence classesequivalence classes of R form aof R form a
partitionpartition of S. Conversely, given a partitionof S. Conversely, given a partition
{A{Aii | i| i∈∈I} of the set S, there is an equivalenceI} of the set S, there is an equivalence
relation R that has the sets Arelation R that has the sets Aii, i, i∈∈I, as itsI, as its
equivalence classes.equivalence classes.
Fall 2002 CMSC 203 - Discrete Structures 55
Equivalence ClassesEquivalence Classes
Example:Example: Let us assume that Frank, Suzanne andLet us assume that Frank, Suzanne and
George live in Boston, Stephanie and Max live inGeorge 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 theLet R be the equivalence relationequivalence relation {(a, b) | a and b{(a, b) | a and b
live in the same city} on the set P = {Frank,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)}.
Fall 2002 CMSC 203 - Discrete Structures 56
Equivalence ClassesEquivalence Classes
Then theThen the equivalence classesequivalence classes of R are:of R are:
{{Frank, Suzanne, George}, {Stephanie, Max},{{Frank, Suzanne, George}, {Stephanie, Max},
{Jennifer}}.{Jennifer}}.
This is aThis is a partitionpartition of P.of P.
The equivalence classes of any equivalence relationThe equivalence classes of any equivalence relation
R defined on a set S constitute a partition of S,R defined on a set S constitute a partition of S,
because every element in S is assigned tobecause every element in S is assigned to exactlyexactly
oneone of the equivalence classes.of the equivalence classes.
Fall 2002 CMSC 203 - Discrete Structures 57
Equivalence ClassesEquivalence Classes
Another example:Another example: Let R be the relationLet 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, …}}

Sadat sumon

  • 1.
    Fall 2002 CMSC203 - Discrete Structures 1 You Never Escape Your…You Never Escape Your… RelationsRelations ByBy Sadat SumonSadat Sumon
  • 2.
    Fall 2002 CMSC203 - Discrete Structures 2 RelationsRelations If we want to describe a relationship betweenIf we want to describe a relationship between elements of two sets A and B, we can useelements of two sets A and B, we can use orderedordered pairspairs with their first element taken from A andwith their first element taken from A and their second element taken from B.their second element taken from B. Since this is a relation betweenSince this is a relation between two setstwo sets, it is, it is called acalled a binary relationbinary relation.. Definition:Definition: Let A and B be sets. A binary relationLet A and B be sets. A binary relation from A to B is a subset of Afrom A to B is a subset of A××B.B. In other words, for a binary relation R we haveIn other words, for a binary relation R we have RR ⊆⊆ AA××B. We use the notation aRb to denote thatB. We use the notation aRb to denote that (a, b)(a, b)∈∈R and aR and aRb to denote that (a, b)b to denote that (a, b)∉∉R.R.
  • 3.
    Fall 2002 CMSC203 - Discrete Structures 3 RelationsRelations When (a, b) belongs to R, a is said to beWhen (a, b) belongs to R, a is said to be relatedrelated toto b by R.b by R. Example:Example: Let P be a set of people, C be a set ofLet P be a set of people, C be a set of cars, and D be the relation describing which personcars, and D be the relation describing which person drives which car(s).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, SuzanneThis means that Carl drives a Mercedes, Suzanne drives a Mercedes and a BMW, Peter drives adrives a Mercedes and a BMW, Peter drives a tricycle, and Carla does not drive any of thesetricycle, and Carla does not drive any of these vehicles.vehicles.
  • 4.
    Fall 2002 CMSC203 - Discrete Structures 4 Functions as RelationsFunctions as Relations You might remember that aYou might remember that a functionfunction f from a set Af from a set A to a set B assigns a unique element of B to eachto a set B assigns a unique element of B to each element of A.element of A. TheThe 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 A××B, it is aB, it is a relationrelation from A to B.from A to B. Moreover, for each elementMoreover, for each element aa of A, there isof A, there is exactly one ordered pair in the graph that hasexactly one ordered pair in the graph that has aa asas its first element.its first element.
  • 5.
    Fall 2002 CMSC203 - Discrete Structures 5 Functions as RelationsFunctions as Relations Conversely, if R is a relation from A to B such thatConversely, if R is a relation from A to B such that every element in A is the first element of exactlyevery element in A is the first element of exactly one ordered pair of R, then a function can beone ordered pair of R, then a function can be defined with R as its graph.defined with R as its graph. This is done by assigning to an element aThis is done by assigning to an element a∈∈A theA the unique element bunique element b∈∈B such that (a, b)B such that (a, b)∈∈R.R.
  • 6.
    Fall 2002 CMSC203 - Discrete Structures 6 Relations on a SetRelations on a Set Definition:Definition: A relation on the set A is a relationA 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 subsetIn other words, a relation on the set A is a subset of Aof A××A.A. Example:Example: Let A = {1, 2, 3, 4}. Which ordered pairsLet A = {1, 2, 3, 4}. Which ordered pairs are in the relation R = {(a, b) | a < b} ?are in the relation R = {(a, b) | a < b} ?
  • 7.
    Fall 2002 CMSC203 - Discrete Structures 7 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
  • 8.
    Fall 2002 CMSC203 - Discrete Structures 8 Relations on a SetRelations on a Set How many different relations can we define onHow many different relations can we define on a set A with n elements?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 A××A.A. How many elements are in AHow many elements are in A××A ?A ? There are nThere are n22 elements in Aelements in A××A, so how manyA, so how many subsets (= relations on A) does Asubsets (= relations on A) does A××A have?A have? The number of subsets that we can form out of aThe number of subsets that we can form out of a set with m elements is 2set with m elements is 2mm . Therefore, 2. Therefore, 2nn22 subsetssubsets can be formed out of Acan be formed out of A××A.A. Answer:Answer: We can define 2We can define 2nn22 different relationsdifferent relations on A.on A.
  • 9.
    Fall 2002 CMSC203 - Discrete Structures 9 Properties of RelationsProperties of Relations We will now look at some useful ways to classifyWe will now look at some useful ways to classify relations.relations. Definition:Definition: A relation R on a set A is calledA relation R on a set A is called reflexivereflexive if (a, a)if (a, a)∈∈R for every element aR for every element a∈∈A.A. Are the following relations on {1, 2, 3, 4} reflexive?Are the following relations on {1, 2, 3, 4} 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 calledA relation on a set A is called irreflexiveirreflexive if (a, a)if (a, a)∉∉R for every element aR for every element a∈∈A.A.
  • 10.
    Fall 2002 CMSC203 - Discrete Structures 10 Properties of RelationsProperties of Relations Definitions:Definitions: A relation R on a set A is calledA 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, b∈∈A.A. A relation R on a set A is calledA relation R on a set A is called antisymmetricantisymmetric ifif 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 calledA relation R on a set A is called asymmetricasymmetric ifif (a, b)(a, b)∈∈R implies that (b, a)R implies that (b, a)∉∉R for all a, bR for all a, b∈∈A.A.
  • 11.
    Fall 2002 CMSC203 - Discrete Structures 11 Properties of RelationsProperties of Relations Are 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. andsym. and antisym.antisym. R = {(1, 3), (3, 2), (2, 1)}R = {(1, 3), (3, 2), (2, 1)} antisym.antisym. and asym.and asym. R = {(4, 4), (3, 3), (1, 4)}R = {(4, 4), (3, 3), (1, 4)} antisym.antisym.
  • 12.
    Fall 2002 CMSC203 - Discrete Structures 12 Properties of RelationsProperties of Relations Definition:Definition: A relation R on a set A is calledA 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, thenR, then (a, c)(a, c)∈∈R for a, b, cR for a, b, c∈∈A.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.
  • 13.
    Fall 2002 CMSC203 - Discrete Structures 13 Counting RelationsCounting Relations Example:Example: How many different reflexive relationsHow 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 ARelations on R are subsets of A××A, whichA, which contains ncontains n22 elements.elements. Therefore, different relations on A can beTherefore, different relations on A can be generated by choosing different subsets out ofgenerated by choosing different subsets out of these nthese n22 elements, so there are 2elements, so there are 2nn22 relations.relations. AA reflexivereflexive relation, however,relation, however, mustmust contain the ncontain the n elements (a, a) for every aelements (a, a) for every a∈∈A.A. Consequently, we can only choose among nConsequently, we can only choose among n22 – n =– n = n(n – 1) elements to generate reflexive relations, son(n – 1) elements to generate reflexive relations, so there are 2there are 2n(n – 1)n(n – 1) of them.of them.
  • 14.
    Fall 2002 CMSC203 - Discrete Structures 14 Combining RelationsCombining Relations Relations are sets, and therefore, we can apply theRelations are sets, and therefore, we can apply the usualusual set operationsset operations to them.to them. If we have two relations RIf we have two relations R11 and Rand R22, and both of, and both of them are from a set A to a set B, then we canthem are from a set A to a set B, then we can combine them to Rcombine them to R11 ∪∪ RR22, R, R11 ∩∩ RR22, or R, or R11 – R– R22.. In each case, the result will beIn each case, the result will be another relationanother relation from A to Bfrom A to B..
  • 15.
    Fall 2002 CMSC203 - Discrete Structures 15 Combining RelationsCombining Relations …… and there is another important way to combineand there is another important way to combine relations.relations. Definition:Definition: Let R be a relation from a set A to aLet R be a relation from a set A to a set B and S a relation from B to a set C. Theset B and S a relation from B to a set C. The compositecomposite of R and S is the relation consisting ofof R and S is the relation consisting of ordered pairs (a, c), where aordered pairs (a, c), where a∈∈A, cA, c∈∈C, and for whichC, and for which there exists an element bthere exists an element b∈∈B such that (a, b)B such that (a, b)∈∈R andR and (b, c)(b, c)∈∈S. We denote the composite of R and S byS. We denote the composite of R and S by SS°° RR.. 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 S°° RR contains a pair (a, c).contains a pair (a, c).
  • 16.
    Fall 2002 CMSC203 - Discrete Structures 16 Combining RelationsCombining Relations Example:Example: Let D and S be relations on A = {1, 2, 3, 4}.Let D and S be relations on A = {1, 2, 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)} SS°° D = {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), andD maps an element a to the element (5 – a), and afterwards S maps (5 – a) to all elements larger thanafterwards S maps (5 – a) to all elements larger than (5 – a), resulting in(5 – a), resulting in SS°° D = {(a,b) | b > 5 – a}D = {(a,b) | b > 5 – a} oror SS°° DD = {(a,b) | a + b > 5}.= {(a,b) | a + b > 5}. (4, 4)}(4, 4)}
  • 17.
    Fall 2002 CMSC203 - Discrete Structures 17 Combining RelationsCombining Relations We already know thatWe already know that functionsfunctions are justare just specialspecial casescases ofof relationsrelations (namely those that map each(namely those that map each element in the domain onto exactly one element inelement in the domain onto exactly one element in the codomain).the codomain). If we formally convert two functions into relations,If we formally convert two functions into relations, that is, write them down as sets of ordered pairs,that is, write them down as sets of ordered pairs, the composite of these relations will be exactly thethe composite of these relations will be exactly the same as the composite of the functions (as definedsame as the composite of the functions (as defined earlier).earlier).
  • 18.
    Fall 2002 CMSC203 - Discrete Structures 18 Combining RelationsCombining Relations Definition:Definition: Let R be a relation on the set A. TheLet R be a relation on the set A. The powers Rpowers Rnn , n = 1, 2, 3, …, are defined inductively by, n = 1, 2, 3, …, are defined inductively by RR11 = R= R RRn+1n+1 = R= Rnn °° RR In other words:In other words: RRnn = R= R°° RR°° …… °° R (n times the letter R)R (n times the letter R)
  • 19.
    Fall 2002 CMSC203 - Discrete Structures 19 Combining RelationsCombining Relations Theorem:Theorem: The relation R on a set A is transitive ifThe relation R on a set A is transitive if and only if Rand 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 calledA 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, thenR, then (a, c)(a, c)∈∈R for a, b, cR for a, b, c∈∈A.A. The composite of R with itself contains exactlyThe 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, R°° R does notR does not contain any pairs that are not in R, so Rcontain any pairs that are not in R, so R°° RR ⊆⊆ R.R. Since RSince R°° R does not introduce any pairs that are notR does not introduce any pairs that are not already in R, it must also be true that (Ralready in R, it must also be true that (R°° R)R)°° RR ⊆⊆ R,R, and so on, so that Rand so on, so that Rnn ⊆⊆ R.R.
  • 20.
    Fall 2002 CMSC203 - Discrete Structures 20 n-ary Relationsn-ary Relations In order to study an interesting application ofIn order to study an interesting application of relations, namelyrelations, namely databasesdatabases, we first need to, we first need to generalize the concept of binary relations togeneralize the concept of binary relations to n-aryn-ary relationsrelations.. Definition:Definition: Let ALet A11, A, A22, …, A, …, Ann be sets. Anbe sets. An n-aryn-ary relationrelation on these sets is a subset of Aon these sets is a subset of A11××AA22××……××AAnn.. The sets AThe sets A11, A, A22, …, A, …, Ann are called theare called the domainsdomains of theof the relation, and n is called itsrelation, and n is called its degreedegree..
  • 21.
    Fall 2002 CMSC203 - Discrete Structures 21 n-ary Relationsn-ary Relations Example:Example: Let R = {(a, b, c) | a = 2bLet R = {(a, b, c) | a = 2b ∧∧ b = 2c with a, b, cb = 2c with a, b, c∈∈NN}} 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.
  • 22.
    Fall 2002 CMSC203 - Discrete Structures 22 Databases and RelationsDatabases and Relations Let us take a look at a type of databaseLet us take a look at a type of database representation that is based on relations, namelyrepresentation that is based on relations, namely thethe relational data model.relational data model. A database consists of n-tuples calledA database consists of n-tuples called recordsrecords,, which are made up ofwhich are made up of fieldsfields.. These fields are theThese fields are the entriesentries of the n-tuples.of the n-tuples. The relational data model represents a database asThe relational data model represents a database as an n-ary relation, that is, a set of records.an n-ary relation, that is, a set of records.
  • 23.
    Fall 2002 CMSC203 - Discrete Structures 23 Databases and RelationsDatabases and Relations Example:Example: Consider a database of students, whoseConsider a database of students, whose records are represented as 4-tuples with the fieldsrecords 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 calledRelations that represent databases are also called tablestables, since they are often displayed as tables., since they are often displayed as tables.
  • 24.
    Fall 2002 CMSC203 - Discrete Structures 24 Databases and RelationsDatabases and Relations A domain of an n-ary relation is called aA domain of an n-ary relation is called a primaryprimary keykey if the n-tuples are uniquely determined byif the n-tuples are uniquely determined by their values from this domain.their values from this domain. This means that no two records have the sameThis 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 fieldsIn our example, which of the fields Student NameStudent Name,, ID NumberID Number,, MajorMajor, and, and GPAGPA are primary keys?are primary keys? Student NameStudent Name andand ID NumberID Number are primary keys,are primary keys, because no two students have identical values inbecause no two students have identical values in these fields.these fields. In a real student database, onlyIn a real student database, only ID NumberID Number wouldwould be a primary key.be a primary key.
  • 25.
    Fall 2002 CMSC203 - Discrete Structures 25 Databases and RelationsDatabases and Relations In a database, a primary key should remain oneIn 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 theTherefore, we should use a primary key of the intensionintension 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 identifycan also uniquely identify n-tuples in an n-ary relation.n-tuples in an n-ary relation. When the values of aWhen the values of a set of domainsset of domains determine andetermine an n-tuple in a relation, then-tuple in a relation, the Cartesian productCartesian product ofof these domains is called athese domains is called a composite keycomposite key..
  • 26.
    Fall 2002 CMSC203 - Discrete Structures 26 Databases and RelationsDatabases and Relations We can apply a variety ofWe can apply a variety of operationsoperations on n-aryon n-ary relations to form new relations.relations to form new relations. Definition:Definition: TheThe projectionprojection PPii11, i, i22, …, i, …, imm maps the n-tuplemaps the n-tuple (a(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 mkeeps the m components acomponents aii11 , a, aii22 , …, a, …, aiimm of an n-tuple and deletes itsof an n-tuple and deletes its (n – m) other components.(n – m) other components. Example:Example: What is the result when we apply theWhat 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).
  • 27.
    Fall 2002 CMSC203 - Discrete Structures 27 Databases and RelationsDatabases and Relations In some cases, applying a projection to an entireIn some cases, applying a projection to an entire table may not only result in fewer columns, but alsotable may not only result in fewer columns, but also inin fewer rowsfewer rows.. Why is that?Why is that? Some records may only have differed in thoseSome records may only have differed in those fields that were deleted, so they becomefields that were deleted, so they become identicalidentical,, and there is no need to list identical records moreand there is no need to list identical records more than once.than once.
  • 28.
    Fall 2002 CMSC203 - Discrete Structures 28 Databases and RelationsDatabases and Relations We can use theWe can use the joinjoin operation to combine twooperation 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 aLet R be a relation of degree m and S a relation of degree n. Therelation of degree n. The joinjoin JJpp(R, S), where p(R, S), where p ≤≤ mm and pand p ≤≤ n, is a relation of degree m + n – p thatn, is a relation of degree m + n – p that consists of all (m + n – p)-tuplesconsists 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) belongs to R and the n-tuple (cto R and the n-tuple (c11, c, c22, …, c, …, cpp, b, b11, b, b22, …, b, …, bn-pn-p)) belongs to S.belongs to S.
  • 29.
    Fall 2002 CMSC203 - Discrete Structures 29 Databases and RelationsDatabases and Relations In other words, to generate Jp(R, S), we have toIn other words, to generate Jp(R, S), we have to find all the elements in R whose p last componentsfind all the elements in R whose p last components match the p first components of an element in S.match the p first components of an element in S. The new relation contains exactly these matches,The new relation contains exactly these matches, which are combined to tuples that contain eachwhich are combined to tuples that contain each matching field only once.matching field only once.
  • 30.
    Fall 2002 CMSC203 - Discrete Structures 30 Databases and RelationsDatabases and Relations Example:Example: What is JWhat is J11(Y, R), where Y contains the(Y, R), where Y contains the fieldsfields Student NameStudent Name andand 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 definedand R contains the student records as defined before ?before ?
  • 31.
    Fall 2002 CMSC203 - 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 relationSince Y has two fields and R has four, the relation JJ11(Y, R) has 2 + 4 – 1 = 5 fields.(Y, R) has 2 + 4 – 1 = 5 fields.
  • 32.
    Fall 2002 CMSC203 - Discrete Structures 32 Representing RelationsRepresenting Relations We already know different ways of representingWe already know different ways of representing relations. We will now take a closer look at tworelations. We will now take a closer look at two ways of representation:ways of representation: Zero-one matricesZero-one matrices andand 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] 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 toNote that for creating this matrix we first need to list the elements in A and B in alist the elements in A and B in a particular, butparticular, but arbitrary orderarbitrary order..
  • 33.
    Fall 2002 CMSC203 - Discrete Structures 33 Representing RelationsRepresenting Relations Example:Example: How can we represent the relationHow 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 MThe matrix MRR is given byis given by           = 11 01 00 RM
  • 34.
    Fall 2002 CMSC203 - Discrete Structures 34 Representing RelationsRepresenting Relations What do we know about the matrices representingWhat do we know about the matrices representing aa relation on a setrelation on a set (a relation from A to A) ?(a relation from A to A) ? They areThey are squaresquare matrices.matrices. What do we know about matrices representingWhat do we know about matrices representing reflexivereflexive relations?relations? All the elements on theAll the elements on the diagonaldiagonal of such matricesof such matrices MMrefref must bemust be 1s1s..                     = 1 . . . 1 1 refM
  • 35.
    Fall 2002 CMSC203 - Discrete Structures 35 Representing RelationsRepresenting Relations What do we know about the matrices representingWhat do we know about the matrices 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.
  • 36.
    Fall 2002 CMSC203 - Discrete Structures 36 Representing RelationsRepresenting Relations The Boolean operationsThe Boolean operations joinjoin andand meetmeet (you(you remember?)remember?) can be used to determine the matricescan be used to determine the matrices representing therepresenting the unionunion and theand the intersectionintersection of twoof two relations, respectively.relations, respectively. To obtain theTo obtain the joinjoin of two zero-one matrices, weof two zero-one matrices, we apply the Boolean “or” function to all correspondingapply the Boolean “or” function to all corresponding elements in the matrices.elements in the matrices. To obtain theTo obtain the meetmeet of two zero-one matrices, weof two zero-one matrices, we apply the Boolean “and” function to all correspondingapply the Boolean “and” function to all corresponding elements in the matrices.elements in the matrices.
  • 37.
    Fall 2002 CMSC203 - Discrete Structures 37 Representing RelationsRepresenting Relations Example:Example: Let the relations R and S be representedLet the relations R and S be represented by the matricesby the matrices           =∨=∪ 011 111 101 SRSR MMM           = 001 110 101 SM What are the matrices representing RWhat are the matrices representing R∪∪S and RS and R∩∩S?S? Solution:Solution: These matrices are given byThese matrices are given by           =∧=∩ 000 000 101 SRSR MMM           = 010 001 101 RM
  • 38.
    Fall 2002 CMSC203 - Discrete Structures 38 Representing Relations Using MatricesRepresenting Relations Using Matrices Example:Example: How can we represent the relationHow 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 MThe matrix MRR is given byis given by           = 11 01 00 RM
  • 39.
    Fall 2002 CMSC203 - Discrete Structures 39 Representing Relations Using MatricesRepresenting Relations Using Matrices Example:Example: Let the relations R and S be representedLet the relations R and S be represented by the matricesby the matrices           =∨=∪ 011 111 101 SRSR MMM           = 001 110 101 SM What are the matrices representing RWhat are the matrices representing R∪∪S and RS and R∩∩S?S? Solution:Solution: These matrices are given byThese matrices are given by           =∧=∩ 000 000 101 SRSR MMM           = 010 001 101 RM
  • 40.
    Fall 2002 CMSC203 - Discrete Structures 40 Representing Relations Using MatricesRepresenting Relations Using Matrices Do you remember theDo you remember the Boolean productBoolean product of two zero-of two zero- one matrices?one matrices? Let A = [aLet A = [aijij] be an m] be an m××k zero-one matrix andk zero-one matrix and B = [bB = [bijij] be a k] be a k××n zero-one matrix.n zero-one matrix. Then theThen the Boolean productBoolean product of A and B, denoted byof A and B, denoted by AAοοB, is the mB, is the m××n matrix with (i, j)th entry [cn matrix with (i, j)th entry [cijij], where], where ccijij = (a= (ai1i1 ∧∧ bb1j1j)) ∨∨ (a(ai2i2 ∧∧ bb2i2i)) ∨∨ …… ∨∨ (a(aikik ∧∧ bbkjkj).). 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 ∧∧ bbnjnj) = 1 for some n; otherwise c) = 1 for some n; otherwise cijij = 0.= 0.
  • 41.
    Fall 2002 CMSC203 - Discrete Structures 41 Representing Relations Using MatricesRepresenting Relations Using Matrices Let us now assume that the zero-one matricesLet us now assume that the zero-one matrices MMAA = [a= [aijij], M], MBB = [b= [bijij] and M] and MCC = [c= [cijij] represent relations A,] represent relations A, B, and C, respectively.B, and C, respectively. Remember:Remember: For MFor MCC = M= MAAοοMMBB 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 ∧∧ bbnjnj) = 1 for some n; otherwise c) = 1 for some n; otherwise cijij = 0.= 0. In terms of theIn terms of the relationsrelations, this means that C contains a, this means that C contains a pair (xpair (xii, z, zjj) if and only if there is an element y) if and only if there is an element ynn suchsuch that (xthat (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 = B°°A (A (compositecomposite of A and B).of A and B).
  • 42.
    Fall 2002 CMSC203 - Discrete Structures 42 Representing Relations Using MatricesRepresenting Relations Using Matrices This gives us the following rule:This gives us the following rule: MMBB°°AA = M= MAAοοMMBB In other words, the matrix representing theIn other words, the matrix representing the compositecomposite of relations A and B is theof relations A and B is the BooleanBoolean productproduct of the matrices representing A and B.of the matrices representing A and B. Analogously, we can find matrices representing theAnalogously, we can find matrices representing the powers of relationspowers of relations:: MMRRnn = M= MRR [n][n] (n-th(n-th Boolean powerBoolean power).).
  • 43.
    Fall 2002 CMSC203 - Discrete Structures 43 Representing Relations Using MatricesRepresenting Relations Using Matrices Example:Example: Find the matrix representing RFind the matrix representing R22 , where, where the matrix representing R is given bythe matrix representing R is given by           = 001 110 010 RM Solution:Solution: The matrix for RThe matrix for R22 is given byis given by           == 010 111 110 ]2[ 2 RR MM
  • 44.
    Fall 2002 CMSC203 - Discrete Structures 44 Representing Relations Using DigraphsRepresenting Relations Using Digraphs Definition:Definition: AA directed graphdirected graph, or, or digraphdigraph, consists, consists of a set V ofof a set V of verticesvertices (or(or nodesnodes) together with a) together with a set E of ordered pairs of elements of V calledset E of ordered pairs of elements of V called edgesedges (or(or arcsarcs).). The vertex a is called theThe vertex a is called the initial vertexinitial vertex of theof the edge (a, b), and the vertex b is called theedge (a, b), and the vertex b is called the terminalterminal vertexvertex of this edge.of this edge. We can use arrows to display graphs.We can use arrows to display graphs.
  • 45.
    Fall 2002 CMSC203 - Discrete Structures 45 Representing Relations Using DigraphsRepresenting Relations Using Digraphs 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.
  • 46.
    Fall 2002 CMSC203 - Discrete Structures 46 Representing Relations Using DigraphsRepresenting Relations Using Digraphs Obviously, we can represent any relation R on a setObviously, we can represent any relation R on a set A by the digraph with A as its vertices and all pairsA by the digraph with A as its vertices and all pairs (a, b)(a, b)∈∈R as its edges.R as its edges. Vice versa, any digraph with vertices V and edges EVice versa, any digraph with vertices V and edges E can be represented by a relation on V containing allcan be represented by a relation on V containing all the pairs in E.the pairs in E. ThisThis one-to-one correspondenceone-to-one correspondence betweenbetween relations and digraphs means that any statementrelations and digraphs means that any statement about relations also applies to digraphs, and viceabout relations also applies to digraphs, and vice versa.versa.
  • 47.
    Fall 2002 CMSC203 - Discrete Structures 47 Equivalence RelationsEquivalence Relations Equivalence relationsEquivalence relations are used to relate objectsare used to relate objects that are similar in some way.that are similar in some way. Definition:Definition: A relation on a set A is called anA relation on a set A is called an equivalence relation if it is reflexive, symmetric,equivalence relation if it is reflexive, symmetric, and transitive.and transitive. Two elements that are related by an equivalenceTwo elements that are related by an equivalence relation R are calledrelation R are called equivalentequivalent..
  • 48.
    Fall 2002 CMSC203 - Discrete Structures 48 Equivalence RelationsEquivalence Relations Since R isSince R is symmetricsymmetric, a is equivalent to b whenever, a is equivalent to b whenever b is equivalent to a.b is equivalent to a. Since R isSince R is reflexivereflexive, every element is equivalent to, every element is equivalent to itself.itself. Since R isSince R is transitivetransitive, if a and b are equivalent and b, if a and b are equivalent and b and c are equivalent, then a and c are equivalent.and c are equivalent, then a and c are equivalent. Obviously, these three properties are necessaryObviously, these three properties are necessary for a reasonable definition of equivalence.for a reasonable definition of equivalence.
  • 49.
    Fall 2002 CMSC203 - Discrete Structures 49 Equivalence RelationsEquivalence Relations Example:Example: Suppose that R is the relation on the setSuppose that R is the relation on the set of strings that consist of English letters such thatof strings that consist of English letters such that aRb if and only if l(a) = l(b), where l(x) is the lengthaRb 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 thereforeR 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.
  • 50.
    Fall 2002 CMSC203 - Discrete Structures 50 Equivalence ClassesEquivalence Classes Definition:Definition: Let R be an equivalence relation on aLet R be an equivalence relation on a set A. The set of all elements that are related toset A. The set of all elements that are related to an element a of A is called thean element a of A is called the equivalence classequivalence class of a.of a. The equivalence class of a with respect to R isThe equivalence class of a with respect to R is denoted bydenoted by [a][a]RR.. When only one relation is under consideration, weWhen only one relation is under consideration, we will delete the subscript R and writewill delete the subscript R and write [a][a] for thisfor this equivalence class.equivalence class. If bIf b∈∈[a][a]RR, b is called a, b is called a representativerepresentative of thisof this equivalence class.equivalence class.
  • 51.
    Fall 2002 CMSC203 - Discrete Structures 51 Equivalence ClassesEquivalence Classes Example:Example: In the previous example (strings ofIn the previous example (strings of identical length), what is the equivalence class ofidentical length), what is the equivalence class of the word mouse, denoted by [mouse] ?the word mouse, denoted by [mouse] ? Solution:Solution: [mouse] is the set of all English words[mouse] is the set of all English words containing five letters.containing five letters. For example, ‘horse’ would be a representative ofFor example, ‘horse’ would be a representative of this equivalence class.this equivalence class.
  • 52.
    Fall 2002 CMSC203 - Discrete Structures 52 Equivalence ClassesEquivalence Classes Theorem:Theorem: Let R be an equivalence relation on a setLet R be an equivalence relation on a set A. The following statements are equivalent:A. The following statements are equivalent: • aRbaRb • [a] = [b][a] = [b] • [a][a] ∩∩ [b][b] ≠≠ ∅∅ Definition:Definition: AA partitionpartition of a set S is a collection ofof a set S is a collection of disjoint nonempty subsets of S that have S as theirdisjoint 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,, ii∈∈I, forms a partition of S if and only ifI, forms a partition of S if and only if (i) A(i) Aii ≠≠ ∅∅ for ifor i∈∈II • AAii ∩∩ AAjj == ∅∅, if i, if i ≠≠ jj • ∪∪ii∈∈II AAii = S= S
  • 53.
    Fall 2002 CMSC203 - Discrete Structures 53 Equivalence ClassesEquivalence 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).
  • 54.
    Fall 2002 CMSC203 - Discrete Structures 54 Equivalence ClassesEquivalence Classes Theorem:Theorem: Let R be an equivalence relation on aLet R be an equivalence relation on a set S. Then theset S. Then the equivalence classesequivalence classes of R form aof R form a partitionpartition of S. Conversely, given a partitionof S. Conversely, given a partition {A{Aii | i| i∈∈I} of the set S, there is an equivalenceI} of the set S, there is an equivalence relation R that has the sets Arelation R that has the sets Aii, i, i∈∈I, as itsI, as its equivalence classes.equivalence classes.
  • 55.
    Fall 2002 CMSC203 - Discrete Structures 55 Equivalence ClassesEquivalence Classes Example:Example: Let us assume that Frank, Suzanne andLet us assume that Frank, Suzanne and George live in Boston, Stephanie and Max live inGeorge 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 theLet R be the equivalence relationequivalence relation {(a, b) | a and b{(a, b) | a and b live in the same city} on the set P = {Frank,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)}.
  • 56.
    Fall 2002 CMSC203 - Discrete Structures 56 Equivalence ClassesEquivalence Classes Then theThen the equivalence classesequivalence classes of R are:of R are: {{Frank, Suzanne, George}, {Stephanie, Max},{{Frank, Suzanne, George}, {Stephanie, Max}, {Jennifer}}.{Jennifer}}. This is aThis is a partitionpartition of P.of P. The equivalence classes of any equivalence relationThe equivalence classes of any equivalence relation R defined on a set S constitute a partition of S,R defined on a set S constitute a partition of S, because every element in S is assigned tobecause every element in S is assigned to exactlyexactly oneone of the equivalence classes.of the equivalence classes.
  • 57.
    Fall 2002 CMSC203 - Discrete Structures 57 Equivalence ClassesEquivalence Classes Another example:Another example: Let R be the relationLet 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, …}}