Escolar Documentos
Profissional Documentos
Cultura Documentos
Problem Solving
Roland Ba khouse
January 7, 2007
January 7, 2007
Contents
1 Introduction
1.1 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Bibliographi
Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Invariants
1
3
5
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
3 Crossing a River
8
8
9
12
13
15
19
19
21
3.1 Problems . . . . . . . . . . . . . . . . .
3.2 Brute For
e . . . . . . . . . . . . . . . .
3.2.1 Goat, Cabbage and Wolf . . . . .
3.2.2 State-Spa
e Explosion . . . . . .
3.2.3 Abstra
tion . . . . . . . . . . . .
3.3 Jealous Couples . . . . . . . . . . . . . .
3.3.1 What's The Problem? . . . . . .
3.3.2 Problem Stru
ture . . . . . . . .
3.3.3 Denoting States and Transitions
3.3.4 Problem De
omposition . . . . .
3.3.5 A Review . . . . . . . . . . . . .
3.4 Rule of Sequential Composition . . . . .
3.5 Summary . . . . . . . . . . . . . . . . .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
i
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
22
22
22
25
26
27
28
28
29
31
34
35
38
January 7, 2007
ii
4 Games
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
6 Induction
6.1
6.2
6.3
6.4
6.5
Example Problems . .
Cutting The Plane . .
Triominoes . . . . . .
Looking For Patterns .
The Need For Proof .
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
41
41
43
43
44
45
48
52
53
55
56
57
59
62
62
63
63
64
64
65
66
68
69
70
72
73
74
76
79
80
83
85
85
89
92
93
95
January 7, 2007
iii
6.6 From Veri
ation to Constru
tion
6.7 Fake-Coin Dete
tion . . . . . . . .
6.7.1 Problem Formulation . . .
6.7.2 Problem Solution . . . . . .
6.8 Summary . . . . . . . . . . . . . .
6.9 Bibliographi
Notes . . . . . . . .
7 The Towers of Hanoi
8 Knights Circuit
8.1
8.2
8.3
8.4
8.5
8.6
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
95
99
100
101
104
105
107
107
107
108
109
109
113
115
116
117
117
121
125
130
132
132
Solutions to Exercises
133
Bibliography
153
January 7, 2007
iv
January 7, 2007
Chapter 1
Introduction
In histori
al terms, the digital
omputer is very, very new. The s
ien
e of
omputing is
yet newer. Compared to its older sister |mathemati
s| whi
h is thousands of years
old, it is hardly in the embryoni
stage of development. Yet,
omputing s
ien
e is already
having a major in
uen
e on our problem-solving skills, amounting to a revolution in the
art of ee
tive reasoning.
Be
ause of the
hallenges of programming (whi
h means instru
ting a dumb ma
hine
how to solve ea
h instan
e of a problem) and the unpre
edented s
ale of programming
problems,
omputing s
ientists have had to hone their problem-solving skills to a very
ne degree. This has led to advan
es in logi
, and to
hanges in the way that mathemati
s
is pra
tised. These le
tures form an introdu
tion to problem-solving using the insights
that have been gained in
omputing s
ien
e.
1.1
Algorithms
Solutions to programming problems are formulated as so-
alled algorithms. An algorithm is a well-dened pro
edure,
onsisting of a number of instru
tions, that are
exe
uted in turn in order to solve the given problem.
A
on
rete example may help to understand better the nature of algorithms and their
relation to problem solving. Consider the following problem, whi
h is typi
al of some of
the exer
ises we dis
uss. You may want to ta
kle the problem before reading further.
Four people wish to
ross a bridge. It is dark, and it is ne
essary to use a tor
h when
rossing the bridge, but they only have one tor
h between them. The bridge is narrow
and only two people
an be on it at any one time. The four people take dierent amounts
of time to
ross the bridge; when two
ross together they must pro
eed at the speed of
the slowest. The rst person takes 1 minute to
ross, the se
ond 2 minutes, the third
5 minutes and the fourth 10 minutes. The tor
h must be ferried ba
k and forth a
ross
January 7, 2007
1. Introdu tion
January 7, 2007
1.2
Bibliographic Remarks
January 7, 2007
1. Introdu tion
January 7, 2007
Chapter 2
Invariants
\Invariant" means \not
hanging". An invariant of some pro
ess is thus some attribute or
property of the pro
ess that doesn't
hange. Other names for \invariant" are \
onstant",
\law" and \pattern".
The re
ognition of invariants is an important problem-solving skill, possibly the most
important. This
hapter introdu
es the notion of an invariant, and dis
usses a number
of examples of its use.
We begin as we mean to go on. We rst present a number of problems for you to
ta
kle. Some you may nd easy, but others you may nd di
ult or even impossible to
solve. If you
an't solve one, move on to the next. To gain full benet, however, it is
important that you try the problems rst, before reading further.
We then return to ea
h of the problems individually. The rst problem we dis
uss
in detail, showing how an invariant is used to solve the problem. Along the way, we
introdu
e some basi
skills related to
omputer programming | the use of assignment
statements, and how to reason about assignments. The se
ond problem, whi
h otherwise
would be quite hard, is now straightforward. We leave it to you to solve, but, be
ause the
te
hniques are new, we suggest a sequen
e of steps whi
h lead dire
tly to the solution.
The third problem is quite easy, but involves a new
on
ept, whi
h we dis
uss in detail.
Then, it is your turn again. From a proper understanding of the solution to these initial
problems, you should be able to solve the next
ouple of problems. This pro
ess is
repeated as the problems get harder; we demonstrate how to solve one problem, and
then leave you to solve some more. You should nd them mu
h easier to solve.
1. Chocolate Bars.
A re
tangular
ho
olate bar is divided into squares by horizontal and verti
al
grooves, in the usual way. It is to be
ut into individual squares. A
ut is made by
taking a single pie
e and
utting along one of the grooves. (Thus ea
h
ut splits
one pie
e into two pie
es.)
January 7, 2007
2. Invariants
Figure 2.1 shows a 43
ho
olate bar that has been
ut into ve pie
es. The
uts
are indi
ated by solid lines.
January 7, 2007
7
to redu
e the number of balls in the urn to one by repeating the following pro
ess
as often as ne
essary.
Take any two balls out of the urn. If both have the same
olour, throw them away,
but put another bla
k ball into the urn; if they have dierent
olours then return
the white one to the urn and throw the bla
k one away.
Ea
h exe
ution of the above pro
ess redu
es the number of balls in the urn by one;
when only one ball is left the game is over. What, if anything,
an be said about
the
olour of the nal ball in the urn in relation to the original number of bla
k
balls and white balls?
5. Dominoes
A
hess board has had its top-right and bottom-left squares removed so that there
are 62 squares remaining. (See g. 2.3.) An unlimited supply of dominoes has
January 7, 2007
2. Invariants
2.1
Chocolate Bars
2.1.1
The Solution
January 7, 2007
We
on
lude that to
ut the
ho
olate bar into all its individual pie
es, the number
of
uts needed is one less than the number of pie
es.
2.1.2
On
e the skill of identifying invariants has been mastered, this is an easy problem to solve.
For this reason, we have used English to des
ribe the solution, rather than formulate the
solution in a mathemati
al notation. For more
omplex problems, mathemati
al notation
helps
onsiderably, be
ause it is more su
in
t and more pre
ise. Let us use this problem
to illustrate what we mean.
Abstraction The mathemati
al solution begins by introdu
ing two variables. We let
variable p
ount the number of pie
es, and we let variable c
ount the number of
uts.
The values of these variables des
ribe the state of the
ho
olate bar.
This rst step is
alled abstra
tion. We \abstra
t" from the problem a
olle
tion
of variables (or \parameters") that
ompletely
hara
terise the essential elements of the
problem. In this step, inessential details are eliminated.
One of the inessential details is that the problem has anything to do with
ho
olate
bars! This is totally irrelevant and, a
ordingly, has been eliminated. The problem
ould equally well have been about
utting postage stamps from a sheet of stamps.
The problem has be
ome a \mathemati
al" problem, be
ause it is about properties of
numbers, rather than a \real-world" problem. Real-world problems are very hard, if not
impossible, to solve; in
ontrast, problems that su
umb to mathemati
al analysis are
relatively easy.
Other inessential details that have been eliminated are the sequen
e of
uts that have
been made, and the shapes and sizes of the resulting pie
es. That is, the variables p
and c do not
ompletely
hara
terise the state of the
ho
olate bar, or the sequen
e of
uts that have been made to rea
h that state. Knowing that, say, four
uts have been
made, making ve pie
es, does not allow us to re
onstru
t the sizes of the individual
pie
es. That is irrelevant to solving the problem.
The abstra
tion step is often the hardest step to make. It is very easy to fall into
the trap of in
luding unne
essary detail, making the problem and its solution over
ompli
ated. Conversely, de
iding what is essential is far from easy |there is no algorithm for doing this!| . The best problem-solvers are probably the ones most skilled in
abstra
tion.
(Texts on problem-solving often advise drawing a gure. This may help to
larify
the problem statement |for example, we in
luded g. 2.1 in order to
larify what is
meant by a
ut| but it
an also be a handi
ap! There are two reasons. The rst is that
extreme
ases are often di
ult to
apture in a gure. This is something we return to
January 7, 2007
10
2. Invariants
later. The se
ond is that gures often
ontain mu
h unne
essary detail, as exemplied
by g. 2.1. Our advi
e is to use gures with the utmost
aution; mathemati
al formulae
are most often far more ee
tive.)
The next step in the problem's solution is to model the pro
ess of
utting
the
ho
olate bar. We do so by means of the assignment statement
Assignments
p , c := p+1 , c+1 .
An assignment statement has two sides, a left side and a right side. The two sides are
separated by the assignment symbol \ := ", pronoun
ed \be
omes". The left side is a
omma-separated list of variables (in this
ase, p , c ). No variable may o
ur more than
on
e in the left side. The right side is a
omma-separated list of expressions (in this
ase,
p+1 , c+1 ). The list must have length equal to the number of variables on the left side.
An assignment ee
ts a
hange of state. An assignment is exe
uted by evaluating,
in the
urrent state, ea
h expression on the right side. The state is then
hanged by
repla
ing the value of ea
h variable on the left side by the value of the
orresponding
expression on the right side. In our example, the state |the number of pie
es and
the number of
uts| is
hanged by evaluating p+1 and c+1 , and then repla
ing the
values of p and c by these values, respe
tively. In words, p \be
omes" p+1 , and c
\be
omes" c+1 . This is how the assignment statement models the pro
ess of making a
single
ut of the
ho
olate bar1 .
An invariant of an assignment is some fun
tion of the state whose value remains
onstant under exe
ution of the assignment. For example, pc is an invariant of the
assignment p , c := p+1 , c+1 .
Suppose E is an expression depending on the values of the state variables. (For
example, pc is an expression depending on variables p and c .) We
an
he
k that E
is an invariant simply by
he
king for equality between the value of E , and the value of
E after repla
ing all variables as pres
ribed by the assignment. For example, the equality
1A
word of warning (for those who have already learnt to program in a language like Java or C):
The assignment statements we will be using are often
alled simultaneous assignments be
ause several
variables are allowed on the left side, their values being updated simultaneously on
e the right side has
been evaluated. Some programming languages do not allow simultaneous assignments, restri
ting the
programmer to a single variable on the left side in all assignments. Java is an example. Instead of a
simultanous assignment, one has to write a sequen
e of assignments. This is a nuisan
e, but only that.
Mu
h worse is that the equality symbol, \ = ", is used instead of the assignment symbol, Java being again
an example. This is a major problem be
ause it
auses
onfusion between assignments and equalities,
whi
h are two quite dierent things. Most novi
e programmers frequently make the mistake of
onfusing
the two, and even experien
ed programmers sometimes do, leading to di
ult-to-nd errors. If you do
write Java or C programs, always remember to pronoun
e an assignment as \left side becomes right side",
and not \left side equals right side", even if your tea
hers do not do so.
January 7, 2007
11
Simple algebra shows that this holds. So, in
reasing m by 3 , simulaneously de
reasing
n by 1 , does not
hange the value of m + 3n .
Given an expression, E , and an assignment, ls := rs ,
E[ls := rs]
is used to denote the expression obtained by repla
ing all o
urren
es of the variables in
E listed in ls by the
orresponding expression in the list of expressions rs . Here are
some examples:
(pc)[p , c := p+1 , c+1] = (p+1) (c+1)
E[ls := rs] = E .
January 7, 2007
12
2. Invariants
Induction The nal step in the solution of the
ho
olate problem is to exploit the
invarian
e of pc .
Initially, p = 1 and c = 0 . So, initially, pc = 1 . But, pc is invariant. So, pc = 1
no matter how many
uts have been made. When the bar has been
ut into all its squares,
p = s , where s is the number of squares. So, at that time, the number of
uts, c , satises
sc = 1 . That is, c = s1 . The number of
uts is one less than the number of squares.
An important prin
iple is being used here,
alled the prin
iple of mathemati
al
indu
tion. The prin
iple is very simple. It is that, if the value of an expression is un
hanged by some assignment to its variables, the value will be un
hanged no matter how
many times the assignment is applied. That is, if the assignment is applied zero times,
the value of the expression is un
hanged (obviously, be
ause applying the assignment
zero times means doing nothing). If the assignment is applied exa
tly on
e, the value
of the expression is un
hanged, by assumption. Applying the assignment twi
e means
applying it on
e and then on
e again. Both times, the value of the expression remains
un
hanged, so the end result is also no
hange. And so on, for three times, four times,
et
.
Note that the
ase of zero times is in
luded here. It is very important not to forget
zero. In the
ase of the
ho
olate-bar problem, it is vital to solving the problem in the
ase that the
ho
olate bar has exa
tly one square (in whi
h
ase zero
uts are required).
This
ompletes our dis
ussion of the
ho
olate-bar problem. A number of
important problem-solving prin
iples have been introdu
ed | abstra
tion, invariants
and indu
tion. We will see these prin
iples again and again throughout these le
tures.
Summary
2.2
Empty Boxes
January 7, 2007
13
unknown number of the medium boxes is sele
ted and into ea
h eight small
boxes are pla
ed.
At the end of this pro
ess there are 102 empty boxes. How many boxes
are there in total?
The following steps should help in determining the solution.
1. Introdu
e the variables e and f for the number of empty and the number of full
boxes, respe
tively.
2. Identify the initial values of e and f . Identify the nal value of e .
3. Model the pro
ess of putting eight boxes inside a box as an assignment to e and
f.
4. Identify an invariant of the assignment.
5. Combine the previous steps to dedu
e the nal value of f . Hen
e dedu
e the nal
value of e+f .
Note that this solution does not try to
ount the number of medium boxes, or the
number of small boxes, or whi
h are full and whi
h are empty. All of these are irrelevant,
and a solution that introdu
es variables representing these quantities is grossly over
ompli
ated.
This is a key to ee
tive problem-solving: keep it simple!
2.3
Let us now look at how to solve the tumbler problem. Re
all the statement of the
problem.
Several tumblers are pla
ed in a line on a table. Some tumblers are upside
down, some are the right way up. It is required to turn all the tumblers the
right way up. However, the tumblers may not be turned individually; an
allowed move is to turn any two tumblers simultaneously. From whi
h initial
states of the tumblers is it possible to turn all the tumblers the right way up?
The problem suggests that we introdu
e just one variable that
ounts the number of
tumblers that are upside down. Let us
all it u .
There are three possible ee
ts of turning two of the tumblers. Two tumblers that
are both the right way up are turned upside down. This is modelled by the assignment
January 7, 2007
14
2. Invariants
u := u+2 .
Turning two tumblers that are both upside down has the opposite ee
t | u de
reases
by two. This is modelled by the assignment
u := u2 .
Finally, turning two tumblers that are the opposite way up (that is, one upside down,
the other the right way up) has no ee
t on u . In programming terms, this is modelled
by a so-
alled skip statement. \Skip" means \do nothing" or \having no ee
t". In this
example, it is equivalent to the assignment
u := u ,
but it is better to have a name for the statement that does not depend on any variables.
We use the name skip . So, the third possibility is to exe
ute
skip .
The
hoi
e of whi
h of these three statements is exe
uted is left unspe
ied. An invariant
of the turning pro
ess must therefore be an invariant of ea
h of the three.
Everything is an invariant of skip . So, we
an dis
ount skip . We therefore seek an
invariant of the two assignments u := u+2 and u := u2 . What does not
hange
if we add or subtra
t two from u ?
The answer is: the so-
alled parity of u . The parity of u is a boolean value: it is
either true or false . It is true if u is even (zero, two, four, eight et
.) and it is false if
u is odd (one, three, ve, seven, et
.). Let us write even.u for this Boolean quantity.
Then,
(even.u)[u := u+2] = even.(u+2) = even.u .
January 7, 2007
15
2.4. Tetrominoes
You should now be in a position to solve the problem of the bla
k and white balls
(problem 4 in the introdu
tory se
tion). Apply the method of introdu
ing appropriate
variables to des
ribe the state of the balls in the urn. Then express the pro
ess of
removing and/or repla
ing balls by a
hoi
e among a number of assignment statements.
Identify an invariant, and draw the appropriate
on
lusion. The
hessboard problem is a
little harder, but
an be solved in the same way. (Hint: use the
olouring of the squares
on the
hessboard.) Problem 6(a) should be a bit easier. It's a preliminary to solving
6(b), whi
h we do |together with 6(a)| in the next se
tion. Have a peek if you want
to.
2.4
Tetrominoes
In this se
tion, we present the solution of problem 6(b). This gives us the opportunity
to introdu
e a style of mathemati
al
al
ulation that improves
larity.
Re
all the problem.
Suppose a re
tangular board
an be
overed with T-tetrominoes. Show that
the number of squares is a multiple of 8 .
A brief analysis of this problem reveals an obvious invariant. Suppose c denotes the
number of
overed squares. Then, pla
ing a tetromino on the board is modelled by
c := c+4 .
January 7, 2007
16
2. Invariants
\di
ult" problem is one that involves putting together the solution to several simple
problems. Looked at this way, \di
ult" problems be
ome a lot more manageable. Just
keep on solving simple problems until you have rea
hed your goal!
At this point, we want to introdu
e a style for presenting
al
ulations that is
learer
than the normal mixture of text with interspersed mathemati
al expresssions. To introdu
e the style we repeat the argument just given. Here it is in the new style:
an mn board is
overed with tetrominoes
{
invariant: c is a multiple of 4 ,
c = mn
mn is a multiple of 4
{
property of multiples }
m is a multiple of 2 n is a multiple of 2 .
This is a two-step
al
ulation. The rst step is a so-
alled \impli
ation" step, as indi
ated
by the \ " symbol. The step is read as
If an
January 7, 2007
17
2.4. Tetrominoes
sin e
January 7, 2007
18
2. Invariants
(b 3d l)[d , b , w := d+1 , b+3 , w+1]
=
denition of substitution }
(b+3) 3(d+1) l
=
arithmeti }
b 3d l
and
(b 3d l)[l , b , w := l+1 , b+1 , w+3]
=
denition of substitution }
(b+1) 3d (l+1)
=
arithmeti }
b 3d l .
Now, the initial value of b 3d l is zero. So, it will always be zero, no matter how
many T-tetrominoes are pla
ed on the board. Similarly, the value of w 3l d will
always be zero.
We
an now solve the given problem.
a re
tangular board is
overed by T-tetrominoes
b=w
b 3d l = 0
w 3l d = 0
(b = w) (3d + l = 3l + d)
arithmeti }
(b = w) (l = d)
b 3d l = 0
January 7, 2007
19
b = w = 4d = 4l
{
arithmeti }
b+w = 8d
{
2.5
Additional Exercises
Given is a bag of three kinds of obje
ts. The total number of obje
ts is
redu
ed by repeatedly removing two obje
ts of dierent kind, and repla
ing them by an
obje
t of the third kind.
Identify exa
t
onditions in whi
h it is possible to remove all the obje
ts ex
ept
one.
Exercise 2.2
2.6
Bibliographic Remarks
The empty-box problem was given to me by Wim Feijen. The problems of the bla
k and
white balls is from [Gri81. The tetromino problems I found in the 1999 Vierkant Voor
January 7, 2007
20
2. Invariants
Voor Wiskunde |foursquare for mathemati
s| is a foundation that promotes mathemati
s in Dut
h s
hools. Their publi
ations
ontain many examples of mathemati
al
puzzles, both new and old. I have made grateful use of them throughout this text. The
domino and tumbler problems are old
hestnuts. I do not know their origin.
Exer
ise 2.2 was posed to me by Dmitri Chubarov. It was posed (in a slightly dierent
form) in the Russian national Mathemati
s Olympiad in 1975 and appears in a book
by Vasiliev entitled \Zada
hi Vsesoyuzynykh Matemti
heskikh Olympiad" published in
Mos
ow, 1988. The author of the problem is apparently not stated.
January 7, 2007
Chapter 3
Crossing a River
The examples in this
hapter all involve getting a number of people or things a
ross
a river under
ertain
onstraints. We use them as simple illustrations of \brute-for
e"
sear
h and problem de
omposition.
Brute-for
e sear
h means systemati
ally trying all possibilities. It's a te
hnique that
doesn't require any skill, but does require a lot of
areful and a
urate work. Using brute
for
e is not something human beings are good at; lots of
areful, a
urate work is something more suited to
omputers. But, brute for
e isn't even pra
ti
al for implementation
on a
omputer. The amount of work involved explodes as the problem size gets bigger,
making it impra
ti
al for all but toy problems. Nevertheless, it is useful to know what
brute for
e entails, be
ause it helps to understand the nature of problem-solving.
Problem de
omposition is something we humans are mu
h better at. Problem de
omposition involves exploiting the stru
ture of a problem to break it down into smaller,
more manageable problems. On
e a problem has been broken down in this way, brute
for
e
an be applied. Indeed, it is often the
ase that, ultimately, brute for
e is the only
solution method, so we
an't dispense with it. However, it is mu
h better to spend more
eort in de
omposing a problem, postponing the use of a brute-for
e sear
h for as long
as possible.
All river-
rossing problems have an obvious stru
tural property, namely the symmetry
between the two banks of the river. The exploitation of symmetry is a very important
problem-solving te
hnique, but is often overlooked, parti
ularly when using brute for
e.
You may already have seen the problems, or similar ones, elsewhere. As illustrations of
brute-for
e sear
h |whi
h is how their solutions are often presented| they are extremely
uninteresting! However, as illustrations of the use of symmetry,
ombined with problem
de
omposition, they have startling, hidden beauty.
An important issue that emerges in this
hapter is naming the elements of a problem.
De
iding on what and how names should be introdu
ed
an be
ru
ial to su
ess. We
shall see how inappropriate or unne
essary naming
an in
rease the
omplexity of a
21
January 7, 2007
22
3. Crossing a River
problem, making it impossible to solve even with the aid of a very powerful omputer.
3.1
Problems
3.2
3.2.1
Brute Force
Goat, Cabbage and Wolf
The goat-,
abbage- and wolf-problem is often used to illustrate brute-for
e sear
h. Our
main purpose in showing the brute-for
e solution is to illustrate the pitfalls of poor
January 7, 2007
23
January 7, 2007
24
3. Crossing a River
f = g = c g 6= c .
That is, either the farmer, the goat and the
abbage are all on the same bank ( f = g = c ),
or the goat and
abbage are on dierent banks ( g 6= c ). This ex
ludes
ases where g
and c are equal, but dierent from f . Similarly, the requirement that the goat
annot
be left alone with the wolf is expressed by the system invariant
f = g = w g 6= w .
If we list all states, eliminating the ones that are not allowed, the total redu
es to ten.
The table below shows the ten dierent
ombinations. (Noti
e that when f and g are
equal all
ombinations of c and w are allowed; when f and g are dierent, c and w
are required to be equal.)
f
L
L
L
L
L
R
R
R
R
R
L
L
L
L
R
L
R
R
R
R
L
L
R
R
L
R
L
L
R
R
L
R
L
R
L
R
L
R
L
R
Now, we enumerate all the possible transitions between these states. The graph in
g. 3.1 does just this. The nodes of the graph |the boxes| represent states, and the
edges of the graph |the lines
onne
ting the boxes| represent transitions. There are
no arrows on the edges be
ause ea
h transition
an be reversed.
At the very left, the box labelled \LLLL" represents the state where all four are on
the left bank. The only allowed transition from this state is to the state where the farmer
and goat are at the right bank, and the
abbage and wolf are at the left bank. This is
represented by the line
onne
ting the \LLLL" box to the \RRLL" box.
From the graph, it is
lear that there are two solutions to the problem. Ea
h solution
is given by a path through the graph from \LLLL" box to the \RRRR" box. The upper
path gives the following solution:
1. The farmer takes the goat to the right bank, and returns alone. This is the path
from LLLL to LRLL.
January 7, 2007
25
RRRL
LLLL
RRLL
LLRL
LRLL
RLRR
RRLR
LLRR
RRRR
LLLR
3.2.2
State-Space Explosion
There is often a tenden
y to apply brute for
e without thinking when fa
ed with a new
problem. However, it should only be used where it is unavoidable. Brute for
e is only
useful for very simple problems. For other problems, the sear
h spa
e qui
kly be
omes
mu
h too large. In the jargon used by
omputing s
ientists, brute for
e does not \s
ale
up" to larger problems. The goat-,
abbage- and wolf-problem is not representative; the
above |thoughtless!| solution has a manageable number of states, and a manageable
number of transitions.
We
an see how qui
kly the sear
h spa
e
an grow by analysing what is involved in
using brute for
e to solve the remaining problems in se
tion 3.1.
In the \overweight" problem, there are four named individuals and no restri
tions
on their being together on the same side of the bank. So, there are 16 possible states;
unlike in the goat-,
abbage- and wolf-problem, no restri
tion on the size of the state
spa
e is possible. Also, from the initial state there are four dierent transitions; from
most other states, there are at least two transitions. So, the total number of transitions
is large, too large even for the most diligent problem-solvers.
The situation in an unskilled solution of the \jealous-
ouples" problem is even worse.
Here, there are six individuals involved, ea
h of whom
an be on one side or other of the
January 7, 2007
26
3. Crossing a River
river bank. If we give ea
h individual a distin
t name, the number of states is 26 , i.e.
64 ! That's an impossible number for any human being to
ope with, and we haven't even
begun to
ount the number of transitions. In another variation on the jealous-
ouples
problem, there are ve
ouples, and the boat
an take three people at a time. That
means, if all are named, there are are 210 , i.e. 1024 , dierent states, and a yet larger
number of transitions. Take note: these are \toy" problems, not real problems.
The \adults-and-
hildren" problem illustrates another failing of brute for
e, namely
that it
an only be applied in spe
i
ases, and not in the general
ase. The number of
adults and
hildren is not spe
ied in this problem. Yet, it is in fa
t the easiest of all to
solve.
The use of a
omputer to perform a brute-for
e sear
h shifts the meaning of what is
a \small" problem and what is a \large" problem, but not as mu
h as one might expe
t.
The so-
alled \state-explosion problem" gets in the way. The river-
rossing problems
illustrate \state explosion" very well. If there are n individuals in su
h a problem,
there are, in prin
iple, 2n dierent states to be
onsidered. But, even for quite small
n , 2n is a very large number. We speak of an \exponential" growth in the number of
states ( n is the exponent in 2n ). Whenever the state spa
e of a
lass of problems grows
exponentially, it means that even the largest and fastest super
omputers
an only ta
kle
quite small instan
es.
Drawing state-transition diagrams is equally inee
tive. A diagram
an o
asionally
be used to illustrate the solution of a simple, well-
hosen problem. But
onstru
ting
a diagram is rarely helpful in problem-solving. Instead, diagrams qui
kly be
ome a
problem in themselves | apart from the size of paper needed, how are the nodes to be
pla
ed on the paper so that the diagram be
omes readable?
3.2.3
Abstraction
January 7, 2007
27
the goat an \alpha" and the
abbage and the wolf \betas".
A farmer wishes to ferry an alpha and two betas a
ross a river. However,
his boat is only large enough to take one of them at a time, making several
trips a
ross the river ne
essary. Also, an alpha should not be left alone with
a beta.
How
an the farmer a
hieve the task?
Now the problem be
omes mu
h easier to solve. Indeed, there is only one solution:
Take the alpha a
ross, and then one beta a
ross, returning with the alpha. Then take
the se
ond beta a
ross, followed by the alpha. Be
ause there is only one solution, it is
easy to dis
over, and it is unne
essary to
onstru
t a state-transition diagram for the
problem.
The problem-solving prin
iple that we learn from this example is very important.
Avoid unnecessary or inappropriate naming.
When elements of a problem are given individual names, it distinguishes them from other
elements of the problem, and adds to the size of the state spa
e. The pro
ess of omitting
unne
essary detail, and redu
ing a problem to its essentials is
alled abstra
tion. Poor
solutions to problems are ones that fail to \abstra
t" adequately, making the problem
more
ompli
ated than it really is. We en
ounter the importan
e of appropriate naming
time and again in the
oming
hapters. Bear it in mind as you read.
3.3
Jealous Couples
Very often, a problem has an inherent stru
ture that fa
ilitates de
omposing the problem
into smaller problems. The smaller problems
an then be further de
omposed until they
be
ome su
iently manageable to be solvable by other means, perhaps even by brute
for
e. Their solutions are then put together to form a solution to the original problem.
The jealous-
ouples problem is an ex
ellent example. It
an be solved by brute for
e,
making it de
idedly boring. But, it
an be solved mu
h more ee
tively, making use of
general problem-solving prin
iples.
Re
all its statement:
Three
ouples (husband and wife) wish to
ross a river. They have one
boat that
an
arry at most two people, making several trips a
ross the river
ne
essary. The husbands are so jealous of ea
h other that none is willing to
allow their wife to be with another man if they are not themselves present.
How
an all three
ouples get a
ross the river?
January 7, 2007
28
3.3.1
3. Crossing a River
Before we ta
kle this parti
ular problem, let us try to determine what the essen
e of the
problem is.
Suppose there is one boat that
an
arry two \things", and there are no other restri
tions. Then,
learly, it is possible to get any number of \things" a
ross the river: repeat
the pro
ess of letting two
ross from left to right, followed by one returning from right
to left, until at most two remain on the left bank.
Now, by repla
ing \thing" by \
ouple", we infer that a boat that
an
arry two
ouples at one
rossing
an be used to ferry an arbitrary number of
ouples a
ross the
river. (After all,
ouples are not jealous of ea
h other! ) Sin
e a
ouple is two people, this
means that a boat that
an
arry four people is su
ient to ferry an arbitrary number
of
ouples a
ross the river.
This simple analysis gives us a dierent slant on the problem. Rather than ta
kle
the problem as stated, we
an ta
kle a related problem, namely, what is the minimum
apa
ity needed to ferry three
ouples a
ross the river? More generally, what is the
minimum
apa
ity needed to ferry n
ouples a
ross the river? Obviously, the minimum
apa
ity is at least two (sin
e it is not possible to ferry more than one person a
ross a
river in a boat that
an only
arry one person at a time), and we have just shown that
it is at most four.
Alternatively, we
an spe
ify the
apa
ity of the boat and ask what is the maximum
number of
ouples that
an be ferried a
ross with that
apa
ity. If the
apa
ity is one
(or less) the maximum number of
ouples is zero, and if the
apa
ity is four, there is no
maximum. So, the question is how many
ouples
an be ferried with a boat of
apa
ity
two, and how many
ouples
an be ferried with a boat of
apa
ity three.
The new problems look more di
ult than the original. In the original problem, we
are given the answer |in the
ase of three
ouples, a boat with
apa
ity two is needed|
and we are required to give a
onstru
tive proof that this is the
ase. But, there is often
an advantage in not knowing the answer | be
ause we
an sometimes gain insight by
generalising, and then rst solving simpler instan
es of the general problem.
3.3.2
Problem Structure
The stru
ture of this problem suggests several ways in whi
h it might be de
omposed.
First, there are three
ouples. This suggests seeking a solution that gets ea
h
ouple
a
ross in turn. That is, we de
ompose the problem into three subproblems: get the rst
ouple a
ross, get the se
ond
ouple a
ross, and get the third
ouple a
ross.
Another de
omposition is into husbands and wives. A
ording to the maxim \ladies
before gentlemen", we
ould try rst getting all the wives a
ross, followed by all the
January 7, 2007
29
husbands. Alternatively, letting \age go before beauty", we
ould try rst getting all the
husbands a
ross, followed by all the wives.
Getting all the wives a
ross, whilst their husbands remain at the left bank turns out
to be easy. The reason is that, if the husbands all stay in one pla
e, there is no di
ulty
in transferring the wives away from them. Getting all the husbands a
ross rst, whilst
their wives stay at the left bank, seems mu
h harder. On the other hand, getting the
husbands to join their wives may prove to be harder than getting the wives to join their
husbands. Ladies before gentleman, or age before beauty; there doesn't seem mu
h to
hoose between them.
There is, however, one key stru
tural property of the problem that we have not yet
onsidered. It is the symmetry between the left and right banks. The pro
ess of getting
a group of people from left to right
an always be reversed; the result is a pro
ess for
getting the same group of people from right to left. Perhaps a symmetri
solution is
possible! If that is the
ase, we only need to do half the work, and that is a major
saving. This is indeed what we do.
(The state-transition diagram for the goat-,
abbage-, wolf-problem exhibits the leftright symmetry very well. The diagram also illustrates the symmetry between the
abbage and wolf. Both symmetries were to be expe
ted from the problem statement; by
ignoring them and using brute-for
e, we lost the opportunity of a redu
tion in eort.)
3.3.3
We begin by introdu
ing some lo
al notation to make the solution strategy pre
ise. The
introdu
tion of notation involves naming the elements of the problem that we want to
distinguish. As dis
ussed earlier, this is a
ru
ial step in nding a solution.
Here, we use letters H , W and C to mean husband, wife and
ouple, respe
tively.
These are pre
eded by a number; for example, 2H means two husbands, 3C means
three
ouples and 1C,2H means one
ouple and two husbands. We exploit the notation
to distinguish between
ouples and individuals; for example, 1H,1W means a husband
and wife who do not form a
ouple, whilst 1C means a husband and wife who do form
a
ouple.
Note that we do not name the individual people as in, for example, Ann, Bob,
Clare et
. It is only the number of husbands, wives and
ouples that is relevant to the
problem's solution. Number is an extremely important mathemati
al abstra
tion.
We distinguish between states and a
tions.
A state des
ribes a situation when ea
h individual (husband or wife) is at one of the
banks. A state is denoted by two sequen
es separated by bars. An example is 3H || 3W ,
whi
h denotes the state in whi
h all three husbands are at the left bank, and all three
wives are at the right bank. A se
ond example of a state is 1C,2H || 2W , whi
h denotes
January 7, 2007
30
3. Crossing a River
the state in whi
h one
ouple and two husbands are at the left bank and two wives are at
the right bank. The starting state is thus 3C || and the required nishing state is || 3C .
An a
tion is when some individuals are being transported a
ross the river. An
example is 3H |2W| 1W ; this denotes the a
tion of transporting two wives a
ross the
river, leaving three husbands at the left bank and one wife at the right bank.
Note that the notation for states and a
tions does not spe
ify the position or dire
tion
of the boat, and, taken out of
ontext,
ould be ambiguous. Sin
e the position of the
boat must alternate between the left bank and the right bank, this ambiguity is easily
resolved.
The notation allows valid and invalid states/a
tions to be easily identied. For example, 1C,1W || 1C,1H is invalid (be
ause there is a wife who is on the same side of
the river as a man other than her husband, who is on the other side of the river). Also,
3H |3W| is invalid be
ause the boat
an only
arry at most two people.
In general, a
omplete, detailed solution to the problem is a sequen
e, beginning
with the state 3C || and ending with the state || 3C , that alternates between states and
a
tions.
An a
tion results in a
hange of state. (In the terminology of state-transition diagrams, an a
tion ee
ts a transition between states.) Additional notation helps to express
the result of a
tions. If p and q denote states, and S denotes a sequen
e of a
tions,
{
p }
S
{
q }
2C,1H || 1W
3H |2W| 1W
{
3H || 3W
is the property that, beginning in the state where two
ouples and one husband are at
the left bank, letting two wives
ross will result in a state in whi
h all three husbands
are at the left bank, whilst all three wives are at the right bank.
Of
ourse, we should always
he
k the validity of su
h properties. It is easy to make
a mistake and make an invalid
laim. Care is needed, but the
he
ks are straightforward.
January 7, 2007
31
3.3.4
Problem Decomposition
Using this notation we
an express our strategy for de
omposing the problem. The goal
is to
onstru
t a sequen
e of a
tions S0 satisfying
{ 3C || }
S0 { || 3C } .
{ 3H || 3W } S2 { 3W || 3H } ,
{ 3W || 3H } S3 { || 3C } .
The sequen
e S1
hanges the state from the start state to the state where all the wives are
at the right bank and all the husbands are at the left bank. The sequen
e S2
hanges the
end state of S1 to the state where the positions of the wives and husbands are reversed.
Finally, the sequen
e S3
hanges the end state of S2 to the state where everyone is
at the right bank. So, doing S1 followed by S2 followed by S3 , whi
h we denote by
S1 ; S2 ; S3 , will a
hieve the obje
tive of
hanging the state from the start state (everyone
is at the left bank) to the nal state (everyone is at the right bank).
The de
omposition is into three
omponents be
ause we want to exploit symmetry,
but,
learly, an odd number of
rossings will be ne
essary. Symmetry is
aptured by
making the fun
tion of S3 entirely symmetri
al to the fun
tion of S1 . If we
onsider the
reverse of S3 , its task is to transfer all the wives from the right bank to the left bank.
So, if we
onstru
t S1 , it is a simple task to
onstru
t S3 dire
tly from it.
We now have to ta
kle the problem of
onstru
ting S1 and S2 .
As mentioned earlier, getting all the wives a
ross the river, leaving their husbands at
the left bank is easy. (It is a problem that
an be solved by brute for
e, if ne
essary.)
Here is how it is a
hieved.
January 7, 2007
32
3. Crossing a River
{
3C ||
1C,2H |2W|
;
1C,2H || 2W
1C,2H |1W| 1W
;
2C,1H || 1W
3H |2W| 1W
{
3H || 3W
} .
That is,
{ 3C || } 1C,2H |2W| ; 1C,2H |1W| 1W ; 3H |2W| 1W
{ 3H || 3W } .
3W || 3H
1W |2W| 3H
;
1W || 2C,1H }
1W |1W| 1C,2H
;
2W || 1C,2H }
|2W| 1C,2H
{
|| 3C
We are now fa
ed with the harder task of
onstru
ting S2 . We seek a solution that is
symmetri
al about the middle.
Note that, for S2 , the starting position of the boat is the right bank, and its nishing
position is the left bank. This is a requirement for S2 to follow S1 and be followed by
S3 . The length of S2 must also be odd.
Again, we look for a de
omposition into three subsequen
es. If the solution is to
remain symmetri
, it must surely take the following form:
{
3H || 3W
T1
;
1C |1C| 1C
T2
{
3W || 3H
} .
January 7, 2007
33
Note parti
ularly the middle a
tion | 1C |1C| 1C | . This may be a left-to-right
rossing, or a right-to-left
rossing; whi
h is not immediately
lear. The task is now to
onstru
t the symmetri
sequen
es of a
tions T1 and T2 .
If the middle a
tion is from right to left, the a
tion must be pre
eded by the state
1C || 2C and results in the state 2C || 1C . Vi
e-versa, if the middle a
tion is from left
to right, the a
tion must be pre
eded by the state 2C || 1C and results in the state
1C || 2C . There is little alternative but to use brute-for
e sear
h to try to determine
whi
h
an be a
hieved.
Fortunately, T1 is soon dis
overed. It
onsists of just two a
tions:
{
3H || 3W
3H |1W| 2W
;
1C,2H || 2W
1C |2H| 2W
{
1C || 2C }
2C || 1C }
2W |2H| 1C
;
2W || 1C,2H }
2W |1W| 3H
{
3W || 3H
} .
Finally, putting everything together, we have the
omplete solution to the jealous-
ouples
problem:
{
3C ||
3H || 3W
3H |1W| 2W ; 1C |2H| 2W
;
1C || 2C }
1C |1C| 1C
;
2C || 1C }
2W |2H| 1C ; 2W |1W| 3H
January 7, 2007
34
3. Crossing a River
;
3W || 3H
|| 3C
(In this solution, not all intermediate states are shown. This helps to do
ument the
solution, by re
ording the main steps, but not every step. Too mu
h detail in program
do
umentation
an be a hindran
e.)
3.3.5
A Review
Pause awhile to review the method used to solve the jealous-
ouples problem, so that
you
an fully appre
iate how mu
h more ee
tive it is than brute-for
e sear
h.
The
onstru
tion seeks at ea
h stage to exploit the symmetry between the left and
right banks. Sin
e the number of
rossings will inevitably be odd, ea
h de
omposition is into three subsequen
es, where the rst and last are \mirror images" in some
sense. Naming the unknown sequen
es, and formally spe
ifying their fun
tion using the
{ p } S { q } notation helps to
larify what has to be a
hieved, and to avoid error.
The nal solution involves eleven
rossings. That's too many for anyone to
ommit
to memory. But, be
ause the solution method is well stru
tured, it is easy to remember,
making a re
onstru
tion of the solution very simple. Moreover, the solution to the
problem
annot be used in other
ontexts, but the solution method
an. For the proof
of the pudding, solve the following related problem:
There are ve jealous
ouples, and their boat
an
arry a maximum of three individuals. Determine how to transport all the
ouples
a
ross the river.
Exercise 3.1 (Five-couple Problem)
January 7, 2007
35
(In general, individual solutions need not be symmetri
, but the set of solutions
is symmetri
. That is, there is a transformation from solutions to solutions based on
reversing left and right. A solution is symmetri
if this transformation maps the solution
to itself.)
3.4
The { p } S { q } notation we used for solving the jealous-
ouples problem is the
notation used for spe
ifying and
onstru
ting
omputer programs. It is
alled a Hoare
triple. (Sir Tony Hoare is a British
omputing s
ientist who pioneered te
hniques for
formally verifying the
orre
tness of
omputer programs; he was one of the rst to use
the notation.)
A
omputer program is spe
ied by a relation between the input values and the output
values. The allowed input values are spe
ied by a so-
alled pre
ondition, p , and the
output values are spe
ied by a post
ondition , q . Pre
onditions and post
onditions
are properties of the program variables.
If S is a program, and p and q are properties of the program variables,
fpgSfqg
means that, if the program variables satisfy property p before exe
ution of statement
S , exe
ution of S is guaranteed to terminate and, afterwards, the program variables will
satisfy property q . For example, a program to
ompute the remainder r and dividend
d after dividing number M by number N would have pre
ondition
N 6= 0 ,
January 7, 2007
36
3. Crossing a River
Programs are often
omposed by sequen
ing; the individual
omponents are exe
uted one
after the other. A semi
olon is usually used to denote sequen
ing. Thus, if S1 , S2 and
S3 are programs, S1 ; S2 ; S3 denotes the program that is exe
uted by rst exe
uting S1 ,
then exe
uting S2 , and then exe
uting S3 . This is
alled the sequential
omposition
of S1 , S2 and S3 .
A sequential
omposition is introdu
ed into a program when the problem it solves
is de
omposed into subproblems. In the
ase of a de
omposition into two
omponents,
given a pre
ondition p and a post
ondition q , an intermediate
ondition, r say, is
invented. The problem of
onstru
ting a program S satisfying the spe
i
ation
fpgSfqg
is then resolved by letting S be S1 ; S2 and
onstru
ting S1 and S2 to satisfy the
spe
i
ations
fpgS frg
1
and
frgS fqg
2
The intermediate
ondition r thus a
ts as post
ondition for S1 and pre
ondition for S2 .
If the problem is de
omposed into three subproblems, two intermediate
onditions are
needed. This is what we did in solving the jealous-
ouples problem. The initial problem
statement has pre
ondition 3C || and post
ondition || 3C . The intermediate
onditions
3H || 3W and 3W || 3H were then introdu
ed in order to make the rst de
omposition.
There are dierent ways of using the rule of sequential
omposition. The stru
ture of
the given problem may suggest an appropriate intermediate
ondition. Alternatively, the
problem may suggest an appropriate initial
omputation S1 ; the task is then to identify
the intermediate
ondition and the nal
omputation S2 . Conversely, the problem may
suggest an appropriate nal
omputation S2 ; then the task be
omes one of identifying
the intermediate
ondition r and the initial
omputation S1 .
A
on
rete illustration is the bridge problem posed in
hapter 1. Re
all its statement:
January 7, 2007
37
as one step. Seeking to exploit symmetry, the task be
omes one of determining sequen
es
of
rossings S1 and S2 , and p and q su
h that
{ 1,2,5,10 || } S1 { p,5,10 || q }
{ p,5,10 || q } p |5,10| q { p || q,5,10 }
and
{ p || q,5,10 } S2 { || 1,2,5,10 } .
January 7, 2007
38
3. Crossing a River
together they must pro
eed at the speed of the slowest. The rst person takes t1 minutes
to
ross, the se
ond t2 minutes, the third t3 minutes and the fourth t4 minutes. The
tor
h must be ferried ba
k and forth a
ross the bridge, so that it is always
arried when
the bridge is
rossed.
You may assume that t1 t2 t3 t4 . Find the optimal method of getting all four
a
ross.
Apply your solution to the following two
ases:
(a) The times taken are 1 minute, 1 minute, 3 minutes and 3 minutes.
(b) The times taken are 1 minute, 4 minutes, 4 minutes and 5 minutes.
Hint: In the spe
i
ase dis
ussed above (where t1 , t2 , t3 and t4 are 1 , 2 , 5 and
10 , respe
tively), the shortest time is a
hieved by letting the two slowest
ross together.
However, this isn't always the best strategy. An alternative strategy is to let the fastest
person a
ompany ea
h of the others a
ross in turn. You will need to evaluate the time
taken for both strategies and
hoose between them on the basis of the times. In order
to derive the solution methodi
ally, we suggest the following steps:
(a) How many times must the tor
h must be
arried a
ross the bridge in order to get all
four people a
ross? (In
lude
rossings in both dire
tions in the
ount.) How many
of these are return trips?
(b) Comparing the number of times a return journey must be made with the number of
people, what
an you say about the number of people who do not make a return
journey? Whi
h of the four people would you
hoose not to make a return journey?
(Give a
onvin
ing argument to support your
hoi
e.)
(c) Now fo us on how to get the people who do not make a return journey a ross the
bridge. What are the dierent strategies? Evaluate the time taken for ea
h. Hen
e,
onstru
t a formula for the minimum time needed to get all four people a
ross in
the general
ase.
(d) Give a solution to the general problem. Use a onditional statement to de ide
Suppose a brute-for
e sear
h is used to solve the tor
h problem (exer
ise
3.4 above). This would mean enumerating all the dierent ways of getting four people
a
ross the bridge. How many ways are there?
Exercise 3.5
January 7, 2007
39
3.5. Summary
3.5
Summary
January 7, 2007
40
3. Crossing a River
January 7, 2007
Chapter 4
Games
This
hapter is about how to win some simple two-person games. Games provide very
good examples of algorithmi
problem solving be
ause playing games is all about winning. The goal is to have some method (i.e. \algorithm") for de
iding what to do so that
the eventual out
ome is a win.
The key to winning is the re
ognition of invariants. So, in essen
e, this
hapter is
a
ontinuation of
hapter 2. The
hapter is also about trying to identify and exploit
stru
ture in problems. In this sense, it introdu
es the importan
e of algebra in problem
solving.
The next se
tion introdu
es a number of games with mat
hsti
ks, in order to give
the
avour of the games that we
onsider. Following it, we develop a method of systemati
ally identifying winning and losing positions in a game (assuming a number of
simplifying
onstraints on the rules of the game). A winning strategy is then what we
all \maintaining an invariant". \Maintaining an invariant" is an important te
hnique in
algorithm development. Here, it will mean ensuring that the opponent is always pla
ed
in a position from whi
h losing is inevitable.
4.1
Matchstick Games
A mat
hsti
k game is played with one or more piles of mat
hes. Two players take it
in turn to make a move. Moves involve removing one or more mat
hes from one of the
piles, a
ording to a given rule. The game ends when it is no longer possible to make
a move. The player whose turn it is to move is the loser, and the other player is the
winner.
A mat
hsti
k game is an example of an impartial, two-person game with
omplete
information. \Impartial" means that rules for moving apply equally to both players.
(Chess, for example, is not impartial, be
ause white
an only move white pie
es, and
bla
k
an only move bla
k pie
es.) \Complete information" means that both players
41
January 7, 2007
42
4. Games
know the
omplete state of the game. In
ontrast, in
ard games like poker, it is usual
that ea
h player does not know the
ards held by the other player; the players have
in
omplete information about the state of the game.
A winning position is one from whi
h a perfe
t player is always assured of a win. A
losing position is one from whi
h a player
an never win, when playing against a perfe
t
player. A winning strategy is an algorithm for
hoosing moves from winning positions,
that guarantees a win.
As an example, suppose there is one pile of mat
hes, and an allowed move is to remove
1 or 2 mat
hes. The losing positions are the positions where the number of mat
hes
is a multiple of 3 (that is, the number of mat
hes is 0 , 3 , 6 , 9 et
.). The remaining
positions are the winning positions. If m is the number of mat
hes in su
h a position
(so, m is not a multiple of 3 ), the strategy is to remove m mod 3 mat
hes1. This is
either 1 or 2 , and so the move is valid. The opponent is then put in a position where
the number of mat
hes is a multiple of 3 . This means that there are either 0 mat
hes
left, in whi
h
ase the opponent loses, or any move they make will result in there again
being a number of mat
hes remaining that is not a multiple of 3 .
In an impartial game that is guaranteed to terminate no matter how the players
hoose their moves (i.e. the possibility of stalemate is ex
luded), it is always possible to
hara
terise the positions as either winning or losing positions. The following exer
ises
ask you to do this in spe
i
ases.
1. There is one pile of mat
hes. Ea
h player is allowed to remove 1 mat
h. What are
the winning positions?
2. There is one pile of mat
hes. Ea
h player is allowed to remove 0 mat
hes. What
are the winning positions?
3. Can you see a pattern in the last two problems and the example dis
ussed above
(in whi
h a player is allowed to remove 1 or 2 mat
hes)? In other words,
an you
see how to win a game in whi
h an allowed move is to remove at least one and at
most N mat
hes, where N is some number xed in advan
e?
4. There is one pile of mat
hes. Ea
h player is allowed to remove 1 , 3 or 4 mat
hes.
What are the winning positions and what is the winning strategy?
5. There is one pile of mat
hes. Ea
h player is allowed to remove 1 , 3 or 4 mat
hes,
ex
ept that it is not allowed to repeat the opponent's last move. (So, if, say, your
opponent removes 1 mat
h, your next move must be to remove 3 or 4 mat
hes.)
What are the winning positions and what is the winning strategy?
1 Re
all
January 7, 2007
43
6. There are two piles of mat
hes. A move is to
hoose one pile and, from that pile,
remove 1 , 2 or 3 mat
hes. What are the winning positions and what is the
winning strategy?
7. There are two piles of mat
hes. A move is to
hoose one pile; from the left pile 1 ,
2 or 3 mat
hes may be removed, and from the right pile 1 thru2 7 mat
hes may
be removed. What are the winning positions and what is the winning strategy?
8. There are two piles of mat
hes. A move is to
hoose one pile; from the left pile, 1 ,
3 or 4 mat
hes may be removed, and, from the right pile, 1 or 2 mat
hes may
be removed. What are the winning positions and what is the winning strategy?
4.2
Winning Strategies
In this se
tion, we formulate what is required of a winning strategy. We begin with the
simple mat
hsti
k game where a move is to remove one or two mat
hes from a single
pile of mat
hes; we show how to sear
h systemati
ally through all the positions of the
game, labelling ea
h as either a winning or a losing position. Although a brute-for
e
sear
h, and thus not pra
ti
al for more
ompli
ated games, the algorithm does give a
better understanding of what is involved, and
an be used as a basis for developing more
e
ient solutions in parti
ular
ases.
4.2.1
Assumptions
We make a number of assumptions about the game, in order that the sear h will work.
use \thru" when we want to spe
ify an in
lusive range of numbers. For example, \ 1 thru 4 "
means the numbers 1 , 2 , 3 and 4 . The English expression \ 1 to 4 " is ambiguous about whether the
number 4 is in
luded or not.
January 7, 2007
44
4. Games
4.2.2
Labelling Positions
The rst step is to draw a dire
ted graph depi
ting all positions, and all moves in the
game. Fig. 4.1 is a graph of the mat
hsti
k game des
ribed at the beginning of se
tion
4.1.
Figure 4.1: Mat
hsti
k Game. Players may take one or two mat
hes at ea
h turn.
A dire
ted graph has a set of nodes and a set of edges. Ea
h edge is from one node
to another node. When graphs are drawn, nodes are depi
ted by
ir
les, and edges are
depi
ted by arrows pointing from the from node to the to node.
The nodes in g. 4.1 are labelled by a number, the number of mat
hes remaining in
the pile. From the node labelled 0 , there are no edges. It is impossible to move from
the position in whi
h no mat
hes remain. From the node labelled 1 , there is exa
tly one
edge, to the node labelled 0 . From the position in whi
h one mat
h remains, there is
only one move that
an be made, namely to remove the remaining mat
h. From all other
nodes, there are two edges. From the node labelled n , where n is at least 2 , there is
an edge to the node labelled n1 and an edge to the node labelled n2 . That is, from
a position in whi
h the number of remaining mat
hes is at least 2 , one is allowed to
remove one or two mat
hes.
Having drawn the graph, we
an begin labelling the nodes as either losing positions
or winning positions. A player who nds themself in a losing position will inevitably
lose, if playing against a perfe
t opponent. A player who nds themself in a winning
position is guaranteed to win, provided the right
hoi
e of move is made at ea
h turn.
The labelling rule has two parts, one for losing positions, the other for winning
positions:
A node is labelled losing if every edge from the node is to a winning position.
A node is labelled winning if there is an edge from the node to a losing position.
At rst sight, it may seem that it is impossible to begin to apply these rules; after
all, the rst rule denes losing positions in terms of winning positions, whilst the se
ond
rule does the reverse. It seems like a vi
ious
ir
le! However, we
an begin by labelling
as losing positions all the nodes with no outgoing edges. This is be
ause, if there are no
edges from a node, the statement \every edge from the node is to a winning position" is
true. It is indeed the
ase that all of the (non-existent) edges is to a winning position.
January 7, 2007
45
This is an instan
e of a general rule of logi
. A statement of the form \every x has
property p " is what is
alled a for-all quanti
ation, or a universal quanti
ation.
Su
h a statement is said to be va
uously true when there are no instan
es of the \ x "
in the quanti
ation. In a sense, the statement is \va
uous" (i.e. empty) be
ause it is a
statement about nothing.
Returning to g. 4.1, the node 0 is labelled \losing", be
ause there are no edges
from it. It is indeed a losing position, be
ause the rules of the game spe
ify that a player
who
annot make a move loses.
Next, nodes 1 and 2 are labelled \winning", be
ause, from ea
h, there is an edge to
0 , whi
h we know to be a losing position. Note that the edges we have identied di
tate
the move that should be made from these positions if the game is to be won.
Now, node 3 is labelled \losing", be
ause both edges from node 3 are to nodes ( 1
and 2 ) that we have already labelled \winning". From a position in whi
h there are 3
mat
hes remaining, every move is to a position starting from whi
h a win is guaranteed.
A player that nds themself in this position will eventually lose.
The pro
ess we have des
ribed repeats itself until all nodes have been labelled. Nodes
4 and 5 are labelled \winning", then node 6 is labelled \losing", then nodes 7 and 8
are labelled \winning", and so on.
Fig. 4.2 shows the state of the labelling pro
ess at the point that node 7 has been
labelled but not node 8 . The
ir
les depi
ting losing positions are drawn with thi
k
lines; the
ir
les depi
ting winning positions are the ones from whi
h there is an edge
drawn with a thi
k line. These edges depi
t the winning move from that position.
Figure 4.2: Labelling Positions. Winning edges are indi
ated by thi
k edges.
Clearly, a pattern is emerging from this pro
ess. The pattern is that the losing
positions are the ones where the number of mat
hes is a multiple of 3 . The winning
positions are the remaining positions; the winning strategy is to remove one or two
mat
hes so as to leave the opponent in a position where the number of mat
hes is on
e
again a multiple of 3 .
4.2.3
Formulating Requirements
The terminology we use to des
ribe the winning strategy is to \maintain invariant"
the property that the number of mat
hes is a multiple of 3 . In programming terms,
January 7, 2007
46
4. Games
we express this property using Hoare triples. Let n denote the number of mat
hes in
the pile. Then, the
orre
tness of the winning strategy is expressed by the following
annotated program segment:
{
n is a multiple of 3 , and n 6= 0
if 1 n n := n1
;
2 n n := n2 fi
n is not a multiple of 3 }
n := n (n mod 3)
{
n is a multiple of 3 }
There are ve
omponents of this program segment, ea
h on a separate line. The rst
line is the pre
ondition. This expresses the assumption that we begin from a position in
whi
h the number of mat
hes is a multiple of 3 , and non-zero.
The se
ond line is a so-
alled
onditional statement. Conditional statements are
re
ognised by \ if - fi " bra
kets. Within these bra
kets is a non-deterministi
hoi
e |
indi
ated by the \ " symbol| among a number of so-
alled guarded
ommands. A
guarded
ommand has the form b S , where b is a boolean-valued expression
alled
the guard, and S is a statement
alled the body. Starting in a given state, a
onditional
statement is exe
uted by
hoosing a guarded
ommand whose guard evaluates to true ,
and then exe
uting its body. If several guards evaluate to true , an arbitrary
hoi
e of
ommand is made. If none of the guards evaluates to true , exe
ution is aborted3 .
In this way, the if - fi statement in the se
ond line models an arbitrary move. Removing one mat
h is only allowed if 1 n ; hen
e, the statement n := n1 is \guarded"
by this
ondition. Similarly, removing two mat
hes |modelled by the assignment
n := n2 | is \guarded" by the
ondition 2 n . At least one of these guards, and
possibly both, is true be
ause of the assumption that n 6= 0 .
The post
ondition of the guarded
ommand is the assertion \ n is not a multiple
of 3 ". The triple,
omprising the rst three lines, thus asserts that, if the number of
mat
hes is a multiple of 3 , and a valid move is made that redu
es the number of mat
hes
by one or two, then, on
ompletion of the move, the number of mat
hes will not be a
multiple of 3 .
The fourth line of the sequen
e is the implementation of the winning strategy; spe
ifi
ally, remove n mod 3 mat
hes. The fth line is the nal post
ondition, whi
h asserts
that, after exe
ution of the winning strategy, the number of mat
hes will again be a
multiple of 3 .
3 If
you are already familiar with a
onventional programming language, you will be familiar with
deterministi
onditional statements | so-
alled if-then-else statements. In su
h statements, the
hoi
e
of whi
h of the optional statements should be exe
uted is
ompletely determined by the state of the
program variables. In a non-deterministi
hoi
e, as used here, the
hoi
e is not
ompletely determined.
January 7, 2007
47
In summary, a winning strategy is a way of
hoosing moves that divides the positions
into two types, the losing positions and the winning positions, in su
h a way that the
following three properties hold:
From a winning position, it is always possible to apply the winning strategy, resulting in a losing position.
If both players are perfe
t, the winner is de
ided by the starting position. If the
starting position is a losing position, the se
ond player is guaranteed to win. Vi
eversa, if the starting position is a winning position, the rst player is guaranteed to win.
Starting from a losing position, one
an only hope that one's opponent is not perfe
t,
and will make a mistake.
Formally, a winning strategy maintains invariant the boolean quantity
(the number of moves remaining before the game ends is even) equals (the
position is a losing position).
Sin
e end positions are losing positions, by assumption, and positions where the number
of moves remaining is zero, whi
h is an even number, this quantity will always be true
in a game played by perfe
t players.
January 7, 2007
48
4. Games
We re
ommend that you now try to solve the mat
hsti
k-game problem when the
rule is that any number of mat
hes from 1 thru M may be removed at ea
h turn. The
number M is a natural number, xed in advan
e. We re
ommend that you try to solve
this general problem by rst
onsidering the
ase that M is 0 . This
ase has a very easy
solution, although it is a
ase that is very often negle
ted. Next,
onsider the
ase that M
is 1 . This
ase also has an easy solution, but slightly more
ompli
ated. Now,
ombine
these two
ases with the
ase that M is 2 , whi
h is the
ase we have just
onsidered.
Do you see a pattern in the solutions? If you don't see a pattern immediately, try a bit
harder. As a last resort, try working out the
ase that M is 3 . (Don't draw a diagram.
Constru
t a table instead. A diagram is mu
h too
ompli
ated.) Then, return to the
ases that M is 0 , 1 and 2 (in parti
ular, the extreme
ases 0 and 1 ) in order to
he
k the pattern you have identied. Finally, formulate the
orre
tness of the strategy
by a sequen
e of assertions and statements, as we did above for the
ase that M is 2 .
Two players alternately name dates. The
winner is the player who names 31st De
ember, and the starting date is 1st January.
Ea
h part of this exer
ise uses a dierent rule for the dates that a player is allowed to
name. For ea
h, devise a winning strategy, stating whi
h player should win. State also
if it depends on whether the year is a leap year or not.
Hint: in prin
iple, you have to determine for ea
h of 365 days (or 366 in the
ase of a
leap year) whether naming the day results in losing against a perfe
t player. In pra
ti
e,
a pattern soon be
omes evident and the days in ea
h month
an be grouped together
into winning and losing days. Begin by identifying the days in De
ember that one should
avoid naming.
Exercise 4.1 (31st December Game)
a) (Easy) A player
an name the 1st of the next month, or in
rease the day of the month
by an arbitrary amount. (For example, the rst player begins by naming 1st February,
or a date in January other than the 1st.)
b) (Harder) A player
an in
rease the day by one, leaving the month un
hanged, or name
the 1st of the next month.
4.3
Subtraction-Set Games
A
lass of mat
hsti
k games is based on a single pile of mat
hes and a (nite) set of
numbers; a move is to remove m mat
hes, where m is an element of the given set. A
game in this
lass is
alled a subtra
tion-set game, and the set of numbers is
alled the
subtra
tion set.
January 7, 2007
49
The games we have just dis
ussed are examples of mat
hsti
k games; if the rule is that
1 thru M may be removed at ea
h turn, the subtra
tion set is {1..M} . More interesting
examples are obtained by
hoosing a subtra
tion set with less regular stru
ture.
For any given subtra
tion set, the winning and losing positions
an always be
omputed. We exemplify the pro
ess in this se
tion by
al
ulating the winning and losing
positions when the allowed moves are:
The results are entered in a table. Table 4.1 shows the entries when the size of the pile
is at most 6 . The top row is the position, and the middle row shows whether or not it is
a winning (W) or losing position (L). In the
ase that the position is a winning position,
the bottom row shows the number of mat
hes that should be removed in order to move
from the position to a losing position. For example, 2 is a losing position be
ause the
only move from 2 is to 1 ; positions 3 and 4 are winning positions be
ause from both a
move
an be made to 0 . Note that there may be a
hoi
e of winning move. For example,
from position 3 there are two winning moves | remove 3 mat
hes to move to position
0 , or remove 1 mat
h to move to position 2 . It su
es to enter just one move in the
bottom row of the table.
Position 0
Type
L
Move
Table 4.1: Winning (W) and Losing (L) Positions for subtra tion set {1 , 3 , 4}
January 7, 2007
50
4. Games
Position 7
Type
L
Move
10
11
12
13
Table 4.2: Winning (W) and Losing (L) Positions for subtra
tion set {1 , 3 , 4}
Continuing this pro
ess, we get the next seven entries in the table: see table 4.2.
Comparing tables 4.1 and 4.2, we noti
e that the pattern of winning and losing
positions repeats itself. On
e the pattern begins repeating in this way, it will
ontinue to
do so forever. We may therefore
on
lude that, for the subtra
tion set {1 , 3 , 4} , whether
or not the position is a winning position
an be determined by
omputing the remainder,
r say, after dividing the number of mat
hes by 7 . If r is 0 or 2 , the position is a losing
position. Otherwise, it is a winning position. The winning strategy is to remove 1 mat
h
if r is 1 , remove 3 mat
hes if r is 3 or 5 , and remove 4 mat
hes if r is 4 or 6 .
The repetition in the pattern of winning and losing positions that is evident in this
example is a general property of subtra
tion-set games, with the
onsequen
e that, for a
given subtra
tion set, it is always possible to determine for an arbitrary position whether
or not it is a winning position (and, for the winning positions, a winning move). The
following argument gives the reason why.
Suppose a subtra
tion set is given. Sin
e the set is assumed to be nite, it must
have a largest element. Let this be M . Then, from ea
h position, there are at most
M moves. For ea
h position k , let W.k be true if k is a winning position, and false
otherwise. When k is at least M , W.k is
ompletely determined by the sequen
e
W.(k1) , W.(k2) , . . . , W.(kM) . Call this sequen
e s.k . Now, there are only 2M
dierent sequen
es of booleans of length M . As a
onsequen
e, the sequen
e s.(M+1) ,
s.(M+2) , s.(M+3) , . . . must eventually repeat, and it must do so within at most 2M
steps. That is, for some j and k , with M j < k < M+2M , we must have s.j = s.k . It
follows that W.j = W.k and the sequen
e W repeats from the k th position onwards.
For the example above, this analysis predi
ts that the W-L pattern will repeat from
the 20 th position onwards. In fa
t, it begins repeating mu
h earlier. Generally, we
an say that the pattern of win-lose positions will repeat at position 2M+M , or before.
To determine whether an arbitrary position is a winning or losing position involves
omputing the status of ea
h position k , for su
essive values of k , until a repetition in
s.k is observed. If the repetition o
urs at position R , then, for an arbitrary position
k , W.k equals W.(k mod R) .
Suppose there is one pile of mat
hes. In ea
h move, 2 , 5 or 6 mat
hes
may be removed. (That is, the subtra
tion set is {2 , 5 , 6} .)
Exercise 4.2
January 7, 2007
51
losing position.
(b) Identify a pattern in the winning and losing positions. Spe
ify the pattern by giving
pre
ise details of a boolean fun
tion of n that determines whether a pile of n
Figure 4.3 shows a variant of snakes and ladders. In this game, there
is just one
ounter. The two players take it in turn to move the
ounter at most four
spa
es forward. The start is square 1 and the nish is square 25 . As in the usual game
of snakes and ladders, if the
ounter lands on the head of a snake, it falls down to the
tail of the snake; if the
ounter lands at the foot of a ladder, it
limbs to the top of the
ladder.
Exercise 4.3
21
22
23
24
25
20
19
18
17
16
11
12
13
14
15
10
Figure 4.3: Snakes and Ladders. Players take it in turn to move the
ounter at most
four spa
es forward.
January 7, 2007
52
4. Games
(a) List the positions in this game. (These are not the same as the squares. Think
(b) Identify the winning and losing positions. Use the rule that a losing position is one
from whi
h every move is to a winning position, and a winning position is one from
whi
h there is a move to a losing position.
(c) Some of the positions annot be identied as winning or losing in this way. Explain
why.
4.4
Sums of Games
In this se
tion, we look at how to exploit the stru
ture of a game in order to
ompute a
winning strategy more ee
tively.
The later examples of mat
hsti
k games in se
tion 4.1 have more than one pile of
mat
hes. When a move is made, one of the piles must rst be
hosen; then, mat
hes
may be removed from the
hosen pile a
ording to some pres
ribed rule, whi
h may dier
from pile to pile. The game is thus a
ombination of two games; this parti
ular way of
ombining games is
alled summing the games.
In general, given two games, ea
h with its own rules for making a move, the sum of
the games is the game des
ribed as follows. For
larity, we
all the two games the left
and the right game. A position in the sum game is the
ombination of a position in the
left game, and a position in the right game. A move in the sum game is a move in one
of the games.
Figure 4.4 is an example of the sum of two games. Ea
h graph represents a game,
where the positions are represented by the nodes, and the moves are represented by the
edges. Imagine a
oin pla
ed on a node. A move is then to displa
e the
oin along one
of the edges to another node. The nodes in the left graph and right graphs are named
by
apital letters and small letters, respe
tively, so that we
an refer to them later.
In the \sum" of the games, two
oins are used, one
oin being pla
ed over a node in
ea
h of the two graphs. A move is then to
hoose one of the
oins, and displa
e it along
an edge to another node. Thus, a position in the \sum" of the games is given by a pair
Xx where \X" names a node in the left graph, and \x" names a node in the right graph;
a move has the ee
t of
hanging exa
tly one of \X" or \x".
Both the left and right games in g. 4.4 are unstru
tured;
onsequently, the brutefor
e sear
h pro
edure des
ribed in se
tion 4.2.2 is unavoidable when determining their
winning and losing positions. However, the left game in g. 4.4 has 15 dierent postions,
January 7, 2007
53
Figure 4.4: A Sum Game. The left and right games are represented by the two graphs.
A position is a pair Xx where \X" is the name of a node in the left graph, and \x" is
the name of a node in the right graph. A move
hanges exa
tly one of X or x.
and the right game has 11 ; thus, the sum of the two games has 1511 dierent positions.
For this game, and for sums of games in general, a brute-for
e sear
h is highly undesirable.
In this se
tion, we study how to
ompute a winning strategy for the sum of two games. We
nd that the
omputational eort is the sum (in the usual sense of addition of numbers)
of the eort required to
ompute winning and losing positions for the
omponent games,
rather than the produ
t. We nd, however, that it is not su
ient to know just the
winning strategy for the individual games. Deriving a suitable generalisation forms the
ore of the analysis.
4.4.1
Symmetry
We begin with a very simple example of the sum of two games. Suppose there are two
piles of mat
hes. An allowed move is to
hoose any one of the piles and remove at least
January 7, 2007
54
4. Games
one mat
h from the
hosen pile. Otherwise, there is no restri
tion on the number of
mat
hes that may be removed. As always, the game is lost when a player
annot make
a move.
This game is the \sum" of two instan
es of the same, very, very simple, game: given
a (single) pile of mat
hes, a move is to remove at least one mat
h from the pile. In this
simple game, the winning positions are, obviously, the positions in whi
h the pile has at
least one mat
h, and the winning strategy is to remove all the mat
hes. The position in
whi
h there are no mat
hes remaining is the only losing position.
It qui
kly be
omes
lear that knowing the winning strategy for the individual games
is insu
ient to win the sum of the games. If a player removes all the mat
hes from
one pile |that is, applies the winning strategy for the individual game| , the opponent
wins by removing the remaining mat
hes in the other pile.
The symmetry between left and right allows us to easily spot a winning strategy.
Suppose we let m and n denote the number of mat
hes in the two piles. In the end
position, there is an equal number of mat
hes in both piles, namely 0 . That is, in the
end position, m = n = 0 . This suggests that the losing positions are given by m = n .
This is indeed the
ase. From a position in whi
h m = n , and a move is possible (that
is, either 1 m or 1 n ), any move will be to a position where m 6= n . Subsequently,
hoosing the pile with the larger number of mat
hes, and removing the ex
ess mat
hes
from this pile, will restore the property that m = n .
Formally, the
orre
tness of the winning strategy is expressed by the following sequen
e of assertions and program statements.
{
if
m = n (m 6= 0 n 6= 0)
1 m redu e m
1 n redu
e n
fi
;
{
if
m 6= n
m < n n := n (nm)
n < m m := m (mn)
fi
{
m=n
The non-deterministi
hoi
e between redu
ing m , in the
ase that 1 m , and redu
ing
n , in the
ase that 1 n , models an arbitrary
hoi
e of move in the sum game. The
fa
t that either m
hanges in value, or n
hanges in value, but not both, guarantees
m 6= n after
ompletion of the move.
January 7, 2007
55
The property m 6= n is the pre
ondition for the winning strategy to be applied.
Equivalently, m < n or n < m . In the
ase that m < n , we infer that 1 nm n , so
that nm mat
hes
an be removed from the pile with n mat
hes. Sin
e, n(nm)
simplies to m , it is
lear that, after the assignment n := n(nm) , the property
m = n will hold. The
ase n < m is symmetri
.
The following sequen
e of assertions and program statements summarises the argument just given for the validity of the winning strategy. Note how the two assignments
have been annotated with a pre
ondition and a post
ondition. The pre
ondition expresses the legitima
y of the move; the post
ondition is the losing property that the
strategy is required to establish.
{
m 6= n
m<n n<m
if
}
}
m < n { 1 nm n } n := n (nm) { m = n }
n < m { 1 mn m } m := m (mn) { m = n }
fi
{
4.4.2
m=n
Keep It Symmetrical!
The game in se
tion 4.4.1 is another example of the importan
e of symmetry; the winning
strategy is to ensure that the opponent is always left in a position of symmetry between
the two individual
omponents of the sum-game. We see shortly that this is how to win
all sum-games, no matter what the individual
omponents are.
There are many examples of games where symmetry is the key to winning. Here is a
ouple. The solutions
an be found at the end of the book.
The Daisy Problem Suppose a daisy has 16 petals arranged symmetri
ally around
its
entre. There are two players. A move involves removing one petal or two adja
ent
petals. The winner is the one who removes the last petal. Who should win and what
is the winning strategy? Generalise your solution to the
ase that there are initially n
petals and a move
onsists of removing between 1 and M adja
ent petals (where M is
xed in advan
e of the game).
The Coin Problem Two players are seated at a re
tangular table whi
h initially is
bare. They ea
h have an unlimited supply of
ir
ular
oins of varying diameter. The
players take it in turns to pla
e a
oin on the table, su
h that it does not overlap any
January 7, 2007
56
4. Games
4.4.3
Let us return to our mat
hsti
k games. A variation on the sum game in se
tion 4.4.1 is
to restri
t the number of mat
hes that
an be removed. Suppose the restri
tion is that
at most K mat
hes
an be removed from either pile (where K is xed, in advan
e).
The ee
t of the restri
tion is to disallow some winning moves. If, as before, m and
n denote the number of mat
hes in the two piles, it is not allowed to remove mn
mat
hes when K < mn . Consequently, the property m = n no longer
hara
terises the
losing positions. For example, if K is xed at 1 , the position in whi
h one pile has two
mat
hes whilst the se
ond pile has no mat
hes is a losing position: in this position a
player is for
ed to move to a position in whi
h one mat
h remains; the opponent
an
then remove the mat
h to win the game.
A more signi
ant ee
t of the restri
tion seems to be that the strategy of establishing
symmetry is no longer appli
able. Worse is if we break symmetry further by imposing
dierent restri
tions on the two piles: suppose, for example, we impose the limit M on
the number of mat
hes that may be removed from the left pile, and N on the number of
mat
hes that may be removed from the right pile, where M 6= N . Alternatively, suppose
the left and right games are
ompletely dierent, for example, if one is a mat
hsti
k game
and the other is the daisy game. If this is the
ase, how is it possible to maintain symmetry? Nevertheless, a form of \symmetry" is a key to the winning strategy: symmetry
January 7, 2007
57
( M is the maximum number of mat
hes that
an be removed from the left pile, and N
is the maximum number that
an be removed from the right pile.)
This, indeed, is the
orre
t solution. In the end position, where both piles have 0
mat
hes, the property is satised. Also, the property
an always be maintained following
an arbitrary move by the opponent, as given by the following annotated program segment.
{
if
1 n redu
e n by at most N
fi
;
(Note: we dis
uss later the full details of how to
he
k the assertions made in this
program segment.)
4.4.4
The idea of dening \symmetri
" to be \the respe
tive remainders are equal"
an be
generalised to an arbitrary sum game.
Consider a game that is the sum of two games. A position in the sum game is a pair
( l,r ) where l is a position in the left game, and r is a position in the right game. A
move ae
ts just one
omponent; so, a move is modelled by either a (guarded) assignment
January 7, 2007
58
4. Games
The question is: what properties should these fun
tions satisfy? In other words, how do
we spe
ify the fun
tions L and R ?
The analysis given earlier of a winning strategy allows us to distill the spe
i
ation.
First, sin
e ( l,r ) is an end position of the sum game exa
tly when l is an end position
of the left game and r is an end position of the right game, it must be the
ase that L
and R have equal values on end positions.
Se
ond, every allowed move from a losing position |a position ( l,r ) satisfying
L.l = R.r | , that is not an end position, should result in a winning position |a position ( l,r ) satisfying L.l 6= R.r | . That is,
{
if
L.l 6= R.r }
Third, applying the winning strategy, from a winning position |a position ( l,r ) satisfying L.l 6= R.r | should result in a losing position |a position ( l,r ) satisfying L.l = R.r |.
That is,
{
L.l 6= R.r }
L.l = R.r }
For end positions l and r of the respe
tive games, L.l = 0 = R.r .
For every l su
h that there is a move from l to l in the left game, L.l 6= L.l .
Similarly, for every r su
h that there is a move from r to r in the right game,
R.r 6= R.r .
Note that the
hoi
e of the natural numbers as range of the fun
tions, and the
hoi
e
of 0 as the fun
tions' value at end positions is quite arbitrary. The advantage of this
hoi
e arises from the third requirement. If L.l and R.r are dierent natural numbers,
January 7, 2007
59
either L.l < R.r or R.r < L.l . This allows us to rene the pro
ess of applying the winning
strategy, by
hoosing to move in the right game when L.l < R.r and
hoosing to move in
the left game when R.r < L.l . (See below.)
L.l 6= R.r }
{
if
L.l = R.r }
For any number m less than R.r , it is possible to move from r to a position r
su
h that R.r = m . Similarly, for any number n less than L.l , it must be possible
to move from l to a position l su
h that L.l = n .
The bulleted requirements are satised if we dene the fun
tions L and R to be the
so-
alled \mex" fun
tion. The pre
ise denition of this fun
tion is as follows.
Let p be a position in a game G . The mex value of p , denoted mexG.p ,
is dened to be the smallest natural number, n , su
h that
For every natural number m less than n , there is a legal move in the
game G from p to a position q satisfying mexG.q = m .
\Mex" is short for \minimal ex
ludant". A brief, informal des
ription of the mex
number of a position p is the minimum number that is ex
luded from the mex numbers
of positions q to whi
h a move
an be made from p .
4.4.5
We use the game depi
ted in g. 4.4 to illustrate the
al
ulation of mex numbers. Figure
4.6 shows the mex numbers of ea
h of the nodes in their respe
tive games.
The graphs do not have any systemati
stru
ture;
onsequently, the only way to
ompute the mex numbers is by a brute-for
e sear
h of all positions. This is easily done
by hand. The end positions are ea
h given mex number 0 . Subsequently, a mex number
an be given to a node when all its su
essors have already been given a mex number.
(A su
essor of a node p is a node q su
h that there is an edge from p to q .) The
January 7, 2007
60
4. Games
Figure 4.6: Mex Numbers. The mex number of a node is the smallest natural number
not in
luded among the mex numbers of its su
essors.
number is, by denition, the smallest number that is not in
luded in the mex numbers
of its su
essors. Fig. 4.7 shows a typi
al situation. The node at the top of the gure
is given a mex number when all its su
essors have been given mex numbers. In the
situation shown, the mex number given to it is 2 be
ause none of its su
essors have
been given this number, but there are su
essors with the mex numbers 0 and 1 .
Now, suppose we play this game. Let us suppose the starting position is \Ok". This
is a winning position be
ause the mex number of \O" is dierent from the mex number
of \k". The latter is larger ( 3 against 2 ). So, the winning strategy is to move in the
right graph to the node \i", whi
h has the same mex number as \O". The opponent
is then obliged to move, in either the left or right graph, to a node with mex number
dierent from 2 . The rst player then repeats the strategy of ensuring that the mex
numbers are equal, until eventually the opponent
an move no further.
Note that, be
ause of the la
k of stru
ture of the individual games, we have to sear
h
through all 15 positions of the left game and all 11 positions of the right game, in order
to
al
ulate the mex numbers of ea
h position. In total, therefore, we have to sear
h
January 7, 2007
61
Figure 4.7: Computing mex numbers. The unlabelled node is given the mex number 2 .
through 26 dierent positions. But, this is just the sum (in the usual sense of the word)
of 15 and 11 , and is mu
h less than their produ
t, 165 . This is a substantial saving
in
omputational eort. Moreover, the saving grows as the size of the
omponent games
in
reases.
a) Constru
t the subtra
tion set game where there is one pile of mat
hes
from whi
h at most 2 , 5 or 6 mat
hes may be removed. Cal
ulate the mex number for
ea
h position until you spot a pattern.
Give a general rule for
omputing the mex number of a pile of m mat
hes in this
game.
Exercise 4.4
b) Consider a game whi
h is the sum of two games. In the left game, 1 or 2 mat
hes
may be removed at ea
h turn. In the right game, 2 , 5 or 6 mat
hes may be removed.
In the sum game, a move is made by
hoosing to play in the left game, or
hoosing to
play in the right game.
The table below shows a number of dierent positions in this game. A position is
given by a pair of numbers: the number of mat
hes in the left pile, and the number of
mat
hes in the right pile.
Left Game Right Game \losing" or winning move
10
20
?
20
20
?
15
5
?
6
9
?
37
43
?
Table 4.3: Fill in entries marked \?"
For ea
h position, state whether it is a winning or a losing position. For winning
positions, give the winning move in the form X m where \X" is one of \L" (for \left
game") or \R" (for right game), and m is the number of mat
hes to be removed.
January 7, 2007
62
4. Games
4.5
Summary
This
hapter has been about determining winning strategies in simple two-person games.
The underlying theme of the
hapter has been problem spe
i
ation. We have seen how
winning and losing positions are spe
ied. A pre
ise, formal spe
i
ation enabled us to
formulate a brute-for
e sear
h pro
edure to determine whi
h positions are whi
h.
Brute-for
e sear
h is only advisable for small, unstru
tured problems. The analysis
of the \sum" of two games exemplies the way stru
ture is exploited in problem solving.
Again, the fo
us was on problem spe
i
ation. By formulating a notion of \symmetry"
between the left and right games, we were able to determine a spe
i
ation of the \mex"
fun
tion on game positions. The use of mex fun
tions substantially redu
es the eort
needed to determine winning and losing positions in the \sum" of two games,
ompared
to a brute-for
e sear
h.
Game theory is a ri
h, well-explored area of Mathemati
s, whi
h we have only tou
hed
upon in this
hapter. It is a theory that is be
oming in
reasingly important in Computing S
ien
e. One reason for this is that problems that beset software design, su
h as the
se
urity of a system, are often modelled as a game, with the user of the software as the
adversary. Another reason is that games often provide ex
ellent examples of \
omputational
omplexity"; it is easy to formulate games having very simple rules but for whi
h
no e
ient algorithm implementing the winning strategy is known.
Mex numbers were introdu
ed by Sprague and Grundy to solve the \Nim" problem,
and mex numbers are sometimes
alled \Sprague-Grundy" numbers, after their originators. Nim is a well-known mat
hsti
k game involving three piles of mat
hes. We have not
developed the theory su
iently, in this
hapter, to show how Nim, and sums of more
than two games, are solved using mex numbers. (What is missing is how to
ompute the
mex number of the sum of two games.)
4.6
Bibliographic Remarks
The two-volume book \Winning Ways" [BCG82 by Berlekamp, Conway and Guy is the
bible of game theory.
The 31st De
ember game (exer
ise 4.1) is adapted from [DW00.
January 7, 2007
Chapter 5
Knights and Knaves
The island of knights and knaves is a
tional island that is often used to test students'
ability to reason logi
ally. The island has two types of natives, \knights" who always
tell the truth, and \knaves" who always lie. Logi
puzzles involve dedu
ing fa
ts about
the island from statements made by its natives without knowing whether or not the
statements are made by a knight or a knave.
The temptation is to solve su
h problems by
ase analysis |in a problem involving n
natives,
onsider the 2n dierent
ases obtained by assuming that the individual natives
are knights or knaves| . Case analysis is a
lumsy way of ta
kling the problems. In
ontrast, these, and similar logi
puzzles, are easy exer
ises in the use of
al
ulational
logi
, whi
h we introdu
e in this
hapter.
5.1
Logic Puzzles
2. Suppose you
ome a
ross two of the natives. You ask both of them whether the
other one is a knight. Will you get the same answer in ea
h
ase?
3. There are three natives A, B and C. Suppose A says \B and C are the same type."
What
an be inferred about the number of knights?
63
January 7, 2007
64
4. Suppose C says \A and B are as like as two peas in a pod". What question should
you pose to A to determine whether or not C is telling the truth?
5. Devise a question that allows you to determine whether a native is a knight.
6. What question should you ask A to determine whether B is a knight?
7. What question should you ask A to determine whether A and B are the same type
(i.e. both knights or both knaves)?
8. You would like to determine whether an odd number of A, B and C is a knight.
You may ask one yes/no question to any one of them. What is the question you
should ask?
9. A tourist
omes to a fork in the road, where one bran
h leads to a restaurant and
one doesn't. A native of the island is standing at the fork. Formulate a single
yes/no question that the tourist
an ask su
h that the answer will be yes if the left
fork leads to the restaurant, and otherwise the answer will be no.
5.2
5.2.1
Calculational Logic
Propositions
The algebra we learn at s
hool is about
al
ulating with expressions whose values are
numbers. We learn, for example, how to manipulate an expression like m2n2 in order
to show that its value is the same as the value of (m+n)(mn) , independently of the
values of m and n . We say that m2n2 and (m+n)(mn) are equal, and write
m2n2 = (m+n)(mn) .
The basis for these
al
ulations is a set of laws. Laws are typi
ally primitive, but general,
equalities between expressions. They are \primitive" in the sense that they
annot be
broken down into simpler laws, and they are \general" in the sense that they hold
independently of the values of any variables in the
onstituent expressions. We
all them
axioms. Two examples of axioms, both involving zero are:
n+0 = n ,
and
nn = 0 ,
January 7, 2007
65
both of whi
h are true whatever the value of the variable n . We say they are true \for
all n ". The laws are often given names so that we
an remember them more easily. For
example, \asso
iativity of addition" is the name given to the equality:
(m+n)+p = m+(n+p) ,
5.2.2
Equality of propositions is
entral to solving puzzles about knights and knaves. Re
all
that a knight always tells the truth, and a knave always lies. If A is a native of the island,
the statement \A is a knight" is either true or false , and so is a proposition. Also, the
statements made by the natives are propositions. A statement like \the restaurant is to
the left" is either true or false. Suppose A denotes the proposition \A is a knight", and
suppose native A makes a statement S. Then, the
ru
ial observation is that the values
of these two propositions are the same. That is,
A=S .
January 7, 2007
66
where L denotes the truth value of the statement \the restaurant is to the left". In
words, A is a knight and the restaurant is to the left, or A is not a knight and the
restaurant is not to the left.
Using this rule, if A says \I am a knight", we dedu
e
A=A .
This doesn't tell us anything! A moment's thought
onrms that this is what one would
expe
t. Both knights and knaves would
laim that they are knights.
If native A is asked a yes/no question Q , the response to the question is the truth
value of A = Q . That is, the response will be \yes" if A is a knight and the answer is
really yes, or A is a knave and the answer is really no. Otherwise the response will be
\no". For example, asked the question \are you a knight" all natives will answer \yes",
as A = A . Asked the question \is B a knight?" A will respond \yes" if they are both the
same type (i.e, A = B ), otherwise \no". That is, A's response is \yes" or \no" depending
on the truth or falsity of A = B .
Be
ause these rules are equalities, the algebrai
properties of equality play a
entral
role in the solution of logi
puzzles formulated about the island. A simple, rst example
is if A is asked whether B is a knight, and B is asked whether A is a knight. As dis
ussed
above, A's response is A = B . Reversing the roles of A and B, B's response is B = A .
But, equality is symmetri
; therefore, the two responses will always be the same. Note
that this argument does not involve any
ase analysis on the four dierent values of A
and B .
The
al
ulational properties of equality of booleans are dis
ussed in the next se
tion
before we return again to the knights and knaves.
5.2.3
Boolean Equality
Equality |on any domain of values| has a number of
hara
teristi
properties. First, it
is re
exive . That is x = x whatever the value (or type) of x . Se
ond, it is symmetri
.
That is, x = y is the same as y = x . Third, it is transitive . That is, if x = y and y = z
then x = z . Finally, if x = y and f is any fun
tion then f.x = f.y (where the inx dot
denotes fun
tion appli
ation). This last rule is
alled substitution of equals for equals
or Leibniz's rule .
Equality is a binary relation. When studying relations, re
exivity, symmetry and
transitivity are properties that we look out for. Equality is also a fun
tion. It is a
fun
tion with range the boolean values true and false . When we study fun
tions, the
January 7, 2007
67
sort of properties we look out for are asso
iativity and symmetry. For example, addition
and multipli
ation are both asso
iative: for all x , y and z ,
x + (y + z) = (x + y) + z
and
x (y z) = (x y) z .
and
xy = yx .
Symmetry of equality, viewed as a fun
tion, is just the same as symmetry of equality,
viewed as a relation. But, what about asso
iativity of equality? Is equality an asso
iative
operator?
The answer is that, in all but one
ase, the question doesn't make sense. Asso
iativity
of a binary fun
tion only makes sense if the domains of its two arguments and the range
of its result are all the same. The expression (p = q) = r just doesn't make sense when
p , q and r are numbers, or
hara
ters, or sequen
es, et
. The one ex
eption is equality
of boolean values. When p , q and r are all booleans it makes sense to
ompare the
boolean p = q with r for equality. That is, (p = q) = r is a meaningful boolean value.
Similarly, so too is p = (q = r) . It also makes sense to
ompare these two values for
equality. In other words, it makes sense to ask whether equality of boolean values is
asso
iative | and, perhaps surprisingly, it is. That is, for all booleans p , q and r ,
(5.1)
[Associativity]
((p = q) = r) = (p = (q = r)) .
You should
he
k this property by
onstru
ting truth tables for (p = q) = r and for
p = (q = r) and
omparing the entries. You should observe that the entries for whi
h
(p = q) = r is true are those for whi
h an odd number of p , q and r is true . If two of
the three are true, and the third is false , (p = q) = r is also false .
The asso
iativity of equality is a very powerful property, for one be
ause it enhan
es
e
onomy of expression. We will see several examples; an elementary example is the
following.
The re
exivity of equality is expressed by the rule
(p = p) = true .
January 7, 2007
68
This holds for all p , whatever its type (number, boolean, string, et
.). But, for boolean
p , we
an apply the asso
iativity of equality to get:
p = (p = true) .
This rule is most
ommonly used to simplify expressions by eliminating \ true " from an
expression of the form p = true . We use it several times below.
5.2.4
Hidden Treasures
We
an now return to the island of knights and knaves, and dis
over the hidden treasures.
Let us
onsider the rst problem posed in se
tion 5.1. What
an we dedu
e if a native
says \I am a knight equals there is gold on the island"? Let A stand for \the native is
a knight" and G stand for \there is gold on the island". Then the native's statement is
A = G , and we dedu
e that
A = (A = G)
is true. So,
true
=
A's statement }
A = (A = G)
=
(A = A) = G
=
(A = A) = true ,
equality is symmetri }
G = true
=
G = (G = true)
G .
We
on
lude that there is gold on the island, but it is not possible to determine whether
the native is a knight or a knave.
Suppose, now, that the native is at a fork in the road, and you want to determine
whether the gold
an be found by following the left or right fork. You want to formulate
January 7, 2007
69
a question su
h that the reply will be \yes" if the left fork should be followed, and \no"
if the right fork should be followed.
As usual, we give the unknown a name. Let Q be the question to be posed. Then,
as we saw earlier, the response to the question will be A = Q . Let L denote \the gold
an be found by following the left fork." The requirement is that L is the same as the
response to the question. That is, we require that L = (A = Q) . But,
L = (A = Q)
=
(L = A) = Q .
So, the question Q to be posed is L = A . That is, ask the question \Is the truth value
of `the gold
an be found by following the left fork' equal to the truth value of `you are
a knight' ".
Note that this analysis is valid independently of what L denotes. It might be that
you want to determine whether there is a restaurant on the island, or whether there are
any knaves on the island, or whatever. In general, if it is required to determine whether
some proposition P is true or false, the question to be posed is P = A . In the
ase of
more
omplex propositions P , the question may be simplied.
5.2.5
Equality is distinguished from other logi
al
onne
tives by Leibniz's rule: if two expressions are equal, one expression
an be substituted for the other. Here, we
onsider one
simple example of the use of Leibniz's rule.
Suppose there are three natives of the island, A, B and C, and C says \A
and B are both the same type". Formulate a question that, when posed to
A, determines whether C is telling the truth.
To solve this problem, we let A , B and C denote the propositions A (respe
tively,
B and C) is a knight. We also let Q be the unknown question.
The response we want is C . So, by the analysis in se
tion 5.2.4, Q = (A = C) . But,
C's statement is A = B . So we know that C = (A = B) . Substituting equals for equals,
Q = (A = (A = B)) . But, A = (A = B) simplies to B . So, the question to be posed is
\is B a knight?". Here is this argument again, but set out as a
al
ulation of Q , with
hints showing the steps taken at ea
h stage.
Q
=
January 7, 2007
70
A = (A = B)
=
(A = A) = B
=
(A = A) = true
(true = B) = B
true = B
=
{
B .
5.3
Asso
iative fun
tions are usually denoted by inx operators1 . The benet in
al
ulations
is immense. If a binary operator is asso
iative (that is, (xy)z = x(yz) for
all x , y and z ), we
an write xyz without fear of ambiguity. The expression
be
omes more
ompa
t be
ause of the omission of parentheses. More importantly, the
expression is unbiased; we may
hoose to simplify xy or yz depending on whi
h is
the most
onvenient. If the operator is also symmetri
(that is, xy = yx for all x
and y ) the gain is even bigger, be
ause then, if the operator is used to
ombine several
subexpressions, we
an
hoose to simplify uw any pair of subexpressions u and w .
Inx notation is also often used for binary relations. We write, for example, 0 m n .
Here, the operators are being used
onjun
tionally : the meaning is 0 m and m n .
In this way, the formula is more
ompa
t (sin
e m is not written twi
e). More importantly, we are guided to the inferen
e that 0 n . The algebrai
property that is being
hidden here is the transitivity of the at-most relation. If the relation between m and n
is m < n rather than m n and we write 0 m < n , we may infer that 0 < n . Here, the
inferen
e is more
omplex sin
e there are two relations involved. But, it is an inferen
e
that is so fundamental that the notation is designed to fa
ilitate its re
ognition.
In the
ase of equality of boolean values, we have a dilemma. Do we understand
equality as a relation and read a
ontinued expression of the form
x=y=z
inx operator is a symbol used to denote a fun
tion of two arguments that is written between the
two arguments. The symbols \ + " and \ " are both inx operators, denoting addition and multipli
ation,
respe
tively.
1 An
January 7, 2007
71
or, equally, as
x = (y = z) ,
in just the same way as we would read x+y+z ? The two readings are, unfortunately,
not the same (for example true = false = false is false a
ording to the rst reading but
true a
ording to the se
ond and third readings). There are advantages in both readings,
and it is a major drawba
k to have to
hoose one in favour of the other.
It would be very
onfusing and, indeed, dangerous to read x = y = z in any other way
than x = y and y = z ; otherwise, the meaning of a sequen
e of expressions separated by
equality symbols would depend on the type of the expressions. Also, the
onjun
tional
reading (for other types) is so universally a
epted |for good reasons| that it would
be quite una
eptable to try to impose a dierent
onvention.
The solution to this dilemma is to use two dierent symbols to denote equality of
boolean values | the symbol \ = " when the transitivity of the equality relation is to be
emphasised and the symbol \ " when its asso
iativity is to be exploited. A
ordingly,
we write both p = q and p q . When p and q are expressions denoting boolean
values, these both mean the same. But a
ontinued expression
pqr ,
omprising more than two boolean expressions
onne
ted by the \ " symbol, is to
be evaluated asso
iatively |i.e. as (p q) r or p (q r) , whi
hever is the most
onvenient| whereas a
ontinued expression
p=q=r
has the meaning given by fully parenthesising the expression (in any way whatsover,
sin
e the out
ome is not ae
ted) and then evaluating the expression as indi
ated by the
hosen parenthesisation.
January 7, 2007
72
[Reflexivity]
5.3.1
true p p .
This se
tion
ontains a
ouple of beautiful examples illustrating the ee
tiveness of the
asso
iativity of equivalen
e.
Even and Odd Numbers The rst example is the following property of the predi
ate
even on numbers. (A number is even exa
tly when it is a multiple of two.)
m+n is even m is even n is even .
It will help if we refer to whether or not a number is even or odd as the parity of the
number. Then, if we parenthesise the statement as
m+n is even (m is even n is even) ,
it states that the number m+n is even exa
tly when the parities of m and n are the
same. Parenthesising it as
(m+n is even m is even) n is even ,
it states that the operation of adding a number n to a number m does not
hange the
parity of m exa
tly when n is even.
Another way of reading the statement is to use the fa
t that, in general, the equivalen
e p q r is true exa
tly when an odd number of p , q and r is true. So the
property
aptures four dierent
ases:
or
or
or
(( m+n
(( m+n
(( m+n
(( m+n
is even)
is odd)
is odd)
is even)
and
and
and
and
(m
(m
(m
(m
is even)
is odd)
is even)
is odd)
and
and
and
and
(n
(n
(n
(n
is even))
is even))
is odd))
is odd)) .
The beauty of this example lies in the avoidan
e of
ase analysis. There are four
distin
t
ombinations of the two booleans \ m is even" and \ n is even". Using the
asso
iativity of equivalen
e the value of \ m+n is even" is expressed in one simple
formula, without any repetition of the
omponent expressions, rather than as a list of
dierent
ases. Avoidan
e of
ase analysis is vital to ee
tive reasoning.
January 7, 2007
73
Sign of Non-Zero Numbers The sign of a number says whether or not the number
is positive. For non-zero numbers x and y , the produ
t xy is positive if the signs of
x and y are equal. If the signs of x and y are dierent, the produ
t xy is negative.
Assuming that x and y are non-zero, this rule is expressed as
xy is positive x is positive y is positive .
Just as for the predi
ate even, this one statement neatly
aptures a number of dierent
ases, even though no
ase analysis is involved. Indeed, our justi
ation of the rule is
the statement
xy is positive (x is positive y is positive) .
The other parenthesisation |whi
h states that the sign of x is un
hanged when it is
multiplied by y exa
tly when y is positive| is obtained \for free" from the asso
iativity
of boolean equality.
5.3.2
On Natural Language
Many mathemati
ians and logi
ians are not aware that equality of booleans is asso
iative;
those that do are often unaware or dismissive of how ee
tive its use
an be. At the
present time, there is
onsiderable resistan
e to a shift in fo
us from impli
ation to
equality. Most
ourses on logi
introdu
e boolean equality as \if and only if". It is akin
to introdu
ing equality of numbers by rst introdu
ing the at-most ( ) and at-least
( ) relations, and then dening an \at-most and at-least" operator.
The most probable explanation lies in the fa
t that many logi
ians view the purpose
of logi
as formalising \natural" or \intuitive" reasoning, and our \natural" tenden
y
is not to reason in terms of equalities, but in
ausal terms. (\If it is raining, I will
take my umbrella.") The equality symbol was rst introdu
ed into mathemati
s by
Robert Re
orde in 1557, whi
h, in the history of mathemati
s, is quite re
ent; were
equality \natural" it would have been introdu
ed mu
h earlier. Natural language has
no
ounterpart to a
ontinued equivalen
e. To take a
on
rete example, the
ontinued
equivalen
e \a blind man
an see through two eyes equivales a blind man
an see through
one eye equivales a blind man
an see through no eyes" may seem very odd, if not
nonsensi
al, even though it is a
tually true!
This fa
t should not be a deterrent to the use of
ontinued equivalen
e. At one
time (admittedly, a very long time ago) there was probably similar resistan
e to the
introdu
tion of
ontinued additions and multipli
ations. The eviden
e is still present
in the language we use today. For example, the most
ommon way to express time is
in words: like \quarter to ten" or \ten past eleven". Cal
ulational requirements (eg.
wanting to determine how long is it before the train is due to arrive) have in
uen
ed
January 7, 2007
74
natural language so that, nowadays, people sometimes say, for example, 9:45 or 11:10 in
everyday spee
h. But, we still don't nd it a
eptable to say 10:70! Yet, this is what we
a
tually use when we want to
al
ulate the time dieren
e between 9.45 and 11:10. In
fa
t, several laws of arithmeti
, in
luding asso
iativity of addition, are fundamental to
the
al
ulation. Changes in natural language have o
urred, and will
ontinue to o
ur,
as a result of progress in mathemati
s, but will always lag a long way behind. The
language of mathemati
s has developed in order to over
ome the limitations of natural
language. The goal is not to mimi
\natural" reasoning, but to provide a more ee
tive
alternative.
5.4
Negation
Consider the following knights-and-knaves problem. There are two natives, A and B.
Native A says, \B is a knight equals I am not a knight". What
an you determine about
A and B?
This problem involves a so-
alled negation : the use of \not". Negation is a unary
operator (meaning that it is a fun
tion with exa
tly one argument) mapping a boolean
to a boolean, and is denoted by the symbol \ ", written as a prex to its argument. If
p is a boolean expression, \ p " is pronoun
ed \not p ".
Using the general rule that, if A makes a statement S , we know that A S , we get,
for this problem:
A B A .
(We swit
h from \ = " to \ " here in order to exploit asso
iativity.) The goal is to
simplify this expression.
In order to ta
kle this problem, it is ne
essary to begin by formulating
al
ulational
rules for negation. For arbitrary proposition p , the law governing p is:
(5.3)
[Negation]
p p false .
Reading this as
p = (p false) ,
January 7, 2007
75
5.4. Negation
rearranging terms }
A A B
=
false B
=
B .
So, B is a knave, but A
ould be a knight or a knave. Note how (5.3) is used in two
dierent ways.
The law (5.3), in
onjun
tion with the symmetry and asso
iativity of equivalen
e,
provides a way of simplifying
ontinued equivalen
es in whi
h one or more terms are
repeated and/or negated. Suppose, for example, we want to simplify
p p q p r q .
We begin by rearranging all the terms so that repeated o
urren
es of \ p " and \ q " are
grouped together. Thus we get
p p p q q r .
Now we
an use (5.2) and (5.3) to redu
e the number of o
urren
es of \ p " and \ q " to
at most one (possibly negated). In this parti
ular example we obtain
true p false r .
Finally, we use (5.2) and (5.3) again. The result is that the original formula is simplied
to
p r .
Just as before, this pro
ess
an be
ompared with the simpli
ation of an arithmeti
expression involving
ontinued addition, where now negative terms may also appear.
The expression
p + (p) + q + (p) + r + q + (q) + r + p
January 7, 2007
76
is simplied to
q + 2r
false false
=
true .
5.5
Contraposition
A rule that should now be obvious, but whi
h is surprisingly useful, is the rule we
all
ontraposition 2 .
(5.4)
[Contraposition]
p q p q .
In words, the number of
rossings in
reases by one, and the boat
hanges side. The rule
of
ontraposition tells us that
even.n l
tion.
authors use the name \ ontraposition" for a less general rule ombining negation with impli a-
January 7, 2007
77
5.5. Contraposition
(even.n l)[n , l := n+1 , l]
=
rule of substitution }
even.(n+1) l
=
even.(n+1) (even.n)
(even.n) l
=
ontraposition }
even.n l .
We are given that, initially, the boat is on the left side. Sin
e zero is an even number,
we
on
lude that even.n l is invariantly true . In words, the boat is on the left side
equivales the number of
rossings is even.
Another example is the following. Suppose it is required to move a square arm
hair
sideways by a distan
e equal to its own width. (See gure 5.1.) However, the
hair is
so heavy that it
an only be moved by rotating it through 90 around one of its four
orners. Is it possible to move the
hair as desired? If so, how? If not, why not?
January 7, 2007
78
So, the value of this expression will remain equal to its initial value, no matter how many
times the arm
hair is rotated. But, moving the arm
hair sideways one square
hanges
the
olour but does not
hange the dire
tion. That is, it
hanges the value of col dir ,
and is impossible to a
hieve by
ontinually rotating the arm
hair as pres
ribed.
In words, an invariant of rotating the arm
hair through 90 around a
orner point is
the
hair is on a bla
k square the
hair is fa
ing north-south
whi
h is false when the
hair is on a white square and fa
ing north-south.
90
January 7, 2007
79
5.6
Handshake Problems
Logi
al properties of negation are fundamental to solving so-
alled handshake problems .
The simplest example of a handshake problem is this: Suppose that at a party, some
people shake hands and some don't. Suppose ea
h person
ounts the number of times
they shake hands. Show that at least two people have the same
ount.
Cru
ial to how we solve this problem are the properties of shaking hands. These are
that it is a binary relation ; it is symmetri
, and it is anti-re
exive . It being a \binary
relation" on people, means that, for any two people |Ja
k and Jill, say| Ja
k shakes
hands with Jill is either true or false. (In general, a relation is any boolean-valued
fun
tion. Binary means that it is a fun
tion of two arguments.) It being a \symmetri
"
relation means that, for any two people |Ja
k and Jill, say| Ja
k shakes hands with
Jill equivales Jill shakes hands with Ja
k. Finally, it being \anti-re
exive" means that
no-one shakes hands with themselves.
We are required to show that (at least) two people shake hands the same number
of times. Let us explore the
onsequen
es of the properties of the shake-hands relation
with respe
t to the number of times that ea
h person shakes hands.
Suppose there are n people. Then, everyone shakes hands with at most n people.
However, the anti-re
exivity property is that noone shakes hands with themselves. We
on
lude that everyone shakes hands with between 0 and n1 people.
There are n numbers in the range 0 to n1 . The negation of \two people shake
hands the same number of times" is \everyone shake hands a distin
t number of times".
In parti
ular, someone shakes hands 0 times and someone shakes hands n1 times.
The symmetry of the shake-hands relation makes this impossible.
Suppose we abbreviate \shake hands" to S , and suppose we use x and y to refer
to people. In this way, xSy is read as \person x shakes hands with person y ", or just
x shakes hands with y . Then the symmetry of \shakes hands" gives us the rule, for all
x and y ,
xSy ySx .
In words, x doesn't shake hands with y equivales y doesn't shake hands with x . Now,
suppose person a shakes hands with noone and person b shakes hands with everyone.
Then, in parti
ular, a does not shake hands with b , i.e. (aSb) , and b shakes hands
with a , i.e. bSa . But then, substituting equals for equals, we have both (aSb) and
aSb , whi
h is false.
January 7, 2007
80
The assumption that everyone shakes hands with a distin
t number of people has
led to a
ontradi
tion, and so we
on
lude that two people must shake hands the same
number of times.
Note
arefully how the symmetry and anti-re
exivity of the shakes-hands relation
are
ru
ial. Were we to
onsider a similar problem involving a dierent relation, the
out
ome might be dierent. For example, if we repla
e \shake hands" by some other
form of greeting like \bows or
urtsies", whi
h is not symmetri
, the property need not
hold3. (Suppose there are two people, and one bows to the other, but the greeting is
not returned.) However, if \shake hands" is repla
ed by \rub noses", the property does
hold. Like \shake hands", \rub noses" is a symmetri
and anti-re
exive relation.
Here is another handshaking problem. It's a bit more di
ult to solve,
but the essen
e of the problem remains the same: \shake hands" is a symmetri
relation,
as is \don't shake hands".
Suppose a number of
ouples (husband and wife) attend a party. Some people
shake hands, others do not. Husband and wife never shake hands. One person, the
\host", asks everyone else how many times they have shaken hands, and gets a different answer every time. How many times did the host and the host's partner shake
hands?
Exercise 5.6
5.7
Inequivalence
B A false
=
B A .
3 At
the time of writing, anyone meeting the British Queen is required to bow or
urtsey, whereas the
Queen never does. The relation is not symmetri
.
January 7, 2007
81
5.7. Inequivalen e
[Inequivalence]
(p q) p q .
Note how asso
iativity of equivalen
e has been used silently in this
al
ulation. Note
also how asso
iativity of equivalen
e in the summary of the
al
ulation gives us two
properties for the pri
e of one. The rst is the one proved dire
tly:
(p q) = (p q) ,
((p q) r)
=
(p q) r
=
p q r
=
p (q r)
=
(p (q r))
=
p 6 (q 6 r) .
As a result, we
an write the
ontinued inequivalen
e p 6 q 6 r without fear of ambiguity4. Note that, as a byprodu
t, we have shown that p 6 q 6 r and p q r are
equal.
As a nal worked example, we show that inequivalen
e asso
iates with equivalen
e:
4 This
is to be read asso
iatively, and should not be
onfused with p 6= q 6= r , whi
h some authors
o
asionally write. Inequality is not transitive, so su
h expressions should be avoided.
January 7, 2007
82
(p q) r
=
(p q) p q
p q r
=
p (q r)
=
denition of q 6 r
p (q 6 r) .
Exercise 5.8
Exercise 5.9
p = p .
January 7, 2007
83
5.8. Summary
(p 6 (q 6 r)) ((p 6 q) 6 r) ,
is used to en
rypt data. To en
rypt a single bit b of data, a key a is
hosen and the
en
rypted form of b that is transmitted is a 6 b . The re
eiver de
rypts the re
eived
bit, c , using the same operation5. That is, the re
eiver uses the same key a to
ompute
a 6 c . Show that, if bit b is en
rypted and then de
rypted in this way, the result is b
independently of the key a .
5.8
Summary
In this
hapter, we have used simple logi
puzzles to introdu
e logi
al equivalen
e |
the equality of boolean values| , the most fundamental logi
al operator. Equivalen
e
has the remarkable property of being asso
iative, in addition to the standard properties
of equality. Exploitation of the asso
iativity of equivalen
e eliminates the tedious and
inee
tive
ase analysis that is often seen in solutions to logi
puzzles.
The asso
iativity of equivalen
e
an be di
ult to get used to, parti
ularly if one
tries to express its properties in natural language. However, this should not be used
as an ex
use for ignoring it. The painful,
enturies-long pro
ess of a
epting zero as a
number, and introdu
ing the symbol \ 0 " to denote it, provides ample eviden
e that the
adheren
e to \natural" modes of reasoning is a major impediment to ee
tive reasoning.
The purpose of a
al
ulus is not to mimi
\natural" or \intuitive" reasoning, but to
provide a more powerful alternative.
The fa
t that equality of boolean values is asso
iative has been known sin
e at least the
1920's , having been mentioned by Alfred Tarski in his PhD thesis, where its dis
overy is
5 This
operation is usually
alled \ex
lusive-or" in texts on data en
ryption; it is not
ommonly known
that ex
lusive-or and inequivalen
e are the same. Inequivalen
e
an be repla
ed by equivalen
e in the
en
ryption and de
ryption pro
ess. But, very few s
ientists and engineers are aware of the algebrai
properties of equivalen
e, and this possibility is never exploited!
January 7, 2007
84
attributed to J. Lukasiewi
z. (See the paper \On the primitive term of logisti
" [Tar56;
Tarski is a famous logi
ian.) Nevertheless, its usefulness was never re
ognised until
brought to the fore by E.W. Dijkstra in his work on program semanti
s and mathemati
al
method. (See e.g. [DS90.)
The origin of the logi
puzzles is Raymond Smullyan's book \What Is The Name Of
This Book?" [Smu78. This is an entertaining book whi
h leads on from simple logi
puzzles to a dis
ussion of the logi
al paradoxes and Godel's unde
idability theorem.
But Smullyan's proofs invariably involve detailed
ase analyses. The exploitation of the
asso
iativity of equivalen
e in the solution of su
h puzzles is due to Gerard Wiltink
[Wil87. For a
omplete a
ount of
al
ulational logi
, whi
h in
ludes dis
ussion of
onjun
tion (\and"), disjun
tion (\or"), follows-from (\if") and impli
ation (\only if"),
see [Ba
03 or [GS93.
January 7, 2007
Chapter 6
Induction
\Indu
tion" is the name given to a problem-solving te
hnique based on using the solution
to small instan
es of a problem to solve larger instan
es of the problem.
The idea is that we somehow measure the \size" of instan
es of a problem. For
example, the problem might involve a pile of mat
hsti
ks, where the number of mat
hes
is a parameter; an instan
e of the problem is then a parti
ular pile of mat
hes, and its
size is the number of mat
hes in the pile. A requirement is that the size is a non-negative,
whole number | thus 0 , 1 , 2 , 3 , et
. We use the term natural number for a nonnegative, whole number1. Usually, how the size of an instan
e of a problem is measured
is quite obvious from the problem des
ription.
Having de
ided how to measure size, we then solve the problem in two steps. First, we
onsider problems of size 0 . This is
alled the basis of the indu
tion. Almost invariably,
su
h problems are very easy to solve. (They are often dismissed as \trivial".) Se
ond,
we show how to solve, for an arbitrary natural number n , a problem of size n+1 , given
a solution to a problem of size n . This is
alled the indu
tion step.
By this pro
ess, we
an solve problems of size 0 . We also know how to solve problems
of size 1 ; we apply the indu
tion step to
onstru
t a solution to problems of size 1 from
the known solution to problems of size 0 . Then, we know how to solve problems of size
2 ; we apply the indu
tion step again to
onstru
t a solution to problems of size 2 from
the known solution to problems of size 1 . And so it goes on. We
an now solve problems
of size 3 , then problems of size 4 , et
.
6.1
Example Problems
All the following problems
an be solved by indu
tion. In the rst, the size is the number
of lines. In the se
ond and third problems, it is expli
tly given by the parameter n , and
1 Warning :
Mathemati
ians often ex
lude the number 0 from the natural numbers. There are,
however, very good reasons why 0 should always be in
luded, making a break with tradition imperative.
85
January 7, 2007
86
6. Indu tion
in the fourth, it is the number of disks. In ea
h
ase, the basis should be easy. You then
have to solve the indu
tion step. We dis
uss ea
h problem in turn in
oming se
tions.
1. Cutting the Plane
A number of straight lines are drawn a
ross a sheet of paper, ea
h line extending
all the way from from one border to another. See g. 6.1. In this way, the paper
is divided into a number of regions. Show that it is possible to
olour ea
h region
bla
k or white so that no two adja
ent regions have the same
olour (that is, so
that the two regions on opposite sides of any line segment have dierent
olours).
January 7, 2007
87
January 7, 2007
88
6. Indu tion
January 7, 2007
89
6.2
January 7, 2007
90
6. Indu tion
The
ase where there are zero lines is easy. The sheet of paper is divided into one
region, and this
an be
oloured bla
k or white, either
olouring meeting the
onditions
of a solution (be
ause there is no pair of adja
ent regions).
For the indu
tion step, we assume that a number of lines have been drawn on the
sheet of paper, and the dierent regions have been
oloured bla
k or white so that no
two adja
ent regions have the same
olour. This assumption is
alled the indu
tion
hypothesis. We now suppose that an additional line is drawn on the paper. This will
divide some of the existing regions into two; su
h pairs of regions will have the same
olour, and so the existing
olouring is not satisfa
tory. Fig. 6.7 is an example. The
plane has been divided into twelve regions by ve lines, and the regions
oloured bla
k
and white, as required. An additional line, shown in red for
larity, has been added. This
has had the ee
t of dividing four of the regions into two, thus in
reasing the number
of regions by four. On either side of the red line, the regions have the same
olour.
Elsewhere, adja
ent regions have dierent
olours. The task is to show how to modify
the
olouring so that it does indeed be
ome a satisfa
tory solution.
January 7, 2007
91
olouring hasn't
hanged, and was satisfa
tory already). Also, the
olouring of adja
ent
regions at the boundary of the left and right regions is satisfa
tory, be
ause they have
hanged from being the same to being dierent.
Fig. 6.8 shows the ee
t on our example. Blue has been used instead of bla
k in
order to make the inversion of the
olours more evident.
Figure 6.8: Cutting the Plane. The
olours are inverted to one side of the additional line
(bla
k is shown as blue to make
lear whi
h
olours have
hanged).
This
ompletes the indu
tion step. In order to apply the
onstru
tion to an instan
e
of the problem with, say, seven lines, we begin by
olouring the whole sheet of paper.
Then the lines are added one-by-one. Ea
h time a line is added, the existing
olouring
is modied as pres
ribed in the indu
tion step, until all seven lines have been added.
The algorithm is non-deterministi
in several ways. The initial
olouring of the sheet
of paper (bla
k or white) is unspe
ied. The ordering of the lines (whi
h to add rst,
whi
h to add next, et
.) is also unspe
ied. Finally, whi
h region is
hosen as the \left"
region, and whi
h the \right" region is unspe
ied. This means that the nal
olouring
may be a
hieved in lots of dierent ways. But that doesn't matter. The nal
olouring
is guaranteed to be \satisfa
tory", as required in the problem spe
i
ation.
Che
k your understanding by
onsidering variations on the problem.
Why is it required that the lines are straight? How might this assumption be relaxed
without invalidating the solution.
The problem assumes the lines are drawn on a pie
e of paper. Is the solution still
valid if the lines are drawn on the surfa
e of a ball, or on the surfa
e of a doughnut?
We remarked that the algorithm for
olouring the plane is non-deterministi
. How
many dierent
olourings does it
onstru
t?
Exercise 6.1
January 7, 2007
92
6.3
6. Indu tion
Triominoes
As a se
ond example of an indu
tive
onstru
tion, let us
onsider the grid problem posed
in se
tion 6.1. Re
all the statement of the problem.
A square pie
e of paper is divided into a grid of size 2n2n , where n is
a natural number. The individual squares are
alled grid squares. One grid
square is
overed, and the others are left un
overed. A triomino is an L-shape
made of three grid squares. Show that it is possible to
over the remaining
squares with (non-overlapping) triominoes.
The obvious measure of the \size" of instan
es of the problem, in this
ase, is the
number n . We solve the problem by indu
tion on n.
The base
ase is when n equals 0 . The grid then has size 2020 , i.e. 11 . That
is, there is exa
tly one square. This one square is, inevitably, the one that is
overed,
leaving no squares un
overed. It takes 0 triominoes to
over no squares! This, then, is
how the base
ase is solved.
Now, suppose we
onsider a grid of size 2n+12n+1 . We make the indu
tion hypothesis that it is possible to
over any grid of size 2n2n with triominoes if, rst, an
arbitrary grid square has been
overed. We have to show how to exploit this hypothesis
in order to
over a grid of size 2n+12n+1 of whi
h one suare has been
overed.
A grid of size 2n+12n+1
an be subdivided into 4 grids ea
h of size 2n2n , simply
by drawing horizontal and verti
al dividing lines through the middle of ea
h side. Let
us
all the four grids the bottom-left, bottom-right, top-left, and top-right grids. One
grid square is already
overed. This square will be in one of the four sub-grids. We may
assume that it is in the bottom-left grid. (If not, the entire grid
an be rotated about
the
entre so that it does be
ome the
ase.)
The bottom-left grid is thus a grid of size 2n2n of whi
h one square has been
overed. By the indu
tion hypothesis, the remaining squares in the bottom-left grid
an
be
overed with triominoes. This leaves us with the task of
overing the bottom-right,
top-left and top-right grids with triominoes.
None of the squares in these three grids is
overed, as yet. We
an apply the indu
tion
hypothesis to them if just one square in ea
h of the three is
overed. This is done by
pla
ing a triomino at the jun
tion of the three grids, as shown in g. 6.9.
Now, the indu
tive hypothesis is applied to
over the remaining squares of the bottomright, top-left and top-right grids with triominoes. On
ompletion of this pro
ess, the
entire 2n+12n+1 grid has been
overed with triominoes.
Exercise 6.2
January 7, 2007
93
Figure 6.9: Triomino Problem. Indu
tive Step. The grid is divided into four sub-grids.
The
overed square, shown in bla
k, identies one sub-grid. A triomino, shown in blue,
is pla
ed at the jun
tion of the other three grids. The indu
tion hypothesis is then used
to
over all four sub-grids with triominoes.
6.4
In se
tions 6.2 and 6.3, we have seen how indu
tion is used to solve problems of a
given \size". Te
hni
ally, the pro
ess we des
ribed is
alled \mathemati
al indu
tion";
\indu
tion", as it is normally understood, is more general.
\Indu
tion", as used in, for example, the experimental s
ien
es, refers to a pro
ess of
reasoning whereby general laws are inferred from a
olle
tion of observations. A famous
example of indu
tion is the pro
ess that led Charles Darwin to formulate his theory
of evolution by natural sele
tion, based on his observations of plant and animal life in
remote parts of the world. In simple terms, indu
tion is about looking for patterns.
Laws formulated by a pro
ess of indu
tion go beyond the knowledge on whi
h they
are based, thus introdu
ing inherently new knowledge. In the experimental s
ien
es,
however, su
h laws are only probably true; they are tested by the predi
tions they
make, and may have to be dis
arded if the predi
tions turn out to be false. In
ontrast,
dedu
tion is the pro
ess of inferring laws from existing laws, whereby the dedu
tions
made are guaranteed to be true provided that the laws on whi
h they are based are true.
In a sense, laws dedu
ed from existing laws add nothing to our sto
k of knowledge sin
e
they are, at best, simply reformulations of existing knowledge.
Mathemati
al indu
tion is a
ombination of indu
tion and dedu
tion. It's a pro
ess of looking for patterns in a set of observations, formulating the patterns as
onje
tures, and then testing whether the
onje
tures
an be dedu
ed from existing knowledge.
Guess-and-verify is a brief way of summarising mathemati
al indu
tion. (Guessing is the
formulation of a
onje
ture; veri
ation is the pro
ess of dedu
ing whether the guess is
January 7, 2007
94
6. Indu tion
orre
t.)
Several of the mat
hsti
k games studied in
hapter 4 provide good examples of mathemati
al indu
tion. Re
all, for example, the game dis
ussed in se
tion 4.2.2: there is
one pile of mat
hes from whi
h it is allowed to remove one or two mat
hes. Exploring
this game, we dis
overed that a pile with 0 , 3 or 6 mat
hes is a losing position, and
piles with 1 , 2 , 4 , 5 , 7 and 8 mat
hes are winning positions. There seems to be
a pattern in these numbers: losing positions are the positions in whi
h the number of
mat
hes are a multiple of 3 , and winning positions are the remaining positions. This is a
onje
ture about all positions made from observations on just nine positions. However,
we
an verify that the
onje
ture is true by using mathemati
al indu
tion to
onstru
t
a winning strategy.
In order to use indu
tion, we measure the \size" of a pile of mat
hes not by the
number of mat
hes but by the number of mat
hes divided by 3 , rounded down to the
nearest natural number. So, the \size" of a pile of 0 , 1 or 2 mat
hes is 0 , the \size"
of a pile of 3 , 4 or 5 mat
hes is 1 , and so on. The indu
tion hypothesis is that a pile
of 3n mat
hes is a losing position, and a pile of 3n + 1 or 3n + 2 mat
hes is a winning
position.
The basis for the indu
tion is when n equals 0 . A pile of 0 mat
hes is, indeed, a
losing position be
ause, by denition, the game is lost when it is no longer possible to
move. A pile of 1 or 2 mat
hes is a winning position be
ause the player
an remove
the mat
hes, leaving the opponent in a losing position.
Now, for the indu
tion step, we assume that a pile of 3n mat
hes is a losing position,
and a pile of 3n + 1 or 3n + 2 mat
hes is a winning position. We have to show that
a pile of 3(n+1) mat
hes is a losing position, and a pile of 3(n+1) + 1 or 3(n+1) + 2
mat
hes is a winning position.
Suppose there are 3(n+1) mat
hes. The player, whose turn it is, must remove 1 or
2 mat
hes, leaving either 3(n+1) 1 or 3(n+1) 2 behind. That is, the opponent is
left with either 3n + 2 or 3n + 1 mat
hes. But, by the indu
tion hypothesis, this leaves
the opponent in a winning position. Hen
e, the position in whi
h there are 3(n+1) is a
losing position.
Now, suppose there are 3(n+1) + 1 or 3(n+1) + 2 mat
hes. By taking 1 mat
h in
the rst
ase, and 2 mat
hes in the se
ond
ase, the player leaves the opponent in a
position where there are 3(n+1) mat
hes. This we now know to be a losing position.
Hen
e, the positions in whi
h there are 3(n+1) + 1 or 3(n+1) + 2 are both winning
positions.
This
ompletes the indu
tive
onstru
tion of the winning moves, and thus veries the
onje
ture that a position is a losing position exa
tly when the number of mat
hes is a
multiple of 3 .
January 7, 2007
95
6.5
When using indu
tion, it is vital that any
onje
ture is properly veried. It is too easy to
extrapolate from a few
ases to a more general
laim that is not true. Many
onje
tures
turn out to be false; only by subje
ting them to the rigours of proof
an we be sure of
their validity. This se
tion is about a non-trivial example of a false
onje
ture.
Suppose n points are marked on the
ir
umferen
e of a
ir
ular
ake and then the
ake is
ut along the
hords joining them. The points are
hosen in su
h a way that all
interse
tion points of pairs of
hords are distin
t. The question is, in how many portions
does this
ut the
ake?
Figure 6.10 shows the
ase when n is 1 , 2 , 3 or 4 .
6.6
In mathemati
al texts, indu
tion is often used to verify known formulae. Veri
ation is
important but has a major drawba
k | it seems that a substantial amount of
lairvoyan
e
January 7, 2007
96
6. Indu tion
Figure 6.11: Cutting the
ake. The
ase n = 6 . The number of portions is 31 , not 261
is needed to
ome up with the formula that is to be veried. And, if one's
onje
ture is
wrong, veri
ation gives little help in determining the
orre
t formula.
Indu
tion is not important in
omputing s
ien
e as a veri
ation prin
iple but be
ause it is a fundamental prin
iple in the
onstru
tion of
omputer programs. This
se
tion introdu
es the use of indu
tion in the
onstru
tion of mathemati
al formulae.
The problem we
onsider is how to determine a
losed formula for the sum of the
k th powers of the rst n natural numbers.
A well-known formula gives the sum of the natural numbers from 1 thru n :
1+2+ ... +n =
1
n(n+1) .
2
Two other exer
ises, often given in mathemati
al texts, are to verify that
12 + 22 + . . . + n2 =
1
n(n+1)(2n + 1)
6
13 + 23 + . . . + n3 =
1 2
n (n+1)2 .
4
and
As well as being good examples of the strength of the prin
iple of mathemati
al indu
tion,
the examples also illustrate the weakness of veri
ation: the te
hnique works if the answer
is known, but what happens if the answer is not already known! Suppose, for example,
that you are now asked to determine a
losed formula for the sum of the 4 th powers of
the rst n numbers
14 + 24 + . . . + n4 = ? .
January 7, 2007
97
How would you pro
eed? Veri
ation, using the prin
iple of mathemati
al indu
tion,
does not seem to be appli
able unless we already know the right side of the equation.
Can you guess what the right side would be in this
ase? Can you guess what the right
side would be in the
ase that the term being summed is, say, k27 ? Almost
ertainly,
not!
Constru
ting solutions to non-trivial problems involves a
reative pro
ess. This means
that a
ertain amount of guesswork is ne
essary, and trial-and-error
annot be
ompletely
eliminated. Redu
ing the guesswork to a minimum, repla
ing it by mathemati
al
al
ulation is the key to su
ess.
Indu
tion
an be used to
onstru
t
losed formulae for su
h summations. The general
idea is to seek a pattern, formulate the pattern in pre
ise mathemati
al terms and then
verify the pattern. The key to su
ess is simpli
ity. Don't be over-ambitious. Leave the
work to mathemati
al
al
ulation.
A simple pattern in the formulae displayed above is that, for m equal to 1 , 2 and
3 , the sum of the m th powers of the rst n numbers is a polynomial in n of degree
m+1 . (The sum of the rst n numbers is a quadrati
fun
tion of n , the sum of the
rst n squares is a
ubi
fun
tion of n , and the sum of the rst n
ubes is a quarti
fun
tion of n .) This pattern is also
onrmed in the (oft-forgotten)
ase that m is 0 :
10 + 20 + . . . + n0 = n .
A strategy for determining a
losed formula for, say, the sum of the fourth powers is thus
to guess that it is a fth degree polynomial in n and then use indu
tion to
al
ulate
the
oe
ients. The
al
ulation in this
ase is quite long, so let us illustrate the pro
ess
by showing how to
onstru
t a
losed formula for 1 + 2 + . . . + n . (Some readers will
already know a simpler way of deriving the formula in this parti
ular
ase. If this is the
ase, please bear with us. The method des
ribed here is more general.)
We
onje
ture that the required formula is a se
ond degree polynomial in n , say
a + bn + cn2 and then
al
ulate the
oe
ients a , b and c . Here is how the
al
ulation
goes.
For brevity, let us use S.n to denote
1+2+ ... +n .
Then,
P.0
=
denition of P
January 7, 2007
98
6. Indu
tion
S.0 = a + b0 + c02
=
So the basis of the indu
tion has allowed us to dedu
e that a , the
oe
ient of n0 , is
0 . Now, we
al
ulate b and c . To do so, we make the indu
tion hypothesis that 0 n
and P.n is true. Then
P.(n+1)
=
denition of P , a = 0
S.(n+1) = S.n + n + 1
arithmeti }
c = c b+1 = b + 2c 1 = b + c
=
arithmeti }
{
1
2
=c
1
2
=b .
From the
onje
ture that the sum of the rst n numbers is a quadrati
in n , we have
thus
al
ulated that
1+2+ ... +n =
1
1
n + n2 .
2
2
Extrapolating from this
al
ulation, one
an see that it embodies an algorithm to express
1m + 2m + . . . + nm as a polynomial fun
tion for any given natural number m . The steps
in the algorithm are: postulate that the summation is a polynomial in n with degree
m+1 . Use the prin
iple of mathemati
al indu
tion together with the fa
ts that S.0 is 0
and S.(n+1) is S.n + (n+1)m (where S.n denotes 1m + 2m + . . . + nm ) to determine
January 7, 2007
99
...
n ,
n1 +
...
1 .
...
n+1 .
n+1 +
From the fa
t that there are n o
urren
es of n+1 we
on
lude that the sum is
1
n(n+1) . However, this method
annot be used for determining 1m + 2m + . . . + nm
2
for m greater than 1 . End of remark.
Exercise 6.3
10 + 20 + . . . + n0 and 12 + 22 + . . . + n2 .
Exercise 6.4
Consider a mat
hsti
k game with one pile of mat
hes from whi
h m
thru n mat
hes
an be removed. By
onsidering a few simple examples (for example, m
is 1 and n is arbitrary, or m is 2 and n is 3 ), formulate a general rule for determining
whi
h are the winning positions and whi
h are the losing positions, and what the winning
strategy is.
Avoid guessing the
omplete solution. Try to identify a simple pattern in the way
winning and losing positions are grouped. Introdu
e variables to represent the grouping,
and
al
ulate the values of the variables.
6.7
Fake-Coin Detection
The motto of se
tion 6.6
an be summarised as \Don't guess! Cal
ulate." We put this
into pra
ti
e in this se
tion.
January 7, 2007
100
6. Indu tion
Suppose we are given a number of
oins, ea
h of the same size and shape. We are
told that among them there is at most one \fake"
oin, and all the rest are \genuine".
All \genuine"
oins have the same weight, whereas a \fake"
oin has a dierent weight
to a \genuine"
oin. The problem is how to use the pair of s
ales optimally in order to
nd the fake
oin, if there is one.
Note the element of vagueness in this problem statement; we don't say what we mean
by using the s
ales \optimally" This is deliberate. Often, an essential element of problem
solving is to
learly identify the problem itself. Our formulation of the problem and its
eventual solution illustrates several other aspe
ts of \real" problem solving. Several
stages are needed, in
luding some \ba
ktra
king" and revision.
6.7.1
Problem Formulation
When we use a pair of s
ales to
ompare two weights |an operation that we
all a
omparison | there are 3 possible out
omes: the s
ales may tip to the left, they may
balan
e, or they may tip to the right. This means that with n
omparisons, there are
at most 3n dierent out
omes3. This gives an upper bound on what
an be a
hieved
using a pair of s
ales.
Now, suppose we are given m
oins, of whi
h at most one is fake and the rest are
genuine. Then there are 1 + 2m dierent possibilities that
an be observed with a pair
of s
ales: \ 1 " possibility is that all
oins are genuine; otherwise, there are \ 2 " ways
that ea
h of the \ m "
oins may be fake (by being lighter or heavier than a genuine
oin). This means that, with n
omparisons, the number of
oins among whi
h at most
one fake
oin
an be dete
ted is at most m , where 1 + 2m = 3n . More pre
isely, if the
number, m , of
oins is greater than (3n1)/2 , it is impossible to guarantee that a fake
oin
an be found with n
omparisons.
We have almost rea
hed the point at whi
h we
an state our problem pre
isely. We
onje
ture that, given (3n1)/2
oins of whi
h at most one is fake, it is possible to
establish that all are genuine or identify the fake
oin (and whether it is lighter or
heavier than a genuine
oin) using at most n
omparisons.
For n equal to 0 , the
onje
ture is
learly true; in this
ase, there are no
oins, all of
whi
h are genuine. For n equal to 1 , however, we run into a problem. The assumption
is that there is one
oin (sin
e (311)/2 = 1 ). But how
an we tell whether this one
oin
is fake or genuine, if there are no other
oins to
ompare it with? Our
onje
ture has
broken down, and needs revision.
We propose to modify the
onje
ture by assuming that we have at our disposal at
least one additional
oin that is known to be genuine. Thus, we are given (3n1)/2
oins about whi
h we know nothing ex
ept that at most one is fake, and we are also given
3 Note
January 7, 2007
101
at least one
oin that is known to be genuine. The problem is to
onstru
t an algorithm
that will identify the fake
oin, if it exists, or determine that all
oins are genuine, using
at most n
omparisons.
6.7.2
Problem Solution
Our formulation of the problem begs the use of indu
tion on the number of
omparisons,
n , in its solution.
The Basis With zero
omparisons, we
an report immediately that all
oins in a
olle
tion of (301)/2 are genuine. The base
ase, n equal to 0 , is thus solved.
Induction Step Now, we ta
kle the indu
tion step. Suppose n is at least zero. For
brevity, let us use c.n to denote (3n1)/2 . By indu
tion, we may assume that a fake
oin, if it exists,
an be found among c.n
oins using at most n
omparisons. We have
to show how to nd a fake
oin, if it exists, among c.(n+1)
oins, using at most n+1
omparisons.
Consider the rst
omparison. It involves putting some number of
oins on the left
s
ale, some on the right s
ale, and leaving some on the table. To be able to draw any
on
lusion from the
omparison, the number of
oins on the two s
ales must be equal.
One possible
onsequen
e of the
omparison is that the s
ales balan
e, from whi
h one
infers that none of the
oins on the s
ales is fake. The algorithm would then pro
eed to
try to nd a fake
oin among the
oins left on the table.
Combined with the indu
tion hypothesis, this di
tates that c.n
oins must be left
on the table. This is be
ause c.n is the maximum number of
oins among whi
h a fake
oin
an be found with n
omparisons.
It also di
tates how many
oins should be put on the s
ales | this is the dieren
e
between c.(n+1) and c.n . Now,
c.(n+1) = (3n+11)/2 = 3((3n1)/2) + 1 = 3 c.n + 1 .
So
c.(n+1) c.n = 2 c.n + 1 = 3n .
This is an odd number; it
an be made even by using one of the
oins we know to be
genuine. (Re
all the assumption that we have at least one
oin that is known to be
genuine, in addition to the c.(n+1)
oins whose kind we must determine.) We
on
lude
that in the rst
omparison, c.n + 1
oins should be put on ea
h of the two s
ales.
The next step is to determine what to do after the rst
omparison is made. There are
three possible out
omes, of whi
h we have already dis
ussed one. If the s
ales balan
e,
January 7, 2007
102
6. Indu tion
the fake
oin should be sought among the c.n
oins left on the table. The problem is
what to do if the s
ales tip either to the left or to the right.
At this point, we realise that the indu
tion hypothesis doesn't help. It is too weak! If
the s
ales tip to one side, we
an
on
lude that all the
oins left on the table are genuine,
and
an be eliminated from
onsideration. But we are still left with 3n
oins none of
whi
h we know to be genuine. And
ru
ially, 3n is greater than c.n . We are unable to
apply the indu
tion hypothesis to this number of
oins.
The
omparison does tell us something about the
oins on the s
ales. If the s
ales tip
to one side, we know that all the
oins on that side are possibly heavier than a genuine
oin, and all the
oins on the other side are possibly lighter than a genuine
oin. By
\possibly lighter" we mean genuine, or fake and lighter. By \possibly heavier" we mean
genuine, or fake and heavier. After the
omparison, we
an mark all the
oins on the
s
ales one way or the other.
The Marked Coin Problem In this way, in the
ase that the s
ales do not balan
e,
the problem we started with has been redu
ed to a dierent problem. The new problem
is this. Suppose a number of
oins are supplied, ea
h of whi
h is marked either \possibly
light" or \possibly heavy". Exa
tly one of the
oins is fake, and all the rest are genuine.
Constru
t an algorithm that will determine, with at most n
omparisons, the fake
oin
among 3n marked
oins.
Again, the base
ase is easy. If n equals 0 , there is one
oin, whi
h must be the fake
oin. That is, 0 (i.e. n )
omparisons are needed to determine this fa
t.
For the indu
tion step, we pro
eed as for the earlier problem. Suppose we are supplied
with 3n+1 marked
oins. In the rst
omparison, some
oins are put on the left s
ale,
some on the right, and some are left on the table. In order to apply the indu
tion
hypothesis in the
ase that the s
ales balan
e, the
oins must be divided equally: 3n
oins must be left on the table, and thus 3n put on the left s
ale and 3n on the right
s
ale.
The
oins are marked in two dierent ways. So, we need to determine how to pla
e
the
oins a
ording to their markings. We
al
ulate the numbers as follows.
Suppose l1 possibly light
oins are pla
ed on the left s
ale and l2 possibly light
oins on the right s
ale. Similarly, suppose h1 possibly heavy
oins are pla
ed on the
left s
ale and h2 possibly heavy
oins on the right s
ale.
To draw any
on
lusion from the
omparison, we require that the number of
oins
on the left s
ale equals the number on the right. That is, l1+h1 and l2+h2 should be
equal. Furthermore, as already determined, they should equal 3n .
Now, if the
omparison
auses the s
ales to tip to the left, we
on
lude that all
oins
on the left s
ale are possibly heavy, and all the
oins on the right s
ale are possibly
light. Combining this with the markings, we
on
lude that the l1 possibly light
oins
January 7, 2007
103
on the left s
ale and the h2 possibly heavy
oins on the right s
ale are in fa
t genuine
(sin
e possibly heavy and possibly light equals genuine); this leaves h1+l2
oins to be
investigated further. Conversely, if the s
ale tips to the right, the h1 possibly heavy
oins on the left s
ale and the l2 possibly heavy
oins on the right s
ale are genuine,
leaving l1+h2
oins to be investigated further.
Again, in order to apply the indu
tion hypothesis, we require that the number of
oins not eliminated be equal to 3n , whatever the out
ome of the
omparison. This
imposes the requirement that h1+l2 = l1+h2 = 3n . Together with l1+h1 = l2+h2 , we
infer that l1 = l2 and h1 = h2 . We must arrange the
oins so that ea
h s
ale
ontains
equal numbers of
oins of the same kind.
This requirement
an be met. Simply pla
e the
oins on the s
ales two at a time,
one on the left and one on the right, until ea
h s
ale has its full
omplement of 3n
oins, always
hoosing two
oins with the same marking. The
hoi
e
an always be made
be
ause there are always at least three
oins from whi
h to
hoose; by
hoosing any three
oins, at least two of them will have the same marking.
The Complete Solution This
ompletes the solution to the marked-
oin problem,
and thus to the unmarked-
oin problem. The fake
oin is identied from a
olle
tion
of 3n+1 marked
oins by pla
ing 3n
oins on ea
h s
ale, in su
h a way that there is an
equal number of possibly light
oins on ea
h of the s
ale. A
ording to the out
ome of
the
omparison, one of the following is exe
uted.
If the s
ales balan
e, all the
oins on the s
ales are genuine. Pro
eed with the
oins
left on the table.
If the s ales tip to the left, the oins on the table are genuine.
If the s ales tip to the right, the oins on the table are genuine.
The solution to the unmarked-
oin problem when the number of
oins is (3n+11)/2
is as follows.
Divide the
oins into three groups of sizes (3n1)/2 , (3n1)/2 + 1 and (3n1)/2 .
Pla
e the rst group on the left s
ale together with the supplied genuine
oin. Pla
e the
se
ond group on the right s
ale, and leave the third group on the table. Determine the
out
ome of the
omparison, and pro
eed as follows:
January 7, 2007
104
6. Indu tion
If the s
ales balan
e, all the
oins on the balan
e are genuine. Apply the solution
to the unmarked-
oin problem (indu
tively) to the
oins on the table.
If the s
ales tip to the left, the
oins on the table are genuine. Mark all the
oins on
the left s
ale, with the ex
eption of the supplied genuine
oin as \possibly heavy".
Mark the
oins on the right s
ale as \possibly light". Apply the solution to the
marked-
oin problem to the 3n marked
oins.
If the s ales tip to the right, the oins on the table are genuine. Mark all the oins
on the left s
ale, with the ex
eption of the supplied genuine
oin as \possibly light".
Mark the
oins on the right s
ale as \possibly heavy". Apply the solution to the
marked-
oin problem to the 3n marked
oins.
We ask the reader to review the development of this algorithm. Note that at no stage
is a guess made at an indu
tive hypothesis, even though the development ne
essitates
several su
h hypotheses. Quite the opposite: ea
h hypothesis is systemati
ally
al
ulated
from the available information. This is the epitome of the art of ee
tive reasoning.
Suppose you are given a number of obje
ts. All the obje
ts have the
same weight, with the ex
eption of one,
alled the unique obje
t, whi
h has a dierent
weight. In all other respe
ts, the obje
ts are identi
al. You are required to determine
whi
h is the unique obje
t. For this purpose, you are provided with a pair of s
ales.
Show, by indu
tion on m , that at most 2m
omparisons are needed to identify
the unique obje
t when the total number of obje
ts is 3m . (Hint: for the indu
tion step,
3m+1 obje
ts
an be split into 3 groups of 3m obje
ts.)
Can you identify whether the unique obje
t is lighter or heavier than all the other
obje
ts?
Exercise 6.5
6.8
Summary
Indu
tion is one of the most important problem-solving prin
iples. The prin
iple of
mathemati
al indu
tion is that instan
es of a problem of arbitrary \size"
an be solved
for all \sizes" if
(a) instan
es of \size" 0
an be solved,
(b) given a method of solving instan
es of \size" n , for arbitrary n , it is possible to
adapt the method to solve instan
es of \size" n+1 .
January 7, 2007
105
Using indu
tion means looking for patterns. The pro
ess may involve some
reative
guesswork, whi
h is then subje
ted to the rigours of mathemati
al dedu
tion. The key
to su
ess is to redu
e the guesswork to a minimum, by striving for simpli
ity, and using
mathemati
al
al
ulation to ll in
ompli
ated details.
6.9
Bibliographic Notes
The solution to the fake-
oin problem is a
ombination of two papers by Edsger W.
Dijkstra [Dij90, Dij97.
January 7, 2007
106
6. Indu tion
January 7, 2007
Chapter 7
The Towers of Hanoi
This
hapter is about the Towers of Hanoi problem. The problem is dis
ussed in many
mathemati
al texts, and is often used in
omputing s
ien
e and arti
ial intelligen
e as
an illustration of \re
ursion" as a problem-solving strategy.
The Towers of Hanoi problem is a puzzle that is quite di
ult to solve without a
systemati
problem-solving strategy. Indu
tion gives a systemati
way of
onstru
ting
a rst solution. However, this solution is undesirable. A better solution is obtained by
observing an invariant of the indu
tive solution. In this way, this
hapter brings together
a number of the te
hniques dis
ussed earlier: prin
ipally indu
tion and invariants, but
also the properties of logi
al equivalen
e.
For this problem, we begin with the solution of the problem. One reason for doing
so is to make
lear where we are headed; the Towers of Hanoi problem is one that is not
solved in one go; several steps are needed before a satisfa
tory solution is found. Another
reason is to illustrate how di
ult it
an be to understand why a
orre
t solution has
been found, if no information about the solution method is provided.
7.1
7.1.1
The Towers of Hanoi problem
omes from a puzzle marketed in 1883 by the Fren
h
douard Lu
as, under the pseudonym M. Claus.
mathemati
ian E
The puzzle is based on a legend a
ording to whi
h there is a temple in Bramah
where there are three giant poles xed in the ground. On the rst of these poles, at the
time of the world's
reation, God pla
ed sixty-four golden disks, ea
h of dierent size,
in de
reasing order of size. (See g. 7.1.) The Brahmin monks were given the task of
moving the disks, one per day, from one pole to another a
ording to the rule that no
disk may ever be above a smaller disk. The monks' task will be
omplete when they
107
January 7, 2007
108
have su
eeded in moving all the disks from the rst of the poles to the se
ond and, on
the day that they
omplete their task, the world will
ome to an end!
7.1.2
Iterative Solution
There is a very easy solution to the Towers of Hanoi problem that is easy to remember
and easy to exe
ute. To formulate the solution, we assume that the poles are arranged at
the three
orners of a triangle. Movements of the disks
an then be su
in
tly des
ribed
as either
lo
kwise or anti
lo
kwise movements. We assume that the problem is to move
all the disks from one pole to the next in a
lo
kwise dire
tion. We also assume that days
are numbered from 0 onwards. On day 0 , the disks are pla
ed in their initial position
and the monks begin moving the disks on day 1 . With these assumptions, the solution
is the following.
On every alternate day, beginning on the rst day, the smallest disk is moved. The
rule for moving the smallest disk is that it should
y
le around the poles. The
dire
tion of rotation depends on the total number of disks. If the total number of
disks is odd, the smallest disk should
y
le in a
lo
kwise dire
tion. Otherwise, it
should
y
le in an anti
lo
kwise dire
tion.
January 7, 2007
109
On every other day, a disk other than the smallest disk is moved | subje
t to the
rule that no disk may ever be above a smaller disk. It is easy to see that be
ause
of this rule there is exa
tly one move possible so long as not all the disks are on
one pole.
The algorithm terminates when no further moves are possible, that is, on an evennumbered day when all the disks are on one-and-the-same pole.
Try exe
uting this algorithm yourself on, say, a 4 -disk puzzle. Take
are to
y
le the
smallest disk on the odd-numbered moves and to obey the rule not to pla
e a disk on
top of a disk smaller than itself on the even-numbered moves. If you do, you will nd
that the algorithm works. Depending on how mu
h patien
e you have, you
an exe
ute
the algorithm on larger and larger problems | 5 -disk, 6 -disk, and so on.
7.1.3
WHY?
Presenting the problem and its solution, like this, provides no help whatsoever in understanding how the solution is
onstru
ted. If anything, it only serves to impress |look
at how
lever I am!| but in a reprehensible way. Matters would be made even worse
if we now pro
eeded to give a formal mathemati
al veri
ation of the
orre
tness of the
algorithm. This is not how we intend to pro
eed! Instead, we rst present an indu
tive
solution of the problem. Then, by observing a number of invariants, we show how to
derive the algorithm above from the indu
tive solution.
7.2
Inductive Solution
Constru
ting a solution by indu
tion on the number of disks is an obvious strategy.
Let us begin with an attempt at a simple-minded indu
tive solution. Suppose that
the task is to move M disks from one spe
i
pole to another spe
i
pole. Let us
all these poles A and B, and the third pole C. (Later, we see that naming the poles is
inadvisable.)
As often happens, the basis is easy. When the number of disks is 0 no steps are
needed to
omplete the task. For the indu
tive step, we assume that we
an move n
disks from A to B, and the problem is to show how to move n+1 disks from A to B.
Here, we soon get stu
k! There is only a
ouple of ways that the indu
tion hypothesis
an be used, but these lead nowhere:
1. Move the top n disks from A to B. After doing this, we have exhausted all possibilities of using the indu
tion hypothesis be
ause n disks are now on pole B, and
we have no hypothesis about moving disks from this pole.
January 7, 2007
110
2. Move the smallest disk from A to C. Then move the remaining n disks from A to
B. On
e again, we have exhausted all possibilities of using the indu
tion hypothesis,
be
ause n disks are now on pole B, and we have no hypothesis about moving disks
from this pole.
The mistake we have made is to be too spe
i
about the indu
tion hypothesis. The
way out is to generalise by introdu
ing one or more parameters to model the start and
nish positions of the disks.
At this point, we make a
ru
ial de
ision. Rather than name the poles (A, B and
C, say), we observe that the problem exhibits a rotational symmetry. The rotational
symmetry is obvious when the poles are pla
ed at the
orners of an equilateral triangle,
as we did in g. 7.1. (This rotational symmetry is obs
ured by pla
ing the poles in a
line, as is often done.) The problem does not
hange when we rotate the poles and disks
about the
entre of the triangle.
The importan
e of this observation is that only one additional parameter needs to
be introdu
ed, namely, the dire
tion of movement. That is, in order to spe
ify how a
parti
ular disk is to be moved, we need only say whether it is to be moved
lo
kwise or
anti
lo
kwise from its
urrent position. Also, the generalisation of the Towers of Hanoi
problem be
omes how to move n disks from one pole to the next in the dire
tion d ,
where d is either
lo
kwise or anti
lo
kwise. The alternative of naming the poles leads
to the introdu
tion of two additional parameters, the start and nish positions of the
disks. This is mu
h more
ompli
ated sin
e it involves unne
essary additional detail.
Now, we
an return to the indu
tive solution again. We need to take
are in formulating the indu
tion hypothesis. It is not su
ient to simply take the problem spe
i
ation
as indu
tion hypothesis. This is be
ause the problem spe
i
ation assumes that there
are exa
tly M disks that are to be moved. When using indu
tion, it is ne
essary to move
n disks in the presen
e of Mn other disks. If some of these Mn disks are smaller
than the n disks being moved, the requirement that a larger disk may not be pla
ed on
top of a smaller disk may be violated. We need a stronger indu
tion hypothesis.
The indu
tion hypothesis we use is that it is possible to move the n smallest disks,
from one pole to its neighbour in the dire
tion d , beginning from any valid starting
position (that is, a starting position in whi
h the disks are distributed arbitrarily over
the poles, but no disk is on top of a disk smaller than itself).
In the
ase that n is 0 , the sequen
e of moves is the empty sequen
e. In the
ase of
n+1 disks we assume that we have a method of moving the n smallest disks from one
pole to either of its two neighbours. We must show how to move n+1 disks from one
pole to its neighbour in dire
tion d , where d is either
lo
kwise or anti
lo
kwise. For
onvenien
e, we assume that the disks are numbered from 1 upwards, with the smallest
disk being given number 1 .
January 7, 2007
111
Given the goal of exploiting the indu
tion hypothesis, there is little
hoi
e of what to
do. We
an begin by moving the n smallest disks in the dire
tion d , or in the dire
tion
d . Any other initial
hoi
e of move would pre
lude the use of the indu
tion hypothesis.
Some further thought (preferably assisted by a physi
al model of the problem) reveals
that the solution is to move the n smallest disks in the dire
tion d . Then disk n+1
an be moved in the dire
tion d. (This a
tion may pla
e disk n+1 on top of another
disk. However, the move is valid be
ause the n disks smaller than disk n+1 are not on
the pole to whi
h disk n+1 is moved.) Finally, we use the indu
tion hypothesis again
to move the n smallest disks in the dire
tion d . This pla
es them above disk n+1 ,
and all n+1 smallest disks have now been moved from their original position to the
neighbouring pole in dire
tion d .
The following
ode summarises this indu
tive solution to the problem. The
ode
denes Hn.d to be a sequen
e of pairs hk , d i where n is the number of disks, k is
a disk number and d and d are dire
tions. Disks are numbered from 1 onwards,
disk 1 being the smallest. Dire
tions are boolean values, true representing a
lo
kwise
movement and false an anti-
lo
kwise movement. The pair hk , d i means move the
disk numbered k from its
urrent position in the dire
tion d . The semi
olon operator
on
atenates sequen
es together, [ ] denotes an empty sequen
e and [x] is a sequen
e
with exa
tly one element x . Taking the pairs in order from left to right, the
omplete
sequen
e Hn.d pres
ribes how to move the n smallest disks one-by-one from one pole
to its neighbour in the dire
tion d , following the rule of never pla
ing a larger disk on
top of a smaller disk.
H0.d = [ ]
Hn+1.d = Hn . d ; [hn+1 , di] ; Hn . d
Note that the pro
edure name H re
urs on the right side of the equation for Hn+1.d .
Be
ause of this we have what is
alled a re
ursive solution to the problem. Re
ursion
is a very powerful problem-solving te
hnique, but unrestri
ted use of re
ursion
an be
unreliable. The form of re
ursion used here is limited; des
ribing the solution as an
\indu
tive" solution makes
lear the limitation on the use of re
ursion.
This indu
tive pro
edure gives us a way to generate the solution to the Towers of
Hanoi problem for any given value of n | we simply use the rules as left-to-right
rewrite rules until all o
urren
es of H have been eliminated. For example, here is how
we determine H2.cw . (We use cw and aw , meaning
lo
kwise and anti
lo
kwise, rather
than true and false in order to improve readability.)
H2.cw
=
January 7, 2007
112
1st equation }
on atenation of sequen es
As an exer
ise you should determine H3.aw in the same way. If you do, you will
qui
kly dis
over that this indu
tive solution to the problem takes a lot of eort to put
into pra
ti
e. The
omplete expansion of the equations in the
ase of n = 3 takes 16
steps, in the
ase of n = 4 takes 32 steps, and so on. This is not the easy solution that the
Bramin monks are using! The solution given in se
tion 7.1.1 is an iterative solution to
the problem. That is, it is a solution that involves iteratively (i.e. repeatedly) exe
uting
a simple pro
edure dependent only on the
urrent state. The implementation of the
indu
tive solution, on the other hand, involves maintaining a sta
k of the sequen
e of
moves yet to be exe
uted. The memory of Bramin monks is unlikely to be large enough
to do that!
The number of days the monks need to
omplete their task is the length
of the sequen
e H64.cw . Let Tn.d denote the length of the sequen
e Hn.d . Derive
an indu
tive denition of T from the indu
tive denition of H . (You should nd that
Tn.d is independent of d .) Use this denition to evaluate To , T1 and T2 . Hen
e, or
otherwise, formulate a
onje
ture expressing Tn as an arithmeti
fun
tion of n . Prove
your
onje
ture by indu
tion on n .
Exercise 7.1
Use indu
tion to derive a formula for the number of dierent states in
the Towers of Hanoi problem.
Use indu
tion to show how to
onstru
t a state-transition diagram that shows all
possible states of n disks on the poles, and the allowed moves between states.
Use the
onstru
tion to show that the above solution optimises the number of times
that disks are moved.
Exercise 7.2
January 7, 2007
113
7.3
Re
all the iterative solution to the problem, presented in se
tion 7.1.2. It has two main
elements: the rst is that the smallest disk
y
les around the poles (that is, its dire
tion
of movement is invariantly
lo
kwise or invariantly anti
lo
kwise), the se
ond is that
the disk to be moved alternates between the smallest disk and some other disk. In this
se
tion, we show how these properties are derived from the indu
tive solution.
Cyclic Movement of the Disks
In this se
tion, we show that the smallest disk always
y
les around the poles. In fa
t, we
do more than this. We show that all the disks
y
le around the poles, and we
al
ulate
the dire
tion of movement of ea
h.
The key is that, for all pairs hk , d i in the sequen
e Hn+1.d the boolean value
even.k d is invariant (that is always true or always false). This is a simple
onsequen
e of the rule of
ontraposition dis
ussed in se
tion 5.5. When the formula for
Hn+1.d is applied, the parameter \ n+1 " is repla
ed by \ n " and \ d " is repla
ed by
\ d ". Sin
e even.(n+1) (even.n) , the value of even.(n+1) d remains
onstant
under this assignment.
Whether even.k d is true or false (for all pairs hk , d i in the sequen
e Hn+1.d )
will depend on the initial values of n and d . Let us suppose these are N and D . Then,
for all moves hk , di , we have
even.k d even.N D .
This formula allows us to determine the dire
tion of movement d of disk k . Spe
i
ally,
if it is required to move an even number of disks in a
lo
kwise dire
tion, all evennumbered disks should
y
le in a
lo
kwise dire
tion, and all odd-numbered disks should
y
le in an anti
lo
kwise dire
tion. Vi
e-versa, if it is required to move an odd number of
disks in a
lo
kwise dire
tion, all even-numbered disks should
y
le in an anti
lo
kwise
dire
tion, and all odd-numbered disks should
y
le in a
lo
kwise dire
tion. In parti
ular,
the smallest disk (whi
h is odd-numbered) should
y
le in a dire
tion opposite to D if
N is even, and the same dire
tion as D if N is odd.
An explorer on
e dis
overed the Bramin temple and was able to se
retly
observe the monks performing their task. At the time of his dis
overy, the monks had
got some way to
ompleting their task, so that the disks were arranged on all three poles.
The poles were arranged in a line and not at the
orners of the triangle so he wasn't
sure whi
h dire
tion was
lo
kwise and whi
h anti
lo
kwise. However, on the day of his
arrival he was able to observe the monks move the smallest disk from the middle pole
Exercise 7.3
January 7, 2007
114
to the rightmost pole. On the next day, he saw the monks move a disk from the middle
pole to the leftmost pole. Did the disk being moved have an even number or an odd
number?
Alternate Disks
We now turn to the se
ond major element of the solution, namely that the disk that is
moved alternates between the smallest disk and some other disk.
By examining the puzzle itself, it is not di
ult to see that this must be the
ase.
After all, two
onse
utive moves of the smallest disk are wasteful as they
an always be
ombined into one. And, two
onse
utive moves of a disk other than the smallest have no
ee
t on the state of the puzzle. We now want to give a formal proof that the sequen
e
Hn.d satises this property.
Let us
all a sequen
e of numbers alternating if it has two properties. The rst
property is that
onse
utive elements alternate between one and a value greater than
one; the se
ond property is that if the sequen
e is non-empty then it begins and ends
with the value one. We write alt.ks if the sequen
e ks has these two properties.
The sequen
e of disks moved on su
essive days, whi
h we denote by diskn.d , is
obtained by taking the rst
omponent of ea
h of the pairs in Hn.d and ignoring the
se
ond. Let the sequen
e that is obtained in this way be denoted by diskn.d . Then,
from the denition of H we get:
disk0.d = [ ]
diskn+1.d = diskn . d ; [n+1] ; diskn . d .
Our goal is to prove alt.(diskn.d) . The proof is by indu
tion on n . The base
ase,
n = 0 , is
learly true be
ause an empty sequen
e has no
onse
utive elements. For the
indu
tion step, the property of alternating sequen
es on whi
h the proof depends is that,
for a sequen
e ks and number k ,
alt.(ks ; [k] ; ks)
denition }
January 7, 2007
115
7.4. Summary
alt.(diskn . d) ((diskn . d = [ ]) (n+1 = 1))
=
true .
The explorer left the area and did not return until several years later.
On his return, he dis
overed the monks in a state of great despair. It transpired that
one of the monks had made a mistake shortly after the explorer's rst visit but it had
taken the intervening time before they had dis
overed the mistake. The state of the disks
was still valid but the monks had dis
overed that they were no longer making progress
towards their goal; they had got into a never-ending loop!
Fortunately, the explorer was able to tell the monks how to pro
eed in order to return
all the disks to one-and-the-same pole whilst still obeying the rules laid down to them
on the day of the world's
reation. They would then be able to begin their task afresh.
What was the algorithm the explorer gave to the monks? Say why the algorithm
is
orre
t. (Hint: The disk being moved will still alternate between the smallest and
some other disk. You only have to de
ide in whi
h dire
tion the smallest disk should
be moved. Be
ause of the monks' mistake this will not be
onstant. Make use of the
fa
t that, beginning in a state in whi
h n disks are all on the same pole, maintaining
invariant the relationship
Exercise 7.4
even.n d even.k d
for the dire tion d moved by disk k will move n disks in the dire tion d .)
7.4
Summary
In this
hapter we have seen how to use indu
tion to
onstru
t a solution to the Towers of
Hanoi problem. Several indu
tive
onstru
tions have been dis
ussed. The
hapter began
January 7, 2007
116
with an indu
tive
onstru
tion of a graph representing all possible moves of the disks
in the general n -disk problem. The graph was used to justify an indu
tive solution to
the problem itself. This solution was then tranformed to an iterative solution, indu
tive
proofs of properties of the sequen
e of movements of the disks being used to establish
the
orre
tness of the iterative solutions.
The
hapter has also illustrated two important design
onsiderations: the in
lusion
of the 0 -disk problem as the basis for the
onstru
tion (rather than the 1 -disk problem)
and the avoidan
e of unne
essary detail by not naming the poles and referring to the
dire
tion of movement of the disks (
lo
kwise or anti
lo
kwise) instead.
7.5
Bibliographic Remarks
Information on the history of the Towers of Hanoi problem is taken from [Ste97. A proof
of the
orre
tness of the iterative solution was published in [BL80. The formulation and
proof presented here is based on [BF01.
January 7, 2007
Chapter 8
Knights Circuit
The problem ta
kled in this
hapter is a parti
ularly hard one. Yet, by suitably de
omposing the problem,
ombined with ee
tive reasoning skills, the problem be
omes
solvable.
The problem is to nd a Knight's
ir
uit of a
hessboard. That is, nd a sequen
e of
moves that will take a Knight in a
ir
uit around all the squares of a
hessboard, visiting
ea
h square exa
tly on
e, and ending at the square at whi
h the
ir
uit began.
The problem is an instan
e of a sear
h problem; in prin
iple, it
an be solved by a
systemati
, exhaustive examination of all the paths a Knight
an follow around a
hessboard | a so-
alled brute-for
e sear
h. However, there are 64 squares on a
hessboard;
that means 64 moves have to be
hosen, one for ea
h square. From ea
h of the
orner
squares, there is a
hoi
e of just 2 moves, but from ea
h of the 16
entral squares, there
is a
hoi
e of 8 moves (see g. 8.1); from the remaining squares either 4 or 6 moves are
possible. This gives a massive amount of
hoi
e in the paths that
an be followed. Lots
of
hoi
e is usually not a problem but, when
ombined with the very restri
tive requirements that the path forms a
ir
uit that visits every square exa
tly on
e, it does be
ome
a problem. The Knight's
ir
uit problem is hard be
ause of this
riti
al
ombination of
an explosion with an implosion of
hoi
e.
But, all is not lost. The squares on a
hessboard are arranged in a very simple pattern,
and the Knight's moves, although many, are spe
ied by one simple rule (two squares
horizontally or verti
ally, and one square in the opposite dire
tion). There is a great
deal of stru
ture, whi
h we must endeavour to exploit.
8.1
Straight-Move Circuits
Finding a Knight's
ir
uit is too di
ult to ta
kle head on. Some experien
e of ta
kling
simpler
ir
uit problems is demanded.
117
January 7, 2007
118
the board and the number of squares? Use your answer to show that it is impossible
to
omplete a
ir
uit of the board if both sides have odd length. (Hint:
ru
ial is
January 7, 2007
119
that ea
h move is from one square to a dierent
oloured square. Otherwise, the
answer does not depend on the sort of moves that are allowed.)
(b) For what values of m is it possible to
omplete a straight-move
ir
uit of a board
of size 2m1 ? (A 2m1 board has one
olumn of squares; the number of squares
is 2m .)
(c) Show that it is possible to
omplete a straight-move
ir
uit of a 2n board for all
(positive) values of n . (A 2n board has two rows, ea
h row having n squares.)
The
on
lusion of exer
ise 8.1 is that a straight-move
ir
uit is only possible if the
board has size 2mn , for positive numbers m and n . That is, one side has length 2m
and the other has length n . (Both m and n must be non-zero be
ause the problem
assumes the existen
e of at least one starting square.) Also, a straight-move
ir
uit
an
always be
ompleted when the board has size 2n , for positive n . This suggests that
we now try to
onstru
t a straight-move
ir
uit of a 2mn board, for m at least one
and n greater than one, by indu
tion on m , the 2n
ase providing the basis for the
indu
tive
onstru
tion.
To
omplete the indu
tive
onstru
tion, we need to
onsider a board of size 2m n ,
where m is greater than 1 . Su
h a
onstru
tion is hopeful be
ause, when m is greater
than 1 , a 2m n board
an be split into two boards of sizes 2p n and 2q n , say,
where both p and q are smaller than m and p+q equals m . We may take as the
indu
tive hypothesis that a straight-move
ir
uit of both boards
an be
onstru
ted. We
just need to
ombine the two
onstru
tions.
The key to the
ombination is the
orner squares. There are two straight moves from
ea
h of the
orner squares, and any straight-move
ir
uit must use both. In parti
ular,
it must use the horizontal moves. Now, imagine that a 2m n board is divided into a
2p n board and a 2q n board, with the former above the latter. (The
onvention
we use is that the rst number gives the number of rows and the se
ond the nunber
of
olumns of the board.) The bottom-left
orner of the 2p n board is immediately
above the top-left
orner of the 2q n board. Constru
t straight-move
ir
uits of these
two boards. Figure 8.2 shows the result diagrammati
ally. The two horizontal, red
lines at the middle-left of the diagram depi
t the horizontal moves that we know must
form part of the two
ir
uits. The blue dotted lines depi
t the rest of the
ir
uits. (Of
ourse, the shape of the dotted lines gives no indi
ation of the shape of the
ir
uit that
is
onstru
ted.)
Now, to
ombine the
ir
uits to form one
ir
uit of the entire board, repla
e the
horizontal moves from the bottom-left and top-left
orners by verti
al moves, as shown
by the verti
al green lines in g. 8.3. This
ompletes the
onstru
tion.
January 7, 2007
120
2p
2m
2q
Figure 8.2: Combining straight-move
ir
uits. First, split the board into two smaller
boards and
onstru
t straight-move
ir
uits of ea
h.
n
2p
2m
2q
Exercise 8.2
January 7, 2007
121
8.2. Supersquares
Figure 8.5: Straight-move
ir
uits (shown in red) of a 3 3 board, omitting one of the
squares.
8.2
Supersquares
Let us now return to the Knight's-
ir
uit problem. The key to a solution is to exploit
what we know about straight moves. The way this is done is to imagine that the 8 8
hessboard is divided into a 4 4 board by grouping together 2 2 squares into \supersquares", as shown in g. 8.6.
If this is done, the Knight's moves
an be
lassied into two types: Straight moves
are moves that are \straight" with respe
t to the supersquares; that is, a Knight's move
is straight if it takes it from one supersquare to another supersquare either verti
ally
above or below, or horizontally to the left or to the right. Diagonal moves are moves
that are not straight with respe
t to the supersquares; a move is diagonal if it takes the
January 7, 2007
122
1
0
0
1
0
1
0
1
0
1
0
1
0
1
0
1
11111111111111
00000000000000
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
11111111111111
00000000000000
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
00
11
0
1
0
1
0
1
0
1
00
11
0
1
0
1
0
1
0
1
11111111111111
00000000000000
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
11111111111111
00000000000000
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
0 1
1
0 1
01
0
1
Figure 8.7: Straight (Blue) and Diagonal (Red) Knight's Moves From Some Starting
Position (Bla
k). Boundaries of the supersquares are indi
ated by thi
kened lines.
Fo
using on the straight moves, we now make a
ru
ial observation. Figure 8.8 shows
the straight moves from one supersquare |the bottom-left supersquare| verti
ally upwards and horizontally rightwards. The
olours indi
ate the moves that
an be made.
For example, from the bottom-left red square a straight move
an be made to the top-left
red square or to the bottom-right red square.
Observe the pattern of the
olours. Verti
al moves
ip the
olours around a verti
al
axis, whilst horizontal moves
ip them around a horizontal axis. (The verti
al moves
inter
hange the red-above-yellow and blue-above-green
olumns; the horizontal moves
inter
hange the red-next-to-blue row with the yellow-next-to-green row.)
January 7, 2007
8.2. Supersquares
111
000
00
11
00011
111
00
000
111
00
11
000
111
00
11
00
11
00
11
00011
111
0011
0011
00
00011
111
0011
0011
00
00011
111
0011
0011
00
123
Both are equivalent to rotating the 2 2 square through 1800 about its
entre. So, let
us use c (short for \
entre") as its name. That is, by denition of c ,
(8.3)
v;h = c = h;v .
Also, doing nothing before or after any operation is the same as doing the operation.
(8.5)
n;x = x;n = x .
The three equations (8.3), (8.4) and (8.5), together with the fa
t that doing one operation
after another is asso
iative (that is, doing one operation x followed by two operations
y and z in turn is the same as doing rst x followed by y and then
on
luding with
z | in symbols, x ; (y ; z) = (x ; y) ; z ) allow the simpli
ation of any sequen
e of the
operations to one operation. For example,
January 7, 2007
124
v;h = c
v;v;h;h
=
v;v = h;h = n
n ; x = x , with x := n
n;n
=
n .
In words,
ipping verti
ally, then rotating through 1800 about the
entre, and then
ipping horizontally is the same as doing nothing. (Note how asso
iativity is used impli
itly between the rst and se
ond steps. The use of an inx operator for \followed
by" fa
ilitates this all-important
al
ulational te
hnique.)
Constru
t a two-dimensional table that shows the ee
t of exe
uting
two operations x and y in turn. The table should have four rows and four
olumns,
ea
h labelled by one of n , v , h and c . (Use the physi
al pro
ess of
ipping squares to
onstru
t the entries.)
Use the table to verify that, for x and y in the set {n,v,h,c} ,
Exercise 8.6
(8.7)
x;y = y;x .
x ; (y ; z) = (x ; y) ; z .
(In prin
iple, you need to
onsider 43 , i.e. 64 , dierent
ombinations. Think of ways to
redu
e the amount of work.)
January 7, 2007
125
8.3
The identi
ation of the four operations on supersquares is a signi
ant step towards
solving the Knight's-
ir
uit problem. Suppose one of the supersquares is labelled \ n ".
Then the remaining fteen supersquares
an be uniquely labelled as n , v , h or c
squares, depending on their position relative to the starting square. Figure 8.9 shows
how this is done. Suppose we agree that the bottom-left square is an \ n " square. Then
immediately above it is a \ v " square, to the right of it is an \ h " square, and diagonally
adja
ent to it is a \ c " square. Supersquares further away are labelled using the rules
for
omposing the operations.
As a
onsequen
e, all 64 squares of the
hessboard are split into four disjoint sets. In
g. 8.9, the dierent sets are easily identied by the
olour of the square. Two squares
have the same
olour equivales they
an be rea
hed from ea
h other by straight Knight's
moves. (That is, two squares of the same
olour
an be rea
hed from ea
h other by
straight Knight's moves, and two squares of dierent
olour
annot be rea
hed from
ea
h other by straight Knight's moves.)
111
000
000
111
00
11
00
11
000
111
000
111
00
11
00
11
v 11
c11
v 11
c11
000
111
000
111
00
00
000
111
000
111
00
00
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
n
h
n
h
000
111
00011
111
0011
00
000
111
00011
111
0011
00
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
v 11
c11
v 11
c11
000
111
000
111
00
00
000
111
000
111
00
00
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
n
h
n
h
000
111
00011
111
0011
00
000
111
00011
111
0011
00
000
111
00011
111
0011
00
000
111
00011
111
0011
00
Figure 8.9: Labelling Supersquares
Re all the dis ussion of straight-move ir uits in se tion 8.1. There we established
January 7, 2007
126
Figure 8.10: S
hema for forming \red-blue" and \green-yellow"
ir
uits. The straightmove
ir
uits are depi
ted as
oloured
ir
les, a single move in ea
h being depi
ted by
a dotted line. These straight moves are repla
ed by the diagonal moves, shown as solid
lines.
Figure 8.10 shows s
hemati
ally how red-blue and green-yellow
ir
uits are formed;
in ea
h
ase, two straight moves (depi
ted by dotted lines) in the respe
tive
ir
uits are
repla
ed by diagonal moves (depi
ted by straight lines). Figure 8.11 shows one way of
hoosing the straight and diagonal moves in order to
ombine red and blue
ir
uits, and
green and yellow
ir
uits | in ea
h
ase, two \parallel" straight moves are repla
ed by
two \parallel" diagonal moves. Exploiting symmetry, it is easy to nd similar \parallel
January 7, 2007
127
moves" with whi
h to
ombine red and yellow
ir
uits, or green and blue
ir
uits. On the
other hand, there are no diagonal moves from red to green, or from yellow to blue squares;
onsequently, it is impossible to
onstru
t a \red-green" or a \yellow-blue"
ir
uit.
11
00
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
00011
111
0011
00
000
111
00011
111
0011
00
00
11
00011
111
0011
00
000
111
00011
111
0011
00
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
00011
111
0011
00
000
111
00011
111
0011
00
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
00011
111
0011
00
000
111
00011
111
0011
00
00
11
00011
111
0011
00
000
111
00011
111
0011
00
Figure 8.11: Forming \red-blue" and \green-yellow"
ir
uits. The straight moves shown
as dotted lines are repla
ed by the diagonal moves shown as solid lines.
Red and blue straight-move
ir
uits have been
ombined in g. 8.12 to form a \redblue"
ir
uit. The method of
ombination is indi
ated by the dotted and solid lines: the
straight moves (dotted lines) are repla
ed by diagonal moves (solid lines). To
omplete a
ir
uit of the whole board, with this red-blue
ir
uit as basis, a green-yellow
ir
uit has
to be
onstru
ted, and this
ir
uit
ombined with the red-blue
ir
uit. This is left as an
exer
ise.
A slight di
ulty of this method is that it
onstrains the straight-move
ir
uits that
an be made. For example,
onsidering the suggested method for
ombining red and blue
ir
uits in g. 8.11, no
onstraint is pla
ed on the blue
ir
uit (be
ause there is only
one way a straight-move
ir
uit of the blue squares
an enter and leave the bottom-left
orner of the board). However, the straight-move
ir
uit of the red squares is
onstrained
by the requirement that it make use of the move shown as a dotted line. The di
ulty is
resolved by rst
hoosing the
ombining moves and then
onstru
ting the straight-move
January 7, 2007
128
Figure 8.12: A \red-blue"
ir
uit. \Parallel" red and blue moves, shown as dotted lines,
are repla
ed by diagonal moves, shown as thi
ker solid lines, thus
ombining the two
ir
uits.
ir
uits appropriately.
In order to
onstru
t a Knight's
ir
uit of smaller size boards, the dierent pairs of
ombining moves need to be positioned as
lose as possible together. This is possible in
the
ase of an 8 6 board, but not for smaller boards.
Constru
t a Knight's
ir
uit of an 8 8 board using the s
heme dis
ussed above. Do the same for an 8 6 board. Indi
ate
learly how the individual
ir
uits have been
ombined to form the entire
ir
uit.
Exercise 8.10
January 7, 2007
129
Figure 8.13: S
hema for Combining Straight-Move Cir
uits. Four straight moves (indi
ated by dotted lines) are repla
ed by four diagonal moves (indi
ated by solid bla
k
lines).
pair of green moves in the bottom-left
orner, severly restri
ts the
hoi
e of diagonal
moves; in
ombination with symmetry, this makes the appropriate moves easy to nd.
(The
onstru
tion of the
ir
uit is easier for an 8 8 board than for a 6 8 board
be
ause, in the latter
ase, more
are has to be taken in the
onstru
tion of the straightmove
ir
uits. If you en
ounter di
ulties, try turning the board through 90 whilst
maintaining the orientation of the
ombining moves.)
Exercise 8.12 Division of a board of size (4m + 2) (4n + 2) into supersquares yields
a (2m + 1) (2n + 1) \super" board. Be
ause this superboard has an odd number of
(super) squares, no straight-move
ir
uit is possible, and the strategy used in exer
ise
8.10 is not appli
able. However, it is possible to
onstru
t Knight's
ir
uits for boards of
size (4m + 2) (4n + 2) , whenever, both m and n are at least 1 , by exploiting exer
ise
8.2.
The strategy is to
onstru
t four straight-move
ir
uits of the board omitting one of
the supersquares. (Re
all exer
ise 8.2 for how this is done.) Then, for ea
h
ir
uit, one
January 7, 2007
130
move is repla
ed by two moves |a straight move and a diagonal move| both with end
points in the omitted supersquare. This s
heme is illustrated in g. 8.14.
11
00
00
11
000
111
000
111
Figure 8.14: Strategy for Constru
ting a Knight's Cir
uit of (4m + 2) (4n + 2) boards.
One supersquare is
hosen, and four straight-move
ir
uits are
onstru
ted around the
remaining squares. These are then
onne
ted as shown.
Complete the details of this strategy for a 6 6 board. Make full advantage of the
symmetry of a 6 6 board. In order to
onstru
t the twelve
ombining moves depi
ted
in g. 8.14 , it su
es to
onstru
t just three; the remaining nine
an be found by a
rotating the moves through a right angle.
Explain how to use your solution for the 6 6 board to
onstru
t Knight's
ir
uits of any board of size (4m + 2) (4n + 2) , whenever, both m and n are at least
1.
8.4
Discussion
In the absen
e of a systemati
strategy, the Knight's
ir
uit problem is a truly di
ult
problem to solve, whi
h means it is a very good example of dis
iplined problem-solving
January 7, 2007
131
skills. The method we have used to solve the problem is essentially problem de
omposition | redu
ing the Knight's
ir
uit problem to
onstru
ting straight-move
ir
uits,
and
ombining these together.
The key
riterion for a good method is whether or not it
an be extended to other
related problems. This is indeed the
ase for the method we have used to solve the
Knight's
ir
uit problem. The method has been applied to
onstru
t a
ir
uit of an
8 8
hessboard, but the method
an
learly be applied to mu
h larger
hessboards.
The key ingredients are
January 7, 2007
132
are known at this time for
onstru
ting solutions. \Complexity theory" is the name given
to the area of
omputing s
ien
e devoted to trying to quantify how di
ult algorithmi
problems really are.
8.5
To be written.
8.6
Bibliographic Remarks
Solutions and histori
al information on the Knight's
ir
uit problem
an easily be found
on the internet. A
ording to one of these [Ma
Quarrie, St. Andrews Univ. the rst
Knight's tour is believed to have been found by al-Adli ar-Rumni in the ninth
entury,
long before
hess was invented.
The use of supersquares in solvingthe Knight's
ir
uit problem is due to Edsger W.
Dijkstra [Dij92. His solution is for a standard-sized
hessboard and uses the method of
ombining straight-move
ir
uits des
ribed in exer
ise 8.11. The pairwise
ombination
of straight-move
ir
uits is due to Diethard Mi
haelis [private
ommuni
ation. Both
solutions involve sear
hing for \parallel moves". Mi
haelis's solution is slightly preferable
be
ause just two pairs of \parallel moves" have to be found at ea
h stage. Dijkstra's
solution involves sear
hing for four pairs (twi
e as many) at the same time, making it a
little bit harder to do. The solution to exer
ise 8.12 was
onstru
ted by the author, with
useful feedba
k from Mi
haelis.
January 7, 2007
Solutions to Exercises
2.1 1233 games must be played. Let k be the number of players that have been kno
ked
out, and let g be the number of games that have been played. Initially, k and g are
both equal to 0 . Every time a game is played, one more player is kno
ked out. So, k
and g are always equal. To de
ide the tournament, 12341 players must be kno
ked
od
133
January 7, 2007
134
3.1
{
5C ||
5H || 5W
5H |2W| 3W ; 2C |3H| 3W
;
2C || 3C }
2C |1C| 2C
;
3C || 2C }
3W |3H| 2C ; 3W |2W| 5H
;
5W || 5H
|| 5C
3.2 We modify the solution to the ve- ouple problem, ee tively by leaving one ouple
4C ||
4H || 4W
4H |2W| 2W ; 2C |2H| 2W
;
2C || 2C }
2C |1C| 1C
;
3C || 1C }
3W |3H| 1C ; 3W |1W| 4H
;
4W || 4H
|| 4C
4C ||
January 7, 2007
135
4H || 4W
4H |1W| 3W ; 1C |3H| 3W
;
1C || 3C }
1C |1C| 2C
;
2C || 2C }
2W |2H| 2C ; 2W |2W| 4H
;
4W || 4H
|| 4C
3.3 Let M denote the
apa
ity of the boat, and let N denote the number of
ouples.
We assume that N is at least 2 and M is at most the minimum of 3 and N/2 . (These
properties are
ommon to the
ases of a boat of
apa
ity 2 and 4
ouples, and a boat
of
apa
ity 3 and 6
ouples.)
Let lH denote the number of husbands on the left bank. The number of husbands
on the right bank, denoted rH , is then NlH . Similarly, let lW denote the number
of wives on the left bank. The number of wives on the right bank, denoted rW , is then
NlW .
(13)
That is, either there are no single individuals on either bank, or all husbands are on one
of the two banks. It is a requirement of any solution that this property is an invariant. (If
not, either 0 < lH < lW or 0 < rH < rW . In words, the wives outnumber the husbands
on the left bank or on the right bank. In both
ases, the solution is invalid.)
Now, we
laim that, under the given assumptions on M and N , either
(a) The boat is on the left bank and
(14)
M < lH ,
or
(b) the boat is on the right bank and
(15)
M lH .
January 7, 2007
136
Property (a) is
learly true initially. (Re
all the assumptions about M and N .)
Now, suppose (a) is true and, then, a
rossing is made from left to right. Note that
lH 6= 0 both before and after the
rossing. (Before the
rossing, lH = 0 is ex
luded by the
assumption that M < lH . After the
rossing, lH = 0 is impossible be
ause at most M
husbands
an
ross together.) So, the invariant (13)
an be strengthened: the
rossing
is made starting in a state in whi
h
(16)
and must result in a state satisfying this property. Otherwise, the
rossing is invalid.
Note also that a left-to-right
rossing
auses lH and/or lW to de
rease.
We
onsider two
ases before the
rossing: lH = N , and (lH = lW) (lH 6= N) .
Sin
e at most N/2 husbands
an
ross together, if lH = N before the
rossing, at
least N/2 are left at the left bank. That is, (b) is true after the
rossing.
If (lH = lW) (lH 6= N) before the
rossing, the property lH = lW must be maintained by the
rossing. (This is be
ause (16) must be maintained and the
rossing
annot
in
rease lH .) That is, an equal number of wives and husbands must make the
rossing.
Sin
e only one
ouple
an
ross at one time, the value of lH is de
reased by at most 1 .
But (14) is assumed to be true before the
rossing;
onsequently, (15) is true after the
rossing, and the boat is at the right bank. Thus, (b) is true after the
rossing.
In both
ases, we have established that, after the
rossing, (b) is true.
Now suppose (b) is true and a
rossing is made from right to left. A right-to-left
rossing
auses lH and/or lW to in
rease. Sin
e lH 6= 0 before the
rossing, and lH
does not de
rease, lH 6= 0 after the
rossing. (Stri
tly, we need to assume that M is
non-zero in order to assert that lH 6= 0 . Obviously, the
ase that the boat has
apa
ity
0
an be ex
luded from
onsideration!)
Again, we
onsider two
ases: this time, no husbands
ross, and some husbands
ross.
If husbands
ross, the a
t of
rossing in
reases lH ; so, after the
rossing M < lH and
(of
ourse) the boat is at the left bank. Thus, (a) is true after the
rossing.
If only wives
ross, it must be the
ase that lH = N (be
ause (16) must be maintained
and, if only wives
ross, it is impossible to maintain that lH = lW ). That is, lH is
un
hanged, and M < lH both before and after the
rossing. After the
rossing, the boat
is, of
ourse, on the left side of the bank. Thus, (a) is true after the
rossing.
In both
ases, we have established that (a) is true after the
rossing.
In summary, property (a) is true initially. Also, if (a) is true and a left-to-right
rossing is made, (b) be
omes true; vi
e-versa, if (b) is true and a right-to-left
rossing
is made, (a) be
omes true. So, at all times, either (a) or (b) is true. That is, it
an never
be the
ase that all husbands are at the right bank.
January 7, 2007
137
5
rossings are needed, of whi
h 2 are return trips. So, 2 people never make
a return trip. Clearly, an optimal solution is found when persons 3 and 4 |the two
3.4
slowest| do not return. (Consider any sequen
e of
rossings that gets all four a
ross
the bridge. If one or both of the two slowest makes a return journey, then one or both of
the two fastest does not make a return journey. By inter
hanging a slower person with
a faster person in the sequen
e, a new sequen
e is found whi
h is at least as fast as the
original sequen
e | sin
e the time for at least one return trip is redu
ed and the time
for at most one forward trip is in
reased by the same amount.)
There are two strategies for getting the two slowest a
ross: let them
ross together
or let them
ross seperately. The strategy \let the two slowest
ross together" is implemented by letting persons 1 and 2 (the two fastest)
ross, with person 1 returning.
Then persons 3 and 4
ross, and person 2 returns. Finally, persons 1 and 2
ross
again. The total time taken using this strategy is
t2 + t1 + t4 + t2 + t2 .
If the two slowest
ross seperately, then the best strategy is to let person 1
ross with
them and then return for the other. Implementing this strategy takes a total time of
t2 + t1 + t4 + t1 + t3 .
(This
orresponds to person 2 and person 1
rossing, then person 1 returning, then
person 4 and person 1
rossing, then person 1 returning, and nally persons 1 and 3
rossing. The order in whi
h persons 2 , 3 and 4
ross is, of
ourse, immaterial. The
order
hosen here fa
ilitates the
omparison of the times.)
Comparing the total times, the rst strategy should be used when t2+t2 t1+t3 and
the se
ond strategy when t1+t3 t2+t2 . (There is a small element of nondeterminism
in this solution: when t2 + t2 = t1 + t3 an arbitrary
hoi
e may be made between the two
strategies.)
Applying this solution to the two spe
i
ases, we get:
(a) The times taken are 1 minute, 1 minute, 3 minutes and 3 minutes: Sin
e 1+1 1+3 ,
the two slowest should
ross together. The shortest time is 1+1+3+1+1 , i.e. 7
minutes.
(b) The times taken are 1 minute, 4 minutes, 4 minutes and 5 minutes. Sin
e
1+5 4+4 , the two slowest should
ross seperately. The shortest time is 4+1+5+1+4 ,
i.e. 15 minutes. (The shortest time if the two slowest
ross together is 4+1+5+4+4 ,
i.e. 18 minutes
January 7, 2007
138
4.1 a) Naming any day in De ember, other than 31st De ember results in losing. This
is for
ed by the opponent naming 30th November (that is, the last day of November).
Similarly, naming any day other than 30th November results in losing, be
ause the
opponent
an then name 30th November. This is for
ed by the opponent naming 31st
O
tober. In general, the winning strategy is to name the last day of the month. The
opponent is then for
ed to name the 1st of the next month. Whether the year is a leap
year or not makes no dieren
e.
b) In De
ember, the losing positions are the odd-numbered days and the winning positions are the even-numbered days. (Take
are: The \losing positions" are the days that
the winning player names. This is in line with the terminology of losing and winning
positions.) That is, if the last-named day is an odd-numbered day, the player whose turn
it is must name an even-numbered day and, so, will eventually lose.
In parti
ular, the player who names 1st De
ember wins. Any day in November is
thus a winning position. In O
tober, like De
ember, the odd-numbered days are losing
positions, and any day in September is a winning position. Similarly, in August, the
odd-numbered days are losing positions, and any day in July is a winning position.
The pattern
hanges in June, whi
h has an even number of days. The player who
names 1st July loses;
onsequently, any even-numbered day in June is a losing day.
This means that every even-numbered day in May is a winning day; ; also, every evennumbered day in April is a winning day. This means that 31st Mar
h is a losing day.
Sin
e Mar
h has an odd number of days, the pattern we saw for De
ember and November
re
urs. Every odd-numbered day in Mar
h is a losing day, and every day in February is
a winning day. Finally, the odd-numbered days in January are losing days.
We
on
lude that the se
ond player is guaranteed to win. The strategy is to name
the 1st day of the following month when the last-named day is in November, September,
July or February. Otherwise, the strategy is to name the next day of the year.
Again, it does not matter if it is a leap-year.
4.2
10
Table 1: Winning (W) and Losing (L) Positions for subtra tion set {2 , 5 , 6}
January 7, 2007
139
The squares that are not positions are the ones at the foot of a ladder or at the
head of a snake. Positions that
annot be identied as winning or losing positions are
ones in a
y
le of positions. Labelling of winning and losing positions assumes that every
game is guaranteed to terminate no matter how either player moves. If
y
les o
ur this
assumption is not valid.
***Remainder to be written.***
4.3
4.4
To be written.
5.5
Let col be the olour of the square, and n be the number of moves. A move is
then
An odd number of moves ( 63 ) is needed, but the
olour of the square doesn't
hange.
So, in order to move the knight as required, a
hange has to be made to col even.n ,
whi
h is impossible.
5.6 Suppose the number of
ouples is n . There are 2n people who ea
h shake hands
with between 0 and 2n 1 people. If the 2n 1 people all shake hands with a dierent
number of people, there must be someone who shakes hands with k people for ea
h k
between 0 and 2n 1 .
If n is 1 , the only person other than the host is the host's partner. Sin
e
ouples
do not shake hands, both shake hands 0 times.
Now suppose that n is greater than 1. In this
ase, there are at least two people
other than the host and the host's partner. Consider the two people who shake hands
January 7, 2007
140
0 and 2n 1 times. The person who shakes hands 2n 1 times does so with everyone
ex
ept their partner. By the symmetry of the shake-hands relation, it is thus the
ase
that everyone ex
ept that person's partner shakes hands with at least one person. It
follows that the two people who shake hands 0 and 2n 1 times are husband and wife.
Be
ause neither is the host, it also follows that neither is the host's partner
Now suppose we dis
ount this
ouple. That is, we
onsider the party
onsisting of
the other n1
ouples. The number of times ea
h person shakes hands is then redu
ed
by one. So, again, all but the host have shaken hands a distin
t number of times.
Repeating this pro
ess, we eliminate all the
ouples one by one until the party has
been redu
ed to just the host and the host's partner. Ea
h time, the number of times
the host and the host's partner shake hands is redu
ed by one. The host and the host's
partner must therefore have both shaken hands n1 times.
5.8
(a) false
(b) false
(c) false
(d) p
(e) false
(f) q 6 r
(g) p
(h) true
5.9
true
=
true false
=
false .
January 7, 2007
141
p false
=
p .
6 is asso iative
(a 6 a) 6 b
=
( a 6 a false )
false 6 b
=
denition of 6
false b
=
b .
whether B is a knave.
6.1 It is required that any two lines interse t in a single point. If the lines are not
straight and they interse
t in a segment of a line, inverting the
olours of one of the two
regions does not guarantee that the
olouring of adja
ent regions at the boundary of the
left and right regions is satisfa
tory. This is be
ause, along the line segment, the
olours
of the adja
ent regions are not the same before the inversion takes pla
e
ontrary to the
assertion made above.
The solution remains valid provided every line
uts the surfa
e in two. A line on a
ball does this, whereas a line on a doughnut need not.
January 7, 2007
142
The number of
olourings is always two no matter how many lines there are. This is
learly the
ase when there are no lines. When there are n+1 lines,
hoose any one of
the lines. Cut the paper along the
hosen line. Assume indu
tively that, for ea
h half,
there are exa
tly two
olourings. Combining these gives four dierent ways of
olouring
the entire sheet of paper. However, two of these are unsatisfa
tory be
ause the
olours
of regions adja
ent at the
hosen line must be dierent. This leaves exa
tly two ways of
olouring the paper with n+1 lines.
6.5 When m is 0 , there is just one obje
t. This is the unique obje
t and 0 (whi
h
equals 20 )
omparisons are needed to dis
over that fa
t.
Suppose now that m is greater than 0 . Split the 3m obje
ts into 3 groups ea
h of
3m1 obje
ts. One of these 3 groups will have a dierent weight to the other two, whi
h
will be of equal weight. At most 2
omparisons are needed to determine whi
h of the
groups it is. Then, by indu
tion, at most a further 2(m1)
omparisons are required
to nd the unique obje
t in that group. This gives a total of 2(m1) + 2 , i.e. 2m ,
denition of T
length(H0.d)
=
denition of H0.d
length.[ ]
=
denition of length
0 ,
and
Tn+1.d
=
denition of T
length(Hn+1.d)
=
denition of Hn+1.d
January 7, 2007
143
denition of length
Tn.d + 1 + Tn.d .
That is,
To.d = 0
Tn+1.d = 2 Tn.d + 1 .
If we expand these equations for n = 0 , 1 , 2 , . . . , just as we did for the equations for
H , we dis
over that To.d is 0 , T1.d is 1 and T2.d is 3 (in ea
h
ase for all d ). This
and the form of the equation for Tn+1.d (in parti
ular the repeated multipli
ation by 2 )
suggest that Tn.d is 2n1 . The simple indu
tive proof is omitted.
7.2 We begin by onsidering the permissible states that the puzzle may be in. In any
state, the disks on any one pole are in order of de
reasing size. So, if we want to spe
ify
the state of the puzzle we only need to spe
ify whi
h pole ea
h disk is on. For example,
suppose there are ve disks and suppose we spe
ify that disk 1 is on pole A , disk 2 is
on pole B , disks 3 and 4 are on pole A and disk 5 is on pole B . Then disk 4 must
be on the bottom of pole A , disk 3 must be on top of it, and disk 1 must be on top of
disk 3 . Also, disk 5 must be on the bottom of pole B and disk 2 must be on top of it.
No other arrangement of the disks satises the rule that no disk is above a disk smaller
than itself.
The state of an n -disk puzzle
an thus be spe
ied by a sequen
e of n pole names.
The rst name in the sequen
e is the lo
ation of disk 1 , the se
ond is the lo
ation of
disk 2 , and so on. That is, the k th name in the sequen
e is the lo
ation (pole name)
of disk k . Sin
e ea
h disk may be on one of three poles we
on
lude that there are 3n
dierent states in the n -disk problem.
Now we
onsider the transitions between states. We
onsider rst the problem where
there are no disks, then the 1 -disk problem, then the 2 -disk problem, and then we
onsider the general n -disk problem.
When there are no disks there is exa
tly one state: the state when there are no disks
on any of the poles. This is shown in g. 15. (You may have di
ulty seeing the gure.
It
onsists of a single dot!)
We now explain how to
onstru
t the state-transition diagram for the ( n+1 )-disk
problem, for an arbitrary n , given that we have
onstru
ted the diagram for the n -disk
January 7, 2007
144
January 7, 2007
145
AnA
BnA
CnC
CnA
CnB
BnC
AnC
AnB
BnB
Figure 16: Constru tion of the state-transition diagram for the (n + 1) -disk problem
January 7, 2007
146
7.3 Even, be ause the dire tion of movement is opposite to that of the smallest disk
7.4 The algorithm is to repeatedly exe
ute the following pro
edure until it
an no longer
be exe
uted (i.e. when it is no longer possible to determine k in step 1).
1. Suppose it is possible to move disk k in the dire
tion d , where k > 1 . (Re
all
that disk 1 is the smallest disk.) Set d to odd.k d .
2. Move disk k (in the dire
tion d , of
ourse).
3. Move the smallest disk in the dire
tion d .
The
orre
tness is justied as follows. When step 1 is exe
uted, we know that the
rst k1 disks are all on the pole in dire
tion d from disk k . Progress is made if
these k smallest disks
an be transferred to the same pole. To do this, it is ne
essary
to move the k1 smallest disks in the dire
tion d . The dire
tion that disk 1 has to
be moved is thus d where
even.(k1) d even.1 d .
Simplifying, we get that d = (odd.k d ) . (In words, the dire
tion that the smallest
disk is moved should be the same as the dire
tion that disk k is moved, if k is also
odd; otherwise the smallest disk is moved in the opposite dire
tion to disk k .) The
orre
tness of the Towers of Hanoi program then guarantees that this a
tion will initiate
a sequen
e of moves after whi
h all k1 disks will have been moved onto disk k . During
this sequen
e of moves the smallest disk will
ontinue to move in the same dire
tion. On
ompletion, however, the dire
tion of the smallest disk may or may not be reversed.
The only time that step 1
annot be exe
uted is when all the disks are on the same
pole, as required.
The solution is to pla
e the disks in order, starting with the largest and ending
with the smallest. Let k denote the number of the disks still to be repla
ed; so, initially
k is N and we are done when k is 0 . Ea
h time the value of k is reassigned, we ensure
that the k smallest disks are on the same pole.
If the k th disk is on the right pole, de
rease k by 1 . Otherwise, suppose it needs
to be moved in dire
tion d from its
urrent position. Move the smallest k1 disks in
the dire
tion d , then move disk k to its rightful position. Finally, de
rease k by 1 .
Continue this pro
ess until k is 0.
7.5
January 7, 2007
147
(a) The number of moves that have to be made equals the number of squares. After an
odd number of moves, the
olour of the
urrent square is dierent from the
olour
of the starting square. So, after an odd number of moves, it is impossible to return
to the starting square.
(b) It's easy to see that a straight-move ir uit of a 21 board is possible |starting
at one of the squares, move to the other square and then move ba
k again| , but,
otherwise, no straight-move
ir
uit is possible. If m is greater than 1 , at least one
square is two moves from the starting square; it is impossible to visit su
h a square
and return to the starting square without visiting the in-between square more than
on
e.
(c) See (b) for a
ir
uit of a 21 board. For n greater than 1 , a straight-move
ir
uit
of a 2n board is
ompleted by starting at a
orner, moving one-by-one to all the
squares in the same row, then returning via the se ond row.
8.2 For the 3 3 board, a ir uit an be onstru ted exa tly when the omitted square is
not adja
ent to a
orner square. For larger boards, the same
ondition applies. Suppose
the
oordinates of the omitted square are (m, n) . (It doesn't matter whether numbering
starts at zero or one.) Then a
ir
uit
an be
onstru
ted of the remaining squares exa
tly
when even.m = even.n . The
onstru
tion is to split the board into four re
tangular
boards in su
h a way that the to-be-omitted square is at a
orner of a board with an odd
number of squares. The other three boards ea
h have an even number of squares, and at
least one of them has at least one square. Constru
t
ir
uits of these three boards, and
|indu
tively, with the 3 3 board as the base
ase| a
ir
uit of the board with the
omitted square. Then,
onne
t the
ir
uits together as shown in g. 17 .
8.6
n
v
h
c
n v
v n
h c
c h
h
c
n
v
c
h
v
n
January 7, 2007
148
even
odd
even
even
Figure 17: Straight-move
ir
uits (shown in red) of a 3 3 board, omitting one of the
squares.
Property (8.7) is veried by observing that the table is symmetri
about the top-left
to bottom-right diagonal. Veri
ation of the asso
iativity property is mu
h more tedious.
The
ase that x , y or z is n
an be dealt with simply. This leaves 27 other
ases to
onsider. This is an example of a \tedious, but straightforward" proof!
8.9 To be added.
See g. 18.
The parallel moves used to
onne
t
ir
uits of dierent
olours are indi
ated by dotted
lines depi
ting the straight moves, and solid bla
k lines, depi
ting the diagonal moves.
Note how the
hoi
e of parallel moves
onstrains the
hoi
e of red
ir
uit. In fa
t, the
red moves are entirely di
tated by this
hoi
e. In
ontrast, there is
omplete freedom
in
hoosing a blue or yellow
ir
uit. There is some freedom in
hoosing a green
ir
uit,
but not
omplete freedom. In this way, a substantial number of
ir
uits
an be found all
based on the same set of
ombining parallel moves. In the
ir
uit shown, the green, blue
and yellow
ir
uits were
onstru
ted by \
opying" the red
ir
uit.
The same set of
ombining parallel moves
an be used to
onstru
t a
ir
uit of an
8 6 board; all that is required is to \shorten" the straight-move
ir
uits in order to
a
ommodate the smaller board. (But note that they
annot be used to
onstru
t a
ir
uit of a 6 8 board.)
8.10
8.11 Figure 19 shows details of how the straight-move ir uits are ombined. Moves
indi
ated by dotted lines are repla
ed by the diagonal moves indi
ated by solid bla
k
lines.
January 7, 2007
149
Figure 18: A Knight's Cir
uit. Solid lines indi
ate the
ir
uit. The dotted lines depi
t
straight moves that are repla
ed. The diagonal moves that repla
e them are depi
ted by
solid bla
k lines.
Figure 20 shows the
ir
uits obtained in this way. The dotted lines are not part of
the
ir
uit; these are the moves that are repla
ed.
In order to
onstru
t a
ir
uit for any board of size 4m 2n , where m is at least
2 and n is at least 3 , it su
es to use the te
hnique detailed in gs. 8.2 and 8.3 for
extending straight-move
ir
uits to boards of arbitrary size. This
onstru
tion has to
be applied four times, on
e for ea
h of the straight-move
ir
uits in the solution to the
8 6 -board problem.
8.12 We begin by identifying the moves shown in g. 8.14. See g. 21. (Note the
symmetry.)
Now it is easy to ll in the straight-move
ir
uits around the remaining squares. See
g. 22.
For the general problem, it is easy to extend the straight-move
ir
uits.
January 7, 2007
150
Figure 19: Details of how the four straight-move
ir
uits are
ombined; the straight
moves indi
ated by dotted lines are repla
ed by diagonal moves indi
ated by solid bla
k
lines.)
January 7, 2007
151
Figure 20: Knight's Cir
uit of an 8 6 and an 8 8 board. (Dotted lines are not part
of the
ir
uit; these are the moves that are repla
ed by diagonal moves, as detailed in
g. 19.)
January 7, 2007
152
Figure 21: Details of Combining Cir
uits. Diagonal moves are shown in bla
k. Straight
moves are
oloured. The dotted lines represent the moves that are repla
ed. Solid lines
represent the moves that repla
e them.
Figure 22: Knight's Cir
uit of a 6 6 board. The dotted lines do not form part of the
ir
uit.
January 7, 2007
Bibliography
[Ba
03 Roland Ba
khouse. Program Constru
tion. Cal
ulating Implementations
From Spe
i
ations. John Wiley & Sons, Ltd., 2003.
[BCG82 Elwyn R. Berlekamp, John H. Conway, and Ri
hard K. Guy. Winning Ways,
volume I and II. A
ademi
Press, 1982.
[BF01
[BL80
P. Buneman and L. Levy. The Towers of Hanoi Problem. Information Pro essing Letters, 10:243{244, 1980.
[Dij90
Edsger W. Dijkstra. EWD1083: The balan
e and the
oins.
ir
ulated privately,
September 1990.
[Dij92
[Dij97
Edsger W. Dijkstra. EWD1260: The marked
oins and the s
ale.
ir
ulated
privately, Mar
h 1997.
[DS90
Edsger W. Dijkstra and Carel S. S
holten. Predi
ate Cal
ulus and Program
Semanti
s. Texts and monographs in Computer S
ien
e. Springer-Verlag,
1990.
[DW00 John P. D'Angelo and Douglas B. West. Mathemati
al Thinking. ProblemSolving and Proofs. Prenti
e Hall, 2000.
[Gri81
[GS93
David Gries and Fred B. S
hneider. A Logi
al Approa
h to Dis
rete Math.
Springer-Verlag, 1993.
153
January 7, 2007
154
[Lev03 Anany Levitin. Introdu
tion to The Design And Analysis of Algorithms.
Addison Wesley, 2003.
[Smu78 Raymond Smullyan. What Is The Name Of This Book?
Englewood Clis, N.J., 1978.
[Ste97
Prenti e-Hall,
Ian Stewart. The Magi al Maze. Weideneld and Ni olson, London, 1997.
January 7, 2007