Module 2 Relations 2024 (3)
Module 2 Relations 2024 (3)
CSC 203
Department of Computer Sciences
2
Outline
What is a relation
Representing Relations
Mapping
Relation Inverse
Relations Versus Functions
Relation properties
Combining Relations
3
Introduction
5
Representing relations
We can represent
We can represent relations in a table:
relations graphically:
CS101 CS201 CS202
Alice X
Bob X X
Claire
Dan X X
2) a mapping. 2 1
-1 3
0 4
3) a graph.
7
Examples
Given the relation
{(3,2), (1,6), (-2,0)}, find the domain and range.
Domain = {3, 1, -2}
Range = {2, 6, 0}
What would this be?
10
Mapping
x -1 0 4 7
y 3 6 -1 3
-1
3
0
6
4
-1
7
You do not need to write 3 twice in the range!
11
Class Activity
What is the domain of the relation
{(2,1), (4,2), (3,3), (4,1)}
1. {2, 3, 4, 4} X
2. {1, 2, 3, 1}
3. {2, 3, 4}
4. {1, 2, 3}
5. {1, 2, 3, 4}
Answer Now
12
Class Activity
What is the range of the relation
{(2,1), (4,2), (3,3), (4,1)}
1. {2, 3, 4, 4}
2. {1, 2, 3, 1}
3. {2, 3, 4}
4. {1, 2, 3}
5. {1, 2, 3, 4}
Answer Now
13
Relation Inverse
Inverse of a Relation: For every ordered pair
(x,y) there must be a (y,x).
Write the relation and the inverse.
-1 -6
3 -4
4 2
Relation = {(-1,-6), (3,-4), (3,2), (4,2)}
Inverse = {(-6,-1), (-4,3), (2,3), (2,4)} 14
Class Activity
Write the inverse of the mapping.
4
3
-3
-1
2
1. {(4,-3),(2,-3),(3,-3),(-1,-3)}
2. {(-3,4),(-3,3),(-3,-1),(-3,2)}
3. {-3} Answer Now
4. {-1, 2, 3, 4} 15
Relations vs. functions
17
Relations on a set
A relation on the set A is a relation from A to A
◼ In other words, the domain and co-domain are the same set
R 1 2 3 4
1 X X X X
2 X X
3 X
4 X
18
More examples
Consider some relations on the set Z
Are the following ordered pairs in the relation?
◼ Symmetric
◼ Asymmetric
◼ Antisymmetric
◼ Transitive
20
Reflexivity
22
Reflexivity vs. Irreflexivity
A relation can be neither reflexive nor
irreflexive
◼ Some elements are related to themselves, others
are not
We will see an example of this later on
23
Symmetry
A relation is symmetric if, for every (a,b)R,
then (b,a)R A= {(2,3), (4.5), (3,2)}
B= {(5,6),(2,3)
Examples of symmetric relations:
◼ =, isTwinOf()
24
Asymmetry
A relation is asymmetric if, for every (a,b)R,
then (b,a)R
◼ Asymmetry is the opposite of symmetry
26
Notes on *symmetric relations
27
Transitivity
A relation is transitive if, for every (a,b)R
and (b,c)R, then (a,c)R
If a = b and b = c, then a = c
◼ Thus, = is transitive
28
Transitivity examples
Consider isAncestorOf()
◼ Let Alice be Bob’s parent, and Bob be Claire’s parent
◼ Thus, Alice is an ancestor of Bob, and Bob is an ancestor of
Claire
◼ Thus, Alice is an ancestor of Claire
◼ Thus, isAncestorOf() is a transitive relation
Consider isParentOf()
◼ Let Alice be Bob’s parent, and Bob be Claire’s parent
◼ Thus, Alice is a parent of Bob, and Bob is a parent of Claire
◼ However, Alice is not a parent of Claire
◼ Thus, isParentOf() is not a transitive relation
29
Relations of relations summary
= < > ≤ ≥
Reflexive X X X
Irreflexive X X
Symmetric X
Asymmetric X X
Antisymmetric X X X
Transitive X X X X X
30
Combining relations
31
Combining relations via Boolean operators
Consider two relations R≥ and R≤
We can combine them as follows:
◼ R≥ U R≤ = all numbers ≥ OR ≤
◼ That’s all the numbers
◼ R≥ ∩ R≤ = all numbers ≥ AND ≤
◼ That’s all numbers equal to
◼ R≥ R≤ = all numbers ≥ or ≤, but not both
◼ That’s all numbers not equal to
◼ R≥ - R≤ = all numbers ≥ that are not also ≤
◼ That’s all numbers strictly greater than
◼ R≤ - R≥ = all numbers ≤ that are not also ≥
◼ That’s all numbers strictly less than
Note that it’s possible the result is the empty set
32
Combining relations via relational composition
33
Combining relations via relational composition
Let M be the relation “is mother of”
Let F be the relation “is father of”
What is M ◦ F?
◼ If (a,b) F, then a is the father of b
◼ If (b,c) M, then b is the mother of c
◼ Thus, M ◦ F denotes the relation “maternal grandfather”
What is F ◦ M?
◼ If (a,b) M, then a is the mother of b
◼ If (b,c) F, then b is the father of c
◼ Thus, F ◦ M denotes the relation “paternal grandmother”
What is M ◦ M?
◼ If (a,b) M, then a is the mother of b
◼ If (b,c) M, then b is the mother of c
◼ Thus, M ◦ M denotes the relation “maternal grandmother”
Note that M and F are not transitive relations!!!
34
Combining relations via relational composition
Given relation R
◼ R ◦ R can be denoted by R2
◼ R2 ◦ R = (R ◦ R) ◦ R = R3
35