Acknowledgements
These notes, mainly inherited from a former colleague Kate Kennard, draw
heavily from a course originally written by Cathy Smallwood. Some of the ideas
and notation were taken from Data Abstraction and Program Development Using
Pascal by R. Hille (now out of print). The sections on combinatorics draw ideas
and notation from Biggs, N, Discrete Mathematics and from my dear colleague
Nino Folic.
Zhanyuan Hou
Week 1
(Introduction, some revision on functions and operators, recursive definitions of
functions, pseudocode, revision of proof)
Introduction
This module largely concerns recursion and mathematical induction in relation to the
natural numbers, lists and trees.
Additionally we will study introductory
combinatorics or `techniques of counting'. The ideas considered underlie various
aspects of data structures, algorithm design and validation of program correctness in
computer science. Recursion, once understood, is a simple but powerful idea which
can be used in producing computer algorithms for solving complex problems.
Mathematical induction is not only a standard mathematical proof technique but may
also be employed to prove the correctness of an algorithm.
In the study of theses mathematical topics it is intended that students will practise and
appreciate the necessity for rigour in mathematical expression in order to build ideas
one upon another and to communicate them to others.
Recursion is particularly pertinent when programming in a functional programming
language such as Miranda, Hope, ML or Haskell. In functional programming, only
functions may be used so that the only means of effecting repetition is recursion (and
side effects are avoided). It is a style of programming in which the mathematical
notation and properties of algorithms are clearly evident in the code. This makes
functional programs easily susceptible to the application of mathematical logic and
proof of their correctness. Learning functional programming can aid understanding
and broaden a students approach to programming.
This course will not cover coding in a functional programming language but will look
at some of the underlying mathematics and the development of algorithms so that on
its completion learning a functional programming language should be relatively
straightforward.
1
Revision of basics
Fill in the gaps in the following:
FUNCTIONS
Definition Let S and T be non-empty sets. A function f from S to T is
a rule which associates with each element s S a unique element t T .
We write
S is called the
f: ST
domain
T is called the co-domain
NB
of f.
of f.
S and T may be equal.
If s S we denote by f(s) the unique element of T which f associates with
s.
f(s) is the value of f at s. f(s) is also called the image of s and the set of all
images is called the range of f.
Give a diagrammatic representation of a function f from S to T and use the
diagram to illustrate the fact that, for some s S and some t T, f(s) = t.
Examples
1. f : {2,4,6,8} {2,4.6.8} given by f(n) = n for all n {2,4,6,8}.
2. g : N N given by g(n) = 2n, for all n N = {0,1,2, …}.
3. h : N {k} given by h(n) = k. (i.e. h(n) is a constant, k)
ALGEBRA Recall familiar arithmetic operators * and +
(a * b) * c = a * (b * c)
(a + b) + c = a + (b + c)
both * and + are associative.
a + b = b + a, a * b = b * a
c * (a + b) = c * a + c * b
both * and + are commutative.
multiply is distributive over plus.
2
Recursively defined functions
We can define functions in a variety of ways, in particular they can be defined:
i)
ii)
explicitly
recursively or inductively ( in terms of previous values).
In what follows, fi : N N for i = 1, 2, … ,6, where N = { 0,1, 2, …}.
i)
Explicitly defined:
a)
f1(n) = n
b) f2(n) = 3n
c)
f3(n) = 2n
d) f4(n) = n!
ii)
e)
f5(n) = 3n * n!
f)
f6(n) = 5n * n!
Recursively or inductively:
A definition in which the item being defined appears as part of the definition is
called a recursive or an inductive definition. This approach works because
there are always two parts to a recursive definition:
i)
ii)
base case(s) or value(s), some instances of the item being defined are
given explicitly
recursive or inductive step, new cases or values of the item are given in
terms of previous ones.
i) allows us to start by giving some simple, actual values to work with.
ii) gives us the means of constructing new values from the simple ones as well
as a means of constructing further values from these new ones and so on.
Example: f(0) = 0,
f(n) = f(n-1) + 1 for n > 0.
Note The essential characteristic of a recursive definition is ‘self-reference’.
In programming, a recursive program, procedure or function is one which can
call itself.
3
To take an explicitly defined function and write it in recursively defined form.
Method a.
Steps
1. Write down the explicit
definition of f(n).
Example (f : N N where N = { 0,1, 2, …}.)
As an example we look at the function n! :
f(n) = n!
2. Write out the base case
In the above example: f(0) = 0! = 1
3. Write out f(n-1) and state
values of n for which valid.
In our example: f(n-1) = (n-1)!, n > 0.
4. Write f(n) = r.h.s. of f(n) in
terms of the r.h.s. of f(n-1).
5. Substitute in f(n-1), state valid
values of n. Don’t forget base case.
In our example: f(n) = n! = (n-1)! * n
Further Example of Method a.
In our example:
f(0) = 1, f(n) = f(n-1) * n, n > 0.
f2 : N N where N = { 0,1, 2, …}, f2(n) = 3n.
f2(n) = 3n
f2(0) = ………………………..
f2(n-1) = …………………………………………….
f2(n) = ………………………………………….
Thus solution is …………………………………………………………………………
Method b.
Write out base case, when n = 0 (or n = 1, as appropriate) and then successive cases,
n = 1, n = 2, …. And each time try to write the latest case in terms of the previous
case until a pattern emerges and then generalise for case n; ie write f(n) in terms of
f(n – 1). Don’t forget to state the base case.
Example f : N N, where N = { 0,1, 2, …}.
f(n) = n!
f(0) = 0! = 1
f(1) = …………………………………………….
f(2) = ……………………………………………….
f(n) = ………………………………………………….
Thus solution is ………………………………………………………………………….
4
Examples We give recursive definitions of the functions defined explicitly above:
a)
f 1(0) = 0
,
f 1(n) = f 1(n-1) + 1 ,
n>0
.
OR can write f 1(n) = { 0 , if n = 0
{ f 1(n-1) + 1 , if n > 0
b)
f 2(0) = ………………,
f 2(n) = ………………………………..,
c)
f 3(0) = ……………,
f 3(n) = …………………………………..,
d)
f 4(0) = 1
,
n>0
.
f 5(0) = …………..,
f 5(n) = …………………………………,
f)
……………….
,
f 4(n) = f 4(n-1) * n
e)
………………...
f 6(0) = 1
…………..
,
f 6(n) = f 6(n-1) * 5 * n
,
n>0 .
Pseudocode – We adopt a computer like language for expressing algorithms to
calculate the values of functions. This is really just a more precise use of the English
language. For example we could give instructions for recursively calculating the
factorial n function, f: N N, f(0) = 1, f(n) = f(n-1) * n, n > 0, by saying:
FUNCTION Fact: natno natno
BEGIN
IF n = 0 THEN Fact(n) := 1
ELSE Fact(n) := Fact(n - 1) * n
END
Note the symbol := is the operator 'becomes'.
The name of this function is 'Fact' - notice how the instructions for evaluating Fact
require the use of the function 'Fact'.
5
Rules of addition:
mN
Ai
m+0
= m
Aii
m + (n + 1) = (m + n) + 1
m, n N
Consider a function m_Plus : N N to add n onto m,
m_Plus(n) = m + n, n N and any m N is the explicit form of m_Plus.
If we think of m as some fixed natural number then Ai and Aii give recursive
definitions for the addition of any natural number to m. Let m_Plus : N N be
given by
or
m_Plus (0)
=
…………………………………………………………
m_Plus (n)
=
……………………………………………………..
m_Plus (n + 1) =
……………………………………………………….
Similarly we have:
Rules of multiplication:
Mi
m*0= 0
mN
Mii
m * (n + 1) = (m * n) + m
m, n N
Let Mult_m_by : N N be given by
or
Mult_m_by (0) =
……………………….
Mult_m_by (n) =
……………………………………………..
Mult_m_by (n + 1) = ………………………………………..
6
What is a proof?
In general terms, a proof in mathematics consists of establishing the truth of a
statement by logical processes from the truth of given statements (axioms and
definitions). We must be able to justify each logical step we take and to cite which
given statement, axiom or definition the step relies upon.
For example:
Consider: What is
Given:
a) Milk is always white.
b) The liquid in my glass is red.
To Prove:
c) The liquid in my glass is not milk.
Proof:
From b),
conclude c)
by using a).
1 + 1?
Whilst we may have an answer in an applied context in the real world, we cannot say
correctly in mathematics and give a proof of our answer without defining what “1”
and “+” mean.
Given
The set
Bin = {0, 1}
function
succ: Bin Bin
defined by,
succ(0) = 1 … Si
succ(1) = 0 … Sii
an operator
plus (or +): Bin x Bin Bin
defined by,
x + 0 = x , x Bin … Ai
x + succ(y) = succ(x + y), x,y Bin … Aii
To prove:
1+1=0
Proof:
LHS = 1 + 1 = …………………., definition of succ, Si
= ………………….. , definition +, Aii
= …………………. , definition +, Ai
= ………………… , definition succ, Sii
= RHS.
Can you think of an application for this?
7
QED (which means it is proven)
© Copyright 2026 Paperzz