Chapter Four
Chapter Four
4.0 INTRODUCTION
Sets, relations and functions are fundamental mathematical structures used throughout computer
science and software engineering. They provide the mathematical language required to describe
collections of data, relationships between objects, mappings between inputs and outputs, and
transformations performed by computational systems.
A database table, for example, can be viewed as a collection of records. Relationships between
database entities can be represented using mathematical relations, while functions can model
transformations such as:
f (x) = 2x + 1
[Link] 1/34
8/11/26, 11:01 AM Course Outline Generation
4.2 SETS
4.2.1 Meaning of a Set
A set is a well-defined collection of distinct objects.
For example:
A = {1, 2, 3, 4, 5}
1, 2, 3, 4, 5
We write:
2∈A
to mean:
2 is an element of A.
7 ∈/ A
Well-defined
A = {2, 4, 6, 8, 10}
Not well-defined
The term "beautiful" is subjective and therefore does not clearly define membership.
A = {1, 2, 3, 4, 5}
2. Set-Builder Form
A property defining the elements is provided.
3. Descriptive Form
The set is described using words.
Symbol:
∅
or:
{}
Example:
A = {x ∈ N : x < 0}
Then:
A=∅
[Link] 3/34
8/11/26, 11:01 AM Course Outline Generation
Singleton Set
A set containing exactly one element is called a singleton.
Example:
A = {7}
Finite Set
A set containing a finite number of elements.
Example:
A = {a, b, c, d}
Infinite Set
A set containing infinitely many elements.
Example:
N = {1, 2, 3, 4, …}
It is written:
∣A∣
If:
A = {2, 4, 6, 8}
then:
∣A∣ = 4
Important Point
Repeated elements are counted only once.
For:
A = {1, 2, 2, 3, 3, 3}
[Link] 4/34
8/11/26, 11:01 AM Course Outline Generation
we have:
A = {1, 2, 3}
Therefore:
∣A∣ = 3
4.3 SUBSETS
Set A is a subset of set B if every element of A is also an element of B .
We write:
A⊆B
Example
Let:
A = {1, 2}
and:
B = {1, 2, 3, 4}
Then:
A⊆B
A⊆B
and:
A=B
A⊂B
[Link] 5/34
8/11/26, 11:01 AM Course Outline Generation
P(A)
Suppose:
A = {1, 2}
The subsets are:
If:
∣A∣ = n
then:
∣P(A)∣ = 2n
22 = 4
subsets exist.
4.4.1 Union
The union of A and B contains all elements that belong to A, B , or both.
[Link] 6/34
8/11/26, 11:01 AM Course Outline Generation
Symbol:
A∪B
Example
Let:
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
Then:
A ∪ B = {1, 2, 3, 4, 5, 6}
4.4.2 Intersection
The intersection contains elements common to both sets.
Symbol:
A∩B
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
Therefore:
A ∩ B = {3, 4}
A−B
A − B = {1, 2}
while:
B − A = {5, 6}
[Link] 7/34
8/11/26, 11:01 AM Course Outline Generation
Therefore, in general:
A−B =B−A
4.4.4 Complement
Suppose U is the universal set.
It is represented as:
Ac
or:
A
Example
Let:
U = {1, 2, 3, 4, 5, 6}
and:
A = {1, 3, 5}
Then:
Ac = {2, 4, 6}
It can be written:
A△B
and:
A△B = (A − B) ∪ (B − A)
For:
A = {1, 2, 3}
B = {3, 4, 5}
[Link] 8/34
8/11/26, 11:01 AM Course Outline Generation
we obtain:
A△B = {1, 2, 4, 5}
A = {1, 2, 3, 4, 5}
B = {4, 5, 6, 7, 8}
Find:
a) A ∪ B
{1, 2, 3, 4, 5, 6, 7, 8}
b) A ∩ B
{4, 5}
c) A − B
{1, 2, 3}
d) B −A
{6, 7, 8}
A∩B =B∩A
[Link] 9/34
8/11/26, 11:01 AM Course Outline Generation
(A ∩ B) ∩ C = A ∩ (B ∩ C)
A ∩ (B ∪ C) = (A ∩ B) ∪ (A ∩ C)
and:
A ∪ (B ∩ C) = (A ∪ B) ∩ (A ∪ C)
(A ∪ B)c = Ac ∩ B c
(A ∩ B)c = Ac ∪ B c
∣A ∪ B∣ = ∣A∣ + ∣B∣ − ∣A ∩ B∣
The intersection is subtracted because its elements are initially counted twice.
Worked Example 2
Suppose a class has:
40 students studying Java;
30 studying Python;
15 studying both.
How many students study at least one of the two languages?
Let:
∣J∣ = 40
∣P ∣ = 30
∣J ∩ P ∣ = 15
[Link] 10/34
8/11/26, 11:01 AM Course Outline Generation
Then:
∣J ∪ P ∣ = 40 + 30 − 15
= 55
Therefore:
55 students
It is written:
A×B
Example
Let:
A = {1, 2}
B = {a, b}
Then:
(1, a) = (a, 1)
because ordered pairs consider position.
∣A∣ = m
and:
∣B∣ = n
then:
[Link] 11/34
8/11/26, 11:01 AM Course Outline Generation
∣A × B∣ = mn
∣A∣ = 2, ∣B∣ = 2
Therefore:
∣A × B∣ = 2(2) = 4
U sers = {U1 , U2 , U3 }
and:
Resources = {R1 , R2 }
Then:
U sers × Resources
Access-control relationships
User permissions
Database relationships
Network connections
Assignment problems
For example:
(U1 , R1 )
may mean:
4.10 RELATIONS
4.10.1 Meaning of a Relation
A relation describes a relationship between elements of sets.
[Link] 12/34
8/11/26, 11:01 AM Course Outline Generation
A×B
Therefore:
R⊆A×B
A = {1, 2, 3}
and:
B = {2, 4, 6}
Define:
R⊆A×B
y = 2x
John Mathematics
Mary Programming
Peter Databases
Student → Course
A = {1, 2, 3}
and:
1 1 1 0
2 0 0 1
3 0 0 1
[Link] 14/34
8/11/26, 11:01 AM Course Outline Generation
∀a ∈ A, (a, a) ∈ R
Example
The equality relation:
R = {(a, a) : a ∈ A}
is reflexive.
aRb ⇒ bRa
Example
Is a classmate of
If:
then:
aRb ∧ bRa ⇒ a = b
Example
The relation:
[Link] 15/34
8/11/26, 11:01 AM Course Outline Generation
≤
is antisymmetric.
If:
a≤b
and:
b≤a
then:
a=b
Example
The relation ≤ is transitive.
If:
a≤b
and:
b≤c
then:
a≤c
Example
Consider the relation:
[Link] 16/34
8/11/26, 11:01 AM Course Outline Generation
It is:
Reflexive: everyone has the same birthday as themselves.
Symmetric: if A has the same birthday as B, B has the same birthday as A.
Transitive: if A has the same birthday as B and B has the same birthday as C, A has the same
birthday as C.
Therefore, it is an equivalence relation.
⊆
on a collection of sets.
For example:
{1} ⊆ {1, 2}
and:
{1, 2} ⊆ {1, 2, 3}
therefore:
{1} ⊆ {1, 2, 3}
4.15 FUNCTIONS
4.15.1 Meaning of a Function
A function is a special type of relation in which every input has exactly one output.
f :A→B
f :A→B
where:
A = domain
B = codomain
f (A) = range or image
Example
f (x) = 2x
If:
A = {1, 2, 3}
then:
f (1) = 2
f (2) = 4
f (3) = 6
Therefore:
Range = {2, 4, 6}
Function
Not a Function
a, b
[Link] 18/34
8/11/26, 11:01 AM Course Outline Generation
Formally:
f (a) = f (b) ⇒ a = b
Example
f (x) = 2x
is injective over the real numbers.
If:
2a = 2b
then:
a=b
Example
Suppose:
A = {1, 2, 3}
B = {a, b, c}
and:
Therefore, f is surjective.
A = {1, 2, 3}
B = {a, b, c}
and:
Is it a function?
Yes. Every input has exactly one output.
Is it injective?
Yes. No two inputs have the same output.
Is it surjective?
Yes. Every element of B is used.
Therefore:
f is bijective
[Link] 20/34
8/11/26, 11:01 AM Course Outline Generation
f :A→B
and:
g:B→C
then their composition is:
g ∘f
and:
f (x) = 2x + 1
and:
g(x) = x2
Find:
(g ∘ f )(x)
Step 1
f (x) = 2x + 1
Step 2
Substitute into g :
Therefore:
Expanding:
= 4x2 + 4x + 1
[Link] 21/34
8/11/26, 11:01 AM Course Outline Generation
f −1 : B → A
Example
Let:
f (x) = 2x + 3
Set:
y = 2x + 3
Solve for x:
y − 3 = 2x
y−3
x=
2
Therefore:
x−3
f −1 (x) =
2
For example:
Students
may be a set of student records.
Students × Courses
U sers = {U1 , U2 , U3 }
and:
Resources = {R1 , R2 , R3 }
Then a relation:
f : Input → Output
For example:
calculateArea(radius)
f (r) = πr2
Each valid radius produces one corresponding area.
4.20.5 Algorithms
Functions can represent transformations performed by algorithms.
For example:
f (x) = x2
[Link] 23/34
8/11/26, 11:01 AM Course Outline Generation
Input → Output
This means:
John registered for Programming.
John registered for Databases.
Mary registered for Networks.
Peter registered for Programming.
Notice that this is a relation, not necessarily a function, because one student can register for
multiple courses.
For example:
(John, P rogramming)
and:
(John, Databases)
both exist.
[Link] 24/34
8/11/26, 11:01 AM Course Outline Generation
A = {1, 2, 3, 4}
B = {3, 4, 5, 6}
Find:
1. A∪B
2. A ∩ B
3. A − B
4. B − A
Answers
A ∪ B = {1, 2, 3, 4, 5, 6}
A ∩ B = {3, 4}
A − B = {1, 2}
B − A = {5, 6}
Exercise 2
Let:
A = {a, b, c}
Find:
P(A)
Answer
P(A) = {∅, {a}, {b}, {c}, {a, b}, {a, c}, {b, c}, {a, b, c}}
There are:
23 = 8
subsets.
Exercise 3
Let:
A = {1, 2}
B = {x, y, z}
[Link] 25/34
8/11/26, 11:01 AM Course Outline Generation
Find:
A×B
Answer
{(1, x), (1, y), (1, z), (2, x), (2, y), (2, z)}
Exercise 4: Relations
Let:
A = {1, 2, 3}
and:
R = {(1, 1), (2, 2), (3, 3), (1, 2), (2, 1)}
Exercise 5: Functions
Determine whether each relation is a function.
a)
b)
Answers
a) Function.
Given:
f (x) = x + 2
and:
g(x) = 3x
Find:
1. g ∘f
2. f ∘ g
Solution
(g ∘ f )(x) = g(x + 2)
= 3(x + 2)
3x + 6
For:
(f ∘ g)(x) = f (3x)
= 3x + 2
Therefore:
f ∘ g = 3x + 2
Notice that:
g ∘f = f ∘g
in general.
Students
Courses
Lecturers
Then create relations representing:
Student-course registration
[Link] 27/34
8/11/26, 11:01 AM Course Outline Generation
Lecturer-course teaching
Student-examination participation
Students should identify whether each relationship is a function or a general relation and explain
why.
U sers = {U1 , U2 , U3 , U4 }
and:
Resources = {R1 , R2 , R3 }
Create a relation:
Students should:
[Link] 28/34
8/11/26, 11:01 AM Course Outline Generation
A = {1, 2, 3, 4, 5, 6}
B = {4, 5, 6, 7, 8}
Find:
1. A∪B
2. A∩B
3. A−B
4. B−A
5. A△B
a)
A = {1, 2}
b)
B = {a, b, c}
c)
C = {x, y, z, w}
Also determine the number of subsets using:
2n
A = {1, 2, 3}
B = {a, b}
Tasks
1. Find A × B .
[Link] 29/34
8/11/26, 11:01 AM Course Outline Generation
2. Find B
× A.
3. Compare A × B and B × A.
4. Determine their cardinalities.
5. Explain why ordered pairs are important in computing.
Assignment 5: Relations
Let:
A = {1, 2, 3}
and:
R = {(1, 1), (2, 2), (3, 3), (1, 2), (2, 1)}
Assignment 6: Functions
Consider:
f (x) = 3x + 2
Tasks
1. Find f (0).
2. Find f (1).
3. Find f (5).
4. Find f (−2).
5. Determine whether f is injective over the real numbers.
6. Determine whether f has an inverse.
7. Find f −1 (x).
[Link] 30/34
8/11/26, 11:01 AM Course Outline Generation
Students = {S1 , S2 , S3 , S4 }
Courses = {C1 , C2 , C3 }
Tasks
1. Explain what each ordered pair means.
2. Determine the students registered for C1 .
4. Represent R as a table.
5. Represent R using a matrix.
6. Determine whether R is a function from Students to Courses.
7. Explain your answer.
Define:
U sers
Roles
P ermissions
Then:
1. Define the three sets.
2. Define a relation between users and roles.
3. Define a relation between roles and permissions.
4. Give at least five sample ordered pairs.
5. Explain whether either relation is a function.
6. Explain how the model could be implemented in a relational database.
7. Discuss how the mathematical model could support access-control software.
[Link] 32/34
8/11/26, 11:01 AM Course Outline Generation
∣A∣
Power Set
∣P(A)∣ = 2∣A∣
Union
A∪B
Intersection
A∩B
Difference
A−B
Complement
Ac
Cartesian Product
A×B
∣A × B∣ = ∣A∣∣B∣
Inclusion-Exclusion
∣A ∪ B∣ = ∣A∣ + ∣B∣ − ∣A ∩ B∣
Relation
R⊆A×B
Function
f :A→B
Function Composition
[Link] 33/34
8/11/26, 11:01 AM Course Outline Generation
Bijective Function
A set represents a collection of objects, while operations such as union, intersection, difference and
complement allow collections to be manipulated mathematically.
R⊆A×B
Relations can be represented using ordered pairs, tables, matrices and directed graphs. Important
properties include:
A relation that is reflexive, symmetric and transitive is an equivalence relation, while one that is
reflexive, antisymmetric and transitive is a partial order.
A function is a special relation where each input has exactly one output:
f :A→B
Functions can be classified as injective, surjective or bijective, and functions can be combined
through composition:
The next chapter can build on these ideas through Chapter Five: Combinatorics, Counting and
Discrete Structures, introducing permutations, combinations, counting principles, recurrence
relations and their applications to algorithmic problem solving.
[Link] 34/34