Maths For Computing
Maths For Computing
Maths For
Computing
A Beginner’s Guide
Undergraduate Topics in Computer
Science
Series Editor
Ian Mackie, University of Sussex, Brighton, France
Advisory Editors
Samson Abramsky , Department of Computer Science, University of Oxford, Oxford,
UK
Chris Hankin , Department of Computing, Imperial College London, London, UK
Mike Hinchey , Lero—The Irish Software Research Centre, University of Limerick,
Limerick, Ireland
Dexter C. Kozen, Department of Computer Science, Cornell University, Ithaca, USA
Hanne Riis Nielson , Department of Applied Mathematics and Computer Science,
Technical University of Denmark, Kongens Lyngby, Denmark
Steven S. Skiena, Department of Computer Science, Stony Brook University, Stony
Brook, USA
Iain Stewart , Department of Computer Science, Durham University, Durham, UK
Joseph Migga Kizza, Engineering and Computer Science, University of Tennessee at
Chattanooga, Chattanooga, USA
Roy Crole, School of Computing and Mathematics Sciences, University of Leicester,
Leicester, UK
Elizabeth Scott, Department of Computer Science, Royal Holloway University of
London, Egham, UK
‘Undergraduate Topics in Computer Science’ (UTiCS) delivers high-quality
instructional content for undergraduates studying in all areas of computing and
information science. From core foundational and theoretical material to final-year
topics and applications, UTiCS books take a fresh, concise, and modern approach
and are ideal for self-study or for a one- or two-semester course. The texts are
authored by established experts in their fields, reviewed by an international advi-
sory board, and contain numerous examples and problems, many of which include
fully worked solutions.
The UTiCS concept centers on high-quality, ideally and generally quite concise
books in softback format. For advanced undergraduate textbooks that are likely
to be longer and more expository, Springer continues to offer the highly regarded
Texts in Computer Science series, to which we refer potential authors.
Quentin Charatan · Aaron Kans
© The Editor(s) (if applicable) and The Author(s), under exclusive license to Springer
Nature Switzerland AG 2025
This work is subject to copyright. All rights are solely and exclusively licensed by the Publisher, whether
the whole or part of the material is concerned, specifically the rights of translation, reprinting, reuse
of illustrations, recitation, broadcasting, reproduction on microfilms or in any other physical way, and
transmission or information storage and retrieval, electronic adaptation, computer software, or by similar
or dissimilar methodology now known or hereafter developed.
The use of general descriptive names, registered names, trademarks, service marks, etc. in this publication
does not imply, even in the absence of a specific statement, that such names are exempt from the relevant
protective laws and regulations and therefore free for general use.
The publisher, the authors and the editors are safe to assume that the advice and information in this book
are believed to be true and accurate at the date of publication. Neither the publisher nor the authors or
the editors give a warranty, expressed or implied, with respect to the material contained herein or for any
errors or omissions that may have been made. The publisher remains neutral with regard to jurisdictional
claims in published maps and institutional affiliations.
This Springer imprint is published by the registered company Springer Nature Switzerland AG
The registered company address is: Gewerbestrasse 11, 6330 Cham, Switzerland
To my sister Madhu
—Aaron Kans
Preface
vii
viii Preface
of East London for their thoughtful comments and feedback. For support and
inspiration, special thanks are due once again to our families and friends.
1 Set Theory . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
1.2 What is a Set? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.3 Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2
1.4 Ordering in a Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.5 Repetition in a Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.6 Number Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.7 Specifying a Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 3
1.8 The Universal Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 4
1.9 The Empty Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.10 Cardinality . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.11 Finite Sets and Infinite Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.12 Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.13 Proper Subsets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
1.14 Set Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
1.14.1 Union . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.14.2 Intersection . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
1.14.3 Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.14.4 Complement . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
1.14.5 Cartesian Product . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
1.15 Venn Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
1.16 Symmetric Difference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
1.17 De Morgan’s Law . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.18 Classes of Sets and Power Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
1.19 Russell’s Paradox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
1.20 Application to Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
1.21 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
2 Sets and Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
2.2 The Algebra of Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
2.3 Number Types . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.3.1 Natural Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
ix
x Contents
2.3.2 Integers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.3.3 Rational Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
2.3.4 Real Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.3.5 Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.4 Number Types and Programming . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
2.5 Working with Complex Numbers . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
2.6 Number Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.7 Discrete Versus Continuous Values . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.8 Countable and Non-countable Sets . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
2.9 Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.9.1 Commutative Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
2.9.2 Associative Operations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
2.10 Groups . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 30
2.11 Notation and Terminology . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 32
2.12 Relevance to Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
2.13 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 35
3 Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.2 Relations: Definition and Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . 37
3.3 Inverse Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 40
3.4 Relations on a Set . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.5 Digraphs of Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.6 Symmetric Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
3.7 Reflexive Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 42
3.8 Transitive Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.9 Equivalence Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
3.10 Equivalence Classes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 44
3.11 Binary Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.12 n-Ary Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 45
3.13 Relevance to Computing: Relational Databases . . . . . . . . . . . . . . . 46
3.14 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 47
4 Functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.2 Definition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 49
4.3 Functions as Mappings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 50
4.3.1 Practical Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 51
4.3.2 Using the Password Function . . . . . . . . . . . . . . . . . . . . . . . 52
4.4 A Function as an Input/Output Device . . . . . . . . . . . . . . . . . . . . . . . 52
4.5 Functions as Formulae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
4.5.1 Applying our Function . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 52
Contents xi
8 Combinatorics . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
8.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 117
8.2 Placing Items in Order . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
8.3 Factorials . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 118
8.4 Choosing just Some of the Items . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 119
8.5 The Formula for Permutations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
8.6 Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
8.7 The Formula for Combinations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120
8.8 What Is the Value of 0!? . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
8.9 Allowing Repetition (When Order Is Important) . . . . . . . . . . . . . . 121
8.10 Allowing Repetition (When Order Is Not Important) . . . . . . . . . 121
8.11 Summary of Formulae . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 122
8.12 Deriving the Formula for Choosing Items when Order Is
Not Important and Repetition Is Allowed . . . . . . . . . . . . . . . . . . . . . 122
8.13 Pascal’s Triangle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 126
8.14 Binomial Expansion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
8.15 Application to Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
8.16 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 129
9 Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
9.1 Introduction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 131
9.2 Terminology and Definitions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 132
9.3 Outcomes, Sample Space and Events . . . . . . . . . . . . . . . . . . . . . . . . . 132
9.4 Calculating Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 133
9.5 Probability that One or Another Event Happens . . . . . . . . . . . . . . 135
9.6 Mutually Exclusive Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
9.6.1 The Addition Rule . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 136
9.6.2 The Addition Rule for Mutually Exclusive
Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 137
9.7 Probability Distribution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138
9.7.1 Non-uniform Probability Distribution . . . . . . . . . . . . . . . 139
9.8 Independent Events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 141
9.9 Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 142
9.10 Expected Value (Mean Value) . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 143
9.11 Conditional Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
9.11.1 Tree Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 145
9.12 Bayes’ Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
9.13 Binomial Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 150
9.13.1 The Binomial Probability Formula . . . . . . . . . . . . . . . . . . 151
9.14 Application to Computing . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
9.15 Exercises . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 153
xiv Contents
Index . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 213
Set Theory
1
1.1 Introduction
In the world of computing we very often have to write applications that han-
dle collections. These could be collections of customers, bank accounts, students,
patients, network users, invoices and many many more.
In this chapter we will be studying set theory, and as we shall see, a set is
a particular type of collection; we will be exploring ways of defining sets and
performing operations on sets, and in the next chapter we will go on to study a
number of laws associated with set theory.
As we progress, you will see that an understanding of set theory is extremely
important when studying other areas of mathematics such as probability, which
you will encounter in this text.
A set is an unordered collection of distinct objects. These objects are called the
elements or members of the set. The objects can be anything—people, words,
numbers, animals or even other sets.
Examples of possible sets could be:
1.3 Notation
We often use upper-case letters to refer to sets. When we list the members of a
set we enclose them in curly brackets, separating each element of the set with a
comma.
Examples
If A is the set of all the countries in the UK, we could write:
If B is the set of even numbers greater than zero and less than ten, then:
B = {2, 4, 6, 8}
When reasoning about sets in mathematics, we often use lower case letters to
represent the elements of a set. For example, we might have:
C = {a, d , f , e, x, g}
x ∈ A means x is an element of A.
x∈/ A means x is not an element of A.
Two sets are said to be equal if and only if they contain exactly the same elements
(no more and no fewer).
1.7 Specifying a Set 3
{a, b, c, d , e} = {e, d , b, c, a}
These two sets are identical—they contain exactly the same elements. We can
list them in any order we choose.
3. By comprehension
Worked Example 1
Express the following specification of a set M in words:
M = {x ∈ R |x ≥ 100}
Solution
M is the set of real numbers greater than or equal to 100.
Worked Example 2
Using set comprehension, specify a set D that contains all the negative integers.
Solution
D = {x ∈ Z |x < 0}
• If we take the set of students on a particular course, the universal set could be
the set of people.
• If we take the set {ELEPHANT, LION, DOG}, the universal set could be the
set of animals.
• If we have a set of positive whole numbers, the universal set could be natural
numbers. But it could also be integers, or real numbers. We need to make it
clear which one it is.
Note
A set containing only one member is often referred to as a unitary set.
1.10 Cardinality
The number of elements that a set contains is called the cardinality of that set.
There are two ways of referring to the cardinality of a set A.
Either: n(A).
Or: |A|
In this text we will use the first one.
Examples
A = {a, b, c, d , e, f } n(A) = 6
B = {1, 4, 6} n(B) = 3
C=∅ n(C) = 0
D = {0} n(D) = 1
In the previous examples we were able to state the cardinality of the sets easily.
This is because when we counted the members, the counting came to an end and
we had a final result.
Such sets are called finite sets.
However there are certain sets where this is not so straightforward, because we
never finish counting. Such sets are called infinite sets.
All of the number sets that we referred to earlier are infinite sets.
6 1 Set Theory
Worked Example 3
State whether each of the following sets is finite or infinite:
Solution
1.12 Subsets
Example
Consider the following sets:
A⊂B
means A is a proper subset of B. This means that we exclude the possibility that
A could be equal to B. In other words, there is always at least one more element
in B than there is in A. If we want to include the possibility that the two sets are
equal, then we use the symbol ⊆.
So: A ⊆ B means that A is a subset of B or is equal to B.
1.14 Set Operations 7
Worked Example 4
Consider the following sets of natural numbers:
A = {1, 4, 6, 7, 9, 10}
B = {6, 7, 9, 10}
C = {3, 7, 9, 10}
D = {10, 7, 6, 9}
For each of the following, state whether the expression is true or false:
(a) B⊂A
(b) A⊂B
(c) B=D
(d) C /⊂ A
(e) D⊂B
(f) B⊆A
Solution
(a) True
(b) False
(c) True
(d) True
(e) False
(f) True
• addition
• subtraction
• multiplication
• division.
We are now going to study the following operations that we can perform on sets:
• union
• intersection
• difference
• complement
• Cartesian product.
8 1 Set Theory
1.14.1 Union
The union of two sets, A and B is the set which contains all the elements of A and
all the elements of B. We use the following notation:
A ∪ B means A union B
Example
Note
Although John and Delroy appear in both sets, they only appear once in the final
set: as we know, we do not record duplicates in a set.
1.14.2 Intersection
The intersection of two sets, A and B is the set which contains all the elements
that are common to both A and B.
We use the following notation:
A ∩ B means A intersection B
Example
Note
If two sets have no common elements, then the intersection is the empty set.
So:
if X = {a, b, d, e, g}.
and Y = {m, n}.
then X ∩ Y = ∅.
So the cardinality of the union is found be adding the cardinality of the two sets
and subtracting the cardinality of the intersection (otherwise we would be counting
the common elements twice).
For example:
if A = {a, b, d , e, g} n(A) = 5
and B = {a, b, c, f } n(B) = 4
A ∩ B = {a, b} n(A ∩ B) = 2
n(A ∪ B) = 5 + 4−2 = 7
1.14.3 Difference
The difference of two sets, A and B, is the set which contains the elements that
belong to A but do not belong to B.
We use the following notation:
Example
1.14.4 Complement
The complement of a set A is the set which contains all the elements in the
universal set except for those that belong to A.
There are several different notations for the complement of A:
A or A, or Ac
Example
If the universal set, U, is all the students at the University of East London.
10 1 Set Theory
and A = {All students who study computing at the University of East London}.
A = {All the students at the University of East London
then .
who do not study computing}
Note
In some texts you will see this described as the absolute complement, while the
difference is referred to as the relative complement.
The Cartesian product results in a set of all possible ordered pairs, made up of
one element from the first set, followed be one element from the second.
Worked Example 5
Consider the following sets:
A = {b, d , f , g, h, x, y}
B = {f , g}
C = {g, x, z}
D = {z}
Solution
(a)
(i) A ∩ C = {g, x}
(ii) B ∪ C = {f, g, x, z}
(iii) B \ C = {f }
(iv) B ∩ D = ∅
(v) B x C = {( f, g), ( f, x), ( f, z), (g, g), (g, x), (g, z)}
(b) A = {z, w}.
A Venn diagram is a way to represent sets pictorially. The universal set is repre-
sented by a rectangle, and a set is represented by a circle. Examples are shown in
Figs. 1.1, 1.2, 1.3, 1.4, 1.5 and 1.6.
The shaded area in the Venn diagram in Fig. 1.7 represents the symmetric
difference:
Worked Example 6
Consider the following sets:
A = {a, b, c, d , e, f }
B = {x, b, c, d , y, w, z}
Solution
See Fig. 1.8.
Worked Example 7
This question refers to students at a particular sixth-form college, where there is
total of 100 students.
P is the set of students who take physics, and C is the set of students who take
chemistry.
30 students take physics, and 25 take chemistry. 20 students take both.
Solution
Worked Example 8
This question refers the same college as the previous question, where there are
100 students.
P is the set of students who take physics, and C is the set of students who take
chemistry, and M is the set of students who take mathematics.
30 students take physics, and 25 take chemistry and 22 take mathematics.
20 students take both physics and chemistry. 18 students take physics and
mathematics. 15 students take chemistry and mathematics.
1.16 Symmetric Difference 15
Solution
Worked Example 9
Use Venn diagrams to prove the following:
A∩B=A∪B
+ + =
We can see that in both diagrams the shaded areas are the same.
A∩B=A∪B
A∪B=A∩B
These two identities are two versions of an important law in set theory known
as De Morgan’s Law.
In set theory we often use the word class to describe a collection of sets (or a set
of sets). For example the class C might be defined as follows:
The power set of a set A is the class of all the subsets of A (including the empty
set and A itself). This is written as:
P(A)
Example
if: A = {CAT, DOG, PARROT}.
Then:
P(A) = {∅, {CAT}, {DOG}, {PARROT}, {CAT, DOG}, {CAT, PARROT},
.
{PARROT, DOG}, {CAT, DOG, PARROT}}
Note
If a set has a cardinality of n, then the number of elements in the power set is 2n .
Worked Example 11
If A is the set {x, y}, what is the power set, P(A)?
Solution
Worked Example 12
(a) If a set has a cardinality of 5, then how many elements will be in the power
set?
(b) How many proper subsets does the above set have?
Solution
The British philosopher Bertrand Russell realised that there was a potential
problem with the notion of a “set of sets”. This is described below:
If T is the set of all the sets which are not elements of themselves, then is T itself
an element of T ?
18 1 Set Theory
Different types of collections such as sequences and sets occur very frequently
in every day situations which, as computer scientists, we need to model in our
applications. However, the importance of set theory in computing lies not only in
direct application, but also in other areas of mathematics that are central to the
understanding of computer science. We will see in future chapters how set theory
is vital to our understanding of functions, of logic and of probability all of which
are of huge importance to computer science.
1.21 Exercises
M = {x ∈ N|x ≥ 50}
2. Using set comprehension, specify a set A that contains all the integers greater
than − 5 and less than 5.
3. State whether each of the following sets is finite or infinite:
(a) The set of natural numbers between 50 and 100.
(b) The set of real numbers less than 10.
(c) The set of chemical elements discovered so far.
4. Consider the following sets:
A = {a, b, d , e, g, h, x}
B = {a, b , c, d }
C = {g, h, x, a}
D = {h, x, a, g}
For each of the following, state whether the expression is true or false:
(a) C ⊂ A (b) A ⊂ C (c) C = D (d) B /⊂ A (e) D ⊂ C
(f) C ⊆ D
5. Consider the following sets:
2.1 Introduction
In this chapter we expand on the work we did in chapter 1, and describe some
laws of set algebra which we can use to solve problems. We apply our knowledge
of sets to sets of numbers, and learn to distinguish between different types of
numbers. Finally we explore the important mathematical concept of a group.
There are a number of laws that follow directly from the definitions that we
covered in the last chapter:
It can be seen that the above pairs are duals of each other. This is a fact of
set algebra called the principle of duality.
Worked Example 1
Show that:
A ∩ (A ∪ B) = A ∩ B
Solution
A ∩ (A ∪ B)
= (A ∩ A) ∪ (A ∩ B) Distributive Law
= ∅ ∪ (A ∩ B) Complement Law
= A∩B Identity Law
Worked Example 2
Show that:
(A ∩ B) ∪ (A ∩ B) = A
Solution
(A ∩ B) ∪ (A ∩ B)
= A ∩ (B ∪ B) Distributive Law
=A ∩ U Complement Law
=A Identity Law
2.3 Number Types 23
Numbers can be categorised into five distinct types as we saw in the previous
chapter.
Natural numbers are the numbers that we use for counting. That is to say, whole
numbers from 0 to infinity.
Examples of natural numbers are:
2.3.2 Integers
Integers include the natural numbers, but also include the numbers less than zero
(negative numbers). Integers are therefore whole numbers from minus infinity to
plus infinity.
Examples of integers are:
Real numbers are numbers that have a fractional part. They include rational num-
bers but also include irrational numbers. They also include integers (and therefore
natural numbers) because integers can be expressed as, for example, 2.0, 3.0, −
5.0 and so on.
Examples of real numbers are:
Complex numbers are used in advanced mathematics and consist of a real part
and an imaginary part. The imaginary part contains a special number i, which is
defined as:
√
−1
Of course there is no real number that gives − 1 when squared, which is why
we use the term "imaginary number".
A typical complex number might look like this: 3 + 2i.
The set of complex numbers also contains real numbers (the coefficient of i is
0).
Worked Example 3
Consider the following numbers:
−4.82 33 − 262 45.987 0.3
Which of these numbers are:
(a) Real numbers (b) Integers (c) Natural numbers
Solution
Addition
We add the two parts separately.
For example
(3 + 2i) + (4−i) = 7 + i
Multiplication
We do a normal binomial multiplication.
For example:
(3 + 2i)(4−2i) = 12 − 6i + 8i−4i2
= 12 + 2i− 4i2
But
i2 = −1
Division
Consider the following example:
3+i
1 − 3i
We need to find a way to remove the imaginary part from the denominator. We
can do this by multiplying both numerator and denominator by (1 + 3i).
3 + 9i + i + 3i2
=
1 + 3i − 3i − 9i2
3 + 10i + 3i2
= Because i2 = −1
1 − 9i2
3 + 10i − 3
=
10
10i
=
10
=i
We see that: N ⊂ Z ⊂ Q ⊂ R ⊂ C.
Imagine turning on a tap just enough so that the tap drips. You can count the
individual drops of water that come out of the tap.
Now imagine you turn the tap further, so that the water comes in a continuous
stream. Now it is not possible to count anything.
Discrete values are like the drops of water. They come in separate packets, and
you can count them. Natural numbers and integers are discrete. You can count how
many integers there are between, say, 1 and 10.
Continuous values are like the stream of water coming from the tap. There
are no separate items that you can count. Real numbers are continuous. It is not
possible to say how many items there are between, say, 2.1 and 2.2.
Consider the set of positive numbers greater than or equal to 5 and less than or
equal to 10. This set would contain 6 numbers—we can count them from 5 up to
10.
2.9 Operations 27
But what if we put no upper limit on the set? We could still count the mem-
bers—the only thing is that we would never finish counting, because the number
of elements is infinite. A set like this is said to be countable and infinite.
Now consider the set of real numbers from 5 to 10. How could we count these?
If we started with 5.0, then counted 5.1, what about 5.01, 5.02 etc.? And what
about the numbers between 5.01 and 5.02? You can see we can never count real
numbers—and in fact it can be mathematically proven that if we take any two real
numbers, there is always another number in between.
The set of real numbers, R, is non-countable and infinite—whereas the set of
natural numbers, N, and the set of integers, Z, are countable and infinite.
So you can see that there are in fact two types of infinity—countable infinity
and non-countable infinity.
2.9 Operations
• Addition (+)
• Subtraction (−)
• Multiplication (× or *)
• Division (÷ or /)
Addition
x+y =y+x
For example: 2 + 3 = 3 + 2 = 5
Multiplication
x×y =y×x
For example: 2 × 3 = 3 × 2 = 6
The following arithmetic operations are not commutative:
28 2 Sets and Groups
Subtraction
x + y /= y + x
10−3 /= 3−10
Division
x ÷ y /= y ÷ x
10 ÷ 5 /= 5 ÷ 10.
When we perform an operation on more than two terms, the operation is asso-
ciative if it doesn’t matter how we group the terms. In arithmetic the following
operations are associative:
Addition
x + (y + z) = (x + y) + z
For example: 2 + (3 + 4) = (2 + 3) + 4 = 9
Multiplication
x × (y × z) = (x × y) × z
For example: 2 × (3 × 4) = (2 × 3) × 4 = 24
The following arithmetic operations are not associative:
Subtraction
x − (y − z) = (x − y) − z
Division
x ÷ (y ÷ z) = (x ÷ y) ÷ z
12 ÷ (6 ÷ 2) /= (12 ÷ 6) ÷ 2
A∪B=B∪A
Intersection
A∩B=B∩A
A\B /= B\A
Cartesian product
A × B /= B × A
(A ∪ B) ∪ C = A ∪ (B ∪ C)
Intersection
(A ∩ B) ∩ C = A ∩ (B ∩ C)
(A\B)\C /= A\(B\C)
30 2 Sets and Groups
(A × B) × C = A × (B × C)
However, we often assume that the Cartesian product of three sets just gives as
triples.
In this case it is associative as both sides would gives us:
2.10 Groups
However, a set and an operation do not constitute a group unless it satisfies the
following criteria, all of which are explained in the subsequent sections:
The set must contain an element known as the identity element. If the operation
is applied to any element and the identity, the element will be unchanged.
For example
For the set of integers and addition, the identity is 0:
5+0 = 5 0+5 = 5
7+0 = 7 0+7 = 7
−5 + 0 = −5 −5 + 0 = −5 etc.
Formally:
For a set G under the operation *:
There exists an e in G, such that a * e = a and e * a = a, for all elements a in
G.
For every element of the group, there’s another element of the group such that
when we use the operator on both of them, we get e, the identity.
For example
For the integers and addition, the inverse of 5 is − 5 (because 5 + − 5 = 0); the
inverse of − 5 is 5.
Notation
The inverse of a is written as a−1 . In the above example, a−1 = b.
Formally:
For all a in G, there exists a b in G, such that a ∗ b = e and b ∗ a = e.
For example
For the set of integers, addition is an associative operation.
Formally:
For all a, b, and c in G, a ∗ (b ∗ c) = (a ∗ b) ∗ c.
• if there are two elements in the group, a and b, and * represents the operation,
then it must be the case that a * b is also in the group.
• we say that the group is closed under the operation.
For example
With integers and addition, the group is closed because whatever the value of a
and b, a + b is always an integer.
Formally:
For all a, b in G, a * b is in G.
We see that the group integers with addition is formally a group because it has
all of the required properties.
Note
Worked Example 4
Is the set {− 1, 1} under multiplication a group?
2.11 Notation and Terminology 33
Solution
Is there closure?
It is closed because the results of all of the following are in the group:
1×1 –1 × − 1 1×− 1 − 1 × 1.
Therefore the set {− 1, 1} under multiplication is a group.
Worked Example 5
Is the set {− 1, 0, 1} under addition a group?
Solution
Is there closure?
It is not closed because the result of adding − 1 to itself is not in the group –
neither is the result of adding 1 to itself.
Worked Example 6
Is the set of integers under multiplication a group?
Solution
Worked Example 7
Is the set of rational numbers excluding zero under multiplication a group?
Solution
Is there closure?
It is closed because the result of any possible multiplication always results in
another rational number.
a b ac
× =
b d bd
Number theory and group theory are essential parts of the foundations of mathe-
matics. But for students studying computing they are essential for areas such as
algorithm design, coding theory, and encryption systems.
2.13 Exercises
A ∪ (A ∩ B)
3. In the last chapter you drew Venn diagrams to prove the following:
A\B = A ∩ B
B ∪ (A\B) = B ∪ A
3.1 Introduction
In this chapter we are going to deal with the topic of relations, which is to do
with the ways in which elements of different sets can be related to each other. This
subject forms the basis upon which other very important mathematical concepts
such as functions are derived, and also has a direct application in computing, since
it is the theoretical base that underlies the structure of relational databases.
CHEMISTRY
ADE
BIOLOGY
MEENA MATHEMATICS
T S
We need to consider which teachers teach which subjects. One way we can
represent this is by means of a diagram, as show in in Fig. 3.1.
The diagram shows a relation between the set of teachers and the set of sub-
jects. We often use R to represent a relation, and express R as a set of ordered
pairs. In our example:
JULIET R MATHEMATICS
Worked Example 1
The diagram in Fig. 3.2 represents the relation “is produced by” between the set F
(food) and the set A (animals):
Solution
F A
Worked Example 2
A and B are two sets and R is a relation from set A to set B.
A = {a, b, c, d }
B = {x, y, z}
Solution
See Fig. 3.3
A B
40 3 Relations
A B
B A
3.6 Symmetric Relations 41
DIANA DIANA
LEENA LEENA
SAM SAM
A A
When a relation is between two identical sets, we talk about a relation on a set.
For example consider the following set, A, consisting of four people.
In Chap. 10 you will study graph theory. There you will see that a graph is a
diagram that consists of a set of vertices and a set of connections - in a directed
graph, or digraph, those connections have a direction. A digraph is a useful way
of representing a relation on a set.
The digraph in Fig. 3.7 represents the relation {(a, b), (b, b), (a, c), (a, d), (b,
c), (b, d), (c, d)} on the set {a, b, c, d}.
A = {1, 2, 3, 4}
R = {(1, 2), (2, 1), (2, 3), (3, 2) (3, 4), (4, 3) }
42 3 Relations
A = {1, 2, 3, 4}
R = {(1, 1), (2, 1), (2, 2), (2, 4) (3, 3), (4, 4) }
Put another way, for every element, a, we find that a R a. We can see that our
previous example is older than is not reflexive.
For example ABDUL is not older than ABDUL, and so on.
A = {1, 2, 3, 4}
R = {(1, 2), (1, 3), (1, 4), (2, 3) (2, 4), (3, 4) }
abdul is older than diana, diana is older than LEENA, and abdul is older
than LEENA.
abdul is older than leena, leena is older than sam, and abdul is older than
sam.
diana is older than leena, leena is older than sam, and diana is older than
sam.
Worked Example 3
Consider the relation is greater than or equal to (≥) on the set of integers. Is this
an equivalence relation?
Solution
It is a reflexive relation, because for every number, a, a ≥ a.
For example 1 ≥ 1, 3 ≥ 3 and so on.
It is a transitive relation, because whenever a ≥ b and b ≥ c, then a ≥ c.
44 3 Relations
A = {1, 2, 3, 4}
R = { (1, 1), (2, 2), (3, 3), (4, 4), (1, 2), (2, 1), (2, 3), (3, 2), (1, 3), (3, 1)}
For each element of the set there is an equivalence class, defined as the set of
elements that that element is related to.
It is written with square brackets:
Notice here that we have arrived at two disjoint sets {1, 2, 3} and {4}.
Equivalence classes always partition the set. In other words, any two equiva-
lence classes are either equal or disjoint.
Formally, given a set S and an equivalence relation R on S, the equivalence
class of an element a in S is the set:
{x ∈ S | x R a}
Worked Example 4
Consider the set, A
A = {a, b, c}
R = {(a, a), (a, b), (b, a), (b, b), (c, c)}
Solution
The relations we have just seen are binary relations: they are relations between
two sets. In our first worked example we had:
In a binary relation, all the possible relations that could exist are given by the
Cartesian product of the two sets.
In this case:
Imagine that they keep goats and hens at Manor Farm and cows and hens at
City Farm. A relation that shows which animal produces which food at which farm
would look like this:
A relation like this, which relates three sets together, is called a ternary
relation. In general we can have 2-ary, 3-ary, 4-ary relations and so on.
P T
3.14 Exercises
1. The diagram in figure 3.8 represents the relation “plays for” between a set of
players, P, and a set of teams, T:
(a) Represent the relation in terms of a set of ordered pairs.
(b) Write in words: Mary R Sidcup Lions
2. A and B are two sets and R is a relation from set A to set B, where:
A = {1, 2, 3}
B = {x, y}
R = {(1, x), (2, x), (3, y)}
b c
5. Consider the digraph in Fig. 3.9 which shows a relation on the set {a, b, c, d}.
(a) Reflexive
(b) Symmetric
(c) Transitive
Functions
4
• define the term function, and determine whether a particular binary relation is
or is not a function;
• apply a function to a particular input;
• write an appropriate function signature;
• where appropriate, specify a function as a mathematical formula;
• describe how a function can effectively have multiple inputs;
• explain the term function composition.
• determine whether a function is injective, surjective or bijective.
4.1 Introduction
4.2 Definition
first set must be assigned to a member of the second set. The examples in Fig. 4.1
will make this clear:
The first set is known as the domain of the function, the second set is known
as the codomain (or range).
The element in the second set that corresponds to a particular element of the
first set is called the image of that element. In the first diagram, for example, y is
the image of a. In a function, every element of the domain must have one and only
one image in the codomain.
Worked Example 1
Which of the diagrams in Fig. 4.2 represents a function?
Solution
On a computer network we need to keep records of users of the system and their
corresponding passwords.
We would need a function that maps names of users (N) to passwords (P). This
is represented in Fig. 4.3.
robinder
mamba
aaron
olu simba
N P
52 4 Functions
Note
This is a function because more than one user can have the same password, but
each user can have only one password.
Let’s name our function f . We can now apply our function to each element of N
in order to find the user’s password. We write this as follows:
f ( peter ) = f ido
f (r obinder ) = f ido
f (aar on) = mamba
f (olu) = simba
When a function takes a number and outputs another number, it can often be
specified as a mathematical formula.
Imagine a function, f , that behaves like this for three different inputs as shown
in Fig. 4.5.
In this case, the function takes an input and adds 5 to it. If the function behaves
like this for every input, we can express our function as a formula:
f (x) = x + 5
Now that we have our function expressed like this: f (x) = x + 5, we can apply it
to any input. For example:
f (2) = 7
f (0) = 5
f (−2) = 3
4.5 Functions as Formulae 53
username password
peter fido
robinder fido
aaron mamba
olu simba
1 6
3 8
7 12
Imagine a function that always outputs the square of the input. We can write this
as:
f (x) = x 2
54 4 Functions
A B
f (2) = 4
f (3) = 9
f (−2) = 4
Worked Example 2
A function f , which maps from a set A to a set B, is represented pictorially in
Fig. 4.6.
What is the value of the following?
Solution
Worked Example 3
A function f is specified as follows:
f (x) = 2x−1
Solution
= 6−1
= 5.
Worked Example 4
A function f is specified as follows:
f (x) = 3x 2 −10
Solution
f (x) = 3x + 1
We usually give a function a name. In the password example we called our function
f.
When we specify a function, we first need to provide its signature:
This means that the function f maps from the set of Usernames to the set of
Passwords. This is sometimes read as f maps Usernames into Passwords.
When we specify a function, we need to do two things: provide its signature, and
say what it does. In general, if f is a function from set A to set B we write its
signature as:
f :A→B
f (x) = y
f (a) = 1
f (b) = 3
f (c) = 3
A B
f (3) = 9
f (4) = 16
f (−2) = 4
f (−3) = 9
The input is an integer, but the output is always a natural number (we never
produce a negative number by taking the square).
We could write our function like this:
f :Z→N
f (x) = x 2
When we specify functions which are formulae, the signature will involve num-
ber sets such as the above. Often, when working with formulae and equations, we
assume that - unless otherwise stated - we are dealing with real numbers; in other
words the function maps from R to R.
f :N→R
√
f = x
58 4 Functions
A number in the domain such as 4 will have two images in the codomain,
namely 2 and − 2.
So f is not a function.
In computer graphics, images are formed from pixels made up of three primary
colours - red, green and blue. Mixing these can produce three secondary colours -
cyan, magenta, yellow:
A function that produces secondary colours from primary colours would look
like that shown in Fig. 4.8.
The first set is actually the Cartesian product of two sets. In this case the two
sets are the same, P and P – but they could be two different sets.
If we call the above function f , then we see that the signature of f is:
f :P×P→S
PxP S
4.12 Function Composition 59
In general:
f (x, y) = z
Worked Example 5
Consider the following function:
f :R×R→R
f (x, y) = x 2 + y−1
Solution
A B
g(x) = 2x + 3 f (x) = x 2
Calculate:
f (g(2))
Solution
g(2) = 2 x 2 + 3 = 7
f (7) = 72 = 49
A B
A B
my Function : f : Z × Z → Z
my Function(x, y) = 2x + y
= AVERAGE(A1:A4).
4.17 Exercises
f :Z×Z→Z
f (x) = 4x 2 −5
a 1 a 1
b 2 b 2
c 3 c 3
d 4 d 4
c)
a 1
b 2
c 3
d 4
f :R × R→R
f (x, y) = 2x 2 + 3y
g(x) = 3x + 1 f (x) = x 3
Calculate:
f (g(3))
f :R→R
f (x) = x 2
Is this function
a)
a 1 b) a
2
b 2 b
3
c 3 c
4
d d
4
c) d)
1 1
a
a
2 b 2
b
3 c 3
c
4 d 4
5.1 Introduction
In this chapter and the next we are moving on to a new topic, mathematical
logic, which is a well-established branch of mathematics, and is essential for
the understanding of a many important topics in computer science, especially in
programming. This chapter concentrates on propositional logic, which involves
performing operations on simple statements which can be true or false. We will
study the use of truth tables that define rules for these operations, and learn some
simple laws that enable us to develop an algebra of propositions.
5.3 Propositions
A proposition is a statement that can be either true or false. We say that it has a
truth value.
Examples of propositions that have a value of true:
We can join two simple propositions together to form a compound statement, using
operators (or connectives) that try to capture the meaning of simple words like
“and” and “or”. The truth value of a compound statement will depend on the value
of each of the two simple statements.
Each connective has a set of rules that are defined in the form of a truth table.
Ʌ
5.4.1 The AND operator ( )
If P and
Ʌ Q are propositions then:
P Q means: P AND Q.
5.4 Logical Operators (Connectives) 67
The meaning of the AND operator is defined in the truth table shown in Fig. 5.1
(where T means true and F means false).
If P Ʌ
is the statement “It is Wednesday”, and Q is the statement “It is February”,
then P Q is the statement “It is Wednesday and it is February”.
The compound statement is true only if both of the simple statements are true –
otherwise it is false.
This is also known as conjunction.
Worked Example 1
If P and Q represent the following statements:
P: 102 < 50.
Q: Nigeria is in Africa.
Ʌ
What is the value of P Q?
Solution
P is false.
Q is true.
Looking at the third line of the truth table we see that F ∧ T is false.
Therefore, P ∧ Q is false.
The compound statement is true if either one of the simple statements is true – it
is false only when both statements are false.
This is also known as disjunction.
Worked Example 2
If P and Q represent the following statements:
Solution
P is false.
Q is true.
Looking at the third line of the truth table we see: F ∨ T is true.
Therefore, P ∨ Q is true.
Unlike the previous two operators, the not operator operates on a single proposi-
tion.
¬P is read as not P.
Its function is to reverse the value of the proposition.
If P is true, then ¬P is false.
If P is false then ¬P is true.
For Example:
If P represents the statement it is raining.
then ¬P represents the statement it is not raining.
The truth table for the NOT operator is shown in Fig. 5.3.
This is known as negation.
Note: you might also come across the notation ~ P and !P to mean NOT P.
Worked Example 3
Let P be “It is cold” and Q be “It is raining”. Give simple sentences which
represent the following statements:
(a) ¬P
(b) P∧Q
(c) P ∨Q
(d) Q ∨ ¬P
e) ¬P ∧ ¬Q
(f) ¬ ¬Q
Solution
Worked Example 4
Let P be “She is tall” and Q be “She is intelligent”. Express each of the following
statements symbolically:
Solution
(a) P ∧Q
(b) P ∧ ¬Q
(c) ¬(P ∨ Q)
(d) ¬P ∧ ¬Q
(e) P ∨ (¬P ∧ Q)
(f) ¬ (¬P ∨ ¬Q)
70 5 Propositional Logic
1 2 3 4 5 6
1. Enter all the possible combinations in
columns 1 to 3. P Q R PɅQ ¬R (P Ʌ Q) v ¬R
T T T T F T
2. Using the values for P and Q in columns
1 and 2, complete column 4 by using the T T F T T T
rules for AND. T F T F F F
3. Complete column 5 by negating the T F F F T T
values for R in column 3.
F T T F F F
4. Using the values in columns 4 and 5, F T F F T T
complete column 6 by using the rules for
F F T F F F
OR.
F F F F T T
Two compound statements are said to be logically equivalent if they have identical
truth tables. We use the symbol ≡ to indicate that two statements are identical.
Worked Example 6
Prove the following identity: ¬(P ∧ Q) ≡ ¬P ∨ ¬Q.
Solution
See Fig. 5.5.
The last column of each truth table is identical – so both statements are
equivalent.
¬(P ∧ Q) ≡ ¬P ∨ ¬Q
5.7 De Morgan’s Law 71
P Q PɅQ ¬(P Ʌ Q) P Q ¬P ¬Q ¬P v ¬Q
T T T F T T F F F
T F F T T F F T T
F T F T F T T F T
F F F T F F T T T
¬(P ∨ Q) ≡ ¬P ∧ ¬Q
Together these laws are known as De Morgan’s Law, and you will remember a
similar law from set theory. As we shall see later, the laws of propositional algebra
and the laws of set theory are isomorphic—which means that “they have the same
shape”.
De Morgan’s Law makes sense—in a previous worked example, we were asked
to express the following statements symbolically, where P represented “She is tall”
and Q represented “She is intelligent”.
In fact these statements mean exactly the same thing, and our answers were:
• ¬(P ∨ Q)
• ¬P ∧ ¬Q
Worked Example 7
Use De Morgan’s law to show that:
Solution
¬(¬P ∨ (P ∧ Q))
≡ ¬¬P ∧ ¬(P ∧ Q)
≡ P ∧ (¬P ∨ ¬Q)
72 5 Propositional Logic
P Q PvQ ¬(P v Q) P Q ¬P ¬Q ¬P Ʌ ¬Q
T T T F T T F F F
T F T F T F F T F
F T T F F T T F F
F F F T F F T T T
Worked Example 8
Use truth tables to prove the second version of De Morgan’s law:
¬(P ∨ Q) ≡ ¬P ∧ ¬Q
Solution
See Fig. 5.6.
The last columns in both truth tables are the same, thus proving the identity.
P∧Q ≡Q∧P
P∨Q ≡Q∨P
(P ∧ Q) ∧ R ≡ P ∧ (Q ∧ R)
(P ∨ Q) ∨ R ≡ P ∨ (Q ∨ R)
P ⇒ Q means : IF P THEN Q.
or alternatively : P IMPLIES Q.
5.10 The Equivalence Operator (⇔) 73
Look carefully at the truth table. Line 3 now gives a result of false. So if the
statement is true (lines 1 and 4), then if it is sunny I go shopping, if it is not sunny
I do not go shopping.
The equivalence operator takes away the uncertainty of the implies operator. I
go shopping only if it is sunny; also it is sunny only if I go shopping.
For this reason we can interpret P ⇔ Q as:
P if and only if Q (sometimes written as P iff Q).
P is a necessary and sufficient condition for Q.
Q is a necessary and sufficient condition for P.
Worked Example 9
Construct a truth tables for the following expression:
Q ⇔ (¬P ∨ ¬Q)
Solution
See Fig. 5.9.
It is important not to confuse the equivalence operator (⇔) with logical equiva-
lence (≡).
The first is an operator that joins two propositions: each of the two propositions
can be true or false, and the truth of the compound statement is determined from
the truth table.
The second is used with two statements (usually compound statements) and
means that they are identical because they have the same truth table.
5.13 Tautologies and Contradictions 75
Just as with arithmetic operators, it is not always immediately clear in which order
to do things. The order in which operations should be performed is as follows:
¬
∧
∨
⇒
⇔
P ⇒Q∨R∧Q
P ⇒ (Q ∨ (R ∧ Q))
A tautology is an expression for which all rows of the truth table evaluate to true.
For example: P ∨ ¬P.
The truth table for this expression appears in Fig. 5.10.
A contradiction is an expression for which all rows of the truth table evaluate
to false.
For example: P ∧ ¬P.
The truth table for this expression is shown in Fig. 5.11.
P Q P→Q P Ʌ (P → Q) (P Ʌ (P → Q)) → Q
T T T T T
T F F F T
F T T F T
F F T F T
Worked Example 10
By constructing a truth table, determine whether the following expression is a
tautology, a contradiction or neither:
(P ∧ (P ⇒ Q)) ⇒ Q
Solution
See Fig. 5.12.
All the rows in the final column are true, so the expression is a tautology.
With the OR operator, all that is required for the compound statement to be true
is that either one of the simple statements is true.
With the EXCLUSIVE OR operator (sometimes known as XOR), the com-
pound statement is true only when one (and only one) proposition is true. If
both are true, then the compound statement is false. The truth table is shown in
Fig. 5.13.
You might observe that XOR is actually closer to the way we use the word “or”
in natural language than is OR.
If you said “Tonight we are going to the cinema or we are going ice-skating”,
most people would assume you were doing one or the other but not both!
P Q P→Q P Q ¬P ¬Q ¬Q → ¬P
T T T T T F F T
T F F T F F T F
F T T F T T F T
F F T F F T T T
The contrapositive is equivalent to the original statement, as can be seen from the
truth tables in Fig. 5.14.
You will see with the laws that follow that there are parallels with set theory. As
we noted earlier, there is an isomorphism between set theory and propositional
logic.
We have already seen that AND and OR are commutative and associative, as
are intersection and union in set theory.
The following laws will also look familiar:
Idempotent Laws
P∧P ≡P
P∨P ≡P
Identity Laws
P∧F≡F
P∧T≡P
P∨F≡P
P∧T≡T
Complement Laws
P ∨ ¬P ≡ T
P ∧ ¬P ≡ F
¬T ≡ F
¬F ≡ T
78 5 Propositional Logic
Distributive Law
P ∨ (Q ∧ R) ≡ (P ∨ Q) ∧ (P ∨ R)
P ∧ (Q ∨ R) ≡ (P ∧ Q) ∨ (P ∧ R)
De Morgan’s Law
¬(P ∨ Q) ≡ ¬P ∧ ¬Q
¬(P ∧ Q) ≡ ¬P ∨ ¬Q
∏ U
You can see that ∧ and ∨ correspond to and in set theory, while T and F
correspond to U and ∅. In addition, negation in logic corresponds to complement
in set theory.
Worked Example 11
Use the distributive law to simplify the following expression:
P ∨ (¬P ∧ Q)
Solution
P ∨ (¬P ∧ Q)
≡ (P ∨ ¬P) ∧ (P ∨ Q) ( Distributive Law)
≡ T ∧ (P ∨ Q) ( Complement Law)
≡ P ∨ Q ( Identity Law)
¬(¬P ∨ Q) ≡ P ∧ ¬Q
So ¬(P ⇒ Q) ≡ P ∧ ¬Q Identity 2
Worked Example 12
Use algebra to show that: (P ∧ Q) ⇒ ¬Q ≡ ¬P ∨ ¬Q.
Solution
(P ∧ Q) ⇒ ¬Q
≡ ¬(P ∧ Q) ∨ ¬Q Using identity1
≡ ¬P ∨ ¬Q ∨ ¬Q De Morgan’s law
≡ ¬P ∨ (¬Q ∨ ¬Q) Using the fact that ∨ is associative
≡ ¬P ∨ ¬Q Idempotent law
Worked Example 13
Negate the following statement and simplify the answer.
(P ∧ Q) ⇒ R
Solution
¬((P ∧ Q) ⇒ R)
≡ (P ∧ Q) ∧ ¬R Using identity 2
≡ P ∧ Q ∧ ¬R Using the fact that ∧ is associative
5.18.1 Programming
One of the most common statements used in program code is the conditional
statement. An example in Python might look like this:
The || symbol in Java (and many other languages) means OR, corresponding to
our logical OR. The symbol && is used in Java for the logical AND.
The compiler will use the rules we have learnt to evaluate the statement that
follows if and thereby determine if it is true or false.
If it is true, the statements that are indented (in the Python example) or con-
tained in the braces (in the Java example) will be executed – otherwise, nothing
will happen and the program will move on to the next bit of code following the
indentation or braces. In this way, the if statement implements the implication
operator, ⇒. It does not say anything about what to do if the statement is false
(other than do nothing and move on).
NOT gate
Input Output
1 0
input output 0 1
OR gate
Input 1 Input 2 Output
1 1 1
input 1 output
1 0 1
input 2
0 1 1
0 0 0
AND gate
1 1 1
input 1 output
1 0 0
input 2
0 1 0
0 0 0
Classical logic assumes that all expressions evaluate to true or false. However,
in reality, this is not always the case when evaluating an expression, because
sometimes an expression can be undefined. For example, the expression 0/0. Also,
undefined terms are very common in programming situations—for example, when
a variable is first declared and has not yet been assigned a value.
A system of three-valued logic has therefore been developed to deal with the
possibility that a proposition could have the value true, false or undefined.
The truth tables for AND and OR in this system are shown Figs. 5.19 and 5.20.
Looking at the truth table for AND, we see that if either P or Q is false, then the
compound statement is false, even if the other proposition is undefined - because
both have to be true in order for the compound statement to be true. However, if
either P or Q is undefined and the other one is true, then the compound statement
is undefined, because its overall value will depend on the value of the undefined
proposition.
Similarly, in the table for OR, if just one of the propositions, P or Q, is true, then
the compound statement is true - the other proposition being undefined doesn’t
matter in this case. However, if one proposition is false and the other undefined
then the compound statement is undefined because it relies on the value of the
undefined proposition.
Unfortunately, you will see different notation used in different texts. Some texts
use single-barred arrows for implication and equivalence:
So instead of ⇒ and ⇔
you will see → and ←→.
To make matters worse, in texts that use this notation you will often see ⇔
used for logical equivalence instead of ≡.
In fact, some mathematicians actually make a distinction between the single-
barred arrow and the double one. They argue that you should use the single-barred
arrows for the logical connectives that join propositions - but you should use the
double-barred arrow for an assertion – that is a statement that is true.
In this notation:
P → Q means that P implies Q, and P and Q can take any values.
But: P ⇒ Q means that P implies Q is true (so in this case we can’t have P
true and Q false).
5.21 Exercises
R: 3.65 is an integer
What is the value of: (a) P ∧ R (b) P ∨ Q (c) Q ∨ R?
2. Let P be “It is summer” and Q be “Leon is playing tennis”. Give simple
sentences which represent the following statements:
(a) ¬P
(b) P ∧ ¬Q
(c) ¬P ∨ Q
(d) ¬ ¬Q
3. Let P be “She is a scientist” and Q be “She is intelligent”. Express each of
the following statements symbolically:
(a) She is intelligent, but she is not a scientist.
(b) She is a scientist, and she is intelligent.
(c) She is a scientist, or she is not intelligent.
(d) It is not true that she is a scientist or that she is intelligent.
4. Construct a truth table for the following expression:
¬(P ∧ Q) ∨ ¬Q
¬Q ∧ (¬P ∨ Q)
9. Negate the following expression, and simplify your answer (Hint: Use De
Morgan’s Law):
(P ⇒ Q) ∧ Q
P ∨ ¬Q
Predicate Logic and Proofs
6
6.1 Introduction
As you can see, we don’t know whether P(x), for example, is TRUE or FALSE
until we know the value of x.
We should note that predicates are functions. A predicate, just like a function,
accepts variables and produces an output. In this case the output is a truth value—
either TRUE or FALSE.
So a predicate is a truth-valued function.
If A is the set of people, then the predicate T (x, y) above would have the
following signature:
T: A × A → B
B = {TRUE, FALSE}.
When we have a predicate such as P(x), it is always important to know the set of
values from which is x drawn. This set is known as the domain of discourse. For
a predicate like “x is an even number”, our domain of discourse might be natural
numbers. For a predicate such as “studies physics”, our domain of discourse could
be people or students.
We can make our domain of discourse clear in one of two ways:
Once we give a value to the variables, we know whether the predicate is TRUE or
FALSE. This is also known as binding the variables. There are two ways of doing
this:
6.5.1 Substitution
As we saw previously, we can simply substitute specific values for the variables.
Using the examples from Sect. 6.3:
6.5.2 Quantification
A quantifier is a device for making a statement about a set of values, not just one
value.
There are three quantifiers that we can use, each with its own symbol.
∀x ∈ A · P(x)
Example
If M(x) is the predicate x chases mice (defined over the set of cats), then
∀x · M (x) reads: All cats chase mice.
If P(x) is a predicate:
∃x ∈ A · P(x)
Example
If M(x) is the predicate x chases mice (defined over the set of cats), then
∃!x · P(x) reads: There exists one and only one x, for which P(x) is true.
Example
If M(x) is the predicate x chases mice, (defined over the set of cats), then
∃!x · M (x) reads: There is one and only one cat who chases mice.
Worked Example 1
(a) B(x) is the predicate x can bark, defined over the domain of dogs.
Write the following statements in words:
(i) B (Rover)
(ii) ∀x · B(x)
(iii) ∃x · B(x)
(iv) ∃!x · B(x)
(v) ¬B(Rover)
(vi) ¬∀x · B(x)
(vii) ¬∃x · B(x)
(b) If we had not stated the domain of discourse in advance, how would we have
written part (ii), using D to represent the set of dogs?
6.5 Giving Values to the Variables 89
Solution
(a)
(i) Rover can bark.
(ii) All dogs can bark.
(iii) There is at least one dog that can bark.
(iv) There is one and only one dog that can bark.
(v) Rover cannot bark.
(vi) It is not true that all dogs can bark.
(vii) There does not exist a dog that can bark.
(b) ∀x ∈ D · B(x)
Worked Example 2
B(x) is the predicate x can bark, and T (x) is the predicate x has a tail, both defined
over the domain of dogs.
Write the following in words:
Solution
Worked Example 3
D(x) is the predicate x is a duck, and S(x) is the predicate x can swim, both defined
over the domain of animals.
Solution
(a)
(i) All ducks can swim (if x is a duck then x can swim).
(ii) There exists an animal that is a duck and cannot swim (there is at least
one duck that cannot swim).
(iii) Basil is a duck and there is one and only one animal that can swim.
(b)
(i) D(Basil) ⇒ ∀x · S(x)
(ii) ∀x · (D(x) ∨ ¬S(x))
(iii) ∃!x · (D(x) ∧ S(x))
¬∀x · M (x)
This means that there must be at least one student that is not good at maths:
∃x · ¬M (x)
¬∃x · M (x)
This is the same as saying that all students are poor at maths:
∀x · ¬M (x)
Worked Example 4
Negate the predicate ∀x · (¬P(x)).
Solution
Mathematical logic gives us the ability to make logical arguments. The way we
do this is to make a number of premises, and to show that if these premises are
correct, a certain conclusion follows.
The notation for this is to place the premises above a horizontal line, and the
conclusion below it.
For example:
P; Q; R
S
This means that if P, Q and R are true, then it follows that S is also true.
To help us with this, there are some basic laws that follow from the definitions
we learnt in the previous chapter.
P ⇒ Q; P
Q
It follows directly from our understanding of the implication operator and its
truth table (reproduced in Fig. 6.1).
It tells us that if P ⇒ Q is true and P is also true, then Q must be true (the first
line of the truth table).
P ⇒ Q; ¬Q
¬P
Again, it follows directly from our understanding of the implication operator
and its truth table.
It tells us that if P ⇒ Q is true and Q is false, then P must be false (the fourth
line of the truth table in Fig. 6.1).
P ⇒ Q; Q ⇒ R
P⇒R
It arises naturally from our definition of implication.
Worked Example 5
Show that if the following statements are true:
Solution
First assign letters to the propositions:
1. S⇒M Premise
2. M ⇒G Premise
3. ¬G Premise
4. S⇒G Chain rule on 1 and 2
5. ¬S Modus Tollens on 3 and 4
We have shown that our premises lead to the conclusion ¬S: the sun is
not hot.
You might come across the following alternative names for the three rules we have
just learnt:
Modus ponens: the rule of detachment
Modus tollens: the rule of contraposition
The chain rule: the rule of syllogism
P∧Q P∧Q
P Q
P; Q
P∧Q
94 6 Predicate Logic and Proofs
P
P∨Q
∀x · P(x)
P(c)
P(c)
∃x · P(x)
Worked Example 6
Show that if the following statements are true:
Solution
We can define the following propositions:
P: Maths is easy
Q: Physics is hard
R: Apples are green
6.6 Proof by Natural Deduction 95
1. (P ∧ Q) ⇒ R Premise
2. Q⇒P Premise
3. Q Premise
4. P Modus ponens on 2, 3
5. P∧Q AND-introduction on 3, 4
6. R Modus ponens on 5, 1
We have shown that our premises lead to the conclusion R: apples are
green.
Worked Example 7
Show that if the following statements are true:
Solution
We will define the following predicates:
P(x): x is a dog
Q(x): x can bark
r: Rover.
We have shown that our premises lead to the conclusion Q(r) : Rover can
bark.
Logic has a very important role to play in many areas of computer science. These
include such areas as artificial intelligence, computer architecture (think back to the
logic gates described in Sect. 5.18.2), the development of programming languages,
databases, and derivation of algorithms, and the theory of computation.
One particular area is in the development of commercial applications that
require high integrity software—that is to say, software that requires a very high
level of confidence in its correctness. Such situations include safety critical sys-
tems and secure financial systems. Formal methods are often used to produce
such software. With such methods the specification is written not just in a natural
language such as English, but in the language of mathematics. In this way the
specification can be far more precise and unambiguous. The mathematics involves
the propositional and predicate logic that we have studied here. Using proofs such
as those we have studied, the process can be taken further and used to develop the
software itself, and help ensure its correctness. The applications developed in this
way will be of a higher integrity than those developed by non-formal methods.
Well known formal methods include:
Prove that the statement holds for the first natural number n, usually, n = 0 or n = 1.
Show that if the statement is true for some natural number k, then it is also true
for k + 1.
This shows that if the statement is true when n = 0 (for example), it is also
true when n = 1, n = 2, n = 3 and so on up to infinity.
Worked Example 8
Prove that the following statement holds for all natural numbers:
n(n + 1)
1 + 2 + 3 + ... + n =
2
Solution
Base Step
Show that it holds when n = 1. This is easily shown:
1(1 + 1)
1=
2
Inductive Step
Assume the statement is true for some value n = k:
k(k + 1)
1 + 2 + 3 + ... + k = (6.1)
2
Now take the sequence up to k + 1:
1 + 2 + 3 + ... + k + k + 1
k(k + 1)
+k +1
2
k 2 + k + 2k + 2
=
2
k 2 + 3k + 2
=
2
(k + 1)(k + 2)
=
2
98 6 Predicate Logic and Proofs
Worked Example 9
Prove that for all n ≥ 1, the expression n3 + 2n is divisible by 3.
Solution
Base Step
Show that it holds when n = 1:
13 + 2 × 1 = 3, which is divisible by 3.
Inductive Step
Assume the statement is true for some value n = k:
k 3 + 2k = 3M (6.2)
(k + 1)3 + 2(k + 1)
= (k + 1)(k + 1)2 + 2(k + 1)
= (k + 1)(k 2 + 2k + 1) + 2(k + 1)
= (k 3 + 3k 2 + 3k + 1) + 2(k + 1)
= (k 3 + 3k 2 + 5k + 3)
= (k 3 + 2k) + (3k + 3k 2 + 3)
3M + (3k + 3k 2 + 3)
= 3(M + k + k 2 + 1)
This is divisible by 3.
Thus if it true for n = k, it is true for n = k + 1, and since it is true for n = 1,
it is true for all n ≥ 1.
6.9 Exercises 99
6.9 Exercises
it is Thursday.
Sam is a snake;
If Sam can bite then Paris is in France;
All snakes can bite;
Paris is in France.
2 + 22 + 23 + 24 + · · · + 2n = 2n+1 − 2
Matrices
7
7.1 Introduction
A matrix is a grid of numbers, consisting of rows and columns. The numbers are
enclosed in round or square brackets. Examples of matrices are shown in Fig. 7.1.
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2025 101
Q. Charatan and A. Kans, Maths For Computing, Undergraduate Topics in Computer
Science, https://doi.org/10.1007/978-3-031-69234-5_7
102 7 Matrices
We often use an upper case letter to denote a particular matrix. For example:
( )
14
A=
36
• transposition;
• addition/subtraction;
• scalar multiplication;
• matrix multiplication.
7.3.1 Transposition
To transpose a matrix we take each row of the original matrix and make it a column
of the transposed matrix.
⎛⎞
( ) 14
123
A= AT = ⎝ 2 5 ⎠
456
36
To add (or subtract) two matrices they must be the same shape.
Then just add (or subtract) the corresponding elements.
7.3 Matrix Operations 103
Example
⎛ ⎞ ⎛ ⎞
3 10 1 5 11 21
⎝ −2 4 6 ⎠ + ⎝ 2 5 8 ⎠
3 1 2 0 1 12
⎛ ⎞
3 + 5 10 + 11 1 + 21
= ⎝ −2 + 2 4 + 5 6 + 8 ⎠
3 + 0 1 + 1 2 + 12
⎛ ⎞
8 21 22
= ⎝ 0 9 14 ⎠
3 2 14
Example
⎛ ⎞
3 10 1
7⎝ −2 4 6 ⎠
3 1 2
⎛ ⎞
3 × 7 10 × 7 1 × 7
= ⎝ −2 × 7 4 × 7 6 × 7 ⎠
3×7 1×7 2×7
⎛ ⎞
21 70 7
= ⎝ −14 28 42 ⎠
21 7 14
To multiply two matrices together, the number of columns of the first matrix must
be equal the number of rows of the second.
Then we multiply the elements of each row of the first matrix by the elements
of each column of the second and add them.
This process is explained in Fig. 7.2
Note: Matrix multiplication is not commutative: A × B /= B × A.
104 7 Matrices
Worked Example 1
Consider the following matrices:
( ) ( )
−2 2 3 367
A= B=
1 84 985
Solution
( ) ( )
−2 + 3 2 + 6 3 + 7 1 8 10
(a) A + B = =
1+9 8+8 4+5 10 16 9
( ) ( )
−2 − 3 2 − 6 3 − 7 −5 −4 −4
(b) A − B = =
1−9 8−8 4−5 −8 0 −1
( ) ( ) ( )
−6 6 9 6 12 14 0 18 23
(c) 3A + 2B = + =
3 24 12 18 16 10 21 40 22
⎛ ⎞
−2 1
(d) AT = ⎝ 2 8 ⎠
3 4
7.4 The Determinant of a Matrix 105
Worked Example 2
Consider the following matrices:
( ) ( )
23 12
A= B=
45 03
Calculate A × B.
Solution
See Fig. 7.3
It should be noted that the notation for powers used in arithmetic is also used
with matrix multiplication. Thus
A2 = A × A A3 = A × A × A A4 = A × A × A × A
and so on.
Square matrices have a special property called the determinant, which can be
very useful in matrix algebra.
The determinant of a matrix A is written as det(A).
To find the determinant of a 2 × 2 matrix we multiply each pair of opposite
corners and subtract the result (we do the left-to-right diagonal first, and then the
right-to-left diagonal). For example:
( )
43
A=
21
106 7 Matrices
det(A) = (4 × 1)−(3 × 2)
= −2
Note: You will also see the following notation for the determinant:
| |
|4 3|
|A| | |
|2 1|
Worked Example 3
Find the determinant of the following matrix, A:
( )
−3 3
A=
−1 4
Solution
If:
⎛ ⎞
abc
A = ⎝d e f ⎠
ghi
We could also write this as (using the alternative notation for the determinant):
| | | | | |
|e f || |d f | |d e|
|A| = a × || |
−b×| | |
+c×| |
h i| g i| g h|
Finding the determinant of 3 × 3 and larger matrices can be very fiddly, but it
is possible to use an online calculator. For example: www.matrixcalc.org/en
An identity matrix is a square matrix that has 1s along the left-to-right diagonal
and 0s everywhere else.
Examples are shown in Fig. 7.4.
In calculations we refer to the identity matrix as I.
When we multiply a matrix by the identity matrix, the original is unchanged:
A × I = A I × A = A.
The inverse of a matrix, A, is a matrix that, when multiplied by A, will give you
the identity matrix. The inverse of A is written as A−1 .
A × A−1 = I
A−1 × A = I
1
if: ax = b then: x = b.
a
So the inverse acts like the reciprocal in arithmetic.
Example
( )
41
A=
23
det (A) = 4 × 3 – 2 × 1 = 10
( )
−1 1 3 −1
A =
10 −2 4
( )
0.3 −0.1
=
−0.2 0.4
Note:
Worked Example 4
Where possible, find the inverse of the following matrices:
( )
6
a. A =
2
( )
38
b. B =
26
( )
−1 0
c. C =
−2 0
Solution
Worked Example 5
Consider two of the matrices from the previous section:
( ) ( )
38 6
B= A=
26 2
B × X = A.
Therefore X = B-1 × A ( )
3 −4
In the last question we found that: B−1 = / .
−1 3 2
Therefore
( ) ( )
3 −4
/ 6
X = ×
−1 3 2 2
( )
(3 × 6) + (−4
( / × 2) )
=
(−1 × 6) + 3 2 × 2
110 7 Matrices
( )
10
=
−3
x+y+z =6
2y + 5z = −4
2x + 5y − z = 27
If we substitute the values of the right hand side of our equations into B we
have:
⎛ ⎞⎛ ⎞ ⎛ ⎞
1 1 1 x 6
⎝0 2 5 ⎠⎝ y ⎠ = ⎝ −4 ⎠
2 5 −1 z 27
x+y+z =6
2y + 5z = −4
2x + 5y − z = 27
To solve our equations we need to find X, which will give us the values for x,
y, and z. We use the fact that:
There are a number of operations that can be performed on the rows of a matrix
which prove useful in other calculations.
R1 ↔ R2 .
2R1 → R1
R1 + R2 → R2 .
R1 + 2R2 → R1 .
This method of solving equations is not easy and requires some patience and
intuition. It is unlikely that you would be required to perform this process in the
early stages of a computer science course, but it is nonetheless something you need
to be aware of, as it is an important tool in more advanced aspects of computing
such as image and graphics processing, specifically in transforming, scaling, and
rotating images.
x+y+z =5
2x + 3y + 5z = 8
4x + 5z = 2
2. Perform row operations on the augmented matrix until the matrix on the left
hand side of the divider is an identity matrix.
3. Read the solution from the right hand side.
Example
x+y+z =5
2x + 3y + 5z = 8
4x + 5z = 2
⎛ | ⎞
1 1 1 || 5
⎝2 3 5|8 ⎠
|
4 0 5|2
114 7 Matrices
x = 3 y = 4 z = −2
7.11 Exercises
Calculate A × B.
3. Consider the following matrices:
( ) ( )
27 15
A= B=
13 02
Calculate A × B.
4. Find the determinant of the following matrix, A:
( )
1 3
A=
4 −2
2x + y + 4z = 7
5y + z = 3
−x + 4y + 2z = −2
116 7 Matrices
2x + 5y = 21
x + 2y = 8
Combinatorics
8
8.1 Introduction
The main topics introduced in this chapter, permutations and combinations, form
part of the overall branch of mathematics known as combinatorics. Combinatorics
is primarily concerned with counting. We will learn here how to count the number
of ways that items can be arranged, and how many ways we can select items from
a larger group of items, depending upon whether or not the order in which we
select them is significant, and whether or not we are allowed to repeat items. We
will later go on to explore how this topic provides a convenient way of expanding
binomial algebraic expressions.
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2025 117
Q. Charatan and A. Kans, Maths For Computing, Undergraduate Topics in Computer
Science, https://doi.org/10.1007/978-3-031-69234-5_8
118 8 Combinatorics
Imagine six runners in a race. How many possible outcomes could there be? In
other words how many different ways are there of ordering the six competitors?
Table 8.1 shows the possible outcomes.
We see that the number of ways of arranging 6 items in order is: 6 × 5 × 4 ×
3 × 2 × 1 = 720.
8.3 Factorials
Worked Example 1
In how many different ways can the letters a, b, c, d be arranged?
Solution
There are 4 letters, so the number of ways of arranging them is
4!= 4 × 3 × 2 × 1 = 24.
8.4 Choosing just Some of the Items 119
Worked Example 2
7!×4!
Find the value of 5! .
Solution
7! × 4! 7 × 6× / 5× / 4× / 3× / 2× / 1 × 4 × 3 × 2 × 1
=
5! / 5× / 4× / 3× / 2× / 1
=7×6×4×3×2×1
= 1008
Consider again the six runners. Imagine we are interested only in the first two
places. The number of ways we can select some or all items from a list, when
(like in this example) the order is important is called a permutation
A permutation of 2 items from 6 is written like this: P(6, 2).
Another common notation is: 6 P2 .
Look again at Table 8.1. You can see that if we are only interested in the first
two places (the first and second rows of the table), then instead of calculating the
whole sequence, (6 factorial) we use only the first two and discard the last four.
Effectively we divided 6 factorial by 4 factorial:
6! 6!
P(6, 2) = =
(6 − 2)! 4!
6×5×4×3×2×1
=
4×3×2×1
= 6 × 5 = 30
6! 6!
P(6, 4) = =
(6 − 4)! 2!
6×5×4×3×2×1
=
2×1
= 6 × 5 × 4 × 3 = 360
120 8 Combinatorics
In general, if we want to know how many ways we can choose k items from n
items when the order in which we select them is important, we use the following
formula:
n!
P(n, k) =
(n − k)!
8.6 Combinations
Imagine a situation, such as a lottery, where we select a number of items, but the
order in which they are selected doesn’t matter. The number of ways we can select
several items from a list, when the order is not important, is called a combination.
The notation for combinations is similar to permutations, but uses a C instead
of a P. So we can write C(6,2) or 6 C 2. ( )
n
Another common notation for combinations is: .
k
( )
6
For example: .
2
Suppose we have a small lottery consisting of 12 balls, and we have to choose
three. There will be fewer choices now. When we were dealing with permutations
we could choose three items - for example the numbers 5, 6, 7 – and arrange them
in 3! ways. But if order is unimportant there is only one way. So we have to divide
the permutation by 3 factorial.
P(12, 3) 12!
C(12, 3) = =
3! 9! × 3!
12 × 11 × 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1
=
9×8×7×6×5×4×3×2×1×3×2×1
12 × 11 × 10
= = 220
3×2×1
In general, if we want to know how many ways we can choose k items from
n items when the order in which we select them is not important, we use the
following formula:
n!
C(n, k) =
(n − k)!k!
8.10 Allowing Repetition (When Order Is Not Important) 121
There are times when a calculation results in 0!, so this needs to have a value.
Imagine we have n objects. In how many different ways can we choose zero of
these objects? The answer has to be 1. There is only one way of choosing zero
items.
So logically: C(n, 0) = 1.
Therefore, using our formula for combinations:
n!
=1
n! × 0!
You can see that the only way this can work is if 0! is equal to 1.
Imagine you have to choose a 4-digit code for an intruder alarm. There are 10 dig-
its available, from 0 to 9. Order is important. However, this time you are allowed
to repeat digits—for example a code of 5508 or 4142.
There are 10 ways of choosing the first digit.
There are 10 ways of choosing the second digit—so the number of ways of
choosing the first 2 digits is: 10 × 10.
There are 10 ways of choosing the third digit—so the number of ways of
choosing the first 3 digits is: 10 × 10 × 10.
There are 10 ways of choosing the final digit—so the number of ways of
choosing all 4 digits is: 10 × 10 × 10 × 10.
The number of different codes is: 104 = 10,000.
In general, if we want to know how many ways we can choose k items from
n items when the order in which we select them is important and repetitions are
allowed, we use the following formula:
nk
In certain restaurants you can choose dishes by means of a colour code. Each
colour represents a particular price. Imagine there are four colours available – red,
blue, green, yellow. You are going to choose three dishes.
You want to know how many different combinations of dishes you could have
- each colour can be chosen more than once. All the possible combinations are
listed below:
122 8 Combinatorics
There are 20 different possibilities. The formula to use in this situation is given
below. It is an easy formula to use, but is harder to derive, so we will show you
the formula now, and explain how this is arrived at later.
In general, if we want to know how many ways we can choose k items from
n items when the order in which we select them is not important and repetitions
are allowed, we use the following formula:
(n + k − 1)!
k!(n − 1)!
The different ways of selecting k items from n items are shown in Table 8.2.
We will refer to the previous example of the restaurant with colour-coded dishes.
This time we will have five colours—red, blue, green, yellow and pink. We will
again select three dishes.
Since order is not important, we have to find a way of eliminating duplicates
such as (R, R, G) and (R, G, R). Because we are allowing repetitions, it is more
complicated than simply dividing the permutation by 3! like we did before.
One way to do it is to go through the colours in order, place a marker to show
that an item has been selected, and then place a different marker between each
type of item.
8.12 Deriving the Formula for Choosing Items when Order Is Not … 123
We will list the items in this order: R, B, G, Y, P and we will use 1 to show an
item is selected and 0 to separate the items. An example will make this clear.
(R, R, G) would be (1 1 0 0 1 0 0)
Here we have put a 1 for red, another 1 for red, then a zero to show we have
moved to blue. There are no blues, so another 0 shows we have moved to green.
We have placed a 1 for green, then another zero to show we have moved to yellow.
Finally another zero to show we have moved to pink.
So we have found a way to have only one string for each combination. Another
couple of examples:
You can see that there are always four 0s and three 1s; seven places in all.
Our question can now be put in a different way: If there are 7 places, how can
we choose 3 of them to put our 1s in? We are finding C(7, 3).
In general: There will always be one fewer 0’s than the total number of items,
as there is a 0 between each item. So if there are n items and we are choosing k
of them, then there will be n + k − 1 places, and we must chose k of them. We
need to calculate C(n + k – 1, k).
So we have:
(n + k − 1)!
C(n + k − 1, k) =
k!(n + k − 1 − k)!
(n + k − 1)!
=
k!(n − 1)!
Worked Example 3
A teacher has a class of 30 students. She has to chose 3 students to be awarded a
prize for good progress.
Solution
As there is no ordering involved here, and no repetition, we use the formula for
combinations without repetition:
n!
C(n, k) =
(n − k)!k!
124 8 Combinatorics
(b) The teacher now has to choose 3 students to be awarded 1st , 2nd and 3rd prize
for overall performance. How many different possibilities are there?
Solution
This time there is ordering, but once again no repetition, so we use the formula
for permutations without repetition:
n!
P(n, k) =
(n − k)!
30!
P(30, 3) =
27!
= 30 × 29 × 28
= 24360
Solution
In fact this is exactly the same as part b). Just think of it as placing the students
in a line with, say, the winners of the mathematics, science and language prizes in
first, second and third position.
So the answer is the same as before, 24360
(d) Once again there are to be three awards—mathematics, science and languages.
But this time the rules are relaxed, and each student is eligible for all three
prizes. So any student could receive as many as three prizes. How many
possibilities are there?
8.12 Deriving the Formula for Choosing Items when Order Is Not … 125
Solution
Once again there is ordering, but this time with repetition, so the correct calculation
is:
nk = 303 = 27000
Worked Example 4
A five-a-side football club consists of six different teams. Three members of the
club are to be chosen for a special award. They can be chosen from any team, so
that a particular team could end up winning three awards, two awards, one award
or no awards. In how many different ways can the awards be distributed among
the teams?
Solution
There is no ordering but there is repetition, because one team could win all three
awards, none of the awards, or any number in between.
Because there are six teams, n = 6 and k = 3.
The correct calculation for no ordering with repetition allowed is:
(n + k − 1)! 8!
=
k!(n − 1)! 3! × 5!
8×7×6
=
3×2×1
= 56
Worked Example 5
Five friends are planning a trip to the cinema. They will all sit together in a row.
Leroy and Calvin want to sit together. In how many different ways can the five
friends occupy the row?
Solution
The easiest thing to do is to consider Leroy and Calvin as one item. We now have
to work out how many ways to arrange four items, which is 4!.
4!= 4 × 3 × 2 × 1 = 24.
There are, however, 2! ways in which Leroy and Calvin can be arranged, so for
each of the above arrangements there are 2 options.
So the total number of arrangements is: 2 × 24 = 48.
Worked Example 6
How many 4-digit numbers can be made from the digits 1–9 if you are not allowed
to repeat numbers, and the number must be divisible by 5?
126 8 Combinatorics
Solution
If the number is divisible by 5, it must end in a 5 (since there is no zero). So this
comes down to arranging 3 numbers out of 8, without repetition.
8!
P(8, 3) =
5!
=8×7×6
= 336
C(5,2) = 10
Solution
(a) Because we start at zero, we will need to draw 6 rows of the triangle as shown
in Fig. 8.2
(b) C(5, 2) = 5!
3!×2! = 5×4×3×2×1
3×2×1×2×1 = 10
(a + b)0 = 1
(a + b)1 = a+b
(a + b)2 = a2 + 2ab + b2
(a + b)3 = a3 + 3a2 b + 3ab2 + b3
(a + b)4 = a4 + 4a3 b + 6a2 b2 + 4ab3 + b4
1
1 1
1 2 1
1 3 3 1
1 4 6 4 1
∑
n
(a + b)n = n
Ck an−k bk
k=0
This means that you start of with k = 0, than add every term until you reach n.
This is known as the binomial theorem.
Worked Example 8
Use the binomial theorem to expand the expression (x + 2y)5
Solution
The first term in our expression is x, the second is 2y, and in this case n = 5.
Worked Example 9
Use the binomial theorem to find the 3rd term in the expansion of the expression
(x − y)7
Solution
We saw that
∑
n
(a + b)n = n
Ck an−k bk
k=0
There are many examples in computer science where we need to compute the
number of different ways of doing things.
In computer graphics, for example, colour is made by combining the three
primary colours, red, blue and green, in different intensities. Different systems are
used to achieve this, and it is very important to know how many different colours
can be obtained using a particular system.
Another very important application is in the area of algorithm efficiency. When
writing the code for a particular task, there are always many ways of achieving the
same goal. Some algorithms are a great deal more efficient than others, because
they require fewer steps. Combinatorics plays a very important role in comparing
different algorithms by determining the number of possible steps involved in each
one.
8.16 Exercises
e % @ π ∗ £
How many different three letter "words" can be made from these symbols?
(Symbols can be repeated).
8. How many 3-digit numbers can be made from the digits 1–6 if:
(a) you are allowed to repeat digits;
(b) you are not allowed to repeat digits;
(c) you are not allowed to repeat digits and the number must end in 3;
(d) you are not allowed to repeat digits and the number must end in 1 or 4?
130 8 Combinatorics
9. Four friends go on a fairground ride. They must sit in a row. Tracey does not
want to sit at the end of the row. In how many different ways can the four be
arranged?
10. Use Pascal’s triangle to find the value of C(4, 2); verify your answer by using
the correct formula.
11. Use the binomial theorem to expand the expression (2x − y)4
12. Use the binomial theorem to find the 3rd term in the expansion of the
expression (x + 2y)6
Probability
9
9.1 Introduction
In this chapter you will be studying probability, which is the branch of mathemat-
ics that builds a framework around random, or chance, events. Probability plays
an extremely important role in many other disciplines such as physics, biology,
finance, and, as we shall see, in computer science.
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2025 131
Q. Charatan and A. Kans, Maths For Computing, Undergraduate Topics in Computer
Science, https://doi.org/10.1007/978-3-031-69234-5_9
132 9 Probability
S = {1, 2, 3, 4, 5}
We see that:
The probability that an event will happen is given by the following formula:
We use the notation P(E) to mean the probability that E will happen. We can
also use the notation for cardinality that we learnt when studied set theory:
If E is an event and S is the sample space, then:
n(E)
P(E) =
n(S)
S = {1, 2, 3, 4, 5}
Therefore
n(S) = 5
Table 9.1 shows how the probability of each of the events described in the
previous section is calculated.
We can express the probability as a fraction, a decimal or a percentage. So we
could have written, for example, P(B) = 0.4 or P(B) = 40%.
It is worth noting that:
Worked Example 1
A 6-sided dice is thrown.
Solution
(a) S = {1, 2, 3, 4, 5, 6}
(b) E = {1, 2}
(c) n(S) = 6 n(E) = 2
(d) P(E) = n(E)
n(S) = 3 .
1
Worked Example 2
Two 3-sided spinners (see Fig. 9.2) are spun together.
Solution
Consider the 5-sided spinner again. Let A be the event of getting a 2 and B be the
event of getting a 5:
A = {2} B = {5}
P(A ∪ B)
The examples we have just seen are mutually exclusive events. They cannot both
happen at the same time; a spinner cannot land on a 2 and a 5 at the same time.
Of course, events containing a single outcome are always mutually exclusive.
When the events contain more than one outcome, they may or may not be
mutually exclusive.
For example, if you throw a dice, the event of getting an even number and the
event of getting a 5 are mutually exclusive. For mutually exclusive events, there
are no common elements.
In other words, for two mutually exclusive events, A and B:
A∩B=∅
A = {1, 3, 5} B = {1, 2, 3}
136 9 Probability
These events are not mutually exclusive. It is possible to throw a number which
is odd and is less than 4 (the number 1 or the number 3). The intersection of the
two sets is not empty:
A ∩ B = {1, 3}
so
4 2
P(A ∪ B) = =
6 3
There is another way we can calculate the probability of one or another event
happening. When we studied sets we saw that:
So we have:
A ∩ B = {1, 3}
So:
3 1
P(A) = =
6 2
9.6 Mutually Exclusive Events 137
3 1
P(B) = =
6 2
2 1
P(A ∩ B) = =
6 3
1 1 1 2
P(A ∪ B) = + − =
2 2 3 3
This is, of course, the same answer that we got before.
A∩B=∅
So:
P(A ∩ B) = 0
Worked Example 3
Solution
Worked Example 4
Consider the following reduced pack of cards consisting of just the following:
2, Q
4, 10, Q
2, Q
2, 4, K
Solution
Let A be the event of drawing a queen. Let B be the event of drawing a red card.
These events are not mutually exclusive. You can draw a card that is a queen
and a red card.
There are 10 cards altogether; there are 5 red cards, there are 3 queens, and
there are 2 cards that are red and are queens.
3 5 2
P(A) = P(B) = P(A ∩ B) =
10 10 10
3 5 2 3
P(A ∪ B) = + − =
10 10 10 5
When we throw a dice, there are 6 possible outcomes. If the dice is fair, then the
probability of throwing any of the numbers from 1 to 6 is 16 .
The notation we use for the probability of an event can also be used for the
probability of outcomes. So for example P(1) is the probability of throwing a 1.
In general, if A is the event of getting outcome a, then P(A) = P(a).
We write the probability distribution for the dice as follows:
1
P(1) =
6
9.7 Probability Distribution 139
1
P(2) =
6
1
P(3) =
6
1
P(4) =
6
1
P(5) =
6
1
P(6) =
6
Since individual outcomes are mutually exclusive, the total of all the proba-
bilities must be 1. And again because they are mutually exclusive we can add
the probabilities to find the probability of one or the other event happening. The
probability of throwing a 2 or a 5 is:
1 1 1
+ =
6 6 3
Now imagine we toss a coin. This time the coin isn’t fair—it has been weighted
so that it is 3 times more likely to land on tails than on heads. How can we find
the probability distribution?
We let x represent the probability of getting heads. Therefore the probability of
getting tails is 3x.
So P(HEADS) = x
P(TAILS) = 3x
x + 3x = 1
4x = 1
x = 0.25
P(HEADS) = 0.25
P(TAILS) = 0.75
140 9 Probability
Worked Example 5
An unfair 6-sided dice is weighted so that the chances of throwing a 4, 5 or 6 are
equal. The chance of throwing a 2 or a 3 is twice as likely as throwing a 4, 5 or
6. The chance of throwing a 1 is three times as likely as throwing a 4, 5 or 6.
Solution
P(1) = 3x
P(2) = 2x
P(3) = 2x
P(4) = x
P(5) = x
P(6) = x
3x + 2x + 2x + x + x + x = 1
10x = 1 x = 0.1
P(1) = 0.3
P(2) = 0.2
P(3) = 0.2
P(4) = 0.1
P(5) = 0.1
P(6) = 0.1
(b) The events are mutually exclusive, therefore the probability of throwing a 3 or
a 4 is: P(3) + P(4) = 0.3.
9.8 Independent Events 141
Spinner Coin
P(RED) = 13 P(HEADS) = 21
P(BLUE) = 13 P(TAILS) = 21
P(YELLOW) = 1
3
When dealing with independent events we find the probability of both events
happening simply by multiplying the two individual probabilities together.
1 1 1
P(YELLOW, HEADS) = × =
3 2 6
Worked Example 6
Three fair coins are tossed. What is the probability of getting exactly two heads?
Solution
Table 9.2 shows that there are 8 possible outcomes:
If S is the sample space, then n(S) = 8
There are 3 outcomes (outcomes 2, 3 and 5) in which there are exactly two
heads.
If E is the event of getting exactly 2 heads, then n(E) = 3.
Therefore:
n(E) 3
P(E) = =
n(S) 8
Example
We saw that a 5-sided spinner can have five following possible outcomes: The
spinner can land on 1, 2, 3, 4, or 5.
We know that the sample space for the spinner is: S = {1, 2, 3, 4, 5}.
In this case, as the outcomes are numbers, it makes sense to define our random
variable X as:
X = {1, 2, 3, 4, 5}
1 1 1
P(X = 1) = P(X = 2) = P(X = 3) =
5 5 5
1 1
P(X = 4) = P(X = 5) =
5 5
The sum of the probabilities is, of course, 1.
Now consider the experiment that we looked at in the last worked example,
namely tossing three coins. We are interested in the number of heads thrown,
which is shown in Table 9.3.
9.10 Expected Value (Mean Value) 143
Table 9.4 The probability distribution for number of heads thrown when tossing three coins
No of heads thrown, x 0 1 2 3
Number of occurrences 1 3 3 1
P(X = x) 1/8 3/8 3/8 1/8
X = {0, 1, 2, 3}
3
Here we have: P(X = 2) =
8
2. The event of throwing at least two heads:
3 1 1
In this case P(X ≥ 2) = + =
8 8 2
Rather than actually throwing a dice, say, ten thousand times we can use a
formula to do this calculation. The calculation involves taking each value (x),
multiplying it by its probability (p) and adding them all together.
This is expressed as: E(x) = ∑xp.
Example
Table 9.5 represents the probability distribution for a fair 5-sided spinner:
1 1 1 1 1
E(x) = ×1+ ×2+ ×3+ ×4+ ×5=3
5 5 5 5 5
For a fair spinner, this is exactly the result we would expect to find.
Now let’s consider the probability for a dice which is weighted as follows
(Table 9.6).
In this case we see that the expected value is not in our sample set—this is to
be expected, because it is an average.
Worked Example 7
Consider the previous example of tossing a coin three times, where X was the
random variable representing the number of heads thrown. What is the expected
value?
Solution
We saw that the probability distribution was as shown in Table 9.4. The expected
value is calculated as follows:
1 3 3 1
E(x) = × 1 + × 3 + × 3 + = 1.5
8 8 8 8
9.11 Conditional Probability 145
2
6 3 2 1
Green P ( GREEN, GREEN ) = x =
3 7 6 7
7
Green
3 4 2
4 Red P ( GREEN, RED ) = x =
7 6 7
6
3
6 4 3 2
Green P (RED, GREEN ) = x =
7 6 7
4
Red
7
4 3 2
3 Red P ( RED, RED ) = x =
7 6 7
6
3 4 2
× =
7 6 7
Worked Example 8
A bag contains three green balls, three yellow balls and four red balls.
146 9 Probability
(a) A ball is picked randomly from the bag, and is then returned to the bag.
Another ball is then picked. What is the probability of picking a green ball,
followed by a red ball?
(b) A ball is picked randomly from the bag, but is not returned. Another ball
is then picked. What is the probability of picking a green ball, followed by
another green ball?
(c) Represent the probabilities described in part (b) on a tree diagram.
(d) Use this diagram to find the probability of picking a red ball followed by a
yellow ball OR a yellow ball followed by a green ball.
Solution
3
10
The probability of picking a red ball is:
4
10
The probability of picking a green ball followed by a red ball is:
3 4 3
× =
10 10 25
(b) In this case, the events are not independent.
The probability of picking a green ball is:
3
10
There are 9 balls left, one green ball has gone, so the probability of another
green ball is:
2
9
So the probability of picking a green ball followed by another green ball
is:
3 2 1
× =
10 9 15
(c) The tree diagram is shown in Fig. 9.5
9.12 Bayes’ Theorem 147
3 2 1
2 Green P (GREEN, GREEN) = x =
9 10 9 15
3
9 3 3 1
Green Yellow P (GREEN, YELLOW) = x =
10 9 10
4 Red 3 4 2
3 9 P (GREEN, RED) = x =
10 10 9 15
3 3 1
3 Green P (YELLOW, GREEN) = x =
9 10 9 10
3
2
10 9 3 2 1
P (YELLOW, YELLOW) = x =
Yellow Yellow 10 9 15
4 3 4 2
9 Red P (YELLOW, RED) = x =
10 9 15
4
10
4 3 2
3 Green P (RED, GREEN) = x =
9 10 9 15
3
9 4 3 2
Red Yellow P (RED, YELLOW) = x =
10 9 15
3
9 Red 4 3 2
P (RED, RED) = x =
10 9 15
(d) The probability of picking a red ball followed by a yellow ball OR a yellow
ball followed by a green ball is:
2 1 7
+ =
15 10 30
P(A) × P(B|A)
P(A|B) =
P(B)
Here P(A|B) is read as the probability of A given B, and refers to the probability
that event A will happen if we already know that event B has happened.
We will not show you the proof of Bayes’ theorem here, but this can easily be
looked up for those who are interested.
148 9 Probability
3
P(GREEN) =
7
Similarly, the probability of picking a red ball is
4
P(RED) =
7
If a red ball has already been picked then the probability of picking a green
ball becomes 36 or 21 . We write this as:
1
P(GREEN|RED) =
2
This is read as the probability of green given red.
Bayes’ theorem tells us:
P(RED) × P(GREEN|RED)
P(RED|GREEN) =
P(GREEN)
7 × 2
4 1
= 3
7
4 1 7 2
= × × =
7 2 3 3
In this case, all the information is known to us, so we could have worked out
for ourselves the probability of choosing a red ball once a green ball has been
removed—in other words P(red|green); and it is indeed 46 or 23 , thus showing
that Bayes’ theorem holds in this particular case.
There are, however, many cases where we do not have all the information, and
this is when Bayes’ theorem comes into its own.
As an example, consider the following:
• a test for a disease has a 95% probability of yielding a correct positive result
when the disease is present;
• 2% of all people who are tested test positive.
• it is known that 1.5% of the population actually has the disease.
We need to know the probability that an individual who tests positive actually has
the disease. In other words we need to calculate P(disease|positive).
Bayes’ theorem tells us that:
P(DISEASE) × P(POSITIVE|DISEASE)
P(DISEASE|POSITIVE) =
P(POSITIVE)
9.12 Bayes’ Theorem 149
P(DISEASE) = 0.015
The probability of someone testing positive when they have the disease is:
P(POSITIVE|DISEASE) = 0.95
P(POSITIVE) = 0.02
0.015 × 0.95
P(DISEASE|POSITIVE) =
0.02
≈ 71%
Worked Example 9
A college has 100 students. 40 are men and 60 are women. 20 of the men study
science subjects, and 50 of the women study science subjects.
(a) What is the probability of randomly selecting a man from the student body?
(b) What is the probability of randomly selecting a student who studies science?
(c) What is the probability of randomly selecting a student who studies science
from the male students?
(d) What is the probability of randomly selecting a male student from those who
study science?
(e) Show that Bayes’ theorem holds for this set of figures.
Solution
(d) P(MAN|SCIENCE) = 20 70 = 7 .
2
P(SCIENCE) × P(MAN|SCIENCE)
P(SCIENCE|MAN) =
P(MAN)
10 × 7
7 2
= 2
5
150 9 Probability
1
=
2
This is the same result as we got for part (c), showing that Bayes’ theorem
holds.
Worked Example 10
A study finds that the probability of a particular word appearing in an email is
15%. It also finds that the probability of an email being spam if it contains this
word is 40%. The probability of any email being spam is 20%.
Use Bayes’ theorem to find the probability that a spam email contains this
particular word.
Solution
P(WORD) = 0.15
P(SPAM) = 0.2
P(SPAM|WORD) = 0.4
P(WORD) × P(SPAM|WORD)
P(WORD|SPAM) =
P(SPAM)
0.15 × 0.4
=
0.2
= 30%
• throwing a dice, where getting a 6 is a success, all other outcomes are failures;
• asking a question in an opinion poll, where “yes” is considered success, but
“no”, “don’t know” and “won’t vote” are failures;
• answering a question in a multiple choice exam, where one answer is correct
(success) all other answers are incorrect (failure).
9.13 Binomial Probability 151
Worked Example 11
A multiple choice paper consists of 10 questions, each with five possible answers.
Only one answer is correct.
If the questions are answered completely randomly, what is the probability of
getting exactly four correct answers?
Solution
We use the formula:
In this case:
n = 10
k=4
n−k =6
p = 0.2 (There are 5 correct answers)
q = 0.8
Note that if you had been asked the probability of getting at least four correct
answers, you would have to add all the probabilities from four correct answers up
to 10 correct answers.
Worked Example 12
A coin is tossed five times. What is the probability of getting exactly three heads
if:
Solution
In both cases we use the formula:
In each case:
n=5
k=3
n−k =2
p = 0.5
q = 0.5
p = 0.5
q = 0.75
9.15 Exercises 153
There are many important areas of computer science in which probability plays
a crucial role. It is used in algorithms for machine learning to model uncertainty
and to make predictions. It is also plays an important role in data analysis. In
cryptography it is used to analyze the security of protocols and algorithms. In
network security probability is used to assess the likelihood of cyber attacks and
to design secure communication protocols.
9.15 Exercises
1. A 5-sided spinner has sides coloured yellow, blue, green, red and black.
(a) What is the sample space, S?
(b) Give the value of E, the event that the spinner lands on a colour which
starts with the letter “B”.
(c) What is the cardinality of the set S and the set E?
(d) Calculate the probability that E happens.
2. A small lottery consists of randomly choosing one of twelve balls, labelled
1–12.
The following events are defined:
A is the event of choosing a number less then 8.
B is the event of choosing an odd number.
C is the event of choosing a number divisible by 4.
(a) What is the sample space, S?
(b) Give the value of A, B and C.
(c) Give the values of: A∩B A∩C B∩C
(d) Which pairs of events are mutually exclusive?
(e) Calculate the probability of choosing an odd number or a number divisible
by 4.
(f) Calculate the probability of choosing a number less than 8 or a number
divisible by 4.
3. An experiment is done, and two events, A and B are defined.
The probability of A happening is 0.6.
The probability of B happening is 0.4.
The probability of both A and B happening is 0.2.
What is the probability of A or B happening?
4. Consider the following reduced deck of cards consisting of just the following:
154 9 Probability
• explain what is meant by the terms graph, vertex, node, edge and adjacency
matrix;
• distinguish between a simple graph and a multigraph, and between a
connected graph and an unconnected graph;
• explain what is meant by the degree of a vertex, and state the sum of degrees
of vertices theorem;
• explain what is meant by the distance between two vertices, the eccentricity
of a vertex and the radius and diameter of a vertex;
• define the term subgraph;
• distinguish between paths, trails, circuits and cycles;
• identify isomorphic and homeomorphic graphs;
• define the terms traversable trail and traversable graph and explain the terms
Eulerian graph and Hamiltonian graph;
• describe what is meant by a weighted graph and provide an adjacency matrix
for a weighted graph;
• explain what is meant by a tree and a binary tree;
• explain the term spanning tree, and find a minimum spanning tree for a
particular graph using Kriskal’s algorithm;
• describe three methods for traversing a binary tree;
• explain the term planar graph, and state and use Euler’s formula;
• describe what is meant by a directed graph (digraph), and provide adjacency
matrices for directed graphs.
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2025 157
Q. Charatan and A. Kans, Maths For Computing, Undergraduate Topics in Computer
Science, https://doi.org/10.1007/978-3-031-69234-5_10
158 10 Graph Theory
10.1 Introduction
In this final chapter we will be studying graph theory. Here the word graph does
not refer to the most common usage of the word—namely a chart showing the
relationship between two or more quantities by means of a curve or line—but to
a representation of connected objects. As you will see at the end of the chapter,
graph theory plays a very important role in the world of computer science.
10.2 Definitions
G = (V , E)
In Fig. 10.1:
V = {a, b, c, d } E = {e1 , e2 , e3 , e4 }
Vertices such as a and b in Fig. 10.1 which are connected by an edge are said
to be adjacent. A graph can be represented by an adjacency matrix, in which
each element of the matrix is either a 0, showing that there is no direct connection
d
10.4 Connected and Unconnected Graphs 159
between the vertices, or 1 showing that there is a connection. The adjacency matrix
below represents the graph shown in Fig. 10.1.
abcd
⎛ ⎞
a 0 1 1 1
b⎜⎜1 0 0 1⎟⎟
c ⎝1 0 0 0⎠
d 1 1 0 0
10.3 Multigraphs
In the graph shown in Fig. 10.1, each pair of vertices is connected by one edge
only. Now consider the graph shown in Fig. 10.2.
In Fig. 10.2, the vertices a and c are connected by two edges, e2 and e5 . These
two edges are referred to as multiple edges. Additionally one edge, e6 , has the
same start point and endpoint—such an edge is referred to as a loop. A graph that
allows multiple edges and loops is called a multigraph.
The graphs shown in Figs. 10.1 and 10.2 are connected graphs, because there is
a path between any two of the vertices.
However, a graph such as that shown in Fig. 10.3 is an unconnected graph,
because, for example, there is no path between vertex a and vertex d.
160 10 Graph Theory
The degree of a vertex is the number of vertices that are adjacent to that vertex.
The degree of a vertex, v, for example, is written:
deg(v)
In a simple graph with n vertices, the degree of any vertex is always less than
or equal to n − 1.
The sum of the degrees of the vertices is equal to twice the number of edges. So
in Fig. 10.1, for example, the sum of the degrees of vertices is 8, which is twice
the number of edges, 4.
This also holds for multigraphs, but we must remember to count the degree of
a vertex which has a loop as 2.
So in Fig. 10.2 we have:
Thus the sum of the degrees is 12, which is twice the number of edges, 6.
The distance between two vertices u and v is the length of the shortest path between
u and v. It is written d(u,v).
10.7 Eccentricity of a Vertex 161
e(b) = 4 (b to a, a to c, c to f , f to g)
e(a) = 3 (a to c, c to f , f to g)
e(c) = 2 (c to f , f to g or c to a, a to b).
d
162 10 Graph Theory
radius(G) = 2.
diameter(G) = 4.
Worked Example 1
Consider the graph shown in Fig. 10.6:
(b) Show that the sum of degrees of vertices theorem holds for this graph.
(c) State the value of:
(e) Referring to the graph in Fig. 10.6 as G, give the values of:
radius(G) diameter(G).
d
10.10 Paths, Trails, Circuits and Cycles 163
c c
G S
Solution
10.9 Subgraphs
V2 ⊂ V1 and E2 ⊂ E1
In Fig. 10.7:
The following are all possible paths in this graph—we have assigned each a
number in order to refer to them later:
1: a, e3 , c, e5 , h, e8 , g
2: a, e4 , d , e7 , f
3: c, e2 , b, e1 , a, e3 , c, e5 , h
4: c, e2 , b, e1 , a, e3 , c, e2 , b
5: d , e4 , a, e1 , b, e2 , c, e3 , a, e9 , h, e6 , d
6: b, e1 , a, e3 , c, e2 , b
7: a, e3 , c, e5 , h, e6 , d
8: a, e4 , d
The number of edges in a path is called the length of the path. For example, path
3 above has a length of 4, while path 8 has a length of 1.
A path in which all vertices are distinct is called a simple path. Paths 1, 2, 7
an 8 above are all simple paths.
A path in which all edges are distinct is called a trail. All of the above paths
are trails, with the exception of path 4.
A path in which all edges are distinct (that is, a trail), and which begins and
ends on the same vertex is called a circuit. Paths 5 and 6 above are circuits.
A circuit in which all vertices (other than the start and end vertex) are distinct
is called a cycle. Path 6 above is a cycle, but path 5 is not. A cycle with n edges
is referred to as an n-cycle. For example path 5 above is a 3-cycle.
Paths 7 and 8 are both paths from a to d. However, 8 is the shortest path from
a to d.
Worked Example 2
Consider the graph shown in Fig. 10.9:
Now consider the following path: f , e6 , a, e5 , d , e3 , b, e1 , a, e6 , f .
State whether this path is:
(b) a trail;
(c) a circuit;
(d) a cycle.
Solution
(a) It is not a simple path because the vertices are not all distinct.
(b) It is not a trail, because not all edges are distinct.
(c) It is not a circuit, because not all edges are distinct.
(d) It is not a cycle, because a cycle is a type of circuit—also not all vertices are
distinct.
f , e5 , c, e4 , d , e1 , a, e2 , b, e3 , c
b
10.12 Traversable Graphs 167
Fig. 10.13 A a
non-traversable graph
A traversable trail that begins and ends on the same vertex is called an Eulerian1
trail or Eulerian circuit. A graph that contains an Eulerian circuit is called an
Eulerian graph.
The graph shown in Fig. 10.14 is Eulerian: it contains the following Eulerian
circuit:
a, e1 , d , e4 , c, e6 , g, e7 , f , e5 , c, e3 , b, e2 , a
We saw above that an Eulerian circuit traverses each edge exactly once but may
repeat vertices.
In contrast a path that begins and ends with the same vertex and visits all the
vertices in a graph exactly once (apart from the beginning and end vertex) is called
a Hamiltonian circuit. A Hamiltonian circuit does not need to contain every edge.
A graph that contains a Hamiltonian circuit is called a Hamiltonian graph.
1 Pronounced “Oilerian”.
168 10 Graph Theory
The graph shown in Fig. 10.15 is a Hamiltonian graph, because it contains, for
example, the following Hamiltonian circuit:
b, e3 , c, e4 , f , e6 , d , e1 , a, e2 , b
Worked Example 3
State whether the graph shown in Fig. 10.16 is:
(a) Traversable;
(b) Eulerian
(c) Hamiltonian.
Solution
(a) It is traversable because exactly two of the vertices (c and d) have an odd
degree. One traversable trail is:
c, e2 , a, e6 , f , e7 , b, e1 , a, e8, d , e4 , c, e5 , b, e3 , d , e4 , c
(b) It is not Eulerian because not all vertices have an even degree (c and d have
odd degrees).
(c) It is Hamiltonian because it contains, for example, the following circuit, which
visits all vertices:
c, e4 , d , e3 , b, e7 , f , e6 , a, e2 , c.
abcd e
⎛ ⎞
a 0 2 030
b⎜⎜2 0 5 0 0⎟
⎟
c⎜⎜0 5 0 6 0⎟
⎟
d ⎝3 0 6 0 1⎠
e 0 0 010
Worked Example 4
Provide an adjacency matrix for the weighted graph shown in Fig. 10.18.
b
170 10 Graph Theory
Solution
abcd
⎛ ⎞
a 0 1 8 5
b⎜⎜1 0 3 0⎟⎟.
c ⎝8 3 0 c⎠
d 5 0 6 0
10.14 Trees
A connected acyclic graph is called a tree. Put more simply, a tree is a connected
graph with no cycles. An example of a tree is shown in Fig. 10.19.
The edges of a tree are known as branches. A node such as a is said to have
two child nodes; a is referred to as the parent of b and d. The nodes without child
nodes (for example b, c and g in Fig. 10.19) are called leaf nodes.
A tree with n vertices will have n-1 edges.
A disconnected acyclic graph—that is to say a disjoint collection of trees—is
called a forest. Figure 10.20 shows an example of a forest.
b
10.14 Trees 171
a a a
b
b b
G H K
Worked Example 5
Use Kriskal’s algorithm to find the minimum spanning tree for the weighted graph
shown in Fig. 10.24.
Solution
Arrange the edges in order of weight as in Table 10.2.
A binary tree is a tree which has a root node that has exactly two children, and
in which each of the subsequent nodes has at most two children. Binary trees
are very important data structures in computer science, and organizing data in
this way provides very useful algorithms for such things as searching and sorting.
Figure 10.26 shows an example of a binary tree.
b
174 10 Graph Theory
Each node must contain either 0 or 2 children. Put another way, each node except
the leaf nodes must contain 2 children.
The nodes are filled from left to right—every level, except the last one must be
full—the nodes of the last level must be as left at possible.
All the internal nodes have 2 children, and all the leaf nodes are at the same level.
Note that a perfect binary tree is a special instance of a balanced binary tree.
All the internal nodes have only one child. The graphs in Figs. 10.32 and 10.32
are referred to left-skewed and right-skewed binary trees respectively.
Let’s apply this to the tree in Fig. 10.33. We start with the left subtree—that is the
subtree whose root is b. We apply the inorder rules to this tree so we traverse the
left subtree of this tree, which is the subtree whose root is c. The left subtree of
this tree has the root d, but this is a leaf-node, so there is no left sub-tree of this
tree, and we therefore visit the root d.
c
10.14 Trees 177
Having visited the root of this subtree, according to the inorder rules, we visit
the root, c. We now traverse the right-hand subtree; again this ends with a leaf
node, so we visit the node e.
We have now traversed the entire subtree whose root is c—that is to say the
left subtree of the tree whose root is b. So now we visit the root of this tree, b. We
now visit the right subtree of this tree applying the same rules so we visit g, f and
h in that order.
Now we can visit the root of the whole tree, a.
So far we have visited the nodes in this order: d , c, e, b, g, f , h, a
We now apply exactly the same procedure to the right subtree (the one whose
root is i). This will mean visiting the nodes in the order k, j, l, i, n, m, o.
Putting these together we get a traversal path of:
d , c, e, b, g, f , h, a, k, j, l, i, n, m, o
Preorder Traversal
Again, we’ll apply this to the tree in Fig. 10.33. We visit the root, a. We then
traverse the left subtree, namely the one whose root it b. We visit the root, b. We
then traverse left subtree of this tree, the one whose root is c. We visit the root,
c. We visit the left subtree of this, whose root is d. We visit d. There is no left
subtree of this tree, so we visit the right subtree, and visit the root e. We have
now traversed the entire left subtree of the tree with root b, so we traverse its right
subtree with the same rules, so we visit f, g, h in that order.
So now we must traverse the right subtree, and, following the same rules, we
visit i, j, k, l, followed by m, n, o.
So our traversal path is: a, b, c, d , e, f , g, h, i, j, k, l, m, n, o
Postorder Traversal
Referring again to Fig. 10.33, we start by traversing the left subtree, the one with
root b, We traverse the left subtree of this, following the same rules, so we traverse
the subtree starting with c. The left subtree of this is the one with root d, which
has no left tree and no right tree. So we visit the root, d. We must now traverse the
right subtree of the tree with root c. Its left subtree is the one with root e, which
again has no subtrees, so we visit e. We then visit the root, c.
We must now visit the right subtree of b, so following the same rules, we visit
g, h, f . We then visit the root, b.
178 10 Graph Theory
That completes the traversal of the left subtree, and we now traverse the right
subtree in the same way: we visit k, l, j followed by n, o, m, i.
Finally we visit the root of the tree, a.
This gives the following path: d , e, c, g, h, f , b, k, l, j, n, o, m, i, a
Worked Example 6
Consider the binary tree shown in Fig. 10.34. Provide the path obtained by using:
Solution
(a) Inorder: d , b, e, a, c, f
(b) Preorder: a, b, d , e, c, f
(c) Postorder: d , e, b, f , c, a.
A planar graph is a graph that can be drawn in a plane without any of the edges
crossing. Consider the graph shown in Fig. 10.35a. This is a planar graph because
it can be redrawn as in Fig. 10.35b. Each particular representation of a planar
graph is called a map.
A map divides the graph into a number of bounded areas known as regions.
Figure 10.36 shows a planar graph divided into regions.
10.15 Planar Graphs 179
b
b
(a) (b)
You will notice that the area outside the graph, r 5 in this case, counts as one
region. This is known as an infinite region.
The degree of a region refers to the sum of the edges that border the region. In
the case of an infinite region, we count the number of edges that bound the graph.
Thus
Euler’s Formula
If V is the number of vertices in a map, E is the number of edges, and R is the
number of regions, then:
V −E+R=2
V =6 E=9 R=5
V −E+R=6−9+5=2
180 10 Graph Theory
Worked Example 7
Consider the planar graph shown in Fig. 10.37:
Solution
See Fig. 10.38.
V − E + R = 4 − 5 + 3 = 2.
G = (V , E)
In Fig. 10.28:
V = {a, b, c, d } E = {(a, b), (a, c), (a, d ), (b, b), (b, c), (b, d ), (c, d )}
In the case of a directed graph, rather than talk about the degree of a vertex,
we talk about the indegree and outdegree. For example, in Fig. 10.28:
It holds that:
The sum of the indegrees of a vertex equals the sum of the outdegrees, which both
equal the number of edges.
In Fig. 10.40 we see an adjacency matrix for the directed graph in Fig. 10.39. In
the case of a digraph, each entry in the matrix indicates whether there is a path of
length 1 from one particular vertex to another. In the example below the direction
is from the row to the column.2 A zero indicates that there is no connection. Some
annotations have been added to make it clear.
In the case of a directed graph it can be shown that, for an adjacency matrix A,
A2 will show the number of paths of length 2 from a particular vertex to another,
A3 will show the number of paths of length 3 and so on.
In general, for a directed graph with the adjacency matrix A, the number of
paths of length k for each entry of the matrix is given by Ak .
2 In some texts you will find that the direction is from column to row.
182 10 Graph Theory
In the above case, referring to the matrix as A, we find (using the rules you
learnt for matrix multiplication in Chap. 7 (or using an online calculator) that:
abc d abcd
⎛ ⎞ ⎛ ⎞
110 0 a 1011 a
⎜
2 ⎜1 0 2 0⎟ ⎜2 2 1 0⎟ b
A =⎝ ⎟b A3 = ⎜ ⎟
101 1⎠ c ⎝2 1 2 0⎠ c
111 0 d 2111 d
You can verify yourselves that this is indeed correct. For example:
There are two paths of length 2 from c to b: cd , db and ca, ab.
There is one path of length 3 from d to a: db, bc, ca
Worked Example 8
Consider the digraph shown in Fig. 10.41:
Solution
abc
⎛ ⎞
(b) 010 a
A =⎝ 1 0 1 ⎠ b
100 c
abc
⎛ ⎞
(c) 010 a
A2 =⎝ 1 0 1 ⎠ b
100 c
25 35
10 27 32 40
10.18 Exercises
(b) Show that the sum of degrees of vertices theorem holds for the above graph.
(c) State the value of:
radius(G) diameter(G)
10.18 Exercises 185
11.1 Chapter 1
A B
a f
6. b d
g
c e
h
i j
AΔ B = A\B ∪ B\A
7. = {pear, banana, plum, lemon} ∪ {mango }
= {pear, banana, plum, lemon, mango}
© The Author(s), under exclusive license to Springer Nature Switzerland AG 2025 189
Q. Charatan and A. Kans, Maths For Computing, Undergraduate Topics in Computer
Science, https://doi.org/10.1007/978-3-031-69234-5_11
190 11 Solutions to Exercises
B C
8. (a)
4
11
8
7
(b)
(i) n(B ∩ C) = 4
(ii) n(B ∪ C) = 23
(iii) n(B\C) = 11
(iv) n(B ∪ C) = 7
U U
A B A B
9.
A\B
10. P(A) = {∅, {x}, {y}, {z}, {x, y}, {x, z}, {y, z}, {x, y, z}}
11. (a) Number of elements in the power set = 24 = 16
(b) Number of proper subsets contained in the power set = 15
11.2 Chapter 2
( ) ( )
A ∪ A ∩ B = A ∪ A ∩ (A ∪ B) DistributiveLaw
1. = U ∩ (A ∪ B) ComplementLaw
=A∪B IdentityLaw
11.2 Chapter 2 191
( ) ( ) ,
B ∩ A ∩ B =( B ∩ A) ∪ (B ) De Morgan s Law
= (B ∩ A) ∪ B ∩ B Distributive Law
2.
= B∩A ∪∅ Complement Law
=B∩A Identity Law
( )
B ∪ (A\B) = B ∪ A ∩(B ) From previous chapter
= (B ∪ A) ∩ B ∪ B Distributive Law
3. (a)
= (B ∪ A) ∩ U Complement Law
=B∪A Identity Law
U U
A B A B
(b)
A\B B
(2−3i)(4 + i) = 8 + 2i−12i−3i2
(b)
= 11−10i
(3 −i)2 = 9−6i + i2
(c)
= 8−6i
192 11 Solutions to Exercises
( )
(1 + i)3 = (1 + i)(1 + i)2 = (1 + i) 1 + 2i + i2
= (1 + i)(1 + 2i − 1)
(d) = 2i(1 + i)
= 2i + 2i2
= −2 + 2i
(e) Multiply numerator and denominator by (1 – i)
3 + 4i (3 + 4i)(1 − i)
=
1+i (1 + i)(1 − i)
7+i 7+i
= =
1 − i2 2
Is there closure?
It is closed because the result of adding any two natural numbers is always
a natural number.
Therefore N0 under addition has an identity, closure and associativity so is
a monoid but not a group.
10. Is there an identity element?
There is no identity element (0 is not in the group).
Are there inverses?
There are no inverses because there is no identity element.
Is the operation associative?
Addition is associative with integers.
Is there closure?
It is closed because the result of adding any two natural numbers is always
a natural number.
Therefore N1 under addition has associativity and closure and is a semi-
group but not a group.
11.3 Chapter 3
1
x
2
2. y
3
A B
3. R−1 = {(2, a), (9, d ), (4, b), (7, c), (1, a)}
4.
(a) It is not symmetric because if a < b, then b ≮ a. For example 4 is less than
5, but 5 is not less than 4.
(b) It is not reflexive because in no case is a < a.
(c) It is transitive, because in every case if a < b and b < c, then a < c. For
example 3 is less then 5 and 5 is less than 6 – of course 3 is also less than
6, and this is true in every case.
5.
/ 1.
(a) It is not reflexive because 1 R
(b) It is not symmetric because, for example, 1 R 3 but 3 R/ 1.
(c) It is transitive because in every case if a R b and b R c then a R c .
194 11 Solutions to Exercises
11.4 Chapter 4
1.
(a) This is not a function because d maps on to two elements in the codomain.
(b) This is a function.
(c) This is not a function because b has no image in the codomain.
2. (a) f (u) = 1 (b) f (v) = 2 (c) f (w) = 2 (d) f (x) = 4
3.
(a) f (3) = 4 × 32 −5 = 4 × 9 − 5 = 31
(b) f (−1) = 4 × (−1)2 −5 = −1
(c) f (0) = −5
4.
(a) f (2, 0) = 2 × 22 + 3 × 0 = 8
(b) f (1, −1) = 2 × 12 + 3 × −1 = −1
5. g(3) = 3 × 3 + 1 = 10
f (10) = 103 = 1000
6. f : Z × Z → Z
f (x, y) = 2(x + y)
7.
(a) This is both injective and surjective (it is a bijective function).
(b) This is surjective, but not injective.
(c) This is surjective, but not injective.
(d) This is neither injective nor surjective.
8.
(a) It is not a surjective function, because the negative numbers in the codomain
are not images of any number in the domain.
(b) It is not an injective function, because more than one element from the
domain maps to the same element in the codomain. For example, both 2
and −2 map onto 4.
11.5 Chapter 5
1. Pis true
Qis false
Ris false
(a) P ∧ R ≡ T ∧ F ≡ F
(b) P ∨ Q ≡ T ∨ F ≡ T
(c) Q ∨ R ≡ F ∨ F ≡ F
2.
(a) It is not summer.
(b) It is summer and Leon is not playing tennis.
(c) It is not summer or Leon is playing tennis.
(d) Leon is playing tennis.
11.5 Chapter 5 195
3.
(a) Q ∧ ¬P
(b) P∧Q
(c) P ∨ ¬Q
(d) ¬(P ∨ Q)
4.
5.
P Q P∨Q P ⇒ (P ∨ Q)
T T T T
T F T T
F T T T
F F F T
6.
(a) ¬Q ⇒ P
(b) ¬P ⇒ ¬ ¬Q ≡ ¬P ⇒ Q
(c) ¬ ¬Q ⇒ ¬P ≡ Q ⇒ ¬P
P ⇒ (P ∨ Q) ≡ ¬P ∨ (P ∨ Q) Identity 1
≡ (¬P ∨ P) ∨ Q ∨ is associative
10.
≡T∨Q Complement Law
≡T Identity Law
196 11 Solutions to Exercises
11.
P Q ¬Q P ∨ ¬Q
T T F T
T F T T
T Undefined Undefined T
F T F F
F F T T
F Undefined Undefined Undefined
Undefined T F Undefined
Undefined F T T
Undefined Undefined Undefined Undefined
11.6 Chapter 6
Proof
1. D ⇒ C Premise
2. C ⇒ T Premise
3. D Premise
4. D ⇒ T Chain rule on 1 and 2
5. T Modus Ponens on 3 and 4
6. Define the following predicates: C(x): x is a cat.
T (x): x likes watching television.
the following proposition: P: the moon is made of cheese.
and the following constants: b: Bernard
s: Susan
Proof
Proof
1. S(SAM ) Premise
2. B(SAM ) ⇒ P Premise
3. ∀x • (S(x) ⇒ B(x)) Premise
4. S(SAM ) ⇒ B(SAM ) Universal Instantiation on 3
5. B(SAM ) Modus Ponens on 1, 4
6. P Modus Ponens on 2, 5
198 11 Solutions to Exercises
8. Base step
2 + 22 + 23 + 24 + . . . 2k = 2k+1 − 2 (11.1)
2 + 22 + 23 + 24 + . . . 2k + 2k+1
11.7 Chapter 7
( ) ( )
−2 + 1 3 + 2 5 + 7 −1 5 12
1. (a) A + B = =
1 + 3 −2 + 8 9 + 4 4 6 13
( ) ( )
−2 − 1 3 − 2 5 − 7 −3 1 −2
(b) A − B = =
1 − 3 −2 − 8 9 − 4 −2 −10 5
( ) ( ) ( )
−4 6 10 3 6 21 −1 12 31
(c) 2A + 3B = + =
2 −4 18 9 24 12 11 20 30
⎛ ⎞
−2 1
(d) AT = ⎝ 3 −2 ⎠
5 9
( )
( ) 41
−2 3 ×
23
2. ( )
= −2 × 4 + 3 × 2 −2 × 1 + 3 × 3
=(−2 7)
( ) ( )
2×1+7×0 2×5+7×2 2 24
3. A × B = =
1×1+3×0 1×5+3×2 1 11
11.7 Chapter 7 199
det(A) = (1 × −2)−(4 × 3)
4. = −2−12
= −14
det(A) = 4 × 3−1 × 2 = 10
( ) ( )
5. (a) 1 3 −2 0.3 −0.2
A−1 = =
10 −1 4 −0.1 0.4
A×X =D
6.
∴ X = A−1 × D
( )
0.3 −0.2
From the previous question we know that: A−1 =
−0.1 0.4
( )
( )
0.3 −0.2 2
∴X = ×
−0.1 0.4 2
( )
0.3 × 2 + −0.2 × 2
=
−0.1 × 2 + 0.4 × 2
( )
0.2
=
0.6
7.
122 21 −12
x= y= z=
31 31 31
200 11 Solutions to Exercises
( | )
1 25 || 21
R2 − R1 → R2 2
0 − 21 | − 25
( | )
1 25 || 21
−2R2 → R2 2
0 1| 5
( | )
5 1 0 || −2
R1 − R2 → R1
2 0 1| 5
The solution is
11.8 Chapter 8
1. 5! = 5 × 4 × 3 × 2 × 1 = 120
8! × 5! 8×7×6×5×4×3×2×1×5×4×3×2×1
=
4! × 3! 4×3×2×1×3×2×1
2.
=8×7×6×5×5×4
= 33600
11.8 Chapter 8 201
n!
P(n, k) =
(n − k)!
10! 10!
P(10, 3) = =
(10 − 3)! 7!
3. (a)
10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1
=
7×6×5×4×3×2×1
= 10 × 9 × 8 = 720
n!
C(n, k) =
(n − k)!k!
9! 9!
C(9, 6) = =
(9 − 6)! × 6! 3! × 6!
(b)
9×8×7×6×5×4×3×2×1
=
3×2×1×6×5×4×3×2×1
9×8×7×6×5×4×3×2×1
=
3×2×1×6×5×4×3×2×1
4. This is the same as selecting 4 from 20 where order is significant and there is
no repetition, so this is a permutation.
The correct formula is:
n!
P(n, k) = where n = 20 and k = 4.
(n − k)!
20!
P(20, 4) =
16!
= 20 × 19 × 18 × 17 = 116280
10!
C(10, 3) =
7! × 3!
202 11 Solutions to Exercises
10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1
=
7×6×5×4×3×2×1×3×2×1
10 × 9 × 8
= = 120
3×2×1
7×6×5×4×3×2×1
=
5×4×3×2×1×2×1
7×6
= = 21
2×1
12 × 11 × 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1
=
3×2×1×9×8×7×6×5×4×3×2×1
12 × 11 × 10 × 9 × 8 × 7 × 6 × 5 × 4 × 3 × 2 × 1
=
3×2×1×9×8×7×6×5×4×3×2×1
63 = 216
nk = 63 = 216
6!
P(6, 3) =
3!
11.8 Chapter 8 203
6×5×4×3×2×1
=
3×2×1
= 120
(c) This is the same as putting the 3 at then end and arranging the other 5 digits
in the first two slots:
5!
P(5, 2) =
3!
5×4×3×2×1
=
3×2×1
= 20
4! = 4 × 3 × 2 × 1 = 24
If Tracey were to sit at one end, then we just have to arrange the other
three. The number of ways of doing this is:
3! = 3 × 2 × 1 = 6
If she sits at the other end there are also 6 ways of arranging the others.
So there are 12 ways of arranging people if Tracey sits at the one or other
end.
So the number of ways of arranging them if Tracey does not sit at the end
is:
24−12 = 12
10.
4!
C(4, 2) =
2! × 2!
4×3×2×1
= =6
2×1×2×1
∑
n
12. (a + b)n = nCk an−k bk
k=0
In this case, a = x and b = 2y
n = 6, and because we start at zero, the third term will be given when k =
2.
6
C2 (x4 ) (2y)2
The third term is therefore: = 15 (x4 ) (4y2 )
= 60x4 y2
11.9 Chapter 9
5. (a) Let the probability of the spinner landing on orange be x. The probability
of it landing on green is 2x. The probability of it landing on blue is 3x. The
probability of it landing on yellow is 4x. The probability of it landing on red is
10x.
10x + 4x + 3x + 2x + x = 1
20x = 1
x = 0.05
206 11 Solutions to Exercises
(b) The probability of the spinner landing on blue or green = P(blue) + P(green)
= 0.25
(continued)
Spinner 1 Spinner 2 Number of yellows
blue red 0
blue yellow 1
blue blue 0
Probability distribution:
No of yellows thrown, x 0 1 2
Number of occurrences 4 4 1
P(X = x) 4/ 4/ 1/
9 9 9
Therefore: E(x) = 0 × 4
9 + 1 × 49 + 2 × 1
9 = 2
3
4
The probability of picking a blue ball is: 15
6
The probability of picking a red ball is: 15
The probability of picking a blue ball followed by a red ball is:
4 6 4 2 8
× = × =
15 15 15 5 75
6 5 2 5 1
× = × =
15 14 5 14 7
208 11 Solutions to Exercises
(c) (i)
(ii) The probability of picking a red ball followed by a yellow ball OR a yellow
ball followed by a blue ball is:
1 2 3+2 5
+ = =
7 21 21 21
n = 12
k= 6
In this case: n − k = 6
p = 0.25
q = 0.75
p(2) = 0.1
p(3) = 0.1
p(4) = 0.1
p(5) = 0.3
p(6) = 0.3
11.10 Chapter 10
a, e6 , f , e5 , b, e3 , d , e4 , c, e2 , a
abcd e
⎛ ⎞
a 01704
b ⎜
⎜1 0 0 6 7⎟
⎟
5. ⎜
c ⎜7 0 0 5 0⎟ ⎟
d ⎝0 6 5 0 0⎠
e 47000
6. Arrange the edges in order of weight:
ac de bc ab cd ad
1 2 3 4 5 7
11.10 Chapter 10 211
8. (a)
b
(b) deg(r1 ) = 3 deg(r2 ) = 3 deg(r3 ) = 6
(c) V = 5 E = 6 R = 3
V −E+R= 5−6 + 3 = 2
abcd
⎛ ⎞
0 1 0 0 a
⎜1 0 1 0⎟
(b) A =⎜ ⎟b
⎝1 0 0 1⎠ c
1 0 1 0 d
abcd
⎛ ⎞
1 0 1 0 a
⎜ 1⎟
(c) A2 =⎜ 1 1 0 ⎟b
⎝1 1 1 0⎠ c
1 1 0 1 d
Index
A Conjunction, 67
Addition rule, 136 Connected graph, 159
Adjacency matrix, 158 Connectives, 66–70
Adjacent vertices, 158 Contradiction, 75–76
Algebra of propositions, 77–79 Contrapositive, 76–77
Algebra of sets, 22 Converse, 76–77
AND-elimination, 93 Countable sets, 26
AND gate, 81 Cycle, 164
AND-introduction, 93
AND operator, 66–67
Associative operations, 28 D
Databases, 46
Degenerate binary tree, 175
B Degree of a vertex, 160
Balanced binary tree, 174 De Morgans law, 16, 70–72
Bayes’ theorem, 147–150 Determinant, 105–107
Bernouilli trial, 150, 151 Diameter of a graph, 162
Bernoulli, 150 Difference, 9
Bijective functions, 60–61 Digital electronics, 80
Binary relations, 45 Digraph, 180–182
Binary trees, 173–178 Directed graph, 180–182
Binomial expansion, 127 Disjunction, 68
Binomial probability, 150 Distance between vertices, 160
Domain of discourse, 86
C
Cardinality, 5 E
Cartesian product, 10 Eccentricity, 161
Chain rule, 92 Edge, 158
Circuit, 164 Empty set, 5
Classes of sets, 16 Equivalence classes, 44
Combination definition, 120 Equivalence operator, 73–74
Combinatorics, 117 Equivalence relations, 43–44
Commutative operations, 27 Euler’s number, 23
Complement, 9–10 Eulerian graph, 167
Complete binary tree, 174 Events, 132
Complex numbers, 3, 24–26 Exclusion principle, 8
Conditional probability, 145–147 EXCLUSIVE OR operator, 76
© The Editor(s) (if applicable) and The Author(s), under exclusive license 213
to Springer Nature Switzerland AG 2025
Q. Charatan and A. Kans, Maths For Computing, Undergraduate Topics in Computer
Science, https://doi.org/10.1007/978-3-031-69234-5
214 Index
G
Gauss-Jordan elimination method, 113–114 N
Graph definition, 158 N-ary relations, 45
Group definition, 30 Natural deduction, 91–96
Natural numbers, 3, 23
Negation, 68
H Node, 158
Hamiltonian circuit, 167
NOT gate, 80
Hamiltonian graph, 167–169
NOT operator, 68
Homeomorphic graph, 165
O
I
One-to-one functions, 60
Identity element, 30–31
Onto functions, 60
Identity matrix, 107
Order of precedence of logical operators, 74
Implication operator, 72–73
OR gate, 81
Independent events, 141
OR-introduction, 94
Induction, 96–98
Injective functions, 61 OR operator, 67–68
Inorder traversal, 176–177 Outcome, 132
Integers, 3, 23
Intersection, 8–9
Inverse of a group, 30–31 P
Inverse of a matrix, 107 Pascal’s Triangle, 126–127
Inverse relation, 39 Path, 163–165
Isomorphic graph, 165 Perfect binary tree, 174
Permutation definition, 119
Planar graph, 178–180
K Postorder traversal, 177
Kriskal’s algorithm, 171–173 Power sets, 16–17
Predicate definition, 86
Preorder traversal, 177
L Probability definition, 133
Linear equations, 110–111 Probability distribution, 139–140
Logical equivalence, 70, 74 Proof by induction, 96–98
Logical operators, 66–70, 73–74 Proper subset, 7
Logic gates, 80–81 Proposition definition, 66
M Q
Map, 178 Quantification, 87–90
Index 215
R T
Radius of a graph, 162 Tautology, 75–76
Random variables, 142–143 Three-valued logic, 81–82
Rational numbers, 3, 23 Trail, 164
Real numbers, 3, 24 Transitive relations, 43
Reflexive relations, 42–43 Transposition, 102
Region, 178–180 Traversable graph, 166–169
Relational databases, 46 Traversing a binary tree, 176–178
Relation definition, 37, 40 Tree diagrams, 145–147
Row operations, 112 Trees, 170–178
Rule of contraposition, 93 Truth tables, 67–70, 71–74
Rule of detachment, 93
Rule of syllogism, 93
Russell’s paradox, 17 U
Unconnected graph, 159
Union, 8
S Unique existential quantifier, 88
Sample space, 132 Universal instantiation, 94
Scalar multiplication, 103 Universal quantifier, 87
Set algebra, 22 Universal set, 4–5
Set comprehension, 4
Set definition, 2
Spanning trees, 171–173 V
Subgraph, 163 Venn diagrams, 11–16
Subset, 6 Vertex, 158
Substitution, 87
Surjective functions, 60
Symmetric difference, 13 W
Symmetric relations, 42–43 Weighted graph, 169–170