Anda di halaman 1dari 196

i

WELCOME
Discrete Mathematics consists of material from this booklet, Lecture Notes, and from the
booklet, Tutorials and Practice Classes: Questions. Each of the 24 sections in these notes is
approximately equivalent to one lecture and you should aim to cover, on average, between 2
and 3 sections per week. The Tutorials present new material and so require additional time to
master. The Practice classes consist of exercises related to material in the Lecture Notes.

H N

In the following, there are a few passages marked on the margin by the and pair
(just like the pair on the right hand side of this paragraph), and a few paragraphs marked by the

symbol. These marked parts are optional for this unit; they are presented for those who
wish to pursue further understanding of the topics concerned.

There are a number of places where subjects are explained in great details (which can at
times even be irritating to some) and often in less rigorous informal terms. Such coverage is to
help bridge the more intuitive understanding. To differentiate such text from the normal ones,
we will always place such informal detailed exposition between a pair of and symbols,
just like the pair on the right hand side of this paragraph.

5 4

There are two optional reference books if you feel the need for extra reading and insights.
The books are
Ralph Grimaldi, Discrete Mathematics and Combinatorial Mathematics, 4th edition, AddisonWesley, 1999.
Susanna Epp, Discrete Mathematics with Applications, 2nd edition, PWS Publishing Company, 1995.
Information on the link between Grimaldis book and the topics in this unit can be found a
few pages on in this booklet.
These reference books are optional since this unit is entirely covered by the booklets: Lecture Notes and Tutorial and Practice Classes.
I wish to acknowledge the work of Dr Chris Radford and Dr Zhuhan Jiang. The material in
the booklets is their work but all the errors and omissions are mine.
Best wishes in your studies.

D R T IM DALBY

N
5

ii
Applied Mathematics 140 Discrete Mathematics

L ECTURE N OTES

P RINTED

IN

N OVEMBER 2001

CONTENTS

Contents
Reading Guide

Preliminary Mathematics

Emergency Kit

14

15

Sets
1.1

Basic Set Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

1.2

Set Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

18

1.3

Russels Paradox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

19

Mathematical Induction

20

2.1

Principle of Mathematical Induction . . . . . . . . . . . . . . . . . . . . . . .

21

2.2

Strong Principle of Mathematical Induction . . . . . . . . . . . . . . . . . . .

25

Efficiency, Big O

28

3.1

Nested Evaluation of a Polynomial . . . . . . . . . . . . . . . . . . . . . . . .

28

3.2

Big O Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

29

3.3

Simple Features of O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

32

Notation

33

4.1

Definition of the Notation . . . . . . . . . . . . . . . . . . . . . . . . . . .

33

4.2

Examples in Exaggerated Details . . . . . . . . . . . . . . . . . . . . . . . . .

35

Analysis of Algorithms

40

5.1

Sequential Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

40

5.2

Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

41

5.3

Complexity of Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . .

43

5.4

Performance of Sorting Algorithms . . . . . . . . . . . . . . . . . . . . . . . .

44

Symbolic Logic

44

6.1

44

Basic Concepts . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

CONTENTS

2
6.2

Logical Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

46

6.3

Conditional Statements . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

47

7 Propositional Logic

48

7.1

Argument Forms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

48

7.2

Converse, Inverse and Contrapositive . . . . . . . . . . . . . . . . . . . . . . .

50

7.3

Rules of Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

51

8 Predicate Calculus

58

8.1

Predicate Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

58

8.2

Predicate Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

60

9 Graphs and Their Basic Types

64

9.1

Graphs and Directed Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . .

64

9.2

Basic Types and Features of Graphs . . . . . . . . . . . . . . . . . . . . . . .

65

9.3

Planar Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

68

10 Eulerian and Hamiltonian Circuits

69

10.1 Two Practical Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

69

10.2 Eulerian and Hamiltonian Paths and Circuits . . . . . . . . . . . . . . . . . . .

70

10.3 Fleurys Algorithm for Finding Eulerian Path or Circuit . . . . . . . . . . . . .

73

11 Graph Isomorphism and Matrix Representations

74

11.1 Graph Isomorphism and Isomorphic Invariants . . . . . . . . . . . . . . . . .

74

11.2 The Adjacency Matrix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

76

12 Trees

80

12.1 Trees, Rooted Trees and Binary Trees . . . . . . . . . . . . . . . . . . . . . .

80

12.2 Traversal of Binary Trees . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

83

13 Spanning Trees
13.1 The Concept of Spanning Trees . . . . . . . . . . . . . . . . . . . . . . . . . .

86
86

CONTENTS

13.2 Spanning Tree Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

88

13.3 Kruskals Algorithm . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

90

14 Number Bases

93

14.1 Frequently Used Number Systems . . . . . . . . . . . . . . . . . . . . . . . .

93

14.2 Conversion to Numbers of Different Bases . . . . . . . . . . . . . . . . . . . .

95

15 Relations
15.1 Binary Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

99
99

15.2 Reflexivity, Symmetry and Transitivity . . . . . . . . . . . . . . . . . . . . . . 101


16 Equivalence Relations

103

16.1 Equivalence Classes and Partitions . . . . . . . . . . . . . . . . . . . . . . . . 103


16.2 Property Closure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 107
17 Partial Order Relations

109

17.1 Partial Orderings . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109


17.2 Hasse Diagrams . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 110
17.3 Topological Sorting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 115
18 Switching Circuits and Boolean Algebra

120

18.1 Switching Circuits . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 120


18.2 Boolean Algebra . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121
18.3 Algebraic Equivalence . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 124
18.4 Sets connection with Boolean Algebra . . . . . . . . . . . . . . . . . . . . . . 125
19 Boolean Functions

126

19.1 Convert Boolean Function to Boolean Expression . . . . . . . . . . . . . . . . 126


19.2 Canonical Form . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 127
19.3 Minimal Representation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 128
19.4 Logic Gates . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 130

CONTENTS

4
20 Karnaugh Maps

132

20.1 Grid Layout for Karnaugh Maps . . . . . . . . . . . . . . . . . . . . . . . . . 133


20.2 Simplification Procedure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 135
21 Recurrence Relations

138

21.1 Recurrence Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 138


21.2 Basic Concepts Related to Recurrence Relations . . . . . . . . . . . . . . . . . 139
21.3 Simplest Case of General Solutions . . . . . . . . . . . . . . . . . . . . . . . 141
22 Solution of Linear Homogeneous Recurrence Relations

144

22.1 General Solutions for Homogeneous Problems . . . . . . . . . . . . . . . . . . 144


22.2 Ideas Behind the Theorem . . . . . . . . . . . . . . . . . . . . . . . . . . . . 147
23 Basics of Linear Nonhomogeneous Recurrence Relations

148

23.1 Connection between Homogeneous and Nonhomogeneous Problems . . . . . . 148


23.2 Examples of the First Glance . . . . . . . . . . . . . . . . . . . . . . . . . . . 149
24 Solution of Linear Nonhomogeneous Recurrence Relations

152

24.1 A Case for Thought . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 152


24.2 Method of Undetermined Coefficients . . . . . . . . . . . . . . . . . . . . . . 153
A Tutorials: Sorting Algorithms

160

A.1 Selection Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 160


A.2 Bubble Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161
A.3 Insertion Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 162
A.4 Merge Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 164
A.5 Quick Sort . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 165
Hints and Solutions to Supplementary Exercises

168

Index

189

READING GUIDE

Reading Guide
Students are only required to read and understand the material covered by both the Lecture
Notes and the booklets for Tutorial and Practice Classes, with the help of completing the prescribed assignments. Anything else is optional and will therefore not be contained in the final
examination.

General
It is quite likely that students can work through this unit without having to read any
reference books. Hence the rest of this reading guide is mainly for those who wish to,
or have already done so, get hold of a copy of the reference book cited below. However
we note that not all subjects in the reference book are covered in this unit, and those
covered, are not necessarily in the same order. This is only natural because the choice of
the subjects depends on the Schools unit syllabus.

Supplementary Exercises given at the end of each section are often relatively challenging
and are completely optional. They are presented here purely for those who wish to practice more. The hints and solutions to the supplementary exercises are appended to the end
of these Notes.
Should you encounter an unfamiliar terminology or symbol at any stage, always look up
the index first to see if you could quickly locate the needed information or explanations.
All textbooks do not contain the same topics and do not present the subjects in the same
order. For this unit, Grimaldis book seems to cover most of the units syllabus.

Reference Book
Ralph P Grimaldi, Discrete and Combinatorial Mathematics, 4th edition, Addison-Wesley,
1999.

Topics for Lectures, Tutorials and Practice Classes


1.
2.

Sets (x3.1, 3.2) x refers to a section in the above Grimaldis book.


Mathematical induction (x4.1)
( R , Z, N , partition, cartesian product )
( sequence, , , induction principle, binomial formula )
T-1: The bubble sort, see also subsection A. (tutorial-1)
P-1: Sets and mathematical induction (practice class-1)
Note Internal students will start the 1st tutorial and practice class all in the 2nd week.

3.

Efficiency, Big O (x5.7))

READING GUIDE

6
4.
5.

6.
7.

8.
9.

10.
11.

12.
13.

14.
15.

notation (x5.7 exercises)


Analysis of algorithms (x5.8)
( Horners algorithm, big O )
( notation, connection with big O )
( sequential/binary search, flooring, B(n) = O (log n) )
T-2: Divide and conquer, see also subsection A.4
P-2: Horners algorithm, O and notation
Symbolic logic (x2.1, 2.2)
Propositional logic (x2.3)
( tautology, logical equivalence, p ! q )
( valid argument, inference rules, p $ q )
T-3: Insertion sort and selection sort, see also subsections A.1 and A.3
P-3: Symbolic logic and logical equivalence
Predicate calculus (x2.4, 2.5)
Graphs and their basic types (x11.111.3)
( quantifiers, 8, 9, ), ,, iff, contrapositive )
( directed/sub/simple/complete graphs, Kn , Km;n , walks, Kuratowski theorem )
T-4: Quicksort, see also subsection A.5
P-4: Propositions and predicates
Euler and Hamiltonian circuits (x11.3, 11.5)
Graph isomorphism and matrix representation (x11.2)
( Eulers theorem, Fleurys algorithm, 7 bridge problem, travelling salesman problem )
( one-to-one, onto, isomorphic invariant, adjacency matrix)
T-5: Features of some graphs
P-5: Isomorphism and adjacency matrix
Trees (x12.1, 12.2)
Spanning trees (x12.1, 13.2)
( cycle, leaf, rooted tree, binary tree, traversal, postfix notation )
( weighted graph, Kruskals algorithm )
T-6: Kruskals algorithm
P-6: Binary trees for representation and sorting
Number bases (x4.3)
Introduction to relations (x5.1, 7.1)
( remainder theorem, division algorithm, blockwise conversion, octal/hex etc )
( reflexive, symmetric, transitive, m  n (mod d) )
T:
This is a phantom tutorial - catch up on your work
P-7 Number bases

READING GUIDE
16.
17.

Equivalence relations (x7.4)

Partial order relations (x7.3)


( equivalence class, property closure )
( antisymmetric, comparable, maximal element, Hasse diagram )

18.
19.

T:

This is a phantom tutorial - catch up on your work

P-8

Equivalence relations and partial order relations

Switching circuits and Boolean algebra (x2.2, 15.4)


Boolean functions (x15.1

( mapping, binary/unary ops, equivalence of Boolean/switching/symbolic.. )


( Boolean function to expression/switches, minimal representation )

20.
21.

T-7

Boolean algebra and voting machines

P-9

Switching circuits and their design

Karnaugh maps (x15.2)

Recurrence relations (x4.2, 10.1)

( 24, 44 blocks )

( iteration, recursion, characteristic eqn, solution for homogeneous eqn )

22.
23.

T-8

Logic gates and digital adders

This is a phantom practice class - catch up on your work

Solution of linear homogeneous recurrence relations (x10.2)

Basics of linear nonhomogeneous recurrence relations (R2: x10.3)


( repeated roots, proof hints )
( particular solutions for non-resonant cases )
T-9: More on Karnaugh maps
P-10: Solutions of homogeneous recurrence relations

24.

Solution of linear nonhomogeneous recurrence relations (R2: x10.3)


( particular solutions in general cases )
T:

Start your revision or see P-12 below

P-11: Solutions of recurrence relations and practical problems


*

Optional: For more extensive coverage on the solution of linear recurrence relations,
readers may consult chapter 11 of the book by H F Mattson, Jr, Discrete Mathematics
with Applications, John Wiley & Sons, 1993.
P-12: Supplementary Practice Class - Revision

READING GUIDE

Other References
There are other references to be mentioned in the Notes. You may consult them if you
have time, but you dont have to. In particular, there is an easy-to-read book by Susanna
S Epp, Discrete Mathematics with Applications, 2nd edition, PWS Publishing Company,
1995. However it does not cover as many topics as the reference book by Ralph Grimaldi.

Symbols in the Margins

H N

Passages marked on the margin by the and pair are for optional reading: they are
there to help those students who want to learn more. This optional material will not be
needed in either the assignments or the final examination.

Any paragraph whose margin is marked by the


symbol should be treated as if the
paragraph were marked by the and pair, implying reading the paragraph is again
optional.

H N

5 4

Passages marked on the margin by the and pair are usually examples deliberately
over-explained in informal and intuitive manners. Beware that such passages at times
may not be very rigorous.

Additional Information
Please be aware that additional reading guide or instructions may be available in the form
of handouts, which will provide more accurate and updated information. Latest online
information is available via the web at http://mcs.une.edu.au/amth140 .

PRELIMINARY

Preliminary Mathematics
This part serves as a simple reminder to the things you need to be aware of. It is basically
presented through a number of examples. Most of this part can be helpful at some stage of
studying this unit. However, you may choose to skip it initially and opt to get referred back
later in the unit.

Absolute Value
For any quantity a, its absolute value is denoted by jaj. Hence jaj = a if a  0, and jaj =
a  0. According to the definition of the absolute values, we have immediately

j23 6j = 23 6 j
:

: ;

23:6j = 23:6;

a if

7086j = 7086 :

Triangle Inequality
For any two numbers a and b one has always

ja  bj  jaj + jbj

ja  bj  jaj jbj

For example, it is easy to verify

3:5

0:2j  j

3:5j + j

0:2j;

3:5

0:2j  j

3:5j

0 :2 j

where we have taken a = 3:5 and b = 0:2. Likewise it is again trivial to verify

3:5

0:2j  j

3:5j

0:2j;

3:5

0:2j  j

0:2j

3 :5 j :

Generalised Triangle Inequality


Let x1 , x2 , ..., xn be n numbers. Then

jx1 + x2 +  + xn j  jx1j + jx2 j +  + jxn j jx1 + x2 +  + xn j  jx1j jx2j  jxn j
;

Hence it is easy to verify, for example,

3:5 + 2:1 0:2j  j 3:5j + j2:1j + j 0:2j;

where we have taken x1 = 3:5, x2 = 2:1 and x3 =

3:5 + 2:1 0:2j  j 3:5j

j2 1 j j
:

0:2j

0:2.

Powers and Logarithms


Let a, b and c be positive, and x and y be arbitrary real values. Then ax
x = loga y. Moreover,
axy = (ax )y ;

ax+y = ax  ay ;

(a

 b)x = (ax)  (bx)

y if and only if

a0 = 1 ;

PRELIMINARY

10
loga bx = x  loga b

loga (b  c) = loga b + loga c

loga 1 = 0 :

Hence we have for example


5 7
57
35
(2 ) = 2
=2 ;

25  27 = 25+7 = 212 ;

log2 29 = 9 :

log2 2 = 1 ;

Trigonometric Properties
Let x be any real number. Then

j sin xj  1

sin2 x + cos2 x = 1 ;

j cos xj  1

We note that the 2 inequalities can be derived easily from the 1st identity. Recall also that
sin(k) = 0 and cos(k) = ( 1)k hold for any integer k.

Roots of Quadratic Equations


A 2nd order polynomial equation
a 6= 0

ax2 + bx + c = 0 ;
has 2 roots x1 and x2 , given precisely by

x1 =

b + b2
2a

4ac

x2 =

b2
2a

4ac

Example
1. The roots of 3x2 + x

2 = 0 are given by
1

x1;2 =

4  3  ( 2)
23

(1)2

Hence x1 = ( 1 + 5)=6 = 2=3 and x2 = ( 1

15
6

5)=6 = 1.

Factorising Quadratic Polynomials


A 2nd order polynomial equation
ax2 + bx + c = 0 ;

a 6= 0

has 2 roots x1 and x2 if and only if the polynomial can be factorised into the following form
ax2 + bx + c  a(x

x1 )(x

x2 ) :

PRELIMINARY

11

Example
2. We have shown in example 1 that equation 3x2 + x
x2 = 1. Thus we have
3x2 + x

2
(x
3

23 x

2 = 0 has 2 roots x1

1))  (3x

2=3 and

2)(x + 1)

which can easily be verified directly.

Root and Factor Equivalence


For an n-th order polynomial
Pn (x) = an xn + an

n 1
+
1x

 + a1x + a0

an 6= 0 ;

x = x0 is a root of the equation


Pn (x) = 0
if and only if Pn (x) has (x

x0 ) as a factor, i.e. Pn (x) can be written as


Pn (x) = (x

x0 )Q(x)

for certain polynomial Q(x).


Examples
3. Let a 3rd order polynomial P(x) be given by
P(x)  6x3

x2

5x + 2 :

Since it is easy to verify that x0 = 1=2 is a root of P(x) = 0 because


P(x0 ) = P(1=2) = 6

1 3
2

1 2
2

1
+2 = 0 ;
2

we expect that we will be able to write P(x) as P(x) = (x 1=2)Q(x) for a polynomial
Q(x). In fact this Q(x) can be shown to be Q(x) = 6x2 + 2x 4. Hence we have
P(x)  6x3

x2

5x + 2  x

1 2
(6x + 2x
2

4) :

4. From examples 2 and 3 we have 6x2 + 2x 4 = 2(3x2 + x 2) = 6(x 2=3)(x + 1) and


hence
1
2
P(x)  6x3 x2 5x + 2  6 x
x
(x + 1 ) :
2
3
Therefore the 3 factors (x 1=2), (x 2=3) and (x + 1) will give 3 roots of P(x). They
are x1 = 1=2, x2 = 2=3 and x3 = 1.

PRELIMINARY

12

5. How did we find the factor Q(x) in example 3? We used the so-called long division.
It is basically a generalisation of the normal division process. So if we divide P(x) =
6x3 x2 5x + 2 by the factor (x 1=2) through the following long division

1
2

6x3
6x3

6x2

+2x

x2
3x2
2x2
2x2

5x

+2

5x
x
4x
4x

+2
+2

0
we obtain the factorisation
6x3

x2

1 2
(6x + 2x
2

5x + 2 = x

4) :

Elimination Method for Linear Equations


Examples
6. Solve the linear equations
x + 3y = 5;

y= 4:

2x

Solution Rewrite the 1st equation x + 3y = 5 as x = 5 3y and substitute it into the 2nd
equation 2x y = 4. Then the variable x in the 2nd equation is eliminated, reducing the
2nd equation to 10 7y = 4. The solution of this reduced equation gives y = 2 which,
when substituted back to the 1st equation via x = 5 3y = 5 3  2 = 1, gives the
solution for x. Hence the final solution is x = 1 and y = 2.
7. Solve the linear equations
2y

3z

10

2x + 3y

5z

y + 2z

3x
7x

Solution First we need to use choose one of the above 3 equations to eliminate one
variable from the other 2 equations. Since the 3rd equation looks simpler, we choose to
use the 3rd equation 7x y + 2z = 7 to eliminate variable y in the other 2 equations. The

PRELIMINARY

13

choice y here is actually arbitrary, though we did so here because the coefficient in front
of y there is the simplest. The 3rd equation is thus converted to
y = 7x + 2z

7:

Substitute the above into the other 2 equations, i.e.


3x

2y

3z = 10;

2x + 3y

5z = 1

3z = 10;

2x + 3(7x + 2z

we see that they are reduced to


3x

2(7x + 2z

7)

7)

5z = 1

which can be further simplified to


11x + 7z = 4;

23x + z = 22 :

We thus use the elimination procedure once again, this time similar to example 8, and
use z = 23x + 22 to eliminate the variable z in 11x + 7z = 4 which is then reduced to
11x + 7( 23x + 22) = 4, i.e. 150x + 150 = 0. Hence we obtain x = 1. Substitute
x = 1 back to 23x + z = 22 we obtain z = 1. Substitute x = 1 and z = 1 back to
7x y + 2z = 7 we obtain y = 2. It is perhaps worth noting that the solutions for each
individual variables are in the opposite order of elimination. We first eliminated y, then z.
So we first solve x, then z and then y. The final solution for the original problem is thus
simply x = 1, y = 2 and z = 1.

Binomial Expansions
The expansions for (a + b)n for the first few ns are
1

a+b

a2 + 2ab + b2

a3 + 3a2 b + 3ab2 + b3

a4 + 4a3 b + 6a2 b2 + 4ab3 + b4

(a + b)
(a + b)
(a + b)
(a + b)

These formulas can be obtained on the fly by observing the the following construction pyramid.
(a + b)1
(a + b)2

1
1

(a + b)3
(a + b)4
(a + b)5

..
.

1
1
1

2
3

1
1
3

10

10

1
5

..
.

Notice that each entry


in the
pyramid is no more than the sum of the 2 elements above it, see


for example 10 =
4 +
6 .

14

EMERGENCY KIT

Emergency Kit
Discrete Mathematics is a solid unit for a first year student. Although the subject is not
necessarily difficult, it does require students to spend a good deal of time in order to master
every aspect of the material covered in this unit. There will of course be highs and lows from
time to time when studying this unit. This is thus to remind you that some frustrations may
indeed be just an anticipated integral part of studying new concepts and methods. A simple
motto is that when confused or frustrated, take a rest and read the same subject once again in
the next few days. New concepts take time to sink in; hastily looking for additional reference
books (which are often more time-consuming or difficult to read) is not necessarily the best
strategy. In enough time, all will fall into their rightful places.
Mathematics is usually presented in quite a dense format; much new information is packed
into a small space. In addition there is new terminology to master plus some everyday words
take on new meanings in this context. So reading mathematics is not like reading most other
subjects. Thus if you dont understand something the first time through, the problem is most
likely not your fault. Mathematics usually needs to be read several times to gain understanding.
My advice is to briefly read through each section or subsection to get a feel for the topic then
read through again, slowly and try to gain understanding in small sections. A third or fourth go
may be necessary.
Remember that learning mathematics requires your active participation - to do is to learn. A
technique that I have found useful, is to always read mathematics with a pen in hand and paper
nearby. This is so that you can make calculations and try to fill in gaps between steps in a proof
say. That is, read not only with your eyes but with your hands. This technique could come in
handy if you decide to create your own glossary of terms - there is a lot of new terminology
coming up.

15

Sets

A set is a collection of elements. A set X has an element x is denoted by x 2 X . If a set X


doesnt have x as one of its elements, then we denote this by x 2
= X . A set can be defined by
its properties or by the enumeration of its elements. A set X may typically be defined in the
form of X = f element token j the properties the element token has to satisfy g, where j, or
alternatively :, serves as a separator. Hence, for instance, A = f1; 9; 13g defines A as the set
containing just 3 elements 1, 9 and 13; and B = fn j n is an odd integerg defines B as the set
of all odd integers. Alternatively the set B may be written as B = f2k + 1 : k is an integerg
or just B = f:::; 5; 3; 1; 1; 3; 5; 9; :::g through the complete enumeration. As a notational
convention,

I R is the set of all real numbers,


I Z is the set of all integers,
I N = fn 2 Zj n  0g is the set of all non-negative integers.
1.1 Basic Set Properties

I
I
I
I
I
I
I

B

A is a subset of B, denoted by A  B, means


8x, if x 2 A then x 2 B.


&

A = B means A  B and B  A

the difference of B minus A (or the relative


complement of A in B), denoted by B A or
BnA, is the set fx 2 B j x 62 Ag.

the union of A and B, denoted by A [ B, is the set


fx j x 2 A or x 2 Bg.
the intersection of A and B, denoted by A \ B, is the set
fx j x 2 A and x 2 Bg.

'

Let A and B be sets. Then

B
A
B
A

x is not an element of A is denoted by x 62 A, and B is not a subset of A is denoted by


B 6 A.

We note that a subset A of S is often defined in the form A = fx 2 S j P(x)g, or A = fx 2


S: P(x)g if you so prefer, in which predicate P(x) determines which x in S is to be accepted
in A. Also, the diagrams depicting the relationship among the sets, like the ones in the
above on the right, are called the Venn diagrams.

1 SETS

16
Examples
1.

f rose, daffodil, carnation g is a set of flower names.

2. Let A = f1; 2; 3; f4g; f1; 2gg and B = f1; 2; f4g; 5g, which are obviously both sets. Then

(a)
(b)
(c)
(d)

22A
4 62 A
f4g 2 A
f4g 6 A

(e)
(f)
(g)
(h)
(i)
(j)
(k)

f1 2g  A
f1 2g 2 A
B 6 A
A [ B = f1 2 3 f4g 5 f1 2 gg
A \ B = f1 2 f4gg
A B = f3 f1 2gg
B A = f5g
;
;

If all the sets we are interested in are subsets of a universal set U , then we can define for each set A its
complement, denoted by A0 ; by fx 2 U j x 62 Ag. We
note that some texts use A instead of A0 to denote the
complement of the set A. Hence
A = A0 = U

2 is an element of A
4 is not an element of A
f4g is an element of A
f4g is not a subset of A because the element 4 in f4g is not an element in A
1,2 are both elements of A
f1; 2g is an element of A
5 2 B but 5 62 A
union of A and B
intersection of A and B
set A minus set B
set B minus set A

A
"!

A:

There exists a unique empty set, denoted by ?, which is a set with no elements. Obviously ? is a subset of any set.
If S is a set, then the power set of S, denoted by P (S), is the set of all subsets of S, i.e.
P (S) = fX j X  Sg.

Two sets A and B are disjoint iff A \ B = ?.

A collection of nonempty sets fA1 ;  ; An g is a partition of a set A iff


(i) A = A1 [ A2 [[ An ,

(ii) A1 ;  ; An are mutually disjoint.

A2

A1
A3

A
A4

a typical partition

1.1 Basic Set Properties

17

Examples
3. The power set of A = f1; 2; 3g is

P (A) = f?; f1g; f2g; f3g; f1; 2g; f1; 3g; f2; 3g; f1; 2; 3gg :
4. Let A1 = f1; 2g and A2 = f3g, then fA1 ; A2 g is a partition of A = f1; 2; 3g because A1
and A2 are disjoint and A1 [ A2 = A. Let B1 = fx 2 R j x < 0g; B2 = fx 2 R j x  0g and
B3 = fx 2 R j x  0g. Then fB1 ; B2 g is a partition of R but fB2 ; B3 g is not a partition
because B2 \ B3 6= ?.
5. Let R be taken as the universal set and let Q be the set of rational numbers. Then the
complement of Q , i.e. Q 0 = fx 2 R j x 62 Qg is the set of irrational numbers.
For any given sets A1 ;  ; An , the Cartesian product of A1 ;  ; An , denoted by A1  A2 
 An , is the set of all ordered ntuples (a1 ;  ; an) where a1 2 A1;  ; an 2 An. In other words,
A1  A2  An = f(a1 ;  ; an ) j ai 2 Ai ; i = 1; 2;  ; ng

In the special case when all the Ai s are equal to each other, then we can use the shorthand
A

=A

}|

 A  A

Examples
6.
7.
8.

R R = R 2 = f(x y) j x y 2 Rg represents a plane.


f1 2gf3 4 5g = f(1 3) (1 4) (1 5) (2 3) (2 4) (2 5)g.
f1 2g = f2 1g but (1 2) =6 (2 1).
;

9. Suppose sets A and B are given respectively by


A = fa; fb; cgg;

B = fcg :

Give the Cartesian product A  B and the power set P (A) explicitly in terms of their
elements.
Solution The Cartesian product is
A  B = f (a; c); (fb; cg; c) g
and the power set is

P (A) = f?; fag; ffb; cgg; fa; fb; cggg :


We note that fb; cg is an element of the set A although this element fb; cg is itself a set.

1 SETS

18

1.2 Set Identities


Theorem Let A, B and C be subsets of a universal set U . Then
S1.
S2.
S3.
S4.
S5.
S6.
S7.
S8.
S9.
S10.

A[B = B[A
A\B = B\A
(A [ B) [ C = A [ (B [ C )
(A \ B) \ C = A \ (B \ C )
A [ (B \ C) = (A [ B) \ (A [ C)
A \ (B [ C) = (A \ B) [ (A \ C)
A [? = A
A \U = A
A [ A0 = U
A \ A0 = ?
(A [ B)0 = A0 \ B0
(A \ B)0 = A0 [ B0
(A0 )0 = A
A [ (A \ B) = A
A \ (A [ B) = A
A\A = A
A[A = A
A B = A \ B0

commutativity
o

associativity
o

distributivity
o

identity
o

complementation
o

De Morgans laws
double complement

absorption
o

idempotent laws
set difference representation

Proof Its often very useful to first draw an interpretation diagram whenever possible as it often
provides good insights.
Here we are only going to exemplify the proofs of the 2nd of S1 and the 1st of S6, i.e.
A\B = B\A

(a)

0
0 0
(A [ B ) = A \ B

(b)

(a) We need to show A \ B  B \ A, and B \ A  A \ B.


(i)
(ii)

8x 2 A \ B, we have x 2 A and x 2 B, and hence x 2 B \ A. Thus A \ B  B \ A.


8x 2 B \ A, then x 2 B and x 2 A. Hence x 2 A \ B and thus B \ A  A \ B
:

From (i) and (ii) we see (a) is valid.


(b)

(i) We show first (A [ B)0  A0 \ B0 .

8x 2 (A [ B)0, we have x 2 U and x 62 A [ B. Hence x 62 A and x 62 B, which means


by definition x 2 A0 and x 2 B0 . Thus x 2 A0 \ B0 implying (A [ B)0  A0 \ B0 .

1.3 Russels Paradox

19

(ii) We need to show furthermore A0 \ B0  (A [ B)0 .

8x 2 A0 \ B0, we have x 2 A0 and x 2 B0. Hence x 2U A and x 2U B, implying x 62 A


and x 62 B. We thus have x 62 A [ B. But since x 2 U , we conclude x 2 U (A [ B) =
(A [ B)0 . Therefore A0 \ B0  (A [ B)0 .

From (i) and (ii) we see (b) is true.

Example
10. Let A, B and C be 3 sets. Prove the following set identities.
(a) (A
(b) (A

B) \ (C
B) [ (B

B) = (A \ C)

A) = (A [ B)

B.
(A

\ B ).

Solution The proofs are the easiest if we make use of the set identities S1S10 given in
the above theorem, although direct proofs are equally acceptable.
(a)
(A

B) \ (C

B)

\ B0) \ (C \ B0)
= A \ C \ B0 \ B0
= (A \ C ) \ B 0
= (A \ C ) B
= (A

(b)
(A

B) [ (B

A)

S10
S1 & S2
S9
S10

T B 0 ) [ (B \ A 0 )
T
= (A [ (B \ A0 )) (B0 [ (B \ A0 ))
T
= ((A [ B) \ (A [ A0)) ((B0 [ B) \ (B0 [ A0 ))
T
= ((A [ B) \ U ) (U \ (B0 [ A0 ))
= (A [ B) \ (B \ A)0
= (A [ B) (A \ B)
= (A

S10
S3
S5
S5 & S6

1.3 Russels Paradox


This subsection is particularly difficult and is only for optional reading. Students having
difficulties with other parts of this unit should avoid reading this subsection completely.
The precise definition of a set is actually quite involved: it has to be determined by a number
of axioms. One of them, the axiom of comprehension (or separability), states
If X is a set, P is a property, then there exists a set Y whose
elements are precisely those of X having property P.
For more details, see the book by Elizabeth J Billinton et al, Discrete Mathematics, Logic and
Structures, Longman, 1990.

2 MATHEMATICAL INDUCTION

20

We now show that R = fx j x 2


= xg is not a set. If otherwise, then S = fx 2 R j x 2
= xg must also
be a set according to the above axiom of comprehension. This is not possible for the following
reasons.
(i) If S 2 R then S 2
= S by the definition of R. This then implies from the definition of S that
S 2 S because S 2 R and S 2
= S, leading thus to a contradiction.
(ii) If S 2
= R then S 2 S by the definition of R. This then implies S 2 R from the definition of
S, leading again to a contradiction.
Hence S cant be a set.

To conclude, Russels paradox R is neither an element of R nor not an element of R would


be true only if R where considered a valid set. But we have just shown that R can not be a (valid)
set, hence the paradox does not arise under the axiom of comprehension.

Supplementary Exercises
1. Let A = fa; 5; 6g and B = fx; ?g be 2 sets. Write down A  B and P (B) explicitly in terms
of their elements.
2. Let A, B and C be 3 sets. Draw a Venn diagram for A \ B \ C. From the Venn diagram,
under what conditions can one draw the conclusion
(A

B) [ (B

C) [ (C

A) = A [ B [ C ?

3. Prove that the identity (A \ B)0 = (A0 ) [ (B0 ) holds for any 2 sets A and B.

2 Mathematical Induction
A sequence am ; am+1 ; am+2 ;  ; is an ordered set of elements, denoted by fai gi2I in which I is
the set of indices. For example, sequence a0 ; a1 ; a2 ;  ; may be written as fai gi2N or simply
faigi0 or fai: i  0g. In the world of sequences, the sigma notation provides a convenient
way of writing sums of a partial or complete sequence while the big-pi notation will denote
the product of corresponding sequence. For example

( j3 + j)

3
3
3
(2 + 2) + (3 + 3) + (4 + 4) ;

3
3
3
(2 + 2) (3 + 3) (4 + 4) ;

j=2
4

( j3 + j)
j=2

2.1 Principle of Mathematical Induction


n

21

ai

am + am+1 +  + an ;

ai

am  am+1  an ;

1
2
k2N; k1 k

1+

1 + 2! + 3! + 4! + 5! +  :

i=m
n
i=m

n=1 i=1

1
1
+
+ 
22 32

It is obvious that for m; n 2 Z with n  m, one has


n

(ai + bi )

i=m

i=m
n

c  ai

i=m

 bi )

(ai
i=m

i =m

bi

i=m

c ai
 n

ai +

  n

ai  bi

i =m

i =m

where c is any constant, i.e. c is independent of the dummy variable i.

2.1 Principle of Mathematical Induction


Suppose m 2 Z and for any integer n  m, Sn is a statement (which is either true of false). If
(i) Sm is true,
(ii) Sn+1 is true whenever Sn and n  m are both true.
Then Sn is true for all integers n  m. This is the Principle of Mathematical Induction (P.M.I.).
Roughly speaking, the first condition (i) ensures that Sn is initially true, i.e. true when n =
the initial index m, while the second condition (ii) ensures that a true statement Sn for any n  m
will guarantee that at least the next statement Sn+1 is also true.
Examples
1. Suppose (a) Foxes can only give birth to foxes and (b) Jim is a fox. Will Jim ever have a
rabbit as one of his future offspring?
Solution Just about everyone will say not possible immediately. Why? This is because
the P.M.I. is already in our natural mind without being noticed. However, let us see how
the underlying logic works mathematically.
Let Sn denote the statement that Jims offsprings of the nth generation are all foxes. Then

2 MATHEMATICAL INDUCTION

22

(i) S0 is true, because Jims offsprings of 0th generation consists of Jim himself alone,
who from (b) is a fox.
(ii) Assume Sk is true with k  0, i.e. the kth generation of Jims offsprings are all
foxes. Then, according to (a), all the children of this kth generation can only be
foxes. That is the (k + 1)th generation of Jims offsprings will all be foxes. i.e.
Sk+1 is true.
From the P.M.I., we conclude Sn is true for all n  0. Hence all Jims offsprings will be
foxes. In other words, Jim will never have a rabbit as one of his offsprings. It is perhaps
surprising that just about everyone can subconsciously perform the above reasoning, with
the use of the principle of mathematical induction, in a nip of time.
2. Prove inductively for all integers n  1,
1 + 2 + 3 +  + n =

n (n + 1 )
2

( )

Solution Let Sn denote the statement (*). Since the initial index for n  1 is n = 1, we
first verify
(i) S1 is true because
l.h.s. of (*) = 1 ;

r.h.s. of (*) =

1(1 + 1)
2

= 1 = l.h.s. ;

where l.h.s. stands for left hand side and r.h.s., right hand side. Hence equality (*)
holds for n = 1, meaning S1 is true.

(ii) Assume Sk is true for some k  1, then by the definition of Sk we have


1 + 2 +  + k =

k(k + 1)
2

)

We now prove Sk+1 is also true. For n = k + 1,

l.h.s. of

( )

=
=
=

k(k+1)
2

}|

1 + 2 +  + k +(k + 1)

use (**)

k (k + 1 )
+ (k + 1)
2

(k + 1)(k + 2)

2
r.h.s. of (*) ;

i.e. Sk+1 is true.


We thus conclude from the P.M.I. that Sn is true for all n  1. This means (*) will hold
for all integers n  1.

2.1 Principle of Mathematical Induction

23

3. Can you do the above example once again, but this time perhaps more informally?
Solution Let us consider how to prove by induction the following statement
n

j = 2 n(n + 1)

Sn :

(2-1)

j =1

First, we are happy with the first case (initial index m = 1) because S1 is true as is readily
verified via 1j=1 = 1 and 12 1  (1 + 1) = 1. The main task is the step from the assumption
of Sk being true to the proof of Sk+1 being true. In other words, we, by assuming
k

j = 2 k(k + 1)

(2-2)

j=1

have to show

k+1

j = 2 (k + 1)[(k + 1) + 1

(2-3)

j=1

is true as well. But how can we prove (2-3) by perhaps the use of knowledge in the form
of (2-2)? Well, the idea is to examine both sides of the statement in (2-3) to see if we can
dis-integrate or break the expressions into the forms that are present in the existing
knowledge (2-2), plus the nasty bits. For instance, the l.h.s. of (2-3)
k +1

(2-4)

j =1

is deceiving: we cant use the existing knowledge (2-2) on it directly. So we break the
expression (2-4) into
"
#
k

+ (k + 1)

(2-5)

j=1

in which the last term (k + 1) is the nasty bit, while the first expression is in the ready
form for the use of (2-2). In general, one can not expect to prove a nontrivial statement for
n = k + 1 without using the assumption for n = k. Thus we expect that we wont be able
prove the statement (2-3) without replacing part of its expression by using the assumption
(2-2). Hence the purpose in establishing (2-5) for (2-4) and thus for the l.h.s. of (2-3) is
that we can now use the knowledge (2-2). Since now we are able to break the l.h.s. of
(2-3) into the form of (2-5), and the kj=1 j part in (2-5) can be replaced by the known
(2-2), (thus successfully making use of the assumption (2-2): almost a must!), we can
now hope that the outcome will be equal to the r.h.s. of (2-3) because we have used the
assumed knowledge (2-2). (If this were not the case, then further this type of breaking
would be needed.)
We are now ready to proceed. The l.h.s. of (2-3) is
k +1

j =1

2 MATHEMATICAL INDUCTION

24
"

which is

+ (k + 1 ) :

j =1

After replacing nj=1 j by 21 k(k + 1) due to (2-2), it reads




1
k (k + 1 )
2

+ (k + 1) ;

which is then simplified to


1
(k + 1)[(k + 1) + 1 :
2
This completes the proof that assumption (2-2) for n = k implies the result (2-3) for
n = k + 1. Combining with the earlier validation of S1 , the statement Sn is proved by
mathematical induction for any integer n  1.
4. What would happen, if I were unlucky and looked at the expression (2-3) in example 3
on the right hand side?
Solution If we are unlucky, no worries, at most just a bit extra work. Let us assume that
we looked the other way and saw the r.h.s. of (2-3) first and we still want to prove it by
making use of the induction assumption (2-2). The first step is to break that expression
into containing forms that are identifiable with some of those in (2-2). But for expression
like
1
(k + 1)[(k + 1) + 1 ?
(2-6)
2
Yes, a bit tricky, but can still be done. Let us first examine the expression in (2-6). It is a
2nd order polynomial in k with a leading coefficient 12 . Let us now examine the expression
in r.h.s. of (2-2), it is also a 2nd order polynomial in k. So we can imagine
1
1
(k + 1)[(k + 1) + 1 = k(k + 1) + nasty bits ;
2
2
in which the nasty bits can be calculated as
nasty bits =

1
(k + 1)[(k + 1) + 1
2

(2-7)

1
k (k + 1 )
2

which is just (after simplification)


(k + 1) :

(2-8)

Thus the r.h.s. of (2-3) is equal to


1
k(k + 1) + nasty bits
2
in which the first part is kj=1 j due to (2-2) and second part is (k + 1) due to (2-7) to
(2-8). We thus finally have that the r.h.s. of (2-3) is equal to
"

j=1

+ (k + 1) = l.h.s.

of (2-3)

2.2 Strong Principle of Mathematical Induction

25

and that (2-3) is proved through the use of (2-2).


5. Prove through the use of the P.M.I. that the following identity
12 + 22 + 32 +  + n2 =

n(n + 1)(2n + 1)
6

()

holds for all positive integers n.


Solution Let Sn denote the statement (). Then
(i) S1 is true because
l.h.s. of () = 12 = 1;

r.h.s. of () =

1  (1 + 1)(2  1 + 1)
6

=1

implies l.h.s.=r.h.s., i.e. () is valid for n = 1.

(ii) Assume Sk is true for some k  1, then

12 + 22 + 32 +  + k2 =

k(k + 1)(2k + 1)
6

()

Hence
12 + 22 +  + k2 + (k + 1)2
from ()

=
=
=
=
=

2
2
(1 + 2 +

 + k2) + (k + 1)2

k(k + 1)(2k + 1)
2
+ (k + 1 )
6
h
i
(k + 1 )
2
(2k + k) + (6k + 6)
6
(k + 1)(2k2 + 7k + 6)
(k + 1)(k + 2)(2k + 3)
=
6
6
(k + 1)((k + 1) + 1)(2(k + 1) + 1)
;
6

i.e. Sk+1 is true.


Hence the principle of mathematical induction implies Sn is true for all integers n  1.

2.2 Strong Principle of Mathematical Induction


The Principle of Mathematical Induction can also be equivalently represented in other forms.
One of them is the following Strong Principle of Mathematical Induction (S.P.M.I.).
Suppose m; m0 2 Z and for any integer n  m, Sn is a statement. If
(i) Sm ;  ; Sm0 with m0  m are all true,
(ii) Sn+1 is true, whenever Sm ;  ; Sn are all true and n  m0 .

2 MATHEMATICAL INDUCTION

26
Then Sn is true for all n  m.

Note S.P.M.I. implies P.M.I. is obvious. As for the converse one only needs to define Tn to be
the statement that Sk for m  k  n are all true. Then the P.M.I. on Tn will imply the S.P.M.I. on
Sn . We shall however skip the simple details here.
Examples


6. Let n; r 2 N with r  n. We define nr , called n choose r, to be the total number of


relement subsets that can be chosen from a set of n elements. We thus have
 
n
= 1,
9 only 1 empty set
(a)
0
 
n
(b)
= 1,
the only subset with all elements in it is the set itself
n
  
 

n
n 1
n 1
=
+
(c)
. if r  1; n > 1
r
r
r 1

Let us briefly explain (c). Suppose S be a set of n elements and a 2 S is any element, then

S = T [fag if T = S fag. To choose r elements from S (there are nr different ways to

do so), we can either choose r elements from T entirely (there are n r 1 different ways

to do so), or choose a as one element and another r 1 elements from T (there are nr 11
different ways to do so). Hence (c) is valid.
Recall the convention 0! = 1. Let us prove
 

n
r

n!
r!(n r)!

nr0:

Solution Let Sn denote the following statement


 

n
r

n!
(n r)!r!

is true for all 0  r  n :


 

(i) S0 is true from either (a) or (b), because

0
0

0!
0!0!

= 1.

(ii) Assume for n  0, Sk is true for all 0  k  n. Then from (c) for 1  r  n we have


n+1
r

 

=
=
=
=

n
r

n
r

n!
n!
+
r!(n r)! (r 1)!(n r + 1)!


n!
1
1
+
(r 1)!(n r)! r
n r+1
(n + 1)!
r!(n + 1 r)!

(from induction assumption)

2.2 Strong Principle of Mathematical Induction

27

As for r = 0 and r = n + 1, we see from (a) and (b)




n+1
0


=

n+1
n+1


=1=

(n + 1)!
(n + 1)!0!

Hence Sn+1 is true..


From (i) and (ii) and the S.P.M.I. (with m = m0 = 0), we conclude Sn is true for all n  0.
7. For integer n  1 show the following statement (often called binomial theorem or binomial expansion) is true
 
 
 


 
n n
n n 1
n n 2 2
n
n n
n
n 1
Sn :
(x + y) =
x +
x y+
x y +  +
xy
+
y
0
1
2
n 1
n
Solution
(i) S1 is true because

 
1

(x + y) = x + y =

 

1
1
x+
y:
0
1

(ii) Assume Sk is true for some k  1, then


k+1
(x + y)

induction
assumption

h
=
=
=

from (a), (b), (c)


of example 5
=

(x + y) (x + y)

 

i
 

  

n n
n n 1
n n
(x + y)
x +
x y +  +
y
0
1
n
 
   
   
n n+1
n
n
n
n
x
+
+
xn y +
+
xn
0
0
1
1
2

  
 
n
n
n n+1
+  +
+
xyn +
y
n 1
n
n






n + 1 n+1
n+1 n
n + 1 n+1
x
+
x y +  +
y
0
1
n+1

1 2

i.e. Sk+1 is true.


Hence from the P.M.I. we conclude Sn is true for all n  1.
8. Prove 2n + 1 < 2n ;

n  3:

Solution Im quite exhausted by now; if youre not then supply the proof yourself.

3 EFFICIENCY, BIG O

28

Supplementary Exercises
1. Prove by mathematical induction that
20 + 21 +  + 2n = 2n+1

holds for all integers n  1.

2. Prove by the use of P.M.I. that 3n > 4n holds for all integers n  2.
3. Suppose a sequence an is defined by the recurrence relation
a0 = 1;

an+1 = 2an + 1

for n = 0; 1; 2; 3; ::: . Prove by induction that an = 2n+1

1 holds for all integers n  0.

3 Efficiency, Big O
3.1 Nested Evaluation of a Polynomial
A polynomial of degree n in variable x is an expression of the form
an xn + an

n 1
+
1x

 + a1x + a0

where a0 ; a1 ; :::; an are constants. For example, 3x5 x + 1 is a polynomial of degree 5, and
y7 + 5y6 + y2 is a polynomial of degree 7. There are often different ways to perform an
evaluation, some may thus be better or more efficient than the others. For example, the direct
term by term evaluation of polynomial

iPPP
* 6 P



2 multiplications: 9  x  x P

f (x) = 2x3 + 9x2 + 5x 1

1 multiplication

3 multiplications

requires a total of 9 operations (6 multiplications + 3 additions/subtractions) while the evaluation in the following form

*
*
* H
YH H
YH H
YH


3 multiplications
3 additions/subtractions
f (x) = x [x (2 x + 9) + 5 1

requires only (3 multiplications plus 3 additions/subtractions)


first evaluate: 2x + 9,
then evaluate: x(2x + 9) + 5,
then evaluate: x(x(2x + 9) + 5)

1,

2 operations
2 (more) operations
2 (more) operations

3.2 Big O Notation

29

This method of evaluation is essentially Horners algorithm, and is also called the nested
evaluation.
Incidentally, the reason that we often dont differentiate additions and subtractions while
counting multiplications separately is because multiplications are more expensive (in terms of
computer time) than additions and subtractions and the latter two will take approximately the
same amount of computer time.
In general for an nth order polynomial
n 1
+
1x

P(x) = an xn + an

 + a1 x + a0

we can rewrite P(x) as


P(x)

=
=
=
=



n 2
+ an 1 x
+
+ a1 ) + a0
n 2
x(x(an x
+
+ a2 ) + a1 ) + a0

x(an xn





x x [  x(x(an x + an

1 ) + an 2 ) +

 + a1

+ a0

The above nested form or telescoping form, requires no more than n multiplications and n
additions/subtractions. It is obviously more efficient than the direct term by term evaluation. In
other words, the Horners algorithm is more efficient for polynomial evaluations.

3.2 Big O Notation


We often need to estimate the time or memory requirements for some given algorithms, and
perform upper bound estimates of such as the complexity and efficiency. Often the magnitude
rather than the precise value is sufficient or is significant. The big O notation will serve well in
this regard.
Let f : D ! R and g: D ! R be two (real-valued) functions with D  R . We say f (x) is
O (g(x)), or f (x) = O (g(x)), if 9M 2 R and 9C > 0 such that 8x 2 D,

j f (x)j  Cjg(x)j

whenever x  M :

The diagram below depicts an intuitive interpretation of the above inequality: the curve y =
j f (x)j will always be below the curve y = Cjg(x)j after x has passed the mark x = M. The value
M and the value C > 0 themselves are not important: the significance lies in the existence of
such M and C, rather than the magnitude of these values.

3 EFFICIENCY, BIG O

30

6y

*
1

y = Cjg(x)j

I
y = j f (x)j

We note that if a property is to be established from first principles, then the property has to
be drived or proved from the basic definitions. Hence if we are to show, for instance, F (n) =
O (G(n)) from first principles for some given functions F (n) and G(n), then we have to derive
it from the very definition of big O . In other words we need to find a C > 0 and an M such that
jF (n)j  CjG(n)j holds whenever n  M. In establishing big O properties, one typically has to
make use of the following triangle inequalities

ja  bj  jaj + jbj ja  bj  jaj jbj


jx1 + x2 +  + xn j  jx1j + jx2j +  + jxn j jx1 + x2 +  + xn j  jx1 j jx2j  jxn j
Hence we have for example jx 2yj  jxj + j 2yj and jx + y 13j  jxj jyj j13j.
Note Some texts use f 2 O (g) to denote that f (x) is O (g(x)). The convenience of our notation
;

however lies in the fact that we can use O (g(x)) to denote any quantity that is O (g(x)). However,
both notations are acceptable as long as they are used consistently.
Examples
1. Let f :

N ! R be given by f (n) = 2n

3. Show f (n) = O (n).

Solution Observe

i.e.

j f (n)j = j2n 3j  j2nj + j3j


j f (n)j  3n 8n  3
;

if n  3

2n + n ;

where we have made use of he triangle inequality

j2n

3j = j(2n) + ( 3)j  j(2n)j + j( 3)j = j2nj + j3j ;

see Preliminary Mathematics at the beginning of these notes for more details about the
triangle inequalities. By taking C = 3 and M = 3 (and D = N ), we see f (n) is O (n). We
note that there are many different yet all valid choices of C and M. For example,
if n  1
j f (n)j  j2nj + j3j  2n + 3n = 5n ; i.e. j f (n)j  5n; 8n  1
implies we can choose C = 5 and M = 1 in the definition of f (n) = O (n).

3.2 Big O Notation

31

2. Show f (x) =

p
3 x(2x + 5)
is O ( x) for x 2 R .
jxj + 1

Solution The inequality

j f (x)j

assume x > 0
assume x  1

3 x(2x + 5)
x

=6

px + p15

p
p
p
p
6 x + 15  6 x + 15 x = 21 x

i.e. j f (x)j  21 x for x  1, gives immediately f (x) = O ( x ) (by choosing C = 21 and


M = 1 in the definition).
1
3. Let f (n) = n(n + 5). Prove f (n) = O (n2).
2

Solution We need to find constant M and positive constant C such that j f (n)j  Cn2 for
all n  M. There are different ways to achieve this, and we give below the 2 most obvious
ones.
(a)

1 promoted to n in the next step

j f (n)j
i.e.


j f (n)j 

1
n(n + 5  1
2
1
n(n + 5n)
2
3n2

if assume n  1

We take in this case M = 1 and C = 3.


(b)

j f (n)j

5 promoted to n in the next step

=
=

i.e.


j f (n)j 

1
n (n + 5 )
2
5
1 2
n +
n
2
2
1 2 1 2
n + n
2
2
2
2n


if assume n  5

We take in this case M = 5 and C = 2.


From (a) or (b) we see that f (n) = O (n2 ).
4. For m  n  0; xn = O (xm ).
Solution DIY.

3 EFFICIENCY, BIG O

32

3.3 Simple Features of O


Let f and g be mappings from D  R to R , then

f (x) = O ( f (x)); j f (x)j = O ( f (x)); c  f (x) = O ( f (x)) (c is any constant).


For example, n2 = O (n2 ).

If g(x) = O ( f (x)), then f (x) + g(x) = O ( f (x)).


This is because g(x) = O ( f (x)) implies the existence of constants C > 0 and M such that
jg(x)j  Cj f (x)j, 8x  M. Hence

j f (x) + g(x)j  j f (x)j + jg(x)j  (C + 1)j f (x)j

xM

which means f (x) + g(x) = O ( f (x)). For example, n3 + 4n = O (n3 ).

If g(x) = O ( f (x)), then f (x) + O (g(x)) = O ( f (x)).


Since f (x) + O (g(x)) represents a quantity f (x) + h(x) such that h(x) = O (g(x)), we see
from f (x) + h(x) = O ( f (x)) that f (x) + O (g(x)) = O ( f (x)). For example, n3 + 2n2 + 5n =
n3 + (2n2 + 5n) = n3 + O (n2 ) = O (n3 ).

In order to make the full use of the notation O ( f (x)), we need to assign an exact meaning to
an identity in which O notations are used. Suppose F and G are two general expressions, with F
containing O ( f1 (x);  ; O ( fm (x)) each exactly once and G containing O (g1 (x));  ; O (gn (x))
each exactly once, then the identity
F (O ( f1 (x));  ; O ( fm (x)); ) = G(O (g1 (x));  ; O (gn(x)); )
means that

8 p1 (x)  8 pm(x) such that pi (x) = O ( fi(x))


9q1 (x)  9qn(x) such that q j (x) = O (g j (x))
;

for all i = 1;  ; m ;


for all i = 1;  ; n ;

the identity
F ( p1 (x);  pm (x); ) = G(q1 (x);  qn (x); )
is held in the conventional sense. Obviously this extension is naturally consistent with the
basic definition of f (x) = O (g(x)).
We hasten to remark that with above such definition, F (O ( f1 )); ) = G(O (g1 )); ) is in
general not same as G(O (g1); ) = F (O ( f1 ); ). In other words the equality = is not the
conventional one, but with the extended meaning.

33
Example
5. n = O (n) is obviously true. But the reversed identity O (n) = n is not true, because 2n is
O (n) but is not equal to the r.h.s. n.

N
Supplementary Exercises
1. How many multiplications have to be performed if one is to evaluate 2x3 + 5x2 + 3x + 1
using Horners method?
9n + 1 = O (n3 ).

2. Show 2n3
3. Show p

n+1

jn

1j + 1

pn).

= O(

4. Let f (x) and g(x) be 2 functions on R with f (x) = O (g(x)). Show that there exists a
constant M such that j f (x)j  jx  g(x)j holds for all x  M.

Notation

We recall that big O is essentially a measurement for magnitude of functions at infinity.


We say f (x) = O (g(x)) if the magnitude of f (x) will be eventually bounded from above by a
constant multiple of the magnitude of g(x), i.e. constants 9C > 0 and M such that j f (x)j 
Cjg(x)j for all x  M. Obviously it is the existence of such C and M, rather than their actual
values, which is significant.
It often turns out that upper bound by big O is not precise enough. For example, to say
3n = O (n2 ), while true, is not describing 3n more precisely than one could have. On the
other hand, O (n) describes much better the magnitude of function 3n than say O (n2 ). For better
distinction of such cases, we introduce below the so-called notation.

4.1 Definition of the Notation


Let f and g be real-valued functions over D  R . We say f (x) = (g(x)) iff 9 constants C > 0,
D > 0 and M such that 8x 2 D
Djg(x)j  j f (x)j  Cjg(x)j;

whenever x  M :

We note that the inequality on the r.h.s., i.e. j f (x)j  Cjg(x)j, gives an upper bound for the
magnitude of function f (x) while the inequality on the l.h.s., i.e. Djg(x)j  j f (x)j, gives a
lower bound. In other words, f (x) = (g(x)) iff j f (x)j is sandwiched between two nonzero
multiples of jg(x)j.

4 NOTATION

34
Examples
1. Let f :

N ! R be given by f (n) = 2n + 1. Then


(a) f (n) = O (n2 ) ;

(b) f (n) = (n) :

Solution
(a) j f (n)j = 2n + 1  2n2 + n2 = 3n2 for n  1 implies f (n) = O (n2 ) by the 1st principle,
i.e. by the definition.
(b) We need to find the upper bound and the lower bound for the function f (n). From

j f (n)j = 2n + 1 

if n 1

3n ;

j f (n)j = 2n + 1  2n

i.e. 2n  j f (n)j  3n for n  1, we easily conclude f (n) = (n).


2. Let f : N

! R be given by f (n) = 2n2n+ n1+ 1 . Show


(a) f (n) = O (1) ;

(b) f (n) = (1) :

Solution
(a)



n2 1


f (n) = 2
2n + n + 1

1

i.e. j f (n)j 

if n 1

n2
2n2 + n + 1

 2nn 2 = 12

 (1) for any n  1. Hence f (n) = O (1) (with C = 12 and M = 1).

(b) We already found an upper bound for f (n) in (a); we just need to find a lower bound
in order to show f (n) = (1). Since n  2 implies n 1  n=2, we have



n2 1

f (n) = 2
2n + n + 1

if n 2 (n + 1)(n

1)
2n2 + n2 + n2

(n 2)
1
 n 4n
=
2
8
=

i.e. 1  8j f (n)j for n  2. Hence, together with the upper bound found in (a), we
have
1
1

1  j f (n)j   1 ;
n2;
8
2
1
1
or 18 jg(n)j j f (n)j  12 jg(n)j for g(n)  1, with M = 2, C = and D = . According
2
8
to the definition, we thus have f (n) = (g(n)), i.e. f (n) = (1).
Though the above 2 examples have very short proofs, its not uncommon for someone new
to the subject to feel completely lost. So in what follows well explain several examples in
painstaking details.

4.2 Examples in Exaggerated Details

35

4.2 Examples in Exaggerated Details

Examples
3. What on earth is the big O ?
Solution Big O is essentially a measurement for the magnitude of functions at infinity,
and tells roughly who is bigger. We say function f (n) is of order of big O of function
g(n), i.e. f (n) = O (g), if the magnitude of f (n) will be eventually bounded from above
by a constant multiple of the magnitude of g(n). Magnitude j f (n)j bounded by a constant
multiple of that of g means
j f (n)j  C jg(n)j
()
for a constant C > 0, while eventually means (*) needs only to be valid for the later
ns. In other words, (*) needs only to be true for n  M for a constant M. We note that
what the actual values the positive constant C and the constant M take is not important,
what is important is that once a C is chosen, it has to remain unchanged in (*) for all
n  M. This is like comparing the potential weight (magnitude) of a cat and a dog: we
have to wait until they have fully grown up (n  M), but exactly when (i.e. the value of
M) is not really important.
4. Let f : N

! R be given by f (n) = n

we can prove f (n) = (n2 ).

3 + 2n2

n+1

. Show in very rudimentary details how

Solution From the definition of notation, we have to establish an upper bound and
lower bound, corresponding to the 2nd and the 1st inequality relation in the formula
below
D jn2j  j f (n)j  C jn2 j :
To establish an upper bound is to find some positive constants C and M such that
3
n + 2n2


n+1

 C  n2

(4-1)

is true at least for n  M. But how can we do this? And in what general direction should
we proceed? We observe that on the r.h.s. of (4-1) it is a just a single term, while the l.h.s.
of (4-1) is a polynomial fraction.
We will now try to move from the expression

to the expression

n3 + 2n2 1
n+1

(4-2)

C  n2

(4-3)

4 NOTATION

36

gradually, in terms of the  sign. In such moves, we will keep some terms intact,
dump some terms and replace some terms. The general rule is that one should always keep
the dominant (leading) terms, while throwing away or manipulating other non-dominant
terms in an expression. Back in expression (4-2), we see that the dominant term in the
numerator n3 + 2n2 1 is n3 while the the dominant term in the denominator n + 1 is n.
So we are happy to throw away (legitimately) the term 1 in the numerator to arrive at a
simpler form (which is closer to the final target in (4-3)). We thus get the first move
n3 + 2n2 1
n+1

 n n++2n1

(4-4)

Because the numerator and denominator of the result


n3 + 2n2
n+1

(4-5)

of the first move are both non-negative, we throw away the 1 in the denominator
of (4-5), because the less the denominator, the larger the total fraction. Hence our 2nd
move is
n3 + 2n2 n3 + 2n2
 n :
(4-6)
n+1
The newly obtained result after the 2nd move is thus
n2 + 2n

(4-7)

which is even closer to (4-3). How do we make the last move? We observe that (47) contains inhomogeneous terms (i.e. different powers) while (4-3) contains only an
homogeneous term. So our next step is to promote legitimately other orders to the
same leading order n2 . In doing so, we notice that
2n  2n2
when n  0, hence our 3rd move gives
n2 + 2n  n2 + 2n2 = 3n2 :
Putting all the above moves together, we obtain for n  0
n3 + 2n2 1
n+1

 n n++2n1  n

3 + 2n2

2
= n + 2n

 n2 + 2n2 = 3n2

(4-8)

 n2 + 2n2 = 3n2

(4-9)

Since value of (4-2) is non-negative if n  1, we see that (4-8) implies


3
n + 2n2


n+1

n + 2n
n
 n+1 

3 + 2n2

2
= n + 2n

is true for all n  1. In other words, (4-1) is true if we choose C = 3 and M = 1.

4.2 Examples in Exaggerated Details

37

Now we proceed to establish a lower bound for f (n). We need to find positive constant
D and constant L such that
3

n + 2n2
2


(4-10)
n+1  Dn
for all n  L. If we can establish (4-10), then along with the already established (4-1), we
see that
3

n + 2n2 1
2

 C  n2
Dn 
(4-11)

n+1
is true for n  M = max(M ; L).

To derive (4-10), we proceed similarly. First let us throw away some non-significant
terms legitimately, by keeping the dominant ones and assuming n  1 (thus (4-2) is nonnegative). Our first move is to throw away the term 2n2 in the numerator of (4-2).
Notice that our new moves are for the  sign, not the  as in the previous case.
Thus we cant throw way the 1 in the numerator and 1 in the denominator as they
would break the sought  sign. Hence after the first move, we obtain
n3 + 2n2 1
n+1

 nn + 11

(4-12)

Next, let us promote the (non-leading) 1 in the denominator to arrive at an homogeneous


expression, i.e. we need to promote the 1 into something like constant n. This turns
out easy as 1  n thus n+1 1  n+1 n for n  1. Hence our 2nd move gives
n3 1
n+1

 nn + n1 = n 2n 1

(4-13)

Observe the r.h.s. of (4-13), we see that we need to promote the 1 in the numerator
to the form of constant n3 to ensure homogeneity. In other words, we need to establish
something like
1  constant  n3 :
(4-14)
It would be obviously true if we choose the constant in (4-14) to be just say 2 and ask
for n  1. But this would inflict out-of-proportion damages on our leading term n3 . So we
have to choose the constant carefully. Since n3 is eventually much larger than the lower
order term 1, we can take out a portion of n3 , say 41 n3 without affecting the magnitude
order, and use it to compensate the 1. In other words,


1=

3 3
1
n + n3
4
4

(4-15)

If we choose n large enough, then the terms in the square brackets in (4-15) will be nonnegative, and then we can legitimately throw that away because we are establishing a 
relation. To ensure


1 3
n 1 0;
(4-16)
4

4 NOTATION

38

we need n  4 3 . Hence if we choose n  2, it will be enough to guarantee the nonnegativeness in (4-16). Hence our 3rd move is
1

1=

or

3 3
1
n + n3
4
4
n3

 34 n2

n
Putting moves 13 together, we obtain
3
n + 2n2


n+1

 34 n3

for n  2 ;

for n  2 :

(4-17)

n 1 n 1 n 1 1 3 2
 n + 1  n + n = 2n  2  4 n

for n  2 :

(4-18)

Notice that (4-18) and (4-9) can be put together as


3 2 n3 + 2n2 1
n 
3n2 ;


8
n+1

for n  2 ;

which implies f (n) = (n2 ).


5. Redo example 4 in such a (recommended) way that the proof will be both concise and
mathematically acceptable.
Solution To find an upper bound we first observe
3
n + 2n2


n+1





n3 + (2n2 1)
n+1
3
n + 2n2
n+1
3
n + n3
n
2
2n

assume n  1 (thus 2n2

1  0 and f (n)  0)

increase the numerator


assume furthermore n  2 (thus 2n2  n3 )
:

The inequality above was due to the (legitimate!) increase of the numerator and the
decrease of the denominator, it could be done so because we were looking for an upper
bound. Obviously we wont allow in this case a decrease of the numerator or an increase
of the denominator. To find a lower bound, incidentally, the opposite is true. We have
thus so far shown
j f (n)j  2n2 ; whenever n  2 :
For a lower bound, we observe
3
n + 2n2


n+1

3
n + (2n2

=


n+1

1)

n3 + (2n2 1)
n+1

if n  1

4.2 Examples in Exaggerated Details



=

which means

39

n3

because 2n2

n+1
n3
n+n
1 2
n ;
2

10

legitimate increase of denominator

j f (n)j  12 n2

whenever n  1 :

Hence, by putting together the upper bound and the lower bound, we have
1 2
n  j f (n)j  2n2 ;
2

whenever n  2 :

1
and M = max(1; 2) = 2
2
where, for any values a and b, max(a; b) denotes their maximum value. Hence we finally
conclude f (n) = (n2 ).
This in the definition of notation corresponds to C = 2, D =

6. Show that log2 n = O (n) for positive integer n.


Solution We first prove by induction the statement
Sn :

log2 n < n

for n  1. Obviously S1 is true because l.h.s.=log2 1 = 0 < 1=r.h.s. Before we embark


on the inductive step, we observe that log2 n is an increasing function in the sense that
log2 m < log2 n whenever m < n (and m; n > 0). Assume Sk is true for an integer k  1,
i.e. log2 k < k, then
log2 (k + 1)  log2 (k + k) = log2 (2k) = log2 2 + log2 k = 1 + log2 k < 1 + k ;
i.e. log2 (k + 1) < k + 1. Hence Sk+1 is also true. From the P.M.I. we have thus shown that
Sn is true for all integers n  1. Since log2 n  0 for n  1 implies j log2 nj = log2 n  jnj
for n  1, we finally conclude log2 n = O (n).
7. Show n2 6= O (n).
Solution If otherwise, i.e. n2 = O (n), then according to the definition of big O there
would exist a positive constant C and another constant M, such that jn2 j  Cjnj whenever
n  M. But this is not possible because, if we choose an n0 such that jn0 j > max(C; M ),
we would have jn0 j2 > Cjn0 j which contradicts the requirement jn2 j  Cjnj whenever
n  M. Hence the earlier assumption n2 = O (n) that leads to this requirement must be
incorrect. Thus n2 6= O (n). This is a proof by contradiction.
Note From the above examples we see easily that the choice of constants C, D and M may vary
and their values often depend on the different approaches we may choose.

5 ANALYSIS OF ALGORITHMS

40

Supplementary Exercises
1. Show 2n3 + 9 log2 n + 1 = (n3 ).

n + sin n
2. Show p
jn j + 1

pn).

= (

3. Suppose f (n) and g(n) are 2 functions defined on N such that f (n) = (g(n)) and f (n) 6=
0 for all n 2 N . Show that n f (n) 6= O (g(n)).

5 Analysis of Algorithms
An algorithm is a set of instructions that can be mechanically executed to produce a unique
output from a valid input in a finite amount of time or steps. Given an algorithm, one often
needs to determine such things as the cost of computer implementation and the efficiency of the
algorithm.
One important algorithm is one that does a search of a list for a particular item. Suppose
we are given an ordered list of items I (1);  ; I (n). Then for any item named KEY, we look
through the given list to see if there is a match in the list. Report the position k at which item
I (k) matches KEY. Otherwise report a phantom position 1 which is thus used to denote that
the KEY is not found.
Input: KEY

Algorithm
Output:

First position k in the list of which I (k)


matches KEY, otherwise output 1

In the following we shall examine two search algorithms for the above problem, the sequential search and the binary search, and show that the latter is a more efficient in general. For
simplicity, the efficiency of an algorithm will always be measured according to the total number
of comparisons involved.

5.1 Sequential Search


Sequential search is to search the list from the first item to the second, then to the third and so
on to locate the position of the matched item. For a given item KEY the algorithm is as follows

5.2 Binary Search

41

1. For k = 1 to n determine if KEY = I (k).


If yes, output k and stop.
2. Output the phantom position 1 to indicate no match has been found.
We note that the second step will not be executed if the execution of the algorithm is terminated
in step 1 due to the stop command.
Let S(n) be the number of comparisons needed in a worst case to complete the sequential
search in a list of n items. Since the worst case is when either the searched item is not found or
it is seated at the last position, we have in both cases S(n) = n.
Before going further, we first introduce two useful notations that relate any real number to
its integer neighbours. For any x 2 R ,

the floor of x, denoted by bx , is the greatest integer not exceeding x, e.g. b3:2 = 3 and
b 4:5 = 5.

the ceiling of x, denoted by dxe, is the smallest integer not less than x, e.g. d3:2e = 4 and
d 4:5e = 4.

integer neighbours of x
PPP

)
q

bx

dxe

5.2 Binary Search


For an ordered list of n items, the general strategy of binary search is to compare a given KEY
with the item at the middlemost position. i.e. at the b n+2 1 th position. Then report this position
if the items are matched. If not, we only need to search one of the two (halved) sublists broken
up at the middlemost point. Which sublist is to be continued for the search depends on whether
the given KEY is after or before the item at the middlemost position. Continue the binary search
on the correct sublist until either a match is found, or the latest sublist is reduced to an empty
list. It is easy to see that binary search has a great deal in common with how we would search
for a word in a dictionary.
Let us recall that if a list am ; am+1 ;  ; an is indexed from m to n, then the middlemost
n
position is at k = b m+
2 . Suppose a given list I (1);  ; I (n) is sorted in the increasing order,
then the binary search algorithm can be rephrased as follows. Here =  :::  = indicate some
comments.

5 ANALYSIS OF ALGORITHMS

42
1. F = 1, L = n.
2. while F  L do

/* F=first index, L=last index */


jF + Lk

(i) find middlemost position k =

(ii) if KEY = I (k) then


output k and stop
else if KEY > I (k) then
F = k+1
else
L=k 1
3. output the phantom position 1.
old F

/* match found */

 move to 2nd half of the list */

/* redefining starting position */


/* move to 1st half of the list */
/* redefining end position */

middlemost new F



old L

{z
old list |



{z
new list

}
}

one possible case

Example
1. Find 13 from the ordered list f1; 3; 5; 7; 9; 13; 15; 17; 19g with both the binary search and
the sequential search. Give the number of comparisons needed in both cases.
Binary search. 3 comparisons are needed as can be seen from the diagram below
i
I (i )

1 2 3
1 3 5

4
7

3




middlemost: 5 =

5
9

6
13

9 < 13 :

j1 + 9k

*


15



13

j 6 + 9k

13

8
17

9
19

17

19

{z
new (sub) list

|{z}
newer list

7=

7
15

15
>

13 :

13=13: match found in 3


comparisons

Sequential search. 6 comparisons are needed in this case, see


i
1 2
I (i) 1 3

3 4
5 7

5
9

6
13

7
8
15 17

9
19

search stops after finding the match after 6 comparisons

5.3 Complexity of Binary Search

43

5.3 Complexity of Binary Search


Let B(n) be the maximum number of comparisons the binary search needs to complete the
search for an ordered list of n items. Then obviously for m; n 2 N with m  1,
(i)
(ii)

B(1) = 1
B(m)  B(n) if m  n

m 1

(iii) B(2m) = 1 + B(m)

see:

(iv) B(2m + 1) = 1 + B(m)

z }| {

  
| 
{z }

z }| {

  
| 
{z }

*
*

Hence we have B(2k ) = B(2k 1 ) + 1 = B(2k 2 ) + 2 = ::: = B(20 ) + k = k + 1 for k  0. For any
n 2 N with n  1, 9k 2 N such that 2k  n < 2k+1 . Taking a log2 on both sides of the inequality
we obtain k  log2 n < k + 1, see Preliminary Mathematics at the beginning of these notes for a
quick reminder of powers and logarithms. Hence
k = blog2 n
From (ii) we thus obtain

2 k  n < 2 k +1 :

B(2k )  B(n)  B(2k+1 )

which gives k + 1  B(n)  k + 2, or simply

blog2 n + 1  B(n)  blog2n + 2


Hence for n  4, we have log2 n  2 and log2 n  blog2 n + 1 for n  1 so
log2 n  B(n)  2 log2 n
:

Thus B(n) = O (log2 n) and log2 n = O (B(n)): We note that in the literature of computer science,
log2 n is often abbreviated to log n.
Note The inequality (ii), B(m)  B(n) if m  n, is easy to understand. However a rigorous proof
can be done by inductively proving that B(n)  B(n + 1) for any n  1. The idea is sketched in
the following diagrams
n even:

| 
{z }

n odd :
n + 1:

| 
{z }
m
| 
{z }
m
| 
{z }
m

B(2m) = 1 + B(m)

)
B(2m)  B(2m + 1)

m 1

n + 1:

| 
{z }

*
*
*

| 
{z }
m
| 
{z }
m
| 
{z }

B(2m + 1) = 1 + B(m)
B(2m + 1) = 1 + B(m)

induction assumption

B(2m + 2) = 1 + B(m + 1)

1 + B (m )

m+1

In fact, it is not difficult to derive a much neater expression B(n) = blog2 n + 1 for n  1 via,
for instance, first an induction on B(n)  1 + log2 n.

6 SYMBOLIC LOGIC

44

5.4 Performance of Sorting Algorithms


A number of sorting algorithms will be introduced in the tutorials over the semester. They include bubble sort, insertion sort, selection sort, merge sort and quick sort. The performance
of these sorting algorithms, for simplicity, will be based purely on the number of comparisons
involved during the sorting process.
In sorting a list of n items, selection sort, bubble sort, insertion sort and quick sort each
require a total of O (n2) comparisons in the corresponding worst (i.e. the most laborious)
cases. Let M (n) be number of comparisons needed for the merge sort in the worst case, and
Q(n) be the average number of comparisons needed for the quick sort, then both M (n) and Q(n)
are O (n log n). More precisely, however, one can show for sorting a list of n ( 1) items
M (n)  4n log2 n ;

Q(n)  (2 ln 2)n log2 n :

This subsection should be regarded as just the passing remarks. Full explanations of the
sorting algorithms and the associated complexity and performance will be covered in a sequence
of the tutorials. For a quick summary of these sorting algorithms, please also see the appendix
Tutorials: Sorting Algorithms.

Supplementary Exercises
1. Let 0 < x < 1. What is the value of

l3

xm
2

, and what is value of

l3

xm
3

2. How many comparisons are needed to search G from the following list
A; B; C; D; E ; F; G; H ; I ; J
through the use of the binary search algorithm?

6 Symbolic Logic
The purpose here is to deal with various forms of statements or compound statements, to symbolize and represent the underlying logic, as well as to set up the basic concepts for further
analysis.

6.1 Basic Concepts

I
I

A proposition is a sentence which is either true or false.


A tautology is a proposition which is always true.

6.1 Basic Concepts

I
I

45

A contradiction is a proposition which is always false.


A compound proposition is built from propositions by the use of the connectives and,
or and not.

Suppose we have two statements or propositions denoted by p and q respectively, then the above
three connectives may give rise to

I p ^ q, the conjunction of p and q, meaning p and q


I p _ q, the disconjunction of p and q, meaning p or q
I  p, the negation of p, meaning not p
Note The negation  p is sometimes also written as : p. Both notations are acceptable in this
unit.
Example
1. Among the following four sentences
(a) Today is a rainy day.
(b) David was wet this morning.
(c) Did David get soaked in the rain?
(d) Please read the notes.
only (a) and (b) are propositions. If we denote by p and q respectively the propositions
(a) and (b), then
p ^ q represents Today is a rainy day and David was wet this morning .

p _ q represents Either today is a rainy day, or David was wet this morning, or
both .

 p represents Today is not a rainy day .


In general, when p and q are propositions, p ^ q (i.e.

p and q) for example is true


(T ) if and only if both p and q are true. The precise effect or truth values for the above
connectives can be summerized in the following truth tables
p
T
T
F
F

q
T
F
T
F

p^q
T
F
F
F

p
T
T
F
F

q
T
F
T
F

p_q
T
T
T
F

p
T
F

p
F
T

6 SYMBOLIC LOGIC

46

In fact we can take these tables as the precise definitions for the corresponding connectives. Likewise one way of describing completely a compound proposition is to give
explicitly its truth table. Going the other way, we note that a truth table can also be used
to define a compound proposition.

6.2 Logical Equivalence


Two (compound) propositions P and Q are said to be equivalent or logically equivalent, denoted
by P  Q or by P , Q, iff (i.e. if and only if) they have the same truth values. In other words
for all possible truth values of the component statements, the compound propositions will have
the same truth values.
Examples

2. Show ( p) _ ( q) and  ( p ^ q) are equivalent.


Solution We first construct below the truth table for the two compound propositions
p
T
T
F
F

q
T
F
T
F

p q
F
F
T
T
|

F
T
F
T

p^q
T
F
F
F

{z

 ( p ^ q) ( p) _ ( q)
F
T
T
T
|

F
T
T
T
{z

exactly same columns


(same truth values)

intermediate results not


necessary but useful

Since the last two columns are the same, we conclude


equivalent.

 p) _ (q) and ( p ^ q) are

3. Show ( p ^ q) and ( p) ^ (q) are not logically equivalent.


Solution This is manifested in the following truth table
p
T
T
F
F

q
T
F
T
F

 p q
F
F
T
T

F
T
F
T

p^q
T
F
F
F

( p ^ q) ( p) ^ (q)
F
T
T
T
|

F
F
F
T
{z

not exactly same

because the corresponding truth values differ (at 2 places).


4. Show ( p _ q) _ ( p) is a tautology and ( p ^ q) ^ ( p) is a contradiction.
Solution From the following truth table

6.3 Conditional Statements


p
T
T
F
F

q
T
F
T
F

 p q
F
F
T
T

F
T
F
T

47
p_q
T
T
T
F

p^q
T
F
F
F

(p

_ q ) _ ( p )
T
T
T
T

tautology
T
T
T
T

{z

(p

^ q ) ^ ( p )
F
F
F
F

same column

{z

contradiction

We see ( p _ q) _ ( p) is always true and is thus a tautology and ( p ^ q) ^ ( p) is always


false and is thus a contradiction.
Note For simplicity, one may use 0 and 1 to denote F and T respectively in the truth
tables.
Theorem (De Morgans Laws)
(i)
(ii)

 ( p ^ q) is equivalent to ( p) _ ( q) , i.e. ( p ^ q)  ( p) _ (q).


 ( p _ q) is equivalent to ( p) ^ ( q) , i.e. ( p _ q)  ( p) ^ (q).

Proof (i) already done in example 2; (ii) can be proved likewise.

6.3 Conditional Statements


The compound proposition implication
p!q
is a conditional statement, and can be read as if p then q or p implies q. Its precise definition
is given by the following truth table
p
T
T
F
F

q
T
F
T
F

p!q
T
F
T
T

Let us briefly see why the above definition via the truth table is reasonable and is consistent with our day to day understanding of the notion of implications. We observe that the only
explicit contradiction to if p then q comes from the case when p is true but q is false, and this
explains the only F entry in the p ! q column. We also note that some people would never
use p implies q to refer to p ! q; they would instead use p implies q to exclusively refer to
p ) q, i.e. p ! q is a tautology. More details on ) can be found in one of the later lectures.

7 PROPOSITIONAL LOGIC

48
Example

5. Let p denote I buy shares and q denote Ill be rich. Then p ! q means If I buy
shares then Ill be rich. Let us check row by row the reasonableness of the truth table
for p ! q given shortly before.
Row 1:
Row 2:
Row 3 and 4:

I buy shares (p true) and Ill be rich (q true) is certainly consistent


with ( p ! q) being true.
I buy shares and I wont be rich means If I buy shares then Ill be
rich (i.e. p ! q) is false.
I dont buy shares wont contradict our statement p ! q, regardless
of whether Ill be rich, as obviously there are other ways to get rich.

Representation

p ! q  ( p ) _ q :

This can easily be proved by the use of the truth table.


Note Obviously a string like p)) ^^ ! qr is not a legitimate logical expression. In this unit, we
always assume that all the concerned strings of logical expressions are well-formed formulas,
or wffs, i.e. the strings are legitimate.

Supplementary Exercises
1. Let p and q be propositions. Show
(a) ( p _ q) ^ (( p) _ q)

(b) p ! q
(c)
(d)

 q.

q ! p.
( p _ q) _ (( p) ^ (q)) is a tautology.
( p _ q) ^ (( p) ^ (q)) is a contradiction.

2. Let p, q and r be propositions. Use a truth table to show that ( p ^ q) _ r is not logically
equivalent to p ^ (q _ r).

7 Propositional Logic
7.1 Argument Forms
We have already encountered a few basic concepts related to propositions. They include true
(T ), false (F), tautology, contradiction, and (^), or (_), not ( or :) as well as implication
(!).

7.1 Argument Forms

49

If we further define an equivalence connective $ for any two propositions p and q by


( p ! q) ^ (q ! p), denoted by p $ q, then the usual order of precedence is
1. connectives within parentheses, innermost parentheses first
2.

;

3.

^, _ ;

!;

4.

5.

From time to time you may also find that some people actually place a higher precedence
for ^ than for _. To avoid possible confusion we shall always insert the parentheses at the
appropriate places.
Example
1. p $ q _r ^ p is same as p $ ((q _ (r)) ^ p). However p $ q _r ^ p would be same
as p $ (q _ ((r) ^ p)) had we adopted higher precedence for ^ than for _.

An argument form, or argument for short, is a sequence of statements. All statements


but the last one are called premises or hypotheses. The final statement is called the
conclusion, and is often preceded by a symbol ), pronounced as therefore. Typically
an argument form will take the form
p1 ; p2 ;

:::;

pn ;

)q

where propositions p1 ; :::; pn are the premises and proposition q is the conclusion. The
argument form in the above can also be represented by

)
I

p1
p2
..
.
pn
q

or by
;

p1
p2
..
.
pn
q

An argument is valid if the conclusion is true whenever all the premises are true. In
other words, an argument form ( p1 ; p2 ; :::; pn ; ) q) is valid if and only the proposition
( p1 ^ p2 ^^ pn ) ! q is a tautology.

The validity of an argument can be tested through the use of the truth table by checking if
the critical rows, i.e. the rows in which all premises are true, will correspond to the value true
for the conclusion.
Examples
2. Show ( p _ q; p ! r; q ! r; ) r) is a valid argument.
Solution From the table

7 PROPOSITIONAL LOGIC

50
p

T
T
T
T
F
F
F
F

T
T
F
F
T
T
F
F

p_q

p!r

q!r

T
F
T
F
T
T
T
T

T
F
T
T
T
F
T
T

T
T
F
T

T
T

F
T

T
T

F
T
F
T
F
F

HHH
Y
Hcritical rows


9


not a critical row

we see all critical rows (in this case, those with the shaded positions all containing a T )
correspond to (the circled) T (true) for r. Hence the argument is valid.
3. Show that the argument ( p ! q; )  p ! q) is invalid.

 p q

p!q

T
T
F

T
F
T

F
F
T

F
T
F

T
F
T

 p
! q
T
T

F

this critical row fails

We see that on the 3rd row, a critical row, the premise p ! q is true while the conclusion
 p ! q is false. Hence the argument ( p ! q; )  p ! q) is invalid.

7.2 Converse, Inverse and Contrapositive


For any given proposition p ! q, p is also known as the premise or hypothesis and q as the
conclusion. For any such a proposition, we have furthermore

I q ! p is the converse of p ! q;
I  p !q is the inverse of p ! q;
I q ! p is the contrapositive of p ! q.
Example
4. Let p be the statement Im sick and q be the statement I go and see a doctor. The
p ! q means If Im sick, then I go and see a doctor. The converse of this statement is
If I go and see a doctor, them Im sick, while the inverse reads If Im not sick, then I
wont go and see a doctor. However, the contrapositive takes the form If I dont go and
see a doctor, then Im not sick.

7.3 Rules of Inference

51

We note that the result in example 3 amounts to saying the inverse of a statement is not
necessarily true. It represents one of the following 2 typical fallacies.

I
I

p ! q,
p ! q,

converse error:
inverse error:

) q! p
)  p !q


invalid argument forms

7.3 Rules of Inference


Rules of inference are no more than valid arguments. The simplest yet most fundamental valid
arguments are

I
I

modus ponens: p ! q; p;
modus tollens:

)q
p ! q q )  p
;

Latin phrases modus ponens and modus tollens carry the meaning of method of affirming
and method of denying respectively. That they are valid can be easily established. Modus
tollens, for instance, can be seen or derived by the following truth table
p
T
T
F
F

q
T
F
T
F

p!q
T
F
T
T

q  p
F
T
F
T

F
F
T
T

shows the validity of the argument form.

I
I
I
I
I
I
I
I

disjunctive addition:

p, ) p _ q.

conjunctive addition:

p, q, ) p ^ q.

conjunctive simplification:

p _ q, q, ) p

disjunctive syllogism:
hypothetical syllogism:
division into cases:

p ^ q, ) p

p ! q, q ! r, ) p ! r

p _ q, p ! r, q ! r; ) r

 p ! contradiction, ) p
contrapositive equivalence: p ! q, ) q !  p
rule of contradiction:

the only critical row




) 


7 PROPOSITIONAL LOGIC

52

The validity of the above argument forms can all be easily verified via truth tables. In fact the
case of division into cases has been proven in example 2. These rules may not mathematically
look very familiar. But it is most likely that everyone has used them all, individually or jointly,
at some stage subconsciously.
The validity of an argument form may be established through the use of the above rules of
inference along with other laws of logic such as De Morgans laws. This type of validation is
particularly useful when the argument form under question contains a relatively larger number
of premises, which means a proof by a truth table is often unrealistically tedious.
Examples
5. Show that the following argument form
p _ q, q ! r, p ^ s ! t,

r, q ! u ^ s, ) t

( )

is valid by breaking it into a list of known elementary valid argument forms or rules. In
other words, show the validity of the above argument form () through the basic inference
rules or the laws of logic.
Solution Well treat all the rules of inference introduced earlier in this subsection as the
known elementary argument forms. The logical inference for the argument form in the
question is as follows.
(1)

)
(2)

)
(3)

(4)

q!r
r
q
p_q
q
p

q ! u ^ s
q
) u^s
u^s
) s

(5)

)
(6)

p
s
p^s

p^s ! t
p^s
t

premise
premise
by modus tollens
premise
by (1)
by disjunctive syllogism
premise
by (1)
by modus ponens
by (3)
by conjunctive simplification
by (2)
by (4)
by conjunctive addition
premise
by (5)
by modus ponens

7.3 Rules of Inference

53

6. Can you explain, in additional details, how those 6 proof steps in the above example come
into existence?
Solution In the following we shall give the reverse route for the proof of the above
argument form. Basically we shall start with the conclusion t (marked by 1 below),
then go downwards towards other required intermediate propositions by making use
of the known premises. The final proof of the argument form will be essentially in the
reverse order of the reverse route. The main idea is to try to lead from the conclusion
to eventually reach only the premises.
1 t

p^s ! t

2 p^s

Final objective
The only premise that involves t explicitly is
p ^ s ! t. Hence to derive t (i.e. t is true)
one needs p ^ s

3 p

Conjunctive addition of p and s


4 s

p_q

 q ! u^s

To derive p from p _
q, one needs  q
(disjunctive syllogism)

q

q

q!r
6

r

All lead (downwards) to known premises

To have  q from q ! r one must have  r


From the diagram above, we see that we could derive the conclusion t in the order of
6

!5!

3;

4;

3; 4

With the help of the above order of derivation, we have


(a)
(b)
(c)
(d)

 r q ! r ) q (modus tollens)
 q p _ q ) p (disjunctive syllogism)
 q  q ! u ^ s ) u ^ s (modus ponens)
u^s ) s
(conjunctive simplification)
;

!2!

7 PROPOSITIONAL LOGIC

54
(e) p; s; ) p ^ s

(conjunctive addition)

(f) p ^ s; p ^ s ! t ; ) t

(modus ponens)

That is, the conclusion is derived from the use of the basic inference rules.
7. Can you do example 6 once again, with some differences?
Solution The dumbest way is to try to determine for each proposition (symbol) if it
is true or not. This way one could be wasting a lot of time unnecessarily; but this often
ensures one gets closer and closer to the solution of the problem.
In the argument form in examples 5-6, we have 6 basic propositions p, q, r, s , t and u. We
will now proceed to determine (in the order dictated by the actual circumstances) whether
each of these 6 propositions is true or not.
(i)

r

is given as a premise, so the truth value of proposition r is already determined


(r is false).

(ii) q ! r;  r; )

q

, so the truth value of q is determined (q is false).

(iii) p _ q;  q; ) p , so the truth value of p is determined (p is true).


(iv)
(v)
(vi)
(vii)
(viii)

 q  q ! u ^ s ) u ^ s.
u ^ s ) u , so u is determined (u is true).
u ^ s ) s , so s is determined (s is true).
p s ) p ^ s.
p ^ s p ^ s ! t ) t , so the truth value of t is determined (t is true).
;

;
;

We have by now established the truth value of all the concerned basic propositions p; q; r; s; t ; u.
We can now proceed to determine if the conclusion t of the original argument form is true
or not. In this case, it is obvious because we have already shown t is true. So the argument
form (*) in example 5 is valid. Notice that step (v) is completely unnecessary. But we
probably wouldnt know it at that time.

Note This dumb method can be very useful if you want to determine whether or not
the conclusion of a lengthy argument form is true or not. Ill leave you to think about
why.
8. Let p, q, r, s and t be propositions.
(a) Use a truth table to show p ! q is equivalent to ( p) _ q.
(b) Use a truth table to determine whether or not the following argument form is valid
p ! q;

q ! r;

)r

7.3 Rules of Inference

55

(c) Show the following argument form


r ! p;

 p_q

s ! p ^ r;

 p^  r ! s _ t  q ) t
;

is valid by deducing the conclusion from the premises step by step through the use
of basic inference rules.
Solution
(a) From the truth table below
p
T
T
F
F

q
T
F
T
F

 p ( p ) _ q
F
F
T
T

T
F
T
T

p!q
T
F
T
T

we see that the last two columns, corresponding to ( p) _ q and p ! q respectively,


are exactly the same. This means ( p) _ q is equivalent to p ! q.
(b) The truth table for the argument form reads
p
T
T
T
T
F
F
F
F

q
T
T
F
F
T
T
F
F

r
T
F
T
F
T
F
T
F

p!q
T
T
F
F
T
T
T
T

q!r
T
F
T
T
T
F
T
T

r
T
F
T
F
T
F
T
F

Among the 8 rows in the above table, the 1st, 5th, 7th and 8th rows are critical rows.
Since the conclusion r fails at the last critical row, the argument form is not valid.
(c) The derivation is as follows:
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)

 q  p _ q ) p (disjunctive syllogism)
 p r ! p ) r (modus tollens)
 p  r ) p^  r (conjunctive addition)
 p^  r  p^  r ! s _ t ) s _ t (modus ponens)
 r ) r _ p (disjunctive addition)
s ! p ^ r is the same as s ! ( r _ p)
(De Morgans laws)
 r _ p s ! ( r _ p) ) s (modus tollens)
s_t  s ) t
(disjunctive syllogism)
;

Hence the argument form is valid.

7 PROPOSITIONAL LOGIC

56

9. A detective established that one person in a gang comprised of 4 members A; B; C and D


killed a person named E. The detective obtained the following statements from the gang
members (SA denotes the statement made by A. Likewise for SB , SC and SD )
(i) SA :

B killed E.

(ii) SB :

C was playing pool with A when E was knocked off.

(iii) SC :

B didnt kill E.

(iv) SD :

C didnt kill E.

The detective was then able to conclude that all but one were lying. Can you decide who
killed E?
Solution Let (1)(4) to be given later on be 4 statements. The 1st two, (1) and (2) below,
are true due to the detectives work.
(1): Only one of the statements SA ; SB ; SC ; SD . is true
(2): One of A; B; C and D killed E.
From the (content of the) statements SA and SC we know
(3): SA ! SD is true because if SA is true, then B killed E which implies C didnt kill E
due to (2), implying SD is also true.
and from (1)
(4): SA ! SB ^SC ^SD is true.
Let us examine the following sequence of statements.
(a)
(b)
(c)
(d)
(e)
(f)

SA ! SD
SA ! SB ^SC ^SD
SB ^SC ^SD ! SD
S A ! S D
 SD !  SA
S A ! S A

SA true implies SD true, i.e. (SA ; ) SD ) (from (3))


SA true implies SB , SC and SD all false (from (4))
conjunctive simplification (direct)
hypothetical syllogism (from (b), (c))
modus tollens (from (a))
hypothetical syllogism (from (d), (e))

We note all the statements on the sequence apart from the first two (a) and (b) are obtained
from their previous statements or form the valid argument forms. However the first 2
statements (a) and (b) are both true hence the conclusion in (f) is also true. A statement
sequence of this type is sometimes called a proof sequence with the last entry called a
theorem. The whole sequence is called the proof of the theorem.
Alternatively sequence (a)(f) can also be regarded as a valid argument form in which a
special feature is that the truth of the first 2 statements will ensure that all the premises
there are true.

7.3 Rules of Inference

57

From (3) and (4) and (a)(f) we conclude SA ! SA is true. Hence SA must be false from
the rule of contradiction (if SA were true then SA would be true, implying SA is false:
contradiction).
From the definition of SC we see SA ! SC . From the modus ponens

SA ! SC SA ) SC
;

we conclude SC is true. Since (1) gives


SC ! SA ^SB ^SD ;
we obtain from the (conjunctive simplification) argument
SC ! SA ^SB ^SD , SA ^SB ^SD ! SD , ) SC !SD
that SC !SD . Finally from modus ponens (SC
is true, that is, C killed E.

!SD, SC , )SD ) , we conclude SD

We note that in the above example, we have deliberately disintegreted our argument into smaller
pieces with mathematical symbolisation. It turns out that verbal arguments in this case are much
more concise. For a good comparison, we give below an alternative solution.
Solution (alternative for example 6) Suppose A wasnt lying, then As statement B killed
E is true. Since A spoke the truth means B; C and D would be lying, hence the statement
C didnt kill E said by D would be false, implying C did kill E. But this is a contradiction
to the assumption A spoke the truth. Hence A was lying, which means B didnt kill E,
which in turn implies C spoke the truth. Since only one person was not lying, D must
have lied. Hence C didnt kill E is false. Hence C killed E.

N
Supplementary Exercises
1. Use a truth table to show that the conditional statement p ! q doesnt imply its converse
q ! p.
2. Let p, q and r be propositions. Is the following
p;

q;

 p ^q ) r
;

a valid argument form? How many criticial rows are there in the truth table?

8 PREDICATE CALCULUS

58

3. Let p, q, r and s be propositions. Show the following argument form


p ! r;

p_  q;

 r_q_s

r;

)s

is valid by deducing the conclusion from the premises step by step through the use of
basic inference rules.
4. Let p1 ; p2 ; :::; pn and q be propositions. Show that the argument form ( p1 ; p2 ; :::; pn; ) q)
is valid if and only if p1 ^ p2 ^^ pn ^ (q) is a contradiction.

8 Predicate Calculus
8.1 Predicate Quantifiers
A predicate is a sentence that contains a finite number of variables and becomes a statement
when specific values are substituted for the variables. The domain of a predicate variable is the
set of all values that may be substituted in place of the variable.
In predicate calculus, the following 2 quantifiers are important

I
I

8,
existential quantifier: 9,
universal quantifier:

meaning for all


meaning there exists

Example
1. The statement that j sin(x)j  1 for any real-valued number x can be written in either of
the following forms

18xBM2 R j|sin(x{z)j  1}


quantifier
predicate
B
predicate variable
;

8x

(x

2 R ! j{zsin(x)j  1})

predicate

Hence we see that the domain of a predicate could also be absorbed in the predicate itself.
Let P(x) and Q(x) be predicates (of a common domain D). We introduce the following two
useful notations

A universal statement (8x 2 D; P(x)) is true if and only if P(x) is true for every x 2 D.

8.1 Predicate Quantifiers

59

An existential statement (9x 2 D; P(x)) is true if and only if P(x) is true for at least one
x 2 D.

I
I

P(x) ) Q(x)

means (8x 2 D; P(x) ! Q(x)) is true.

P(x) , Q(x)

means (8x 2 D; P(x) $ Q(x)) is true.

We note that in the case of p and q being propositions, then p ) q simply means p ! q is a
tautology. Moreover p , q means both p ) q and q ) p. We also note that the predicate variable x actually represents all the predicate variables. In general a statement may involve many
mathematical or logical operations. It is thus worthwhile to list a finer order of precedence
1. parentheses;

2. ;

3. ^, _;

4. ! ;

5. $ ;

6. ), ,;

7. 8, 9

Examples
2. Let N be the set of natural numbers, i.e.
following statements.

N = f0

1; 2; :::g. Represent mathematically the

(a) The sum or subtraction of any natural number will remain an natural number.
(b) A natural number, if divided by an natural number, may not remain an natural number.
(c) There exist 2 natural numbers such that the sum of the squares of these 2 natural
numbers can be written as the square of an natural number.
(d) The sum of the squares of any 2 natural numbers can be written as the square of an
natural number.
Solution Well explain in more details in the first 2 cases.
(a) This statement can be written in any of the following 3 forms
8m; n 2 N , ((m + n) 2 N ) ^ ((m n) 2 N ) ,
8m 2 N , 8n 2 N , ((m + n) 2 N ) ^ ((m n) 2 N ) ,
8m 2 N , ( 8n 2 N , ((m + n) 2 N ) ^ ((m n) 2 N ) ) .
This is because essentially (m + n) 2 N says the sum of 2 natural numbers is again
an natural number, and (m n) 2 N says likewise for the subtraction of 2 natural
numbers. We note that while the 1st form is perhaps the neatest, the 1st and the
2nd forms can both be interpreted as an equivalent shorthand notation of the 3rd
form. Moreover, the 3rd form can be regarded as an embedded predicate statement
because the 3rd form can considered as

8m 2 N

P(m) ;

where P(m) for each given m is again a predicate statement:

8n 2 N

((m + n)

2 N ) ^ ((m

n) 2 N )) :

8 PREDICATE CALCULUS

60

(b) First we rephrase the original sentence in a form closer to the mathematical language. It is easy to observe that the original sentence is equivalent to There exist 2
natural numbers such that the division of the 1st natural number by the 2nd natural
number is no longer a natural number. Written symbolically,

9m n 2 N
;

m
n

62 N

Obviously the above form can also be written in other equivalent variant forms similar to the case (a).
(c)
(d)

9m 2 N , 9n 2 N , 9 p 2 N ,
8m 2 N , 8n 2 N , 9 p 2 N ,

m2 + n 2 = p 2 .
m2 + n 2 = p 2 .

3. Are the 2 statements (a) and (b) below true?


(a)
(b)

9m 2 N , 9n 2 N , 9 p 2 N , m2 + n2 = p2 and m n p  1.
8m 2 N , 8n 2 N , 9 p 2 N , m2 + n2 = p2 .
;

Solution We note that more than 1 quantifier are used in both (a) and (b).
(a) Though m = 0; n = 0 and p = 0 satisfy m2 + n2 = p2 , they do not satisfy m; n; p  1.
Hence this case is not able to shed much light on the validity of the statement (a).
However m = 3, n = 4 and p = 5 satisfy both m2 + n2 = p2 and m; n; p  1. We see
that (a) is true regardless of other cases of m; n and p.
(b) For m = 0; n = 0, we can choose p = 0 so that m2 + n2 = p2 , i.e. the predicate is
true in this particular case. However, for m =p1 and n = 1, we see that if a p 2 N
satisfies p2 = m2 + n2 = 2, then p must be = 2. But no such nonnegative integers
p satisfy p2 = 2. Hence (b) is false.
4. For those who happen to know a little about taking a limit of a sequence, we just mention
that limn! an = a if the following statement is true

8 2 fx 2 R x
;

>

0g; 9N 2 N ; 8n 2 fx 2 N ; n  N g;

8.2 Predicate Properties

negation of quantifiers

(8x 2 D P(x)) is equivalent to (9x 2 D P(x)).


(9x 2 D P(x)) is equivalent to (8x 2 D P(x)).
;

jan

aj < :

8.2 Predicate Properties

universal modus ponens

8x P(x) ! Q(x))

P(a); ) Q(a)

universal modus tollens

8x P(x) ! Q(x)) Q(a) ) P(a)

I
I

61

rules of inference in the previous lecture may be similarly utilised.


vacuous truth
Statement (8x 2 D, P(x) ! Q(x)) is vacuously true (therefore also true) if and only if D
is an empty set, or P(x) is false for every x in D.

Examples
5. What is the negation of the statement Everyone loves someone?
Solution Let D denote the set of all people, and predicate L(x; y) denote that person x
loves person y. Then the statement Everyone loves someone can be written as

8x 2 D 9y 2 D
;

L(x; y)

or equivalently (8x 2 D; (9y 2 D; L(x; y) ). Hence the negation is

9x 2 D 8y 2 D L(x y)
;

which means There is someone who loves no one.


We note here the order in which the quantifiers appear is important. If for instance we
change the order of quantifiers for the original statement and consider instead

9y 2 D 8x 2 D
;

L(x; y) ;

we see that this new statement is true means that Theres someone everyone loves which
is quite different from the original statement.
6. Let S be the set of all students and A, the set of all assignments. Let predicate D(x; y)
denote that student x has done assignment y, and predicate P(x) denote that student x
passes this unit. Represent the predicate statement If a student does all the assignments,
then he or she will pass this unit.
Solution We begin with writing out certain related simpler but useful components. We
first observe that student x does all assignments can be written as

8y 2 A

D(x; y) :

8 PREDICATE CALCULUS

62

Hence the statement If student x does all the assignments, then he or she will pass this
unit can be written as

8y 2 A

D(x; y))

! P(x)

It is now straightforward to see that the original statement If a student does all the assignments, then he or she will pass this unit can thus be simply denoted by

8x 2 S

8y 2 A

D(x; y))

! P(x)

For any statement (8x 2 D, P(x) ! Q(x)) , we define its contrapositive by (8x 2 D,
Q(x) ! P(x)), its converse by (8x 2 D, Q(x) ! P(x)) and its inverse by (8x 2 D, P(x) !
Q(x)). Then the original universal conditional statement is equivalent to the contrapositive,
and the converse is equivalent to the inverse.
The converse and inverse, as in the propositional logic, are in general not equivalent to
the original statement. The fallacies due to wrongly assumed such equivalences are termed
converse error and inverse error respectively.
Example
7. Give the contrapositive, converse and inverse of the statement If a real number is greater
than 3 then its square is greater than 4.
Solution The statements can be written as
original:
contrapositive:
converse:
inverse:

8x 2 R (x 3) ! (x2 4)
8x 2 R (x2  4) ! (x  3)
8x 2 R (x2 4) ! (x 3)
8x 2 R (x  3) ! (x2  4)
;

>

>

;
;
;

>

>

(a)
(b)
(c)
(d)

It is easy to see that (a) and (b) are both true, while (c) and (d) are both wrong. For
example, if we take x = 2:5 in (c) we have x2 = 2:52 > 4 but x > 3 is false. Hence (c) is
false.

In predicate calculus, an argument is valid if no matter what particular predicates are substituted for the predicate symbols in its premises, the conclusion is true whenever the resulting premise statements are all true. We say 8x, P(x) is a sufficient condition for Q(x) if
(8x, P(x) ! Q(x)) is true, and we say 8x, P(x) is a necessary condition for Q(x) if (8x,
P(x) ! Q(x)) is true. If 8x, P(x) is both a sufficient and a necessary condition for Q(x),
then we say 8x, P(x) if and only if (iff) Q(x).

8.2 Predicate Properties

63

Examples
8. Statement One has to drink water in order to survive, means for any person, drinking
water is a necessary condition for survival, i.e.

8 person x (x drinks water) ! (x survives)


;

It is obviously not a sufficient condition because drinking water alone is not enough to
guarantee the survival of a person, i.e. the statement

8 person x

(x

drinks water)

! (x survives)

is not true.
9. Prove that the statement (8n 2 N ; log2 (n + 1000)  n) is not true.

Solution To prove (8n 2 N ; log2 (n + 1000)  n) is not true is to prove that its negation,
 (8n 2 N ; log2 (n + 1000)  n), is true. Since

 ( 8n 2 N

log2 (n + 1000)  n )

 ( 9n 2 N  (log2(n + 1000)  n) )
 ( 9n 2 N log2(n + 1000) n )
;
;

<

we just need to show that there exists a nonnegative integer n0 such that log2 (n0 + 1000) <
n0 . If we try the first few integers in N , we see that n0 = 10 will suffice our purpose
because log2 (10 + 1000) < 10, i.e. 1010 < 210 = 1024. Obviously such n0 is not unique,
and the uniqueness of such n0 is in fact irrelevant.
The rules in predicate logic can be represented as axioms symbolically. For example, we
can easily convince ourselves that the followings are true

I P ) (Q ! P) ,
I (Q ! P) ) (P ! Q) ,
I (8x P(x) ! Q(x)) ) (8x P(x)) ! (8x Q(x)) .
;

Supplementary Exercises
1. Let D denote the set of all people, and predicate L(x; y) denote that person x loves person
y. Represent mathematically the statement love between two people is mutual.
2. Let R be the set of all real numbers. What is the negation of the statement

8x 2 R

(x > 3)

! (x2

>

4) ?

Write down this negation as a sentence. Is this negated statement true?


3. Suppose a student will pass this unit if he or she does all the assignments. According to
this statement, is there a sufficient condition for a student to pass this unit? If yes, what is
it? Is there a necessary condition for a student to pass this unit? If yes, what is it?

9 GRAPHS AND THEIR BASIC TYPES

64

9 Graphs and Their Basic Types


9.1 Graphs and Directed Graphs
A graph G consists of

I
I
I

a set V containing all vertices of G,


a set E containing all edges of G,
an edge-endpoint function on G which associates each edge a unique pair of end points
on V .

It is denoted by G = (V; E ; ), or often simply by G = (V; E ). Also for a given graph G, we use
V (G) and E (G) to denote respectively the corresponding vertex set V and edge set E.
Note

I
I

A graph is by default an undirected graph which means a typical edge e is represented by


the set fv; wg of its vertices through the edge-endpoint function , i.e. (e) = fv; wg.
A graph is called a directed graph, or digraph, if a typical edge in E is represented by
an ordered pair (v; w) of its vertices, i.e. (e) = (v; w). An edge in a digraph is sometimes
also called a directed edge or an arc.

Examples
1. In the graph below (heavy dots denote vertices, lines or arcs denote edges)
e3
e1
e2
r

v1

v2

e4

v3
r

e5

the vertex set V = fv1 ; v2 ; v3 ; v4 ; v5 g, the edge set E


endpoint function is given by
edge e
e1
e2
e3
e4
e5

v4

fe1

endpoints set (e)

fv1 g

fv1
fv2
fv2
fv2

v2 g
; v3 g
; v3 g
; v3 g
;

v5

e2 ; e3 ; e4 g and the edge-

9.2 Basic Types and Features of Graphs

65

Note The e1 type edge is called a loop, and e3 ; e4 ; e5 type edges are called multiple
edges or parallel edges.
2. In the directed graph below

e1

>v1

e2

e3

1/
r

v2

e4

the vertex set V = fv1 ; v2 ; v3 g, the edge set E


function is given by

fe1

r v3

e2 ; e3 ; e4 g, and the edge-endpoint

edge e

endpoints pair (e)

e1
e2
e3
e4

(v1 ; v1 )
(v1 ; v2 )
(v3 ; v2 )
(v2 ; v3 )

9.2 Basic Types and Features of Graphs

I
I

A simple graph is a graph that has neither loops nor parallel edges. In such cases, the
identification of an edge e with its endpoints (e) will not cause confusion. Thus an edge
with endpoints v and w may be denoted by fv; wg in simple graphs.
A complete graph is a simple graph in which each pair of vertices are joined by an edge.

Examples
3. The complete graph with n vertices is denoted by Kn . The first few are
r
r

K1

K2

K3

K4

4. A graph is complete bipartite if it is a simple graph, and its vertices can be put into two
groups so that any pair of vertices from different groups are joined by an edge while no
pair of vertices are joined by an edge if they are from a same group. Such a graph is
denoted by Km;n if the two groups contains exactly m and n vertices respectively. For
example,

9 GRAPHS AND THEIR BASIC TYPES

66
r

K1;2

K2;3

K3;3

A graph H is a subgraph of graph G iff


(i) V (H )  V (G),
(ii) E (H )  E (G),
(iii) every edge in H has same endpoints as in G.
Examples
5. Suppose we let V
can be drawn as

fv1

v2 ; v3 g, E

feg and 1 (e) = fv1 v2 g, then graph G1(V E 1)


;

v
r 2
r

v1

v3

If we choose 2 (e) = fv2 ; v3 g then graph G2 = (V; E ; 2 ) represents


v
r 2
r

v1

v3

Although it is obvious that


V (G2 )  V (G1 ) ;

E (G2 )  E (G1 ) = feg ;

the endpoints of e in G2 are different from those of e in G1 . Hence G2 is not a subgraph


of G1 .
Note Intuitively we see from the diagram that e = fv2 ; v3 g in G2 is not an edge of
G1 breaks the condition (ii) in this definition of a subgraph. This argument is however
based on the implicit edge association with the corresponding end points. In other words,
condition (iii) may be dropped, as in some texts, if the condition (ii) is extended in this
sense.

9.2 Basic Types and Features of Graphs

67

6. For graph G

rv2

e2

e1
v1
graph H

r
r

e4

v3
e3

v4

and graph F

rv2

rv2

e1
v1

e1

v1

v4

e2
r

v3

v4

are both subgraphs of G.


For any vertex v of a graph G, its degree is the number of incidences of edges at the vertex,
and is denoted by (v), or deg(v).
Example
7. For graph
v1

r

rv2
r

v3

v4



we have (v1 ) = 5, (v2 ) = 4, (v3 ) = 1 and (v4 ) = 0.


Note For any graph G, if we denote by E the total number of edges, and by N the sum of the
total degrees of all the vertices, then N = 2E . This is because every edge will supply exactly
1 to the degrees of each of its two end vertices. Hence N must be always even. For the graph
in the above example, for instance, we have E = 5 and N = (v1 ) + (v2 ) + (v3 ) + (v4 ) =
5 + 4 + 1 + 0 = 10, i.e. we have N = 2E .
Let v and w be vertices of a graph G. Then a walk from v to w is a sequence of the form
v0 ; e1 ; v1 ; e2 ;  ; vn
or alternatively
v0

1 ; en ; vn

! v1 e!  e ! vn

e1

n 1

! vn

en

or simply v0 e1 v2 e2  vn 1 en vn , such that v0 = v, vn = w and vi and vi 1 are the endpoints


of ei for i = 1;  ; n. The number of edges, n, is called the length of the walk. A trivial walk
consists of a single vertex, and has thus a zero length. A graph G is connected iff every pair of
vertices of G is joined by a walk. A bridge is an edge whose removal will cause the graph to
become disconnected
Note Some texts use the word path instead of the word walk, while others may use path to
denote a walk with distinct edges.

9 GRAPHS AND THEIR BASIC TYPES

68

Note An orderd edge list is a list of the edges using the vertices to define the edges and to
give a direction along these edges. For example, in graph G above, an odered edge list would
be fv1 v2 ; v1 v4 ; v2 v3 ; v3 v4 g.
Example
8. Graph G
v5r

rv1

e1
r

v3

v2 e2

e3
v4

e4

v6

e5

v7

is disconnected, because we cant walk from vertex v4 to for instance vertex v1 , i.e. there
is no walk from v4 to v1 . Obviously G can be decomposed into three connected components G1 ; G2 and G3 .
V (G1 )
V (G2 )
V (G3 )

=
=
=

fv1 v2 v3 g
fv4 g
fv4 v5 v6 g
;

E (G1 )
E (G2 )
E (G3 )

fe1 e2 g ;
? (the empty set) ;
fe3 e4 e4 g

=
=

This way each of G1 ; G2 and G3 is a connected subgraph.

9.3 Planar Graphs

A graph is planar iff it can be drawn in a 2-dimensional plane without any accidental crossing. We introduce without proof the following important result in this connection.
Kuratowskis Theorem A graph is nonplanar if and only if it can be obtained from either K5
or K3;3 by adding some, or no, vertices and edges.
Examples
9. Graph

is planar because it can be drawn as

10. Graph
v1

v
v2

69
is nonplanar because, after removing edges e and f , and vertex v, the graph becomes
K3;3 . Hence Kuratowskis Theorem implies the original graph is nonplanar. We note that
removal of vertex like v (and then connecting the end points v1 and v2 ) is called series
reduction. If a graph is planar, then obviously any of its subgraphs is also planar, even if
series reductions are further performed. Hence Kuratowskis Theorem can be rephrased
as saying a graph is nonplanar iff it contains a subgraph which, after series reduction, is
K5 or K3;3 .
The algorithmic testing of planarity of a graph is actually very complicated and relies on
various other results. For reference pointers, see the book by Narsingh Deo, Graph Theory with
Applications to Engineering and Computer Science, Prentice-Hall, 1974.
Note In some texts an edge e in a graph G is actually implicitly regarded as an entity containing
the edge label e and the corresponding endpoints, i.e. (e; (e)) where is the edge-endpoint
function. Since for a given graph G a label e is sufficient to determine uniquely the edge entity
(e; (e)), a graph G may be regarded as consisting of only its vertices V (G) and its edges E (G)
in this generalised sense, as the endpoint function is now absorbed into the edges. This way a
graph H is a subgraph of G, for instance, iff V (H )  V (G) and all edges of H are also edges of
G.

Supplementary Exercises
1. Construct a connected graph of 4 vertices of degrees 1, 2, 3 and 4 respectively, assuming
the graph has no multiple edges.
2. Let n  1. How many edges are there in Kn;2n ? What is the length of a longest walk in
Kn;2n from one vertex to another without walking on any edge and any vertex more than
once?

10 Eulerian and Hamiltonian Circuits


10.1 Two Practical Problems
In order to see how graphs can be used to denote, or interpret, or even solve some practical
problems, we first give below two well-known cases.
Seven bridge problem
Two islands surrounded by a river are connected to each other and the riverbanks by seven
bridges as shown below

10 EULERIAN AND HAMILTONIAN CIRCUITS

70

A
B
river

> :

bridges

river

Then is it possible to walk over each bridge exactly once, starting


from one of the locations A; B; C; D and ending at the same location? If we use vertices to denote the locations and edges to denote
bridges, then the map is simplified to the graph on the right

Ar
B

Travelling salesman problem


Suppose the distances between each pair of the cities A; B; C and D are given, and suppose
a salesman must travel to each city exactly once, starting and ending at city A. Which route
from city to city will minimize the total travelling distance? If we use vertices to denote cities,
and put the distance between any two cities on the edge joining them, then we can represent the
given knowledge by the following weighted graph
B
30
s

sC

30

35

50
40

25
s

Before considering the above two problems in detail, we first introduce below a few related
basic terminology.

10.2 Eulerian and Hamiltonian Paths and Circuits

I
I
I
I

A circuit is a walk that starts and ends at a same vertex, and contains no repeated edges.
An Eulerian circuit in a graph G is a circuit that includes all vertices and edges of G. A
graph which has an Eulerian circuit is an Eulerian graph.
A Hamiltonian circuit in a graph G is a circuit that includes every vertex (except first/last
vertex) of G exactly once.
An Eulerian path in a graph G is a walk from one vertex to another, that passes through
all vertices of G and traverses exactly once every edge of G. An Eulerian path is therefore
not a circuit.

10.2 Eulerian and Hamiltonian Paths and Circuits

71

A Hamiltonian path in a graph G is a walk that includes every vertex of G exactly once.
A Hamiltonian path is therefore not a circuit.

Examples
1. In the following graph

e2
r

e1
v1

v2

e3
r

e4

v3

(a) Walk v1 e1 v2 e3 v3 e4 v1 ; loop v2 e2 v2 and vertex v3 are all circuits, but vertex v3 is a
trivial circuit.
(b) v1 e1 v2 e2 v2 e3 v3 e4 v1 is an Eulerian circuit but not a Hamiltonian circuit.
(c) v1 e1 v2 e3 v3 e4 v1 is a Hamiltonian circuit, but not an Eulerian circuit.
2. K3 is an Eulerian graph, K4 is not Eulerian.
r

3. Graph

has an Eulerian path but is not Eulerian.

r
r

Eulers Theorem Let G be a connected graph.


(i) G is Eulerian, i.e. has an Eulerian circuit, if and only if every vertex of G has even degree.
(ii) G has an Eulerian path, but not an Eulerian circuit, if and only if G has exactly two
vertices of odd degree. The Eulerian path in this case must start at any of the two odddegree vertices and finish at the other vertex.
Proof We only consider the case (i).
(a) We first show G is Eulerian implies all vertices have even degree.
Let C be an Eulerian (circuit) path of G and v an arbitrary vertex. Then each edge in C
that enters v must be followed by an edge in C that leaves v. Thus the total number of
edges incident at v must be even.
one edge leaves

one edge enters v

^j > 

r
r

10 EULERIAN AND HAMILTONIAN CIRCUITS

72

(b) We then show by induction that G is Eulerian if all of its vertices are of even degree.
Let Sn be the statement that connected graph of n vertices must be Eulerian if its every
vertex has even degree.
For n = 1, G is either a single vertex or a single vertex with loops. Hence S1 is true
because an Eulerian circuit can be obtained by traversing all loops (if any) one by one.
For inductions we now assume Sk is true, and G has k + 1 vertices. Select a vertex v of
G. We form a subgraph G0 with one vertex less as follows: remove all loops of v and
break all remaining edges incident at v; remove v and connect in pairs the broken edges
in such a way G remains connected. Since the degrees of the vertices remain even when
G is reduced to G0 , the induction assumption implies the existence of an Eulerian circuit
of G0 . The Eulerian circuit of G can thus be constructed by traversing all loops (if any) at
v and then the Eulerian circuit of G0 starting and finishing at v. Hence G is Eulerian and
Sk+1 is true, implying Sn is true for all n  1. For clarity and intuitiveness, the induction
step is exemplified by the following graphs
G0

G
r
r

)


break
edges
r

 -v

r
r

M


i
R

break edges 
r


r




r
r

Put back the deleted parts


induces a Eulerian circuit for G:

glued together

wM
R z
i M
Rk


r


Examples
4. Due to the above Eulers theorem, the seven bridge problem described earlier has no
solution, i.e. the graph in the problem does not have an Eulerian circuit because all
vertices there (A; B; C; D) have odd degrees.
5. As for the travelling salesman problem, we need to find all the Hamiltonian circuits for
the graph, calculate the respective total distance and then choose the shortest route.

10.3 Fleurys Algorithm for Finding Eulerian Path or Circuit


route
A !B !C !D!A
A !B !D!C !A
A !C ! B ! D ! A
A !C ! D ! B ! A
A !D !B !C !A
A ! D !C ! B ! A

73

total distance
30 + 30 + 25 + 40 = 125
140
155
140
155
125

Hence the best route is either ABCDA or ADCBA.

10.3 Fleurys Algorithm for Finding Eulerian Path or Circuit


(i) If there are odd degree vertices (there then must be exactly two if an Eulerian path is to
exist), choose one. Travel over any edge whose removal will not result in breaking the
graph into disconnected components.
(ii) Rub out the edge (or colour the edge if you like) you have just traversed, and then travel
over any remaining edge whose removal will not result in breaking the remaining subgraph into disconnected components.
(iii) Repeat (ii) until other edges are rubbed out or coloured.
You may consult for further details, if you wish, the book by John E Munro, Discrete Mathematics for Computing, Thomas Nelson, 1992.
Example
6. Find an Eulerian path for the graph G below
s v6

v3
s

v2

e7

e3

e2

v5

e5

v4

e1

e8

s



e4

e6

e9

v1

v7

We start at v5 because (v5 ) = 5 is odd. We cant choose edge e5 to travel next because
the removal of e5 breaks G into 2 connected parts. However we can choose e6 or e7 or e9 .
We choose e6 . One Eulerian path is thus
v5

! v5 e! v6 e! v7 e! v5 e! v4 e! v3 e! v2 e! v1 e! v4

e6

11 GRAPH ISOMORPHISM AND MATRIX REPRESENTATIONS

74

Supplementary Exercises
1. Is K4 Eulerian? Is K2;3 Eulerian?
2. Find an Eulerian circuit for K2;4 .
3. Does K2;3 contain a Hamiltonian circuit? Does it contain a Hamiltonian path?

11 Graph Isomorphism and Matrix Representations


11.1 Graph Isomorphism and Isomorphic Invariants
A mapping f : A ! B is one-to-one if f (x) 6= f (y) whenever x; y 2 A and x 6= y, and is onto if for
any z 2 B there exists an x 2 A such that f (x) = z. In other words, a one-to-one function maps
different elements to different elements, while onto function implies f (A) reaches everywhere
in B.
Two graphs G1 and G2 are isomorphic if there exist one-to-one and onto functions g:
V (G1 ) ! V (G2 ) and h: E (G1 ) ! E (G2 ) such that for any v 2 V (G1 ) and any e 2 E (G1 ), v
is an endpoint of e if and only if g(v) is an endpoint of h(e). The pair of functions g and h is
called an isomorphism.
Roughly speaking, graphs G1 and G2 are isomorphic to each other if they are essentially
the same. More intuitively, if graphs are made of elastic bands (edges) and knots (vertices),
then two graphs are isomorphic to each other if and only if one can stretch, shrink and twist
one graph so that it can sit right on top of the other graph, vertex to vertex and edge to edge.
The isomorphism functions g and h will thus provide the one-to-one correspondences for the
vertices and the edges respectively. We recall that V (G) and E (G) denote the vertex set and the
edge set of the graph G respectively.
Example
1. Show graphs G1 and G2 below are isomorphic.
v2
s

v1 e4

f1

f2

e3

e1
s

w1

e2
s

f4
e5

v4
G1

v3

w2

s
s

f3
f5
w4
G2

w3

11.1 Graph Isomorphism and Isomorphic Invariants

75

Solution How to find isomorphism function g and h in general will be clearer when we
introduce the concept of isomorphism invariants later on. But at this stage it is mostly
guesswork. Let g and h be given by
g(v1 ) = w2 ;
h(e1 ) = f2 ;

g(v2 ) = w1 ;

h(e2 ) = f1 ;

g(v3 ) = w4 ;

h(e3 ) = f4 ;

g(v4 ) = w3 ;

h(e4 ) = f3 ;

h(e5 ) = f5

which can be alternatively represented in the diagrams below


V (G1 )

- V (G 2 )

E (G 1 )

- E (G2)

#


#


v1
v2
v3
v4

*jw1
w2
* w3
j w4


"!

e1
e2
e3
e4
e5

* f1
j f2
* f3
j f4
- f5


"!

We now verify the preservation of endpoints under g and h


e1 + fv1 ; v2 g

e2 + fv2 ; v4 g
e3 + fv2 ; v3 g
e4 + fv1 ; v4 g
e5 + fv3 ; v4 g

!
!
!
!
!

f2 + fw1 ; w2 g = fg(v1 ); g(v2 )g


f1 + fw1 ; w3 g = fg(v2 ); g(v4 )g
f4 + fw1 ; w4 g = fg(v2 ); g(v3 )g
f3 + fw2 ; w3 g = fg(v1 ); g(v4 )g
f5 + fw3 ; w4 g = fg(v3 ); g(v4 )g

where we used e + fv; wg to indicate that edge e has endpoints fv; wg. Since g and h are
obviously one-to-one and onto, the pair g and h thus constitute an isomorphism of graphs
G1 and G2 , i.e. G1 and G2 are isomorphic.
Isomorphic graphs are same in shapes, so properties on shapes will remain invariant
for all graphs isomorphic to each other. More precisely, a property P is called an isomorphic
invariant if and only if given any graphs isomorphic to each other, all the graphs will have the
property P whenever any of the graphs does. There are many isomorphic invariants, e.g.
(a)
(c)
(e)
(g)

vertices of a given degree,


number of connected components,
number of loops,
has a Hamiltonian circuit.

(b) number of edges,


(d) has a circuit of given length,
(f) number of sets of parallel edges,

Incidentally, an isomorphic invariant is sometimes also referred to as an isomorphism invariant.

11 GRAPH ISOMORPHISM AND MATRIX REPRESENTATIONS

76
Examples

2. Graphs G1 and G2 below


r

r
r


r

r



G1

G2

are not isomorphic to each other because vertex v of G1 has degree 5 while no vertices of
G2 have degree 5.
3. Back to example 1. We now explain briefly how we found the isomorphism functions g
and h there.
First, since v2 ; v4 in G1 and w1 and w3 in G2 are the only vertices of degree 3, g must map
v2 , v4 to w1 , w3 or w3 , w1 respectively. We thus choose g(v2 ) = w1 and g(v4 ) = w3 . Since
fv2 ; v4g are the endpoints of e2 , we must have h(e2) = f1 so that the endpoints fv2 ; v4g of
edge e2 are preserved because f1 has endpoints fw1 ; w3 g = fg(v2 ); g(v4 )g.
Next we need to map v1 ; v3 to w2 ; w4 or w4 ; w2 respectively. If we choose w2 = g(v1 )
we must have w4 = g(v3 ). Thus the edge e1 joining v1 and v2 should be mapped to the
edge f2 joining g(v1 ) = w2 and g(v2 ) = w1 , i.e. f2 = h(e1 ). The rest of g and h can be
determined similarly.

11.2 The Adjacency Matrix


Given a directed or undirected graph G of n vertices v1 ;  ; vn , we can represent the graph by
an n  n matrix A over N , i.e.
2

a11
6 ..
A=4 .
an1


:::

a1n
.. 7 ;
. 5

ai j 2 N

ann

in which ai j = the number of arrows from vi to v j if G is a directed graph, and ai j = the number
of edges connecting vi to v j if G is an undirected graph. This matrix A is then said to be the
adjacency matrix of the graph G. We note that a matrix is essentially just a table, and an
adjacency matrix basically represents a table of nonnegative integers which correspond to the
number of edges between different pair of vertices.

11.2 The Adjacency Matrix

77

Examples
e1

4. The adjacency matrix of digraph on the right


2

/
v1 k

1 0 0
4
A= 1 1 2 5
1 0 0
.

is

e3

9v2

e2
e6

e4

e5

v3

e1

5. The adjacency matrix of graph on the right


2

1 1 1
A=4 1 1 2 5
1 2 0
.

is

v1

e3

e2

e6

v2
e5

e4
r

v3
Let A = (ai j ) and B = (bi j ) be two n  n matrices, the product of A and B, i.e. AB, is another
n

n  n matrix C = (ci j ) in which ci j =


2
a11 a12 : : : a1n
8
6 a a22 : : : a2n
>
<6 21
..
6 ...
.
i
6
>
:6
6 ai1 ai2 : : : ain
6
4 ::: ::: ::: :::

an1 an2

:::

aik bk j , i.e.

k=1
j

2 z

}|

7 b11 b
12
7
76b
7 6 21 b22
7 6 ..
74 .
7
5 bn1 bn2

b1 j
b2 j
..
.

:::
:::

:::

c11 c12
6 c21 c22
6
6 ..
6 .

:::
:::

:::

bn j

ann

b1n
b2n 7
7
.. 7 = 6
. 5 6
6 ci1 ci2
6 .
bnn
4 ..
cn1 cn2

:::
:::

:::

:::

c1 j
c2 j
..
.

:::

ci j
..
.
cn j

:::

:::

:::

c1n
c2n 7
7
.. 7
. 7
7

cin 7
7
.. 7
. 5
cnn

To brush up on the matrix multiplications, we look at 2 simple examples below.


Examples
6. Let 2  2 (i.e. 2 by 2) matrices A and B be given respectively by


A=

2
5

1
3

B=

7
1

4
2

Find A + B, AB and A2 .
Solution

A+B

AB


=

2+7
5+1
2
5

1+4
3 2
1
3


=

2  7 + ( 1)  1
57+31

7
1

9 3
6 1
4
2


;

2  4 + ( 1)  ( 2)
5  4 + 3  ( 2)


=

13
38

10
14


;

11 GRAPH ISOMORPHISM AND MATRIX REPRESENTATIONS

78


=

2
5

2  2 + ( 1)  5 2  ( 1) + ( 1)  3
52+35
5  ( 1) + 3  3


=

1
25

5
4


:

7. Let 3  3 matrices A and B be given by


2

2
4
A= 5
11

1
3
0

13
65
10

7
4
B= 1
9

4
2
4

8
0 5
3

Then the matrix product AB is


2

AB
2

2
4 5
11

1
3
0

7
13
6 5 41
10
9

4
2

8
0 5

2  7 + ( 1)  1 + 13  9 2  4 + ( 1)  ( 2) + 13  ( 4) 2  ( 8) + ( 1)  0 + 13  ( 3)

6 5  7 + 3  1 + ( 6)  9
4

5  4 + 3  ( 2) + ( 6)  ( 4)

11  7 + 0  1 + 10  9

2
=

130
4 16
167

42
38
4

11  4 + 0  ( 2) + 10  ( 4)

5  ( 8) + 3  0 + ( 6)  ( 3) 7
5
11  ( 8) + 0  0 + 10  ( 3)

55
22 5
118

Theorem Let G be a directed or undirected graph of n vertices v1 ; v2 ; : : : ; vn , and A be the


adjacency matrix of G. Then for any positive integer m, the (i; j)-th entry of Am is equal to the
number of walks of length m from vi to v j , where i; j = 1; 2; : : : n.
Proof Let Sm denote the statement that (i; j)th entry of Am is equal to the number of walks of
length m from vi to v j . Then for m = 1, S1 is true because the adjacency matrix A is defined that

way. For the induction purpose we now assume Sk is true with k  1. Let B = (bi j ) = Ak , then
bs j = the number of walks of length k from vs to v j due to the induction assumption. Hence
def

(i; j)-th element

of Ak+1

=
=

(i; j)-th element

of AB

ai1 b1 j + ai2 b2 j + : : : + ais bs j + : : : + ain bn j

the number of walks of length k + 1 from vi


to v j that have vs as their 2nd vertex

the number of walks of length k + 1 from vi to v j


(taking any vertex as the 2nd vertex)

i.e. Sk+1 is true. Hence Sm is true for all m  1, and the proof of the theorem is thus completed.

11.2 The Adjacency Matrix

79

Example
8. Consider the digraph on the right, which has the adjacency matrix A below
2

1 1 0
4
A= 1 0 0 5
1 1 0

v2

36
r

Since
2


*v1Y
r

6
= 4 1
2
=

1
0
1

2
6
4 1
2

0
1
7 6
0 54 1
0
1

11+11+01
6
4 11+01+01
11+11+01

2
=

11+10+01 10+10+00
7
11+00+01 10+00+00 5
11+10+01 10+10+00

1 0
7
1 0 5
1 0

1 0
7
0 0 5
1 0

v3

we see there are exactly 2 walks of length 2 that start at v3 and end at v1 . Since
2

1 1 0
2 1 0
3 2 0
2
3
4
5
4
5
4
A+A +A = 1 0 0 + 1 1 0 + 2 1 0 5
1 1 0
2 1 0
3 2 0

6 4 0
4
=
4 2 0 5
6 4 0

has only 0s in the third column, we conclude that no vertex can reach v3 via a walk of
nonzero length.

Supplementary Exercises
1. Is graph K4 isomorphic to graph K5 ?
2. Does K5 have a subgraph that is isomorphic to K4 ?
3. Let undirected graphs G1 and G2 be represented by their corresponding adjacency matrices A1 and A2 respectively, with
2
6
6
A1 = 6
4

0
1
0
1

1
0
1
1

0
1
0
1

1
1
1
1

7
7
7
5

6
6
A2 = 6
4

0
1
0
1

1
1
1
1

0
1
0
1

1
1
1
0

3
7
7
7
5

Are graphs G1 and G2 isomorphic? If yes, give the vertex correspondence of the isomorphism. How many walks of length 2 are there in graph G1 ?

12 TREES

80

4. Show the following 2 graphs are isomorphic by providing the vertex correspondence.
r

r
r

G1

G2

12 Trees
Tree is a special type of graph which is particularly important in both theory and application.
Many powerful algorithms in computer science and software engineering are tree based algorithms.

12.1 Trees, Rooted Trees and Binary Trees

I
I
I
I
I
I

A nontrivial circuit is a circuit with at least one edge. A nontrivial circuit is also called
a cycle.
A tree is a connected graph without nontrivial circuits.
A forest is composed of one tree or some disconnected trees.
A terminating vertex (or a leaf) in a tree is a vertex of degree 1.
An internal vertex (or a branch vertex) in a tree is a vertex of degree greater than 1.
Vertices are sometimes referred to as nodes, particularly when dealing with graph trees.

Examples
1. Graphs
r

r
r

and

r
r

are both trees.


r

2. Graph

r
r

r
r

is not a tree.

12.1 Trees, Rooted Trees and Binary Trees

81

Lemma Any tree with at least one edge must have at least one vertex of degree 1.
Proof We start from any vertex v0 and walk along the edges not walked before, thus arriving
at vertices v1 ; v2 ;  ; vn ;  in sequence. If all vertices of the tree had degree  2, then such
walking wouldnt terminate at any vertex (because its degree  2) without going back to one
of the vertices walked over before. Since the walking has to terminate because we are only
allowed to walk on the (finite number of) edges not walked before, we must go back to at least
one of the vertices already walked over. A nontrivial circuit would thus be found. This would
contradict the definition of a tree. Hence not all vertices have degree  2, i.e the Lemma is true.
r

v1
r

v0

r
r

~ v2
r

vs

rvs+1


vs+3

rv

s+2

Theorem For any n  1, a connected graph with n vertices is a tree if and only if it has exactly
n 1 edges.
Proof The theorem is equivalent to the following two parts:
(i) Statement Sn : A tree Tn with n vertices has n
(ii) A connected graph with n vertices and n

1 edges.

1 edges is a tree.

Proof of (i) by induction on statement Sn . For n = 1, S1 is true because T1 contains 1 vertex


and 0 edges. Now assume Sk is true. From the
r
Lemma for n = k + 1, we can find a vertex v0
r r
r
of the tree Tk+1 such that (v0 ) = 1, see figure
r
r
r
on the left. We then removed v0 and its edge to
r
r
obtain Tk , which is obviously still a tree (with k
r
vertices). Hence Tk has k 1 edges because of
r
r
the induction assumption Sk is true. Thus Tk+1
Tk
Tk+1
has 1 + (k 1) = k edges, i.e. Sk+1 is also true,
proving (i).
Proof of (ii). Let G be a connected graph with n vertices and n 1 edges. We show G is a tree
by showing it has no nontrivial circuits. Assume otherwise, i.e. G has a nontrivial circuit H. We
show that this will lead to a contradiction. Since the removal of an edge from circuit H wont
disconnect G, we can remove from G sufficient edges ( 1) so that the resulting subgraph G
has no nontrivial circuits while remaining connected with n vertices. G now by definition is
a tree, and should have n 1 edges from (i). Hence G must have more than n 1 edges when

12 TREES

82

the removed edges are added back. Thus G has no nontrivial circuit. Consequently G is a tree,
proving (ii).
Example
3. List all (up to isomorphism) trees of 4 vertices.
From the Lemma, there exists a vertex, call it v0 , of degree 1. We denote by v1 the
only vertex which is connected to v0 . Since there are exactly 3 edges in a tree of 4
vertices, the highest degree v1 can attain is 3. It is also obvious that v1 must have at
least 2 edges because otherwise edge fv0 ; v1 g would be disconnected from the remaining
vertices, which would contradict the definition of a tree. Hence we conclude 2  (v1 )  3
and will enumerate the two cases below.
(a) (v1 ) = 3.
r
r

v0

v1

(b) (v1 ) = 2. This means v1 is connected to another vertex v2 . Since only one extra
edge is needed, the edge has to be attached to v2 .
r

v0

v1

v2

Hence there are exactly 2 different trees, which are (a) and (b) respectively.

A rooted tree is a tree in which one vertex is designated as the root. The level of a vertex is
the number of edges in the unique walk between the vertex and the root. The height (or depth)
of a tree is the maximum level of any vertex there.

I
I
I

root

u is parent of v
v; w are children of u
v and w are siblings.

level 0

q
r

level 1
r

level 2

level 3
vertices in enclosed region are descendents
of u, which is an ancestor of each.

A binary tree is a rooted tree in which each vertex has at most two children. Each child
there is designated either a left child or a right child.

12.2 Traversal of Binary Trees

83
root
r
u

3
v

r
r

r
r

v is the left
child of u

r
r

right subtree of w

left subtree of w
Example
4. Draw a binary tree to represent ((a

b)  c) + (d =e).

Solution

+l

l
l

al

=l

cl
bl

dl

el

12.2 Traversal of Binary Trees


Preorder traversal

Traverse left subtree in pre-order;

Inorder traversal

Traverse right subtree in pre-order.

Traverse left subtree in in-order;


Visit the root;

Traverse right subtree in in-order.

Postorder traversal
r
r

Visit the root;

Traverse left subtree in post-order;


Traverse right subtree in post-order;

Visit the root.

12 TREES

84
Examples
5. For binary tree

ra
r

b
d

e
r

r
r

the traversals are as follows.


Preorder:
Inorder:
Postorder:

a; b; d ; c; e; f ; g
d ; b; a; f ; e; g; c
d ; b; f ; g; e; c; a

6. The binary tree representation of (a + b  c)=d is easily seen as


=l

+l

dl

l

al
bl

cl

But how is the calculation actually done? It may be processed via a postorder traversal
a; b; c; ; +; d ; =

()

by interpreting each binary operation as acting on the 2 quantities immediately to its left.
That is, ; ; ; ; ;  means ; ; (  ); , if ; ; ; are numbers and  is any binary
operation. This way, the formula is processed successively in following sequence
a; b; c; 
a; b  c; +;
a + b  c; d ;
(a + b  c)=d

d;

+;

d;

=
:

The sequence () is said to be in the Polish postfix notation.


7. Use a binary tree to sort the following list of numbers
15; 7; 24; 11; 27; 13; 18; 19; 9 :
We note that when a binary tree is used to sort a list, the inorder traversal will be automatically assumed in this unit.

12.2 Traversal of Binary Trees

85

Solution To sort a list, just create a binary tree by adding to the tree one item after
another, because the insertion is essentially a sorting process. The binary tree constructed
during the course of sorting the above list then reads
15
7

24
11

18

27

13

19

It is constructed in the order of A to I depicted below.


15

15

15

7
B

A
15

15

24

15

11

24
11

7
27

24
11

15

7
13

27
G

24
11

18
13

15

24
18

27
13

15

11

24

27
19

24
11

18
13

27
19

The sorted list is thus


7; 9; 11; 13; 15; 18; 19; 24; 27 :
This is because, according to the inorder traversal, we have to visit the left subtree of
(the root) 15 containing vertices 7, 11, 9 and 13 before visiting 15 itself and then its
right subtree. However, to visit the subtree containing exactly the vertices 7, 11, 9 and
13 we have to visit first the (empty) left subtree of 7, then 7, then the right subtree of 7
containing vertices 11, 9 and 13. Hence 7 is the very first vertex to be visited. To visit
the right subtree of 7 containing vertices 11, 9 and 13 we visit first 9, then 11 and then
13. Hence the first 4 vertices visited are 7, 9, 11 and 13. The rest is similar. We note that
a better tree sorting algorithm will involve balancing the trees. However such additional
features fall beyond the scope of the current unit.
Note One of the immediate applications of binary trees is for the binary coding, in particular for
the Huffman coding. For further details, interested readers may consult the book Mathematical
Structures for Computer Science by Judith L Gersting, 3rd edition, W H Freeman and Company,
1993.

13 SPANNING TREES

86

Supplementary Exercises
1. Let n  0 be an integer. Can a forest have n + 2 vertices and n edges? If yes, how many
connected components will there be?
2. Construct, up to isomorphisms, all trees of 5 vertices.
3. Use a binary tree to sort alphabetically the list B; I ; N ; A; R; Y; L; U ; S; T ; F; O; E

4. For the binary tree below, list the vertices according to the preorder, inorder and postorder
traversals respectively.
A
B
C
D
E
F
G
H
J
I
K
L
M

13 Spanning Trees
13.1 The Concept of Spanning Trees
A spanning tree for a graph G is a subgraph of G that contains all vertices of G and is a tree.
Obviously every connected graph G has a spanning tree. In fact, if one keeps breaking any
remaining nontrivial circuit of G, or the intermediate subgraphs after such steps, by removing
an edge from the circuit, then the final resulting subgraph will be a spanning tree.
Examples
1. Graph G
v2

v2

v1

v5

r
r

becomes

v3

v1

v4

v5

v3

v4

after removing edge fv2 ; v3 g from G to break the cycle v5 ! v2 ! v3 ! v5 , and becomes
a spanning tree
v
r

v1

v5

r
r

v4

v3

13.1 The Concept of Spanning Trees

87

after removing edge fv4 ; v3 g to break the last remaining cycle v5 ! v3 ! v4


course, there can be different spanning trees for the same connected graph G.

! v5 .

Of

2. Suppose we want to establish a communications network among four centres A; B; C and


D, and due to geographical problems direct communications (via cables for instance) may
only be established between the two centres connected by an edge is the graph
B

Ar

Suppose each communication link (edge) costs $1000 to install. Since communications
may be relayed via intermediate centres, we may choose a spanning tree to lay the communication links so as to minimise the total cost. We now list below all possible spanning
trees
r

each will thus only cost $3000 to build, in comparison with the $5000 (5 edges) required
for the original graph G.
3. Back to the graph G in example 1, see the graph below
v2

e1
r

v1

e4
v5

e3
r

v4

e2
e5

v3
e6
G

Recall that a spanning tree was obtained there by breaking all the cycles in the graph.
However, we can also construct a spanning tree in the opposite way, that is, by gradually
collecting edges from G, and making sure that the resulting subgraph is always connected
and contains no cycles. For instance, a spanning tree T4 , different from that obtained in

13 SPANNING TREES

88
example 1, can derived from the following 4 steps
v2

v2

e1
v1

step 1

T1

v1

step 2

v2

e1
v1

e2

e1

v5

T2

v2

e2

e1

v3

e3

v1

v4
step 3

v3

T3

e4

v5

e2

v3

e3
r

v4
step 4

T4

Notice that neither e5 nor e6 can be added because that would create cycles. We stop the
procedure when consideration for all edges are exhausted.
A technical problem in step 4 of example 3 is how do we know that connecting vertices v2
and v5 wont create a cycle! This is not trivial, particularly when there are a great many vertices.
Hence we need an effective algorithm.

13.2 Spanning Tree Algorithm


We may use the following procedure to find a spanning tree. It is a procedure which analyses
edges one by one to see if it can be collected into the resulting spanning tree.
(i) Let v1 ; v2 ;  ; vn be all the vertices of a connected graph G.
(ii) List all edges in a sequence.
(iii) Attach a label (or an indicator) L(i) to each vertex vi , and initiate L(i) to distinct values
by setting L(i) = i (i = 1; 2;  ; n).
(iv) Let the resulting graph T be initially empty.
(v) Pick next available edge e in the edge list in (ii). Let vi0 and v j0 be the vertices of e.
(a) If L(i0 ) 6= L( j0 ) (implying adding e to T will not create cycles), then add edge e
to T and change all L(k) which are equal to L(i0 ) or L( j0 ) to the minimum value
min(L(i0); L( j0 )).
(b) If L(i0 ) = L( j0 ), ignore edge e.

13.2 Spanning Tree Algorithm

89

(vi) Do (v) until all edges are considered.


Note It is not difficult to see that step (vi) above may (think!) also be replaced by
(vi0 ) Do (v) until all labels L(i) are equal to each other.
We also note that the values of the labels L(i) are to be altered in step (v) above to keep track
of the connectivity of any pair of vertices: two vertices, say vi0 and v j0 , are connected through
the edges already collected to the resulting spanning tree T if and only if the corresponding
labels have exactly the same value, i.e. L(i0 ) = L( j0 ). In the algorithm, moreover, the resulting
spanning tree T will gradually grow from the empty tree ? at the very beginning to the full
spanning tree at the end of the procedure.
Example
4. Find a spanning tree for the graph
r

v1

v2
r
e1

e7

e9

e5

v5

v3
e4

e8

e10

v4

v
r6

e2

e3

e6

v7

Solution Since the graph is already provided with a vertex list and an edge list, we might
just as well use these lists as the outcome of steps (i) and (ii) though we could use lists of
different orders. Steps (iii)(vi) can be summarised in the following table

step edges
0
?

labels of the vertex list


L(1) L(2) L(3) L(4) L(5) L(6)
1
2
3
4
5
6
|

{z

edge added
L(7)
to tree
7
}

initial values

1
2
3
4
5
6
7
8
9
10

e1
e2
e3
e4
e5
e6
e7
e8
e9
e10

1
1
1
1
1
1
1
1
1

2
2
2
2
2
2
1
1
1

2
2
2
2
2
2
1
1
1

4
4
4
2
2
2
1
1
1

5
5
5
5
5
5
5
5
1

6
5
5
5
5
5
5
5
1

no need to consider because all labels


are now equal in value

7
7
5
5
5
5
5
5
1

e1
e2
e3
e4

e7
e9

13 SPANNING TREES

90

Notice that at step 1, since e1 has vertices v2 and v3 and L(2) = 2 6= L(3) = 3, we thus
change L(2); L(3) both to 2 and add e1 to the spanning tree. At step 5, since e5 has
vertices v2 and v4 and L(2) = L(4) = 2 (at the end of step 4), we know from (v) that we
cant add e5 to the tree (because the addition of e5 would create a cycle). At step 7, since
e7 has vertices v1 and v2 whose labels are 1 and 2 respectively, we change in this step
all the labels with value either 1 or 2 to the min(1; 2)=1. At step 10, nothing needs to be
considered because all labels are now equal at the end of step 9, implying the spanning
tree is already obtained. The spanning tree, collecting the edges in the last column, has
edges fe1 ; e2; e3 ; e4 ; e7 ; e9 g, or
r

r
r

r
r

13.3 Kruskals Algorithm


A weighted graph G is a graph for which each edge e has an associated real number called
weight. A minimal spanning tree of a weighted graph is a spanning tree that has the least
possible total weight. We often use w(e) to denote the weight of edge e and w(G), the total
weight of G. Also, for simplicity here all weights are assumed to be non-negative.
The Spanning Tree Algorithm given earlier on in this lecture can be used to find a minimal
spanning tree if we change step (ii) there to the following step
(ii0 ) List all edges in the order of increasing weights.
Then the algorithm becomes Kruskals algorithm.
Examples
5. Find a minimal spanning tree for the weighted graph

v1

4
5

v2
r
1r
4 v3
3
r

v5
7

v
r6

3
4

v7

v4
in which the number on the edges indicates the corresponding weight.

13.3 Kruskals Algorithm

91

Solution Since this is a simple graph, we may use a pair of vertices to represent an edge.
One possible list of edges ordered in increasing weight is
e1 = fv2 ; v3 g ;
e2 = fv5 ; v6 g ;
e3 = fv6 ; v7 g ;
e4 = fv3 ; v4 g ;
e5 = fv2 ; v4 g ;

w(e1 ) = 1 ;
w(e2 ) = 2 ;
w(e3 ) = 3 ;
w(e4 ) = 3 ;
w(e5 ) = 4 ;

e6 = fv5 ; v7 g ;
e7 = fv1 ; v2 g ;
e8 = fv1 ; v4 g ;
e9 = fv3 ; v5 g ;
e10 = fv3 ; v7 g ;

w(e6 ) = 4 ;
w(e7 ) = 4 ;
w(e8 ) = 5 ;
w(e9 ) = 5 ;
w(e10 ) = 7 :

We note that this edge list happens to be the same as that in example 4. The Kruskals
algorithm on this example can be performed/summarised in the following table
edges L(1) L(2) L(3) L(4) L(5) L(6)
? 1 2 3 4 5 6
e1
1
2
2
e2
5
5
e3
5
5
e4
1
2
2
2
5
5
e5
e6
e7
1
1
1
1
5
5
e8
e9
1
1
1
1
1
1
e10

L(7)
7

extra edge extra weight

7
5
5

e1
e2
e3
e4

1
2
3
3

e7

e9

in which the numbers conveniently represented by the bar are the same as the numbers in the previous step. You may of course use the actual numbers instead if you so
prefer. Hence a minimal spanning tree can be formed by the edges fe1 ; e2 ; e3 ; e4 ; e7 ; e9 g.
The corresponding total weight is 18 (=1+2+3+3+4+5).
6. Is there perhaps a quickn dirty way to solve the problem in the previous example?
Solution Minimal spanning trees can also be found through the following procedure.
(i) Assume that the weighted graph is initially not coloured.
(ii) Pick an edge that has the smallest weight among all the remaining uncoloured edges
(pick any one if there are more than one such edges).
(iii) Colour the picked edge into red if, in doing so, the red-coloured edges will not form
any nontrivial circuits. Otherwise colour the edge into green.
(iv) If all edges have been coloured into either red or green, then the red-coloured edges
form a minimal spanning tree. Otherwise go back to step (ii) to process the remaining uncoloured edges.

13 SPANNING TREES

92

When a weighted graph is not too complicated, this procedure tends to be quite intuitive
and manually quick. If we apply this procedure to the weighted graph in the previous
example, the resulting minimal spanning tree can be obtained instantly.
7.

(a) Find a minimal spanning tree for the following weighted graph G
B
r
5
A r
6 r 8 7
7 3
E
4
F 7 r C
3
6
r

4
r

D
where the numbers each represent the weight of the corresponding edge. What is
the total weight of the minimal spanning tree?
(b) Can we find an Eulerian path for the graph in (a)? Explain why.
(c) Give a Hamiltonian path, if any, for the graph in (a).
Solution
(a) We use Kruskals algorithm to obtain a minimal spanning tree. The procedure can
be summarised in the following table
edge weight L(A) L(B) L(C) L(D) L(E) L(F)
initial
1
2
3
4
5
6
BC
3
1
2
2
4
5
6
DE
3
1
2
2
4
4
6
EF
4
1
2
2
4
4
4
DF
4
1
2
2
4
4
4
AB
5
1
1
1
4
4
4
AD
6
1
1
1
1
1
1
AE
6
stop: all labels are now equal
..
.
i.e. the spanning we obtained is
B
r

r
r
r

take?
yes
yes
yes
no
yes
yes

93
and the minimal weight=3+3+4+5+6=21.
(b) No. Because there are strictly more than 2 vertices of odd degrees.
(c) The minimal tree found in part (a) in the above is a Hamiltonian path.

Supplementary Exercises
1. Let n  1 be an integer. How many spanning trees, up to isomorphisms, are there for
K2;n ?
2. Find a minimal spanning tree for the following weighted graph G
B
A

5
6 r 3
E

4
5

7
F 6

3
r

9
,

where the numbers each represent the weight of the corresponding edge. What is the total
weight of the minimal spanning tree?
3. Prove that if a connected graph is not a tree, then it must have more than 1 spanning trees.
4. If a weighted graph has different weight on each edge, can there be more than 1 minimal
spanning trees?

14 Number Bases
14.1 Frequently Used Number Systems
The numbers used most frequently in our daily life are written in the standard decimal number
system, e.g.
7086 = 7  103 + 0  102 + 8  101 + 6  100 :
The base number in a decimal system is 10, whose special role is obvious in the above example,
implying there are exactly 10 digits required for the system. These 10 elementary digits are 0,
1, 2, : : : ; 9. In general a nonnegative number in a base p number system is denoted by
(an an 1 : : : a1 a0  b1 b2 : : : bN : : :) p

14 NUMBER BASES

94
representing the value of
an pn + an

1p

n 1

1
0
1
2
N
+ : : : + a1 p + a0 p + b1 p + b2 p + : : : + bN p
+:::

We note that a0 ; : : : ; an and b1 ; b2 : : : are all just digits, coming from a set of p elementary digits
for the number system. These p digits will represent values from 0 to ( p 1) respectively.
Examples
1. When base number p = 2, the system is called the binary number system. Two digits
are needed, and these two digits are chosen to be 0 and 1, consistently representing
the values of 0 and 1 respectively. For example,
(1011:1)2 = 1

 23 + 0  22 + 1  2 + 1 + 1  12 = (11 5)10
:

i.e. 1011:12 is equal to 11.5 in decimal.


2. When base p = 8, the system is called the octal number system. Naturally we still
conveniently use the existing first 8 digits in the decimal system, i.e. 0; 1; 2; : : : ; 7, to
denote the corresponding values for the new number system. For example,
13:78 = 1  81 + 3  80 + 7  8

= 11:875

i.e. 13:78 represents the decimal number 11.875.


3. When p = 16, the system is called the hexadecimal number system. 16 digits are needed
in this system, representing values from 0 to 15. We shall still make use of the 1st 10 digits
0; 1; : : : ; 9 from the decimal number system, and use A; B; C; D; E ; F to make up for the
rest of the digits. Hence the digits for the hexadecimal system are
0 ; 1 ; 2 ; 3 ; 4 ; 5 ; 6 ; 7 ; 8 ; 9 ; A ; B ; C ; D; E ; F

representing 0 to 15 respectively. For example,


A7F16

=
=

A  162 + 7  161 + F  160

10  162 + 7  16 + 15  1 = 2687 :

4. When p = 10, the system is our standard decimal number system. Thus for instance
(36429)10 is just 36429.

14.2 Conversion to Numbers of Different Bases

95

14.2 Conversion to Numbers of Different Bases


Conversion of a number in one base system to another can be done from the definitions, e.g.
(45)6

=
=
=
=

4  61 + 5  60 = 29
20 + 2  4 + 1

45+24+1

4  (4 + 1) + 2  4 + 1

=
=

(first converted to the decimal system)

 42 +

(131)4

 41 +

 40

However, the procedure in the above example doesnt seem systematic and may be cumbersome to perform in general to say the least. Hence we shall introduce below an algorithm to
convert numbers from one base system to another. But first, let us state the following
Remainder Theorem For any integers p and q with p > 0, there exist integers s and r such
that
q = sp + r; 0  r < p:
jqk
In fact s is given explicitly by s =
.
p
jqk
one has
Proof The definition of flooring implies for s =
p
s
Let r = q

q
p

<

s+1

sp, then 0  r < (s + 1) p

sp  q < (s + 1) p :

sp = p, which thus proves the theorem.

The division algorithm to convert a decimal integer a 2 N to a number of base p is to find


r0 ; r1 ; r2 ; : : : in the following procedure
a = pq0 + r0 ;
q0 = pq1 + r1 ;
q1 = pq2 + r2 ;

(1)
(2)
(3)
..
.



(m + 1)

Then a = (rm rm

qm

1=

 r1 r0)

pqm + rm ;

0  r0 < p ;
0  r1 < p ;
0  r2 < p ;
0  rm < p ;
and qm = 0

Remainder Theorem on a
R.T. on q0
R.T. on q1

..
.

Termination Condition

is the representation in the base p number system.

To convert a non-integer number b with 0 < b < 1 to a base p number system, the procedure
is as follows

14 NUMBER BASES

96

s1 = bbp ;
s2 = bt1 p ;
s3 = bt2 p ;

bp = s1 + t1 ;
t1 p = s2 + t2 ;
t2 p = s3 + t3 ;

(1)
(2)
(3)
..
.



(m)

tm

..
.

1 p = sm + tm

sm = btm
..
.



1p

s1 is the integer part of bp


s2 is the integer part of t1 p
s3 is the integer part of t2 p
..
.
;

procedure terminates at m if tm = 0
otherwise continue forward

Then b = (0:s1s2  sm ) p is the representation in the base p number system.
We note that while the algorithm for converting an integer into a different base will always
terminate in finite number of steps, the conversion of a non-integer may not terminate at all,
although we usually can still stop in finite number of steps after observing a repeating pattern.
The proofs of the above 2 algorithms are essentially no more than the re-interpretation of
a = r m pm + r m

1p

m 1

 + r1 p + r0

b=

s1 s2
sm
+
+  +
+ 
2
p p
pm

Examples
5. Convert 45 to base 4.
(1)
(2)
(3)

45 = 114 +
11 = 24 +
2 = 04 +


+
+


1
3
2

Hence 45 = (231)4 .

stop now because its become 0

That is, we first divide 45 by 4. The quotient 11 is then divided by 4, with the new
quotient 2 being again divided by 4. This repeating process is then stopped because the
latest quotient has reached 0. The remainders obtained at these division steps, when listed
in the reverse order, finally give the desired representation of 45 in base 4.
6. Convert 123 into binary.
Solution We illustrate here an alternative approach to number conversions. It uses exactly the same algorithm as before, and the only difference lies in the organisation or
presentation of the derivation. Obviously you dont have to bother with this approach if
you dont like it: the approach adopted in the previous example is already neat enough.

14.2 Conversion to Numbers of Different Bases

97

(1111011)2

123

61

30

15

1
0

2

123
61
123=612 + 1
successive divisions

end when quotient becomes zero

Hence 123=11110112.

7. Convert 0.1 to base 2.


(1)
(2)
(3)
(4)
(5)

0:1  2 =
0.2  2 =
0.4  2 =
0.8  2 =
0.6  2 =

0
0
0
1
1

+
+
+
+
+

0.2
0.4
0.8
0.6
0.2

Hence 0:1 = (0:000110011 )2 = (0:0 0011)2 .


0.2 here is repeating the outcome of step (1)

integer part/portion

N
If p = qk and p; q and k are all positive integers, then the conversion of (an : : : a1 a0  b1 b2 : : :) p
into base q can be done digitwise, with each digit ai or b j being mapped to a block of k consecutive digits in the base q number system. This is called the blockwise conversion.
Examples
8. Convert (7A:1)16 into binary format.
Solution Since 16 = 24 , 1 digit in hexademical will correspond to exactly 4 digits in
binary. Since
716 = 01112 ; A16 = 10102 ; 116 = 00012 ;

PPPP
i

*




make sure they are all full 4-bit blocks

we have
(7A

 1)

16

= (01111010:0001)2 :

14 NUMBER BASES

98
9. Convert the binary number (1111010:0001)2 to base 8.

Solution Since 8 = 23 implies every 3 consecutive digits, going left or right starting from
the fractional point, in binary will correspond to a single digit in base 8. By grouping
consecutive digits into blocks of 3, appending proper 0s if necessary, we have
(1111010:0001)2 = (001 111 010:000 100)2 = (172:04)8

because 0012 = 18 , 1112 = 78 , 0102 = 28 and 1002 = 48 .


Basic direct operations such as additions and subtractions can be performed in any number
systems in the way similar to what we would do in the standard decimal system.
Examples
10. Find 3435 + 1145 .

3
1
1 0

4
1
1

3
4
2

3

+ 4 = 7 = (12)5

i.e. 3435 + 1145 = 10125 . We note that the addition is performed digitwise from the
rightmost digit towards the left. On the rightmost digit, 35 + 45 = 710 = (12)5 gives the
resulting digit 2 and the carry 1 to the next digit on the left. On the 2nd rightmost digit,
we have thus 45 + 15 + (carry) 15 = 610 = (11)5, resulting to digit 1 plus a new carry 1.
As the last step, we then have 35 + 15 + 15 = 510 = (10)5. This completes our addition.
11. Find 100112

1012 .
1 0 0 1 1
1 0 1
1 1 1 0

Hence 100112 1012 = 11102 . The subtraction is also performed digitwise from the
rightmost digit towards the left. On the rightmost digit, 12 12 = 02 gives the resulting
digit 0. On the 2nd rightmost digit, 12 02 = 12 gives the result 1. On the 3rd rightmost
digit, we borrow 1 from the digit on the left; actually we have to borrow 1 further on the
left because the digit on the immediate left is 0 and therefore cant be borrowed. The
subtraction on the 3rd rightmost digit thus becomes (10)2 12 = 210 110 = 110 = 12 .
Finally, the 4th rightmost digit, after borrowed 1 from the left and being borrowed 1 from
the right, is left with the remaining 1. This completes the subtraction.

Supplementary Exercises
1. Convert 367 to base 2.
2. Convert A8:F16 to base 4.
3. Find A8:F16 + 11011:12 and give the result in the hexademical number system.

99

15 Relations
Relations are fundamental to modern database systems and artificial intelligence. The relational database systems widely used in corporations of all levels are modeled on the concept of
relations.

15.1 Binary Relations


A binary relation from set A to set B is a subset R of A  B. Thus for any pair (x; y) in A  B, x
= R may be similarly
is related to y by R, written xRy, if and only if (x; y) 2 R. Likewise (x; y) 2
denoted by x 6 Ry.
Examples
1. Let A = f1; 3g and B = f2; 5g. Then we ask how elements in A are related to elements in
B via the inequality . The answer is
1  2; 1  5; 3 6 2; 3  5 :

In other words, 3 pairs (1; 2); (1; 5) and (3; 5) observe the  relationship. Hence by
collecting them in R, i.e.
R = f(1; 2); (1; 5); (3; 5)g ;
we know relation R describes precisely the relationship between elements of A and elements of B under . For example,

, (3 5) 2 R , 3 is related to 5 by R
, 35
, (3 2) 62 R , 3 is not related to 2 by R , 3 6 2
Let A = f1 2g and B = f1 2 3g and define a binary relation R from A to B by
for any (x y) 2 A  B : (x y) 2 R iff x y is even
3R5
3 6R 2

2.

(a) Give R by its explicit elements.


(b) Is 1R1 ? Is 2R3 ? Is 1R3 ?
Solution
(a) For any (x; y) pair in A  B = f(1; 1); (1; 2); (1; 3); (2; 1); (2; 2); (2; 3)g, we must
check if xRy or if x y is even. This is done in the following table
(x; y)
(1; 1)
(1; 2)
(1; 3)
(2; 1)
(2; 2)
(2; 3)

property of x
1 1 even
1 2 odd
1 3 even
2 1 odd
2 2 oven
2 3 odd

conclusion
(1 ; 1 ) 2 R
(1 ; 2 ) 2
= R
(1 ; 3 ) 2 R
(2 ; 1 ) 2
= R
(2 ; 2 ) 2 R
(2 ; 3 ) 2
= R

15 RELATIONS

100
Hence R = f(1; 1); (1; 3); (2; 2)g.

(b) Yes. 1R1 since (1; 1) 2 R.


= R.
No. 2R3 since (2; 3) 2
Yes. 1R3 since (1; 3) 2 R.

Note A function f : A ! B may be regarded as, or represented by, a special relation F from A
to B that satisfies
(i)
(ii)

8x 2 A 9y 2 B such that (x y) 2 F ,
8x 2 A 8y z 2 B, if (x y) 2 F, (x z) 2 F, then y = z .
;
;

The identification of f with F is via y = f (x) iff (x; y) 2 F. Notice that (i) describes a function
property that every element in A should be mapped somewhere on B, and (ii) implies a single
element in A shouldnt be mapped to 2 different elements in B.
Example
3. Function f : f1; 2; 3g ! f1; 2; 3; 4; 5; 6g with f (n) =
F

n (n + 1 )
is equivalent to the relation
2

= (1; 1); (2; 3); (3; 6)

in which, for instance, (2; 3) corresponds to 3 = f (2).


For a binary relation on a set A, i.e. a binary relation from A to A, the relation may be
represented by a directed graph. The representation is simple: each element in A is denoted by
a vertex, and each (x; y) 2 R, i.e., x is related to y, is denoted by an arrow from x to y.
Examples
4. Let A = R be the set of all real numbers, and a binary relation S on the set A be defined
by
S = f(x; y) 2 R R j (x 10)2 + y2  16; or (x 5)2 + y2  4g :
Graph S in the Cartesian plane.
Solution Recall from analytic geometry that a Cartesian plane is a plane with the normal
x and y axis, and a circle of radius r centred at the coordinates (a; b) is given precisely by
the equation
2
2
2
(x a ) + (y b ) = r :
Hence equation (x 10)2 + y2 = 16 (= 42 ) represents a circle of radius 4 centred at the
coordinates (10; 0), and (x 5)2 + y2 = 4 is a circle of radius 2 centred at the coordinates
(5; 0). Thus S is the union of these 2 disks, and is represented by the shaded area in the
graph below.

15.2 Reflexivity, Symmetry and Transitivity

6y
10

101
...........................
.....
......
.
.
.
.
...
.
.
.
.
.
.
.
.
...
.... ....
4
.......2 ..........
...
... ...
..
..
...
.
.
..
... 5 .... ...
.
10
.
...... ........
.
............. ...
..
.....
.
.
......
....
..........................

7




I

5. Let A = f1; 2; 3g and a binary relation E be defined by (x; y) 2 E iff x y is even and
x; y 2 A. Then E=f (1,1), (2,2), (3,3), (1,3), (3,1)g can be represented by the following
digraph

W

(2,2):

2 2 = 0 even, hence the loop at vertex


labelled by 2 2 A.

(1,3):

1 3 = 2 even, hence the arrow from


vertex 1 to vertex 3.

15.2 Reflexivity, Symmetry and Transitivity


Let R be a binary relation on a set A.

I
I
I
I

R is reflexive if for all x 2 A, xRx.


R is symmetric if for all x; y 2 A, if xRy, then yRx.
R is transitive if for all x; y; z 2 A, if xRy and yRz, then xRz.
R is an equivalence relation if A is nonempty and R is reflexive, symmetric and transitive.

In terms of digraphs for the representation of binary relations, reflexivity is equivalent to


having at least a loop on each vertex; symmetry means any arrow from one vertex to another
will always be accompanied by another arrow in the opposite direction; and transitivity is the
same as saying there must be a direct arrow from one vertex to another if one can walk from that
vertex to the other through a list of arrows, travelling always along the direction of the arrows.
Examples
6. Let A = f0; 1; 2; 3g and a relation R on A be given by
R = f(0; 0); (0; 1); (0; 3); (1; 0); (1; 1); (2; 2); (3; 0); (3; 3)g:
Is R reflexive? symmetric? transitive?
Solution Well make use of the digraph for R on the right.

15 RELATIONS

102

zi
o
W
K3
r

jY
r

(a) R is reflexive, i.e. there is a loop at each


vertex.

(b) R is symmetric, i.e. the arrows joining a


pair of different vertices always appear in a
pair with opposite arrow directions.

(c) R is not transitive. This is because otherwise the arrow from 1 to 0 and arrow from
0 to 3 would imply the existence of an arrow from 1 to 3 (which doesnt exist). In
other words (1; 0) 2 R, (0; 3) 2 R and (1; 3) 2
= R imply R is not transitive.
Note It is equally easy to show these properties without resorting to the digraph.
7. Let m; n and d be integers with d 6= 0. Then if d divides (m n), denoted by d j (m n),
i.e. m n = dk for some integer k, then we say m is congruent to n modulo d, written
simply as m  n (mod d). Let R be the relation of congruence modulo 3 on the set Z of
all integers, i.e.

mRn

m  n (mod) 3

3 j (m

n) :

Show R is an equivalence relation.


Solution We just need to verify that R is reflexive, symmetric and transitive.
(a) Reflexive: for any n 2 Z we have nRn because 3 divides n

n = 0.

(b) Symmetric: for any m; n 2 Z if mRn, i.e. m  n (mod 3) then there exists a k 2 Z
such that m n = 3k. This means n m = 3( k), i.e. n  m (mod 3), implying
finally nRm. For example, 7R4 is equivalent to 4R7 can be seen from
7R4

, 7  4(mod 3) , 7

4 = 31

,4

7 = 3  ( 1)

, 4  7(mod 3) , 4R7

(c) Transitive: for any m; n; p 2 Z, if mRn and nRp, then there exist r; s 2 Z such that
m n = 3r and n p = 3s. Hence m p = (m n) + (n p) = 3(r + s), i.e. mRp.
We thus conclude that R is an equivalence relation.

Supplementary Exercises
1. Let a binary relation R on R be defined by
R = f(x; y) 2 R R j
Graph R in the Cartesian plane.

3x + y  1g :

103
2. Let function f (n) = n2 be defined on Z. Represent the function f by a binary relation F
on Z.
3. Let a digraph G be given by

= - = v2

v1

?

Y Y v3

v4

We define a binary relation R on the set of the vertices of the digraph G by


(v; w)

2R

iff there exists a walk (following the directed


edges) from vertex v of G to vertex w of G

Draw the digraph that represents the binary relation R. Is R reflexive, symmetric and
transitive?
4. Let F be the set of all real-valued functions on R . We define a binary relation R on F by
( f ; g)

2R

iff

f (x) = (g(x)) :

Show that R is reflexive and transitive.

16 Equivalence Relations
16.1 Equivalence Classes and Partitions
We recall that a binary relation R on a set A is an equivalence relation if and only if the following
3 conditions are all true.

Reflexivity:
Symmetry:
Transitivity:

8x 2 A xRx
8x y 2 A xRy ! yRx
8x y z 2 A xRy ^ yRz ! xRz
;

; ;

Example
1. Let set A = f 2; 4; 6; 8; 10 g and R be a binary relation on A defined by
(m; n)

2R

iff m  n (mod 4)

8m n 2 A
;

Recall that m  n (mod 4) is equivalent to saying 4 divides (m n) or (m n) is an integer


multiple of 4. Thus it is easy to see

16 EQUIVALENCE RELATIONS

104
(m; n)

(2,2)
(2,4)
(2,6)
(2,8)
(2,10)
(4,2)
(4,4)
(4,6)
..
.

m
2
2
2
2
2
4
4
4
..
.

n
2=0
4= 2
6= 4
8= 6
10 = 8
2=2
4=0
6= 2

is multiple of 4?
yes, 0 is a multiple of 4
no, 2 is not a multiple of 4
4: yes
6: no
8: yes
2: no
0: yes
2: no
..
.

conclusion
(2 ; 2 ) 2 R
(2 ; 4 ) 2
= R
(2 ; 6 ) 2 R
(2 ; 8 ) 2
= R
(2; 10) 2 R
(4 ; 2 ) 2
= R
(4 ; 4 ) 2 R
(4 ; 6 ) 2
= R
..
.

which gives explicitly


R = f (2; 2); (2; 6); (2; 10); (4; 4); (4; 8); (6; 6); (6; 10); (8; 8); (10; 10);
(6; 2); (10; 2); (8; 4); (10; 6)

This relation R can be drawn as


10
8
4

+
M
r


>I

6
2


O

and is obviously an equivalence relation (cf. examples in the previous lecture). Notice
that there are 2 connnected components, one containing elements 4 and 8 and the other,
elements 2, 6 and 10. Here the connection is made through certain walks along the
directions of the arrows. These 2 components are just the 2 distinct equivalence classes
under the equivalence relation R. We may use any element in an equivalence class to
represent that particular class which basically contains all elements that are connected to
the arbitrarily chosen representative element. Hence, in this case, we may choose 2 to
represent the class f2; 6; 10g, written simply [2 = f2; 6; 10g, and choose for instance 8 to
represent the other class f4; 8g, that is, [8 = f4; 8g. Since any member of an equivalence
class can be used to represent that class, [6 and [10 will be representing exactly the same
equivalence class as [2. Hence
[2

 [6  [10 = f 2

6; 10 g;

[4

 [8 = f 4 8 g
;

are the 2 distinct equivalence classes.


The definition of equivalence classes and the related properties as those exemplified above
can be described more precisely in terms of the following lemma.

16.1 Equivalence Classes and Partitions

105

Lemma Let A be a set and R an equivalence relation on A. For any a 2 A we define the
equivalence class of a, written [a, by [a = fx 2 A : x R ag: Then
(i) For any a 2 A, a 2 [a.
(ii) For any a; b 2 A , a R b iff [a = [b.
(iii) For any a; b 2 A, either [a \ [b = ?, or [a = [b.
(iv) For an equivalence class S, any element a
guaranteeing [a = S.

2 S can be used as a representative of S,

Proof First, R is an equivalence relation means R is reflexive, symmetric and transitive.


(i) Since R is reflexive implies aRa for any a 2 A, hence a 2 [a.
(ii)

(a) Necessity. Let aRb. For any x 2 [a we have xRa. From xRa and aRb, we derive
from Rs transitivity that xRb, i.e. x 2 [b. Hence [a  [b. Likewise [b  [a
because bRa is implied by aRb and R is symmetric. Hence [a = [b.
(b) Sufficiency. Let [a = [b. Since (i) implies a 2 [a and thus a 2 [b, we have by
definition of [b] that aRb.

(iii) If [a \ [b 6= ?, then there exists an x 2 A such that x 2 [a and x 2 [b. This means xRa
and xRb, i.e. aRx and xRb due to the symmetry of R, implying aRb due to the transitivity.
(ii) thus implies [a = [b.
(iv) S is an equivalence class means S = [b for some b 2 A. Thus for any a 2 S = [b, we have
aRb and hence from (ii) [a = [b = S.
Examples
2. For the binary relation R defined in example 1, let us verify the results of the above lemma.
Solution Actually this is more like explanations.
(a) (i) of the lemma in this case implies 2 2 [2 = f2; 6; 10g, 6 2 [6, 10 2 [10, 4 2 [4
and 8 2 [8 = f4; 8g which is obviously true due to the explicit formula at the end of
example 1.
(b) (ii) in this case is the same as saying [2 = [6 = [10 and [4 = [8, which are already
shown in example 1.
(c) (iii) is consistent with the fact that any 2 of the equivalence classes [2, [4, [6, [8
and [10 are either disjoint (e.g. [2] and [4] have no elements in common) or exactly
the same (e.g. [2]=[6]).

16 EQUIVALENCE RELATIONS

106

(d) (iv) for the equivalence class f2; 6; 10g implies we can use either 2 or 6 or 10 to represent that same class, which is consistent with [2 = [6 = [10 observed in example
1. Similar observations can be made to the equivalence class f4; 8g.
3. Show that the distinct equivalence classes in example 1 form a partition of the set A there.
Solution In example 1 we have shown that [2 = f2; 6; 10g and [4 = f4; 8g are the only
distinct equivalence classes. Since A in example 1 is given by A = f2; 4; 6; 8; 10g, we can
easily verify
(a): A = [2 [ [4 ;
(b): [2 \ [4 = ? :
From the definition of the set partition (see the earlier lecture for sets) we conclude that
f[2; [4g is a partition of set A.
The above example is in fact a special case of the more universal properties to be summarised in the following theorem.
Theorem Let A be a nonempty set.
(i) If R is an equivalence relation on A, then the distinct equivalence classes of R form a
partition of A. This partition is said to be induced by the equivalence relation R.
(ii) Suppose A has a partition fA1 ;  ; An g, i.e. A =
a relation R on A is defined by

8x y 2 A :
;

Sn

i=1 Ai

and Ai \ A j = ? for any i 6= j. If

x R y iff 9Ai of the partition such that x 2 Ai and y 2 Ai ;

( )

then R is an equivalence relation, and the distinct equivalence classes of R form the original partition fA1 ;  ; An g.
Proof
(i) Let Ai for i = 1;  ; m be all the distinct equivalence classes of R. For any x 2 A, since
[x is an equivalence class and hence must be one of the Ai s, we have from Lemma (i)
S A . Hence A  Sm A , implying A = Sm A because A  A for any
x 2 [x  m
i
i=1 i
i=1 i
i=1 i
i = 1; ::; m. Notice that Ai \ A j = ? for any i 6= j is obvious from Lemma (iii).
(ii)

(a) Reflexive: 8x 2 A =
() of R.

Sn

i=1 Ai ,

9i0 such that x 2 Ai . Hence xRx from the definition

(b) Symmetric: 8x; y 2 R such that xRy,


yRx from ().

9io such that x y 2 Ai , i.e. y x 2 Ai , implying


;

(c) Transitive: 8x; y; z 2 R such that xRy and yRz, 9i0 and j0 such that x; y 2 Ai0 and
y; z 2 A j0 . Since y 2 Ai0 , y 2 A j0 and Ai s are disjoint for different i, we must have
i0 = j0 , hence x; z 2 Ai0 , implying xRz.

From (a), (b), (c) we conclude R is an equivalence relation.

16.2 Property Closure

107

Since by definition of x 2 Ai means [x = fy 2 A : yRxg = fy 2 A : 9i0 such that x; y 2 Ai0 g, if


x 2 Ai for some i then i0 must be equal to i because A j \ Ak = ? for any j 6= k. We thus conclude
that all Ai s are just the distinct equivalence classes, and [x for any x 2 A is just one of Ai s.
Therefore we see that R will induce the original partitions fA1 ;  ; An g.
Example
4. Let R be the equivalence relation defined on Z by R = f(m; n) : m; n 2 Z, m  n (mod 3)g,
see examples in the previous lecture. Give the partition of Z in terms of the equivalence
classes of R.
Solution
(a) Pick any element in Z, say 0, we have
[0 =

fx 2 Z : x R 0g = f

6; 3; 0; 3; 6; g :

(b) Pick any element in Zn[0, say 1, we have


[1 =

fx 2 Z : x R 1g = f

5; 2; 1; 4; 7; g :

(c) Pick any element in Zn([0 [ [1), say 5, we have


[5 =

fx 2 Z : x R 5g = f

4; 1; 2; 5; 8; g :

(d) Since Zn([0 [ [1 [ [5) = ? , all distinct equivalence classes are now exhausted.
Hence the partition of Z under R is f [0; [1; [5 g.

16.2 Property Closure


Let R be a binary relation on a set A. A binary relation R0 on A is said to be the closure of R
with respect to property P if
(i) R0 has property P ,
(ii) R  R0 ,
(iii) If S is any relation with property P and R  S, then R0  S .

16 EQUIVALENCE RELATIONS

108
Example

5. Let A = f0; 1; 2; 3g and R = f(0; 1); (1; 2); (2; 3)g. From the graph for R
0s

-1 -2 s

we see that a transitive closure, or closure w.r.t. the transitivity, can be obtained by
adding arrows (0; 2) due to (0; 1) and (1; 2), (1; 3) due to (1; 2) and (2; 3), and (0; 3) due
to (0; 1) and newly obtained (1; 3). Hence the directed graph R0 will be
0s

-1

? ^ ?
s

i.e.

R0 =f(0,1), (1,2), (2,3), (0,2), (1,3), (0,3) g .

Note We may make an equivalence relation out of R as follows


0s

-1

 ?

-1

0s

= -1=

0s

? ^ ?
s

transitive closure !

? ^ ?
Y 2Y
3
s

reflexive closure !

3 =O) - =k
0s

1s

? ^ ?
Y 32Y
3
s

symmetric closure !
equivalence relation

N
Supplementary Exercises

1. Suppose R is an equivalence relation on a set A. If aRb for all a; b 2 A, how many equivalence classes are there?
2. Let A = f1; 2; 3; 4; 5; 6; 7; 8; 9; 10g. We define a binary relation R on A by
(m; n)

2R

iff

m  n(mod 2) :

List all the distinct equivalence classes.


3. Let A = f(0; 0); (0; 1); (0; 2); (1; 0); (1; 1); (1; 2); (2; 0); (2; 1); (2; 2)g. We define a binary relation R on the set A by
(m; n)R( j; k)

iff m

n= j

k:

Is R an equivalence relation? If yes, give representatives for all the distinct equivalence
classes.

109

17 Partial Order Relations


17.1 Partial Orderings
Let R be a binary relation on a set A.

I
I

R is antisymmetric if for all x; y 2 A, if xRy and yRx, then x = y.


R is a partial order relation if R is reflexive, antisymmetric and transitive.

In terms of the digraph of a binary relation R, the antisymmetry is tantamount to saying


there are no arrows in opposite directions joining a pair of (different) vertices.
Example
1. Let A = f0; 1; 2g and R = f(0; 0); (0; 1); (0; 2); (1; 1); (1; 2); (2; 2)g and S = f(0; 0); (1; 1); (2; 2)g
be 2 relations on A. Show
(i) R is a partial order relation.
(ii) S is an equivalence relation.
Solution We choose to use digraphs to make the explanations in this case.
(i) The digraph for R on the right implies

Reflexive: loops on every vertex.

Transitive: if you can travel from vertex v to vertex


w along consecutive arrows of same direction, then
there is also a single arrow pointing from v to w.

Antisymmetric: no r

q1Y
r

~ )y 2
r

type of arrows.

(ii) The digraph for S on the right is reflexive due


to loops on every vertex, and is symmetric
and transitive because no no-loop arrows exist.

Note In example 1, R and S are built on A from  and = respectively by


R

f(x y) :
f(x y) :
;
;

x; y 2 A; x  yg ;
x; y 2 A; x = yg :

Hence partial order relation and equivalence relation can be in general regarded as generalisation of  and = respectively. For the same reasons, they are often denoted by

17 PARTIAL ORDER RELATIONS

110

I x  y if xR1 y and R1 is a partial order relation,


I x  y if xR2 y and R2 is an equivalence relation.
Let R be a partial order relation on a set A.

For any elements a; b 2 A, aRb can be alternatively denoted by a  b (hence aRb


also implies a  b implicitly) because R is a partial order relation. a  b means
element a precedes element b under the partial order relation R.

I
I

Two elements a; b 2 A are comparable if either aRb or bRa, i.e. either a  b or b  a.


If all elements of A are comparable with each other, then the partially ordered set A (w.r.t.
R) is said to be a totally ordered set, and the relation R is also said to be a total order
relation.

An element a 2 A is a maximal element of A if b  a holds for every b 2 A whenever b


and a are comparable.

I
I

An element a 2 A is a greatest element of A if b  a holds for all b 2 A.

An element a 2 A is a minimal element of A if a  b holds for every b 2 A whenever b


and a are comparable.
An element a 2 A is a least element of A if a  b holds for all b 2 A.

Example
2. Let A be the set of all subsets of set fa; b; cg. Show the subset relation
8u; v 2 A,
u  v or uRv; iff u  v;

 on A, i.e.

is a partial order relation. Find a minimal element and a greatest element.


Solution It is easy to verify that  is a partial ordering. Since ? is a subset of any
u 2 A, i.e. ?  u, we see ? is not only a minimal element, it is also a least element of
A. Since for any u 2 A one has u  fa; b; cg, i.e. u  fa; b; cg, we see that fa; b; cg is a
greatest element of A.

17.2 Hasse Diagrams


Hasse diagrams are meant to present partial order relations in equivalent but somewhat simpler
forms by removing certain deducible noncritical parts of the relations. For better motivation
and understanding, well introduce it through the following examples.

17.2 Hasse Diagrams

111

Examples
3. The relation in example 2 can be drawn as

fa b cg
>*7 6Y}I
9 fb cg
y
>* 6
f>*a icg

6 fbg 1 y fcg
y?
r

fa bg~ rY

6}6
fag Yk

rp

It is somewhat messy and some arrows can be derived from transitivity anyway. If we

I
I
I
I

omit all loops,


omit all arrows that can be inferred from transitivity,
draw arrows without heads,
understand that all arrows point upwards,

then the above graph simplifies to


q

fa b g

fag

fa b cg
;

fa cg
q
fbg

fb cg

fcg

This type of graph is called a Hasse diagram, it is often used to represent a partially
ordered set. If a digraph of a partial order relation is simplified according to the steps
under the 4 I symbols in the above, then the graph becomes a Hasse diagram.
4. Let A = f1; 2; 3; 9; 18g and for any a; b 2 A,

a  b iff

ajb :

Draw the Hasse diagram of the relation. Give a maximal element and a greatest element
if any.
Solution First it is easy to verify that the relation
r
18
 defined above is a partial ordering.The directed
graph of relation  is drawn on the right. By
r
9
removing all the loops, deleting all the edges that
r
2r
can be derived transitively, and making sure all
3
the retained edges will point upwards implicitly,
r
1
we obtain the following Hasse diagram

9
 6Y
o

6Y

9
>

17 PARTIAL ORDER RELATIONS

112
18r

This is because the edge from vertex 1 to vertex


18, for instance, can be derived from the edge
from 1 to 2 and the edge from 2 to 18. Hence
the edge from 1 to 18 shouldnt be kept in the
Hasse diagram as it can be induced from the
transitivity.

r9
r

1
Since 18 divides no elements in the set A other than itself, no elements in A is preceded
by 18 other than 18 itself because of the definition of the partial order relation . Hence
18 is a maximal element. Since all elements in A divide 18 means a  18 holds for all
elements a 2 A, 18 is also a greatest element.
Finally we remark that the vertices in the digraphs in this and the previous examples happen to be conveniently placed for creating the corresponding Hasse diagrams, in the sense
that all arrows are already pointing upwards. If this is not the case, then one may have to
twist the digraph to make it so, ready for the transformation into a Hasse diagram.
Note In a partial order relation, all greatest elements are also maximal elements and all least
elements are also minimal elements. In terms of a Hasse diagram, all local top vertices are
maximal elements, and all local bottom vertices are minimal elements. Two elements are comparable if there is a path connecting them and that the walk along the path does not change
direction vertically on any edge of the path. If a Hasse diagram has only finite number of vertices, then a maximal element is also a greatest element if there is only 1 maximal element, and
a minimal element is also a least element if there is only 1 minimal element.
Example
5.

(a) Let set A be given by

A = f3; 4; 5; 6; 10; 12g

and a binary relation R on A be defined by


(x; y)

iff

xjy ;

i.e. (x; y) 2 R if and only if x divides y. Give explicitly R in terms of its elements
and draw the corresponding Hasse diagram. List all the maximal, minimal, greatest,
and least elements.
(b) Let a new binary relation R0 on the set A given in (a) be defined by
(x; y)

2R

2 R0

if and only if either x j y or y j x

and R00 be the transitive closure of R0 . Use directed graphs to represent R, R0 and R00
respectively. Which of the three relations R, R0 and R00 is an equivalence relation?
For the equivalence relation, give all the distinct equivalence classes.

17.2 Hasse Diagrams

113

Solution
(a) Among all the elements of set A = f3; 4; 5; 6; 10; 12g, obviously 3 2 A, for instance,
divides 3, 6 and 12. Hence by the definition of the relation R specified by the question we conclude (3; 3), (3; 6) and (3; 12) are all elements of the relation R. Likewise
we can show that (4; 4), (4; 12), (5; 5), (5; 10), (6; 6), (6; 12), (10; 10), (12; 12) are
all elements of R. In fact we have
R

f (3 3)
;

(3; 6); (3; 12); (4; 4); (4; 12); (5; 5);

(5; 10); (6; 6); (6; 12); (10; 10); (12; 12)

Hence the digraph for R is


12


 6
r

10
6

?
M

r
r

k4

63

which induces the following Hasse diagram


12

r
r

10

r
r

5
4

3
From the Hasse diagram, we observe that there are 2 local top vertices, 12 and 10,
and 3 local bottom vertices, 3, 4 and 5. Hence 12 and 10 are maximal elements, and
3, 4 and 5 are minimal elements. Since the Hasse diagram has only finite number
of vertices, the fact that the Hasse diagram has more than 1 local top vertices means
there are no greatest elements. Likewise there are no least elements either.

17 PARTIAL ORDER RELATIONS

114

(b) The digraphs for R is already given in (a). As for R0 we observe that, according to the
definition of the relation R0 , if (x; y) (e.g. (3; 6) ) is in R so will (y; x) (thus e.g. (6; 3)
). Hence we can obtain R0 by adding to R the symmetric pairs like (6; 3), (12; 3)
etc. In terms of the digraph, such addition of elements is equivalent to drawing an
opposite arrows to each existing (non-loop) arrows. The digraph of R0 thus takes the
following form
12


 6
r

?
M

10
6
r

Uk4
?
^
63
r

Since relation R00 is the transitive closure of R0 , we can derive R00 from R0 by connecting 3 to 4 and 4 to 6 and connecting 4 to 3 and 6 to 4. We connect 3 to 4 via an
direct arrow because we can travel from 3 to 12 and then from 12 to 4 all along the
arrows, and we connect 4 to 3 because we can travel from 4 to 12 then to 3 all along
the arrows too. Similar reasons are applicable for the arrows from 4 to 6 and 6 to 4.
Hence R00 can be drawn as
12


 6
r

?
6M

10
6
r

:U k 4
?
^9 
63
r

Since there is no arrow from element 12 to element 3 in the digraph of R despite


the existence of an arrow from 3 to 12, relation R is not symmetric hence is not an
equivalence relation. Since relation R0 is not transitive (because its transitive closure
R00 is not the same as R0 itself), relation R0 is not an equivalence relation either. As
for the relation R00 , it is obviously reflexive, symmetric and transitive. Hence R00 is
an equivalence relation.

17.3 Topological Sorting

115

Since elements 3, 4, 6 and 12 are all related (connected) to each other through the
arrows of the digraph R00 and none of these 4 elements are related to any other
elements, they must form a single equivalence class. Hence we have
[3 =

f3

4; 6; 12 g :

Likewise we can derive another equivalence class


[5 =

f5

10 g :

Because any element of A is either in the equivalence class [3] or in the equivalence
class [5], these two classes are all the distinct equivalence classes.

N
17.3 Topological Sorting
For any finite set A and a partial order relation  on the set, the purpose of topological sorting
is to sort all the elements of the set A into an ordered list such that its sequential order preserves
the partial order dictated by the relation . In other words, if a and b are 2 arbitrary elements
of A and a precedes b, i.e. a  b, then a must appear before b in the resulting topologically
sorted list. More precisely, topologically sorting a set A of n elements with respect to (w.r.t.) a
partial order relation  is to find an ordered enumeration, a1 ; a2 ; :::; an , such that for all i and
j, ai  a j implies i  j, where ak for k = 1; ::; n denotes the element placed to the k-th position
of the resulting list. We note that the topologically sorted list a1 ; :::; an can also be characterised
by another partial order relation 0 defined by (recall a binary relation on A is a subset of the
Cartesian product A  A)

0 = f (a b) 2 A  A j 9i
def

j such that a = ai ; b = a j ; i  j g :

We observe that the relation 0 is in fact a total order relation and is defined so that a1 0 a2 ,
a2 0 a3 , ..., an 1 0 an . Hence if we sort the set A w.r.t. the ordering 0 using a usual sorting
algorithm such as the insertion sort, we will then obtain exactly the same resulting list a1 ; :::; an.
Two partial order relations R1 and R2 on the same set A are said to be compatible if, whenver
a and b are comparable under both R1 and R2 , we have (a; b) 2 R1 iff (a; b) 2 R2 . Hence the
topological sorting of a set A w.r.t. a partial order relation  can be regarded as the construction
of a total order relation 0 such that 0 is compatible with the existing partial order . In
general, a topological sorting doesnt produce a unique result, unless the existing partial order
relation  is in fact also a total order relation.

17 PARTIAL ORDER RELATIONS

116
Example

6. Let A = fF; M ; D; Sg denote a set of family members, F (father), M (mother), D (daughter)


and S (son). Suppose the family have just acquired a computer game and all wish to play
it as soon as possible. In what order can the family take turns to play the game, if the
family tradition that children be given priority when it comes to playing games is to be
observed?
Solution It is obvious that there are 4 acceptable solutions. They are (i) D; S; M ; F; (ii)
D; S; F; M; (iii) S; D; M ; F and (iv) S; D; F; M. In obtaining any of the above 4 solutions,
we have implicitly done a topological sorting! In fact, the family tradition that children
be given priority can be precisely represented by a partial order relation  where all the
comparable pairs are list below
D  M;

D  F;

S  M;

SF

Let us now check that the list (i) is indeed a topological sorting of the set A. That is, the
list D; S; F; M preserves the partial order relation .
First we compare D with S, M and F. Since D and S are not comparable because neither
has the priority, the order these 2 appear in the resulting list is not relevant. Since D
comes ahead of both M and F is consistent with the existing partial order D  M and
D  F respectively, we conclude that the 1st element, D, observes the existing partial
order . We then compare the 2nd element, S, with all of its later elements, M and F, in
the list (i). We can show likewise that S also preserves the existing partial order. Similarly
it can be verified that all elements in the list (i) are ordered consistently with the family
tradition characterised by the relation .
The topological sorting algorithm for a (nonempty) finite set with respect to a partial order
relation  is in fact straightforward. The algorithm is as follows.
(i) Set the resulting list to empty initially.
(ii) Pick any minimal element in A. Append the element to the end of the resulting list and
remove the element from the set A.
(iii) Go back to step (ii) if A is still nonempty. Otherwise the algorithm terminates.
With the assistance of Hasse diagrams, the above topological sorting algorithm can be simplified
to the following steps.
(i) Set the resulting list to empty initially.

17.3 Topological Sorting

117

(ii) Pick any minimal element of the Hasse diagram. Append the element to the end of the
resulting list and remove the element, along with all the edges that are directly connected
to it, from the Hasse diagram.
(iii) Go back to step (ii) if the Hasse diagram is still nonempty. Otherwise the algorithm
terminates.
We note that minimal elements in a Hasse diagram are those bottom vertices in the diagram. By
a bottom vertex we mean a vertex that is not downwardly connected to any other vertices in the
Hasse diagram.
Examples
7. Let A = f1; 2; 3; 9; 18g and, for any a; b 2 A, a  b iff a j b. Construct a topological sorting
for the relation  on the set A.
Solution The diagrams below depict the Hasse diagrams created and utilised in the topological sorting algorithm. To start with, we first set the resulting list T to an empty list,
and observe that the Hasse diagram in A shows 1 is the only minimal element in A.
Hence we choose 1 as the 1st element of the resulting list T , and remove 1 and the
2 edges that are directly connected to 1. The resulting Hasse diagram for the new set
f2; 3; 9; 18g, the original set A after the removal of 1, is then given in B . There are 2
minimal elements in this case, 2 and 3. We choose 3 although we could also choose
2. By removing the selected minimal element 3 and its directly connected edge from
the Hasse diagram B , we obtain the new T and the new Hasse diagram for f2; 9; 18g in
C . There are again 2 minimal elements, 2 and 9. This time we pick 2. The resulting list T and the shrunk Hasse diagram for f9; 18g then become those in D . We proceed
similarly until all the elements of the Hasse diagram have been moved to the resulting list
T , see F . The list produced by the topological sorting is thus 1; 3; 2; 9; 18. We note
that the topological sorting is not unique in this case. For instance, the list 1; 3; 9; 2; 18 is
another valid topological sorting.
T:

T: 1
18q

T : 1; 3
18q

18q

q9

q
1

q9

q
3

T : 1; 3; 2; 9

18q

T : 1; 3; 2

q9

q
3

C
T : 1; 3; 2; 9; 18

18q
q9

17 PARTIAL ORDER RELATIONS

118
8. Let A be the set of all subsets of set fa; b; cg, i.e.

A = f ?; fag; fbg; fcg; fa; bg; fa; cg; fb; cg; fa; b; cg g ;
and a partial order relation  on A be defined by u  v iff u  v. Construct a topological
sorting for the relation  on the set A.
Solution The intermediate results, obtained by carrying out steps (i)-(iii) in the topological sorting algorithm, are summarised in the table below.
elements of set A
minimal elements
?; fag; fbg; fcg; fa; bg; fa; cg; fb; cg; fa; b; cg
?
fag; fbg; fcg; fa; bg; fa; cg; fb; cg; fa; b; cg
fag; fbg; fcg
fag; fcg; fa; bg; fa; cg; fb; cg; fa; b; cg
fag; fcg
fcg; fa; bg; fa; cg; fb; cg; fa; b; cg
fa; bg; fcg
fcg; fa; cg; fb; cg; fa; b; cg
fcg
fa; cg; fb; cg; fa; b; cg
fa; cg; fb; cg
fb; cg; fa; b; cg
fb; cg
fa; b; cg
fa; b; cg
none
none

pick

?
fbg
fag
fa bg
fcg
fa cg
fb cg
fa b cg
;

;
;

stop

Hence the topological sorting for the set A gives

? fbg fag fa bg fcg fa cg fb cg fa b cg


;

We note that the construction of the above table may, or may not, make use of the following list of Hasse diagrams for locating the minimal elements.
q fa; b; cg

fa bg

fag

fa cg
q fbg
;

q fa; b; cg
q
q

fb cg fa bg

fag

fcg

fa cg
;

fa cg
q fbg
;

q
q

q fa; b; cg

fa bg

q fa; b; cg

fb cg fa bg

fag

fcg

fb cg
;

fcg

fa cg
;

D
q fa; b; cg

fa cg
;

fb cg

fcg

B
q fa; b; cg

C
q fa; b; cg

q
q

fb cg
;

fcg

fa cg
;

fb cg
;

q fa; b; cg
q

fb cg
;

17.3 Topological Sorting

119

9. Find an alternative topological sorting for the problems in the previous 2 examples respectively.
Solution DIY.
10. Suppose a partial order relation R on the set A
Hasse diagram.

f3 4 5 6 10 12g has the following

12q

q10

q
q
q
q

Construct a topological sorting for the set A.


Solution One possible topological sorting is 5; 10; 4; 3; 6; 12.
Note Topological sorting has many applications, including syntax tree evalution in compiler
construction and schedule serialisation in concurrent programming or database management.
For further details on topological sorting, interested readers may consult the book Discrete
Mathematics with Applications by Susanna S Epp, 2nd edition, PWS Publishing Company,
1995.

Supplementary Exercises
1. Suppose a partial order relation R on the set A = fa; b; c; d ; e; f g has the following Hasse
diagram
dr
e

r
r

r
r

Give the maximal, minimal, greatest and least elements respectively whenever they exist.
2. Let R be a binary relation on R defined by xRy iff x  y. Show R is a partial order relation.
3. Let R be a binary relation on R defined by xRy iff x < y. Is R a partial order relation?
4. Suppose T is a rooted tree and V is the set of all the vertices of T . For any 2 vertices u
and v of the tree, we define u  v if and only if u is a vertex of a subtree of v. Show that
 is a partial order relation on V and that the Hasse diagram is in fact isomorphic to the
tree T itself.

18 SWITCHING CIRCUITS AND BOOLEAN ALGEBRA

120

18 Switching Circuits and Boolean Algebra


18.1 Switching Circuits
A switch has two states, open or closed, and a switching system is a collection of connected
switches, with 2 connection points available to the outside.
For example, the following
switching system

xm

ym

zm
r
m

switches

battery

light

is a switching circuit, making use of an energy source (battery) an output (light) as well as a
switching system. If switches x and z are open while switch y is closed, then the state of the
switching system may be represented by
r
r

r
r

z
In a switching system, switches may be connected with one another
xm
I in parallel
ym
ym
I or in series
xm
or through the use of complementary switches. For any given switch x the corresponding
complementary switch, denoted by x0 , is always in the opposite state to that of x. In order to
describe switching systems formally and mathematically, we denote open and closed states by
0 and 1 respectively. Furthermore, for any two switches x and y, we use x + y and x  y to denote
their parallel and series connections respectively. Its obvious the state space S for any switch
or switching system is composed of 2 states 0 (open) and 1 (closed), i.e. S = f0; 1g.
The symbols . and + here shouldnt be confused with those in the arithmetic, although
there exist some similarities. With the above introduced notations, we can represent the state of
or the effect of switches in parallel, in series and so on by the following tables

18.2 Boolean Algebra

x
0
0
1
1

121
Parallel
y x+y
0
0
1
1
0
1
1
1

x
0
0
1
1

Series
y xy
0 0
1 0
0 0
1 1

Complement
x x0
0 1
1 0

QQQ
OC k
CC
QQ
QQ
 switch y is open
switch x is closed
switching system with x and y in parallel is closed
7



As the special case in the above tables we have in particular 1 + 1 = 1.

18.2 Boolean Algebra


First recall that a collection of elements is typically called a set. If an element a belongs to a
set A, then we write a 2 A. Similarly we use a 62 A to denote that a is not an element of the set
A. For any two sets A and B, we can define another set, the Cartesian product of set A and
set B, written A  B, as the collection of all ordered pairs (a; b) such that a 2 A and b 2 B. In
def
other words, we have more explicitly A  B=f (a; b) j a 2 A; b 2 B g, where the pair of bracelets
enlists all the elements of the set. Similarly,
B

def

}|

B  B  B

is the set of all n-tuples (a1 ; a2 ; :::; an) for a1 ; :::; an 2 B.

A mapping f : A ! B is a relation which associates with each element x of set A an


element, denoted by f (x), in set B. The set A is said to the domain of the mapping, and
the set B, the codomain. In other words, a mapping is an entity that consists of a domain,
a codomain, and an element association between them.

A binary operation on a set A is a mapping f : A  A ! A, i.e. f (a; b) 2 A for any pair


(a; b) with a and b both in A.

A unary operation on a set A is a mapping f : A ! A, i.e. f (a) 2 A for any a in A.

Example
1. For switching systems with state space S=f0; 1g, the + and  operation are binary and
the 0 operation is unary.
Solution This is because for any switching systems x and y, we have that x + y, x  y and
x0 are all still switching systems with the same state space S.

Note Binary operator or operation has nothing to do with binary numbers.

18 SWITCHING CIRCUITS AND BOOLEAN ALGEBRA

122

A Boolean algebra is essentially a mathematical abstraction and extension of the switching


systems or the symbolic logic. More precisely, a Boolean algebra is a set S on which are
defined two binary operations + and  and one unary operation 0 and in which there are at least
two distinct elements 0 and 1 such that the following properties hold for all a; b; c 2 S
B1.
B2.
B3.
B4.
B5.

a+b = b+a
ab = ba
(a + b) + c = a + (b + c)
(a  b)  c = a  (b  c)
a + (b  c) = (a + b)  (a + c)
a  (b + c) = (a  b) + (a  c)
a+0 = a
a1 = a
a + a0 = 1
a  a0 = 0

commutativity
o

associativity
o

distributivity
o

identity relations
o

complementation

The 0 and 1 in the above are just 2 special elements denoted by 0 and 1 respectively.
They are symbols rather than normal numerical values. Likewise the operators +,  and
0 are also symbols, each representing the designated special roles. A Boolean algebra is
thus often represented by a tuple (S; +; ;0 ; 0; 1) which carries all the relevant components. A
combination of some elements of S via the connectives + and  and the complement 0 is a
Boolean expression. Among the 3 operations 0 ,  and + in a Boolean expression, the 0
operation has the highest precedence, then comes the  operation, and then the + operation.
For example, a + b0  c in fact means a + ((b0 )  c).
Theorem Let (S; +; ;0 ; 0; 1) be a Boolean algebra. Then the following properties hold for all
a; b; c 2 S

H
N

P1.
P2.
P3.
P4.
P5.
P6.
P7.

a+a = a; aa = a:
a+1 = 1; a0 = 0:
(a0 )0 = a :
a+ab = a:
If a + c = 1; a  c = 0, then c = a0 .
If a  c = b  c; a  c0 = b  c0 , then a = b.
If a + c = b + c; a + c0 = b + c0 , then a = b.

(idempotent laws)
(dominance laws)
(double complement)
(absorption law)
(uniqueness of inverses)
(cancellation law)
(cancellation law)

Proof For P1, the 1st half is derived from B1B5 by


a + a = (a + a)  1 = (a + a)  (a + a0) = a  (a + a0) = a  1 = a ;
B4

B5

B3

B5

B4

where the names on the equality sign indicate the property being used, while the 2nd half is
derived by
B4
B5
B3
B5
B4
a = a  1 = a  (a + a0) = a  a + a  a0 = a  a + 0 = a  a :

18.2 Boolean Algebra

123

For P2, we just need to observe


a + 1 = a + (a + a0 ) = (a + a) + a0 = a + a0 = 1 ;
B5

B2

P1

0 = a  a0 = (a  a)  a0 = a  (a  a0) = a  0 :

B5

B5

P1

B2

B5

The proof of P3 is equally straightforward because


a00 = a00  1 = a00  (a + a0 )
B4

B5

B3;B1 00
= a

B4
00
0 B3 00 0 B5
 a + a00  a0 B5=B1 a00  a + 0 B5
= a  a + a  a = (a + a )  a = 1  a = a
;

Other properties, P4P7, can be derived similarly.


Note An immediate corollary of P5 in the above theorem is that 00 = 1 and 10 = 0 hold on any
Boolean algebra (S; +; ;0 ; 0; 1).
Examples
2. Switching system (S; +; ;0 ; 0; 1) with S = f0; 1g is a Boolean algebra.

Solution We need to show B1 B5, by letting +,  and 0 specifically denote


switches in parallel, in series and in complementation respectively. The identities in B1
are valid because
am
bm
=
, i.e. a + b = b + a
am
bm
am

bm

bm

am

ab = ba

i.e.

The proof of B2 B5 is elementary. Hence well simply show only the first half of B3,
i.e. a + (b  c) = (a + b)  (a + c). Well show the identity by the use of the evaluation table
below
a
0
0
0
0
1
1
1
1

b
0
0
1
1
0
0
1
1

c
0
1
0
1
0
1
0
1

bc a+b a+c


0
0
0
0
0
1
0
1
0
1
1
1
0
1
1
0
1
1
0
1
1
1
1
1

a + (b  c)
0
0
0
1
1
1
1
1
|

(a + b) (a + c)

0
0
0
1
1
1
1
1
{z

all corresponding values exactly same

3. Let R be the set of real numbers, +;  be the normal addition and multiplication, 0; 1 2 R
be the normal numbers. If we define 0 by x0 = 1 x for any x 2 R , then is (R ; +; ;0 ; 0; 1)
a Boolean algebra?

18 SWITCHING CIRCUITS AND BOOLEAN ALGEBRA

124

H
N

Solution No. Because neither the 1st half of B3 nor the 2nd half of B5 is satisfied. For
example,
1 + (2  3) 6= (1 + 2)  (1 + 3):
4. Suppose S = f1; 2; 3; 5; 6; 10; 15; 30g. Let a + b denote the least common multiple of a and
30
b, a  b denote the greatest common divisor of a and b, and a0 = . Prove (S; +; ;0 ; 1; 30)
a
is a Boolean algebra.
Solution Since this question is marked by a pair of solid arrows on the margin, it is
entirely optional and is not examinable. Ill thus save it for one of those bored evenings
or sleepless nights.

18.3 Algebraic Equivalence


Switching systems and symbolic logic are essentially the same. Furthermore they both form a
Boolean algebra (S; +; ; ; 0; 1) on a set S = f0; 1g, where 0 is open in the switching systems
and is F in the symbolic logic. The correspondence can be seen in the following table
Boolean algebra
S = f0; 1g
+

0
1

Switching systems

Symbolic logic

fopen, closedg

fF T g
p _ q (or)
p ^ q (and)
 p (not)
;

x + y (in parallel)
x  y (in series)
x0 (complement)
circuit open
circuit closed

contradiction
tautology

Hence properties B1B5 and P1P7 will also hold in symbolic logic, when +, , 0, 0
and 1 are replaced by _, ^, , contradiction and tautology respectively. Hence, for
example,
a _ (b ^ c)  (a _ b) ^ (a _ c); a ^ (b _ c)  (a ^ b) _ (a ^ c);
a _ a  a;

a _ (a ^ b )  a ;

a ^ a  a;

 ( a )  a a _ F 0  a a ^ T 0  a
a ^ ( a)  F0 a _ ( a)  T0 a ^ F0  F0 a _ T0  T0
;

hold for any propositions a, b and c, where F0 represents a contradiction and T0 represents a
tautology.
Examples
5. Convert ( p _ q) ! r into the corresponding Boolean expression.

Solution Since p ! q is equivalent to ( p) _ q, we see that ( p _ q) ! r is equivalent to


(( p _ q)) _ r which is thus converted to ( p + q)0 + r.

18.4 Sets connection with Boolean Algebra

125

6. (De Morgans Laws) Let (S; +; ;0 ; 0; 1) be a Boolean algebra, then for any x; y 2 S

0  y0;

(x + y) = x

(x

 y)0 = x0 + y0

Solution Proof obvious from the theorem in the previous section.


7. Suppose (T; |; }; ~; t; u) is a Boolean algebra. Show (t~ )|t = u.

Solution Recall that when we say (T; |; }; ~; t; u) is a Boolean algebra, the tuple means
that |, } and ~ correspond respectively to the +,  and 0 operations entailed by a
Boolean algebra, and that t and u correspond respectively to the 0 and 1 elements
possessed by the Boolean algebra. Hence (t~ )|t = u is the same as (00 ) + 0 = 1 and is
thus obviously true.

8. Let B = f0; 1g and (B; +; ; 0 ; 0; 1) be a Boolean algebra. Let Bn denote the set of all the
def
tuples (x1 ; x2 ; :::; xn) with x1 ; :::; xn being any elements of B, i.e. Bn = f(x1 ; :::; xn) j x1 2
B; :::; xn 2 Bg. Then (Bn , +, , , 0, 1) is also a Boolean algebra if
z

n 00 s

}|


(1 

x+y

(x1 + y1 ;

xy

=
=

(0;
;

0) ;

1) ;

 xn + yn )
(x1  y1  xn  yn )
0
0
(x1  xn )
;

18.4 Sets connection with Boolean Algebra


If we make the correspondence between

?U[\
;

0 and 0; 1; +; ; 0

for sets and Boolean algebra respectively, we see that properties S1S5 are exactly those B1B5
for the definition of Boolean algebra. Hence for any nonempty set S, for instance, (P (S); [; \; 0 ; ?; S)
is a Boolean algebra.
Theorem If B is a Boolean algebra with exactly n elements then n = 2m for some m. Furthermore B and (P (f1; 2;  mg); [; \; 0; ?; f1; 2;  ; mg) essentially represent the same Boolean
algebra.
Example

9. Let U = f2; 3g and S = P (U ). Then (S; [; \;0 ; ?; U ) is a Boolean algebra. Notice that
the set S in this case contains more than 2 elements.

19 BOOLEAN FUNCTIONS

126

Supplementary Exercises
1. Represent the Boolean expression ((a  b) + c)  (b + d ) as a switching system. Is the
switching system conductive when switches a and b are closed and switches c and d are
open?
2. Let Z be the set of all integers and, N , the set of all nonnegative integers. Is the mapping
f : Z ! Z with f (n) = n2 exactly the same as the mapping g : Z ! N with g(n) = n2 ?
3. Let (S; +; ;0 ; 0; 1) be a Boolean algebra. Show that a = a + a  b holds for all elements a
and b of the set S.

19 Boolean Functions
A Boolean function or truth function f (x1 ;  xn ) is a mapping f : f0; 1gn
completely determined by the corresponding truth table and vice versa.

! f0 1g.
;

It is

19.1 Convert Boolean Function to Boolean Expression


Let B = f0; 1g and f : Bn ! B be a Boolean function. If f is not already explicitly defined by a
truth table, we can easily establish one first. The procedure is as follows
(i) List all possible input states and the corresponding function values, i.e. list the truths
table.
x1 x2
0
0
0 0
2n rows
. ..
>
>
> ..
.
8 0
>
>
>
0
>
<
>
:



xn 1 xn
0
0
0
1
1
0
..
..
.
.
1
1

f (x1 ;  ; xn )


X
yXXXX

..
.

* represents 0 or 1
depending on the function f

(ii) remove all rows whose output state is 0, i.e. f (x1 ;  ; xn ) = 0.
(iii) replace 0 and 1 in xi columns by x0i and xi respectively.
(iv) Sum up all the (input state) product terms.

19.2 Canonical Form

127

Example
1. Let f : f0; 1g3 ! f0; 1g be given by f (x; y; z) = xyz + min (x; (y + z)0 ). Here min means
minimum. Write f as a Boolean expression, i.e. as a sum of product terms.
Solution From the following truth table
x
0
0
0
0
1
1
1
1

QQ
s 
Q
1



y
x

y
0
0
1
1
0
0
1
1

z
0
1
0
1
0
1
0
1

f (x; y; z)
0
0
z
0
0
1
contributes to x  y  z
0
0
1










We see that only two rows will actually contribute to the final Boolean expression
f (x; y; z) = xy0 z0 + xyz
where the binary operator  is omitted for simplicity.

19.2 Canonical Form


Once we know that all Boolean functions can be expressed as a sum of product terms, it will be
meaningful to introduce the concept of canonical representation so that each function will have
a unique canonical form.
Let (S; +; ; 0; 0; 1) be a Boolean algebra and x, y and z be any variables taking values in S,
then

I
I
I
I
I

literals are individual variables or their complements, e.g. x, x0 , y, y0


product terms are expressions made of literals and the binary operation  although the
symbol  is optional, e.g. x, x0 , x  y0 z, x  y  x0 y  z
Boolean expression is a sum of product terms, e.g. x + x0  y(z + y0 )
standard product term of an expression is a product in which each variable appears
in the product exactly once, e.g. xy0 is a standard product of xy0 + x + x0 y0 , but is not a
standard product of xy0 + xyz because variable z is missing from the product.
a Boolean expression is canonical if it is a sum of distinct standard product terms, e.g.
xy + x0 y0 is canonical, but xy + x0 y + z is not canonical.

19 BOOLEAN FUNCTIONS

128
Two Boolean expressions are equivalent if they either

I
I
I

have the same truth table, or


can be transformed from one another through the use of the axioms B1 B5 in the previous lecture, and their induced properties such as the De Morgans Laws, or
have the same canonical form.

Example
2. Show Boolean expressions x and xy + xy0 are equivalent.
(a) By truth table
x
0
0
1
1

y y0
0 1
1 0
0 1
1 0

xy0
0
0
1
0

xy
0
0
0
1

xy + xy0
0
0
1
1

same

(b) By properties of Boolean algebra


xy + xy0

B3

B5

= x(y + y ) =

x1

B4
=

(c) By canonical form


x = x  (y + y0 )
xy + xy0

=
=

xy + xy0
xy + xy0


same canonical form

19.3 Minimal Representation


Among equivalent Boolean functions or expressions, it is natural that we often want a simpler
or even simplest form. To serve this purpose we call a Boolean expression E a minimal
representation of a Boolean function f if E represents f and
(i) E is a sum of product terms;
(ii) if F is any sum of product terms representing f , then the number of product terms in E is
no greater than that in F;
(iii) if F is any sum of product terms representing f and having same number of product terms
as in E, then the total number of literals in E is no greater than that in F.

19.3 Minimal Representation

129

Examples
3. Find the minimal representation for f

= xy + xy + xx0 + xy0

Solution First we observe


f

(xy + xy) + xx + xy

xy + 0 + xy0 = xy + xy0

x(y + y0 ) = x  1 = x :

Since f is not identical to 0 or 1, and its equivalent expression x has only 1 product term
and 1 literal in total, this equivalent Boolean expression must be a minimal representation
of f .
4. Show f
below

= xyz0 + xy0 z + xy0 z0 + x0 yz + x0 yz0 is equivalent to any of the 4 Boolean expressions

(a) xy0 + x0 y + xyz0


(c) xy0 + x0 y + yz0

(b) xy0 + x0 y + xz0


(d) xy0 + x0 yz + yz0

and (a) and (d) are not minimal.


Solution Since f is already in canonical form, once we convert (a)(d) into canonical
forms we should see that the newly obtained canonical forms are in exactly the same form
as f itself. For (a), for instance, we have
xy0 + x0 y + xyz0

=
=

xy0 (z + z0) + x0 y(z + z0 ) + xyz0

xy0 z + xy0 z0 + x0 yz + x0 yz0 + xyz0

which is same as f . We can also derive (a)(d) from f directly. For (c), for instance, we
have
f

xyz0 + xy0 z + xy0 z0 + x0 yz + x0 yz0

(xy z + xy

=
=

0 z0 ) + (xyz0 + x0 yz0 ) + (x0yz + x0 yz0 )


xy0 (z + z0 ) + (x + x0 )yz0 + x0 y(z + z0 )
xy0 + yz0 + x0 y :

It is easy to see that expressions (a)(d) all have 3 product terms. However (a) and (d)
both have 7 literals while (b) and (c) both have only 6 literals. Hence neither (a) nor (d)
is a minimal representation. We finally note that (b) and (c) in example 4 are minimal
representations.
5. The Boolean expression f in example 4 may be drawn as a switching circuit below
according to (b)

130
s

xn

0
yn

0
xn

yn

xn

0
zn

19 BOOLEAN FUNCTIONS
0
xn s
yn
s

0
zn

or

0
xn

yn

In general it is difficult to show a Boolean expression is minimal from the very definitions.
We shall instead claim that the simplified forms provided by Karnaugh maps, to be given shortly,
are minimal representations.

19.4 Logic Gates


Electronically, Boolean functions or expressions are typically implemented through the use of
a collection of logic gates, which are the basic building blocks of logic circuits. These logic
gates are as usual characterised by their input-output correspondence, and the most popular 5
are as follows.

An AND-gate, taking p and q as the input, will output the logical value pq. The diagram
for the gate is drawn as
p
pq
q
A typical application of an AND-gate can be found in the following electronic circuit

circuit
and

5V

source

0V

0V

output

?
ground

Incidentally, a 5 voltage indicates a logical 1 or true, and a 0 voltage indicates a logical 0


or false.

An OR-gate, taking p and q as the input, will output the logical value p + q :
p

p+q

A NOT-gate, taking p as the input, will output the logical value p0 :


p

p0

19.4 Logic Gates

131

An NAND-gate, taking p and q as the input, will output the logical value ( pq)0 :
p
d
( pq)0
q
Obviously, an NAND-gate can be built through the use of an AND-gate and a NOT-gate
as in the following diagram
p
d
( pq)0
q

An NOR-gate, taking p and q as the input, will output the logical value ( p + q)0 :
p
d
( p + q )0
q
Similarly, an NOR-gate can be built from an OR-gate and a NOT-gate via
p
d
( p + q )0
q

Examples
6. Draw a gate implementation for the Boolean expression xy0 z0 + xyz.
Solution

xy0 z0
x
y

xy
d

xyz

y0

0
dz
z

xy0 z0 + xyz

y0 z0

7. Show that any Boolean expression can be represented by NAND-gates alone.


Solution Since every Boolean expression can be represented by some AND-gates, ORgates and NOT-gates, if we can construct these 3 types of gates with NAND-gates alone,
then we have shown that any Boolean expression can be presented by just the NANDgates. Since we can construct NOT-gates via
p
r
d
p0
,

20 KARNAUGH MAPS

132
AND-gates via

( pq)0

q
and OR-gates via
p

p0
d

pq

p+q

q0

because ( p0 q0 )0 = p + q, we conclude that NAND-gates alone are enough to represent any


Boolean expressions.

Supplementary Exercises
1. Use Boolean algebra properties to show
(a) x + x + x + yz + xx0 + yz = x + yz.

(b) x = xyz + xy0 z + xyz0 + xy0 z0 .

2. Let a Boolean function f (x; y; z) be given by the table


x
0
0
0
0
1
1
1
1

y
0
0
1
1
0
0
1
1

z
0
1
0
1
0
1
0
1

f (x; y; z)
0
1
0
0
0
1
1
0

Give a Boolean expression that corresponds to this function.


3. Implement the Boolean expression xy through the use of NOR-gates alone.
4. Design a switching circuit for a light that has 3 switches. The light has to be able to be
turned on or off from any of the 3 switches.

20 Karnaugh Maps
The Karnaugh map provides a pictorial technique to simplify Boolean expressions.

20.1 Grid Layout for Karnaugh Maps

133

20.1 Grid Layout for Karnaugh Maps

I
I

Two product terms are said to be adjacent, if they differ by exactly one literal and if this
literal appears in one product and its complement in the other. For example, wx0 y0 z0 and
wx0 yz0 are adjacent.
Since a Boolean expression has a canonical form, we draw a grid or table such that all possible standard products have a unique position or box in the grid. For example, Boolean
expression f (x; y; z) may be associated with the grid
xy

xy0

x0 y0

x0 y

z
z0
in which the terms on the edges of the grid serve as labels. More specifically, a special
case of Boolean expression xy + y0 z, for instance, has the canonical form
xyz + xyz0 + xy0 z + x0 y0 z
which can be represented by placing a 1 in the corresponding box in the above grid for
each standard product term in the canonical form, leaving the rest of the boxes (if any)
empty or filled with 0. The finished grid here, called a Karnaugh map, reads

z
z0

xy
1
1

xy0
1

x0 y0
1

x0 y

The grids for Karnaugh maps should satisfy

I
I
I

each standard product term uniquely represented by a box,


adjacent boxes (vertically or horizontally or in other legal directions) represent adjacent
product terms,
each box has exactly n neighbours (adjacent product terms) if the Boolean expression has
exactly n variables.

Examples
1. For Boolean expressions with variables x; y and z,
xy
z
z0

xy0 x0 y0 x0 y
2l
 3l

AK
HHH AA 6
Y
3 neighbours

1l

and z0
z

y0 x0

y0 x

yx

yx0

20 KARNAUGH MAPS

134

are both valid grids, and there are other possible valid grids as well.
2. For Boolean expression with 4 variables w; x; y and z, a typical valid grid would be
wx0

wx
yz
yz0
y0 z0
y0 z

w0 x0

w0 x

2l

1l

3l

4l

where 1l
, 2l
, 3land 4lare the 4 neighbours of the shaded box.
3. For Boolean expression with 5 variables v; w; x; y and z, the following grid
vwx
yz
yz0
y0 z0
y0 z

vwx0

vw0 x0 vw0 x
4l

v0 w0 x

v0 w0 x0

v0 wx0

v0 wx

2l

1l

3l

5l

is a typical grid and shaded box has 5 neighbours 1l


, 2l
, ..., 5l.
The main feature of a Karnaugh map is that any block of boxes (of 1) can be represented
by a single product term if the number of boxes in each dimension or direction is of the form
2m for some m 2 N . In fact well reserve the word block exclusively for this sense.

I
I

A block of 2M boxes in a Boolean expression of N variables can be represented by a single


product term with N M literals.
If a variable changes when we move inside a block, the literals related to the variable will
not exist in the reduced single product term.

Examples
4. The circled area in the Karnaugh map
is a 2  2 block representing

x changes inside the block

xy xy0 x0 y0 x0 y
z

z0

virtually connected

xyz

z changes

inside the
block

xyz0

x0 yz

x0 yz0

xy(z + z0 ) + x0 y(z + z0 )

xy + x0 y = (x + x0 )y = y

20.2 Simplification Procedure

135

In other words, the block of 4 is simplified to a single product term y (with 1 = N


3 2 literals)

M=

5. The circled area in the Karnaugh map


wx wx0 w0 x0 w0 x
yz

1 1


is a 2  4 block representing z.

Notice that in a typical term wlxlylz in


the block, the variable names w, x and y
may change inside the circled block, and
are thus not present in the final product
term which becomes z.
0

yz0
y0 z0


y0 z

vertically wrapped around

20.2 Simplification Procedure


The procedure for simplifying a Boolean expression via Karnaugh map is as follows
(i) Write the expression in terms of standard product terms.
(ii) Place a 1 in the box corresponding to each standard product term.
(iii) Circle isolated 1s.
(iv) Circle 2 adjacent 1s (pairs in any direction, or blocks of 2) such that the newly circled
1s cant be contained a block of 4. Use as few circles as possible.
(v) Circle any blocks of 4 (containing at least one uncircled 1) such that the newly circled 1s
cant be contained in a block of 8. Use as few circles as possible.
(vi) Circle blocks of 2n (n  3) boxes similar to (v).
Then simplified expression can be read off the Karnaugh map with each circle representing one
product term. The final result is a minimal representation of the Boolean expression.
Examples
6. Use a Karnaugh map to simplify F

= xyz + x0 y + x0 y0 z0 .

Solution
(a) F

= xyz + x0 yz + x0 yz0 + x0 y0 z0

is the canonical form.

20 KARNAUGH MAPS

136

(b)

z
z0

xy0

xy
1

x0 y0

x0 y
1
1

(c) no isolated 1s .
(d) No blocks of 4. But there are pairing choices. We select least number of pairs.
xy xy0 x0 y0 x0 y
z

z0

(e) No blocks of 4 means no blocks of 2m for m  2.


Thus the minimal representation can be read off as yz + x0 z0 .
7. Suppose a Boolean expression can be represented by the following Karnaugh map

yz
yz0
y0 z0
y0 z

wx
1
1

wx0
1
1
1

w0 x0
1

w0 x
1

1
1

1
1

the the simplification procedure is as follows.


wx wx0 w0 x0 w0 x

(a) No isolated 1s.

(b) Only 2 possible pairs exist, that


yz
1 
1 1 1
cant be included in a block of 4
1 1
yz0
(1s). Since a 2nd pair will not


1
1
y0 z0
cover any uncircled 1s that are not

1

be covered by blocks of 4, we need
1 1
y0 z
to pick just 1 pair.
(c) No blocks of 8. But there are 3 blocks of 4, each covering new 1s. The Karnaugh
map now looks like

yz
yz0
y0 z0
y0 z

0 0 0

wx wx $
wx wx
'


1

1 1 '$
&%


1

&%

20.2 Simplification Procedure

137

(d) Since no uncircled 1s are left over the procedure is completed. The minimal representation is thus read off as
wy + yz + w0 y0 + x0 y0 z0
8. If one reverses the procedure and goes from big blocks to smaller ones, the results may
not be minimal. For instance, with bigger block first, we would have to circle in the
Karnaugh map below in the following order


1
'


'$

&%

then







&

which gives 5 product terms (corresponding to 5 circles). However, if we drop the block
of 4 circle, we still have all the 1s circled. But the new and equivalent expression will
only have 4 product terms.
With our normal procedure, nevertheless, well arrive at the correct answer represented
by








Note A reduction via the Quine-McCluskey procedure, for the similar purpose of Karnaugh
maps but more suitable for computerisation and for large number of variables, can be found in
the book Mathematical Structures for Computer Science by Judith L Gersting, 3rd edition, W
H Freeman and Company, 1993.

Supplementary Exercises
1. Among all the product terms in the Boolean expression
x0 yz + xy0 z0 + xz + xy0 + x0 yz + xyz + x0 y0 z ;
which are adjacent to the product term xy0 z? Write down the canonical form of the above
Boolean expression.

21 RECURRENCE RELATIONS

138
2. Simplify the Boolean expression x + x0 yz0 .

3. Give an alternative minimal representation, if any, for the Boolean expression yz0 + y0 z +
wx0 y.
4. Use a Karnaugh map to simplify the Boolean expression
vwy + vw0 yz + v0 yz + vwz0 + v0 wx0 z0 + vw0 x0 y0 z :
Is the minimal representation unique in this case?

21 Recurrence Relations
21.1 Recurrence Relations
A recurrence relation for a given sequence fai gim is a formula that relates each ak to some
of its predecessors. The first few elements in the sequence that can not be related to each other
by the recurrence relation are often determined by the initial conditions. A recurrence relation
is sometimes also called a difference equation.
Examples
1. For sequence fai gi2N , the following formula
an = 7an

5an

is a recurrence relation valid for n  2. The elements in the sequence that are not related
by the above formula are a0 and a1 . Hence a0 and a1 can be determined by the initial
conditions. Once the values of a0 and a1 are specified, the whole sequence fai gi0 is
completely specified by the recurrence relation.
2. Let a sequence fai gi2N be determined by the recurrence relation
an = 3an

1 + 2an 2

a0 = 1;

a1 = 2 :

( )

and the initial conditions


(

)

Calculate a4 recursively first. Then calculate a4 again iteratively.


Solution Recursively we use (*) repeatedly (topdown) to decrease the indices involved
until they all reach the initial ones. Hence
a4 = 3a3 + 2a2
used () for n = 4
= 3(3a2 + 2a1 ) + 2a2
used () for n = 3
= 11a2 + 6a1
= 11(3a1 + 2a0 ) + 6a1
= 39a1 + 22a0
= 39  2 + 22  1 = 100 .
used ()

21.2 Basic Concepts Related to Recurrence Relations

139

Iteratively we use (*) repeatedly (building-up) to derive more and more known elements
until the desired index is reached. Hence
a0 = 1 ,
a1 = 2 ,
a2 = 3a1 + 2a0 = 8 ,
a3 = 3a2 + 2a1 = 28 ,
used () for n = 3
a4 = 3a3 + 2a2 = 100 .
used () for n = 4
3. Let f (n) for n 2 N be given by the recurrence relation
f (n)

n f (n

f (0)

1); n 2 N ; n  1

(initial condition )

Find the solution f (n).


Solution We first derive
f (n)

=
=
=
=

n f (n 1)
n(n 1) f (n 2)
n(n 1)(n 2) f (n
n! f (0) = n! :

if n  1
if n  2
if n  3

3)

Then we can show inductively f (n) = n! for n  0.

21.2 Basic Concepts Related to Recurrence Relations


An mth order linear, constant coefficient recurrence relation on a sequence fan gn0 is a
recurrence relation which can be written in the form
cm an+m + cm

1 an+m 1 +

 + c1 an

+1

+ c0 an = g(n);

n0

)

where c1 ;  ; cm are constants, c0 cm 6= 0, and g(n) is a function of n. If furthermore g(n) = 0


for all n, then the relation is said to be homogeneous. We note that the recurrence relation
() is said to be linear because the dependent functions ak , i.e. an through an+m , appear in
() in the linear form k ck an+k where none of the coefficients ck are allowed to depend on
any dependent functions ak s. The term constant coefficient thus refers to the fact that all the
coefficients ck are in fact constant: they dont depend on the index n explicitly.
Examples
4. an = 5an 1 + 2an 2 + 3n ; n  2, is a 2nd order linear, constant coefficient recurrence
relation and is nonhomogeneous. This is because we can equivalently rewrite it as
ak+2

5ak+1

2ak = 3k+2 ;

k0:

In terms of the notation in () we have in this case m = 2 and


cm = c2 = 1;

cm

1 = c1 =

5;

cm

2 = c0 =

2;

g(n) = 3n+2 :

21 RECURRENCE RELATIONS

140

5. Bn+2 = sin(Bn+1 ), n  1, is not a linear recurrence relation because sin(Bn+1 ) is not a


linear function of the dependent function Bn+1 .
6. an+1 = nan , n  0, is not a constant coefficient recurrence relation, though it is linear and
homogeneous.
7. f (n + 1) = 3 f (n 5) + 4 f (n 2), n  5, is a homogeneous, 6th order, linear, constant
coefficient recurrence relation. Observe that the difference of the highest index (n + 1)
and the lowest index (n 5) is exactly the order 6 of the recurrence relation.
8. an+1 = ran with constant r and n  0 induces a geometric sequence fan gn0 with an =
ran 1 = r(ran 2 ) = r2 an 2 =  = rn a0 , i.e. an = rn a0 as its solution.
The characteristic equation of linear, constant coefficient recurrence relation
cm an+m + cm

1 cn+m 1 +

 + c1 an

+1

+ c0 an = g(n);

n0

with cm c0 6= 0 is the following polynomial equation


cm m + cm

m 1

 + c1 + c0 = 0

where , pronounced as lambda, is just an unknown variable.


Examples
9. Recurrence relation an+2 + 3an+1 + 2an = 0, n  0, has the characteristic equation 2 +
3 + 2 = 0.
10. Recurrence relation f (n + 1) = 3 f (n 2) + n2 + 5; n  2 has the characteristic equation
3 3 = 0 because the recurrence relation can be written via n = k + 2 as f (k + 3)
3 f (k) = (k + 2)2 + 5. In fact we have in this case cm  c3 = 1, c2 = 0, c1 = 0 and c0 = 3
in (), and thus the characteristic equation c3 3 + c2 2 + c1 + c0 = 0 becomes simply
3 3 = 0.
Note An alternative way to construct the characteristic equation: Since the highest index
in the recurrence relation is n + 1 while the lowest index is n 2, their difference (n +
1) (n 2) = 3 must be the order m, i.e. m = 3. Likewise the characteristic equation can
also be obtained in following way.
(i) Remove the nonhomogeneous terms g(n). This way the recurrence relation f (n +
1) = 3 f (n 2) + n2 + 5 becomes the reduced recurrence relation f (n + 1) = 3 f (n
2).
(ii) Find the lowest index L. Here we thus have L = n

2.

21.3 Simplest Case of General Solutions

141

(iii) For each term in the reduced recurrence relation, if its index is K then replace the
term by K L . For the term f (n + 1) we see K = n + 1 hence K L = (n + 1)
(n 2) = 3. Hence f (n + 1) is to be replaced by 3 . Likewise for the term f (n 2)
on the r.h.s. we see K = n 2 hence K L = 0, implying that the term f (n 2) is
to be replaced by 0 which is simply 1. This way the reduced recurrence relation is
finally changed into the characteristic equation 3 = 3.

21.3 Simplest Case of General Solutions


A solution an of a recurrence relation (  ) is said to be a general solution, typically containing some arbitrary constants in the solution expression for an , if any particular solution
of the recurrence relation () can be obtained as a special case of the general solution. For
example, it is easy to verify that an = A2n + B3n for arbitrary constants A and B solves the
recurrence relation an+2 5an+1 + 6an = 0. Likewise we can show that an = 5  2n is also
a (particular) solution. Obviously the particular solution an = 5  2n is included in the more
general solution expression an = A2n + B3n if we choose A = 5 and B = 0. In fact one can
show that all the solutions of an+2 5an+1 + 6an = 0 are embraced by the solution expression
an = A2n + B3n , which is hence the general solution. An alternative way to determine if a solution is the general solution of an m-th order linear, constant coefficient recurrence relation is
to see if the solution expression contains exactly m independent arbitrary constants. The word
independent here roughly means that none of the arbitrary constants can be made redundant.
From this perspective, we can also conclude that an = A2n + B3n is the general solution of the
2nd order recurrence relation an+2 5an+1 + 6an = 0 because the solution contains A and B as
the 2 independent arbitrary constants.
To better prepare ourselves for the rest of the topics, we recall that a polynomial f () has
0 as one of its roots means, precisely, f (0 ) = 0. For example, if f () = 2 5 + 6, then
0 = 3 is one of its roots because f (0 ) = f (3) = 32 5  3 + 6 = 0. In other words, 3 is a root
of the equation 2 5 + 6 = 0. In general a polynomial equation will have many roots, some
of which may be distinct while others may be repeated. Recall that a 2nd order polynomial
equation
a2 + b + c = 0 ;
a 6= 0
has 2 roots 1 and 2 given by
1 =

b + b2
2a

4ac

2 =

b2
2a

4ac

see also Preliminary Mathematics at the beginning of these notes. If 1 = 2 , then the 2 roots
are the repeated roots and 1 , which is the same as 2 , is a root of multiplicity 2.
For a polynomial equation f () = 0, a value 0 is said to be a root of multiplicity m if f ()
can be written as f () = ( 0 )m g() such that g() is again a polynomial with g(0 ) 6= 0.

21 RECURRENCE RELATIONS

142

For example, the polynomial equation 2 6 + 9 = 0 has a root 3 of multiplicity 2. This is


because 2 6 + 9 = ( 3)2  1, in which f () = 2 6 + 9, g() = 1, 0 = 3 and m = 2.
If we solve the equation 2 6 + 9 = 0 through the use of the above root formula for 1 and
2 , we see that both 1 and 2 are equal to the same value 3. This also explains why 3 is a root
of multiplicity 2 for the equation 2 6 + 9 = 0.
Note A polynomial equation f () = 0 has a root 0 , i.e. f (0 ) = 0, if and only if f () =
( 0 )g() for another nonzero polynomial g(). If 0 is furthermore a root of multiplicity
m > 1 of f () = 0, then 0 must be a root of multiplicity m 1 of g() = 0. A root of multiplicity
1 is called a simple root. A simple root is thus not a repeated root.
Theorem If the characteristic equation of an mth order homogeneous, linear, constant coefficient recurrence relation
cm an+m + cm

1 an+m 1 +

 + c1 an

+1

+ c0 an = 0;

cm c0 6= 0; n  0

has m distinct roots 1 ; 2 ;  ; m , then


an = A1 n1 + A2 n2 +  + Am nm
with arbitrary constants A1 ;  ; Am is the general solution of the recurrence relation.
m

Proof First we show an =

Aini is a solution. For this purpose we substitute the expression

i=1

for an into the recurrence relation and obtain


+

cm an+m

 + c1 an

+1 + c0 a0
n+m
n+1
cm (A1 1 + + Am nm+m ) + + c1 (A1 n1+1 + + Am m
)
n
n
+ c0 (A1 1 +
+ Am m )
A1 (cm n1+m + + c1 n1+1 + c0 n1 ) + A2 (cm n2+m + + c1 n2+1 + c0 n2 )
n+m
n+1
n
+
+ Am (cm m +
+ c1 m + c0 m )
m
Ai ni (cm m
+ c1 i + c0 ) = 0 :
i +
i=1


















Hence an =

Aini is a solution. Since the solution involves m arbitrary constants A1 


;

Am ,

i=1

it is in fact a general solution. Alternatively we can also argue that for any initial values of
a0 ;  ; am 1 , since the linear system of m equations
m

Aiki = ak

i=1

k = 0; 1;  ; m

has a (unique) solution for A1 ;  ; Am , the expression an =

Aini is indeed a general solution.

i=1

21.3 Simplest Case of General Solutions

143
m

Note If the roots 1 ;  ; m are not distinct, i.e. there are repeated roots, then an = Ai ni is
i=1

still a solution but is not a general solution. The general solution for such cases will be dealt
with in the next section.
Example
11. Find the general solution of
an+2

5an+1 + 6an = 0 ;

n0:

Give also the particular solution satisfying a0 = 0 and a1 = 1.


Solution Since the associated characteristic equation is
2

5 + 6 = 0

and has 2 distinct roots 1 = 2 and 2 = 3, i.e. 22 5  2 + 6 = 0 and 32 5  3 + 6 = 0,


the general solution for the recurrence relation, according to the theorem earlier on, is
an = A1 2n + A2 3n , n  0, where A1 and A2 are 2 arbitrary constants. To find the particular
solution, we need to determine A1 and A2 explicitly using the initial conditions a0 = 0
and a1 = 1. Hence we require
a 0 = A1 2 0 + A2 3 0 = 0
a 1 = A1 2 1 + A2 3 1 = 1

A1 + A2 = 0
2A1 + 3A2 = 1

i.e.

If we convert the first equation A1 + A2 = 0 into A2 = A1 and substitute it into the


2nd equation 2A1 + 3A2 = 1, we get 2A1 + 3( A1 ) = 1 and hence A1 = 1. Substitute
A1 = 1 back to the 1st equation A1 + A2 = 0, we get 1 + A2 = 0 which gives A2 = 1.
Thus the particular solution is an = 2n + 3n for n  0. For more examples on how to
solve a set of linear equations, see Preliminary Mathematics at the beginning of these
notes.
Note It is often a good practice to check your answers, even if just partially. To check
the above particular solution, we see
a0 =

20 + 30 = 0

a1 =

21 + 31 = 1

an+2

5an+1 + 6an

=
=

2n+2 + 3n+2 ) 5( 2n+1 + 3n+1 ) + 6( 2n + 3n )


2n (22 5  2 + 6) +3n (32 5  3 + 6) = 0 :
|

{z
k
0

i.e. all conditions are satisfied.

{z
k
0

144

22 SOLUTION OF LINEAR HOMOGENEOUS RECURRENCE RELATIONS

Supplementary Exercises
1. Find all the roots of (2 + 9 + 14)(2 + 7 + 10) = 0.
2. Suppose a sequence fan gn0 is determined by the recurrence relation 3an an+1 + an+2 = 1
and the initial conditions a0 = 1 and a1 = 2. Find a3 both iteratively and recursively.
3. Is the recurrence relation an+2 + nan+1 + 5an = 0 linear and homogeneous? Is it a constant
coefficient recurrence relation?
4. Find the general solution of the recurrence relation an+1

an

1 = 0.

22 Solution of Linear Homogeneous Recurrence Relations


22.1 General Solutions for Homogeneous Problems
If the characteristic equation associated with a given m-th order linear, constant coefficient,
homogeneous recurrence relation has some repeated roots, then the solution given by Ai ni
will not have m arbitrary constants. To see this, we assume for instance 1 = 2 , i.e. root 1 is
m

repeated. Then the solution an =

Aini = (A1 + A2 )n2 + A3n3 +  + Am nm has less than m

i=1

arbitrary constants because A1 + A2 comprises essentially only one arbitrary constant. To make
up for the missing ones, we introduce the following more general theorem.
Theorem Suppose the characteristic equation of the linear, constant coefficient recurrence
relation
cm an+m + cm 1 an+m 1 +  + c1 an+1 + c0 an = 0; cm c0 6= 0; n  0
has the following roots (all roots accounted for)
z

m1

}|

{ z

m2

}|

1 ;  ; 1 ; 2 ;  ; 2 ;



z
;

mk

}|

k ;  ; k

such that 1 ;  ; k are distinct, m1 ;  ; mk  1 and m1 +  + mk = m. Then the general solution
of the recurrence relation is
"
#
k

an =

i =1

 mi 1

Ai; j n ni

j=0

i.e.
an

A1;0 + A1;1 n +  + A1;m1


+
+

1n

m1 1

A2;0 + A2;1 n +  + A2;m2


Ak;0 + Ak;1 n +  + Ak;mk

 n

1
m2 1

n
) 2 +
1n

mk 1
nk
1n

where Ai; j for i = 1;  ; k and j = 0;  ; mi are m arbitrary constants.



22.1 General Solutions for Homogeneous Problems

145

Examples
1. Find a particular solution of
f (n + 2) + 4 f (n + 1) + 4 f (n) = 0;

n0

with initial conditions f (0) = 1 and f (1) = 2.


Solution For clarity we artificially split the solution procedure into 3 steps below.
(a) The associated characteristic equation is
2 + 4 + 4 = 0 ;
which has a repeated root 1 =
multiplicity are
1 ; 1

2. In other words, all of its roots counting the


(i.e.

m1 = 2 ; k = 1 ) :

(b) The general solution from the theorem in this lecture is thus
f (n) = (A1;0 + A1;1 n)n1 = (B0 + B1 n)( 2)n ;
where B0 and B1 , denoting A1;0 and A1;1 respectively, are arbitrary constants.
(c) Constants B0 and B1 are to be determined from the initial conditions
f (0) = (B0 + B1  0)( 2)0 = 1
f (1) = (B0 + B1  1)( 2)1 = 2

B0 = 1
2 (B 0 + B 1 ) = 2

They are thus B0 = 1 and B1 = 2. Hence the requested particular solution is


f (n) = (1

n0:

2n)( 2)n ;

2. Find the general solution of


an+3

3an+2 + 4an = 0 ;

n  0:

Solution The associated characteristic equation is


F ()

def

32 + 4 = 0 :

Although there are formulas to give explicit roots for a 3rd order polynomial in terms
of its coefficients, we are taking a shortcut here by guessing one root 1 = 1. We can
check that 1 = 1 is indeed a root by verifying F ( 1) = ( 1)3 3( 1)2 + 4 = 0. To
find the remaining roots, we first factorise F () by performing a long division

146

22 SOLUTION OF LINEAR HOMOGENEOUS RECURRENCE RELATIONS


2
+1

3
3

32
+ 2
42
42

+4
+4

0
4
4
4

+4
+4

0
which implies
3

32 + 4 = ( + 1)(2

2)2 :

4 + 4) = ( + 1)(

Therefore all the roots, counting the corresponding multiplicity, are


1; 2; 2;
i.e. 1 = 1, m1 = 1 and 2 = 2, m2 = 2. Hence the general solution reads (with A =
A1;0 ; B = A2;0 ; C = A2;1 )
an = A( 1)n + (B + Cn)2n ; n  0 :
3. Find the particular solution of
un+3 + 3un+2 + 3un+1 + un = 0 ; n  0
satisfying the initial conditions u0 = 1; u1 = 1 and u2 =

7.

Solution
(a) The associated characteristic equation
3 + 32 + 3 + 1  ( + 1)3 = 0
has roots 1 =

1 with multiplicity 3, i.e. m1 = 3.

(b) The general solution then reads for arbitrary constants A, B and C
n0:

un = (A + Bn + Cn2 )( 1)n ;

(c) To determine A; B and C through the use of the initial conditions, we set n in the
solution expression in (b) to 0, 1 and 2 respectively, and then observe
u0
u1
u2

gives
gives
gives

A
(A + B + C )( 1)
A + 2B + 4C

=
=
=

The solution of these 3 equations,


A = 1; B = 0; C =

2:

finally produces the required particular solution


un = (1

2n2 )( 1)n ;

n0:

1
1
7:

22.2 Ideas Behind the Theorem

147

22.2 Ideas Behind the Theorem


We now briefly show that the general solution given in the theorem indeed satisfies the recurrence relation. Let us first define a step operation by F (n) = F (n + 1) for any function F.
Let
def
f () = cm m +  + c1 + c0

and for any s 2 N

Ps () = f(b0 + b1 n +  + bs ns )n j bi 2 C ; i = 0; 1; : : : ; sg

Since the binomial expansion gives


)(ni n )

i n+1

(n + 1)

n+1

ni n
 
i i 1
i i
n +
n
1
2

 

 +

i
i

 

n+

i
i

then we have
(




)Ps()
)2Ps ()
..
.

)Ps

 Ps

)s+1 Ps

1 ( ) ;

 P0
 f0g

)sPs

Ps

Because i s are roots of f () with multiplicity mi , for any such given i there exists a polynomial
g() such that f () = ( i )mi g() and g(i ) 6= 0, g( j ) = 0, 8 j 6= i. Hence for any i with
1  i  k, we have
 mi 1

f ()

Ai j n

ni

j=0

f ()Pmi

1 (i )

g()(

i)mi Pmi


Thus

g()f0g

 f0g

 k  mi 1

f ()

i=1
k

i.e. an =

Ai j n j ni

j=0

 mi 1

i=1


=

k
}|

 mi 1

f ()

j =0

1 (i )

Ai; j n j ni

 ms 1

s=1
s6=i

k
}|

f ( )

As; j n j ns

=0;

j=0

Ai; j n j ni is a solution of the recurrence relation f ()an = 0. It is easy to see

j=0

that this solution expression for an has exactly m arbitrary constants Ai; j .

148

23 BASICS OF LINEAR NONHOMOGENEOUS RECURRENCE RELATIONS

Supplementary Exercises
1. Find all the roots, and their corresponding multiplicity, of the equation
3

42 + 5

2=0;

assuming we already know that = 1 is one of the roots.


2. Find the general solution of the recurrence relation an+2 + 14an+1 + 49an = 0.
3. Solve the recurrence relation an+1
a0 = 5 and a1 = 6.

6an + 9an

1=0

for n  1 with the initial conditions

23 Basics of Linear Nonhomogeneous Recurrence Relations


23.1 Connection between Homogeneous and Nonhomogeneous Problems
The solutions of linear nonhomogeneous recurrence relations are closely related to those of the
corresponding homogeneous equations, as can be seen from the following.
Theorem Consider the following linear constant coefficient recurrence relation
cm an+m +  + c1 an+1 + c0 an = g(n);

c0 cm 6= 0

( )

and its corresponding homogeneous form


cm an+m +  + c1 an+1 + c0 an = 0 :

)

(i) If vn and wn are two solutions of the nonhomogeneous equation () then
n = wn

vn ; n  0

is a solution of the homogeneous equation ().


(ii) If un is the general solution of the homogeneous equation (), and vn is any particular
solution of the nonhomogeneous equation (), then
an = un + vn ; n  0
is the general solution of the nonhomogeneous equation ().

23.2 Examples of the First Glance

149

Proof
(i) The result comes from the following observation
cm n+m +  + c1 n+1 + c0 n

ci n+i

i =0

g(n)

ciwn+i

i=0

g (n )

civn+i

i=0

0:

(ii) For an = un + vn , we have

g(n)

cm an+m +  + c1 an+1 + c0 an

cian+i

i =0

k
}|

k
}|

ciun+i + civn+i

i=0

i =0

g(n) ;

i.e. an satisfies the nonhomogeneous recurrence relation (). Since the general solution un
of the homogeneous problem has m arbitrary constants thus so is an = un + vn . Hence an
is the general solution of (). More precisely, for any solution wn of (), since (i) asserts
n = wn vn satisfies (), n will just be a special case of the general solution un of ().
Hence wn = n + vn is included in the solution an = un + vn . Therefore an = un + vn is the
general solution of the nonhomogeneous problem ().

23.2 Examples of the First Glance


In the following, well give a few simple examples for finding particular solutions for nonhomogeneous problems. The arguments will be intuitive and some underneath subtleties are thus
deliberately hidden at this stage to keep the main idea simple. Therefore the readers are urged
not to make simplistic conclusion or generalisation based on the possible incomplete arguments
in these examples, until you have read thoroughly the rest of the subject in the next section.
Examples
1. Find a particular solution of an+2

5an = 2  3n for n  0.

Solution As the r.h.s. is 2  3n , we try the special solution in the form of an = C3n , with
the constant C to be determined. The substitution of an = C3n into the recurrence relation
thus gives
C  3n+2 5  C  3n = 2  3n ;
| {z }
k
an+2

| {z }
k
an

1
1
i.e. 4C = 2 or C = . Hence an =  3n for n  0 is a particular solution.
2
2

23 BASICS OF LINEAR NONHOMOGENEOUS RECURRENCE RELATIONS

150

2 fn + 3 fn

2. Find a particular solution of fn+1

4 = 6n,

n  4.

Solution As the r.h.s. is 6n, we try the similar form


fn = An + B ;
with constants A and B to be determined. Hence fn be a solution requires
6n

fn+1

2 fn + 3 fn

(A(n + 1) + B)

2An + (2B

2(An + B) + 3(A(n

11A)

i.e.
2B

2A
11A

4 ) + B)

6
0

Therefore our particular solution is fn = 3n +

A
B

=
=

B
33=2

33
.
2

3. Find the particular solution of


an+3

7an+2 + 16an+1

12an = 4n n

with
a0 = 2 ;

a1 = 0 ;

a2 = 5 :

Solution We first find the general solution un for the corresponding homogeneous problem. Then we look for a particular solution vn for the nonhomogeneous problem without
concerning ourselves with the initial conditions. Once these two are done, we obtain
the general solution an = un + vn for the nonhomogeneous recurrence relation, and we
just need to use the initial conditions to determine the arbitrary constants in the general
solution an so as to derive the final particular solution.
(a) The associated characteristic equation
3

72 + 16

12 = 0

can be shown to admit the following roots


1 = 3;
2 = 2;

(m 1 = 1 ;

simple root)
(m2 = 2; double root) :

We can easily verify this by first guessing 1 = 3 is a root and then factorising
3 72 + 16 12 into ( 3)(2 4 + 4) to find the remaining roots. If you
are concerned that you might not be able to guess a first root like 1 in similar
circumstances, then you can leave your worry behind because we wont expect you
to make such a guess.

23.2 Examples of the First Glance

151

The general solutions for the corresponding homogeneous problem thus reads
n0:

un = A3n + (B + Cn)2n;
7an+2 + 16an+1

That is, un solves an+3

12an = 0.

(b) Since the r.h.s. of the nonhomogeneous recurrence relation is 4n  n, which fits into
the description of 4n  (1st order polynomial in n), well try a particular solution in
a similar form, i.e.
vn = 4n (Dn + E ) :
The substitution of vn into the original recurrence relation then gives
4n  n

vn+3

7vn+2 + 16vn+1

4n+3 (D(n + 3) + E )
+ 16

 4n

+1

12vn

7  4n+2(D(n + 2) + E )

12  4n(Dn + E ) ;

(D(n + 1) + E )

i.e.
n

64(Dn + 3D + E )

112(Dn + 2D + E )

+ 64(Dn + D + E )
=

12(Dn + E )

4Dn + 4E + 32D :

Hence we have
4D = 1 ;

4E + 32D = 0

D=

1
;
4

E=

and consequently

n
2) :
4
(c) The general solution for the nonhomogeneous problem is then given by an = un + vn ,
i.e.
n

an = 4n
2 + A3n + (B + Cn)2n ; n  0 :
4
(d) We now determine A, B, C by the initial conditions and the use of the solution
expression in (b)
vn = 4n (

Initial Conditions
a0
a1
a2

=
=
=

Induced Equations
A+B 2
3A + 2B + 2C 24
9A + 4B + 8C

2
0
5

Solutions

=
=
=

2
5
29

A
B
C

=
=

1
3

Finally the particular solution satisfying both the nonhomogeneous recurrence relations and the initial conditions is given by
an = 4n

n

n
+ 3 + (3n

1 )2 n ;

n0:

152

24 SOLUTION OF LINEAR NONHOMOGENEOUS RECURRENCE RELATIONS

Note
1. In all the examples in this lecture, it is easy to verify that the g(n) function in () is in the
form of
g(n) = n (k nk +  + 1 n + 0 ) ;

where is not a root of the associated characteristic equation. If this were not the case,
we would have to use different forms to try for the particular solutions. These will be the
topics of the next lecture.
2. If g(n) = n1 n + n2 (3n2 + 1), for instance, with 1 and 2 neither being a root of the
characteristic equation, then the particular solution should be tried in the form
vn = n1 (A1 n + A0 ) + n2 (B2 n2 + B1 n + B0 ) :
3. If g(n) = cos(n)  n, for another instance, then we can treat it as
g(n) =

(ein + e in )

n=

n
n

n1 +  n2
2
2

in which 1 = ei and 2 = e i . Alternatively, we could try the particular solution in the


form
vn = sin(n)(A1n + A0 ) + cos(n)(B1n + B0 ) :

Supplementary Exercises
1. Suppose both un and vn are some solutions of the recurrence relation an+15 2an+1 + an =
1, i.e. both un+15 2un+1 + un = 1 and vn+15 2vn+1 + vn = 1 are satisfied. Is un + vn
also a solution of the same nonhomogeneous recurrence relation?
2. Find the general solution of the recurrence relation an+2

9an+1 + 14an = 6n.

3. Find the particular solution of the recurrence relation an+2 9an+1 + 14an = 6n, satisfying
the initial conditions a0 = 16 and a1 = 31
6.

24 Solution of Linear Nonhomogeneous Recurrence Relations


24.1 A Case for Thought
We already mentioned that finding a particular solution for a nonhomogeneous problem can be
more involved than those exemplified in the previous lecture. Let us first highlight our point
with the following example.

24.2 Method of Undetermined Coefficients

153

Example
1. Solve an+2 + an+1

6an = 2n for n  0.

Solution First we observe that the homogeneous problem


un+2 + un+1

6un = 0

has the general solution un = A2n + B( 3)n for n  0 because the associated characteristic equation 2 + 6 = 0 has 2 distinct roots 1 = 2 and 2 = 3. Since the r.h.s. of
the nonhomogeneous recurrence relation is 2n , if we formally follow the strategy in the
previous lecture we would try vn = C2n for a particular solution. But there is a difficulty:
C2n fits into the format of un which is a solution of the homogeneous problem. In other
words it cant be a particular solution of the nonhomogeneous problem. This is really
because 2 happens to be one of the 2 roots 1 and 2 . However, we suspect that a particular solution would still have to have 2n as a factor, so we try vn = Cn2n. Substituting
it to vn+2 + vn+1 6vn = 2n , we obtain
C(n + 2)2n+2 + C(n + 1)2n+1

6Cn2n = 2n ;

1
i.e. 10C2n = 2n or C = 10
. Hence a particular solution is vn
solution of our nonhomogeneous recurrence relation is

an = A2n + B( 3)n +

n n
2 and the general
10

n n
2 ; n0:
10

In general, it is important that a correct form, often termed ansatz in physics, for a particular
solution is used before we fix up the unknown constants in the solution ansatz. The choice of
the form of a particular solution, covering the cases in this current lecture as well as the previous
one, can be summarized below.

24.2 Method of Undetermined Coefficients


Consider a linear, constant coefficient recurrence relation of the form
cm an+m +  + c1 an+1 + c0 an = g(n) ;

c0 cm 6= 0 ; n  0 :

( )

Suppose function g(n), the nonhomogeneous part of the recurrence relation, is of the following
form
g(n) = n (b0 + b1 n +  + bk nk ) ;
()
where k 2 N , ; b0 ;  ; bk are constants, and is a root of multiplicity M of the associated
characteristic equation
cm m +  + c1 + c0 = 0 :

154

24 SOLUTION OF LINEAR NONHOMOGENEOUS RECURRENCE RELATIONS

Then a particular solution vn of () should be sought in the form


h k

vn

Bi n i n n M

i=0

n B0 + B1 n +  + Bk

k
1n

+ Bk n

nM ;

)

where constants B0 ;  ; Bk are to be determined from the requirement that an = vn should satisfy
the recurrence relation (). Obviously the vn in (  ) is composed of two parts: one is the
n (B0 + B1 n +  + Bk nk ) which is of the same form of g(n) in (), the other is the nM which is a
necessary adjustment for the case when , appearing in g(n) in (), is also a root (of multiplicity
M) of the characteristic equation of the associated homogeneous recurrence relation.
Note

1. If is not a root of the characteristic equation then just choose M = 0, implying alternatively that is a root of 0 multiplicity.


M
M
1
j
2. We can also try vn = n kj+
A j n j . If we rewrite vn as n kj+
A j n j + n M
=M
j=0 A j n ,
=0
then the 1st part is essentially () while the 2nd part is just a solution of the homogeneous problem. It is however obvious that vn in () is simpler than vn .

3. We briefly hint why vn is chosen in the form of (). Let ; f () and Ps () be defined
in the same way as we did in the derivation hints of the theorem in the lecture just before
the previous one, and well also make use of some intermediate results there. Recall that
() can be written as f ()an = g(n) and () implies g(n) 2 Pk ().
(i) If f () 6= 0, then f ()Pk ()  Pk (). Hence if we try vn = (B0 + B1 n +  +
Bk nk )n 2 Pk (), then we can derive a set of exactly (k + 1) linear equations in
B0 ;  ; Bk , which can be used to determine these Bi s.

(ii) If is a root of f () = 0 with multiplicity M  1, then

 f0g f ()PM k ()  Pk ()
Hence if we try vn = nM (B0 +  + Bk nk )n 2 PM k (), well again have a set of
exactly (k + 1) linear equations as the coefficients of the terms n n n  n nk .
The (k + 1) constants B0  Bk can thus be determined from these linear equations.
f ()PM

1 ()

Examples
2. Find the general solution of f (n + 2)

6 f (n + 1) + 9 f (n) = 5  3n , n  0.

Solution Let f (n) = un + vn , with un being the general solution of the homogeneous
problem and vn a particular solution.

24.2 Method of Undetermined Coefficients

155

(a) Find un : The associated characteristic equation 2 6 + 9 = 0 has a repeated root


= 3 with multiplicity 2. Hence the general solution of the homogeneous problem
un+2

6un+1 + 9un = 0 ; n  0

is un = (A + Bn)3n.
(b) Find vn : Since the r.h.s. of the recurrence relation, the nonhomogeneous part, is
5  3n and 3 is a root of multiplicity 2 of the characteristic equation (i.e. = 3,
k = 0, M = 2), we try due to (  ) vn = B0 n  nM  Cn2 3n : we just need to
observe that C3n is of the form 5  3n and that the extra factor n 2 is due to = 3
being a double root of the characteristic equation. Thus
5  3n

Hence C =

vn+2

6vn+1 + 9vn

C(n + 2)23n+2

18C3n :

6C(n + 1)23n+1 + 9Cn2 3n

5
5
and vn = n2 3n . Therefore our general solution reads
18
18


f (n) = A + Bn +

5 2 n
n 3 ;
18

n0:

3. Find the particular solution of


an+4

5an+3 + 9an+2

7an+1 + 2an = 3 ;

satisfying the initial conditions a0 = 2 ; a1 =

1
2;

a2 =

n0

5 ; a3 =

31
2

Solution We first find the general solution un for the homogeneous problem. We then find
a particular solution vn for the nonhomogeneous problem without considering the initial
conditions. Then an = un + vn would be the general solution of the nonhomogeneous
problem. We finally make use of the initial conditions to determine the arbitrary constants
in the general solution so as to arrive at our required particular solution.
(a) Find un : Since the associated characteristic equation
4

53 + 92

7 + 2 = 0

has the sum of all the coefficients being zero, i.e. 1 5 + 9 7 + 2 = 0, it must
have a root = 1. After factorising out ( 1) via 4 53 + 92 7 + 2 = (
1)(3 42 + 5 2), the rest of the roots will come from 3 42 + 5 2 = 0.
Notice that 3 42 + 5 2 = 0 can again be factorised by a factor ( 1) because
1 4 + 5 2 = 0. This way we can derive in the end that the roots are
1 = 1 with multiplicity m1 = 3 ; and
2 = 2 with multiplicity m2 = 1 :

156

24 SOLUTION OF LINEAR NONHOMOGENEOUS RECURRENCE RELATIONS


Thus the general solutions for the homogeneous problem is
un = (A + Bn + Cn2 )1n + D2n ;
or simply un = A + Bn + Cn2 + D2n because 1n  1.
(b) Find vn : Notice that the nonhomogeneous part is a constant 3 which can be written
as 3  1n when cast into the form of (), and that 1 is in fact a root of multiplicity 3.
In other words, we have in () = 1, k = 0 and M = 3. Hence we try a particular
solution vn = En3  1n = En3 . The substitution of vn into the nonhomogeneous recurrence equations then gives, using a formula in the subsection Binomial Expansions
in the Preliminary Mathematics at the beginning of these notes,
3

=
=
=

5vn+3 + 9vn+2

vn+4

7vn+1 + 2vn

3
+ 9E (n + 2)
7E (n + 1)3 + 2En3
E (n3 + 3n2 4 + 3n 42 + 43 ) 5E (n3 + 3n2 3 + 3n 32 + 33 )
3
2
+ 9E (n + 3n
2 + 3n 22 + 23 ) 7E (n3 + 3n2 1 + 3n 12 + 13 ) + 2En3

E (n + 4)

6E

5E (n + 3)

i.e. E = 12 . Hence vn = n2 .
Note Should you find it very tedious to perform the expansions in the above, you
could just substitute, say, n = 0 into
3 = E (n + 4)3

5E (n + 3)3 + 9E (n + 2)3

7E (n + 1)3 + 2En3

to obtain readily 3 = E43 5E33 + 9E23 7E = 6E. Incidentally you dont have
to substitute n = 0; you can in fact substitute any value for n because the equation
is valid for all n. Obviously this alternative technique also applies even if there are
more than 1 unknowns in the equation; we just need to substitute sufficiently many
distinct values for n to collect enough equations to determine the unknowns. The
drawback of this technique is that you have to make sure that the form you have
proposed for vn is absolutely correct through the use of the proper theory, otherwise
an error in the form for vn will go undetected in this alternative approach.
(c) The general solution of the nonhomogeneous problem is thus
an = un + vn = A + Bn + Cn2 + D2n

n3
:
2

(d) We now ask the solution in (c) to comply with the initial conditions.
Initial Conditions
a0
a1
a2
a3

=
=
=
=

2
1=2
5
31=2

Induced Equations
A+D
A + B + C + 2D
A + 2B + 4C + 4D
A + 3B + 9C + 8D

=
=
=
=

Solutions
2
0
1
2

A
B
C
D

=
=
=

2
1
1

24.2 Method of Undetermined Coefficients

157

Hence our required particular solution takes the following final form
n3
2n ; n  0 :
an = 3 2n + n2
2
4. Find the general solution of an+1

an = n2n + 1 for n  0.

Solution
(a) The general solution for homogeneous problem is un = A because the only root of
the characteristic equation is 1 = 1.

(b) Since n2n + 1 = 2n  n + 1n is of the form n1 (b1 n + b0 ) + n2 c0 and 2 = 1 is a simple


root of the characteristic equation, we try the similar form vn = 2n (B + Cn) + Dn for
a particular solution. Substituting vn into the recurrence relation, we have
n2n + 1 = vn+1

vn

i.e.

2n+1 (B + C(n + 1)) + D(n + 1)

2n (Cn + B + 2C) + D ;

Dn

2n (C

2n(B + Cn)

1)n + (B + 2C)

+ (D

1) = 0 :

In order the above equation be identically 0 for all n  0, we require all its coefficients to be 0, i.e.
1 = 0;

C
Hence B =

B + 2C = 0 ;

1=0:

2, C = 1 and D = 1 and the particular solution vn = 2n (n

(c) The general solution is un + vn and thus reads


an = 2n (n

2) + n + A ;

2) + n.

n0:

5. Let m 2 N and S(n) = im for n 2 N . Convert the problem of finding S(n) to a problem
i=0

of solving a recurrence relation.


Solution We first observe
S(n + 1) = (n + 1)m +

im = S(n) + (n + 1)m

i =0

Since the general solution will contain just 1 arbitrary constant, 1 initial condition should
suffice. Hence S(n) is the solution of
S (n + 1 )

S (n )

(n + 1)

S(0)

8n 2 N

Note A similar procedure for solving linear, constant coefficient nonhomogeneous recurrence
relations can be found in the book by Stephen B Maurer and A Ralston, Discrete Algorithmic
Mathematics, Addison-Wesley, 1991.

158

24 SOLUTION OF LINEAR NONHOMOGENEOUS RECURRENCE RELATIONS

Example
6. (a) Find the general solution of the recurrence relation
n0:

5an+1 + 6an = 0;

an+2

(b) Find a particular solution of the recurrence relation given in (a) such that it satisfies
the initial conditions a0 = 2 and a1 = 3.
(c) Find the general solution for the recurrence relation
wn+2

n0:

4wn+1 + 4wn = 2n+2 ;

Solution
(a) The associated characteristic equation 2 5 + 6 = 0 has 2 (distinct) roots 1 = 2
and 2 = 3. Hence the general solution of the recurrence relation is
an = A2n + B3n ;

n0;

where A and B are arbitrary constants.


(b) From the general solution obtained in (a), the initial conditions give rise to the following 2 equations
a1 = A  2 + B  3 = 3 :

a0 = A + B = 2 ;

Solving the above 2 equations we obtain A = 3 and B =


solution satisfying the initial conditions is
an = 3  2n

3n ;

1. Hence the particular

n0:

(c) The associated characteristic equation 2 4 + 4 = ( 2)2 = 0 has a double root


1 = 2. Hence the general solution of the corresponding homogeneous problem
is un = 2n (A + Bn) for n  0, where A and B are arbitrary constants. Since the
nonhomogeneous term is 2n+2 = 4  2n , a particular solution vn will take the form
vn = Cn2 2n because 2 is a double root of the associated characteristic equation.
Hence, substituting vn into the nonhomogeneous recurrence relation vn+2 4vn+1 +
4vn = 2n+2 , we obtain
h

C 4(n + 2)2

4(n + 1)2  2 + 4n2

=4;

which then simplifies to just 2C = 1. Hence we have C = 12 and vn = n2 2n 1 . The


general solution wn of the nonhomogeneous recurrence relation is thus wn = un + vn ,
and hence takes the form
wn

=
=

2n (A + Bn) + n2  2n
h
n2 i
2n A + Bn +
;
2

n0:

24.2 Method of Undetermined Coefficients

159

Supplementary Exercises
1. Find the general solution of the recurrence relation an+2
2. Solve the recurrence relation an+2
a1 = 1.

9an+1 + 14an = 2n .

2an+1 + an = 2 with the initial conditions a0 = 1 and

160

A TUTORIALS: SORTING ALGORITHMS

A Tutorials: Sorting Algorithms


The topics here should be regarded as a summary of the sorting algorithms introduced in the
tutorials. While it is quite possible to understand the selected classical sorting algorithms from
this section alone, given that a reader has already mastered the principle of mathematical induction from either this unit or elsewhere, it is highly desirable that you read this section at the
same time as you read the corresponding tutorials.
For any list of items and a predefined ordering among them, a sorting algorithm will rearrange the list so that all items in the new list will be placed in the correct order. For the simplicity
of description and terminology, we assume without loss of generality that all sorting algorithms
in this section will sort lists of items to an increasing order. In other words, for any list of n
items, a1 ; a2 ; :::; an, a sorting algorithm will find a reordering, ak1 ; ak2 ; :::; akn , of these n items
such that ak1  ak2    akn . A sorting algorithm is well defined if for any list of n items,
the sorting algorithm will, in finitely many steps, either sort the list completely, or reduce the
sorting problem to sorting some shorter lists of less than n items. This latter part highlights the
strategy of divide and conquer. In fact how to perform those finitely many steps completely
determines the corresponding sorting algorithm.
The efficiency of a sorting algorithm is affected by many factors. Here for simplicity we assume that the number of comparisons performed during a sorting process will solely determine
the algorithm efficiency. In other words, sorting algorithms that require less number comparisons in terms of the worst cases or in terms of the average, will be generally deemed better
algorithms, if other factors such as system resource requirement and implementation cost are
not to be taken into account.

A.1 Selection Sort


The selection sort is a very simple sorting algorithm. For any list of n items, a1 ; a2 ; :::; an, the
selection sort will select the smallest item, say ak , from the list and then exchange a1 with ak
to reach an intermediate list ak ; a2 ; :::; ak 1; a1 ; ak+1 ; :::; an, where item ak is now moved to the
correct position. The total number of comparisons made in this pass is n 1. The original list
a1 ; :::; an will be completely sorted once we sort the shorter sublist a2 ; :::; ak 1; a1 ; ak+1 ; :::; an,
as the final result is simply the 1st item ak followed by the sorted shorter sublist. The sorting of
the shorter sublist is then done in exactly the same manner, resulting in the gradual completion
of the sorting process.
Let Sn denote the maximum number of comparisons needed to sort a list of n items with the
selection sort. Since n 1 comparisons are needed in the 1st pass, the pass that sorts a1 ; a2 ; :::; an
into ak ; a2 ; :::; ak 1; a1 ; ak+1 ; :::; an, we need Sn 1 comparisons to sort the remaining sublist
a2 ; :::; ak 1, a1 , ak+1 ; :::; an. Hence Sn = n 1 + Sn 1 because it takes exactly Sn comparisons for
the selection sort to sort any list of n items as the existing order in a list is irrelevant to the algo-

A.2 Bubble Sort

161

rithm. By induction this means Sn = (n 1) + (n


Since S1 = 0 we thus conclude Sn = 12 n(n 1).

2) + Sn

2=

 = (n

1) +  + 2 + 1 + S1 .

Example
1. Use the selection sort to sort the list 5; 4; 1; 2; 3; 6; 7.
Solution We start with the 1st item, 5, and set the current smallest item to 5 itself. Then
we compare 5 with the next item, 4. We then update the smallest item to 4 because
4 < 5, and compare it with the next item, 1. We repeat this process to all the items in
the list. After 6 comparisons we know 1 is the smallest and then exchange it with
item, 5, which is at the position the current pass started with. Hence the 1st pass
gives 1 ; 4; 5; 2; 3; 6; 7, and we then proceed to sort the sublist 4; 5; 2; 3; 6; 7. After 5
comparisons we select the smallest item 2 there and exchange it with the 1st item 4 of
the sublist. Hence the 2nd pass gives 1; 2 ; 5; 4; 3; 6; 7. We then repeat the process until
the list is completely sorted. All the intermediate steps are summarised in the following
table, where a boxed item indicates that the sublist up to, and including, the boxed item
has already been sorted.
steps

lists

extra comparisons

Hence the total number of comparisons made in the sorting is 6+5+4+3+2+1=21.

A.2

Bubble Sort

For a list n items, a1 ; a2 ; :::; an, the bubble sort will start from the 1st item and gradually move
towards the penultimate item. The sorting procedure is as follows. At each position, compare
the current item with its next neighbour. Exchange the 2 items if they are out of order, then move
on to the next item position. After all items up to the penultimate one have been processed this
way, a complete pass is done and the resulting list becomes ak1 ; ak2 ; :::; akn which is a reordering
of the original list a1 ; :::; an. The last item there, akn , will be the largest due to the algorithm,
and hence will be in the correct position itself at the cost of n 1 comparisons performed in the
pass. If no exchanges of items were made in the pass, then the original list a1 ; :::; an is already
in the correct order and the bubble sort will simply terminate. Otherwise, the sorting problem

A TUTORIALS: SORTING ALGORITHMS

162

is reduced to sorting the sublist of the 1st n 1 items ak1 ; :::; akn 1 , which will be sorted again
by the bubble sort. If we denote by Bn the maximum number of comparisons needed to sort a
list of n items, then Bn  n 1 + Bn 1 . Hence by induction we can show that Bn  12 n(n 1).
If a list of n items is in the reverse order, then the bubble sort in fact needs exactly 21 n(n 1)
comparisons to sort the list. Hence we conclude Bn = 12 n(n 1). We note that the bubble sort is
done entirely through item exchanges which move smaller items ahead, resembling the process
of bubbling up the lighter items.
Example
2. Use the bubble sort to sort the list 5; 4; 1; 2; 3; 6; 7.
Solution We first compare the 1st item 5 with its next item 4. Since they are out of order,
we exchange their positions. We then move to the 2nd item which is now 5 due to the
previous item exchange, and compare it with its next item, 1. They are out of order again,
so we again exchange their positions by moving 5 to the 3rd position and moving 1 to the
2nd position of the list. We repeat this process up to the penultimate position of the list
to complete the 1st pass, giving rise to an intermediate list 4; 1; 2; 3; 5; 6; 7 at the cost of
6 comparisons and 4 item exchanges. We repeat such passes until a pass is made without
having to make any exchanges, implying the list has been completely sorted. All the
intermediate steps are summarised in the following table, where a boxed item indicates
that the sublist after, and including, the boxed item has already been sorted.
steps

lists

extra comparisons exchanges

4 1

1 2

2 3

2 3

Hence the total number of comparisons made is 6+5+4=15.

A.3 Insertion Sort


The algorithm of insertion sort initially sets the resulting list to an empty list, then takes items
from the list to be sorted and insert them to the resulting list one by one in such a way that the
resulting list remains in the correct order. For a list of n items, a1 ; a2 ; :::; an, we first rewrite it
as a1 ; a2 ; :::; an to indicate that the boxed item is the next to be processed for insertion and that
the current resulting sublist on the left of the boxed item is empty. Since the resulting list is
initially empty, the processing of the 1st item a1 is to insert a1 to the resulting sublist and move
the box to the next item. The list after this 1st pass then becomes a1 ; a2 ; a3 ; :::; an. If, typically
after some sorting passes, the current list becomes
ak1 ; :::; akm ; am+1 ; am+2 ; :::; an ;

()

A.3 Insertion Sort

163

in which the sublist ak1 ; :::; akm in front of the boxed item is sorted from a1 ; :::; am, and sublist
am+1 ; :::; an after and including the boxed item is yet to be processed for insertions. The insertion
of the next item, the boxed item am+1 , is done as follows. We compare am+1 first with akm , the
largest in the sorted resulting sublist. If am+1 is larger than akm then just insert am+1 after akm .
Then the current pass is done and we move the box to the next item. If am+1 is smaller than akm
then we compare am+1 with the item just before akm , i.e. akm 1 . The insertion (or not) of am+1
right after akm 1 is determined similarly. If the insertion of am+1 cant be made after comparing
am+1 with akm up to ak1 because am+1 is smaller than all the items in akm ; :::; ak1 , then just insert
am+1 right in front of the 1st item ak1 . The resulting list, after the insertion of am+1 , will then
become
a j1 ; :::; a jm+1 ; am+2 ; am+3 ; :::; an ;
()
where a j1 ; :::; a jm+1 is thus the sorted from a1 ; :::; am+1. The insertion procedure continues until
the box has moved past the last item of the list.
Let In denote the maximum number of comparisons needed to sort a list of n items with the
insertion sort. Since a maximum of m comparisons may have to be made in the pass from ()
to (), we have In  1 + 2 +  + (n 1) = 12 n(n 1). In fact a list of n items that are in the
reverse order will take exactly 12 n(n 1) comparisons to complete the sorting if it is to be done
with the insertion sort. Hence we have In = 12 n(n 1).
Example
3. Use the insertion sort to sort the list 5; 4; 1; 2; 3; 6; 7.
Solution The intermediate steps are summarised in the table below, where a boxed item
indicates that the sublist strictly before the boxed item has already been sorted.
steps

lists

extra comparisons

The boxed item itself is yet to be inserted. Notice that step 1 is trivial because the item
5 just needs to be added to the empty resulting sublist on the left of the boxed item. In
step 2, the single comparison of 5 with 4 is enough for us to insert 4 in front of 5. Steps 3
and 4 are likewise. In the 5th step, or rather in getting the results listed in step 5, we first

A TUTORIALS: SORTING ALGORITHMS

164

compare 3 with 5, we see 3 is smaller than 5. This means we have to compare 3 with the
item 4 which is just ahead of 5. Since 4 is again larger than 3, we have to compare 3 with
the item 2 which is immediately ahead of 4. Since this time 3 is smaller than the item
2, we know 3 has to be inserted right between 2 and 4, thus completing the step 5. We
note that we dont have to compare 3 with 1 in this pass. The rest of the steps are similar.
Hence the total number of comparisons made during the sorting is 1+2+3+3+1+1=11.

A.4 Merge Sort


If we have to 2 sorted lists b1 ; b2 ; :::; bm and c1 ; c2 ; :::; cn, then we may merge them into a single
sorted list as follows. We first set an empty resulting list. We compare the 1st items of the 2
lists, b1 and c1 in this case, and move the smaller one to the end of the resulting list. Thus one
of the 2 lists being merged will have 1 item less. We repeat this procedure until one of the 2 lists
being merged has become empty. When this happens, we just append the other remaining list
to the end of the resulting list. It is easy to see that the merge of the 2 lists requires a minimum
of minfm; ng comparisons and a maximum of m + n 1 comparisons.
When sorting a list of n items, a1 ; :::; an, the merge sort will halve the list into 2 sublists
a1 ; :::; as and as+1 ; :::; an, where s = b n+2 1 is the middlemost position, and then sort these sublists separately with the merge sort again. Once the 2 sublists are sorted, we can then merge
them into a single list according to the method we described in the previous paragraph. The
sorting of the 2 sublists is done similarly. For any integer n  1, we can find an integer k such
that 2k  n < 2k+1 . In fact this k is k = blog2 n . For any list of n items, a1 ; :::; an, the merge
sort will recursively halve the list and sublists until no sublists contain more than 1 items. The
recursion depth is thus bounded by k + 1, see the table below
depths
lists
0
a1 ; a2 ; :::; an
1
a1 ; :::; as1
as1 +1 ; :::; an
2
a1 ; :::; at1 at1 +1 ; :::; at2 at2 +1 ; :::; at3 at3 +1 ; :::; an
..
..
.
.
k+1

j j

:: :::: :::

j j

j j

:: :::: ::

j j

:: :::: ::

:: :::: ::

number of sublists
1
2
 22
..
.

 2k

+1

Since at any depth m there are 2m sublists each having less than or equal to 2k+1 m items, the
merging of the sublists at depth m to those at depth m 1 requires at most 2m 1  (2  2k+1 m
1) comparisons. Let Mn denote the maximum number of comparisons needed to sort a list of n
items with the merge sort. Then we have from the above analysis
k+1 h

Mn

k+1

 2m 1  (2  2k 1 m 1)  2k 1 = (k + 1)2k
m 1
m 1
b
log n 1
= (blog2 n + 1)2
 2(log2 n + 1)2log n
+

+1

A.5 Quick Sort

165
=

2(log2 n + 1)n

if n  2

4n log2 n :

Hence Mn  4n log2 n for n  1 because it obviously holds for n = 1 too.


Example
4. Use the merge sort to sort the list 5; 4; 1; 2; 3; 6; 7.
Solution We first keep halving the lists until no sublists contain more than 1 items, see the
list at the end of step 0 in the table below. Since the merging is actually done recursively,
we first merge 5 with 4, then 1 with 2, then 3 with 6, then 7 with nothing. The resulting
intermediate sublists are given in step 1. We then merge 4,5 with 1,2 and 3,6 with
7. The resulting lists are given in step 2. Finally we merge the sublists 1,2,4,5 and
3,6,7 to obtain the final sorted list.
steps

lists

extra comparisons

6 7

just recursively

4 1

6 7

halve the lists

4 1

6 7

5 1

6 7

2 4

6 7

6 7

Hence the total number of comparisons made is 3+4+5=12.

A.5

Quick Sort

The quick sort is one of the most efficient sorting algorithms. Although its worst case sorting
complexity in terms of the number of comparisons is still O (n2 ), where n is the number of items
in the list to be sorted, on the average, the complexity for the quick sort is just O (n log2 n).
For a list of n items, a1 ; a2 ; :::; an, the quick sort will choose the an arbitrary item, say am , in
the list as the pivot and compare it with all the rest of the items. For each item in the list, if it is
smaller than the pivot item then it will moved ahead of the pivot item. Otherwise it will be left
where it is. After the completion of the 1st such pass, the list becomes
a j1 ; :::; a js ; am ; ak1 ; :::; akt
where all items inside the left box will be smaller than am , and all those inside the right box will
be not smaller than am . This means that item am will be in the correct position, marked therefore

A TUTORIALS: SORTING ALGORITHMS

166

by a star *, after the 1st pass. Incidentally the relative ordering of the sublists inside the boxes
remain the same as they were in the original list a1 ; :::; an. The quick sort then proceeds to sort
the 2 sublists inside the boxes in the same way. We note that although a pivot can be any item
in the list, we will for simplicity always choose the 1st item as the pivot in this unit.
Suppose a list of n items, a1 ; a2; :::; an, are in the reverse order. If we apply the quick sort
to the list using always the 1st item as the pivot, then after the 1st pass at the cost of n 1
comparisons we obtain the new list a1 ; a2 ; a3 ; :::; an in which a1 is now known to be in the
correct position. We then sort the still reversely ordered sublist a2 ; :::; an in the same way.
Hence a total of (n 1) + (n 2) +  + 2 + 1 = 12 n(n 1) comparisons are needed in sorting
a reversely ordered list of n items. Let Qn denote the maximum number of comparisons needed
to sort a list of n items with the quick sort. We have just shown Qn  12 n(n 1). We now prove
by strong induction that Qn  12 n(n 1). First we observe Q0 = Q1 = 0 because there is no
need to sort any lists of 1 item or less. Suppose after the 1st pass by the quick sort, the original
list a1 ; :::; an is halved into 2 sublists one of which having k  0 items. Then according to the
induction assumption we have Qn  n 1 + Qk + Qn k 1 and hence
Q n  (n

1) +

k (k

1)
2

(n

1)(n
2

2)

n (n

1)
2

k (n

1) 

n (n

1)
2

Thus we conclude Qn = 12 n(n 1). It is obvious that this result will in fact hold no matter
how the pivots are to be selected. However, if we use Qn to denote the average number of
comparisons needed to sort a list of n items with the quick sort, then it can be shown with
some tedious efforts that Qn  (2 ln 2) n log2 n, see the supplementary exercises at the end of
this section. For more details, interested readers may consult the book Data Structures and
Algorithm Analysis in C by Mark A Weiss, Benjamin/Cummings, 1993.
Examples
5. Use the quick sort, with always the 1st item as a pivot, to sort the list 5; 4; 1; 2; 3; 6; 7.
Solution We first underline the 1st item, 5, as the pivot, and compare it with all the rest
of the items in the list. After 6 such comparisons, we are able to separate the original list
into 2 parts. The 1st part contains all the items that are smaller than the pivot, and the 2nd
contains all the items that are larger than the pivot. The pivot item 5 itself will be moved
to separate these 2 sublists, and will be marked by a star * because it is already in the
correct position of the eventual sorted list, see the table below. We now underline the 1st
items, 4 and 6 respectively, as the pivot items of the 2 newly created sublists 4,1,2,3 and
6,7. The same quick sort algorithm is then applied to these 2 sublists separately. For
the sublist 6,7, we compare 6 with 7. We see no movement of items is needed in this
case and item 6 will then be marked by a *. Since the new sublist containing just 7
is a list of a single item, hence it is trivially in the correct order. Hence no sorting needs to
be performed on this single item sublist and the item 7 there will be duly marked by a
star * as well. We repeat the quick sort procedure to all the remaining unsorted sublists
until all of them are sorted, i.e., all items are marked by a *.

A.5 Quick Sort

167
steps

lists

2
3
4

extra comparisons
3

5

1 2 3 4 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7

4
2
1

The total number of comparisons made in this case is 6+4+2+1=13.


6. Use the quick sort algorithm to sort alphabetically the following list
E ; C ; A ; D; B ; F

Use the first element as a pivot, underline the pivot elements and use an asterisk (*) to
mark the elements in their final positions. How many comparisons are needed in this
case?
Solution The sorting can be done in the following 3 main passes
comparisons
5
E
C
C
A
A
D
D
B
B
E
F
F
original list
!

3
A
B
C
D
E
F
new list

1
A
B
C
D
E
F

The total number of comparisons is thus 5+3+1=9.

Supplementary Exercises
1. For a list of n = 2k items, how many comparisons are needed in the best cases for the
selection sort, the bubble sort, the insertion sort and the merge sort?

2. Explain why the fixed choice of the 1st item as the pivot, while simple, is not really a
good idea for the quick sort. Can you suggest some remedies to overcome the potential
embarrassment on sorting an already sorted list?
3. Let T (n) denote the average number of comparisons needed to sort a list of n items with
the quick sort. Show that T (n)  Cn log2 n, with C = 2= log2 e, holds for all n  1 and
that the factor C = 2= log2 e = 2 ln 2  1:386 is the smallest such a value.

168

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

Hints and Solutions to Supplementary Exercises

x1

1. A  B = f (a; x); (5; x); (6; x); (a; ?); (5; ?); (6; ?) g, P (B) = f?; fxg; f?g; fx; ?gg.
2. From the Venn diagram below we see that (A
holds if and only if A \ B \ C = ?.
A

}
C

B ) [ (B

C) [ (C

A) = A [ B [ C

This area is A \ B \ C.

3. Let U be the universal set. We first show (A \ B)0  A0 [ B0 . For any x 2 (A \ B)0 ,
we have x 2 U (A \ B), i.e. x 2 U but x 2
= A \ B. If x 2
= A then x 2 U
A = A0 
A0 [ B0 . If (otherwise) x 2 A then we must have x 2
= B because x 2
= A \ B. Thus
0
0
0
0
0
x 2 U B = B  A [ B . Hence we have shown x 2 A [ B in both cases, implying
(A \ B)0  A0 [ B0 . Now we show A0 [ B0  (A \ B)0 . For any y 2 A0 [ B0 , if y 2 A0
0
0
0
then y 2
= A, then y 2
= (A [ B) and then y 2 (A [ B) . If (otherwise) y 2
= A then y 2 B
because y 2 A0 [ B0 . Thus we have likewise y 2 (A [ B)0 . Hence y 2 (A [ B)0 in both
cases, implying A0 [ B0  (A \ B)0. From (A \ B)0  A0 [ B0 and A0 [ B0  (A \ B)0
we then conclude (A \ B)0 = A0 [ B0 .

x2

1. Let Sn denote the statement 20 +  + 2n = 2n+1 1. Since for n = 1 the statement
S1 is 20 + 21 = 22 1 which is obviously true, we know S1 is true. For the inductive
step, we assume Sk is true for some k  1 and proceed to show that this will ensure
Sk+1 is also true. Since
20 +  + 2k + 2k+1 = (20 +  + 2k ) + 2k+1 = (2k+1 1) + 2k+1 = 2k+1 (1 + 1) 1 = 2k+2 1 ;
where we have made use of the identity 20 +  + 2k = 2k+1 1 implied by the
induction assumption Sk , we see that Sk+1 is true whenever Sk is true. Hence, from
the P.M.I., we conclude that Sn is true for all integers n  1.
2. Let Sn denote the statement 3n > 4n. Obviously S2 is true because the statement in
this case is just 32 > 4  2. Assume that Sk is true for some k  2. Then 3k > 4k,
and thus
3k+1 = 3k  3 > 4k  3 = 4(k + 1) + (8k

4 ) > 4 (k + 1 )

because 8k 4 > 0 due to k  2. Hence we have shown 3k+1 > 4(k + 1), i.e. Sk+1
is true whenever Sk is true. The P.M.I. then implies Sn is true for all n  2.

Hints and Solutions to Supplementary Exercises

169

3. Let Sn denote the statement an = 2n+1 1. First we observe easily that S0 is true
because a0 = 20+1 1 = 1 is exactly the same value given by the question. Assume
Sk is true for some k  0, thus implying ak = 2k+1 1. Then from the recurrence
relation we have
ak+1 = 2ak + 1 = 2(2k+1

1) + 1 = 2k+2

2 + 1 = 2(k+1)+1

1 for all integers n  0.

and hence Sk+1 is true. The P.M.I. then implies an = 2n+1

x3

1;

1. 3 multiplications are needed because 2x3 + 5x2 + 3x + 1 = x  (x  (x  2 + 5) + 3) +


1.
2. Since j2n3

9n + 1j  12jn3j holds for n  1 because

j2n3

9n + 1j

 j2n3j + j9nj + j1j


 2n3 + 9n  n2 + n3
 12jn3j

(generalised triangle inequality)


(assume n  1)

we conclude from the definition of big O that 2n3

3. Let n  2. Then jn
Hence

1j  n

1n

n
n
2 = 2





n+1
n+1


p
= p

n 1 + 1
n 1 +1

In other words we have shown that n+1

Hence

pjnn

+1

1j+1

and

9n + 1 = O (n3 ).

jn

1j + 1 

jn

1j 

pn

2.

p
 np+n1  np+nn = 2 2pn
j
2
2

p
pjn 1j 1  (2 2)  jpnj holds for all n  2.

pn).

= O(

4. Since f (x) = O (g(x)) implies the existence of a constant C > 0 and a constant L
such that j f (x)j  Cjg(x)j holds for all x  L. Let M be the maximum value of
C and L, i.e. M = maxfC; Lg. Then for x  M we have x  L and thus j f (x)j 
Cjg(x)j  M jg(x)j  jx  g(x)j.

x4

1. Upper bound.

j2n3 + 9 log n + 1j




2n3 + 9 log n + 1
2n

2n

+ 9n + 1
+ 9n

n

+n

(assume n  1)

(use log2 n  n)
3

12jn3 j

Lower bound.

j2n3 + 9 log n + 1j

2n3 + 9 log n + 1
2n

2jn

(assume n  1)
:

Hence we have 2jn3 j  j2n3 + 9 log n + 1j  12jn3 j for all n  1, implying 2n3 +
9 log n + 1 = (n3).

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

170
2. Upper bound.





n
+
sin
n


p

=
n + 1

jj

n + sin n
(assume n  2, observe j sin nj  1)
jnj + 1
n+n
pn  2pn
:
p

Lower bound.




n
+
sin
n


p

=
n + 1

jj



=

Hence we have

n + sin n
(assume n  2, observe j sin nj  1)
jnj + 1
pn +n +sinpnn (pn  1 because n  2)
n n2
n 1
p
p

( n2  1 because n  2)
2 n
2 n
1p
n
:
4
p



p
1p
n + sin n
j
nj  p
 2j nj
jnj + 1
4

n + sin n
for all n  2, implying p
jnj + 1

pn).

= (

3. If otherwise, i.e. n f (n) = O (g(n)), then there would exist a constant C > 0 and
another constant M such that jn f (n)j  Cjg(n)j whenever n  M. Since f (n) =
(g(n)) implies the existence of a constant D > 0 and a constant N such that
Djg(n)j  j f (n)j whenever n  N, we would have
Djg(n)j  j f (n)j 

C
jg(n)j;
n

hence,

C
jg(n)j  0 ;
n

for all n > L = maxfM ; N ; C=D; 1g. Due to D

C=n > 0 we would thus have to


C
have jg(n)j  0 which is impossible because jg(n)j   j f (n)j > 0. Hence our
n
initial assumption n f (n) = O (g(n)) must have been incorrect. In other words we
have shown n f (n) 6= O (g(n)).

x5
x6

l3

xm

= 2,

l3

xm

= 1.
2
3
2. 4 comparisons. The comparisons are made with E, H, F and G sequentially.

1.

1. (a) and (b) are shown in the 1st truth table below, while (c) and (d) are shown in the
2nd truth table.

Hints and Solutions to Supplementary Exercises

p
T
T
F
F

q
T
F
T
F

p
F
F
T
T

q
F
T
F
T

p_q
T
T
T
F

(

p) _ q
T
F
T
T

171

( p _ q) ^ ((

p) _ q)

T
F
T
F

{z

p!q
T
F
T
T

same as the column for q

p
T
T
F
F

q
T
F
T
F

p
F
F
T
T

q
F
T
F
T

p_q
T
T
T
F

(

p) ^ ( q)
F
F
F
T

( p _ q) _ ((

( q)

{z

identical columns

p) ^ ( q))

( p _ q) ^ ((

T
T
T
T

! ( p)
T
F
T
T

p) ^ ( q))

F
F
F
F

{z

all T : tautology

{z
all F: contradiction

2. The truth table below shows ( p ^ q) _ r and p ^ (q _ r) are not logically equivalent
because their corresponding columns in the truth table are not exactly the same.
p
T
T
T
T
F
F
F
F

q
T
T
F
F
T
T
F
F

r
T
F
T
F
T
F
T
F

p^q q_r
T
T
T
T
F
T
F
F
F
T
F
T
F
T
F
F

(p

^ q) _ r

p ^ (q _ r)
T
T
T
F
F
F
F
F

T
T
T
F
T
F
T
F
|

{z

columns not exactly the same

x7

1. p ! q doesnt imply q ! p, i.e. ( p ! q; ) q ! p) is not valid, because the argument


form fails at 1 critical row shaded in the truth table below.
p
T
T
F
F

q
T
F
T
F

p!q q! p
T
T
F
T
T
F
T
T

2. We observe from the truth table below that there are no critical rows at all. Hence
the argument form is valid.

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

172

p
T
T
T
T
F
F
F
F

q
T
T
F
F
T
T
F
F

r
T
F
T
F
T
F
T
F

 p  q  p^  q
F
F
F
F
T
T
T
T

F
F
T
T
F
F
T
T

F
F
F
F
F
F
T
T

r
T
F
T
F
T
F
T
F

3. The derivation is as follows:


(1) r; p ! r;
(2)
(3)
(4)
(5)

) p (modus tollens)
 p p_  q ) q (disjunctive syllogism)
r  q ) r^  q
(conjunctive addition)
 r _ q _ s ) (r^  q) _ s (De Morgans laws)
r^  q  (r^  q) _ s ) s
(disjunctive syllogism)
;

Hence the argument form is valid.


4. If ( p1 ; :::; pn; ) q) is valid, then q is true whenever p1 ; :::; pn are all true, i.e. whenever p1 ^  ^ pn is true. Hence  q is false whenever p1 ^ ^ pn is true. That
is, p1 ^  ^ pn ^  q is false whenever p1 ^  ^ pn is true. However, since p1 ^
 pn ^  q is also false if p1 ^  ^ pn is false, hence p1 ^  ^ pn ^  q is always false. In other words p1 ^  ^ pn ^  q is a contradiction. Conversely, if
p1 ^^ pn ^  q is a contradiction, then ( p1 ^^ pn )^  q is always false. Hence
if p1 ^^ pn is true, then  q must be false, i.e. q must be true. This means exactly
that the argument form ( p1 ; :::; pn; ) q) is valid.

x8

1.
2.

8x 2 D 8y 2 D L(x y) $ L(y x).


Due to p ! q  p _ q for any statements p and q we observe that the negation is
;

 (8x 2 R  (x
;

>

3) _ (x2 > 4))

 9x 2 R  (  (x 3) _ (x2
 9x 2 R (x 3)^  (x2 4)
 9x 2 R (x 3) ^ (x2  4)
;

>

>

>

>

4))

>

i.e., there exists an x 2 R such that x > 3 and x2  4. This negated statement is
obviously not true because any real value x > 3 will not satisfy the condition x2  4
simultaneously.
3. A sufficient condition for a student to pass this unit is that he or she does all the
assignments. But this condition is not a necessary condition. The original statement
has not specified any necessary conditions.

Hints and Solutions to Supplementary Exercises

x9

173

1. There are essentially 3 different such graphs. They are


r
r

G1

G3

G2

2. Kn;2n has 2n2 edges, and the length of a longest walk in Kn;2n from one vertex to
another, without walking on any edge and any vertex more than once, is 2n.

x10

1. K4 is Eulerian, but K2;3 is not. This is because all vertices of K4 , but not of K2;3 , are
of even degrees. However K2;3 does have an Eulerian path because K2;3 has exactly
2 vertices of odd degrees.
2. One Eulerian circuit in K2;4 is drawn below, in which the arrows indicate the walking
direction and the numbers on the edges denote the walking order.

3K
3 6

r


4 5
U+

7
r

3. K2;3 doesnt contain any Hamiltonian circuits, but does contain a Hamiltonian path,
see the arrowed path below.

?
?

x11

1. No, because K4 doesnt have a vertex of degree 5 while K5 does.


2. Yes, K5 has a subgraph that is isomorphic to K4 . Pick any vertex from K5 , say v in
the diagram below. If we remove that vertex and all the edges incident upon that
vertex, then the remaining subgraph is isomorphic to K4 .
r

v
r
r

3. The graphs G1 and G2 represented by A1 and A2 respectively are drawn as


v1 r
w1 r
v2

v3
r

G1

v4

w4

w3
r

G2

w2
,

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

174

from which we see that the 2 graphs are isomorphic and the vertex correspondence
of the isomorphism is
 
v1
v2
v3
v4



-w1
*w2
-w3
jw4

 .

The total number of walks of length 2 in G1 is the sum of all the elements of A21 .
Since
2

A21 =

6
6
6
4

0
1
0
1

1
0
1
1

0
1
0
1

1
1
1
1

7
7
7
5

6
6
6
4

0
1
0
1

1
0
1
1

0
1
0
1

1
1
1
1

7 6
7 6
7=6
5 4

2
1
2
2

1
3
1
3

2
1
2
2

2
3
2
4

3
7
7
7
5

the total number of walks of length 2 is 33.


4. The isomorphism is equivalent to following correspondence of the vertices
r
r v2
v5 r
v1
v3
rv1
r
r
v2 r v6
v5
r
v
r
r 4
r v4
v3 r
v6
.
G1
G2

x12

1. Yes, it is possible to have a forest of n + 2 vertices and n edges. In fact such a forest
must have exactly 2 connected components, each of which is a tree.
2. The strategy is to start with a vertex of degree 1, then gradually put back vertices and
edges one by one logically and exhausively. Remove the trees that are duplicated
under graph isomorphisms. There are 3 nonisomorphic trees of 5 vertices, and they
are
r
r
r
r

r
r

G1

G3

G2

3. The binary tree constructed during the sorting is


B
I
A
N
F
E
L
R
O
U
S
T

Y
.

The sorted list, read off the binary tree, is thus A; B; E ; F; I ; L; N ; O; R; S; T ; U ; Y .

Hints and Solutions to Supplementary Exercises

175

4. Preorder traversal: A; B; D; F; I ; K ; C; E ; G; H ; J; L; M.

Inoder traversal: B; F; K ; I ; D; A; C; G; E ; L; J; M ; H.
Postoder traversal: K ; I ; F; D; B; G; L; M ; J; H ; E ; C; A.

x13

1. A typical spanning tree for K2;n will look like


r



k vertices

  

1 vertices

where the number of vertices in the 2 dashed boxes should sum up to n 1. The
dashed box with smaller number of vertices will be allowed to contain 0 up to b n 2 1
vertices. Hence the total number of nonisomorphic spanning trees is b n+2 1 .
2. One of the minimal spanning trees is
B
A

5
r

F 6
r

3
r

The total weight of the minimal spanning tree is 21.


3. If a connected graph G is not a tree, then there exists at least 1 edge (taken from a
nontrivial circuit of graph G ), denoted by e, such that the removal of edge e will
leave the remaining graph, denoted by G0 , still connected. If we apply Fleurys
algorithm to G0 , we get a spanning tree (not containing edge e) of G0 , which is also
a spanning tree of G. If we order all edges of G in such a way that edge e is the very
1st edge and then apply Fleurys algorithm again, we then obtain a spanning tree
that will contain edge e. Hence we have at least 2 different spanning trees for G.
4. No. A graph with distinct weight on each edge has exactly 1 minimal spanning tree.
This is because there is just 1 way of listing all the edges in the order of increasing
weight.

x14

1. 367

= 2710 = 110112 .

2. Since 16 = 42 , we can use the blockwise conversion. Since A16 = 224 , 816 = 204 and
F16 = 334 , we thus have A8:F16 = 2220:334 .
3. Since 11011:12 = 1B:816 , we have A8:F16 + 11011:12 = A8:F16 + 1B:816

= C4:716 .

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

176

x15

1. Since tan(2=3) = tan 120 =


3, equation y = 1
3x is a straight p
line of slope

120 that goes through the point (0; 1) in the Cartesian plane. Hence 3x + y  1
describes the area on the right hand side of the straight line, including the line itself.
y

p1

2. The binary relation F is F


F

f (0 0 )
;

x
3x + y = 1

j n 2 Zg, i.e.

= (n ; n 2 )

(1; 1); (

1; 1); (2; 4); ( 2; 4); (3; 9); ( 3; 9); ::: g :

3. The digraph that represents the binary relation R is

v1

v4

= - = v2

? ^ ?
Y Y v3
s

Hence R is reflexive and transitive, but not symmetric.


4. Since j f (x)j  j f (x)j means f (x) = ( f (x)), we conclude immediately ( f ; f ) 2 R.
That is, R is reflexive. If ( f ; g) 2 R and (g; h) 2 R, then there exist positive constants
C1 ; D1 ; C2 and D2 and constants M1 and M2 such that
D1 jg(x)j j f (x)j  C1 jg(x)j; 8x  M1 ;

D2 jh(x)j  jg(x)j C2 jh(x)j; 8x  M2 :

Hence for x  M = maxfM1 ; M2 g we have


def

jh(x)j  D1 jg(x)j  j f (x)j  C1jg(x)j  (C1C2 )jh(x)j


which means f (x) = (h(x)), i.e. ( f h) 2 R. Hence R is transitive.
(D1 D2 )

x16

1. Just 1 equivalence class.


2. There are 2 distinct equivalence classes. They are [1 = f1; 3; 5; 7; 9g and [2 =
f2; 4; 6; 8; 10g.

Hints and Solutions to Supplementary Exercises

177

3. Yes, R is an equivalence relation because the reflexivity, symmetry and transitivity


can all be easily verified. There are 5 distinct equivalence classes. They are represented by (2,0), (1,0), (0,0), (0,1) and (0,2) respectively. In the graph below, vertices
in the same equivalence class are connected by some edges.
(1,2)
r
r(2,2)
(0,2) r
(0,1) r

(1,1)

(0,0) r

r(2,1)

r(2,0)

(1,0)

x17

1. There are 2 minimal elements, a and f , and 1 maximal element d. The maximal
element d is also the greatest element because it can reach all other elements along
paths that go downwards on every edge of the paths. However, there are no least
elements because a and f are not comparable.
2. Let x, y and z be any elements of R . R is reflexive because xRx holds for any x due
to x  x. R is antisymmetric because xRy and yRx will imply x  y and y  x, and
hence x = y. R is transitive because xRy and yRz will imply x  y and y  z, and
hence x  z or, equivalently, xRz. Therefore R is a partial order relation.
3. No, R is not a partial order relation because R is not reflexive. In fact, xRx, or x < x,
is not true for any x 2 R .
4. If we redraw the tree T such that the root is at the very top and that child vertices
are alway drawn below their parents, then the newly drawn tree becomes also the
Hasse diagram for the partial order relation . The Hasse diagram here is obviously
isomorphic to T .

x18

1. The switching system is


an

bn
cn

bn
dn

and is conductive when a an b are closed and c and d are open.


2. No. Because mappings f and g dont have the same codomain.
3. Boolean algebra properties B1B5, plus P2, imply
a = a  1 = a  (1 + b) = a  1 + a  b = a + a  b :
1. (a) and (b) are shown in the 1st truth table below, while (c) and (d) are shown in the
2nd truth table.

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

178

p
T
T
F
F

q
T
F
T
F

p
F
F
T
T

q
F
T
F
T

p_q
T
T
T
F

(

p) _ q
T
F
T
T

( p _ q) ^ ((

p) _ q)

T
F
T
F

{z

p!q
T
F
T
T

same as the column for q

p
T
T
F
F

q
T
F
T
F

p
F
F
T
T

q
F
T
F
T

p_q
T
T
T
F

(

p) ^ ( q)
F
F
F
T

( p _ q) _ ((

( q)

{z

identical columns

p) ^ ( q))

( p _ q) ^ ((

T
T
T
T

! ( p)
T
F
T
T

p) ^ ( q))

F
F
F
F

{z

all T : tautology

{z
all F: contradiction

2. The truth table below shows ( p ^ q) _ r and p ^ (q _ r) are not logically equivalent
because their corresponding columns in the truth table are not exactly the same.
p
T
T
T
T
F
F
F
F

q
T
T
F
F
T
T
F
F

r
T
F
T
F
T
F
T
F

p^q
T
T
F
F
F
F
F
F

q_r
T
T
T
F
T
T
T
F

(p

^ q) _ r

p ^ (q _ r)
T
T
T
F
F
F
F
F

T
T
T
F
T
F
T
F
|

{z

columns not exactly the same

x19

1. (a) x + x + x + yz + xx0 + yz = (x + x) + x + (yz + yz) + (xx0 ) = x + x + yz + 0 =


(x + x) + yz = x + yz.
(b) x = x  1  1 = x  (y + y0 )  (z + z0 ) = (xy + xy0 )  (z + z0 ) = xyz + xy0 z + xyz0 + xy0 z0 .

2. f (x; y; z) = x0 y0 z + xy0 z + xyz0 .

3. We obtain from xy = (x0 + y0 )0 = ((x + x)0 + (y + y)0 )0 the following gate implementation
0
dx
d
x
xy
y

y0

4. There are two possible answers to this question. They correspond to the two tables
below.

Hints and Solutions to Supplementary Exercises


x
0
0
0
0
1
1
1
1

y
0
0
1
1
0
0
1
1

179

z light
0
0
1
1
0
1
1
0
1
0
1
0
0
0
1
1

x
0
0
0
0
1
1
1
1

z light
0
1
1
0
0
0
1
1
0
0
1
1
0
1
1
0

y
0
0
1
1
0
0
1
1

(A)

(B)

For the 2nd table (table B), for instance, the Boolean expression for the output is
x0 y0 z0 + x0 yz + xy0 z + xyz0 :
This expression does not simplify any further. (Incidentally it is no coincidence that
there are no adjacent terms, can you explain why? ) The switching circuit thus takes
the form
x0 y0 z0
x0 y z
x y0 z
x y z0
In fact a complete electrical circuit can be drawn as
switch
r

b
b

b
b

r
r

b
b

b
b

switch

longest cable here


r

light

switch

b
b

b
b

b
b

b
b

b
b

b
b

b
b

b
b

r
r

power

r
m

where each switch is actually a multi-switch having just 2 states


A
B
C
D

a
b
b
b
b
b
b
b
b
b
c
b
b
b
b
d
multi-switchs only 2 states
b
b

b
b

The 1st state connects A with a and B with b while disconnecting C from c and
D from d. The 2nd state does the opposite. If we use the readily-available toggle

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

180

switches instead, then the electrical circuit can be wired as


q
i

qq

q q
q q
q q

q q
q q
q q

q q
q q
q q

where a toggle switch has the following 2 states


q q
q q
q q

x20

q q
q q
q q

1. xy0 z0 , xyz and x0 y0 z are adjacent to xy0 z. Since


x0 yz + xy0 z0 + xz + xy0 + x0 yz + xyz + x0 y0 z
=

x0 yz + xy0 z0 + (xyz + xy0 z) + (xy0 z + xy0 z0 ) + x0 yz + xyz + x0 y0 z

(x

0 yz + x0 yz) + (xy0 z0 + xy0 z0 ) + (xyz + xyz) + (xy0 z + xy0 z) + x0 y0 z


x0 yz + xy0 z0 + xyz + xy0 z + x0 y0 z ;

the canonical form is x0 yz + xy0 z0 + xyz + xy0 z + x0 y0 z.


2. x + x0 yz0 = (x + xyz0 ) + x0 yz0 = x + (x + x0 )yz0 = x + 1  yz0 = x + yz0 .

3. An alternative minimal representation is yz0 + y0 z + wx0 z, as can be observed from


the following Karnaugh map
wx wx0 w0 x0 w0 x
yz
yz0
y0 z0
y0 z

4. First, the Karnaugh map grid for the given 5-variable Boolean expression
vwy + vw0 yz + v0 yz + vwz0 + v0 wx0 z0 + vw0 x0 y0 z
can be drawn as

Hints and Solutions to Supplementary Exercises


vwx vwx0 vw0 x0 vw0 x

181
v0 w0 x v0 w0 x0 v0 wx0 v0 wx

yz

yz0

10

11

yz0

y0 z0

12

13

14

y0 z0

y0 z

yz

y0 z

15

where the temporary box labels 1 15 are placed there to faciltate our later explanations. Next, we put proper 1s according to the given Boolean expression.
One way of doing this is to first cast the expression into the canonical form and then
place a corresponding 1 for each standard product term. An alternative way is
to interpret each product term, which may or may not be a standard product term,
in the Boolean expression directly as a block of 1s. Hence the product terms vwy,
vw0 yz, v0 yz, vwz0 , v0 wx0 z0 and vw0 x0 y0 z correspond to the blocks containing 1 , 2 ,
9 , 10 , 3 , 4 , 5 , 6 , 7 , 8 , 9 , 10 , 12 , 13 and 15 respectively.
Hence the Karnaugh map takes the following form
vwx vwx0 vw0 x0 vw0 x
v0 w0 x v0 w0 x0 v0 wx0 v0 wx
yz
yz0

' 
$

y0 z0

& %


y0 z

yz

yz0

y0 z0

y0 z

1
&

Obviously, the block of 8 1s and the block of 4 1s, as those circled in



1 1


1 1
1 1


1 1

1 1 1 1
1
1




correspond to yz and vwz0 respectively. The block of 2 1s and another block of 4


1s, as those circled in
1 1 1 1
  
1 1
1 1 

1


1 1 1 1

1
1



182

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES


correspond to vw0 x0 z and wx0 z0 respectively. Hence the minimal representation is
yz + vwz0 + wx0 z0 + vw0 x0 z :
In fact the minimal representation is unique in this case.

x21

1. The roots are collected from both 2 + 9 + 14 = 0 and 2 + 7 + 10 = 0. The 1st


equation gives 2 roots 2 and 7 while the 2nd gives 2 roots 2 and 5. Hence all
the roots, counting multiplicity, of the equation (2 + 9 + 14)(2 + 7 + 10) = 0
are 2, 2, 5 and 7. In other words, 5 and 7 are 2 simple roots while 2 is
a repeated root of multiplicity 2.
2. Iteratively, we have a2 = 1 3a0 a1 = 5 and a3 = 1 3a1 a2 = 31. Recursively, we
have a3 = 1 3a1 a2 = 1 3a1 (1 3a0a1 ) = 1 3  2  (1 3  1  2) = 31.
3. Yes, the recurrence relation an+2 + nan+1 + 5an = 0 is linear and homogeneous. But
it is not a constant coefficient recurrence relation because one of the coefficients, n,
is not a constant.
4. Since the associated characteristic equation 2 1 = 0 has 2 roots 1 and 1, the
general solution is thus given by an = A  1n + B  ( 1)n = A + B  ( 1)n, where
A and B are arbitrary constants.

x22

1. = 1 is a root of the equation 3 42 + 5 2 = 0 because, if = 1 is substituted


into the equation, the equation evaluates to 0, i.e. 13 4  12 + 5  1 2 = 0. In fact
a polynomial equation has 1 as one of its roots if and only if the summation of all the
coefficients is 0. The polynomial equation has 1 as a root implies the factorisation
3 42 + 5 2 = ( 1)  (a 2nd degree polynomial in ). According to the long
division

3
3

+2

42
2
32
32

+5

+5
+3

2
2

2
2
0

the factorisation takes the form 3 42 + 5 2 = ( 1)  (2 3 + 2). Since


2 3 + 2 = 0 has 2 distinct roots 1 and 2, the original equation 3 42 + 5 2 =
0 has a simple root 2 and a repeated root 1 of multiplicity 2.
2. The associated characteristic equation 2 + 14 + 49 = 0 has just 1 repeated root 7
because the roots
p
14  142 4  1  49
21

Hints and Solutions to Supplementary Exercises

183

both give the value 7. Hence the general solution of the recurrence relation is
an = (A + Bn)  ( 7)n, where A and B are arbitrary constants.
3. The associated characteristic equation has just 1 repeated root 3 of multiplicity 2.
Hence the general solution of the recurrence relation is an = (A + Bn)  3n for some
arbitrary constants A and B. From the initial conditions we have the following 2
equations
5 = a0 = (A + B  0)  30 ;

6 = a 1 = (A + B  1 )  3 1 ;

i.e. 5 = A and 6 = (A + B)  3. Solving these 2 equations we obtain A = 5 and


B = 7. Hence the final solution of the recurrence relation, satisfying also the initial
conditions, is an = (5 7n)  3n for n  0.

x23

1. No, an = un + vn cant satisfy the recurrence relation an+15


is because
an+15

2an+1 + an

(un+15 + vn+15 )

(un+15

2an+1 + an = 1. This

2(un+1 + vn+1 ) + (un + vn )

2un+1 + un ) + (vn+15

2vn+1 + vn ) = 1 + 1 = 2 6= 1 :

2. The associated characteristic equation 2 9 + 14 = 0 has 2 distinct roots 2 and


7. Hence the general solution of the corresponding homogeneous recurrence relation is un = A2n + B7n for some arbitrary constants A and B. The nonhomogeneous term 6n on the r.h.s. of the recurrence relation is of the form n = (1n ) 
(polynomial of degree 1), in which the base 1 of the power 1n is not a root of
the characteristic equation. Hence a particular solution vn of the nonhomogeneous
problem will take the form vn = (1n )  (Cn + D) = Cn + D, where Cn + D is a general polynomial of degree 1 with C and D as undetermined constants. Substituting
an = vn into the nonhomogeneous recurrence relation we obtain
vn+2

9vn+1 + 14vn = (C(n + 2) + D)

9(C(n + 1) + D) + 14(Cn + D) = 6n

which then simplifies to (6C 6)n + (6D 7C) = 0. Hence C and D must satisfy
the equations 6C 6 = 0 and 6D 7C = 0, implying C = 1 and D = 76 , and thus
vn = n + 76 . The general solution of the nonhomogeneous recurrence relation is
therefore an = A2n + B7n + n + 76 , where A and B are arbitrary constants.
3. From the solution of the previous question, the general solution of the nonhomogeneous recurrence relation, ignoring the initial conditions, is an = A2n + B7n + n + 76
for some arbitrary constants A and B. The initial conditions thus give rise to the
following 2 equations
1
7
31
7
0
0
1
1
= a0 = A  2 + B  7 + 0 + ;
= a1 = A  2 + B  7 + 1 +
6
6
6
6
whose solutions are A = 2 and B = 1. Hence the particular solution of the nonhomogeneous recurrence relation, satisfying the initial conditions, is an = 2n+1 +
7n + n + 76 .

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

184

x24

1. The characteristic equation and the general solution of the corresponding homogeneous recurrence relation have already been obtained in the questions in the previous section. The general solution of the homogeneous problem was found to
be an = A2n + B7n for some arbitrary constants A and B. We thus now proceed
to find a particular solution for the nonhomogeneous problem. Since the nonhomogeneous term on the r.h.s. of the recurrence relation has the form 2n = 2n 
(polynomial of degree 0), a particular solution vn will take the same form, i.e. 2n 
(polynomial of degree 0), plus an adjustment multiplier due to the base 2 of 2n being
also a root of the characteristic equation. The general form of 2n  (polynomial of degree 0)
can be represented by 2n  C for an arbitrary constant C, and the adjustment multiplier is n1 because 2 is a simple root of the characteristic equation. Hence a particular
solution will take the form vn = (2nC)n. Substituting vn into the nonhomogeneous
recurrence relation we obtain
vn+2

9vn+1 + 14vn = 2n+2C(n + 2)

9  2n+1C(n + 1) + 14  2nCn = 2n

1
1
which then simplifies to 10C = 1. Hence C = 10
 n2n, and the
, vn = 10
final general solution of the nonhomogeneous recurrence relation thus reads an =
n
(A 10 )2n + B7n .

2. The associated characteristic equation has only the root 1 of multiplicity 2. Hence
the general solution of the corresponding homogeneous problem is just un = (A +
Bn)  1n , where A and B are arbitrary constants. Since the nonhomogeneous term
2 = 2  1n and 1 is a root of multiplicity 2 of the characteristic equation, a particular solution will take the form vn = (1n  C)  n2 = Cn2 . Substituting vn into the
nonhomogeneous recurrence relation we obtain
C(n + 2)2

2C(n + 1)2 + Cn2 = 2

2C = 2

C=1:

Hence vn = n2 , and the general solution of the nonhomogeneous recurrence relation


is thus an = un + vn = A + Bn + n2 . Since the initial conditions result in the following
2 equations
1 = a0 = A + B  0 + 02 ;

1 = a1 = A + B  1 + 12

whose solutions are A = 1 and B = 1, the final solution of the nonhomogeneous


recurrence relation, satisfying the initial conditions, is therefore an = 1 n + n2 .

xA

1. For the selection sort, the least possible number of comparisons needed to sort a
list of n items is 12 n(n 1) because every case is both the best and the worst case.
For the bubble sort and the insertion sort, the best cases are when a list is already
in the correct order. In such cases, n 1 comparisons are needed to sort a list of n
items. For the merge sort, the best case is when the merging of every pair of lists
of, say, p items and q items respectively, will take exactly the minimum minf p; qg

Hints and Solutions to Supplementary Exercises

185

of comparisons. Hence for a list of n = 2k items, the breakdown of the least number
of comparisons is summarised in the table below, where t1 = 2k 2 , t2 = 2k 1 and
t 3 = 2k 1 + 2k 2 .
depths
k
a1 ja2 j:: j ::: j ::
..
.

lists

:: ::::: ::

2
1
0

:: ::::: ::

..
.
a1 ; :::; at1
at1+1 ; :::; at2 at2 +1 ; :::; at3
a1 ; :::; a2k 1
a2k
a1 ; a2 ; :::; a2k

comparisons
2k 1  1
..
.

j jan 1janj

:: ::::: ::

21  2k
20  2k
0

at3+1 ; :::; a2k


1 +1 ; :::; a2k

The total number of comparisons is thus


20  2k

+2

 2k

 + 2k 2  2 + 2k 1  1 = k2k

n
log n :
2 2

Hence in the best case of sorting a list of n = 2k items, the merge sort needs 12 n log2 n
comparisons.
2. If we always use the 1st item as the pivot, the sorting of a correctly ordered list
is actually the worst case for the quick sort. This is rather embarrassing as we
would normally expect that sorting a list that is already in the correct order should
be easier. A simple way to overcome this problem is to choose a pivot randomly.
Another simple method is to pick 3 items, the 1st, the last and the middlemost
items, then choose for the pivot the item which is the median (the middle value) of
the three. This is called the Median-of-Three Partitioning. For example, if we use
this partitioning method to sort the list 5; 4; 1; 2; 3; 6; 7, then the intermediate steps
are
steps

lists

comparisons 1st,middle,last items


3

4 1 2 3 5 6 7
1 2 3 4 5 6 7
1 2 3 4 5 6 7

1
2
3

5; 2; 7

4; 1; 3 ;

6; 6; 7

1; 1; 2

The total number of comparisons made in this case is 6+4+1=11.


3. The derivation here requires the knowledge of integrations. In a typical pass by the
quick sort, the pivot item, say am , will have the probability 1n to be moved to position
k for any k with 1  k  n,
L

a1 ;

}|
:::;

am ;

{
:::;

an

L1

}|

aN1 ;
|

(k

:::;

{z

aNk

1) items

}1

L2

}|

am ; aNk+1 ;
|

:::;

{z

(n

aNn

k) items

2
1

HINTS AND SOLUTIONS TO SUPPLEMENTARY EXERCISES

186

requiring an average of additional T (k 1) and T (n


newly created sublists L1 and L2 respectively. Hence
T (n) = (n

1) +

1
n

T (i

1 ) + T (n

i)

k) comparisons to sort the 2

= (n

1) +

i =1

2
n

n 1

T (i)

i=0

where the term (n 1) is the number of comparisons needed to split the original
list L into the 2 sublists L1 and L2 . This means
h n 2

nT (n)

1 ) + 2 T (i )

n(n
n(n

(n + 1)T (n

+ 2T (n

1)

i=0

1) + (n

1)T (n

1 ) + 2 (n

1)

(n

1)(n

2)

+ 2T (n

1)

1) ;

or equivalently,
T (n 1)
2
T (n)
=
+
2 (n + 1 )
2n
n+1
Hence, recursively and inductively,
T (n)
2(n + 1)

1
n

T (n 2 )  2
1   2
1
+
+
=
2 (n 1 )
n n 1
n+1 n
T (1 )
2
1
1
1 1
+
+ +
+  +
:
22 n+1 n n 1
3 2

=
=



We thus have, due to T(1)=0,


T (n)
2(n + 1)

2
n+1
n 1

Zi

i=1 i

h1

+1

1
1

dx
2
+
x
n+1

 + 13 + 12
Z n dx
1

1:
+

1 n
n+1

ln(n) +

1 n
n+1

n1:

Since for f (n) = ln(n) + 1 n we have f (n)  0 for all n  1 because f (1) = 0 and
f 0 (n) = 1=n 1  0, we obtain


T (n)  2(n + 1) ln(n) + 2(1 n) = 2n ln(n) + 2 ln(n) + 1 n

 2n ln(n) 8n  1
;

Hence we conclude
T (n ) 

2 
 n log2 n ;
log2 e

8n  1

Likewise we have also for n  1


T (n)
2(n + 1)

i
1 1
+
+
+  + + + 1
2
n+1
n n 1
3 2
Z n+1 dx 2n
n Z i+1
dx
2
+
2 =

x
n+1
x
n+1
1
i=1 i

h1

ln(n + 1)

2n
n+1

Hints and Solutions to Supplementary Exercises

187

Hence for any such that 0 < < 1 we have for all n  dexp(2=)e
T (n)



=


because
ln(n)

2(n + 1) ln(n + 1)


2n ln(n)
2 (1


2 (1

4n

)n ln(n) + 2n ln(n)


)= log2 e n log2 n

2  lndexp(2=)e

20:

Hence if there exist constants D and M such that T (n)  Dn log2 n holds for all
n  M, then we must have D  2(1 )= log2 e. By taking the limit ! 0 we then
conclude D  2= log2 e = 2 ln 2  1:386.

Index
(S; +;

0

m  n (mod d), 102


p $ q, 49
p _ q, 45
p ^ q, 45
p , q, 59
p ) q, 47, 59
p ! q, 47, 50
e1
e2
n
v0 !
v1 !
 en!1 vn 1 e!
vn , 67
v0 ; e1 ; v1 ; e2 ;  ; vn 1 ; en ; vn , 67
xRy, 99
x 6 Ry, 99
E , 67
N , 67
O , 29
P (S), 16
N , 15
Q , 17
R , 15
Z,15
n
r , 26
p1 ; p2 ; :::; pn; ) q, 49
F0 , 124
T0 , 124

0; 1), 122
27
A \ B, 15
A [ B, 15
A  B, 15
A0 , 16
A  B, 121
A1  A2  An , 17
B 6 A, 15
B A, 15
BnA, 15
Bn , 121
Km;n , 65
Kn , 65
P  Q, 46
P(x) , Q(x), 59
P(x) ) Q(x), 59
P , Q, 46
[a, 105
, 33
bx , 41
dxe, 41
9, 58
8, 58
: p, 45
A, 16
, 20
 p, 45
, 20
), 49
?, 16
jaj, 9
a 2 A, 121
a 62 A, 121
a  b, 110
d j m, 102
f : A ! B, 121
f : A  A ! A, 121
; ;

(x + y)n ,

pq

q
p

q
p

q
p
p
q

( pq)0

, 130
, 131

( p + q)0

p0

, 131

, 130
p+q

, 130

absolute value, 9
absorption law, 122
addition, 98
adjacency matrix, 76, 77
adjacent, 133
algorithm, 40
ancestor, 82
and, 48
189

Index

190
AND-gate, 130
ansatz, 153
antisymmetric, 109
arc, 64
argument, 49
argument form, 49
arrows, 76
associativity, 122
axiom of comprehension, 19
base p, 93
base number, 93
big-pi notation, 20
binary number system, 94
binary operation, 121
binary relation, 99
binary relation on a set, 100
binary search, 40
binary tree, 82
binary tree to sort, 84
binomial expansion, 27
binomial expansions, 13
binomial theorem, 27
block, 134
blockwise conversion, 97
books for reference, 5, 7, 8, 19, 69, 73, 85,
119, 137, 157, 166
Boolean algebra, 122, 125
Boolean expression, 122, 127
Boolean function, 126
borrow, 98
branch vertex, 80
bubble sort, 44, 161
cancellation law, 122
canonical, 127
carry, 98
Cartesian plane, 100
Cartesian product, 17, 121
ceiling, 41
characteristic equation, 140
children, 82

circuit, 70
closed, 120
closure, 107
codomain, 121
commutativity, 122
comparable, 110
compatible, 115
complementary switches, 120
complementation, 15, 122
complete bipartite, 65
complete graph, 65
complexity, 44
compound proposition, 45
conclusion, 49, 50
conditional statement, 47
congruent, 102
conjunction, 45
conjunctive addition, 51
conjunctive simplification, 51
connected, 67
connected components, 68
connectives, 45
contradiction, 45, 48
contrapositive, 50, 62
contrapositive equivalence, 51
converse, 50, 62
converse error, 62
convert numbers, 95
critical rows, 49
criticial rows, 57
cycle, 80
De Morgans Laws, 47
decimal number system, 93
degree, 67
depth, 82
descendents, 82
difference, 15
difference equation, 138
digraph, 64
directed edge, 64

Index
directed graph, 64
disconjunction, 45
disjoint, 16
disjunctive addition, 51
disjunctive syllogism, 51
distributivity, 122
divide and conquer, 160
division algorithm, 95
division into cases, 51
domain, 58, 121
dominance laws, 122
double complement, 122
edge-endpoint function, 64
edges, 64
elimination method, 12
emergency kit, 14
empty set, 16
equivalence class, 105
equivalence connective, 49
equivalence relation, 101
Eulers Theorem, 71
Eulerian circuit, 70
Eulerian graph, 70
Eulerian path, 70
existential quantifier, 58
factorise, 145
factorising quadratic polynomials, 10
false, 48
first principles, 30
Fleurys algorithm, 73
floor, 41
forest, 80
general solution, 141, 142, 144, 148
generalised triangle inequality, 9
graph, 64
greatest element, 110
grid, 133
Hamiltonian circuit, 70
Hamiltonian path, 71

191
Hasse diagram, 111
height, 82
hexadecimal number system, 94
homogeneous, 139
Horners algorithm, 29
Huffman coding, 85
hypotheses, 49
hypothesis, 50
hypothetical syllogism, 51
idempotent laws, 122
identity relations, 122
iff, 46
implication, 47, 48
implies, 47
induced, 106
initial conditions, 138, 150, 151
inorder traversal, 83
insertion sort, 44, 162
internal vertex, 80
intersection, 15
inverse, 50, 62
inverse error, 62
isomorphic, 74
isomorphic invariant, 75
isomorphism, 74
iteratively, 138
Karnaugh map, 132
Kruskals algorithm, 90
Kuratowskis Theorem, 68
l.h.s., 22
leaf, 80
least element, 110
left child, 82
left subtree, 83
legitimate logical expression, 48
length, 67
level, 82
linear equations, 12

Index

192
linear, constant coefficient recurrence relation, 139
literals, 127
logarithm, 9
logic circuits, 130
logic gates, 130
logically equivalent, 46
long division, 145
loop, 65
lower bound, 33, 34
mapping, 121
matrix, 76
matrix multiplications, 77
maximal element, 110
Median-of-Three Partitioning, 185
merge sort, 44, 164
method of undetermined coefficients, 153
middlemost, 41
minimal element, 110
minimal representation, 128
minimal spanning tree, 90
modulo, 102
modus ponens, 51
modus tollens, 51
multiple edges, 65
multiplicity, 145
NAND-gate, 131
necessary condition, 62
negation, 45
negation of quantifiers, 60
nested evaluation, 29
nodes, 80
nonhomogeneous part, 153
nonhomogeneous problem, 149
nonhomogeneous recurrence relations, 148,
157
nontrivial circuit, 80
NOR-gate, 131
not, 48
NOT-gate, 130

number of walks, 78
octal number system, 94
one-to-one, 74
onto, 74
open, 120
or, 48
OR-gate, 130
order of precedence, 49, 59
P.M.I., 21
parallel, 120
parallel edges, 65
parent, 82
partial order relation, 109
particular solution, 141, 148, 152
partition, 16, 106
pass, 160, 161, 163, 166
path, 67
pivot, 165
planar, 68
polynomial, 28
postfix notation, 84
postorder traversal, 83
power, 9
power set, 16
precedence, 49, 59, 122
predicate, 58
preliminary mathematics, 9
premise, 50
premises, 49
preorder traversal, 83
Principle of Mathematical Induction, 21
product terms, 127
proof, 56
proof sequence, 56
property closure, 107
proposition, 44
quick sort, 44, 165
Quine-McCluskey procedure, 137
r.h.s., 22

Index
recurrence relation, 138
recursively, 138
reference books, 5, 7, 8, 19, 69, 73, 85, 119,
137, 157, 166
reflexive, 101
relations, 99
relative complement, 15
Remainder Theorem, 95
repeated root, 145
repeated roots, 144
representative, 105
right child, 82
right subtree, 83
root, 82
root and factor equivalence, 11
root of multiplicity, 141
root of multiplicity M, 153
rooted tree, 82
roots of quadratic equations, 10
rule of contradiction, 51
rules of inference, 61
Russels Paradox, 19
S.P.M.I., 25
sandwiched, 33
selection sort, 44, 160
sequence, 20
sequential search, 40
series, 120
series reduction, 69
set, 15, 121
set identities, 18
seven bridge problem, 72
siblings, 82
sigma notation, 20
simple graph, 65
simple root, 142
simplification procedure, 135
sleepless nights, 124
sorting algorithm, 160
spanning tree, 86

193
standard product term, 127
step operation, 147
Strong Principle of Mathematical Induction,
25
subgraph, 66
subset, 15
subtraction, 98
successive divisions, 97
sufficient condition, 62
switch, 120
switching circuit, 120
switching system, 120
symmetric, 101
tautology, 44, 48
telescoping form, 29
terminating vertex, 80
theorem, 56
therefore, 49
topological sorting, 115
topological sorting algorithm, 116
total order relation, 110
totally ordered, 110
transitive, 101
transitive closure, 108, 112, 114
tree, 80
triangle inequalities, 30
triangle inequality, 9
trigonometric properties, 10
trivial walk, 67
true, 48
truth function, 126
truth tables, 45
tuple, 122
unary operation, 121
undirected graph, 64
union, 15
uniqueness of inverses, 122
universal modus ponens, 61
universal modus tollens, 61
universal quantifier, 58

194
universal set, 16
upper bound, 33, 34
vacuous truth, 61
valid, 49, 62
Venn diagrams, 15
vertices, 64
w.r.t., 115
walk, 67
weight, 90
weighted graph, 70, 90
well-formed formulas, 48
wff, 48

Index

Anda mungkin juga menyukai