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
15
1.2
Set Identities . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
18
1.3
Russels Paradox . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
19
Mathematical Induction
20
2.1
21
2.2
25
Efficiency, Big O
28
3.1
28
3.2
Big O Notation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
29
3.3
Simple Features of O . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
32
Notation
33
4.1
33
4.2
35
Analysis of Algorithms
40
5.1
Sequential Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
40
5.2
Binary Search . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
41
5.3
43
5.4
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
50
7.3
Rules of Inference . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
51
8 Predicate Calculus
58
8.1
Predicate Quantifiers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
58
8.2
Predicate Properties . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
60
64
9.1
64
9.2
65
9.3
Planar Graphs . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
68
69
69
70
73
74
74
76
12 Trees
80
80
83
13 Spanning Trees
13.1 The Concept of Spanning Trees . . . . . . . . . . . . . . . . . . . . . . . . . .
86
86
CONTENTS
88
90
14 Number Bases
93
93
95
15 Relations
15.1 Binary Relations . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
99
99
103
109
120
126
CONTENTS
4
20 Karnaugh Maps
132
138
144
148
152
160
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.
3.
READING GUIDE
6
4.
5.
6.
7.
8.
9.
10.
11.
12.
13.
14.
15.
READING GUIDE
16.
17.
18.
19.
T:
P-8
20.
21.
T-7
P-9
22.
23.
T-8
24.
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.
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.
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
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 :
jx1 + x2 + + xn j jx1j + jx2 j + + jxn j jx1 + x2 + + xn j jx1j jx2j jxn j
;
j2 1 j j
:
0:2j
0:2.
ax+y = ax ay ;
(a
y if and only if
a0 = 1 ;
PRELIMINARY
10
loga bx = x loga b
loga 1 = 0 :
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.
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
15
6
5)=6 = 1.
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)
n 1
+
1x
+ a1x + a0
an 6= 0 ;
x0 )Q(x)
x2
5x + 2 :
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) :
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) :
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:
2y
3z = 10;
2x + 3y
5z = 1
3z = 10;
2x + 3(7x + 2z
2(7x + 2z
7)
7)
5z = 1
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
(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
..
.
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
I
I
I
I
I
I
I
B
&
A = B means A B and B A
'
B
A
B
A
1 SETS
16
Examples
1.
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.
A2
A1
A3
A
A4
a typical partition
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.
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
1 SETS
18
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)
19
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
2 MATHEMATICAL INDUCTION
20
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
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
(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
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.
k(k + 1)
2
)
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 (*) ;
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
1
k (k + 1 )
2
+ (k + 1) ;
1
(k + 1)[(k + 1) + 1
2
(2-7)
1
k (k + 1 )
2
(2-8)
j=1
+ (k + 1) = l.h.s.
of (2-3)
25
n(n + 1)(2n + 1)
6
()
r.h.s. of () =
1 (1 + 1)(2 1 + 1)
6
=1
12 + 22 + 32 + + k2 =
k(k + 1)(2k + 1)
6
()
Hence
12 + 22 + + k2 + (k + 1)2
from ()
=
=
=
=
=
2
2
(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
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
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:
n
r
n!
(n r)!r!
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)!
27
n+1
0
=
n+1
n+1
=1=
(n + 1)!
(n + 1)!0!
1
(x + y) = x + y =
1
1
x+
y:
0
1
induction
assumption
h
=
=
=
(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
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
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
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
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
1,
2 operations
2 (more) operations
2 (more) operations
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
=
=
=
=
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.
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
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
Solution Observe
i.e.
if n 3
2n + n ;
j2n
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).
31
2. Show f (x) =
p
3 x(2x + 5)
is O ( x) for x 2 R .
jxj + 1
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
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)
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
j f (n)j
=
=
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
3 EFFICIENCY, BIG O
32
xM
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
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
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 :
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
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
(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.
35
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
3 + 2n2
n+1
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)
(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)
n + 2n
n
n+1
3 + 2n2
2
= n + 2n
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)
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)
3 2 n3 + 2n2 1
n
3n2 ;
8
n+1
for n 2 ;
n3 + (2n2 1)
n+1
3
n + 2n2
n+1
3
n + n3
n
2
2n
1 0 and f (n) 0)
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
=
which means
39
n3
because 2n2
n+1
n3
n+n
1 2
n ;
2
10
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 =
log2 n < n
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:
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.
41
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 ANALYSIS OF ALGORITHMS
42
1. F = 1, L = n.
2. while F L do
/* match found */
middlemost new F
old L
{z
old list |
{z
new list
}
}
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 :
3 4
5 7
5
9
6
13
7
8
15 17
9
19
43
B(1) = 1
B(m) B(n) if m n
m 1
see:
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 :
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
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
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.
I
I
I
I
45
Suppose we have two statements or propositions denoted by p and q respectively, then the above
three connectives may give rise to
p _ q represents Either today is a rainy day, or David was wet this morning, or
both .
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.
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
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
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
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:
Representation
p ! q ( p ) _ q :
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
(!).
49
;
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 _.
:::;
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
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
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.
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.
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
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
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:
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
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
p_q
q ! u^s
To derive p from p _
q, one needs q
(disjunctive syllogism)
q
q
q!r
6
r
!5!
3;
4;
3; 4
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
(ii) q ! r; r; )
q
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
55
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
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)
;
7 PROPOSITIONAL LOGIC
56
B killed E.
(ii) SB :
(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
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.
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 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
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:
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.
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)
P(x) , Q(x)
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
(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) ;
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 .
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;
negation of quantifiers
jan
aj < :
8x P(x) ! Q(x))
P(a); ) Q(a)
I
I
61
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)
9x 2 D 8y 2 D L(x y)
;
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).
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.
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) ?
64
I
I
I
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
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
v4
fe1
fv1 g
fv1
fv2
fv2
fv2
v2 g
; v3 g
; v3 g
; v3 g
;
v5
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
fe1
r v3
edge e
e1
e2
e3
e4
(v1 ; v1 )
(v1 ; v2 )
(v3 ; v2 )
(v2 ; v3 )
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,
66
r
K1;2
K2;3
K3;3
fv1
v2 ; v3 g, E
v
r 2
r
v1
v3
v1
v3
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
rv2
r
v3
v4
1 ; en ; vn
! v1 e! e ! vn
e1
n 1
! vn
en
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
=
=
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
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?
70
A
B
river
> :
bridges
river
Ar
B
sC
30
35
50
40
25
s
Before considering the above two problems in detail, we first introduce below a few related
basic terminology.
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.
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
r
r
^j >
r
r
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
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.
73
total distance
30 + 30 + 25 + 40 = 125
140
155
140
155
125
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
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?
v1 e4
f1
f2
e3
e1
s
w1
e2
s
f4
e5
v4
G1
v3
w2
s
s
f3
f5
w4
G2
w3
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
- 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
"!
e2 + fv2 ; v4 g
e3 + fv2 ; v3 g
e4 + fv1 ; v4 g
e5 + fv3 ; 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)
76
Examples
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.
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.
77
Examples
e1
/
v1 k
1 0 0
4
A= 1 1 2 5
1 0 0
.
is
e3
9v2
e2
e6
e4
e5
v3
e1
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
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
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
;
78
=
2
5
2 2 + ( 1) 5 2 ( 1) + ( 1) 3
52+35
5 ( 1) + 3 3
=
1
25
5
4
:
2
4
A= 5
11
1
3
0
13
65
10
7
4
B= 1
9
4
2
4
8
0 5
3
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
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
of Ak+1
=
=
of AB
i.e. Sk+1 is true. Hence Sm is true for all m 1, and the proof of the theorem is thus completed.
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.
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
2. Graph
r
r
r
r
is not a tree.
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.
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.
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
Inorder traversal
Postorder traversal
r
r
12 TREES
84
Examples
5. For binary tree
ra
r
b
d
e
r
r
r
a; b; d ; c; e; f ; g
d ; b; a; f ; e; g; c
d ; b; f ; g; e; c; a
+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;
=
:
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
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
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
87
! v5 .
Of
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.
89
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
?
{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
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
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.
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
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
:
= 11:875
=
=
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.
95
=
=
=
=
4 61 + 5 60 = 29
20 + 2 4 + 1
45+24+1
4 (4 + 1) + 2 4 + 1
=
=
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 q < (s + 1) p :
(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
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
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 .
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.
97
(1111011)2
123
61
30
15
1
0
2
123
61
123=612 + 1
successive divisions
Hence 123=11110112.
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
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
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.
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.
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.
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
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):
(1,3):
I
I
I
I
15 RELATIONS
102
zi
o
W
K3
r
jY
r
(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) :
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
2R
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)) :
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
;
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
..
.
+
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
;
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.
(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
( )
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 ,
(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.
107
fx 2 Z : x R 0g = f
6; 3; 0; 3; 6; g :
fx 2 Z : x R 1g = f
5; 2; 1; 4; 7; g :
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 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.
-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) :
iff m
n= j
k:
Is R an equivalence relation? If yes, give representatives for all the distinct equivalence
classes.
109
I
I
Antisymmetric: no r
q1Y
r
~ )y 2
r
type of arrows.
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
110
I
I
I
I
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.
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
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
>
112
18r
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.
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
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.
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)
6
r
10
6
?
M
r
r
k4
63
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.
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
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 :
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.
116
Example
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.
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
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
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
;
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.
12q
q10
q
q
q
q
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.
120
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
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
def
}|
B B B
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.
122
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)
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 :
123
B2
P1
0 = a a0 = (a a) a0 = a (a a0) = a 0 :
B5
B5
P1
B2
B5
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
;
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
a + (b c)
0
0
0
1
1
1
1
1
|
(a + b) (a + c)
0
0
0
1
1
1
1
1
{z
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?
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.
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.
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 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 )
;
?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
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.
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.
I
I
I
I
I
19 BOOLEAN FUNCTIONS
128
Two Boolean expressions are equivalent if they either
I
I
I
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
B3
B5
= x(y + y ) =
x1
B4
=
=
=
xy + xy0
xy + xy0
same canonical form
129
Examples
3. Find the minimal representation for f
= xy + xy + xx0 + xy0
(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
=
=
which is same as f . We can also derive (a)(d) from f directly. For (c), for instance, we
have
f
(xy z + xy
=
=
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.
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
An OR-gate, taking p and q as the input, will output the logical value p + q :
p
p+q
p0
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
20 KARNAUGH MAPS
132
AND-gates via
( pq)0
q
and OR-gates via
p
p0
d
pq
p+q
q0
Supplementary Exercises
1. Use Boolean algebra properties to show
(a) x + x + x + yz + xx0 + yz = x + yz.
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
20 Karnaugh Maps
The Karnaugh map provides a pictorial technique to simplify Boolean expressions.
133
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
I
I
I
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
I
I
Examples
4. The circled area in the Karnaugh map
is a 2 2 block representing
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
135
M=
1 1
is a 2 4 block representing z.
yz0
y0 z0
y0 z
= xyz + x0 y + x0 y0 z0 .
Solution
(a) F
= xyz + x0 yz + x0 yz0 + x0 y0 z0
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
yz
yz0
y0 z0
y0 z
wx
1
1
wx0
1
1
1
w0 x0
1
w0 x
1
1
1
1
1
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
&%
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 :
( )
)
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 )
=
=
=
=
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)
1 an+m 1 +
+ c1 an
+1
+ c0 an = g(n);
n0
)
5ak+1
2ak = 3k+2 ;
k0:
cm
1 = c1 =
5;
cm
2 = c0 =
2;
g(n) = 3n+2 :
21 RECURRENCE RELATIONS
140
1 cn+m 1 +
+ c1 an
+1
+ c0 an = g(n);
n0
m 1
+ c1 + c0 = 0
2.
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.
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
1 an+m 1 +
+ c1 an
+1
+ c0 an = 0;
cm c0 6= 0; n 0
i=1
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 =
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
i=1
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:
5 + 6 = 0
A1 + A2 = 0
2A1 + 3A2 = 1
i.e.
20 + 30 = 0
a1 =
21 + 31 = 1
an+2
5an+1 + 6an
=
=
{z
k
0
{z
k
0
144
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.
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
1n
m1 1
n
1
m2 1
n
) 2 +
1n
mk 1
nk
1n
145
Examples
1. Find a particular solution of
f (n + 2) + 4 f (n + 1) + 4 f (n) = 0;
n0
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
n0:
2n)( 2)n ;
3an+2 + 4an = 0 ;
n 0:
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
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)(
7.
Solution
(a) The associated characteristic equation
3 + 32 + 3 + 1 ( + 1)3 = 0
has roots 1 =
(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
=
=
=
2:
2n2 )( 1)n ;
n0:
1
1
7:
147
Ps () = f(b0 + b1 n + + bs ns )n j bi 2 C ; i = 0; 1; : : : ; sg
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
j=0
that this solution expression for an has exactly m arbitrary constants Ai; j .
148
Supplementary Exercises
1. Find all the roots, and their corresponding multiplicity, of the equation
3
42 + 5
2=0;
6an + 9an
1=0
c0 cm 6= 0
( )
)
(i) If vn and wn are two solutions of the nonhomogeneous equation () then
n = wn
vn ; n 0
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:
g(n)
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 ().
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
150
2 fn + 3 fn
4 = 6n,
n 4.
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
A
B
=
=
B
33=2
33
.
2
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
(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.
151
The general solutions for the corresponding homogeneous problem thus reads
n0:
un = A3n + (B + Cn)2n;
7an+2 + 16an+1
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
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
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
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.
153
Example
1. Solve an+2 + an+1
6an = 2n for n 0.
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.
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
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.
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.
155
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 :
5
5
and vn = n2 3n . Therefore our general solution reads
18
18
f (n) = A + Bn +
5 2 n
n 3 ;
18
n0:
5an+3 + 9an+2
7an+1 + 2an = 3 ;
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
=
=
=
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
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.
vn
i.e.
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) + 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
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
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:
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;
a0 = A + B = 2 ;
3n ;
n0:
C 4(n + 2)2
=4;
=
=
2n (A + Bn) + n2 2n
h
n2 i
2n A + Bn +
;
2
n0:
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 .
160
161
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
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
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
4 1
1 2
2 3
2 3
()
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
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.
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
165
=
2(log2 n + 1)n
if n 2
4n log2 n :
lists
extra comparisons
6 7
just recursively
4 1
6 7
4 1
6 7
5 1
6 7
2 4
6 7
6 7
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
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 *.
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
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
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
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.
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
x3
1;
j2n3
9n + 1j
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).
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
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.
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
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
x7
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.
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
) 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)
;
x8
1.
2.
(8x 2 R (x
;
>
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.
x9
173
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
v
r
r
v3
r
G1
v4
w4
w3
r
G2
w2
,
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
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
Y
.
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
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
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 .
176
x15
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
f (0 0 )
;
x
3x + y = 1
j n 2 Zg, i.e.
= (n ; n 2 )
(1; 1); (
v1
v4
= - = v2
? ^ ?
Y Y v3
s
x16
177
(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
bn
cn
bn
dn
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
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
x19
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.
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
light
switch
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
b
r
r
power
r
m
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
180
q q
q q
q q
q q
q q
q q
q q
q q
q q
x20
q q
q q
q q
(x
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
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
&
1 1
1 1
1 1
1 1 1 1
1
1
1 1 1 1
1
1
182
x21
x22
3
3
+2
42
2
32
32
+5
+5
+3
2
2
2
2
0
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 ;
x23
2an+1 + an
(un+15 + vn+15 )
(un+15
2an+1 + an = 1. This
2un+1 + un ) + (vn+15
2vn+1 + vn ) = 1 + 1 = 2 6= 1 :
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 .
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
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 = 2
C=1:
1 = a1 = A + B 1 + 12
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
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
+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
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
a1 ;
}|
:::;
am ;
{
:::;
an
L1
}|
aN1 ;
|
(k
:::;
{z
aNk
1) items
}1
L2
}|
am ; aNk+1 ;
|
:::;
{z
(n
aNn
k) items
2
1
186
1) +
1
n
T (i
1 ) + T (n
i)
= (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
=
=
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
2n ln(n) 8n 1
;
Hence we conclude
T (n )
2
n log2 n ;
log2 e
8n 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
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
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