Problem Solving
Roland Ba
khouse
1 Introduction 1
1.1 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 1
2 Invariants 5
2.1 Cho
olate Bars . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
2.4 Tetrominoes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
3 Crossing a River 21
3.1 Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
3.3.5 A Review . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 34
3.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 39
4 Games 41
4.1 Mat
hsti
k Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 41
4.2.1 Assumptions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 43
4.4.1 Symmetry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54
4.5 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 64
5.2.1 Propositions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 68
5.4 Negation . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 78
5.5 Contraposition . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 80
5.7 Inequivalen e . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 84
5.8 Summary . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 87
6 Induction 89
6.1 Example Problems . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 89
6.3 Triominoes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 96
Bibliography 177
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
iv
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 problemsolving 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 problemsolving 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 problemsolving 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 al
gorithm is a welldened 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
Algorithmi
Problem Solving
Roland Ba
khouse. 1 May 28, 2008
2 1. Introdu
tion
The solution to this problem whi
h we won't dis
lose just yet! is
learly a se
quen
e of instru
tions about how to get all four people a
ross the bridge. A typi
al in
stru
tion will be: \persons x and y
ross the bridge" or \person z
rosses the bridge".
The sequen
e of instru
tions solves the problem if the total time taken to exe
ute the
instru
tions is (no more than) 17 minutes.
An algorithm is typi
ally more general than this. Normally, an algorithm will have
ertain inputs ; for ea
h input, the algorithm should
ompute an output whi
h is related
to the input by a
ertain so
alled inputoutput relation. In the
ase of the bridge
rossing problem, an algorithm might input four numbers, the
rossing time for ea
h
person, and output the total time needed to get all four a
ross the bridge. For example,
if the input is the numbers 1 , 3 , 19 , 20 , the output should be 30 and if the input
is the numbers 1 , 4 , 5 , 6 the output should be 17 . The input values are
alled the
parameters of the algorithm.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
1.2. Bibliographi
Remarks 3
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4 1. Introdu
tion
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 problemsolving 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.)
Algorithmi
Problem Solving
Roland Ba
khouse. 5 May 28, 2008
6 2. Invariants
Figure 2.1 shows a 4×3
ho
olate bar that has been
ut into ve pie
es. The
uts
are indi
ated by solid lines.
How many
uts in total are needed to
ompletely
ut the
ho
olate into all its
pie
es?
2. Empty Boxes.
Eleven large empty boxes are pla
ed on a table. An unknown number of the boxes
is sele
ted and, into ea
h, eight medium boxes are pla
ed. An 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?
3. Tumblers.
Several tumblers are pla
ed in a line on a table. Some tumblers are upside down,
some are the right way up. (See g. 2.2.) 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?
4. Black and White Balls
Consider an urn lled with a number of balls ea
h of whi
h is either bla
k or white.
There are also enough balls outside the urn to play the following game. We want
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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 topright and bottomleft squares removed so that there
are 62 squares remaining. (See g. 2.3.) An unlimited supply of dominoes has
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
8 2. Invariants
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
2.1. Cho
olate Bars 9
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.
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 \realworld" problem. Realworld 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 algo
rithm for doing this! . The best problemsolvers are probably the ones most skilled in
abstra
tion.
(Texts on problemsolving 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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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.)
Assignments 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
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
ommaseparated 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
ommaseparated 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, p−c 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, p−c 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
1Aword 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
simultaneous 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Æ
ulttond 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
2.1. Cho
olate Bars 11
holds whatever the values of p and c . This
he
ks that p−c is an invariant of the
assignment p,c := p+1 , c+1 . The left side of this equality is the expression E
and the right side is the expression E after repla
ing all variables as pres
ribed by the
assignment.
As another example, suppose we have two variables m and n, and we onsider the
assignment
m , n := m+3 , n−1
Simple algebra shows that this holds. So, in reasing m by 3, simulaneously de reasing
E[ls := rs]
is used to denote the expression obtained by repla ing all o urren es of the variables in
some examples:
E[ls := rs] = E .
Induction The nal step in the solution of the
ho
olate problem is to exploit the
invarian
e of p−c .
Initially, p = 1 and c = 0 . So, initially, p−c = 1 . But, p−c is invariant. So, p−c = 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
s−c = 1 . That is, c = s−1 . 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
olatebar 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).
Summary This
ompletes our dis
ussion of the
ho
olatebar problem. A number of
important problemsolving 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.
Exercise 2.1 A kno
kout tournament is a series of games. Two players
ompete in
ea
h game; the loser is kno
ked out (i.e. doesn't play anymore), the winner
arries on.
The winner of the tournament is the player that is left after all other players have been
kno
ked out.
Suppose there are 1234 players in a tournament. How many games are played be
fore the tournament winner is de
ided? (Hint:
hoose suitable variables, and seek an
invariant.)
2
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
2.3. The Tumbler Problem 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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 := u−2 .
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 := u−2 . 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 .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
2.4. Tetrominoes 15
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 Ttetrominoes. 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 .
\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 intro
du
e the style we repeat the argument just given. Here it is in the new style:
an m×n board is
overed with tetrominoes
⇒ { invariant: c is a multiple of 4 ,
c = m×n }
m×n is a multiple of 4
⇒ { property of multiples }
m is a multiple of 2 ∨ n is a multiple of 2 .
This is a twostep
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 m×n board is
overed with tetrominoes, m×n is a multiple of 4 .
(Alternatively, \an m×n board is
overed with tetrominoes implies m×n is a multiple
of 4 " or \an m×n board is
overed with tetrominoes only if m×n is a multiple of
4 .")
The text between
urly bra
kets, following the \ ⇒ " symbol is a hint why the state
ment is true. Here the hint is the
ombination of the fa
t, proved earlier, that the number
of
overed squares is always a multiple of 4 (whatever the shape of the area
overed)
together with the fa
t that, if an m×n board has been
overed, the number of
overed
squares is m×n .
The se
ond step is read as:
If m×n is a multiple of 4 , m is a multiple of 2 or n is a multiple of 2 .
Again, the \ ⇒ " symbol signies an impli
ation. The symbol \ ∨ " means \or". Note
that by \or" we mean so
alled \in
lusive or"  the possibility that both m and n are
multiples of 2 is in
luded. A so
alled \ex
lusive or" would mean that m is a multiple
of 2 or n is a multiple of 2 , but not both, i.e. it would ex
lude this possibility.
The hint, in this
ase, is less spe
i
. The property that is being alluded to has to do
with expressing numbers as multiples of prime numbers. You may or may not be famil
iar with the general theorem, but you should have suÆ
ient knowledge of multiplying
numbers by 4 to a
ept that the step is valid.
The
on
lusion of the
al
ulation is also an \if" statement. It is:
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
2.4. Tetrominoes 17
We introdu
e four variables to des
ribe the state of the board. The variable b
ounts
the number of
overed bla
k squares, whilst w
ounts the number of
overed white
squares. In addition, d
ounts the number of dark Ttetrominoes that have been used,
and l
ounts the number of light tetrominoes.
Pla
ing a dark tetromino on the board is modelled by the assignment
d , b , w := d+1 , b+3 , w+1 .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
18 2. Invariants
b − 3×d − l ,
sin
e
(b − 3×d − l)[d , b , w := d+1 , b+3 , w+1]
= { denition of substitution }
(b+3) − 3×(d+1) − l
= { arithmeti
}
b − 3×d − l
and
(b − 3×d − l)[l , b , w := l+1 , b+1 , w+3]
= { denition of substitution }
(b+1) − 3×d − (l+1)
= { arithmeti
}
b − 3×d − l .
Now, the initial value of b − 3×d − l is zero. So, it will always be zero, no matter how
many Ttetrominoes are pla
ed on the board. Similarly, the value of w − 3×l − d will
always be zero.
We
an now solve the given problem.
a re
tangular board is
overed by Ttetrominoes
⇒ { from problem 6(a) we know that at least one
side of the board has an even number of squares,
whi
h means that the number of bla
k squares
equals the number of white squares }
b=w
⇒ { b − 3×d − l = 0
w − 3×l − d = 0 }
(b = w) ∧ (3×d + l = 3×l + d)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
2.5. Additional Exer
ises 19
⇒ { arithmeti
}
(b = w) ∧ (l = d)
⇒ { b − 3×d − l = 0
w − 3×l − d = 0 }
b = w = 4×d = 4×l
⇒ { arithmeti
}
b+w = 8×d
⇒ { b+w is the number of
overed squares }
the number of
overed squares is a multiple of 8 .
We
on
lude that
If a re
tangular board is
overed by Ttetrominoes, the number of
overed
squares is divisible by 8 .
You
an now ta
kle 6(
). The problem looks very mu
h like 6(b), whi
h suggests that
it
an be solved in a similar way. Indeed, it
an. Look at other ways of
olouring the
squares bla
k and white. Having found a suitable way, you should be able to repeat the
same argument as above. Be
areful to
he
k that all steps remain valid.
(How easily you
an adapt the solution to one problem in order to solve another is a
good measure of the ee
tiveness of your solution method. It shouldn't be too diÆ
ult
to solve 6(
) be
ause the solution to 6(b), above, takes
are to
learly identify those steps
where a property or properties of Ttetrominoes are used. Similarly, the solution also
learly identies where the fa
t that the area
overed is re
tangular is exploited. Badly
presented
al
ulations do not make
lear whi
h properties are being used. As a result,
they are diÆ
ult to adapt to new
ir
umstan
es.)
Problem 6(d) is relatively easy, on
e 6(
) has been solved. Good lu
k!
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
20 2. Invariants
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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 \brutefor
e"
sear
h and problem de
omposition.
Brutefor
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 some
thing 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 problemsolving.
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 brutefor
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
problemsolving 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
brutefor
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
Algorithmi
Problem Solving
Roland Ba
khouse. 21 May 28, 2008
22 3. Crossing a River
problem, making it impossible to solve even with the aid of a very powerful omputer.
3.1 Problems
1. Goat, Cabbage and Wolf.
A farmer wishes to ferry a goat, a
abbage and a wolf 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, the goat should not be left alone with the abbage
(otherwise, the goat would eat the abbage), and the wolf should not be left alone
with the goat (otherwise, the wolf would eat the goat).
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
How an all the adults and all the hildren ross the river? Make lear any assump
4. Overweight
Ann, Bob, Col and Dee are on one side of a river. They have one rowing boat that
an arry at most 100 kilos. Ann is 46 kilos, Bob is 49 kilos, Col is 52 kilos and
main purpose in showing the brutefor e solution is to illustrate the pitfalls of poor
The problem involves four individuals, and ea
h is at one of the two river banks.
This means that we
an represent a state by four variables, ea
h of whi
h has one of
two values. We
all the variables f (for farmer), g (for goat), c (for
abbage) and w
(for wolf), and we
all their possible values L (for left) and R (for right). A value of R
means \at the right bank". A value of L means at the left bank. Note that the boat
is always where the farmer is, so we do not need to introdu
e a variable to represent its
position.
A brutefor
e sear
h involves
onstru
ting a statetransition graph that models all
possible states, and ways of
hanging from one state to another  the state transitions.
In the goat,
abbage, wolfproblem, a state des
ribes on whi
h bank ea
h of the four
individuals
an be found. A state transition is a
hange of state that is allowed by
the problem spe
i
ation. For example, two states between whi
h there is a valid state
transition are:
1. All four are at the left bank.
2. The farmer and goat are at the right bank, whilst the
abbage and wolf are at the
left bank.
For the very simplest problems, a diagram
an be drawn depi
ting a statetransition
graph. The states are drawn as
ir
les, and the state transitions are drawn as lines
onne
ting the
ir
les. The lines have arrows on them if some state transitions are
not reversible; if so, the diagram is
alled a dire
ted graph. If all state transitions are
reversible, the arrows are not ne
essary and the diagram is
alled an undire
ted graph.
We are going to draw a statetransition graph to demonstrate the brutefor
e solution to
this problem.
If four variables
an ea
h have one of two values, there are 24 (i.e. sixteen) dierent
ombinations of values. However, in this problem some of these
ombinations are ex
luded. The requirement that the goat
annot be left alone with the
abbage is expressed
by the system invariant
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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 g c w
L L L L
L L L R
L L R L
L L R R
L R L L
R L R R
R R L L
R R L R
R R R L
R R R 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
3.2. Brute For
e 25
RRRL LLRL
RRLR LLLR
2. The farmer takes the
abbage to the right bank, and returns with the goat. This
is the path from LRLL to LLRL.
3. The farmer takes the wolf to the right bank, and returns alone. This is the path
from LLRL to LLRR.
4. The farmer takes the goat to the right bank. This is the path from LLRR to RRRR.
The alternative solution, given by the lower path, inter
hanges \
abbage" and\wolf"
in the se
ond and third steps.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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 \adultsand
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 brutefor
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 \statespa
e explosion problem" gets in the way. The river
rossing
problems illustrate \statespa
e 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 statetransition 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 problemsolving. 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
The statespa
e explosion is often
aused by a failure to properly analyse a problem; a
parti
ularly frequent
ause is unne
essary or inappropriate naming. The goat
abbage
andwolf problem is a good example.
In the goat
abbageandwolf problem, distin
t names are given to the \farmer", the
\goat", the \
abbage" and the \wolf". But, do we really need to distinguish between all
four? In the dis
ussion of the state spa
e, we remarked on a \similarity" between the
wolf and the
abbage. Spe
i
ally, the goat
annot be left with either the wolf or the
abbage. This \similarity" also emerged in the solution: two solutions were obtained,
symmetri
al in the inter
hange of \wolf" and \
abbage". Why, then, are the \wolf" and
the \
abbage" distinguished by giving them dierent names?
Let us restate the problem, this time with a naming
onvention that omits the un
ne
essary distin
tion between the wolf and the
abbage. In the restated problem, we
all
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
3.3. Jealous Couples 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 statetransition diagram for the
problem.
The problemsolving 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
28 3. Crossing a River
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
3.3. Jealous Couples 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 statetransition diagram for the goat,
abbage, wolfproblem exhibits the left
right symmetry very well. The diagram also illustrates the symmetry between the
ab
bage and wolf. Both symmetries were to be expe
ted from the problem statement; by
ignoring them and using brutefor
e, we lost the opportunity of a redu
tion in eort.)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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 ex
ample, 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 statetransition dia
grams, 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
3.3. Jealous Couples 31
{ 3C  } S0 {  3C } .
{ 3C  } S1 { 3H  3W } ,
{ 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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,
{ 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 } .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
3.3. Jealous Couples 33
Note parti ularly the middle a tion  1C 1C 1C  . This may be a lefttoright
rossing, or a righttoleft rossing; whi h is not immediately lear. The task is now to
1C  2C and results in the state 2C  1C . Vi eversa, 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
whi h an be a hieved.
{ 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  }
1C,2H 2W ; 1C,2H 1W 1W ; 3H 2W 1W
; { 3H  3W }
3H 1W 2W ; 1C 2H 2W
; { 1C  2C }
1C 1C 1C
; { 2C  1C }
2W 2H 1C ; 2W 1W 3H
; { 3W  3H }
1W 2W 3H ; 1W 1W 1C,2H ; 2W 1C,2H
{  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 brutefor
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
omposi
tion 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:
Exercise 3.1 (Fivecouple 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.2 (Fourcouple Problem) Unfortunately, the symmetry between the left
and right banks does not guarantee that every river
rossing problem has a symmetri
solution. The
ase that there are four jealous
ouples, and their boat
an
arry a max
imum of three, has a solution, but it is not symmetri
. Determine a solution to this
problem.
The following hint may be helpful. Four is less than ve, and, by now, you will have
solved the problem of transporting ve
ouples a
ross the river (exer
ise 3.1). So, try to
modify the solution for ve
ouples to obtain a solution for four. You should be able to
nd two solutions in this way, one being obtained from the other by reversing left and
right.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
3.4. Rule of Sequential Composition 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.)
Exercise 3.3 Show that, if the boat
an hold a maximum of two people, it is impossible
to transport four or more
ouples a
ross the river.
Show that, if the boat
an hold a maximum of three people, it is impossible to
transport six or more
ouples a
ross the river.
Hint: Both problems
an be handled together. The
ru
ial properties are:
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 ,
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
36 3. Crossing a River
M = N×d + r ∧ 0 ≤ r < N .
{ N 6= 0 } S { M = N×d + r ∧ 0 ≤ r < N } .
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 frg1
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:
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
3.4. Rule of Sequential Composition 37
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
38 3. Crossing a River
(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. (Note that steps (a)
and (b) were already dis
ussed above.)
(a) How many times must the tor
h 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
trip? Whi
h of the four people would you
hoose not to make a return trip? (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 trip 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 whi
h
strategy to use.
Note that this exer
ise is mu
h harder than the spe
i
ase dis
ussed above. This is not
just be
ause the input times are parameters but primarily be
ause you must establish
without doubt that your solution
annot be bettered. The original problem was
arefully
worded so that this was not required.
Exercise 3.5 Suppose a brutefor
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?
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
3.5. Summary 39
3.5 Summary
In this
hapter, we have
ontrasted brutefor
e sear
h with problem de
omposition.
Brutefor
e sear
h should only be used as a last resort. Modern
omputer te
hnology
means that some problems that are too large for human beings to solve do be
ome solv
able, but the statespa
e explosion makes the method impra
ti
al for realisti
problems,
even with the most powerful
omputers. Complexity theory, whi
h you will study in a
ourse on algorithm design, lends greater for
e to this argument; no matter how mu
h
bigger and faster
omputers be
ome, they
an never
ompete with the in
rease in size of
the state spa
e
aused by modest in
reases in problem size.
Problem de
omposition seeks to exploit the inherent stru
ture of the problem domain.
In all river
rossing, or similar, problems, there is a symmetry between the left and right
banks. This suggests ta
kling the problems by de
omposing them into three
omponents,
the rst and last being symmetri
al in some way. Unfortunately, this strategy has no
guarantee of su
ess, but, if the problems are ta
kled in this way, they be
ome more
manageable, and often have
lear, easily remembered and easily reprodu
ed solutions.
Most importantly, the solution method
an be applied repeatedly, in
ontrast to the
solutions, whi
h are only relevant to one parti
ular problem.
Along the way, the issue of de
iding what to name (and what not to name) has
emerged as an important problemsolving skill that
an have signi
ant impa
t on the
omplexity of the problem. The pro
ess is
alled abstra
tion  from the myriad of
details that surround any realworld des
ription of a problem, we abstra
t the few that
are relevant, introdu
ing appropriate,
learly dened mathemati
al notation to assist in
the problem's solution.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
40 3. Crossing a River
Games
This
hapter is about how to win some simple twoperson games. Games provide very
good examples of algorithmi
problem solving be
ause playing games is all about win
ning. 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 sys
temati
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.
Algorithmi
Problem Solving
Roland Ba
khouse. 41 May 28, 2008
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 that m mod 3 denotes the remainder after dividing m by 3.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.2. Winning Strategies 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.1 Assumptions
We make a number of assumptions about the game, in order that the sear
h will work.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
44 4. Games
0 1 2 3 4 5 6 7 8
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 n−1 and an edge to the node labelled n−2 . 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 (nonexistent) edges is to a winning position.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.2. Winning Strategies 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 forall 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.
0 1 2 3 4 5 6 7 8
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 .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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 := n−1 2 2 ≤ n → n := n−2 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 nonzero.
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 nondeterministi
hoi
e 
indi
ated by the \ 2 " symbol among a number of so
alled guarded
ommands. A
guarded
ommand has the form b → S , where b is a booleanvalued 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. Remov
ing one mat
h is only allowed if 1 ≤ n ; hen
e, the statement n := n−1 is \guarded"
by this
ondition. Similarly, removing two mat
hes modelled by the assignment
n := n−2  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
if
i
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 ifthenelse 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 nondeterministi hoi e, as used here, the hoi e is not ompletely determined.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.2. Winning Strategies 47
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
48 4. Games
We re
ommend that you now try to solve the mat
hsti
kgame 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 .
Exercise 4.1 (31st December Game) 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.
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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.3. Subtra
tionSet Games 49
The games we have just dis
ussed are examples of mat
hsti
k games; if the rule is
that 1 thru M mat
hes 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
om
puted. We exemplify the pro
ess in this se
tion by
al
ulating the winning and losing
positions when the allowed moves are:
remove one mat
h,
remove three mat
hes,
remove four mat
hes.
In other words, the subtra
tion set is {1 , 3 , 4} .
Positions in the game are given by the number of mat
hes in the pile. We refer to
the positions using this number. So, \position 0 " means the position in whi
h there are
no mat
hes remaining in the pile, \position 1 " means the position in whi
h there is just
one mat
h in the pile, and so on.
Beginning with position 0 , and working onebyone through the positions, we identify
whether ea
h position is a winning position using the rules that
a position is a losing position if every move from it is to a winning position, and
a position is a winning position if there is a move from it to a losing position.
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.
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 0 1 2 3 4 5 6
Type L W L W W W W
Move 1 3 4 3 4
Table 4.1: Winning (W) and Losing (L) Positions for subtra
tion set {1 , 3 , 4}
Position 7 8 9 10 11 12 13
Type L W L W W W W
Move 1 3 4 3 4
Table 4.2: Winning (W) and Losing (L) Positions for subtra tion set {1 , 3 , 4}
Exercise 4.2 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} .)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.4. Sums of Games 51
Exercise 4.3 This exer
ise is
hallenging; its solution involves thinking beyond the
material presented in the rest of the
hapter.
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 ; the winner is the rst to rea
h the
nish. 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.
(a) List the positions in this game. (These are not the same as the squares. Think
arefully about squares linked by a snake or a ladder.)
(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.
2
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
52 4. Games
21 22 23 24 25
20 19 18 17 16
11 12 13 14 15
10 9 8 7 6
1 2 3 4 5
Figure 4.3: Snakes and Ladders. Players take it in turn to move the
ounter at most
four spa
es forward.
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 brute
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.4. Sums of Games 53
M N k
J K L h i j
F G H I f g
D E c d e
A B C a b
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.
for
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,
and the right game has 11 ; thus, the sum of the two games has 15×11 dierent positions.
For this game, and for sums of games in general, a brutefor
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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
54 4. Games
4.4.1 Symmetry
A Simple Sum Game
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
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 se
quen
e of assertions and program statements.
{ m = n ∧ (m 6= 0 ∨ n 6= 0) }
if 1≤m → redu
e m
2 1≤n → redu
e n
fi
; { m 6= n }
if m < n → n := n − (n−m)
2 n < m → m := m − (m−n)
fi
{ m=n }
The nondeterministi
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.
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 ≤ n−m ≤ n , so
that n−m mat
hes
an be removed from the pile with n mat
hes. Sin
e, n−(n−m)
simplies to m , it is
lear that, after the assignment n := n−(n−m) , the property
m = n will hold. The
ase n < m is symmetri
.
The following sequen
e of assertions and program statements summarises the argu
ment 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 ex
presses 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 ≤ n−m ≤ n } n := n − (n−m) { m = n }
2 n < m → { 1 ≤ m−n ≤ m } m := m − (m−n) { m = n }
fi
{ m=n }
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).
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
56 4. Games
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
oin already on the table. The winner is the one who puts the last
oin on the table.
Who should win and what is the winning strategy? (Harder ) What, if anything, do you
assume about the
oins in order to justify your answer?
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.4. Sums of Games 57
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 sym
metry? Nevertheless, a form of \symmetry" is a key to the winning strategy: symmetry
is too important to abandon so easily!
We saw, in se
tion 4.2, that the way to win the onepile game, with the restri
tion
that at most M mat
hes
an be removed, is to
ontinually establish the property that
the remainder after dividing the number of mat
hes by M+1 is 0 . Thus, for a pile of
m mat
hes, the number m mod (M+1) determines whether the position is a winning
position or not. This suggests that, in the twopile game, \symmetry" between the piles
is formulated as the property that
( 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.
(Note: we dis
uss later the full details of how to
he
k the assertions made in this
program segment.)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
58 4. Games
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
l := l ′ (for some l ′ ) to the left
omponent or a (guarded) assignment r := r ′ (for some
r ′ ) to the right
omponent.
The idea is to dene two fun
tions L and R , say, on left and right positions, respe

tively, in su
h a way that a position ( l,r ) is a losing position exa
tly when L.l = R.r .
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 po
sition ( l,r ) satisfying L.l 6= R.r  . That is,
{ L.l = R.r ∧ (l is not an end position ∨ r is not an end position ) }
if l is not an end position →
hange l
2 r is not an end position →
hange r
fi
{ L.l 6= R.r } .
Third, applying the winning strategy, from a winning position a position ( l,r ) satisfy
ing 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 }
apply winning strategy
{ L.l = R.r } .
Similarly, for every r ′ su
h that there is a move from r to r ′ in the right game,
R.r 6= R.r ′ .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.4. Sums of Games 59
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,
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 →
hange r
2 R.r < L.l →
hange l
fi
{ L.l = R.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
There is no legal move in the game G from p to a position q satisfying
mexG.q = n .
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 .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
60 4. Games
1 1 3
2 0 0 1 2 0
0 0 0 2 0 0
1 1 1 1 1
0 0 0 0 0
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.
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
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.
0 1 4 5
Figure 4.7: Computing mex numbers. The unlabelled node is given the mex number 2 .
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
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.
Exercise 4.4 a) Consider the subtra
tionset 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.
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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
62 4. Games
\left game") or \R" (for right game), and m is the number of mat
hes to be re
moved.
Exercise 4.5 A re
tangular board is divided into m horizontal rows and n verti
al
olumns, where m and n are both stri
tly positive integers, making a total of m×n
squares. The number of squares is
alled the area of the board.
A game is played on the board as follows. Ea
h of the two players takes it in turn
to
ut the board either horizontally or verti
ally along one of the dividing lines. A
ut
divides the board into two parts; when a
ut has been made a part whose area is at
most the area of the other part is dis
arded. (This means that the part with the smaller
area is dis
arded if the two parts have dierent areas, and one of the two parts is
hosen
arbitrarily if the two areas are equal.) For example, if the board has 4×5 squares, a
single move redu
es it to 2×5 , 3×5 , 4×3 , or 4×4 squares. Also, if the board has 4×4
squares, a single move redu
es it to either 2×4 or 3×4 squares. (Boards with 3×4 and
4×3 squares are ee
tively the same; the orientation of the board is not signi
ant.)
The game ends when the board has been redu
ed to a 1×1 board. At this point, the
player whose turn it is to play loses.
This game is a sum game be
ause, at ea
h move, a
hoi
e is made between
utting
horizontally or verti
ally. The
omponent games are
opies of the same game. This game
is as follows. A position in the game is given by a stri
tly positive integer m . A move
in the game is to repla
e m by a number n su
h that n < m ≤ 2n ; the game ends when
m has been redu
ed to 1 , at whi
h point the player whose turn it is to play loses. For
example, if the board has 5
olumns, the number of
olumns
an be redu
ed to 3 or 4
be
ause 3 < 5 ≤ 6 and 4 < 5 ≤ 8 . No other moves are possible be
ause, for n less than
3 , 2n < 5 , and for n greater than 4 , 5 ≤ n .
The game is easy to win if it is possible to make the board square. This question
is about
al
ulating the mex numbers of the
omponent games in order to determine a
winning move even when the board
annot be made square.
(a) For the
omponent game,
al
ulate whi
h positions are winning and whi
h positions
are losing for the rst 15 positions. Make a general
onje
ture about the winning
and losing positions in the
omponent game and prove your
onje
ture.
Base your proof on the following fa
ts. The end position, position 1 , is a losing
position. A winning position is a position from whi
h there is a move to a losing
position. A losing position is a position from whi
h every move is to a winning
position.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.4. Sums of Games 63
(b) For the omponent game, al ulate the mex number of ea h of the rst 15 positions.
Give the results of your
al
ulation in the form of a table with two rows. The
rst row is a number m and the se
ond row is the mex number of position m .
Split the table into four parts. Part i gives the mex numbers of positions 2i thru
2i+1−1 (where i begins at 0 ) as shown below. (The rst three entries have been
ompleted as illustration.)
Position: 1
Mex Number: 0
Position: 2 3
Mex Number: 1 0
Position: 4 5 6 7
Mex Number: ? ? ? ?
Position: 8 9 10 11 12 13 14 15
Mex Number: ? ? ? ? ? ? ? ?
(You should nd that the mex number of ea
h of the losing positions (identied
in part (a)) is 0 . You should also be able to observe a pattern in the way entries
are lled in for part i+1 knowing the entries for part i . The pattern is based on
whether the position is an even number or an odd number.)
(c) Table 4.3 shows a position in the board game; the rst
olumn shows the number
of
olumns and the se
ond
olumn the number of rows. Using your table of mex
numbers, or otherwise, ll in \losing" if the position is a losing position. If the
position is not a losing position, ll in a winning move either in the form \C n " or
\R n ", where n is an integer; \C" or \R" indi
ates whether the move is to redu
e
the number of \C"olumns or the number of \R"ows, and n is the number whi
h
it should be
ome.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
64 4. Games
4.5 Summary
This
hapter has been about determining winning strategies in simple twoperson 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 brutefor
e sear
h pro
edure to determine whi
h positions are whi
h.
Brutefor
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 brutefor
e sear
h.
Game theory is a ri
h, wellexplored 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 Comput
ing 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 \
omputa
tional
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 \SpragueGrundy" numbers, after their origina
tors. Nim is a wellknown 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.)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
4.6. Bibliographi
Remarks 65
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
66 4. Games
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.
1. It is rumoured that there is gold buried on the island. You ask one of the natives
whether there is gold on the island. The native replies: \There is gold on this
island is the same as I am a knight." The problem is
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?
Algorithmi
Problem Solving
Roland Ba
khouse. 67 May 28, 2008
68 5. Knights and Knaves
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?
7. What question should you ask A to determine whether A and B are the same type
(i.e. both knights or both knaves)?
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.
m2−n2 = (m+n)×(m−n) .
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
n−n = 0 ,
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.2. Cal
ulational Logi
69
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) ,
A=S .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
70 5. Knights and Knaves
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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.2. Cal
ulational Logi
71
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 .
x+y = y+x
and
x×y = y×x .
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 ,
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 .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
72 5. Knights and Knaves
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.
A = (A = G)
is true. So,
true
= { A's statement }
A = (A = G)
= { equality of booleans is asso
iative }
(A = A) = G
= { (A = A) = true ,
substitution of equals for equals }
true = G
= { 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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.2. Cal
ulational Logi
73
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)
= { equality is asso
iative }
(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.
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
= { rule for formulating questions }
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
74 5. Knights and Knaves
A=C
= { from C's statement, C = (A = B) ,
substitution of equals for equals. }
A = (A = B)
= { asso
iativity of equality }
(A = A) = B
= { (A = A) = true }
true = B
= { (true = B) = B }
B .
inx operator is a symbol used to denote a fun
tion of two arguments that is written between the
1 An
two arguments. The symbols \ + " and \ × " are both inx operators, denoting addition and multipli
ation,
respe
tively.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.3. Equivalen
e and Continued Equalities 75
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
p≡q≡r ,
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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
76 5. Knights and Knaves
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.
Moreover, we re
ommend that the \ ≡ " symbol is pronoun
ed as \equivales"; being
an unfamiliar word, its use will help to avoid misunderstanding.
Shortly, we introdu
e a number of laws governing boolean equality. They invariably
involve a
ontninued equivalen
e. A rst example is its re
exivity.
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.)
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
it states that the number m+n is even exa
tly when the parities of m and n are the
same. Parenthesising it as
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 equiv
alen
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:
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.3. Equivalen
e and Continued Equalities 77
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.
Sign of NonZero Numbers The sign of a number says whether or not the number
is positive. For nonzero numbers x and y , the produ
t x×y is positive if the signs of
x and y are equal. If the signs of x and y are dierent, the produ
t x×y is negative.
Assuming that x and y are nonzero, this rule is expressed as
x×y 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
x×y 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
78 5. Knights and Knaves
5.4 Negation
Consider the following knightsandknaves 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) ,
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.4. Negation 79
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 .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
80 5. Knights and Knaves
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
is simplied to
q + 2r
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 .
The name refers to the use of the rule in the form (p ≡ q) = (¬p ≡ ¬q) .
We used the rule of
ontraposition impli
itly in the river
rossing problems. (See
hapter 3.) Re
all that ea
h problem involves getting a group of people from one side of
a river to another, using one boat. If we let n denote the number of
rossings, and l
2 Other authors use the name \
ontraposition" for a less general rule
ombining negation with impli
a
tion.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.5. Contraposition 81
denote the boolean \the boat is on the left side of the river", a
rossing of the river is
modelled by the assignment:
n , l := n+1 , ¬l .
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
is invariant under this assignment. This is be
ause
(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?
The answer is that it is impossible. Suppose the arm
hair is initially positioned along
a northsouth axis. Suppose, also, that the
oor is painted alternately with bla
k and
white squares, like a
hess board, with ea
h of the squares being the same size as the
arm
hair. (See Figure 5.2.) Suppose the arm
hair is initially on a bla
k square. The
requirement is to move the arm
hair from a northsouth position on a bla
k square to a
northsouth position on a white square.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
82 5. Knights and Knaves
Now, let boolean col represent the
olour of the square that the arm
hair is on (say,
true for bla
k and false for white), and dir represent the dire
tion that the arm
hair is
fa
ing (say, true for northsouth and false for eastwest). Then, rotating the arm
hair
col ≡ dir .
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
whi h is false when the hair is on a white square and fa ing northsouth.
90◦
Exercise 5.5 (Knight’s Move) In the game of hess, a knight's move is two pla es
up or down and one pla e left or right, or, vi e versa, two pla es left or right and one
Show that it is impossible to move a knight from the bottomleft
orner of a
hess
board to the topright
orner in su
h a way that every square on the board is visited
exa
tly on
e.
Hint: How many moves have to be made? Model a move in terms of the ee
t on the
number of moves and the
olour of the square on whi
h the knight is standing; identify
a relation between the two that is invariant under a move.
xSy ≡ ySx .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
84 5. Knights and Knaves
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.
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 antire
exivity of the shakeshands 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 antire
exive relation.
Exercise 5.6 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 dif
ferent answer every time. How many times did the host and the host's partner shake
hands?
5.7 Inequivalence
In the knightsandknaves problem mentioned at the beginning of se
tion 5.4, A might
have said \B is dierent from myself". This statement is formulated as B 6= A , or
¬(B = A) This is, in fa
t, the same as saying \B is a knight equals I am not a knight",
as the following
al
ulation shows. Note that we swit
h from \ = " to \ ≡ " on
e again,
in order to exploit asso
iativity.
3 At the time of writing, anyone meeting the British Queen is required to bow or
urtsey, whereas the
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.7. Inequivalen
e 85
¬(B ≡ A)
= { the law ¬p ≡ p ≡ false with p := (B ≡ A) }
B ≡ A ≡ false
= { the law ¬p ≡ p ≡ false with p := A }
B ≡ ¬A .
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) ,
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
86 5. Knights and Knaves
(p 6≡ q) ≡ r
= { expanding the denition of p 6≡ q }
¬(p ≡ q) ≡ r
= { ¬(p ≡ q) ≡ p ≡ ¬q }
p ≡ ¬q ≡ r
= { using symmetry of equivalen
e, the law (5.7)
is applied in the form ¬(p ≡ q) ≡ ¬q ≡ p
with p,q := q,r }
p ≡ ¬(q ≡ r)
= { denition of q 6≡ r }
p ≡ (q 6≡ r) .
Exercise 5.8 Simplify the following. (Note that in ea
h
ase it does not matter in
whi
h order you evaluate the subexpressions. Also, rearranging the variables and/or
onstants doesn't make any dieren
e.)
(a) false 6≡ false 6≡ false
(d) p ≡ p ≡ ¬p ≡ p ≡ ¬p
(e) p 6≡ q ≡ q ≡ p
(f) p 6≡ q ≡ r ≡ p
(g) p ≡ p 6≡ ¬p 6≡ p ≡ ¬p
(h) p ≡ p 6≡ ¬p 6≡ p ≡ ¬p 6≡ ¬p
4 This is to be read asso
iatively, and should not be
onfused with p 6= q 6= r , whi
h some authors
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
5.8. Summary 87
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 .
Exercise 5.12 On the island of knights and knaves, you en
ounter two natives, A
and B. What question should you ask A to determine whether A and B are dierent
types?
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.
5 This operation is usually
alled \ex
lusiveor" in texts on data en
ryption; it is not
ommonly known
that ex lusiveor 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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
88 5. Knights and Knaves
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,
enturieslong 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
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"), followsfrom (\if") and impli
ation (\only if"),
see [Ba
03℄ or [GS93℄.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Chapter 6
Induction
\Indu
tion" is the name given to a problemsolving 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 nonnegative,
whole number  thus 0 , 1 , 2 , 3 , et
. We use the term natural number for a non
negative, 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
.
however, very good reasons why 0 should always be in luded, making a break with tradition imperative.
Algorithmi
Problem Solving
Roland Ba
khouse. 89 May 28, 2008
90 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.
2. Triominoes
A square pie
e of paper is divided into a grid of size 2n×2n , where n is a natural
number2. The individual squares are
alled grid squares. One grid square is
overed, and the others are left un
overed. A triomino is an Lshape made of three
grid squares. Figure 6.2 shows, on the left, an 8×8 grid with one square
overed.
On the right is a triomino.
Show that it is possible to
over the remaining squares with (nonoverlapping)
triominoes. (Fig. 6.3 shows a solution in one
ase.)
NB: The
ase n = 0 should be in
luded in your solution.
3. Trapeziums
An equilateral triangle, with side of length 2n for some natural number n , is
made of smaller equilateral triangles. The topmost equilateral triangle is
overed.
A bu
ketshaped trapezium is made from three equilateral triangles. See g. 6.4.
2 Re
all that the natural numbers are the numbers 0, 1, 2, et
.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.1. Example Problems 91
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
92 6. Indu
tion
poles to the se
ond and, on the day that they
omplete their task, the world will
ome to an end!
Constru
t an indu
tive solution to this problem. The base
ase is when there are
no disks to be moved.
(We see later that the indu
tive solution is
ertainly not the one that the Brahmin
monks use. However, it does provide the basis for
onstru
ting a so
alled iterative
solution to the problem.)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.2. Cutting The Plane 93
For this problem, the number of lines is an obvious measure of the \size" of the
problem. The goal is, thus, to solve the problem \by indu
tion on the number of lines".
This means that we have to show how to solve the problem when there are zero lines
this is the \basis" of the indu
tion and we have to show how to solve the problem
when there are n+1 lines, where n is an arbitrary number, assuming that we
an solve
the problem when there are n lines this is the indu
tion step .
For brevity, we
all a
olouring of the regions with the property that no two adja
ent
regions have the same
olour a satisfa
tory
olouring.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
94 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.
The key to a solution is to note that inverting the
olours of any satisfa
tory
olour
ing (that is,
hanging a bla
k region to white, and vi
eversa) also gives a satisfa
tory
olouring. Now, the additional line divides the sheet of paper into two regions. Let us
all these regions the left and right regions. (By this
hoi
e of names, we do not imply
that the additional line must be from top to bottom of the page. It is just a
onvenient,
easily remembered, way of naming the regions.) Note that the assumed
olouring is a
satisfa
tory
olouring of the left region and of the right region. In order to guarantee
that, either side of the additional line, all regions have opposite
olour,
hoose, say, the
left region, and invert all the
olours in that region. This gives a satisfa
tory
olouring
of the left region (be
ause inverting the
olours of a satisfa
tory
olouring gives a satis
fa
tory
olouring). It also gives a satisfa
tory
olouring of the right region (be
ause the
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.2. Cutting The Plane 95
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 onebyone. 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 nondeterministi
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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
96 6. Indu
tion
6.3 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 2n×2n , 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 Lshape
made of three grid squares. Show that it is possible to
over the remaining
squares with (nonoverlapping) 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 20×20 , i.e. 1×1 . 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+1×2n+1 . We make the indu
tion hy
pothesis that it is possible to
over any grid of size 2n×2n 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+1×2n+1 of whi
h one square has been
overed.
A grid of size 2n+1×2n+1
an be subdivided into 4 grids ea
h of size 2n×2n , simply
by drawing horizontal and verti
al dividing lines through the middle of ea
h side. Let
us
all the four grids the bottomleft, bottomright, topleft, and topright grids. One
grid square is already
overed. This square will be in one of the four subgrids. We may
assume that it is in the bottomleft grid. (If not, the entire grid
an be rotated about
the
entre so that it does be
ome the
ase.)
The bottomleft grid is thus a grid of size 2n×2n of whi
h one square has been
overed. By the indu
tion hypothesis, the remaining squares in the bottomleft grid
an
be
overed with triominoes. This leaves us with the task of
overing the bottomright,
topleft and topright 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 bottom
right, topleft and topright grids with triominoes. On
ompletion of this pro
ess, the
entire 2n+1×2n+1 grid has been
overed with triominoes.
Exercise 6.2 Solve the trapezium problem given in se
tion 6.1.
2
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.4. Looking For Patterns 97
Figure 6.9: Triomino Problem. Indu
tive Step. The grid is divided into four subgrids.
The
overed square, shown in bla
k, identies one subgrid. 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 subgrids with triominoes.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
98 6. Indu
tion
orre
t.)
Several of the mat
hsti
k games studied in
hapter 4 provide good examples of math
emati
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 .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.5. The Need For Proof 99
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
100 6. Indu
tion
Figure 6.11: Cutting the ake. The ase n = 6 . The number of portions is 31 , not 26−1
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 wellknown formula gives the sum of the natural numbers from 1 thru n :
1
1+2+ ... +n = n(n+1) .
2
Two other exer
ises, often given in mathemati
al texts, are to verify that
1
12 + 22 + . . . + n2 = n(n+1)(2n + 1)
6
and
1 2
13 + 23 + . . . + n3 = n (n+1)2 .
4
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 = ? .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.6. From Veri
ation to Constru
tion 101
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 nontrivial problems involves a
reative pro
ess. This means
that a
ertain amount of guesswork is ne
essary, and trialanderror
annot be
ompletely
eliminated. Redu
ing the guesswork to a minimum, repla
ing it by mathemati
al
al
u
lation 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 overambitious. 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 (oftforgotten)
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 }
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
102 6. Indu
tion
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) = b(n+1) + c(n+1)2
= { heading for use of the indu
tion hypothesis,
S.(n+1) = S.n + n + 1 }
S.n + n + 1 = b(n+1) + c(n+1)2
= { assumption: P.n . Also, a = 0 .
That is, S.n = bn + cn2 }
bn + cn2 + n + 1 = b(n+1) + c(n+1)2
= { arithmeti
}
cn2 + (b+1)n + 1 = cn2 + (b + 2c)n + b + c
⇐ {
omparing
oeÆ
ients of powers of n }
c = c ∧ b+1 = b + 2c ∧ 1 = b + c
= { arithmeti
}
1 1
2
=c ∧ 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 1
1+2+ ... +n = 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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.7. FakeCoin Dete
tion 103
a system of simultaneous equations in the
oeÆ
ients. Finally, solve the system of
equations.
Remark : In the
ase of the sum 1 + 2 + . . . + n there is an easier way to derive the
orre
t formula. Simply write down the required sum
1 + 2 + . .. + n ,
and immediately below it
n + n−1 + . .. + 1 .
Then add the two rows together:
n+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
for m greater than 1 . End of remark.
2
Exercise 6.3 Use the te hnique just demonstrated to onstru t losed formulae for
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 mat
hes
an be removed. By
onsidering a few simple examples (for example, m
n
is and is arbitrary, or m is 2 and n is 3 ), formulate a general rule for determining
1 n
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.
2
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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.7. FakeCoin Dete
tion 105
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.
The Basis With zero
omparisons, we
an report immediately that all
oins in a
ol
le
tion of (30−1)/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 (3n−1)/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+1−1)/2 = 3×((3n−1)/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,
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
106 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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.7. FakeCoin Dete
tion 107
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. So too are the
possibly light
oins on the left s
ale and the possibly heavy
oins on the right
s
ale. Pro
eed with the possibly heavy
oins on the left s
ale and the possibly light
oins on the right s
ale.
If the s
ales tip to the right, the
oins on the table are genuine. So too are the
possibly light
oins on the right s
ale and the possibly heavy
oins on the left s
ale.
Pro
eed with the possibly heavy
oins on the right s
ale and the possibly light
oins
on the left s
ale.
The solution to the unmarked
oin problem when the number of
oins is (3n+1−1)/2
is as follows.
Divide the
oins into three groups of sizes (3n−1)/2 , (3n−1)/2 + 1 and (3n−1)/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:
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
108 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.
Exercise 6.5 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 2×m
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.6 Given are n obje
ts, where 1 ≤ n , ea
h of dierent weight. A pair of
s
ales is provided so that it is possible to determine, for any two of the obje
ts, whi
h is
the lighter and whi
h is the heavier.
a) How many
omparisons are needed to nd the lightest obje
t?
b) Show, by indu
tion on n , that it is possible to determine whi
h is the lightest and
whi
h is the heaviest obje
t using 2n − 3
omparisons. Assume that 2 ≤ n .
) Suppose there are 4 obje
ts with weights A , B , C and D , and suppose A < B
and C < D . Show how to nd the lightest and heaviest of all four with two additional
omparisons. Use this to show how to nd the lightest and heaviest of 4 obje
ts using
4
omparisons (and not 5 , as in your solution to part (b)).
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
6.8. Summary 109
d) Suppose there are 2m obje
ts, where 1 ≤ m . Show, by indu
tion on m , that it
is possible to nd the lightest and heaviest obje
ts using 3m − 2
omparisons. (Hint:
make use of (
).)
6.8 Summary
Indu
tion is one of the most important problemsolving 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,
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
110 6. Indu
tion
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 problemsolving strategy.
The Towers of Hanoi problem is a puzzle that is quite diÆ
ult to solve without a
systemati
problemsolving 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.
Algorithmi
Problem Solving
Roland Ba
khouse. 111 May 28, 2008
112 7. The Towers of Hanoi
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!
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
7.2. Indu
tive Solution 113
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 even
numbered day when all the disks are on oneandthesame pole.
Try exe
uting this algorithm yourself on, say, a 4 disk puzzle. Take
are to
y
le the
smallest disk on the oddnumbered moves and to obey the rule not to pla
e a disk on
top of a disk smaller than itself on the evennumbered 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 under
standing 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.
1. Move the top n disks from A to B. After doing this, we have exhausted all possi
bilities 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
114 7. The Towers of Hanoi
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 formulat
ing 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 M−n other disks. If some of these M−n 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 .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
7.2. Indu
tive Solution 115
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 onebyone 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 problemsolving 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 lefttoright
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
= { 2nd equation, n,d := 1,cw }
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
116 7. The Towers of Hanoi
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!
Exercise 7.1 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.2 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 statetransition 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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
7.3. The Iterative Solution 117
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
on
sequen
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 even
numbered disks should
y
le in a
lo
kwise dire
tion, and all oddnumbered disks should
y
le in an anti
lo
kwise dire
tion. Vi
eversa, if it is required to move an odd number of
disks in a
lo
kwise dire
tion, all evennumbered disks should
y
le in an anti
lo
kwise
dire
tion, and all oddnumbered disks should
y
le in a
lo
kwise dire
tion. In parti
ular,
the smallest disk (whi
h is oddnumbered) 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.
Exercise 7.3 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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
118 7. The Towers of Hanoi
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 nonempty 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) ⇐ alt.ks ∧ ((ks = [ ]) ≡ (k = 1)) .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
7.4. Summary 119
Exercise 7.4 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 neverending loop!
Fortunately, the explorer was able to tell the monks how to pro
eed in order to return
all the disks to oneandthesame 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
even.n ≡ d ≡ even.k ≡ d ′
for the dire tion d ′ moved by disk k will move n disks in the dire tion d .)
Exercise 7.5 (Coloured Disks) Suppose ea
h disk is
oloured, red white or blue.
The
olouring of disks is random; dierent disks may be
oloured dierently.
Devise an algorithm that will sort the disks so that all the red disks are on one pole,
all the white disks are on another pole, and all the blue disks are on the third pole. You
may assume that, initially, all disks are on one pole.
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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
120 7. The Towers of Hanoi
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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Chapter 8
In this
hapter, we present a solution to a more general version of the tor
h problem in
exer
ise 3.4. The generalisation is to
onsider an arbitrary number of people; the task is
to get all the people a
ross a bridge in the optimal time.
Spe
i
ally, the problem we dis
uss is the following.
For simpli
ity, we assume that t.i < t.j whenever i < j . (This means that we assume
the people are ordered a
ording to
rossing time and that their
rossing times are
distin
t. Assuming that the
rossing times are distin
t makes the arguments simpler, but
is not essential. If the given times are su
h that t.i = t.j for some i and j , where i < j , we
an always
onsider pairs (t.i , i) , where i ranges over people, ordered lexi
ographi
ally.
Renaming the
rossing \times" to be su
h pairs, we obtain a total ordering on times with
the desired property.)
Algorithmi
Problem Solving
Roland Ba
khouse. 121 May 28, 2008
122 8. The Tor
h Problem
In the original problem given in
hapter 1 , there are four people with
rossing times
of 1 minute, 2 minutes, 5 minutes and 10 minutes. Cru
ially, the question asked
was to show that all four
an
ross the bridge within 17 minutes. In other words, the
question asks for a so
alled upper bound on the time taken. In general, an upper bound
is established by exhibiting a sequen
e of
rossings that takes the required time.
A mu
h harder problem is to show that 17 minutes is a lower bound on the time
taken. Showing that it is a lower bound means showing that the time
an never be
bettered.
We
an use the same instan
e of the tor
h problem to further illustrate the dieren
e
between lower and upper bounds. Most of us, when
onfronted with the tor
h problem
above, will rst explore the solution in whi
h the fastest person a
ompanies the others
a
ross the bridge. Su
h a solution takes a total time of 2+1+5+1+10 , i.e. 19 minutes.
By exhibiting the
rossing sequen
e, we have established that 19 minutes is an upper
bound on the
rossing time; we have not established that it is a lower bound. (Indeed, it
is not.) Similarly, by exhibiting the
rossing sequen
e that gets all four people a
ross in
17 minutes does not prove that this time
annot be bettered. Doing so is mu
h harder
than just
onstru
ting the sequen
e.
In this
hapter, the goal is to
onstru
t an algorithm for s
heduling N people to
ross
the bridge. The algorithm we derive is quite simple but, on its own, it only establishes
an upper bound on the optimal
rossing time. The greatest eort goes into showing
that the algorithm simultaneously establishes a lower bound on the
rossing time. The
ombination of equal lower and upper bounds is
alled an exa
t bound; this is what is
meant by an optimal solution.
In se
tion 8.6, we present two algorithms for
onstru
ting an optimal sequen
e. The
more eÆ
ient algorithm assumes a knowledge of algorithm development that goes beyond
the material in this book.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
8.2. Outline Strategy 123
Re
all that, when
rossing the bridge, the tor
h must always be
arried. This means
that
rossings alternate between \forward"and \return" trips, where a forward trip is a
rossing in the desired dire
tion, and a return trip is a
rossing in the opposite dire
tion.
Informally, the forward trips do the work whilst the return trips servi
e the forward trips.
The idea is that, if we
an
ompute the optimal
olle
tion of forward trips, the return
trips needed to sequen
e them
orre
tly
an be easily dedu
ed.
In order to turn this idea into an ee
tive solution, we need to pro
eed more formally.
First, by the \
olle
tion" of forward trips, we mean a \bag" of sets of people. The
mathemati
al notion of a \bag" (or \multiset" as it is sometimes
alled) is similar to a
set but, whereas a set is dened solely by whether or not a value is an element of the
set, a bag is dened by the number of times ea
h value o
urs in the set. For example, a
bag of
oloured marbles would be spe
ied by saying how many red marbles are in the
bag, how many blue marbles, and so on. We will write, for example, {1∗a , 2∗b , 0∗c} to
denote a bag of a s, b s and c s in whi
h a o
urs on
e, b o
urs twi
e and c o
urs
no times. For brevity, we also write {1∗a , 2∗b} to denote the same bag.
It is important to stress that a bag is dierent from a sequen
e. Even though when
we write down an expression denoting a bag we are for
ed to list the elements in a
ertain
order (alphabeti
al order in {1∗a , 2∗b , 0∗c} , for example), the order has no signi
an
e.
The expressions {1∗a , 2∗b , 0∗c} and {2∗b , 1∗a , 0∗c} both denote the same bag.
A trip is given by the set of people involved in the trip. So, for example, {1,3} is a
trip in whi
h persons 1 and 3
ross. If we are obliged to distinguish between forward
and return trips, we prex the trip with either \ + " (for forward) or \ − " (for return).
So +{1,3} denotes a forward trip made by persons 1 and 3 and −{2} denotes a return
trip made by person 2 .
As we said above, our fo
us will be on
omputing the bag of forward trips in an
optimal sequen
e of trips. We begin by establishing a number of properties of sequen
es
of trips that allow us to do this.
We
all a sequen
e of trips that gets everyone a
ross in a
ordan
e with the rules a
valid sequen
e. We will say that one valid sequen
e subsumes another valid sequen
e
if the time taken by the rst is at most the time taken for the se
ond. Note that the
subsumes relation is re
exive (every valid sequen
e subsumes itself) and transitive (if
valid sequen
e a subsumes valid sequen
e b and valid sequen
e b subsumes valid
sequen
e c then valid sequen
e a subsumes valid sequen
e c ). The problem is to nd
a valid sequen
e that subsumes all valid sequen
es.
Formally, a valid sequen
e is a set of numbered trips with the following two proper
ties:
The trips are sets; ea
h set has one or two elements, and the number given to a
trip is its position in the sequen
e (where numbering begins from 1 ).
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
124 8. The Tor
h Problem
Oddnumbered trips in the sequen
e are
alled forward trips; evennumbered trips
are
alled return trips. The length of the sequen
e is odd.
The trips made by ea
h individual person alternate between forward and return
trips, beginning and ending with forward trips. (A trip T is made by person i if
i∈T .)
The number of forward trips is one more than the number of return trips.
The number of forward trips made by ea
h individual person is one more than the
number of return trips made by that person.
A regular forward trip means a forward trip made by two people, and a regular
return trip means a return trip made by exa
tly one person. A regular sequen
e is a
valid sequen
e that
onsists entirely of regular forward and return trips.
The rst step (lemma 8.1) is to show that every valid sequen
e is subsumed by one
in whi
h all trips are regular. The signi
an
e of this is threefold.
The number of forward trips is N−1 and the number of return trips is N−2 .
(Re
all that N is the number of people.)
The time taken by a regular sequen
e
an be evaluated knowing only whi
h forward
trips are made; not even the order in whi
h they are made needs to be known.
(Knowing the bag of forward trips, it is easy to determine how many times ea
h
person makes a return trip. This is be
ause ea
h person makes one fewer return
trips than forward trips. In this way, the time taken for the return trips
an be
al
ulated.)
Most importantly, knowing just the bag of forward trips in a regular sequen
e is
suÆ
ient to re
onstru
t a valid regular sequen
e. Sin
e all su
h sequen
es take the
same total time, we
an thus repla
e the problem of nding an optimal sequen
e of
forward and return trips by the problem of nding an optimal bag of forward trips
Finding an optimal bag of forward trips is then a
hieved by fo
using on whi
h people
do not make a return trip. We prove the obvious property that, in an optimal solution,
the two slowest people do not return. We
an then use indu
tion to determine the
omplete solution.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
8.3. Regular Sequen
es 125
Lemma 8.1 Every valid sequen
e
ontaining irregular trips is subsumed by a stri
tly
faster valid sequen
e without irregular trips.
Proof Suppose a given valid sequen
e
ontains irregular trips. We
onsider two
ases:
the rst irregular trip is forward and the rst irregular trip is ba
kward.
If the rst irregular trip is ba
kward,
hoose an arbitrary person, p say, making the
trip. Identify the forward trip made by p prior to the ba
kward trip, and remove p
from both trips. More formally, suppose the sequen
e has the form
u +{p,q} v −{p,r} w
where q and r are people, u , v and w are subsequen
es and p o
urs nowhere in v .
(Note that the forward trip made by p involves two people be
ause it is assumed that
the rst irregular trip is ba
kward.) Repla
e the sequen
e by
u +{q} v −{r} w
This results in a valid sequen
e, the time for whi
h is no greater than the original
sequen
e. (To
he
k that the sequen
e remains valid, we have to
he
k that the trips
made by ea
h individual
ontinue to alternate between forward and return. This is true
for individuals other than p be
ause the points at whi
h they
ross remain un
hanged,
and it is true for p be
ause the trips made by p have
hanged by the removal of
onse
utive forward and return trips. The time taken is no greater sin
e, for any x and
y , t.p ↑ x + t.p ↑ y ≥ x+y .) The number of irregular
rossings is not redu
ed, sin
e a
new irregular forward trip has been introdu
ed, but the total number of persontrips is
redu
ed.
Now suppose the rst irregular trip is forward. There are two
ases to
onsider: the
irregular trip is the very rst in the sequen
e, and it is not the very rst.
If the rst trip in the sequen
e is not regular, it means that one person
rosses and
then immediately returns. (We assume that N is at least 2 .) These two
rossings
an
be removed. Clearly, sin
e times are positive, the total time taken is redu
ed. Also, the
number of persontrips is redu
ed.
If the rst irregular
rossing is a forward trip but not the very rst, let us suppose it
is person p who
rosses, and suppose q is the person who returns immediately before
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
126 8. The Tor
h Problem
this forward trip. (There is only one su
h person be
ause of the assumption that p 's
forward trip is the rst irregular trip.) That is, suppose the sequen
e has the form
u −{q} +{p} v
Consider the latest
rossing that pre
edes q ′ s return trip and involves p or q. There
are two
ases: it is a forward trip or it is a return trip.
If it is a forward trip, it must involve q and not p . Swap p with q in this trip and
remove q 's return trip and p 's irregular
rossing. That is, transform
u +{q,r} w −{q} +{p} v
The result is a valid sequen
e. Moreover, the total
rossing time is redu
ed (sin
e, for
any x , t.q ↑ x + t.q + t.p > t.p ↑ x ), and the number of persontrips is also redu
ed.
If it is a return trip, it is made by one person only. (This is be
ause we assume
that p ′ s forward trip is the rst irregular trip in the sequen
e.) That person must be
p . Swap p with q in this return trip, and remove q 's return trip and p 's irregular
rossing. That is, transform
u −{p} w −{q} +{p} v
The result is a valid sequen
e. Moreover, the total
rossing time is redu
ed (sin
e,
t.p + t.q + t.p > t.q ), and the number of persontrips is also redu
ed.
We have now des
ribed how to transform a valid sequen
e that has at least one irreg
ular
rossing; the transformation has the ee
t of stri
tly de
reasing the total time taken.
Repeating this pro
ess whilst there are still irregular
rossings is therefore guaranteed to
terminate with a valid sequen
e that is regular, subsumes the given valid sequen
e and
has a smaller persontrip
ount.
2
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
8.4. Sequen
ing Forward Trips 127
the number of people that have
rossed by one, and the last trip in
reases the number
by two. Thus, after the rst 2×(N−2) trips, N−2 people have
rossed and 2 have not;
so, after 2×(N−2) + 1 trips everyone has
rossed.
Se
ond, it means that the total time taken to
omplete any regular sequen
e
an be
evaluated if only the bag of forward trips in the sequen
e is known; not even the order in
whi
h the trips are made is needed. This is be
ause the bag of forward trips enables us
to determine how many times ea
h individual makes a forward trip. Hen
e, the number
of times ea
h individual returns
an be
omputed, from whi
h the total time for the
return trips
an be
omputed.
For example, suppose the forward trips in a regular sequen
e are as follows:
3∗{1,2} , 1∗{1,6} , 1∗{3,5} , 1∗{3,4} , 1∗{7,8}
(The trips are seperated by
ommas; re
all that 3∗{1,2} means that persons 1 and 2
make 3 forward trips together, 1∗{1,6} means that persons 1 and 6 make one forward
trip together, et
. Note that no indi
ation is given of the order in whi
h the forward
trips o
ur in the sequen
e.) Then,
ounting the number of o
urren
es of ea
h person
in the bag, person 1 makes 4 forward trips, and hen
e 3 return trips; similarly, person
2 makes 3 forward trips and hen
e 2 return trips, whilst person 3 makes 2 forward
trips and hen
e 1 return trip. The remaining people ( 4 , 5 , 6 , 7 and 8 ) all make 1
forward trip and, hen
e, no return trips. The total time taken is thus:
3 × (t.1↑t.2) + 1 × (t.1↑t.6) + 1 × (t.3↑t.5) + 1 × (t.3↑t.4) + 1 × (t.7↑t.8)
+ 3 × t.1 + 2 × t.2 + 1 × t.3
(The top line gives the time taken by the forward trips, and the bottom line gives the
time taken by the return trips.) Note that the total number of forward trips is 7 (one
less than the number of people), and the total number of return trips is 6 .
The third important
orollary of lemma 8.1 is that, given just the bag of forward
trips
orresponding to a regular sequen
e, it is possible to
onstru
t a regular sequen
e
to get everyone a
ross with the same
olle
tion of forward trips.
This is a nonobvious property of the forward trips and to prove that it is indeed the
ase we need to make some
ru
ial observations.
Suppose F is a bag of forward trips
orresponding to some regular sequen
e. That
is, F is a
olle
tion of sets, ea
h with exa
tly two elements and ea
h having a
ertain
multipli
ity. The elements of the sets in F are people, whi
h we identify with numbers
in the range 1 thru N , and ea
h number in the range must o
ur at least on
e. The
number of times a person o
urs is the number of forward trips made by that person.
We will
all a person a settler if they make only one forward trip; we
all a person
a nomad if they make more than one forward trip. Division of people into these two
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
128 8. The Tor
h Problem
types
auses the trips in F to be divided into three types depending on the number of
settlers in the trip. If both people in a trip are settlers we say the trip is hard ; if one
of the people is a settler and the other a nomad, we say the trip is rm ; nally, if both
people are nomads we say the trip is soft.
Now, suppose we use #nomad , #hard , #firm and #soft to denote the number
of nomads, the number of hard trips, the number of rm trips and the number of soft
trips, respe
tively, in the
olle
tion F . Then, the number of trips in F is
#hard + #firm + #soft .
The number of return trips equals the total number of forward trips made by individual
nomads less the number of nomads, sin
e ea
h nomad makes one more forward trip than
return trip. Sin
e ea
h soft trip
ontributes 2 to the number of forward trips made by
nomads, and ea
h rm trip
ontributes 1 , the number of return trips is thus
2 × #soft + #firm − #nomad .
But the number of forward trips is one more than the number of return trips. That is,
2
Formally, what we have proved is the following.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
8.4. Sequen
ing Forward Trips 129
Lemma 8.4 Given a valid regular sequen
e of trips to get N people a
ross, where N
is at least 2 , the bag of forward trips F that is obtained from the sequen
e by forgetting
the numbering of the trips is a regular N bag. Moreover, the time taken by the sequen
e
of trips
an be evaluated from F . Let #FT denote the multipli
ity of T in F. Then, the
time taken is
(8.5) hΣT : T ∈F : h⇑i : i∈T : t.ii × #FT i + hΣi :: t.i × rF.ii
where
(8.6) rF.i = hΣT : T ∈F ∧ i∈T : #FT i − 1 .
If N (the number of people) is greater than 2 , not all
an
ross at on
e and, so,
#nomad.F is at least 1 . It follows from (8.3) that #soft.F is at least 1 .
Now we
an show how to
onstru
t a regular sequen
e from F .
Lemma 8.10 G iven N (at least 2 ) and a regular N bag, a valid regular sequen
e of
trips
an be
onstru
ted from F to get N people a
ross. The time taken by the sequen
e
is given by (8.5).
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
130 8. The Tor
h Problem
order and inserting a return trip by the nomad n in between ea
h pair of forward trips
of whi
h the rst is the trip {n,s} for some s .
The third
ase is that #hard.F is nonzero and F has more than one trip. In this
ase, by (8.9), #soft.F is at least 1 . It follows, by denition of soft that #nomad.F
is at least 2 . Choose any soft trip in F . Suppose it is {n,m} where n and m are both
nomads. Constru
t the sequen
e whi
h begins with the trip {n,m} and is followed by
the return of n , then an arbitrary hard trip and then the return of m . Redu
e the
multipli
ity of the
hosen hard trip and the
hosen soft trip in F by 1 . (That is, remove
one o
urren
e of ea
h from F .) We get a new bag F ′ in whi
h the number of trips
made by ea
h of n and m has been redu
ed by 1 and the number of people has been
redu
ed by 2 . By indu
tion, there is a regular sequen
e
orresponding to F ′ whi
h gets
the remaining people a
ross.
2
Optimisation Problem Lemmas 8.4 and 8.10 have a signi
ant impa
t on how to
solve the general
ase of the bridge problem. Instead of seeking a sequen
e of
rossings
of optimal duration, we seek a regular bag as dened in denition 8.2 that optimises the
time given by (8.5). It is this problem that we now solve.
In solving this problem, it is useful to introdu
e some terminology when dis
ussing
the time taken as given by (8.5). There are two summands in this formula, The value of
the rst summand we
all F 's total forward time and the value of the se
ond summand
F 's total return time. Given a bag F and a trip T in F , we
all h⇑i : i∈T : t.ii × #FT
the forward time of T in F . (Sometimes \in F " is omitted if this is
lear from the
ontext.) For ea
h person i , we
all the value of t.i × rF.i the return time of person i
(or person i 's return time).
Lemma 8.11 Every regular bag is subsumed by a regular bag for whi
h all settlers
are slower than all nomads.
Proof Suppose the regular N bag F is given. Call a pair of people (p, q) an inversion
if, within F , p is a settler, q is a nomad and p is faster than q .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
8.5. Choosing Settlers and Nomads 131
Choose any inversion (p, q) . Suppose q and p are inter
hanged everywhere in
F . We get a regular N bag. Moreover, the return time is
learly redu
ed by at least
t.q − t.p .
The forward times for the trips not involving p or q are, of
ourse, un
hanged. The
forward time for the trips originally involving q are not in
reased (be
ause t.p < t.q ).
The forward time for the one trip originally involving p is in
reased by an amount that
is at most t.q − t.p . This is veried by
onsidering two
ases. The rst
ase is when the
trip involving p is {p,q} . In this
ase, swapping p and q has no ee
t on the trip, and
the in
rease in time taken is 0 . In the se
ond
ase, the trip involving p is {p,r} where
r 6= q . In this
ase, it suÆ
es to observe that
Finally, the times for all other forward trips are un
hanged.
The net ee
t is that the total time taken does not in
rease. That is, the transformed
bag subsumes the original bag. Also, the number of inversions is de
reased by at least
one. Thus, by repeating the pro
ess of identifying and eliminating inversions, a bag F
is obtained that subsumes the given bag.
2
Corollary 8.12 Every regular N bag is subsumed by a regular N bag F with the
following properties:
In any rm trip in F , the nomad is person 1 .
Every soft trip in F is {1,2} . (Note: the multipli
ity of this trip in the bag
an be
an arbitrary number, in
luding 0 .)
The multipli
ity of {1,2} in F is j , for some j where 1 ≤ j ≤ N÷2 , and the hard
trips are {k: 0 ≤ k < j−1: {N − 2×k , N − 2×k − 1}} . (Note that this is the empty
set when j equals 1 .)
Proof Suppose F is a regular N bag that optimises the total travel time. From 8.11,
we may assume that the nomads are slower than the settlers.
Suppose there is a rm trip in F in whi
h the nomad is person i where i is not 1 .
Repla
e i in one o
urren
e of the trip by person 1 . This has no ee
t on the forward
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
132 8. The Tor
h Problem
time, sin
e i is slower than the other person in the trip. However, the total return
time is redu
ed (by t.i − t.1 ). We
laim that this results in a regular N bag, whi
h
ontradi
ts F being optimal. (Please refer to lemma 8.4 for the properties required of a
regular bag.)
Of
ourse, the size of ea
h set in F is un
hanged. The number of trips made by
i de
reases by one, but remains positive be
ause i is a nomad in F . The number of
trips made by all other persons either remains un
hanged or, in the
ase of person 1 ,
in
reases. So it remains to
he
k property (8.3). If person i is still a nomad after the
repla
ement, property (8.3) is maintained be
ause the type (hard, rm or soft) of ea
h
trip remains un
hanged. However, person i may be
ome a settler. That is, the number
of nomads may be de
reased by the repla
ement. If so, person i is an element of two
trips in F. The se
ond trip is either a rm trip in F and be
omes a hard trip, or it
is a soft trip in F and be
omes rm. In both
ases, it is easy to
he
k that (8.3) is
maintained.
Now suppose there is a soft trip in F dierent from {1,2} . A similar argument to the
one above shows that repla
ing the trip by {1,2} results in a regular bag with a stri
tly
smaller total travel time,
ontradi
ting F being optimal.
We may now
on
lude from (8.3) that either there are no soft trips or the multipli
ity
of {1,2} in F is j , for some j where j is at least 2 , and there are j−1 hard trips. When
there are no soft trips, all the trips are rm or hard. But, as we have shown, person 1
is the only nomad in rm trips and there are no nomads in hard trips; it follows that
person 1 is the only nomad in F and, from (8.3), that there are no hard trips. Thus
persons 1 and 2 are the elements of one (rm) trip in F .
It remains to show that, when j is at least 2 , the hard trips form the set
(The multipli
ity of ea
h hard trip is 1 , so we
an ignore the distin
tion between bags
and sets.)
Assume that the number of soft trips is j where j is at least two. Then the settlers
are persons 3 thru N , and 2×(j−1) of them are elements of hard trips, the remaining
N − 2×(j−2) being elements of rm trips. Any regular bad
learly remains regular under
any permutation of the settlers. So we have to show that
hoosing the settlers so that
the hard trips are lled in order of slowness gives the optimal arrangement. This is done
by indu
tion on the number of settlers in the hard trips.
2
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
8.6. The Algorithm 133
(The rst three terms give the forward times, and the last two terms give the return
times.)
Proof There are two
ases to
onsider. If there are no soft trips, the value of j is 1 .
In this
ase, the total time taken is
hΣi : 2 ≤ i ≤ N : t.ii + (N−2) × t.1 .
But
HF.1 + FF.1 + 1 × t.2 + (N−1−1) × t.1 + (1−1) × t.2
= { denition of HF and FF , arithmeti
}
0 + hΣi : 3 ≤ i ≤ N : t.ii + t.2 + (N−2) × t.1
= { arithmeti
}
hΣi : 2 ≤ i ≤ N : t.ii + (N−2) × t.1 .
If there are soft trips, the value of j is equal to the number of soft trips and is at
least 2 . In this
ase, HF.j is the forward time for the hard trips in F and FF.j is the
forward time for the rm trips in F . Also, j × t.2 is the forward time for the j soft
trips. Finally, person 1 's return time is (N−j−1) × t.1 and person 2 's return time is
(j−1) × t.2 . (Person 2 is an element of j forward trips, and person 1 is an element of
j + (N − 2×(j−1) − 3 + 1) forward trips. Note that the sum of j−1 and N−j−1 is N−2 ,
whi
h is what we expe
t the number of return trips to be.)
2
For all j , where j is at least 2 , dene OT .j to be the optimal time taken by a regular
N bag where the multipli
ity of {1,2} in the bag is j . That is, OT .j is given by (8.14).
Now, we observe that
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
134 8. The Tor
h Problem
and
FF.(j+1) − FF.j = −(t.(N−2j+2) + t.(N−2j+1)) .
As a
onsequen
e,
OT .(j+1) − OT .j = −t.(N−2j+1) + 2 × t.2 − t.1 .
Note that
OT .(j+1) − OT .j ≤ OT .(k+1) − OT .k
= { above }
−t.(N−2j+1) + 2 × t.2 − t.1 ≤ −t.(N−2k+1) + 2 × t.2 − t.1
= { arithmeti
}
t.(N−2k+1) ≤ t.(N−2j+1)
= { t is in
reasing }
j ≤ k .
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
8.7. Con
lusion 135
8.7 Conclusion
In this
hapter, we have presented an algorithm to solve the tor
h problem for an arbi
trary number of people and arbitrary individual
rossing times. The greatest
hallenge in
an optimisation problem of this nature is to establish without doubt that the algorithm
onstru
ts a solution that
an not be bettered. A major step in solving the problem
was to eliminate the need to
onsider sequen
es of
rossings and to fo
us on the bag
of forward trips. Via a number of lemmas, we established a number of properties of an
optimum bag of forward trips whi
h then enabled us to
onstru
t the required algorithm.
Many of the properties we proved are not surprising. An optimal sequen
e is \regular"
 that is, ea
h forward trip is made by two people and ea
h return trip is made by
one; the \settlers" (the people who never make a return trip) are the slowest, and the
\nomads" (the people who do make return trips) are the fastest. Less obvious is that
there are at most two nomads and the number of \hard" trips (trips made by two settlers)
is one less than the number of trips that the two fastest people make together. The proof
of the fa
t that there are at most two nomads is made parti
ularly easy by the fo
us
on the bag of forward trips; if we had had to reason about the sequen
e of trips, this
property
ould have been very diÆ
ult to establish.
Even though these properties may seem unsurprising and the nal algorithm (in
retrospe
t) perhaps even \obvious", it is important to appre
iate that proof is required
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
136 8. The Tor
h Problem
 the interest in the tor
h problem is that the most \obvious" solution (letting the
fastest person a
ompany everyone else a
ross the bridge) is not always the best solution.
Always beware of
laims that something is \obvious".
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Chapter 9
Knight’s 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
hess
board  a so
alled brutefor
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. 9.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 require
ments 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.
Algorithmi
Problem Solving
Roland Ba
khouse. 137 May 28, 2008
138 9. Knight's Cir
uit
Let's turn the problem on its head. Suppose you want to make a
ir
uit of a
hess
board and you are allowed to
hoose a set of moves that you are allowed to make. What
sort of moves would you
hoose?
The obvious rst answer is to allow moves from any square to any other square. In
that
ase, it's always possible to
onstru
t a
ir
uit of any board, whatever its size 
starting from any square,
hoose a move to a square that has not yet been visited until
all the squares are exhausted; then return to the starting square. But that is just too
easy. Let's
onsider
hoosing from a restri
ted set of moves.
The simplest move is one square horizontally or verti
ally. (These are the moves that
a King
an make, but ex
luding diagonal moves.) We
all these moves straight moves.
Is it possible to make a
ir
uit of a
hessboard just with straight moves?
The answer is yes, although it isn't immediately obvious. You may be able to nd a
straightmove
ir
uit by trial and error, but let us try to nd one more systemati
ally.
As is often the
ase, it is easier to solve a more general problem; rather than restri
t the
problem to an 8×8 board, let us
onsider an arbitrary re
tangular board. Assuming
ea
h move is by one square only, to the left or right, or up or down, is it possible to
omplete a straightmove
ir
uit of the entire board? That is, is it possible to visit every
square exa
tly on
e, beginning and ending at the same square, making \straight" moves
at ea
h step?
In order to gain some familiarity with the problem, please ta
kle the following exer
ise.
Its solution is relatively straightforward.
Exercise 9.1
(a) What is the relation between the number of moves needed to
omplete a
ir
uit of
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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
9.1. StraightMove Cir
uits 139
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 straightmove
ir
uit of a board
of size 2m×1 ? (A 2m×1 board has one
olumn of squares; the number of squares
is 2m .)
(c) Show that it is possible to
omplete a straightmove
ir
uit of a 2×n board for all
(positive) values of n . (A 2×n board has two rows, ea
h row having n squares.)
2
The
on
lusion of exer
ise 9.1 is that a straightmove
ir
uit is only possible if the
board has size 2m×n , 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 nonzero be
ause the problem
assumes the existen
e of at least one starting square.) Also, a straightmove
ir
uit
an
always be
ompleted when the board has size 2×n , for positive n . This suggests that
we now try to
onstru
t a straightmove
ir
uit of a 2m×n board, for m at least one
and n greater than one, by indu
tion on m , the 2×n
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 straightmove
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 straightmove
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 bottomleft
orner of the 2p × n board is immediately
above the topleft
orner of the 2q × n board. Constru
t straightmove
ir
uits of these
two boards. Figure 9.2 shows the result diagrammati
ally. The two horizontal, red
lines at the middleleft 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 bottomleft and topleft
orners by verti
al moves, as shown
by the verti
al green lines in g. 9.3. This
ompletes the
onstru
tion.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
140 9. Knight's Cir
uit
2p
2m
2q
Figure 9.2: Combining straightmove
ir
uits. First, split the board into two smaller
boards and
onstru
t straightmove
ir
uits of ea
h.
n
2p
2m
2q
Figure 9.4 shows the
ir
uit that is
onstru
ted in this way for a 6 × 8 board. Ef
fe
tively, the basis of the indu
tive algorithm
onstru
ts straightmove
ir
uits of three
2 × 8 boards. The indu
tion step then
ombines them by repla
ing horizontal moves by
the green verti
al moves shown in g. 9.4.
Exercise 9.2 As mentioned above, when a board has an odd number of squares, no
ir
uit is possible.
Consider a 3×3 board. It is easy to
onstru
t a straightmove
ir
uit of all its squares
but the middle square. (See g. 9.5.) It is also possible to
onstru
t a straightmove
ir
uit of all its squares but one of the
orner squares. However, a straightmove
ir
uit
of all but one of the other four squares the squares adja
ent to a
orner square, for
example, the middleleft square
annot be
onstru
ted.
Explore when it is possible, and when it is not possible, to
onstru
t a straightmove
ir
uit of all the squares but one in a board of odd size.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
9.2. Supersquares 141
Figure 9.5: Straightmove
ir
uits (shown in red) of a 3 × 3 board, omitting one of the
squares.
9.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 \su
persquares", as shown in g. 9.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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
142 9. Knight's Cir
uit
Knight from one supersquare to another along one of the diagonals through the starting
supersquare. In g. 9.7, the boundaries of the supersquares are indi
ated by thi
kened
lines; the starting position of the Knight is shown in bla
k, the straight moves are to the
blue positions, and the diagonal moves are to the red positions.
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
100
110
1 0
1
0
1
0
1 0
1
0
100
110
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
1 0 1010
0
1
Figure 9.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 9.8 shows
the straight moves from one supersquare the bottomleft supersquare verti
ally up
wards and horizontally rightwards. The
olours indi
ate the moves that
an be made.
For example, from the bottomleft red square a straight move
an be made to the topleft
red square or to the bottomright 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 redaboveyellow and blueabovegreen
olumns; the horizontal moves
inter
hange the rednexttoblue row with the yellownexttogreen row.)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
9.2. Supersquares
111
00000
11
00011
11100
143
000
111
000
11100
11
00
1100
11
000
111
00011
1110011
00
000
111
00011
1110011
00
000
111
00011
1110011
00
000
111
Figure 9.8: Straight moves from the bottomleft supersquare.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
144 9. Knight's Cir
uit
v;c;h
= { v;h = c }
v;v;h;h
= { v;v = h;h = n }
n;n
= { n ; x = x , with x := 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 im
pli
itly between the rst and se
ond steps. The use of an inx operator for \followed
by" fa
ilitates this allimportant
al
ulational te
hnique.)
Exercise 9.6 Constru
t a twodimensional 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} ,
(9.7) x;y = y;x .
(In prin
iple, you need to
onsider 43 , i.e. 64 , dierent
ombinations. Think of ways to
redu
e the amount of work.)
Exercise 9.9 Two other operations that
an be done on a 2 × 2 square are to rotate it
about the
entre through 90◦ , in one
ase
lo
kwise and in the other anti
lo
kwise. Let
r denote the
lo
kwise rotation and let a denote the anti
lo
kwise rotation. Constru
t
a table that shows the ee
t of performing any two of the operations n , r , a or c in
sequen
e.
Identify a
omplete set of operations on a 2 × 2 square and extend your solution to ex
er
ise 9.6 so that it is possible to determine the ee
t of
omposing any pair of operations.
(Avoid
onstru
ting the
omplete table be
ause it is quite large!)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
9.3. Partitioning the Board 145
111
000
000
11100
1100
11
000
111
000
111 00
1100
11
000
111
000
111
v 11
000
111
000
11100
c11
00
1100
000
111
000
111
00
11
000
111v 11
000
111 00
c11
00
1100
00
11
000
111
000
111
000
111
000
11100
11
00
1100
11
000
111
000
111
00
11
000
111
000
111 00
11
00
1100
11
00
11
000
111
00011
111
n 0011
h00
000
111
00011
111
n 0011
h00
000
111
000
11100
1100
11
000
111
000
111 00
1100
11
000
111
000
111
v 11
000
111
000
11100
c11
00
1100
000
111
000
111
00
11
000
111v 11
000
111 00
c11
00
1100
00
11
000
111
000
111
000
111
000
11100
11
00
1100
11
000
111
000
111
00
11
000
111
000
111 00
11
00
1100
11
00
11
000
111
00011
111
n 0011
h00
000
111
00011
111
n 0011
h00
000
111
00011
1110011
00
000
111
00011
111 0011
00Figure 9.9: Labelling Supersquares
Re all the dis ussion of straightmove ir uits in se tion 9.1. There we established
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
146 9. Knight's Cir
uit
Figure 9.10: S
hema for forming \redblue" and \greenyellow"
ir
uits. The straight
move
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 9.10 shows s
hemati
ally how redblue and greenyellow
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 9.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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
9.3. Partitioning the Board 147
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 \redgreen" or a \yellowblue"
ir
uit.
11
00
000
11100
1100
11
000
111
000
11100
11
000
111
00
11
00011
1110011
00
000
111
00011
11100
000
111
00
11
00011
1110011
00
000
111
00011
11100
000
111
00
11
000
111
00
11
000
11100
11
00
1100
11
000
111
000
111
00
11
000
111
000
11100
11
000
111
00
11
000
111
00
11
000
111
00
11
000
11100
11
00
1100
11
000
111
000
111
00
11
000
111
000
11100
11
000
111
00
11
000
111
00
11
00011
1110011
00
000
111
00011
11100
000
111
00
11
000
111
00
11
000
11100
11
00
1100
11
000
111
000
111
00
11
000
111
000
11100
11
000
111
00
11
000
111
00
11
00011
1110011
00
000
111
00011
11100
000
111
00
11
00011
1110011
00
000
111
00011
11100
000
111
Figure 9.11: Forming \redblue" and \greenyellow"
ir
uits. The straight moves shown
as dotted lines are repla
ed by the diagonal moves shown as solid lines.
Red and blue straightmove
ir
uits have been
ombined in g. 9.12 to form a \red
blue"
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 redblue
ir
uit as basis, a greenyellow
ir
uit has
to be
onstru
ted, and this
ir
uit
ombined with the redblue
ir
uit. This is left as an
exer
ise.
A slight diÆ
ulty of this method is that it
onstrains the straightmove
ir
uits that
an be made. For example,
onsidering the suggested method for
ombining red and blue
ir
uits in g. 9.11, no
onstraint is pla
ed on the blue
ir
uit (be
ause there is only
one way a straightmove
ir
uit of the blue squares
an enter and leave the bottomleft
orner of the board). However, the straightmove
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 straightmove
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
148 9. Knight's Cir
uit
Figure 9.12: A \redblue"
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.
Exercise 9.10 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 9.11 Figure 9.13 illustrates another way that the
ir
uits
an be
ombined.
The four straightmove
ir
uits are depi
ted as
ir
les, one segment of whi
h has been
attened and repla
ed by a dotted line. The dotted lines represent straight moves be
tween
onse
utive points. If these are repla
ed by diagonal moves (represented in the
diagram by solid bla
k lines), the result is a
ir
uit of the
omplete board.
To
arry out this plan, the four diagonal moves in g. 9.13 have to be identied.
The key to doing this with a minimum of eort is to seek parallel red and green moves,
and parallel blue and yellow moves, whilst exploiting symmetry. (In
ontrast, the above
solution involved seeking parallel red and blue moves.) Choosing to start from, say, the
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
9.3. Partitioning the Board 149
Figure 9.13: S
hema for Combining StraightMove Cir
uits. Four straight moves (in
di
ated by dotted lines) are repla
ed by four diagonal moves (indi
ated by solid bla
k
lines).
pair of green moves in the bottomleft
orner, severly restri
ts the
hoi
e of diagonal
moves; in
ombination with symmetry, this makes the appropriate moves easy to nd.
Constru
t a knight's
ir
uit of an 8×8 and a 6×8 board using the above
s
heme. Explain how to extend your
onstru
tion to any board of size 4m × 2n
for any m and n su
h that m≥2 and n≥3.
(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 straight
move
ir
uits. If you en
ounter diÆ
ulties, try turning the board through 90◦ whilst
maintaining the orientation of the
ombining moves.)
Exercise 9.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 straightmove
ir
uit is possible, and the strategy used in exer
ise
9.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
9.2.
The strategy is to
onstru
t four straightmove
ir
uits of the board omitting one of
the supersquares. (Re
all exer
ise 9.2 for how this is done.) Then, for ea
h
ir
uit, one
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
150 9. Knight's Cir
uit
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. 9.14.
11
00
00
11
000
111
000
111
Figure 9.14: Strategy for Constru
ting a Knight's Cir
uit of (4m + 2) × (4n + 2) boards.
One supersquare is
hosen, and four straightmove
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. 9.14 , it suÆ
es to
onstru
t just three; the remaining nine
an be found by
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.
2
9.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 problemsolving
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
9.4. Dis
ussion 151
skills. The method we have used to solve the problem is essentially problem de
ompo
sition  redu
ing the Knight's
ir
uit problem to
onstru
ting straightmove
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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
152 9. Knight's Cir
uit
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.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
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, 1234−1 players must be kno
ked
out. Hen
e, this number of games must be played.
In general, if there are p players, the tournament
onsists of p−1 games.
2
2.2 Let m , n and p denote the number of obje
ts of ea
h kind.
The repla
ement pro
ess is modelled by the assignment
m,n,p := m+1 , n−1 , p−1 .
Consider the dieren
es m−n , n−p , p−m . It is easily
he
ked that the parity of
ea
h is un
hanged by the assignment. (In ea
h
ase, the dieren
e either is un
hanged
or in
reases or de
reases by 2 .) Also, the number of odd dieren
es of any three numbers
is always even (i.e. either zero or two). Sin
e the goal is to rea
h a state in whi
h there
are two odd dieren
es, we
on
lude that the goal is impossible to rea
h if the starting
state has zero odd dieren
es. The goal is also impossible to rea
h if the obje
ts are all
of the same kind and there is more than one of them.
The algorithm to remove obje
ts maintains the invariant that all obje
ts are of the
same kind equivales there is only one obje
t remaining. If there is more than one obje
t
remaining, there must be two obje
ts of dierent kind. Choosing to in
rease the number
of obje
ts of the kind that o
urs least frequently will maintain this invariant, and redu
e
the number of obje
ts.
2
3.1
{ 5C  }
2C,3H 3W ; 2C,3H 1W 2W ; 5H 3W 2W
; { 5H  5W }
Algorithmi
Problem Solving
Roland Ba
khouse. 153 May 28, 2008
154 Solutions to Exer
ises
5H 2W 3W ; 2C 3H 3W
; { 2C  3C }
2C 1C 2C
; { 3C  2C }
3W 3H 2C ; 3W 2W 5H
; { 5W  5H }
2W 3W 5H ; 2W 1W 2C,3H ; 3W 2C,3H
{  5C } .
2
3.2 We modify the solution to the ve
ouple problem, ee
tively by leaving one
ouple
behind on the left bank. We get:
{ 4C  }
1C,3H 3W ; 1C,3H 1W 2W ; 4H 2W 2W
; { 4H  4W }
4H 2W 2W ; 2C 2H 2W
; { 2C  2C }
2C 1C 1C
; { 3C  1C }
3W 3H 1C ; 3W 1W 4H
; { 4W  4H }
2W 2W 4H ; 2W 1W 1C,3H ; 3W 1C,3H
{  4C } .
By reversing left and right, we get the se
ond solution:
{ 4C  }
1C,3H 3W ; 1C,3H 1W 2W ; 4H 2W 2W
; { 4H  4W }
4H 1W 3W ; 1C 3H 3W
; { 1C  3C }
1C 1C 2C
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 155
; { 2C  2C }
2W 2H 2C ; 2W 2W 4H
; { 4W  4H }
2W 2W 4H ; 2W 1W 1C,3H ; 3W 1C,3H
{  4C } .
2
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 N−lH . Similarly, let lW denote the number
of wives on the left bank. The number of wives on the right bank, denoted rW , is then
N−lW .
We note that an invariant is
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
(14) M < lH ,
or
(15) M ≤ lH .
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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 157
a return trip. Clearly, an optimal solution is found when persons 3 and 4 the two
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 imple
mented 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
t1↑t2 + t1 + t3↑t4 + t2 + t1↑t2 .
(This
orresponds to person 1 and person 2
rossing, then person 1 returning, then
person 1 and person 4
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.)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
158 Solutions to Exer
ises
2
3.5 6×2×3×3×1 , i.e. 108 .
2
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 oddnumbered days and the winning posi
tions are the evennumbered 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 lastnamed day is an oddnumbered day, the player whose turn
it is must name an evennumbered 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 oddnumbered days are losing
positions, and any day in September is a winning position. Similarly, in August, the
oddnumbered 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 evennumbered day in June is a losing day.
This means that every evennumbered day in May is a winning day; ; also, every even
numbered 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 oddnumbered day in Mar
h is a losing day, and every day in February is
a winning day. Finally, the oddnumbered 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 lastnamed 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 leapyear.
2
4.2 The rst eleven positions are shown in table 1.
The pattern repeats in the se
ond eleven positions. See table 2.
2
4.3 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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 159
Position 0 1 2 3 4 5 6 7 8 9 10
Type L L W W L W W W L W W
Move 2 2 5 6 6 5 6
Table 1: Winning (W) and Losing (L) Positions for subtra
tion set {2 , 5 , 6}
Position 11 12 13 14 15 16 17 18 19 20 21
Type L L W W L W W W L W W
Move 2 2 5 6 6 5 6
Table 2: Winning (W) and Losing (L) positions for subtra tion set {2 , 5 , 6}
Table 3: Snakes and Ladders. Winning (W), Losing (L) and Stalemate (S) positions
2
4.4 a) See table 4 for the mex numbers up to and in
luding position 10. The mex
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
160 Solutions to Exer
ises
numbers repeat from here on; that is, the mex number for position m is equal to the
mex number for position m mod 11 .
Position 0 1 2 3 4 5 6 7 8 9 10
Type L L W W L W W W L W W
Mex Number 0 0 1 1 0 2 1 3 0 2 1
In the left game, the mex number of position m is m mod 3 . Together with the mex
numbers for the right game given above, we
an
omplete table 5. (Other answers
an
be given for the winning moves.)
2
4.5 (a) The losing positions are positions 2i+1−1 where i is a natural number; all other
positions are winning positions.
The proof is in two parts: we show that, for all i , every move from position 2i+1−1
is to a position n where 2i−1 < n < 2i+1−1 ; also, from a position n where, for all i ,
n 6= 2i+1−1 we show that we
an
hoose i so that there is a move from n to position
2i−1 .
When i equals 0 , 2i+1−1 equals 1 . Position 1 is an end position and thus a losing
position. When i is greater than 0 , every move from position 2i+1−1 is to a position
n where n < 2i+1−1 ≤ 2×n . But
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 161
(b)
Position: 1
Mex Number: 0
Position: 2 3
Mex Number: 1 0
Position: 4 5 6 7
Mex Number: 2 1 3 0
Position: 8 9 10 11 12 13 14 15
Mex Number: 4 2 5 1 6 3 7 0
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
162 Solutions to Exer
ises
No. of Columns No. of Rows \losing" or winning move
2 15 C1 (or R11)
4 11 C2 (or R9)
4 14 R9
13 6 losing
21 19 C19 (or R10)
2
5.5 Let col be the
olour of the square, and n be the number of moves. A move is
then
col , n := ¬col , n+1 .
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.
2
5.6 Suppose the number of
ouples is n . There are 2n people, in
luding the host, who
ea
h shake hands with between 0 and 2n − 2 people. If 2n − 1 of them everyone but
the host 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 − 2 (in
lusive).
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
0 and 2n − 2 times. The person who shakes hands 2n − 2 times does so with everyone
ex
ept their partner (and themself, of
ourse). By the symmetry of the shakehands
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 − 2 times
are husband and wife. Be
ause neither is the host, it also follows that neither is the
host's partner.
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 163
Now suppose we dis ount this ouple. That is, we onsider the party onsisting of
the other n−1 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
2
5.8
(a) false
(b) false
(c) false
(d) p
(e) false
(f) q 6≡ r
(g) p
(h) true
2
5.9
¬true
= { law ¬p ≡ p ≡ false with p := true }
true ≡ false
= { law true ≡ p ≡ p with p := false }
false .
2
5.10
¬¬p
= { law ¬p ≡ p ≡ false with p := ¬p }
2
5.11 The pro
ess of de
ryption after en
ryption
omputes a 6≡ (a 6≡ b) . But,
a 6≡ (a 6≡ b)
= { 6≡ is asso
iative }
(a 6≡ a) 6≡ b
= { ( a 6≡ a ≡ false ) }
false 6≡ b
= { denition of 6≡ }
false ≡ ¬b
= { denition of negation: (5.3) }
b .
2
5.12 Let Q be the question. Then, Q ≡ A ≡ A 6≡ B , i.e. Q ≡ ¬B . In words, ask A
whether B is a knave.
2
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.
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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 165
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.
2
6.5 When m is 0 , there is just one obje
t. This is the unique obje
t and 0 (whi
h
equals 2×0 )
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
3m−1 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×(m−1)
omparisons are required
to nd the unique obje
t in that group. This gives a total of 2×(m−1) + 2 , i.e. 2×m ,
omparisons as required by the indu
tion hypothesis.
It is possible to determine whether the unique obje
t is lighter or heavier than the
others (although, in the
ase that there is just one obje
t, the answer is that it is both
lighter and heavier than all the rest). It
an be de
ided in the rst two
omparisons.
2
6.6 a) For n = 1 , it is
lear that 0
omparisons are needed. For the indu
tion step,
assume that n−1
omparisons are needed to nd the lightest of n obje
ts. To nd the
lightest of n+1 obje
ts, use n−1
omparisons to nd the lightest of n obje
ts, then
ompare this obje
t with the ( n+1 )th obje
t. The lightest of the two is the lightest of
them all. Also, one extra
omparison has been made, making n in total.
b) For n = 2 , it is
lear that 1
omparison is needed. For the indu
tion step, assume that
2n − 3
omparisons are needed to nd the lightest and heaviest of n obje
ts. To nd the
lightest and heaviest of n+1 obje
ts, use 2n − 3
omparisons to nd the lightest and
heaviest of n obje
ts. Call these L and H . Call the ( n+1 )th obje
t N . The lightest
of L and N is the lightest of them all, and the heaviest of H and N is the heaviest of
them all. This requires two extra
omparisons, making (2n − 3) + 2 , i.e. 2(n+1) − 3 in
total.
) Compare A and C . The lightest of the two is the lightest of the four. Compare B
and D . The heaviest of the two is the heaviest of the four.
To weigh four obje
ts, rst
ompare two. Call the lighter one A and the heavier
one B . Likewise,
ompare the remaining two obje
ts and
all the lighter one C and the
heavier one D . Then pro
eed as above.
d) For m = 1 , it is
lear that 1
omparison is needed to nd the lightest and heaviest
of 2 obje
ts. And, 1 = 3×1 − 2 .
Suppose there are 2(m+1) obje
ts. Sele
t and
ompare any two of the obje
ts.
Let the lightest be A and the heaviest B . By indu
tion, we
an nd the lightest and
heaviest of the remaining 2m obje
ts in 3m − 2
omparisons. Let these be C and
D , respe
tively. We now have four obje
ts, A , B , C and D , su
h that A < B and
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
166 Solutions to Exer
ises
C < D . By part (
), the lightest and heaviest of these four
an be found in 2 further
omparisons. These are then the lightest and heaviest of all 2(m+1) obje
ts. And, the
total number of
omparisons is 1 + (3m − 2) + 2 whi
h equals 3(m+1) − 2 .
2
7.1 Formally we have
To.d
= { 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 }
length(Hn.¬d ; [hn+1 , ¬di] ; Hn.¬d)
= { denition of length }
length(Hn.¬d) + length([hn+1 , ¬di]) + length(Hn.¬d)
= { denition of T (twi
e) and 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 2n−1 . The simple indu
tive proof is omitted.
2
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 167
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 statetransition diagram for the ( n+1 )disk
problem, for an arbitrary n , given that we have
onstru
ted the diagram for the n disk
problem. (See g. 16.) Ea
h state is a sequen
e of n+1 pole names. The rst n names
spe
ify the lo
ation of the smallest n disks and the ( n+1 )th spe
ies the lo
ation of
the largest disk. Thus, ea
h state in the statetransition diagram for the n disk problem
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
168 Solutions to Exer
ises
gives rise to 3 states in the statetransition diagram for the ( n+1 )disk problem. That
is, a state in the statetransition diagram for the ( n+1 )disk problem is spe
ied by
a sequen
e of n pole numbers followed by the pole name A , B or C . We split the
permissible moves into two sets: those where the largest disk (the disk numbered n+1 )
is moved and those where a disk other than the largest disk is moved.
Consider rst moving a disk other than the largest disk. When doing so, the largest
disk may be on pole A , B or C . But its position doesn't ae
t the permissibility or
otherwise of a move of a smaller disk. That means that every transition from state s to
state t in the n disk problem is also a valid transition from state sp to state tp in the
( n+1 )disk problem, where the pole name p is either A , B or C . The rst step in
the
onstru
tion of the statetransition diagram for the ( n+1 )disk problem given the
statetransition diagram for the n disk problem is to make three
opies of the latter.
The p th
opy is then modied by simply adding p at the end of ea
h sequen
e of pole
numbers labelling the nodes.
Now
onsider moving the largest disk, the disk numbered n+1 . Being the largest
disk it may only be moved if all the other disks are on one and the same pole dierent
to the pole that the largest disk is on. This gives six possibilities for moving disk n+1 ,
or three edges in the undire
ted statetransition diagram: an edge
onne
ting the states
AnB and AnC , an edge
onne
ting the states BnC and BnA and an edge
onne
ting
the states CnA and CnB . The
onstru
tion is shown s
hemati
ally in g. 16, the three
inner triangles representing the set of all moves that do not move disk n+1 .
2
7.3 Even, be
ause the dire
tion of movement is opposite to that of the smallest disk
(whi
h has an odd number).
2
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 k−1 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 k−1 smallest disks in the dire
tion ¬d ′ . The dire
tion that disk 1 has to
be moved is thus d where
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 169
AnA
`
A
AA
` `
` `
` `
AA
n
B A` ` ` ` A` CnA
A
A
BnC ` A ` CnB
A A
AA
AA
` ` ` `
` ` ` `
` ` ` `
AA AA
` ` ` ` A` ` ` ` ` A`
Figure 16: Constru tion of the statetransition diagram for the (n + 1) disk problem
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 k−1 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.
2
7.5 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 k−1 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.
2
9.1
(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 straightmove
ir
uit of a 2×1 board is possible starting
at one of the squares, move to the other square and then move ba
k again , but,
otherwise, no straightmove
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 inbetween square more than
on
e.
(c) See (b) for a
ir
uit of a 2×1 board. For n greater than 1 , a straightmove
ir
uit
of a 2×n board is
ompleted by starting at a
orner, moving onebyone to all the
squares in the same row, then returning via the se
ond row.
2
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 171
9.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 tobeomitted 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 .
odd even
odd
even even
Figure 17: Straightmove
ir
uits (shown in red) of a 3 × 3 board, omitting one of the
squares.
2
9.6
; n v h c
n n v h c
v v n c h
h h c n v
c c h v n
Property (9.7) is veried by observing that the table is symmetri
about the topleft
to bottomright 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
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
172 Solutions to Exer
ises
onsider. This is an example of a \tedious, but straightforward" proof!
2
9.9
; n r a c
n n r a c
r r c n a
a a n c r
c c a r n
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 173
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.
extending straightmove
ir
uits to boards of arbitrary size. This
onstru
tion has to
be applied four times, on
e for ea
h of the straightmove
ir
uits in the solution to the
8 × 6 board problem.
2
9.12 We begin by identifying the moves shown in g. 9.14. See g. 21. (Note the
symmetry.)
Now it is easy to ll in the straightmove
ir
uits around the remaining squares. See
g. 22.
For the general problem, it is easy to extend the straightmove
ir
uits.
2
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
174 Solutions to Exer
ises
Figure 19: Details of how the four straightmove
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.)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
Solutions to Exer
ises 175
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.)
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008
176 Solutions to Exer
ises
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
Figure 22: Knight's Cir uit of a 6×6 board. The dotted lines do not form part of the
ir uit.
[Ba
03℄ Roland Ba
khouse. Program Constru
tion. Cal
ulating Implementations
From Spe
i
ations. John Wiley Sons, Ltd., 2003.
[Ba
07℄ Roland Ba
khouse. The tor
h problem. Available at
[BCG82℄ Elwyn R. Berlekamp, John H. Conway, and Ri
hard K. Guy. Winning Ways,
volume I and II. A
ademi
Press, 1982.
[BF01℄ Roland Ba khouse and Maarten Fokkinga. The asso iativity of equivalen e
and the Towers of Hanoi Problem. Information Pro essing Letters, 77:71{
76, 2001.
[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.
http://www.cs.utexas.edu/users/EWD/ewd10xx/EWD1083.PDF, Septem
ber 1990.
http://www.cs.utexas.edu/users/EWD/ewd11xx/EWD1135.PDF, Septem
ber 1992.
[Dij97℄ Edsger W. Dijkstra. EWD1260: The marked oins and the s ale.
[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. SpringerVerlag,
1990.
[Wil87℄ J.G. Wiltink. A de ien y of natural dedu tion. Information Pro essing
Algorithmi
Problem Solving
Roland Ba
khouse. May 28, 2008