Discrete Structures
Relations
2
Relations
Functions as Relations
 Let A and B be nonempty sets.
A function f from A to B is an assignment of exactly
one element of B to each element of A.
We write f(a) = b if b is the unique element of B
assigned by the function f to the element a of A.
If f is a function from A to B,
we write f : A B
→
Relations are a generalization of function
3
Relations
binary relation
Let A, B be sets, a binary relation R from A to B,
is a subset of A×B. R  AxB
A binary relation from A to B is a set R of ordered
pairs where the first element of each ordered pairs
comes from A and the second element comes from B.
R:A×B, or R:A,B is a subset of the set A×B.
The notation a R b means that (a,b)R.
The notation a R b means that (a,b)R.
When (a,b) belongs to R , a is said to be related to b
by relation R.
4
Relations
binary relation
Example
Let A be the set of students in your school and
let B be set of courses, and
let R be the relation that consists of those pairs
(a,b), where a is a student enrolled in course b.
 If Ahmed, Ali, and Mohamed are enrolled in CP223
and Ahmed, Ali, and Osman are enrolled in CS313
 Then the pairs (Ahmed,CP223), (Ali, CP223),
(Mohamed, CP223), (Ahmed, CS313), (Ali, CS313 ),
and (Osman, CS313) belong to (are in) R.
 The pair (Osman, CP223) is not in R.
5
Relations
Representation of relation (Arrow diagram & table)
Example
Let A ={0,1,2} and B={a,b} and the relation R from A
to B is {(0,a),(0,b),(1,a),(2,b)}.
0
a
1
b
2
Arrow diagram table
0 R a 0 R b 1 R a 2 R b
1 R b 2 R a
R a b
0 x x
1 x
2 x
6
Relations
Representation of relation (digraphs)
A directed graph, or digraph consists of a set V of
vertices (or nodes) together with a set E of ordered
pairs of elements of V called edges (or arcs).
The vertex a is called the initial vertex of the edge
(a,b), and vertex b is called the terminal vertex of
this edge.
a b
An edge of the form (a,a) is represented by an arc
from the vertex a back to itself and it is called a
loop.
a
edge or arc
7
Relations
Representation of relation (digraphs)
Example
R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
loop
vertex(node)
edge(arc)
1
2
3
4
8
Relations
Representation of relation (matrix)
A relation between finite sets can be represented
using a zero-one matrix.
Suppose that R is a relation from A={a1,a2,…,am) to
B={b1,b2,….,bn}. This relation can be represented by
the matrix MR=[mij], where:
[mij]= 1 if (ai,bj)  R
0 if (ai,bj)  R
Example Let A ={0,1,2} and B={a,b} and the relation
R from A to B is {(0,a),(0,b),(1,a),(2,b)}.
1 1
1 0
0 1
MR=
a b
0
1
2
9
Relations on a Set
 A (binary) relation from a set A to itself is called
a relation on the set A.
Example
Let A={1,2,3,4} which ordered pairs are in the
R={(a,b) | a divides b}.
1,2,3,4 are positive integer, max is 4
R= {(1,1),(1,2),(1,3),(1,4),(2,2),(2,4),
(3,3),(4,4)}
Draw the arrow diagram, digraph,
and matrix?
R 1 2 3 4
1 x x x x
2 x x
3 x
4 x
10
Relations on a Set
Example
Consider these relations on the set of integers
R1={(a,b) | a  b}
R2={(a,b) | a  b}
R3={(a,b) | a=b or a=-b}
R4={(a,b) | a=b}
R5={(a,b) | a=b+1}
R6={(a,b) | a+b  3}
Which of these relations contain each of the pairs
(1,1), (1,2), (2,1), (1,-1), and (2,2) ?
The pair (1,1) is in …..
…..
11
Relations on a Set
How many relations are there on a set with n
elements?
A relation on a set A is a subset of AxA.
AxA has n2
elements when A has n elements, and
a set with m elements has 2m
subsets,
there are 2n
2
subsets of AxA.
Thus there are 2n
2
relations on a set with n elements.
For example there are 23
2
= 29
=512 relations on the
set {a,b,c}
12
Properties of Relations
There are several properties that are used to
classify relations on a set.
In some relations an element is always related to
itself.
For example, let R be the relation on the set of all
people consisting of pairs (x,y) where x and y has the
same father and the same mother. Then xRx for
every person x.
13
Properties of Relations
A relation R on a set A is called reflexive if
(a,a)R for every element aA (aA), aRa.
– E.g., the relation ≥ : {(a,b) | a≥b}
≡ is reflexive
 A relation R on the set A is reflexive if
a((a,a)R) when the universe of discourse is the set of
all elements in A.
Reflexive means that every member is related to itself.
 A relation R on a set A is called irreflexive if
(a,a)  R for every element in A
There is no element in A is related to itself
14
Properties of Relations
Example
Consider the following relations on the {1,2,3,4}
R1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)}
R2 ={(1,1),(1,2),(2,1)}
R3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)}
R4 ={(2,1),(3,1),(3,2),(4,1),(3,4)}
R5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
R6 ={(3,4)}
Which of these relations are reflexive?
The relations R3 and R5 are reflexive because they
both contain all pairs of the form (a,a).
irreflexive ?
15
Properties of Relations
Example
Consider the following relations on the set of integers
R1={(a,b) | a  b}
R2={(a,b) | a  b}
R3={(a,b) | a=b or a=-b}
R4={(a,b) | a=b}
R5={(a,b) | a=b+1}
R6={(a,b) | a+b  3}
Which of these relations are reflexive?
The relations R1 , R3 and R4 are reflexive because
they both contain all pairs of the form (a,a).
irreflexive ?
16
Properties of Relations
Example
 Is the “divides” relation on the set of positive
integers reflexive?
 Is the “divides” relation on the set of integers
reflexive?
Note that 0 does not divide 0.
17
Properties of Relations
A relation R on a set A is called reflexive if and only
if there is a loop at every vertex of the directed
graph.
R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
1
2
3
4
irreflexive ?
18
Properties of Relations
A relation R on a set A is called reflexive if and only
if (ai,ai)R this means that mii=1 for i=1,2,.,n
All the elements on the main diagonal of MR are equal
to 1
R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
1 1 1 1
0 1 1 1
0 0 1 1
0 0 0 1
MR=
irreflexive ?
19
Properties of Relations
 A relation R on a set A is symmetric
if (b,a)R whenever (a,b)R for all a,b A
ab((a,b) R → (b,a)R )
 A relation R on a set A is antisymmetric
if (a,b)R and (b,a)R then a=b for all a,b A
ab((a,b) R  (b,a)R → (a=b) )
Note that “the term symmetric and antisymmetric are
not opposites, the relation can have both of these
properties or may lack both of them”
20
Properties of Relations
A relation cannot be both symmetric and
antisymmetric if it contains some pair of the form
(a,b), where a≠b
example
Let R be the following relation defined on the set
{a, b, c, d}:
R = {(a, a), (a, c), (a, d), (b, a), (b, b), (b, c), (b,
d), (c, b), (c, c), (d, b), (d, d)}.
Determine whether R is:
(a) reflexive. Yes
(b) symmetric. No there is no (c,a) for example
(c) antisymmetric. No b  c b  d
21
Properties of Relations
A relation cannot be both symmetric and
antisymmetric if it contains some pair of the form
(a,b), where a≠b
example
Let R be the following relation defined on the set
{a, b, c, d}:
R = {(a, a), (b, b), (c, c), (d, d)}.
Determine whether R is:
(a) reflexive. Yes
(b) symmetric. yes
(c) antisymmetric. yes
22
Properties of Relations
Example
Consider the following relations on the {1,2,3,4}
R1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)}
R2 ={(1,1),(1,2),(2,1)}
R3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)}
R4 ={(2,1),(3,1),(3,2),(4,1),(3,4)}
R5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
R6 ={(3,4)}
Which of these relations are symmetric and which are
antisymmetric ?
R2 and R3 are symmetric because in each case (b,a)
belongs to the relation whenever (a,b) does.
23
Properties of Relations
Example
Consider the following relations on the set of integers
R1={(a,b) | a  b}
R2={(a,b) | a  b}
R3={(a,b) | a=b or a=-b}
R4={(a,b) | a=b}
R5={(a,b) | a=b+1}
R6={(a,b) | a+b  3}
Which of these relations are symmetric and which are
antisymmetric ?
R3 , R4 ,and R6 are symmetric because in each case
(b,a) belongs to the relation whenever (a,b) does.
24
Properties of Relations
Example
Consider the following relations on the set of integers
R1={(a,b) | a  b} ab and ba imply that a=b
R2={(a,b) | a  b}
R3={(a,b) | a=b or a=-b}
R4={(a,b) | a=b}
R5={(a,b) | a=b+1}
R6={(a,b) | a+b  3}
R1 , R2 , R4 , R5 are antisymmetric
R2 is antisymmetric it is impossible for a>b and b>a
R5 is antisymmetric it is impossible for a=b+1 and
b=a+1
25
Properties of Relations
Example
Is the “divides” relation on the set of positive
integers symmetric? Is it antisymmetric ?
This relation is not symmetric because 1|2, but 2|1.
It is antisymmetric because a|b, and b|a then a=b.
26
Properties of Relations
A relation R on a set A is called symmetric if and
only if for every edge between distinct vertices in its
directed graph there is an edge in the opposite
direction.
R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
1
2
3 4
Not
symmetric
27
Properties of Relations
A relation R on a set A is called antisymmetric if and
only if there are never two edges in the opposite
direction between distinct vertices in its directed
graph
1
2
3 4
Antisymmetric
Not reflexive
Not symmetric
28
Properties of Relations
A relation R on a set A is called symmetric if and
only if mij=mji of MR for i=1,2,.,n j=1,2,.,n
R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
1 1 1 1
0 1 1 1
0 0 1 1
0 0 0 1
MR=
1 2 3 4
1
2
3
4
(a,b)
Antisymmetric
29
Properties of Relations
A relation R on a set A is called symmetric if and
only if mij=mji of MR for i=1,2,.,n j=1,2,.,n
R={(1,1),(1,2),(1,3),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
1 1 1 0
0 1 1 1
0 0 1 1
0 0 0 1
MR=
1 2 3 4
1
2
3
4
(a,b)
Antisymmetric
30
Properties of Relations
Suppose that the relation R on a set A is represented
by the matrix
1 1 0
1 1 1
0 1 1
MR=
A relation R is reflexive iff (ai,ai)R
this means that mii=1 for i=1,2,.,n
A relation R is symmetric
if (a,b)R (b,a)
↔ R
this means that mij=mji for i=1,2,.,n
1 1 0
1 1 1
0 1 1
MR=
31
Properties of Relations
Suppose that the relation R on a set A is represented
by the matrix
1 1 0
0 1 1
0 1 0
MR= This relation is reflexive symmetric
antisymmetric
This relation is reflexive symmetric
antisymmetric
0 0 0
1 1 1
1 0 1
MR=
32
Properties of Relations
Suppose that the relation R on a set A is represented
by the matrix
1 1 0
1 1 1
0 1 0
MR= This relation is reflexive symmetric
antisymmetric
This relation is reflexive symmetric
antisymmetric
1 1 0
1 1 0
0 0 1
MR=
33
Properties of Relations
Let R be the relation consisting of all pairs (x,y) of
students at your school, where x has taken more
credits than y.
Suppose that x is related to y and y related to z.
This means that
x has taken more credits than y and
y has taken more credits than z
We can conclude that
x has taken more credits than z, so that x is
related to z.
The relation R has the transitive property.
34
Properties of Relations
A relation R on a set A is called transitive if
whenever (a,b)R and (b,c)R then (a,c)R , for all
a, b, c  A
abc(( (a,b)R  (b,c)R) → (a,c)R)
35
Properties of Relations
Consider the following relations on the {1,2,3,4}
R1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)}
R2 ={(1,1),(1,2),(2,1)}
R3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)}
R4 ={(2,1),(3,1),(3,2),(4,1),(4,2),((4,3)}
R5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3),
(3,4),(4,4)}
R6 ={(3,4)}
Which of these relations are transitive ?
 The relation is transitive
If (a,b) and (b,c) belong to the relation
then (a,c) also does.
R4 (3,2),(2,1),(3,1) (4,2) (2,1),(4,1)
36
Properties of Relations
Consider the following relations on the set of integers
R1={(a,b) | a  b}
R2={(a,b) | a  b}
R3={(a,b) | a=b or a=-b}
R4={(a,b) | a=b}
R5={(a,b) | a=b+1}
R6={(a,b) | a+b  3}
Which of these relations are transitive ?
 The relation is transitive
If (a,b) and (b,c) belong to the relation
then (a,c) also does.
37
Properties of Relations
Is the “divides” relation on the set of positive
integers transitive?
Suppose that a divides b and b divides c.
Then there are positive integers k and l such that
b=ak and c=bl.
Hence, c=a(kl), so a divides c.
It follows that the relation is transitive
38
Properties of Relations
A relation is transitive if and only if whenever there
is an edge from a vertex x to a vertex y and an edge
from a vertex y to a vertex z, there is an edge
from a vertex x to a vertex z completing a triangle
where each side is a directed edge with the correct
direction.
1
2
3 4
39
Combining Relations
Let A={1,2,3} and B={1,2,3,4}
The relation
R1={(1,1),(2,2),(3,3)}
R2={(1,1),(1,2),(1,3),(1,4)}
R1  R2 = {(1,1),(1,2),(1,3),(1,4),(2,2),(3,3)}
R1  R2 = {(1,1)}
R1 - R2 = {(2,2),(3,3)}
R2 - R1 = {(1,2),(1,3),(1,4)}
R1  R2 = R2  R1 = R1  R2 - R1  R2
= {(1,2),(1,3),(1,4),(2,2),(3,3)}
40
Combining Relations
Let A={1,2,3} and B={1,2,3,4}
The relation
R1={(1,1),(2,2),(3,3)}
R2={(1,1),(1,2),(1,3),(1,4)}
Construct MR1
and MR2
R1  R2 = MR1R2
= MR1
 MR2
R1  R2 = MR1R2
= MR1
 MR2
41
Compositions of Relations
Let R be a relation from a set A to a set B and S a
relation from B to a set C.
The composite of R and S is the relation consisting of
ordered pairs (a,c), where aA , cC, and for which
there exists an element bB such that (a,b)R and
(b,c)S. we denote the composite of R and S by
SR
Example
R is the relation from {1,2,3} to {1,2,3,4}
S is the relation from {1,2,3,4} to {0,1,2}
R = {(1,1),(1,4),(2,3),(3,1),(3,4)}
S = {(1,0),(2,0),(3,1),(3,2),(4,1)}
SR={(1,0),(1,1),(2,1),(2,2),(3,0),(3,1)}} T/F
42
Compositions of Relations
To find the matrix representing the relation SR
(composite of R and S)
Construct MR and Ms
Then calculate the Boolean product (⊙) of the matrix
MR and Ms
MSR= MR ⊙ Ms
43
Compositions of Relations
• The nth
power Rn
of a relation R on a set A can be
defined recursively by:
R1 =
R Rn+1
= Rn
R for all n>0.
R2
= RR , R3
= R2
R = (RR)R
Example
R = {(1,1),(2,1),(3,2),(4,3)}, find the powers
Rn
,n=2,3,4,….
R2
= RR= {(1,1),(2,1),(3,1),(4,2)}
R3
= R2
R= {(1,1),(2,1),(3,1),(4,1)}
R4
= R3
R= {(1,1),(2,1),(3,1),(4,1)}= R3
Rn
= R3
44
Compositions of Relations
Let R be a relation from a set A to a set B,
 The inverse relation (R-1
) from B to A is the set of
ordered pairs { (b,a) | (a,b)  R }
 The complement relation R is the set of ordered
pairs { (a,b) | (a,b)  R }
45
Closures of Relations
Consider relation R={(1,2),(2,2),(3,3)} on the
set A = {1,2,3,4}.
Is R reflexive? No
What can we add to R to make it reflexive?
(1,1), (4,4)
R’ = R U {(1,1),(4,4)} is called the reflexive closure
of R.
46
Closures of Relations
In general
Let R be a relation on a set A
R may or may not have some property P such as:
Reflexivity – Symmetry – Transitivity
The closure of relation R on set A with respect to
property P is the relation R’ with
 R  R’
 R’ has property P
R’ is called the closure of R with respect to P
47
Closures of Relations
Let R be the relation on {1, 2, 3, 4} such that
R = {(1, 1), (1, 4), (2, 3), (3, 1), (3, 3), (4, 4)}.
Find: (a) the reflexive closure of R.
(b) the symmetric closure of R.
(c) the transitive closure of R.
(a) {(1,1), (1,4), (2,2), (2,3), (3,1), (3,3), (4,4)}.
(b) {(1,1), (1,3), (1,4), (2,3), (3,1), (3,2), (3,3),
(4,1), (4,4)}.
(c) {(1,1), (1,4), (2,1), (2,3), (2,4), (3,1), (3,3),
(3,4), (4,4)}.
48
Equivalence Relations
A relation on a set A is called equivalence relation if
it is reflexive, symmetric, and transitive.
Two elements a and b that related by an equivalence
relation are called equivalent. a ~ b
Is R is equivalence relation?
R={(a,b) | a=b or a=-b} r,s, and t
49
Equivalence Relations
Congruence Modulo m
Let m be a positive integer m>1 . Show that the
following relation is an equivalence relation on the set
of integers.
R={ (a,b) | ab(mod m) }
Note that ab(mod m) Means m divides a-b
 a-a=0 and is divisible by m ( R is reflexive )
 ab(mod m) then a-b=km where k is an integer
It follows that b-a=(-k)m means ba(mod m)
( R is symmetric )
 suppose that ab(mod m) and bc(mod m)
a-b=km and b-c=lm add both equations we get:
a-b+ b-c= km+ lm=(k+l)m
a-c=(k+l)m I.e ac(mod m) ( R is transitive )
R is equivalence relation
50
Equivalence Classes
Let R be an equivalence relation on S. The set of all
elements that are related to an element a of S is
called equivalence class of a. the equivalence class of
a with respect to R is denoted by [a]R .
a  S, [a]R, is
[a]R = {s|(a,s)  R} or
[a]R = {s: aRs}
If b  [a]R b is called a representative of this
equivalence relation
 Any element of a class can be used as a
representative of this class.
51
Equivalence Classes
Example
What is equivalence class of an integer for the
following equivalence relation?
R={(a,b) | a=b or a=-b}
In this equivalence relation the integer is related to
itself and its negative, so :
[a]R={-a,a} or [a] ={-a,a}
[7] ={-7,7}
[5] ={-5,5}
[0] ={0}
52
Equivalence Classes
Example
What is equivalence class of 0 and 1 for the
Congruence Modulo 4?
The equivalence class of 0 contains all integers a such
that a0(mod 4)
[0] ={…………,-8,-4,0,4,8,……………….}
The equivalence class of 1 contains all integers a such
that a1(mod 4)
[1] ={…………,-7,-3,1,5,9,……………….}
Congruence classes modulo m
[a]m={……………,a-2m,a-m,a,a+m,a+2m,……………..}
53
Equivalence Classes
Example
Let n be a positive integer and S a set of strings.
Rn is the relation on S such that sRnt iff s=t or both
s and t have at least n characters and the first n
characters of s and t are the same.
sR3t 01 R3 01 00111 R3 00101
01 R3 11 00111 R3 01101
What is equivalence class of the string 0111 with
respect to the R3?
[011]R3 ={ 011, 0110,0111,01100,01101,01110,
01111,………}
54
Equivalence Classes and Partitions
Let n be a positive integer and S a set of strings.
Rn is the relation on S such that sRnt iff s=t or both
s and t have at least n characters and the first n
characters of s and t are the same.
sR3t
[ ]R3 ={ }
[0]R3 ={0}
[1]R3 ={1}
[00]R3 ={00}
[01]R3 ={01}
[10]R3 ={10}
[11]R3 ={11}
55
Equivalence Classes and Partitions
[000]R3 ={000,0000,0001,00000,00001,00011,………}
[001]R3 ={001,0010,0011,00100,00101,00111,………}
[010]R3 ={010,0100,0101,01000,01001,01011,………}
[011]R3 ={011,0110,0111,01100,01101,01111,………}
[100]R3 ={100,1000,1001,10000,10001,10011,………}
[101]R3 ={101,1010,1011,10100,10101,10111,………}
[110]R3 ={110,1100,1101,11000,11001,11011,………}
[111]R3 ={111, 1110,1111,11100,11101,11111,………}
These 15 equivalence classes are disjoint and every
bit string is in exactly one of them.
These equivalence classes partition the set of all bit
strings.
56
Partial Orderings
Let R be a relation on a set S, then R is a Partially
Ordered Set (POSet) if it is
 Reflexive - aRa, a
 Transitive - aRb  bRc  aRc, a,b,c
 Antisymmetric - aRb  bRa  a=b, a,b
and denoted by (R,S)
R={(a,b) | a  b}
 a  a Reflexive
 a  b and b  a implies a=b Antisymmetric
 a  b and b  c implies a  c Transitive
 is is a partial ordering on Z, and (Z,) is poset
57
Partial Orderings
Example
(Z+
, | ), the relation “divides” on positive integers.
Reflexive?
a|a since a=1a (k=1)
Antisymmetric?
a|b means b=ak,
b|a means a=bj. But b = bjk this means jk=1.
jk=1 means j=k=1, and we have b=a1, or b=a
Transitive?
a|b means b=ak, b|c means c=bj.
c = bj = akj =am where m=kj then a|c
| is is a partial ordering on Z+
, and (Z+
,|) is poset
58
Partial Orderings
Example
Show that the inclusion relation  is a partial
ordering on the power set of a set S?
Reflexive? A  A
Antisymmetric? A  B and B  A then A=B
Transitive? A  B and B  C then A  C
 is is a partial ordering on P(s), and (P(s),  ) is
poset
59
Partial Orderings
Different symbols such , , and | are used for a
partial ordering.
The general symbol ≼ is used for a partial ordering.
a b
≼ means (a,b)  R in an arbitrary poset (S,R).
 The elements a and b of a poset (S, )
≼ are called
comparable if either a b
≼ or b a
≼ .
 when a and b are elements of S such that neither
a b
≼ nor b a
≼ , a and b are called incomparable.
60
Partial Orderings
Example
In the poset (Z+
,|) , are the integers 3 and 9
comparable? are the integers 5 and 7 comparable?
3|9 comparable
5|7 7|5 incomparable
 The adjective “partial” is used to describe partial
orderings because pairs of elements may be
incomparable.
 When every two elements in the set are
comparable, the relation is called total ordering.
61
Partial Orderings
If (S, )
≼ is a poset and every two elements of S are
comparable, S is called a totally ordered or linear
ordered set (chain). And is called a total order or
≼
a linear order.
Examples
 The poset (Z,) is totally ordered ab or ba.
 The poset (Z+
,|) is not totally ordered ex. 5,7
62
Hasse Diagrams
Hasse diagrams are a special kind of graphs used to
describe posets.
Ex. In poset ({1,2,3,4}, ), we can draw the following directed
graph, or digraph to describe the relation.
1 2 3 4
63
Hasse Diagrams
Hasse diagrams are a special kind of graphs used to
describe posets.
1. Draw edge (a,b) if a b
2. Don’t draw self loops
3. Don’t draw transitive edges
4. Don’t draw up arrows
1 2 3 4
64
Hasse Diagrams
Hasse diagrams are a special kind of graphs used to
describe posets.
1. Draw edge (a,b) if a b
2. Don’t draw self loops
3. Don’t draw transitive edges
4. Don’t draw up arrows
1 2 3 4
65
Hasse Diagrams
Hasse diagrams are a special kind of graphs used to
describe posets.
1. Draw edge (a,b) if a b
2. Don’t draw self loops
3. Don’t draw transitive edges
4. Don’t draw up arrows
1 2 3 4
The poset (Z,) is totally ordered (chain) ab or ba.
66
Hasse Diagrams
Í is is a partial ordering on P(s), and (P(s),  ) is
poset
The hasse digram of (P({a,b,c}),  )
{a,b,c} or 111
{a,b} or 110 {a,c} or 101 {b,c} or 011
{a} or 100 {b} or 010 {c} or 001
{} or 000
67
Hasse Diagrams
Maximal and Minimal Elements
o An element in the poset is called maximal if it is
not less than any elements of the poset.
o An element in the poset is called minimal if it is
not greater than any elements of the poset.
Reds are maximal.
whites are minimal.
68
Hasse Diagrams
Which elements of the poset ({2,4,5,10,12,20,25),|)
Are maximal, and which are minimal?
Maximal elements are 12,20,25
minimal elements are 2,5
2
4
12
5
10
20
25
Note that: 25 is the greatest element and 2 is the
least element.
69
Hasse Diagrams
Which elements of the poset ( {1,2,3,4}, ),
Are maximal, and which are minimal?
Maximal element is 4
minimal element is 1
1
2
3
4
Note that: 4 is the greatest
element and 1 is the least
element.
70
N-ary Relations and Their Applications
The relationships among elements from more than
two sets are called n-ary relations.
Let A1, A2, …., An be sets, an n-ary relations on
these sets is a subset of A1xA2x…..xAn.
The sets A1, A2, …., An are called the domains of the
relation, and n is called its degree.
Example
Let R be the relation on NxNxN consisting of triples
(a,b,c), where a, b, and c are integers with a<b<c.
(1,2,3)R (2,4,3)R
The degree of this relation is 3
Its domains are equal to the sets of natural numbers.
71
N-ary Relations and Their Applications
Example
Let R be the relation on ZxZxZ consisting of triples
(a,b,c), where a, b, and c are integers with b-a=k
and c-b=k, where k (common difference) is an
integer. This relation is called arithmetic progression
(a,a+k,a+2k).
(1,2,3) , (1,3,5) R ,
(2,4,3) , (2,5,9) R
The degree of this relation is 3
Its domains are equal to the sets of integers.
72
N-ary Relations and Their Applications
Example
Let R be the relation on ZxZxZ consisting of triples
(a,b,c), where a, b, and c are integers with b/a=k
and c/b=k, where k (common ratio) is an integer. This
relation is called geometric progression (a,ak,ak2
).
(1,3,9) , (1,4,16) R ,
(2,4,3) , (2,5,9) R
The degree of this relation is 3
Its domains are equal to the sets of integers.
73
N-ary Relations and Their Applications
Example
Let R be the relation on ZxZxZ+
consisting of triples
(a,b,m), where a, b, and m are integers with m1
and ab(mod m).
(8,2,3) , (-1,9,5) ,(14,0,7) R ,
(7,2,3) , (-2,-8,5) , (11,0,6) R
The degree of this relation is 3
Its first two domains are the sets all of integers.
And its third domain is the set of all positive
integers.
Congruence Modulo m m>0
ab(mod m). Means m divides a-b
74
N-ary Relations and Their Applications
Example
Let R be the relation consisting of 5-tuples
(A,N,S,D,T) representing airplane flights, where A is
the airline, N is the flight number, S is the starting
point, D is the destination, and T is the departure
time.
(Saudi Arabian Airlines,304,Cairo,Jeddah,15:00) R
The degree of this relation is 5
Its domains are the set of all airlines, the set of
flight numbers, the set of cities , the set of cites,
and the set of times.
75
Databases and Relations
Relational Databases
A relational database is essentially just an n-ary
relation R.
A database consists of records, which are n-tuples,
made up of fields. These fields are the entire of the
n-tuples. Relations used to represent databases are
called tables.
Each column of the table corresponds to an attribute of
the database.
76
Databases and Relations
A domain of an n-ary relation is called a primary key
when the value of the n-tuple from this domain
determines the n-tuple. That is,a domain is primary
key when no two n-tuples in the relation have the
same value from this domain.
Student_name ID_number Major GPA
Ahmed Ali 0612345 CS 3.88
Ashraf Sami 0412364 Physics 3.65
Waleed Tarek 0512432 Math 2.88
Tarek Morad 0723465 CS 3.65
77
Databases and Relations
Records are often added to or deleted from
databases. Thus, the primary key should be chosen
that remains one whenever the database is changed.
The current collection of the n-tuples in a relation is
called the extension of the relation.
The more permanent part of a database, including the
name and attributes of the database is called the
intension.
Selecting the primary key depends on the possible
extensions of the database.
78
Databases and Relations
Combinations of domains can also uniquely identify
n-tuples in n-ary database.
The Cartesian product of these domains is called a
composite key
A composite key for the database is a set of domains
{Ai, Aj, …} such that R contains at most 1 n-tuple
(…,ai,…,aj,…) for each composite value
(ai, aj,…)Ai×Aj×…
See student relation
Is (Major x GPA) a composite key for the n-ary
relation ? Assuming that no n-tuples are ever added
79
Operations on n-ary Relations
Selection Operator
Let R be an n-ary relation and C a condition that
elements in R may satisfy.
Then the selection operator sC maps the n-ary
relation R to the n-ary relation of all n-tuples from R
that satisfy the condition C.
80
Operations on n-ary Relations
selection operator sC1 where c1 is the condition
major=“CS“ The result is the two 4-tuples.
(Ahmed Ali, 0612345, CS , 3.88)
(Tarek Morad, 0723465, CS , 3.65)
sC2 GPA >3.5 sC3 (Major=“CS“  GPA >3.5 )
Student_name ID_number Major GPA
Ahmed Ali 0612345 CS 3.88
Ashraf Sami 0412364 Physics 3.65
Waleed Tarek 0512432 Math 2.88
Tarek Morad 0723465 CS 3.65
81
Operations on n-ary Relations
Projection Operators
The projection Pi1,i2,….im where i1<i2<….im , maps the n-
tuple (a1,a2,….,an) to the m-tuple (ai1,ai2,…aim), where
m ≤ n
The projection Pi1,i2,….im deletes n-m of the components
of an n-tuple, leaving the i1th, i2th,….,imth
components
P1,3 is applied to the 4-tuples
(2,3,0,4) ,(Tarek Morad, 0723465, CS , 3.65)
(2,0) , (Tarek Morad, CS)
82
Operations on n-ary Relations
P1,4 is applied to the relation in the table
Student_name ID_number Major GPA
Ahmed Ali 0612345 CS 3.88
Ashraf Sami 0412364 Physics 3.65
Waleed Tarek 0512432 Math 2.88
Tarek Morad 0723465 CS 3.65
Student_name GPA
Ahmed Ali 3.88
Ashraf Sami 3.65
Waleed Tarek 2.88
Tarek Morad 3.65
New relation is produced
using projection
83
Operations on n-ary Relations
Join Operator
• Puts two relations together to form a sort of
combined relation.
• If the tuple (A,B) appears in R1, and the tuple (B,C)
appears in R2, then the tuple (A,B,C) appears in the
join J(R1,R2).
– A, B, and C here can also be sequences of
elements (across multiple fields), not just single
elements
84
Operations on n-ary Relations
Join Operator example
Suppose R1 is a teaching assignment table, relating
Professors to Courses.
Suppose R2 is a room assignment table relating
Courses to Rooms,Times.
Then J(R1,R2) is like your class schedule, listing
(professor,course,room,time).

Discrete Structures_Relations_Lec 1.pptx

  • 1.
  • 2.
    2 Relations Functions as Relations Let A and B be nonempty sets. A function f from A to B is an assignment of exactly one element of B to each element of A. We write f(a) = b if b is the unique element of B assigned by the function f to the element a of A. If f is a function from A to B, we write f : A B → Relations are a generalization of function
  • 3.
    3 Relations binary relation Let A,B be sets, a binary relation R from A to B, is a subset of A×B. R  AxB A binary relation from A to B is a set R of ordered pairs where the first element of each ordered pairs comes from A and the second element comes from B. R:A×B, or R:A,B is a subset of the set A×B. The notation a R b means that (a,b)R. The notation a R b means that (a,b)R. When (a,b) belongs to R , a is said to be related to b by relation R.
  • 4.
    4 Relations binary relation Example Let Abe the set of students in your school and let B be set of courses, and let R be the relation that consists of those pairs (a,b), where a is a student enrolled in course b.  If Ahmed, Ali, and Mohamed are enrolled in CP223 and Ahmed, Ali, and Osman are enrolled in CS313  Then the pairs (Ahmed,CP223), (Ali, CP223), (Mohamed, CP223), (Ahmed, CS313), (Ali, CS313 ), and (Osman, CS313) belong to (are in) R.  The pair (Osman, CP223) is not in R.
  • 5.
    5 Relations Representation of relation(Arrow diagram & table) Example Let A ={0,1,2} and B={a,b} and the relation R from A to B is {(0,a),(0,b),(1,a),(2,b)}. 0 a 1 b 2 Arrow diagram table 0 R a 0 R b 1 R a 2 R b 1 R b 2 R a R a b 0 x x 1 x 2 x
  • 6.
    6 Relations Representation of relation(digraphs) A directed graph, or digraph consists of a set V of vertices (or nodes) together with a set E of ordered pairs of elements of V called edges (or arcs). The vertex a is called the initial vertex of the edge (a,b), and vertex b is called the terminal vertex of this edge. a b An edge of the form (a,a) is represented by an arc from the vertex a back to itself and it is called a loop. a edge or arc
  • 7.
    7 Relations Representation of relation(digraphs) Example R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} loop vertex(node) edge(arc) 1 2 3 4
  • 8.
    8 Relations Representation of relation(matrix) A relation between finite sets can be represented using a zero-one matrix. Suppose that R is a relation from A={a1,a2,…,am) to B={b1,b2,….,bn}. This relation can be represented by the matrix MR=[mij], where: [mij]= 1 if (ai,bj)  R 0 if (ai,bj)  R Example Let A ={0,1,2} and B={a,b} and the relation R from A to B is {(0,a),(0,b),(1,a),(2,b)}. 1 1 1 0 0 1 MR= a b 0 1 2
  • 9.
    9 Relations on aSet  A (binary) relation from a set A to itself is called a relation on the set A. Example Let A={1,2,3,4} which ordered pairs are in the R={(a,b) | a divides b}. 1,2,3,4 are positive integer, max is 4 R= {(1,1),(1,2),(1,3),(1,4),(2,2),(2,4), (3,3),(4,4)} Draw the arrow diagram, digraph, and matrix? R 1 2 3 4 1 x x x x 2 x x 3 x 4 x
  • 10.
    10 Relations on aSet Example Consider these relations on the set of integers R1={(a,b) | a  b} R2={(a,b) | a  b} R3={(a,b) | a=b or a=-b} R4={(a,b) | a=b} R5={(a,b) | a=b+1} R6={(a,b) | a+b  3} Which of these relations contain each of the pairs (1,1), (1,2), (2,1), (1,-1), and (2,2) ? The pair (1,1) is in ….. …..
  • 11.
    11 Relations on aSet How many relations are there on a set with n elements? A relation on a set A is a subset of AxA. AxA has n2 elements when A has n elements, and a set with m elements has 2m subsets, there are 2n 2 subsets of AxA. Thus there are 2n 2 relations on a set with n elements. For example there are 23 2 = 29 =512 relations on the set {a,b,c}
  • 12.
    12 Properties of Relations Thereare several properties that are used to classify relations on a set. In some relations an element is always related to itself. For example, let R be the relation on the set of all people consisting of pairs (x,y) where x and y has the same father and the same mother. Then xRx for every person x.
  • 13.
    13 Properties of Relations Arelation R on a set A is called reflexive if (a,a)R for every element aA (aA), aRa. – E.g., the relation ≥ : {(a,b) | a≥b} ≡ is reflexive  A relation R on the set A is reflexive if a((a,a)R) when the universe of discourse is the set of all elements in A. Reflexive means that every member is related to itself.  A relation R on a set A is called irreflexive if (a,a)  R for every element in A There is no element in A is related to itself
  • 14.
    14 Properties of Relations Example Considerthe following relations on the {1,2,3,4} R1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)} R2 ={(1,1),(1,2),(2,1)} R3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)} R4 ={(2,1),(3,1),(3,2),(4,1),(3,4)} R5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} R6 ={(3,4)} Which of these relations are reflexive? The relations R3 and R5 are reflexive because they both contain all pairs of the form (a,a). irreflexive ?
  • 15.
    15 Properties of Relations Example Considerthe following relations on the set of integers R1={(a,b) | a  b} R2={(a,b) | a  b} R3={(a,b) | a=b or a=-b} R4={(a,b) | a=b} R5={(a,b) | a=b+1} R6={(a,b) | a+b  3} Which of these relations are reflexive? The relations R1 , R3 and R4 are reflexive because they both contain all pairs of the form (a,a). irreflexive ?
  • 16.
    16 Properties of Relations Example Is the “divides” relation on the set of positive integers reflexive?  Is the “divides” relation on the set of integers reflexive? Note that 0 does not divide 0.
  • 17.
    17 Properties of Relations Arelation R on a set A is called reflexive if and only if there is a loop at every vertex of the directed graph. R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} 1 2 3 4 irreflexive ?
  • 18.
    18 Properties of Relations Arelation R on a set A is called reflexive if and only if (ai,ai)R this means that mii=1 for i=1,2,.,n All the elements on the main diagonal of MR are equal to 1 R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} 1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1 MR= irreflexive ?
  • 19.
    19 Properties of Relations A relation R on a set A is symmetric if (b,a)R whenever (a,b)R for all a,b A ab((a,b) R → (b,a)R )  A relation R on a set A is antisymmetric if (a,b)R and (b,a)R then a=b for all a,b A ab((a,b) R  (b,a)R → (a=b) ) Note that “the term symmetric and antisymmetric are not opposites, the relation can have both of these properties or may lack both of them”
  • 20.
    20 Properties of Relations Arelation cannot be both symmetric and antisymmetric if it contains some pair of the form (a,b), where a≠b example Let R be the following relation defined on the set {a, b, c, d}: R = {(a, a), (a, c), (a, d), (b, a), (b, b), (b, c), (b, d), (c, b), (c, c), (d, b), (d, d)}. Determine whether R is: (a) reflexive. Yes (b) symmetric. No there is no (c,a) for example (c) antisymmetric. No b  c b  d
  • 21.
    21 Properties of Relations Arelation cannot be both symmetric and antisymmetric if it contains some pair of the form (a,b), where a≠b example Let R be the following relation defined on the set {a, b, c, d}: R = {(a, a), (b, b), (c, c), (d, d)}. Determine whether R is: (a) reflexive. Yes (b) symmetric. yes (c) antisymmetric. yes
  • 22.
    22 Properties of Relations Example Considerthe following relations on the {1,2,3,4} R1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)} R2 ={(1,1),(1,2),(2,1)} R3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)} R4 ={(2,1),(3,1),(3,2),(4,1),(3,4)} R5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} R6 ={(3,4)} Which of these relations are symmetric and which are antisymmetric ? R2 and R3 are symmetric because in each case (b,a) belongs to the relation whenever (a,b) does.
  • 23.
    23 Properties of Relations Example Considerthe following relations on the set of integers R1={(a,b) | a  b} R2={(a,b) | a  b} R3={(a,b) | a=b or a=-b} R4={(a,b) | a=b} R5={(a,b) | a=b+1} R6={(a,b) | a+b  3} Which of these relations are symmetric and which are antisymmetric ? R3 , R4 ,and R6 are symmetric because in each case (b,a) belongs to the relation whenever (a,b) does.
  • 24.
    24 Properties of Relations Example Considerthe following relations on the set of integers R1={(a,b) | a  b} ab and ba imply that a=b R2={(a,b) | a  b} R3={(a,b) | a=b or a=-b} R4={(a,b) | a=b} R5={(a,b) | a=b+1} R6={(a,b) | a+b  3} R1 , R2 , R4 , R5 are antisymmetric R2 is antisymmetric it is impossible for a>b and b>a R5 is antisymmetric it is impossible for a=b+1 and b=a+1
  • 25.
    25 Properties of Relations Example Isthe “divides” relation on the set of positive integers symmetric? Is it antisymmetric ? This relation is not symmetric because 1|2, but 2|1. It is antisymmetric because a|b, and b|a then a=b.
  • 26.
    26 Properties of Relations Arelation R on a set A is called symmetric if and only if for every edge between distinct vertices in its directed graph there is an edge in the opposite direction. R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} 1 2 3 4 Not symmetric
  • 27.
    27 Properties of Relations Arelation R on a set A is called antisymmetric if and only if there are never two edges in the opposite direction between distinct vertices in its directed graph 1 2 3 4 Antisymmetric Not reflexive Not symmetric
  • 28.
    28 Properties of Relations Arelation R on a set A is called symmetric if and only if mij=mji of MR for i=1,2,.,n j=1,2,.,n R={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} 1 1 1 1 0 1 1 1 0 0 1 1 0 0 0 1 MR= 1 2 3 4 1 2 3 4 (a,b) Antisymmetric
  • 29.
    29 Properties of Relations Arelation R on a set A is called symmetric if and only if mij=mji of MR for i=1,2,.,n j=1,2,.,n R={(1,1),(1,2),(1,3),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} 1 1 1 0 0 1 1 1 0 0 1 1 0 0 0 1 MR= 1 2 3 4 1 2 3 4 (a,b) Antisymmetric
  • 30.
    30 Properties of Relations Supposethat the relation R on a set A is represented by the matrix 1 1 0 1 1 1 0 1 1 MR= A relation R is reflexive iff (ai,ai)R this means that mii=1 for i=1,2,.,n A relation R is symmetric if (a,b)R (b,a) ↔ R this means that mij=mji for i=1,2,.,n 1 1 0 1 1 1 0 1 1 MR=
  • 31.
    31 Properties of Relations Supposethat the relation R on a set A is represented by the matrix 1 1 0 0 1 1 0 1 0 MR= This relation is reflexive symmetric antisymmetric This relation is reflexive symmetric antisymmetric 0 0 0 1 1 1 1 0 1 MR=
  • 32.
    32 Properties of Relations Supposethat the relation R on a set A is represented by the matrix 1 1 0 1 1 1 0 1 0 MR= This relation is reflexive symmetric antisymmetric This relation is reflexive symmetric antisymmetric 1 1 0 1 1 0 0 0 1 MR=
  • 33.
    33 Properties of Relations LetR be the relation consisting of all pairs (x,y) of students at your school, where x has taken more credits than y. Suppose that x is related to y and y related to z. This means that x has taken more credits than y and y has taken more credits than z We can conclude that x has taken more credits than z, so that x is related to z. The relation R has the transitive property.
  • 34.
    34 Properties of Relations Arelation R on a set A is called transitive if whenever (a,b)R and (b,c)R then (a,c)R , for all a, b, c  A abc(( (a,b)R  (b,c)R) → (a,c)R)
  • 35.
    35 Properties of Relations Considerthe following relations on the {1,2,3,4} R1 ={(1,1),(1,2),(2,1),(2,2),(3,4),(4,1),(4,4)} R2 ={(1,1),(1,2),(2,1)} R3 ={(1,1),(1,2),(1,4),(2,1),(2,2),(3,3),(4,1),(4,4)} R4 ={(2,1),(3,1),(3,2),(4,1),(4,2),((4,3)} R5 ={(1,1),(1,2),(1,3),(1,4),(2,2),(2,3),(2,4),(3,3), (3,4),(4,4)} R6 ={(3,4)} Which of these relations are transitive ?  The relation is transitive If (a,b) and (b,c) belong to the relation then (a,c) also does. R4 (3,2),(2,1),(3,1) (4,2) (2,1),(4,1)
  • 36.
    36 Properties of Relations Considerthe following relations on the set of integers R1={(a,b) | a  b} R2={(a,b) | a  b} R3={(a,b) | a=b or a=-b} R4={(a,b) | a=b} R5={(a,b) | a=b+1} R6={(a,b) | a+b  3} Which of these relations are transitive ?  The relation is transitive If (a,b) and (b,c) belong to the relation then (a,c) also does.
  • 37.
    37 Properties of Relations Isthe “divides” relation on the set of positive integers transitive? Suppose that a divides b and b divides c. Then there are positive integers k and l such that b=ak and c=bl. Hence, c=a(kl), so a divides c. It follows that the relation is transitive
  • 38.
    38 Properties of Relations Arelation is transitive if and only if whenever there is an edge from a vertex x to a vertex y and an edge from a vertex y to a vertex z, there is an edge from a vertex x to a vertex z completing a triangle where each side is a directed edge with the correct direction. 1 2 3 4
  • 39.
    39 Combining Relations Let A={1,2,3}and B={1,2,3,4} The relation R1={(1,1),(2,2),(3,3)} R2={(1,1),(1,2),(1,3),(1,4)} R1  R2 = {(1,1),(1,2),(1,3),(1,4),(2,2),(3,3)} R1  R2 = {(1,1)} R1 - R2 = {(2,2),(3,3)} R2 - R1 = {(1,2),(1,3),(1,4)} R1  R2 = R2  R1 = R1  R2 - R1  R2 = {(1,2),(1,3),(1,4),(2,2),(3,3)}
  • 40.
    40 Combining Relations Let A={1,2,3}and B={1,2,3,4} The relation R1={(1,1),(2,2),(3,3)} R2={(1,1),(1,2),(1,3),(1,4)} Construct MR1 and MR2 R1  R2 = MR1R2 = MR1  MR2 R1  R2 = MR1R2 = MR1  MR2
  • 41.
    41 Compositions of Relations LetR be a relation from a set A to a set B and S a relation from B to a set C. The composite of R and S is the relation consisting of ordered pairs (a,c), where aA , cC, and for which there exists an element bB such that (a,b)R and (b,c)S. we denote the composite of R and S by SR Example R is the relation from {1,2,3} to {1,2,3,4} S is the relation from {1,2,3,4} to {0,1,2} R = {(1,1),(1,4),(2,3),(3,1),(3,4)} S = {(1,0),(2,0),(3,1),(3,2),(4,1)} SR={(1,0),(1,1),(2,1),(2,2),(3,0),(3,1)}} T/F
  • 42.
    42 Compositions of Relations Tofind the matrix representing the relation SR (composite of R and S) Construct MR and Ms Then calculate the Boolean product (⊙) of the matrix MR and Ms MSR= MR ⊙ Ms
  • 43.
    43 Compositions of Relations •The nth power Rn of a relation R on a set A can be defined recursively by: R1 = R Rn+1 = Rn R for all n>0. R2 = RR , R3 = R2 R = (RR)R Example R = {(1,1),(2,1),(3,2),(4,3)}, find the powers Rn ,n=2,3,4,…. R2 = RR= {(1,1),(2,1),(3,1),(4,2)} R3 = R2 R= {(1,1),(2,1),(3,1),(4,1)} R4 = R3 R= {(1,1),(2,1),(3,1),(4,1)}= R3 Rn = R3
  • 44.
    44 Compositions of Relations LetR be a relation from a set A to a set B,  The inverse relation (R-1 ) from B to A is the set of ordered pairs { (b,a) | (a,b)  R }  The complement relation R is the set of ordered pairs { (a,b) | (a,b)  R }
  • 45.
    45 Closures of Relations Considerrelation R={(1,2),(2,2),(3,3)} on the set A = {1,2,3,4}. Is R reflexive? No What can we add to R to make it reflexive? (1,1), (4,4) R’ = R U {(1,1),(4,4)} is called the reflexive closure of R.
  • 46.
    46 Closures of Relations Ingeneral Let R be a relation on a set A R may or may not have some property P such as: Reflexivity – Symmetry – Transitivity The closure of relation R on set A with respect to property P is the relation R’ with  R  R’  R’ has property P R’ is called the closure of R with respect to P
  • 47.
    47 Closures of Relations LetR be the relation on {1, 2, 3, 4} such that R = {(1, 1), (1, 4), (2, 3), (3, 1), (3, 3), (4, 4)}. Find: (a) the reflexive closure of R. (b) the symmetric closure of R. (c) the transitive closure of R. (a) {(1,1), (1,4), (2,2), (2,3), (3,1), (3,3), (4,4)}. (b) {(1,1), (1,3), (1,4), (2,3), (3,1), (3,2), (3,3), (4,1), (4,4)}. (c) {(1,1), (1,4), (2,1), (2,3), (2,4), (3,1), (3,3), (3,4), (4,4)}.
  • 48.
    48 Equivalence Relations A relationon a set A is called equivalence relation if it is reflexive, symmetric, and transitive. Two elements a and b that related by an equivalence relation are called equivalent. a ~ b Is R is equivalence relation? R={(a,b) | a=b or a=-b} r,s, and t
  • 49.
    49 Equivalence Relations Congruence Modulom Let m be a positive integer m>1 . Show that the following relation is an equivalence relation on the set of integers. R={ (a,b) | ab(mod m) } Note that ab(mod m) Means m divides a-b  a-a=0 and is divisible by m ( R is reflexive )  ab(mod m) then a-b=km where k is an integer It follows that b-a=(-k)m means ba(mod m) ( R is symmetric )  suppose that ab(mod m) and bc(mod m) a-b=km and b-c=lm add both equations we get: a-b+ b-c= km+ lm=(k+l)m a-c=(k+l)m I.e ac(mod m) ( R is transitive ) R is equivalence relation
  • 50.
    50 Equivalence Classes Let Rbe an equivalence relation on S. The set of all elements that are related to an element a of S is called equivalence class of a. the equivalence class of a with respect to R is denoted by [a]R . a  S, [a]R, is [a]R = {s|(a,s)  R} or [a]R = {s: aRs} If b  [a]R b is called a representative of this equivalence relation  Any element of a class can be used as a representative of this class.
  • 51.
    51 Equivalence Classes Example What isequivalence class of an integer for the following equivalence relation? R={(a,b) | a=b or a=-b} In this equivalence relation the integer is related to itself and its negative, so : [a]R={-a,a} or [a] ={-a,a} [7] ={-7,7} [5] ={-5,5} [0] ={0}
  • 52.
    52 Equivalence Classes Example What isequivalence class of 0 and 1 for the Congruence Modulo 4? The equivalence class of 0 contains all integers a such that a0(mod 4) [0] ={…………,-8,-4,0,4,8,……………….} The equivalence class of 1 contains all integers a such that a1(mod 4) [1] ={…………,-7,-3,1,5,9,……………….} Congruence classes modulo m [a]m={……………,a-2m,a-m,a,a+m,a+2m,……………..}
  • 53.
    53 Equivalence Classes Example Let nbe a positive integer and S a set of strings. Rn is the relation on S such that sRnt iff s=t or both s and t have at least n characters and the first n characters of s and t are the same. sR3t 01 R3 01 00111 R3 00101 01 R3 11 00111 R3 01101 What is equivalence class of the string 0111 with respect to the R3? [011]R3 ={ 011, 0110,0111,01100,01101,01110, 01111,………}
  • 54.
    54 Equivalence Classes andPartitions Let n be a positive integer and S a set of strings. Rn is the relation on S such that sRnt iff s=t or both s and t have at least n characters and the first n characters of s and t are the same. sR3t [ ]R3 ={ } [0]R3 ={0} [1]R3 ={1} [00]R3 ={00} [01]R3 ={01} [10]R3 ={10} [11]R3 ={11}
  • 55.
    55 Equivalence Classes andPartitions [000]R3 ={000,0000,0001,00000,00001,00011,………} [001]R3 ={001,0010,0011,00100,00101,00111,………} [010]R3 ={010,0100,0101,01000,01001,01011,………} [011]R3 ={011,0110,0111,01100,01101,01111,………} [100]R3 ={100,1000,1001,10000,10001,10011,………} [101]R3 ={101,1010,1011,10100,10101,10111,………} [110]R3 ={110,1100,1101,11000,11001,11011,………} [111]R3 ={111, 1110,1111,11100,11101,11111,………} These 15 equivalence classes are disjoint and every bit string is in exactly one of them. These equivalence classes partition the set of all bit strings.
  • 56.
    56 Partial Orderings Let Rbe a relation on a set S, then R is a Partially Ordered Set (POSet) if it is  Reflexive - aRa, a  Transitive - aRb  bRc  aRc, a,b,c  Antisymmetric - aRb  bRa  a=b, a,b and denoted by (R,S) R={(a,b) | a  b}  a  a Reflexive  a  b and b  a implies a=b Antisymmetric  a  b and b  c implies a  c Transitive  is is a partial ordering on Z, and (Z,) is poset
  • 57.
    57 Partial Orderings Example (Z+ , |), the relation “divides” on positive integers. Reflexive? a|a since a=1a (k=1) Antisymmetric? a|b means b=ak, b|a means a=bj. But b = bjk this means jk=1. jk=1 means j=k=1, and we have b=a1, or b=a Transitive? a|b means b=ak, b|c means c=bj. c = bj = akj =am where m=kj then a|c | is is a partial ordering on Z+ , and (Z+ ,|) is poset
  • 58.
    58 Partial Orderings Example Show thatthe inclusion relation  is a partial ordering on the power set of a set S? Reflexive? A  A Antisymmetric? A  B and B  A then A=B Transitive? A  B and B  C then A  C  is is a partial ordering on P(s), and (P(s),  ) is poset
  • 59.
    59 Partial Orderings Different symbolssuch , , and | are used for a partial ordering. The general symbol ≼ is used for a partial ordering. a b ≼ means (a,b)  R in an arbitrary poset (S,R).  The elements a and b of a poset (S, ) ≼ are called comparable if either a b ≼ or b a ≼ .  when a and b are elements of S such that neither a b ≼ nor b a ≼ , a and b are called incomparable.
  • 60.
    60 Partial Orderings Example In theposet (Z+ ,|) , are the integers 3 and 9 comparable? are the integers 5 and 7 comparable? 3|9 comparable 5|7 7|5 incomparable  The adjective “partial” is used to describe partial orderings because pairs of elements may be incomparable.  When every two elements in the set are comparable, the relation is called total ordering.
  • 61.
    61 Partial Orderings If (S,) ≼ is a poset and every two elements of S are comparable, S is called a totally ordered or linear ordered set (chain). And is called a total order or ≼ a linear order. Examples  The poset (Z,) is totally ordered ab or ba.  The poset (Z+ ,|) is not totally ordered ex. 5,7
  • 62.
    62 Hasse Diagrams Hasse diagramsare a special kind of graphs used to describe posets. Ex. In poset ({1,2,3,4}, ), we can draw the following directed graph, or digraph to describe the relation. 1 2 3 4
  • 63.
    63 Hasse Diagrams Hasse diagramsare a special kind of graphs used to describe posets. 1. Draw edge (a,b) if a b 2. Don’t draw self loops 3. Don’t draw transitive edges 4. Don’t draw up arrows 1 2 3 4
  • 64.
    64 Hasse Diagrams Hasse diagramsare a special kind of graphs used to describe posets. 1. Draw edge (a,b) if a b 2. Don’t draw self loops 3. Don’t draw transitive edges 4. Don’t draw up arrows 1 2 3 4
  • 65.
    65 Hasse Diagrams Hasse diagramsare a special kind of graphs used to describe posets. 1. Draw edge (a,b) if a b 2. Don’t draw self loops 3. Don’t draw transitive edges 4. Don’t draw up arrows 1 2 3 4 The poset (Z,) is totally ordered (chain) ab or ba.
  • 66.
    66 Hasse Diagrams Í isis a partial ordering on P(s), and (P(s),  ) is poset The hasse digram of (P({a,b,c}),  ) {a,b,c} or 111 {a,b} or 110 {a,c} or 101 {b,c} or 011 {a} or 100 {b} or 010 {c} or 001 {} or 000
  • 67.
    67 Hasse Diagrams Maximal andMinimal Elements o An element in the poset is called maximal if it is not less than any elements of the poset. o An element in the poset is called minimal if it is not greater than any elements of the poset. Reds are maximal. whites are minimal.
  • 68.
    68 Hasse Diagrams Which elementsof the poset ({2,4,5,10,12,20,25),|) Are maximal, and which are minimal? Maximal elements are 12,20,25 minimal elements are 2,5 2 4 12 5 10 20 25 Note that: 25 is the greatest element and 2 is the least element.
  • 69.
    69 Hasse Diagrams Which elementsof the poset ( {1,2,3,4}, ), Are maximal, and which are minimal? Maximal element is 4 minimal element is 1 1 2 3 4 Note that: 4 is the greatest element and 1 is the least element.
  • 70.
    70 N-ary Relations andTheir Applications The relationships among elements from more than two sets are called n-ary relations. Let A1, A2, …., An be sets, an n-ary relations on these sets is a subset of A1xA2x…..xAn. The sets A1, A2, …., An are called the domains of the relation, and n is called its degree. Example Let R be the relation on NxNxN consisting of triples (a,b,c), where a, b, and c are integers with a<b<c. (1,2,3)R (2,4,3)R The degree of this relation is 3 Its domains are equal to the sets of natural numbers.
  • 71.
    71 N-ary Relations andTheir Applications Example Let R be the relation on ZxZxZ consisting of triples (a,b,c), where a, b, and c are integers with b-a=k and c-b=k, where k (common difference) is an integer. This relation is called arithmetic progression (a,a+k,a+2k). (1,2,3) , (1,3,5) R , (2,4,3) , (2,5,9) R The degree of this relation is 3 Its domains are equal to the sets of integers.
  • 72.
    72 N-ary Relations andTheir Applications Example Let R be the relation on ZxZxZ consisting of triples (a,b,c), where a, b, and c are integers with b/a=k and c/b=k, where k (common ratio) is an integer. This relation is called geometric progression (a,ak,ak2 ). (1,3,9) , (1,4,16) R , (2,4,3) , (2,5,9) R The degree of this relation is 3 Its domains are equal to the sets of integers.
  • 73.
    73 N-ary Relations andTheir Applications Example Let R be the relation on ZxZxZ+ consisting of triples (a,b,m), where a, b, and m are integers with m1 and ab(mod m). (8,2,3) , (-1,9,5) ,(14,0,7) R , (7,2,3) , (-2,-8,5) , (11,0,6) R The degree of this relation is 3 Its first two domains are the sets all of integers. And its third domain is the set of all positive integers. Congruence Modulo m m>0 ab(mod m). Means m divides a-b
  • 74.
    74 N-ary Relations andTheir Applications Example Let R be the relation consisting of 5-tuples (A,N,S,D,T) representing airplane flights, where A is the airline, N is the flight number, S is the starting point, D is the destination, and T is the departure time. (Saudi Arabian Airlines,304,Cairo,Jeddah,15:00) R The degree of this relation is 5 Its domains are the set of all airlines, the set of flight numbers, the set of cities , the set of cites, and the set of times.
  • 75.
    75 Databases and Relations RelationalDatabases A relational database is essentially just an n-ary relation R. A database consists of records, which are n-tuples, made up of fields. These fields are the entire of the n-tuples. Relations used to represent databases are called tables. Each column of the table corresponds to an attribute of the database.
  • 76.
    76 Databases and Relations Adomain of an n-ary relation is called a primary key when the value of the n-tuple from this domain determines the n-tuple. That is,a domain is primary key when no two n-tuples in the relation have the same value from this domain. Student_name ID_number Major GPA Ahmed Ali 0612345 CS 3.88 Ashraf Sami 0412364 Physics 3.65 Waleed Tarek 0512432 Math 2.88 Tarek Morad 0723465 CS 3.65
  • 77.
    77 Databases and Relations Recordsare often added to or deleted from databases. Thus, the primary key should be chosen that remains one whenever the database is changed. The current collection of the n-tuples in a relation is called the extension of the relation. The more permanent part of a database, including the name and attributes of the database is called the intension. Selecting the primary key depends on the possible extensions of the database.
  • 78.
    78 Databases and Relations Combinationsof domains can also uniquely identify n-tuples in n-ary database. The Cartesian product of these domains is called a composite key A composite key for the database is a set of domains {Ai, Aj, …} such that R contains at most 1 n-tuple (…,ai,…,aj,…) for each composite value (ai, aj,…)Ai×Aj×… See student relation Is (Major x GPA) a composite key for the n-ary relation ? Assuming that no n-tuples are ever added
  • 79.
    79 Operations on n-aryRelations Selection Operator Let R be an n-ary relation and C a condition that elements in R may satisfy. Then the selection operator sC maps the n-ary relation R to the n-ary relation of all n-tuples from R that satisfy the condition C.
  • 80.
    80 Operations on n-aryRelations selection operator sC1 where c1 is the condition major=“CS“ The result is the two 4-tuples. (Ahmed Ali, 0612345, CS , 3.88) (Tarek Morad, 0723465, CS , 3.65) sC2 GPA >3.5 sC3 (Major=“CS“  GPA >3.5 ) Student_name ID_number Major GPA Ahmed Ali 0612345 CS 3.88 Ashraf Sami 0412364 Physics 3.65 Waleed Tarek 0512432 Math 2.88 Tarek Morad 0723465 CS 3.65
  • 81.
    81 Operations on n-aryRelations Projection Operators The projection Pi1,i2,….im where i1<i2<….im , maps the n- tuple (a1,a2,….,an) to the m-tuple (ai1,ai2,…aim), where m ≤ n The projection Pi1,i2,….im deletes n-m of the components of an n-tuple, leaving the i1th, i2th,….,imth components P1,3 is applied to the 4-tuples (2,3,0,4) ,(Tarek Morad, 0723465, CS , 3.65) (2,0) , (Tarek Morad, CS)
  • 82.
    82 Operations on n-aryRelations P1,4 is applied to the relation in the table Student_name ID_number Major GPA Ahmed Ali 0612345 CS 3.88 Ashraf Sami 0412364 Physics 3.65 Waleed Tarek 0512432 Math 2.88 Tarek Morad 0723465 CS 3.65 Student_name GPA Ahmed Ali 3.88 Ashraf Sami 3.65 Waleed Tarek 2.88 Tarek Morad 3.65 New relation is produced using projection
  • 83.
    83 Operations on n-aryRelations Join Operator • Puts two relations together to form a sort of combined relation. • If the tuple (A,B) appears in R1, and the tuple (B,C) appears in R2, then the tuple (A,B,C) appears in the join J(R1,R2). – A, B, and C here can also be sequences of elements (across multiple fields), not just single elements
  • 84.
    84 Operations on n-aryRelations Join Operator example Suppose R1 is a teaching assignment table, relating Professors to Courses. Suppose R2 is a room assignment table relating Courses to Rooms,Times. Then J(R1,R2) is like your class schedule, listing (professor,course,room,time).