Você está na página 1de 160

Algorithmi

Problem Solving

Roland Ba khouse

January 7, 2007

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Contents
1 Introduction

1.1 Algorithms . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
1.2 Bibliographi Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .
2 Invariants

1
3
5

2.1 Cho olate Bars . . . . . . . . . . .


2.1.1 The Solution . . . . . . . .
2.1.2 The Mathemati al Solution
2.2 Empty Boxes . . . . . . . . . . . .
2.3 The Tumbler Problem . . . . . . .
2.4 Tetrominoes . . . . . . . . . . . . .
2.5 Additional Exer ises . . . . . . . .
2.6 Bibliographi Remarks . . . . . . .

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.

3 Crossing a River

8
8
9
12
13
15
19
19
21

3.1 Problems . . . . . . . . . . . . . . . . .
3.2 Brute For e . . . . . . . . . . . . . . . .
3.2.1 Goat, Cabbage and Wolf . . . . .
3.2.2 State-Spa e Explosion . . . . . .
3.2.3 Abstra tion . . . . . . . . . . . .
3.3 Jealous Couples . . . . . . . . . . . . . .
3.3.1 What's The Problem? . . . . . .
3.3.2 Problem Stru ture . . . . . . . .
3.3.3 Denoting States and Transitions
3.3.4 Problem De omposition . . . . .
3.3.5 A Review . . . . . . . . . . . . .
3.4 Rule of Sequential Composition . . . . .
3.5 Summary . . . . . . . . . . . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.

Algorithmi Problem Solving Roland Ba khouse.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
i

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.

22
22
22
25
26
27
28
28
29
31
34
35
38

January 7, 2007

ii
4 Games

4.1 Mat hsti k Games . . . . . . . . .


4.2 Winning Strategies . . . . . . . . .
4.2.1 Assumptions . . . . . . . .
4.2.2 Labelling Positions . . . . .
4.2.3 Formulating Requirements .
4.3 Subtra tion-Set Games . . . . . . .
4.4 Sums of Games . . . . . . . . . . .
4.4.1 Symmetry . . . . . . . . . .
4.4.2 Keep It Symmetri al! . . .
4.4.3 More Simple Sums . . . . .
4.4.4 The MEX Fun tion . . . .
4.4.5 Using the MEX Fun tion .
4.5 Summary . . . . . . . . . . . . . .
4.6 Bibliographi Remarks . . . . . . .

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

5 Knights and Knaves

5.1 Logi Puzzles . . . . . . . . . . . . . . . . . . . . . .


5.2 Cal ulational Logi . . . . . . . . . . . . . . . . . . .
5.2.1 Propositions . . . . . . . . . . . . . . . . . .
5.2.2 Knights and Knaves . . . . . . . . . . . . . .
5.2.3 Boolean Equality . . . . . . . . . . . . . . . .
5.2.4 Hidden Treasures . . . . . . . . . . . . . . . .
5.2.5 Equals for Equals . . . . . . . . . . . . . . . .
5.3 Equivalen e and Continued Equalities . . . . . . . .
5.3.1 Examples of the Asso iativity of Equivalen e
5.3.2 On Natural Language . . . . . . . . . . . . .
5.4 Negation . . . . . . . . . . . . . . . . . . . . . . . . .
5.5 Contraposition . . . . . . . . . . . . . . . . . . . . .
5.6 Handshake Problems . . . . . . . . . . . . . . . . . .
5.7 Inequivalen e . . . . . . . . . . . . . . . . . . . . . .
5.8 Summary . . . . . . . . . . . . . . . . . . . . . . . .

6 Induction

6.1
6.2
6.3
6.4
6.5

Example Problems . .
Cutting The Plane . .
Triominoes . . . . . .
Looking For Patterns .
The Need For Proof .

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

Algorithmi Problem Solving Roland Ba khouse.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

41

41
43
43
44
45
48
52
53
55
56
57
59
62
62

63

63
64
64
65
66
68
69
70
72
73
74
76
79
80
83

85

85
89
92
93
95

January 7, 2007

iii
6.6 From Veri ation to Constru tion
6.7 Fake-Coin Dete tion . . . . . . . .
6.7.1 Problem Formulation . . .
6.7.2 Problem Solution . . . . . .
6.8 Summary . . . . . . . . . . . . . .
6.9 Bibliographi Notes . . . . . . . .
7 The Towers of Hanoi

7.1 Spe i ation and Solution . . .


7.1.1 The End of the World! .
7.1.2 Iterative Solution . . . .
7.1.3 WHY? . . . . . . . . . .
7.2 Indu tive Solution . . . . . . .
7.3 The Iterative Solution . . . . .
7.4 Summary . . . . . . . . . . . .
7.5 Bibliographi Remarks . . . . .

8 Knights Circuit

8.1
8.2
8.3
8.4
8.5
8.6

Straight-Move Cir uits


Supersquares . . . . .
Partitioning the Board
Dis ussion . . . . . . .
Boards of Other Sizes
Bibliographi Remarks

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.
.

95
99
100
101
104
105
107

107
107
108
109
109
113
115
116

117

117
121
125
130
132
132

Solutions to Exercises

133

Bibliography

153

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

iv

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Chapter 1
Introduction
In histori al terms, the digital omputer is very, very new. The s ien e of omputing is
yet newer. Compared to its older sister |mathemati s| whi h is thousands of years
old, it is hardly in the embryoni stage of development. Yet, omputing s ien e is already
having a major in uen e on our problem-solving skills, amounting to a revolution in the
art of e e tive reasoning.
Be ause of the hallenges of programming (whi h means instru ting a dumb ma hine
how to solve ea h instan e of a problem) and the unpre edented s ale of programming
problems, omputing s ientists have had to hone their problem-solving skills to a very
ne degree. This has led to advan es in logi , and to hanges in the way that mathemati s
is pra tised. These le tures form an introdu tion to problem-solving using the insights
that have been gained in omputing s ien e.

1.1

Algorithms

Solutions to programming problems are formulated as so- alled algorithms. An algorithm is a well-de ned 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 di erent 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.

January 7, 2007

1. Introdu tion

the bridge, so that it is always arried when the bridge is rossed.


Show that it takes 17 minutes for all four to ross the bridge.
The solution to this problem |whi h we won't dis lose just yet!| is learly a sequen e of instru tions about how to get all four people a ross the bridge. A typi al instru 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 input-output 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 .
Formulating an algorithm makes problem-solving de idedly harder, be ause it is ne essary to formulate very learly and pre isely the pro edure for solving the problem. The
more general the problem, the harder it gets. (For instan e, the bridge- rossing problem
an be generalised by allowing the number of people to be variable.) The advantage,
however, is a mu h greater understanding of the solution. The pro ess of formulating an
algorithm demands a full understanding of why the algorithm is orre t.
The key to e e tive problem-solving is e onomy of thought and of expression | the
avoidan e of unne essary detail and omplexity. The mastery of omplexity is espe ially
important in omputing s ien e be ause of the unpre edented size of omputer programs
| a typi al omputer program will have hundreds, thousands or even millions lines of
ode. Coupled with the unforgiving nature of digital omputers |whereby a single error
an ause an entire system to abruptly \ rash"'| it is perhaps not so surprising that
the hallenges of algorithm design have had an immense impa t on our problem-solving
skills.
This book aims to impart these new skills and insights to a broad audien e, using
an example-driven approa h. It aims to demonstrate the importan e of mathemati al
al ulation, but the hosen examples are typi ally not mathemati al; instead, like the
bridge- rossing problem above, they are problems that are readily understood by a lay
person, with only elementary mathemati al knowledge. The book also aims to hallenge;
most of the problems are quite di ult, at least to the untrained or poorly trained
pra titioner.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

1.2. Bibliographi Remarks

1.2

Bibliographic Remarks

The bridge problem is from [Lev03.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

1. Introdu tion

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Chapter 2
Invariants
\Invariant" means \not hanging". An invariant of some pro ess is thus some attribute or
property of the pro ess that doesn't hange. Other names for \invariant" are \ onstant",
\law" and \pattern".
The re ognition of invariants is an important problem-solving skill, possibly the most
important. This hapter introdu es the notion of an invariant, and dis usses a number
of examples of its use.
We begin as we mean to go on. We rst present a number of problems for you to
ta kle. Some you may nd easy, but others you may nd di ult or even impossible to
solve. If you an't solve one, move on to the next. To gain full bene t, 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.

January 7, 2007

2. Invariants

Figure 2.1 shows a 43 ho olate bar that has been ut into ve pie es. The uts
are indi ated by solid lines.

Figure 2.1: Cho olate-Bar Problem.


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.

Figure 2.2: Tumbler Problem.


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.

January 7, 2007

7
to redu e the number of balls in the urn to one by repeating the following pro ess
as often as ne essary.
Take any two balls out of the urn. If both have the same olour, throw them away,
but put another bla k ball into the urn; if they have di erent olours then return
the white one to the urn and throw the bla k one away.
Ea h exe ution of the above pro ess redu es the number of balls in the urn by one;
when only one ball is left the game is over. What, if anything, an be said about
the olour of the nal ball in the urn in relation to the original number of bla k
balls and white balls?
5. Dominoes
A hess board has had its top-right and bottom-left squares removed so that there
are 62 squares remaining. (See g. 2.3.) An unlimited supply of dominoes has

Figure 2.3: Mutilated Chess Board


been provided; ea h domino will over exa tly two squares of the hessboard. Is
it possible to over all 62 squares of the hessboard with the dominoes without
any domino overlapping another domino or sti king out beyond the edges of the
board?
6. Tetrominoes
A tetromino is a gure made from 4 squares of the same size. There are ve
di erent tetrominoes, alled the O-, Z-, L-, T- and I-tetrominoes. (See g. 2.4.)
The following exer ises all on ern overing a re tangular board with tetrominoes.
Assume that the board is made up of squares of the same size as the ones used
to make the tetrominoes. Overlapping tetrominoes or tetrominoes that sti k out
from the sides of the board are not allowed.
(a) Suppose a re tangular board is overed with tetrominoes. Show that at least
one side of the re tangle has an even number of squares.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

2. Invariants

Figure 2.4: O-, Z-, L-, T- and I-tetromino


(b) Suppose a re tangular board an be overed with T-tetrominoes. Show that
the number of squares is a multiple of 8 .
( ) Suppose a re tangular board an be overed with L-tetrominoes. Show that
the number of squares is a multiple of 8 .
(d) An 88 board annot be overed with one O-tetromino and fteen L-tetrominoes.
Why not?

2.1

Chocolate Bars

Re all the problem statement:


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.)
How many uts in total are needed to ompletely ut the ho olate into
all its pie es?

2.1.1

The Solution

The solution to the ho olate-bar problem is as follows. Whenever a ut is made, the


number of uts in reases by one, and the number of pie es in reases by one. Thus, the
number of uts and the number of pie es both hange. What doesn't hange, however,
is the di eren e between the number of uts and the number of pie es. This is an
\invariant", or a \ onstant", of the pro ess of utting the ho olate bar.
Now, we begin with one pie e and zero uts. So, the di eren e between the number
of pie es and the number of uts, at the outset, is one. It being a onstant means that
it will always be one, no matter how many uts have been made. That is, the number
of pie es will always be one more than the number of uts. Equivalently, the number of
uts will always be one less than the number of pie es.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

2.1. Cho olate Bars

We on lude that to ut the ho olate bar into all its individual pie es, the number
of uts needed is one less than the number of pie es.

2.1.2

The Mathematical Solution

On e the skill of identifying invariants has been mastered, this is an easy problem to solve.
For this reason, we have used English to des ribe the solution, rather than formulate the
solution in a mathemati al notation. For more omplex problems, mathemati al notation
helps onsiderably, be ause it is more su in t and more pre ise. Let us use this problem
to illustrate what we mean.
Abstraction The mathemati al solution begins by introdu ing two variables. We let
variable p ount the number of pie es, and we let variable c ount the number of uts.

The values of these variables des ribe the state of the ho olate bar.
This rst step is alled abstra tion. We \abstra t" from the problem a olle tion
of variables (or \parameters") that ompletely hara terise the essential elements of the
problem. In this step, inessential details are eliminated.
One of the inessential details is that the problem has anything to do with ho olate
bars! This is totally irrelevant and, a ordingly, has been eliminated. The problem
ould equally well have been about utting postage stamps from a sheet of stamps.
The problem has be ome a \mathemati al" problem, be ause it is about properties of
numbers, rather than a \real-world" problem. Real-world problems are very hard, if not
impossible, to solve; in ontrast, problems that su umb to mathemati al analysis are
relatively easy.
Other inessential details that have been eliminated are the sequen e of uts that have
been made, and the shapes and sizes of the resulting pie es. That is, the variables p
and c do not ompletely hara terise the state of the ho olate bar, or the sequen e of
uts that have been made to rea h that state. Knowing that, say, four uts have been
made, making ve pie es, does not allow us to re onstru t the sizes of the individual
pie es. That is irrelevant to solving the problem.
The abstra tion step is often the hardest step to make. It is very easy to fall into
the trap of in luding unne essary detail, making the problem and its solution over ompli ated. Conversely, de iding what is essential is far from easy |there is no algorithm for doing this!| . The best problem-solvers are probably the ones most skilled in
abstra tion.
(Texts on problem-solving often advise drawing a gure. This may help to larify
the problem statement |for example, we in luded g. 2.1 in order to larify what is
meant by a ut| but it an also be a handi ap! There are two reasons. The rst is that
extreme ases are often di ult to apture in a gure. This is something we return to

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

10

2. Invariants

later. The se ond is that gures often ontain mu h unne essary detail, as exempli ed
by g. 2.1. Our advi e is to use gures with the utmost aution; mathemati al formulae
are most often far more e e tive.)
The next step in the problem's solution is to model the pro ess of utting
the ho olate bar. We do so by means of the assignment statement
Assignments

p , c := p+1 , c+1 .

An assignment statement has two sides, a left side and a right side. The two sides are
separated by the assignment symbol \ := ", pronoun ed \be omes". The left side is a
omma-separated list of variables (in this ase, p , c ). No variable may o ur more than
on e in the left side. The right side is a omma-separated list of expressions (in this ase,
p+1 , c+1 ). The list must have length equal to the number of variables on the left side.
An assignment e e ts a hange of state. An assignment is exe uted by evaluating,
in the urrent state, ea h expression on the right side. The state is then hanged by
repla ing the value of ea h variable on the left side by the value of the orresponding
expression on the right side. In our example, the state |the number of pie es and
the number of uts| is hanged by evaluating p+1 and c+1 , and then repla ing the
values of p and c by these values, respe tively. In words, p \be omes" p+1 , and c
\be omes" c+1 . This is how the assignment statement models the pro ess of making a
single ut of the ho olate bar1 .
An invariant of an assignment is some fun tion of the state whose value remains
onstant under exe ution of the assignment. For example, pc is an invariant of the
assignment p , c := p+1 , c+1 .
Suppose E is an expression depending on the values of the state variables. (For
example, pc is an expression depending on variables p and c .) We an he k that E
is an invariant simply by he king for equality between the value of E , and the value of
E after repla ing all variables as pres ribed by the assignment. For example, the equality
1A

word of warning (for those who have already learnt to program in a language like Java or C):
The assignment statements we will be using are often alled simultaneous assignments be ause several
variables are allowed on the left side, their values being updated simultaneously on e the right side has
been evaluated. Some programming languages do not allow simultaneous assignments, restri ting the
programmer to a single variable on the left side in all assignments. Java is an example. Instead of a
simultanous assignment, one has to write a sequen e of assignments. This is a nuisan e, but only that.
Mu h worse is that the equality symbol, \ = ", is used instead of the assignment symbol, Java being again
an example. This is a major problem be ause it auses onfusion between assignments and equalities,
whi h are two quite di erent things. Most novi e programmers frequently make the mistake of onfusing
the two, and even experien ed programmers sometimes do, leading to di ult-to- nd errors. If you do
write Java or C programs, always remember to pronoun e an assignment as \left side becomes right side",
and not \left side equals right side", even if your tea hers do not do so.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

11

2.1. Cho olate Bars


pc = (p+1) (c+1) ,

holds whatever the values of p and c . This he ks that pc 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 , n1

We he k that m + 3n is invariant by he king that


m + 3n = (m+3) + 3(n1) .

Simple algebra shows that this holds. So, in reasing m by 3 , simulaneously de reasing
n by 1 , does not hange the value of m + 3n .
Given an expression, E , and an assignment, ls := rs ,
E[ls := rs]

is used to denote the expression obtained by repla ing all o urren es of the variables in
E listed in ls by the orresponding expression in the list of expressions rs . Here are
some examples:
(pc)[p , c := p+1 , c+1] = (p+1) (c+1)

(m + 3n)[m , n := m+3 , n1] = (m+3) + 3(n1)

(m+n+p)[m , n , p := 3n , m+3 , n1] = (3n) + (m+3) + (n1)

The invariant rule for assignments is then the following.


E is an invariant of the assignment ls := rs if, for all instan es of the variables in E ,

E[ls := rs] = E .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

12

2. Invariants

Induction The nal step in the solution of the ho olate problem is to exploit the
invarian e of pc .
Initially, p = 1 and c = 0 . So, initially, pc = 1 . But, pc is invariant. So, pc = 1

no matter how many uts have been made. When the bar has been ut into all its squares,
p = s , where s is the number of squares. So, at that time, the number of uts, c , satis es
sc = 1 . That is, c = s1 . The number of uts is one less than the number of squares.
An important prin iple is being used here, alled the prin iple of mathemati al
indu tion. The prin iple is very simple. It is that, if the value of an expression is un hanged by some assignment to its variables, the value will be un hanged no matter how
many times the assignment is applied. That is, if the assignment is applied zero times,
the value of the expression is un hanged (obviously, be ause applying the assignment
zero times means doing nothing). If the assignment is applied exa tly on e, the value
of the expression is un hanged, by assumption. Applying the assignment twi e means
applying it on e and then on e again. Both times, the value of the expression remains
un hanged, so the end result is also no hange. And so on, for three times, four times,
et .
Note that the ase of zero times is in luded here. It is very important not to forget
zero. In the ase of the ho olate-bar problem, it is vital to solving the problem in the
ase that the ho olate bar has exa tly one square (in whi h ase zero uts are required).
This ompletes our dis ussion of the ho olate-bar problem. A number of
important problem-solving prin iples have been introdu ed | abstra tion, invariants
and indu tion. We will see these prin iples again and again throughout these le tures.
Summary

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 before the tournament winner is de ided? (Hint: hoose suitable variables, and seek an
invariant.)
Exercise 2.1

2.2

Empty Boxes

Try ta kling the empty-box problem. Re all its statement.


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

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

13

2.3. The Tumbler Problem

unknown number of the medium boxes is sele ted and into ea h eight small
boxes are pla ed.
At the end of this pro ess there are 102 empty boxes. How many boxes
are there in total?
The following steps should help in determining the solution.
1. Introdu e the variables e and f for the number of empty and the number of full
boxes, respe tively.
2. Identify the initial values of e and f . Identify the nal value of e .
3. Model the pro ess of putting eight boxes inside a box as an assignment to e and
f.
4. Identify an invariant of the assignment.
5. Combine the previous steps to dedu e the nal value of f . Hen e dedu e the nal
value of e+f .
Note that this solution does not try to ount the number of medium boxes, or the
number of small boxes, or whi h are full and whi h are empty. All of these are irrelevant,
and a solution that introdu es variables representing these quantities is grossly over ompli ated.
This is a key to e e tive problem-solving: keep it simple!

2.3

The Tumbler Problem

Let us now look at how to solve the tumbler problem. Re all the statement of the
problem.
Several tumblers are pla ed in a line on a table. Some tumblers are upside
down, some are the right way up. It is required to turn all the tumblers the
right way up. However, the tumblers may not be turned individually; an
allowed move is to turn any two tumblers simultaneously. From whi h initial
states of the tumblers is it possible to turn all the tumblers the right way up?
The problem suggests that we introdu e just one variable that ounts the number of
tumblers that are upside down. Let us all it u .
There are three possible e e ts of turning two of the tumblers. Two tumblers that
are both the right way up are turned upside down. This is modelled by the assignment

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

14

2. Invariants
u := u+2 .

Turning two tumblers that are both upside down has the opposite e e t | u de reases
by two. This is modelled by the assignment
u := u2 .

Finally, turning two tumblers that are the opposite way up (that is, one upside down,
the other the right way up) has no e e t on u . In programming terms, this is modelled
by a so- alled skip statement. \Skip" means \do nothing" or \having no e e 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 i ed. An invariant
of the turning pro ess must therefore be an invariant of ea h of the three.
Everything is an invariant of skip . So, we an dis ount skip . We therefore seek an
invariant of the two assignments u := u+2 and u := u2 . What does not hange
if we add or subtra t two from u ?
The answer is: the so- alled parity of u . The parity of u is a boolean value: it is
either true or false . It is true if u is even (zero, two, four, eight et .) and it is false if
u is odd (one, three, ve, seven, et .). Let us write even.u for this Boolean quantity.
Then,
(even.u)[u := u+2] = even.(u+2) = even.u .

That is, even.u is an invariant of the assignment u := u+2 . Also,


(even.u)[u := u2] = even.(u2) = even.u .

That is, even.u is also an invariant of the assignment u := u2 .


We on lude that, no matter how many times we turn two tumblers over, the parity
of the number of upside-down tumblers will not hange. If there is an even number at
the outset, there will always be an even number; if there is an odd number at the outset,
there will always be an odd number.
The goal is to repeat the turning pro ess until there are zero upside-down tumblers.
Zero is an even number, so the answer to the question is that there must be an even
number of upside-down tumblers at the outset.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

15

2.4. Tetrominoes

You should now be in a position to solve the problem of the bla k and white balls
(problem 4 in the introdu tory se tion). Apply the method of introdu ing appropriate
variables to des ribe the state of the balls in the urn. Then express the pro ess of
removing and/or repla ing balls by a hoi e among a number of assignment statements.
Identify an invariant, and draw the appropriate on lusion. The hessboard problem is a
little harder, but an be solved in the same way. (Hint: use the olouring of the squares
on the hessboard.) Problem 6(a) should be a bit easier. It's a preliminary to solving
6(b), whi h we do |together with 6(a)| in the next se tion. Have a peek if you want
to.

2.4

Tetrominoes

In this se tion, we present the solution of problem 6(b). This gives us the opportunity
to introdu e a style of mathemati al al ulation that improves larity.
Re all the problem.
Suppose a re tangular board an be overed with T-tetrominoes. Show that
the number of squares is a multiple of 8 .
A brief analysis of this problem reveals an obvious invariant. Suppose c denotes the
number of overed squares. Then, pla ing a tetromino on the board is modelled by
c := c+4 .

Thus, c mod 4 is invariant. ( c mod 4 is the remainder after dividing c by 4 . For


example, 7 mod 4 is 3 , and 16 mod 4 is 0 .) Initially c is 0 , so c mod 4 is 0 mod 4 ,
whi h is 0 . So, c mod 4 is always 0 . In words, we say \ c is a multiple of 4 is an
invariant property". More often, the words \is an invariant property" are omitted, and
we say \ c is a multiple of 4 ".
Now, suppose the tetrominoes over an mn board. (That is, the number of squares
along one side is m and the number along the other side is n .) Then, c = mn and,
so, mn is a multiple of 4 . For the produ t mn of two numbers m and n to be a
multiple of 4 , it must be the ase that either m or n (or both) is a multiple of 2 .
Note that, so far, the argument has been about tetrominoes in general, and not
parti ularly about T-tetrominoes. What we have just shown is, in fa t, the solution to
problem 6(a): if a re tangular board is overed by tetrominoes, at least one of the sides
of the re tangle must have even length.
The dis overy of a solution to problem 6(a), in this way, illustrates a general phenomenon in solving problems . The pro ess of solving more di ult problems typi ally
involves formulating and solving simpler subproblems. In fa t, one ould say that a

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

16

2. Invariants

\di ult" problem is one that involves putting together the solution to several simple
problems. Looked at this way, \di ult" problems be ome a lot more manageable. Just
keep on solving simple problems until you have rea hed your goal!
At this point, we want to introdu e a style for presenting al ulations that is learer
than the normal mixture of text with interspersed mathemati al expresssions. To introdu e the style we repeat the argument just given. Here it is in the new style:
an mn board is overed with tetrominoes
{

invariant: c is a multiple of 4 ,
c = mn

mn is a multiple of 4
{

property of multiples }

m is a multiple of 2 n is a multiple of 2 .

This is a two-step al ulation. The rst step is a so- alled \impli ation" step, as indi ated
by the \ " symbol. The step is read as

If an

mn board is overed with tetrominoes, mn is a multiple of 4 .

(Alternatively, \an mn board is overed with tetrominoes implies mn is a multiple


of 4 " or \an mn board is overed with tetrominoes only if mn is a multiple of
4 .")
The text between urly bra kets, following the \ " symbol is a hint why the statement 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 mn board has been overed, the number of overed
squares is mn .
The se ond step is read as:
If mn is a multiple of 4 , m is a multiple of 2 or n is a multiple of 2 .
Again, the \ " symbol signi es 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 familiar 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.

January 7, 2007

17

2.4. Tetrominoes

If an mn board is overed with tetrominoes, m is a multiple of 2 or n is


a multiple of 2 .
This style of presenting a mathemati al al ulation reverses the normal style: mathemati al expressions are interspersed with text, rather than the other way around. In luding hints within urly bra kets between two expressions means that the hints may
be as long as we like; they may even in lude other sub al ulations. In luding the symbol
\ " makes lear the relation between the expressions it onne ts. More importantly,
it allows us to use other relations. Later, we present al ulations in whi h \ " is the
onne ting symbol. Su h al ulations work ba kwards from a goal to what has been
given, whi h is often the most e e tive way to reason.
Let us now ta kle problem 6(b) head on. The insight needed is to olour the re tangle
with bla k and white squares, as on a hessboard. The T-tetrominoes should be oloured
in the same way. This gives us two types, one with three bla k squares and one white
square, and one with three white squares and one bla k square. We all them dark
and light T-tetrominoes. (See g. 2.5.) Pla ing the tetrominoes on the board now
involves hoosing the appropriate type so that the olours of the overed squares mat h
the olours of the tetrominoes.

Figure 2.5: Dark and light T-tetrominoes


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 T-tetrominoes 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 .

Pla ing a light tetromino on the board is modelled by the assignment


l , b , w := l+1 , b+1 , w+3 .

An invariant of both assignments is


b 3d l ,

sin e

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

18

2. Invariants
(b 3d l)[d , b , w := d+1 , b+3 , w+1]
=

de nition of substitution }

(b+3) 3(d+1) l
=

arithmeti }

b 3d l

and
(b 3d l)[l , b , w := l+1 , b+1 , w+3]
=

de nition of substitution }

(b+1) 3d (l+1)
=

arithmeti }

b 3d l .

Similarly, another invariant of both assignments is


w 3l d .

Now, the initial value of b 3d l is zero. So, it will always be zero, no matter how
many T-tetrominoes are pla ed on the board. Similarly, the value of w 3l d will
always be zero.
We an now solve the given problem.
a re tangular board is overed by T-tetrominoes

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 3d l = 0
w 3l d = 0

(b = w) (3d + l = 3l + d)

arithmeti }

(b = w) (l = d)

b 3d l = 0

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

19

2.5. Additional Exer ises


w 3l d = 0

b = w = 4d = 4l
{

arithmeti }

b+w = 8d
{

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 T-tetrominoes, 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 e e 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 T-tetrominoes are used. Similarly, the solution also
learly identi es 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!

2.5

Additional Exercises

Given is a bag of three kinds of obje ts. The total number of obje ts is
redu ed by repeatedly removing two obje ts of di erent kind, and repla ing them by an
obje t of the third kind.
Identify exa t onditions in whi h it is possible to remove all the obje ts ex ept
one.
Exercise 2.2

2.6

Bibliographic Remarks

The empty-box problem was given to me by Wim Feijen. The problems of the bla k and
white balls is from [Gri81. The tetromino problems I found in the 1999 Vierkant Voor

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

20

2. Invariants

Wiskunde alendar. (See http://www.vierkantvoorwiskunde.nl/puzzels/.) Vierkant

Voor Wiskunde |foursquare for mathemati s| is a foundation that promotes mathemati s in Dut h s hools. Their publi ations ontain many examples of mathemati al
puzzles, both new and old. I have made grateful use of them throughout this text. The
domino and tumbler problems are old hestnuts. I do not know their origin.
Exer ise 2.2 was posed to me by Dmitri Chubarov. It was posed (in a slightly di erent
form) in the Russian national Mathemati s Olympiad in 1975 and appears in a book
by Vasiliev entitled \Zada hi Vsesoyuzynykh Matemti heskikh Olympiad" published in
Mos ow, 1988. The author of the problem is apparently not stated.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Chapter 3
Crossing a River
The examples in this hapter all involve getting a number of people or things a ross
a river under ertain onstraints. We use them as simple illustrations of \brute-for e"
sear h and problem de omposition.
Brute-for e sear h means systemati ally trying all possibilities. It's a te hnique that
doesn't require any skill, but does require a lot of areful and a urate work. Using brute
for e is not something human beings are good at; lots of areful, a urate work is something more suited to omputers. But, brute for e isn't even pra ti al for implementation
on a omputer. The amount of work involved explodes as the problem size gets bigger,
making it impra ti al for all but toy problems. Nevertheless, it is useful to know what
brute for e entails, be ause it helps to understand the nature of problem-solving.
Problem de omposition is something we humans are mu h better at. Problem de omposition involves exploiting the stru ture of a problem to break it down into smaller,
more manageable problems. On e a problem has been broken down in this way, brute
for e an be applied. Indeed, it is often the ase that, ultimately, brute for e is the only
solution method, so we an't dispense with it. However, it is mu h better to spend more
e ort in de omposing a problem, postponing the use of a brute-for e sear h for as long
as possible.
All river- rossing problems have an obvious stru tural property, namely the symmetry
between the two banks of the river. The exploitation of symmetry is a very important
problem-solving te hnique, but is often overlooked, parti ularly when using brute for e.
You may already have seen the problems, or similar ones, elsewhere. As illustrations of
brute-for e sear h |whi h is how their solutions are often presented| they are extremely
uninteresting! However, as illustrations of the use of symmetry, ombined with problem
de omposition, they have startling, hidden beauty.
An important issue that emerges in this hapter is naming the elements of a problem.
De iding on what and how names should be introdu ed an be ru ial to su ess. We
shall see how inappropriate or unne essary naming an in rease the omplexity of a

Algorithmi Problem Solving Roland Ba khouse.

21

January 7, 2007

22

3. Crossing a River

problem, making it impossible to solve even with the aid of a very powerful omputer.

3.1

Problems

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).
How an the farmer a hieve the task?
2. The Jealous Couples.
Three ouples (husband and wife) wish to ross a river. They have one boat that
an arry at most two people, making several trips a ross the river ne essary. The
husbands are so jealous of ea h other that none is willing to allow their wife to be
with another man, if they are not themselves present.
How an all three ouples get a ross the river?
3. Adults and Children.
A group of adults and hildren are on one side of a river. They have one boat that
is only big enough to a ommodate one adult or two hildren.
How an all the adults and all the hildren ross the river? Make lear any assumptions you are obliged to make.
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
Dee is 100 kilos. Bob an't row.
How an they all get to the other side?

3.2
3.2.1

Brute Force
Goat, Cabbage and Wolf

The goat-, abbage- and wolf-problem is often used to illustrate brute-for e sear h. Our
main purpose in showing the brute-for e solution is to illustrate the pitfalls of poor

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

23

3.2. Brute For e

problem-solving skills. Additionally, we introdu e some terminology that is useful when


dis ussing the e ien y of a parti ular solution to a problem.
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).
How an the farmer a hieve the task?
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 brute-for e sear h involves onstru ting a state-transition graph that models all
possible states, and ways of hanging from one state to another | the state transitions.
In the goat-, abbage-, wolf-problem, 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 state-transition
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 state-transition graph to demonstrate the brute-for e solution to
this problem.
If four variables an ea h have one of two values, there are 24 (i.e. sixteen) di erent
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.

January 7, 2007

24

3. Crossing a River
f = g = c g 6= c .

That is, either the farmer, the goat and the abbage are all on the same bank ( f = g = c ),
or the goat and abbage are on di erent banks ( g 6= c ). This ex ludes ases where g
and c are equal, but di erent 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 di erent ombinations. (Noti e that when f and g are
equal all ombinations of c and w are allowed; when f and g are di erent, c and w
are required to be equal.)
f

L
L
L
L
L
R
R
R
R
R

L
L
L
L
R
L
R
R
R
R

L
L
R
R
L
R
L
L
R
R

L
R
L
R
L
R
L
R
L
R

Now, we enumerate all the possible transitions between these states. The graph in
g. 3.1 does just this. The nodes of the graph |the boxes| represent states, and the
edges of the graph |the lines onne ting the boxes| represent transitions. There are
no arrows on the edges be ause ea h transition an be reversed.
At the very left, the box labelled \LLLL" represents the state where all four are on
the left bank. The only allowed transition from this state is to the state where the farmer
and goat are at the right bank, and the abbage and wolf are at the left bank. This is
represented by the line onne ting the \LLLL" box to the \RRLL" box.
From the graph, it is lear that there are two solutions to the problem. Ea h solution
is given by a path through the graph from \LLLL" box to the \RRRR" box. The upper
path gives the following solution:
1. The farmer takes the goat to the right bank, and returns alone. This is the path
from LLLL to LRLL.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

25

3.2. Brute For e

RRRL
LLLL

RRLL

LLRL

LRLL

RLRR
RRLR

LLRR

RRRR

LLLR

Figure 3.1: Goat-, Cabbage-, Wolf-Problem


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.

3.2.2

State-Space Explosion

There is often a tenden y to apply brute for e without thinking when fa ed with a new
problem. However, it should only be used where it is unavoidable. Brute for e is only
useful for very simple problems. For other problems, the sear h spa e qui kly be omes
mu h too large. In the jargon used by omputing s ientists, brute for e does not \s ale
up" to larger problems. The goat-, abbage- and wolf-problem is not representative; the
above |thoughtless!| solution has a manageable number of states, and a manageable
number of transitions.
We an see how qui kly the sear h spa e an grow by analysing what is involved in
using brute for e to solve the remaining problems in se tion 3.1.
In the \overweight" problem, there are four named individuals and no restri tions
on their being together on the same side of the bank. So, there are 16 possible states;
unlike in the goat-, abbage- and wolf-problem, no restri tion on the size of the state
spa e is possible. Also, from the initial state there are four di erent transitions; from
most other states, there are at least two transitions. So, the total number of transitions
is large, too large even for the most diligent problem-solvers.
The situation in an unskilled solution of the \jealous- ouples" problem is even worse.
Here, there are six individuals involved, ea h of whom an be on one side or other of the

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

26

3. Crossing a River

river bank. If we give ea h individual a distin t name, the number of states is 26 , i.e.
64 ! That's an impossible number for any human being to ope with, and we haven't even
begun to ount the number of transitions. In another variation on the jealous- ouples
problem, there are ve ouples, and the boat an take three people at a time. That
means, if all are named, there are are 210 , i.e. 1024 , di erent states, and a yet larger
number of transitions. Take note: these are \toy" problems, not real problems.
The \adults-and- hildren" problem illustrates another failing of brute for e, namely
that it an only be applied in spe i ases, and not in the general ase. The number of
adults and hildren is not spe i ed in this problem. Yet, it is in fa t the easiest of all to
solve.
The use of a omputer to perform a brute-for e sear h shifts the meaning of what is
a \small" problem and what is a \large" problem, but not as mu h as one might expe t.
The so- alled \state-explosion problem" gets in the way. The river- rossing problems
illustrate \state explosion" very well. If there are n individuals in su h a problem,
there are, in prin iple, 2n di erent states to be onsidered. But, even for quite small
n , 2n is a very large number. We speak of an \exponential" growth in the number of
states ( n is the exponent in 2n ). Whenever the state spa e of a lass of problems grows
exponentially, it means that even the largest and fastest super omputers an only ta kle
quite small instan es.
Drawing state-transition diagrams is equally ine e tive. A diagram an o asionally
be used to illustrate the solution of a simple, well- hosen problem. But onstru ting
a diagram is rarely helpful in problem-solving. Instead, diagrams qui kly be ome a
problem in themselves | apart from the size of paper needed, how are the nodes to be
pla ed on the paper so that the diagram be omes readable?

3.2.3

Abstraction

The state-spa 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- abbageand-wolf problem is a good example.
In the goat- abbage-and-wolf 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 di erent names?
Let us restate the problem, this time with a naming onvention that omits the unne essary distin tion between the wolf and the abbage. In the restated problem, we all

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

27

3.3. Jealous Couples

the goat an \alpha" and the abbage and the wolf \betas".
A farmer wishes to ferry an alpha and two betas a ross a river. However,
his boat is only large enough to take one of them at a time, making several
trips a ross the river ne essary. Also, an alpha should not be left alone with
a beta.
How an the farmer a hieve the task?
Now the problem be omes mu h easier to solve. Indeed, there is only one solution:
Take the alpha a ross, and then one beta a ross, returning with the alpha. Then take
the se ond beta a ross, followed by the alpha. Be ause there is only one solution, it is
easy to dis over, and it is unne essary to onstru t a state-transition diagram for the
problem.
The problem-solving prin iple that we learn from this example is very important.
Avoid unnecessary or inappropriate naming.

When elements of a problem are given individual names, it distinguishes them from other
elements of the problem, and adds to the size of the state spa e. The pro ess of omitting
unne essary detail, and redu ing a problem to its essentials is alled abstra tion. Poor
solutions to problems are ones that fail to \abstra t" adequately, making the problem
more ompli ated than it really is. We en ounter the importan e of appropriate naming
time and again in the oming hapters. Bear it in mind as you read.

3.3

Jealous Couples

Very often, a problem has an inherent stru ture that fa ilitates de omposing the problem
into smaller problems. The smaller problems an then be further de omposed until they
be ome su iently manageable to be solvable by other means, perhaps even by brute
for e. Their solutions are then put together to form a solution to the original problem.
The jealous- ouples problem is an ex ellent example. It an be solved by brute for e,
making it de idedly boring. But, it an be solved mu h more e e tively, making use of
general problem-solving prin iples.
Re all its statement:
Three ouples (husband and wife) wish to ross a river. They have one
boat that an arry at most two people, making several trips a ross the river
ne essary. The husbands are so jealous of ea h other that none is willing to
allow their wife to be with another man if they are not themselves present.
How an all three ouples get a ross the river?

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

28

3.3.1

3. Crossing a River

Whats The Problem?

Before we ta kle this parti ular problem, let us try to determine what the essen e of the
problem is.
Suppose there is one boat that an arry two \things", and there are no other restri tions. Then, learly, it is possible to get any number of \things" a ross the river: repeat
the pro ess of letting two ross from left to right, followed by one returning from right
to left, until at most two remain on the left bank.
Now, by repla ing \thing" by \ ouple", we infer that a boat that an arry two
ouples at one rossing an be used to ferry an arbitrary number of ouples a ross the
river. (After all, ouples are not jealous of ea h other! ) Sin e a ouple is two people, this
means that a boat that an arry four people is su ient to ferry an arbitrary number
of ouples a ross the river.
This simple analysis gives us a di erent slant on the problem. Rather than ta kle
the problem as stated, we an ta kle a related problem, namely, what is the minimum
apa ity needed to ferry three ouples a ross the river? More generally, what is the
minimum apa ity needed to ferry n ouples a ross the river? Obviously, the minimum
apa ity is at least two (sin e it is not possible to ferry more than one person a ross a
river in a boat that an only arry one person at a time), and we have just shown that
it is at most four.
Alternatively, we an spe ify the apa ity of the boat and ask what is the maximum
number of ouples that an be ferried a ross with that apa ity. If the apa ity is one
(or less) the maximum number of ouples is zero, and if the apa ity is four, there is no
maximum. So, the question is how many ouples an be ferried with a boat of apa ity
two, and how many ouples an be ferried with a boat of apa ity three.
The new problems look more di ult than the original. In the original problem, we
are given the answer |in the ase of three ouples, a boat with apa ity two is needed|
and we are required to give a onstru tive proof that this is the ase. But, there is often
an advantage in not knowing the answer | be ause we an sometimes gain insight by
generalising, and then rst solving simpler instan es of the general problem.

3.3.2

Problem Structure

The stru ture of this problem suggests several ways in whi h it might be de omposed.
First, there are three ouples. This suggests seeking a solution that gets ea h ouple
a ross in turn. That is, we de ompose the problem into three subproblems: get the rst
ouple a ross, get the se ond ouple a ross, and get the third ouple a ross.
Another de omposition is into husbands and wives. A ording to the maxim \ladies
before gentlemen", we ould try rst getting all the wives a ross, followed by all the

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

29

3.3. Jealous Couples

husbands. Alternatively, letting \age go before beauty", we ould try rst getting all the
husbands a ross, followed by all the wives.
Getting all the wives a ross, whilst their husbands remain at the left bank turns out
to be easy. The reason is that, if the husbands all stay in one pla e, there is no di ulty
in transferring the wives away from them. Getting all the husbands a ross rst, whilst
their wives stay at the left bank, seems mu h harder. On the other hand, getting the
husbands to join their wives may prove to be harder than getting the wives to join their
husbands. Ladies before gentleman, or age before beauty; there doesn't seem mu h to
hoose between them.
There is, however, one key stru tural property of the problem that we have not yet
onsidered. It is the symmetry between the left and right banks. The pro ess of getting
a group of people from left to right an always be reversed; the result is a pro ess for
getting the same group of people from right to left. Perhaps a symmetri solution is
possible! If that is the ase, we only need to do half the work, and that is a major
saving. This is indeed what we do.
(The state-transition diagram for the goat-, abbage-, wolf-problem exhibits the leftright symmetry very well. The diagram also illustrates the symmetry between the abbage and wolf. Both symmetries were to be expe ted from the problem statement; by
ignoring them and using brute-for e, we lost the opportunity of a redu tion in e ort.)

3.3.3

Denoting States and Transitions

We begin by introdu ing some lo al notation to make the solution strategy pre ise. The
introdu tion of notation involves naming the elements of the problem that we want to
distinguish. As dis ussed earlier, this is a ru ial step in nding a solution.
Here, we use letters H , W and C to mean husband, wife and ouple, respe tively.
These are pre eded by a number; for example, 2H means two husbands, 3C means
three ouples and 1C,2H means one ouple and two husbands. We exploit the notation
to distinguish between ouples and individuals; for example, 1H,1W means a husband
and wife who do not form a ouple, whilst 1C means a husband and wife who do form
a ouple.
Note that we do not name the individual people as in, for example, Ann, Bob,
Clare et . It is only the number of husbands, wives and ouples that is relevant to the
problem's solution. Number is an extremely important mathemati al abstra tion.
We distinguish between states and a tions.
A state des ribes a situation when ea h individual (husband or wife) is at one of the
banks. A state is denoted by two sequen es separated by bars. An example is 3H || 3W ,
whi h denotes the state in whi h all three husbands are at the left bank, and all three
wives are at the right bank. A se ond example of a state is 1C,2H || 2W , whi h denotes

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

30

3. Crossing a River

the state in whi h one ouple and two husbands are at the left bank and two wives are at
the right bank. The starting state is thus 3C || and the required nishing state is || 3C .
An a tion is when some individuals are being transported a ross the river. An
example is 3H |2W| 1W ; this denotes the a tion of transporting two wives a ross the
river, leaving three husbands at the left bank and one wife at the right bank.
Note that the notation for states and a tions does not spe ify the position or dire tion
of the boat, and, taken out of ontext, ould be ambiguous. Sin e the position of the
boat must alternate between the left bank and the right bank, this ambiguity is easily
resolved.
The notation allows valid and invalid states/a tions to be easily identi ed. For example, 1C,1W || 1C,1H is invalid (be ause there is a wife who is on the same side of
the river as a man other than her husband, who is on the other side of the river). Also,
3H |3W| is invalid be ause the boat an only arry at most two people.
In general, a omplete, detailed solution to the problem is a sequen e, beginning
with the state 3C || and ending with the state || 3C , that alternates between states and
a tions.
An a tion results in a hange of state. (In the terminology of state-transition diagrams, an a tion e e 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 }

is the property that, if the sequen e of a tions S is performed beginning in state p , it


will result in state q . So, for example,
{

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.

January 7, 2007

31

3.3. Jealous Couples

3.3.4

Problem Decomposition

Using this notation we an express our strategy for de omposing the problem. The goal
is to onstru t a sequen e of a tions S0 satisfying
{ 3C || }

S0 { || 3C } .

Our strategy an be summarised as exploiting two properties of the problem.

 The left-right symmetry.


 The fa t that it is easy to get the wives from one side to the other whilst their
husbands remain on one bank.

This strategy is realised by de omposing S0 into three sequen es S1 , S2 and S3 su h


that
{ 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.

January 7, 2007

32

3. Crossing a River
{

3C ||

1C,2H |2W|
;

1C,2H || 2W

1C,2H |1W| 1W
;

2C,1H || 1W

3H |2W| 1W
{

3H || 3W

} .

That is,
{ 3C || } 1C,2H |2W| ; 1C,2H |1W| 1W ; 3H |2W| 1W

{ 3H || 3W } .

As dis ussed above, the sequen e S3 is the reverse of S1 :


{

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.

January 7, 2007

33

3.3. Jealous Couples

Note parti ularly the middle a tion | 1C |1C| 1C | . This may be a left-to-right
rossing, or a right-to-left rossing; whi h is not immediately lear. The task is now to
onstru t the symmetri sequen es of a tions T1 and T2 .
If the middle a tion is from right to left, the a tion must be pre eded by the state
1C || 2C and results in the state 2C || 1C . Vi e-versa, if the middle a tion is from left
to right, the a tion must be pre eded by the state 2C || 1C and results in the state
1C || 2C . There is little alternative but to use brute-for e sear h to try to determine
whi h an be a hieved.
Fortunately, T1 is soon dis overed. It onsists of just two a tions:
{

3H || 3W

3H |1W| 2W
;

1C,2H || 2W

1C |2H| 2W
{

1C || 2C }

Symmetri ally, for T2 we have:


{

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

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

34

3. Crossing a River
;

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 e e tive it is than brute-for e sear h.
The onstru tion seeks at ea h stage to exploit the symmetry between the left and
right banks. Sin e the number of rossings will inevitably be odd, ea h de omposition is into three subsequen es, where the rst and last are \mirror images" in some
sense. Naming the unknown sequen es, and formally spe ifying their fun tion using the
{ p } S { q } notation helps to larify what has to be a hieved, and to avoid error.
The nal solution involves eleven rossings. That's too many for anyone to ommit
to memory. But, be ause the solution method is well stru tured, it is easy to remember,
making a re onstru tion of the solution very simple. Moreover, the solution to the
problem annot be used in other ontexts, but the solution method an. For the proof
of the pudding, solve the following related problem:
There are ve jealous ouples, and their boat
an arry a maximum of three individuals. Determine how to transport all the ouples
a ross the river.
Exercise 3.1 (Five-couple Problem)

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 maximum 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.

Exercise 3.2 (Four-couple Problem)

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

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.)

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:
Exercise 3.3

 At most half of the husbands an ross together.


 The boat an only hold one ouple.

3.4

Rule of Sequential Composition

The { p } S { q } notation we used for solving the jealous- ouples problem is the
notation used for spe ifying and onstru ting omputer programs. It is alled a Hoare
triple. (Sir Tony Hoare is a British omputing s ientist who pioneered te hniques for
formally verifying the orre tness of omputer programs; he was one of the rst to use
the notation.)
A omputer program is spe i ed by a relation between the input values and the output
values. The allowed input values are spe i ed by a so- alled pre ondition, p , and the
output values are spe i ed by a post ondition , q . Pre onditions and post onditions
are properties of the program variables.
If S is a program, and p and q are properties of the program variables,

fpgSfqg

means that, if the program variables satisfy property p before exe ution of statement
S , exe ution of S is guaranteed to terminate and, afterwards, the program variables will
satisfy property q . For example, a program to ompute the remainder r and dividend
d after dividing number M by number N would have pre ondition
N 6= 0 ,

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

36

3. Crossing a River

(sin e dividing by 0 is not allowed) and post ondition


M = Nd + r 0 r < N .

If the program is S , the spe i ation of the program is thus


{ N 6= 0 } S { M = Nd + 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 frg
1

and

frgS fqg
2

The intermediate ondition r thus a ts as post ondition for S1 and pre ondition for S2 .
If the problem is de omposed into three subproblems, two intermediate onditions are
needed. This is what we did in solving the jealous- ouples problem. The initial problem
statement has pre ondition 3C || and post ondition || 3C . The intermediate onditions
3H || 3W and 3W || 3H were then introdu ed in order to make the rst de omposition.
There are di erent 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.

January 7, 2007

37

3.4. Rule of Sequential Composition

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 di erent 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 the bridge, so
that it is always arried when the bridge is rossed.
Show that it takes 17 minutes for all four to ross the bridge.
It is easy to see that ve trips are needed to get all four people a ross he bridge.
The ve trips omprise three trips where two people ross together interleaved with two
trips where one person returns with the tor h. Sin e there are only two return trips, and
there are four people, at least two people never make a return trip. Clearly, to a hieve
the shortest time, the two slowest should not make a return journey. The question is
whether they should ross together or seperately.
The times have been hosen in this example so that the shortest time is a hieved
when the two slowest ross together. (It isn't always the ase that the best strategy is
to let the two slowest ross together. See exer ise 3.4.) Using the times as identi ers for
the individuals, the solution will therefore in lude, for some p and q , a rossing of the
form
p |5,10| q

as one step. Seeking to exploit symmetry, the task be omes one of determining sequen es
of rossings S1 and S2 , and p and q su h that
{ 1,2,5,10 || } S1 { p,5,10 || q }
{ p,5,10 || q } p |5,10| q { p || q,5,10 }

and
{ p || q,5,10 } S2 { || 1,2,5,10 } .

We leave you to omplete the rest.


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 di erent amounts of time to ross the bridge; when two ross
Exercise 3.4 (The Torch Problem)

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

38

3. Crossing a River

together they must pro eed at the speed of the slowest. The rst person takes t1 minutes
to ross, the se ond t2 minutes, the third t3 minutes and the fourth t4 minutes. The
tor h must be ferried ba k and forth a ross the bridge, so that it is always arried when
the bridge is rossed.
You may assume that t1 t2 t3 t4 . Find the optimal method of getting all four
a ross.
Apply your solution to the following two ases:
(a) The times taken are 1 minute, 1 minute, 3 minutes and 3 minutes.
(b) The times taken are 1 minute, 4 minutes, 4 minutes and 5 minutes.

Hint: In the spe i ase dis ussed above (where t1 , t2 , t3 and t4 are 1 , 2 , 5 and
10 , respe tively), the shortest time is a hieved by letting the two slowest ross together.
However, this isn't always the best strategy. An alternative strategy is to let the fastest
person a ompany ea h of the others a ross in turn. You will need to evaluate the time
taken for both strategies and hoose between them on the basis of the times. In order
to derive the solution methodi ally, we suggest the following steps:
(a) How many times must the tor h must be arried a ross the bridge in order to get all

four people a ross? (In lude rossings in both dire tions in the ount.) How many
of these are return trips?

(b) Comparing the number of times a return journey must be made with the number of

people, what an you say about the number of people who do not make a return
journey? Whi h of the four people would you hoose not to make a return journey?
(Give a onvin ing argument to support your hoi e.)

(c) Now fo us on how to get the people who do not make a return journey a ross the

bridge. What are the di erent 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

between the strategy to be used.

Suppose a brute-for e sear h is used to solve the tor h problem (exer ise
3.4 above). This would mean enumerating all the di erent ways of getting four people
a ross the bridge. How many ways are there?
Exercise 3.5

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

39

3.5. Summary

3.5

Summary

In this hapter, we have ontrasted brute-for e sear h with problem de omposition.


Brute-for 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 solvable, but the state-spa 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 problem-solving 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 real-world des ription of a problem, we abstra t the few that
are relevant, introdu ing appropriate, learly de ned mathemati al notation to assist in
the problem's solution.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

40

3. Crossing a River

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Chapter 4
Games
This hapter is about how to win some simple two-person games. Games provide very
good examples of algorithmi problem solving be ause playing games is all about winning. The goal is to have some method (i.e. \algorithm") for de iding what to do so that
the eventual out ome is a win.
The key to winning is the re ognition of invariants. So, in essen e, this hapter is
a ontinuation of hapter 2. The hapter is also about trying to identify and exploit
stru ture in problems. In this sense, it introdu es the importan e of algebra in problem
solving.
The next se tion introdu es a number of games with mat hsti ks, in order to give
the avour of the games that we onsider. Following it, we develop a method of systemati ally identifying winning and losing positions in a game (assuming a number of
simplifying onstraints on the rules of the game). A winning strategy is then what we
all \maintaining an invariant". \Maintaining an invariant" is an important te hnique in
algorithm development. Here, it will mean ensuring that the opponent is always pla ed
in a position from whi h losing is inevitable.

4.1

Matchstick Games

A mat hsti k game is played with one or more piles of mat hes. Two players take it
in turn to make a move. Moves involve removing one or more mat hes from one of the
piles, a ording to a given rule. The game ends when it is no longer possible to make
a move. The player whose turn it is to move is the loser, and the other player is the
winner.
A mat hsti k game is an example of an impartial, two-person game with omplete
information. \Impartial" means that rules for moving apply equally to both players.
(Chess, for example, is not impartial, be ause white an only move white pie es, and
bla k an only move bla k pie es.) \Complete information" means that both players

Algorithmi Problem Solving Roland Ba khouse.

41

January 7, 2007

42

4. Games

know the omplete state of the game. In ontrast, in ard games like poker, it is usual
that ea h player does not know the ards held by the other player; the players have
in omplete information about the state of the game.
A winning position is one from whi h a perfe t player is always assured of a win. A
losing position is one from whi h a player an never win, when playing against a perfe t
player. A winning strategy is an algorithm for hoosing moves from winning positions,
that guarantees a win.
As an example, suppose there is one pile of mat hes, and an allowed move is to remove
1 or 2 mat hes. The losing positions are the positions where the number of mat hes
is a multiple of 3 (that is, the number of mat hes is 0 , 3 , 6 , 9 et .). The remaining
positions are the winning positions. If m is the number of mat hes in su h a position
(so, m is not a multiple of 3 ), the strategy is to remove m mod 3 mat hes1. This is
either 1 or 2 , and so the move is valid. The opponent is then put in a position where
the number of mat hes is a multiple of 3 . This means that there are either 0 mat hes
left, in whi h ase the opponent loses, or any move they make will result in there again
being a number of mat hes remaining that is not a multiple of 3 .
In an impartial game that is guaranteed to terminate no matter how the players
hoose their moves (i.e. the possibility of stalemate is ex luded), it is always possible to
hara terise the positions as either winning or losing positions. The following exer ises
ask you to do this in spe i ases.
1. There is one pile of mat hes. Ea h player is allowed to remove 1 mat h. What are
the winning positions?
2. There is one pile of mat hes. Ea h player is allowed to remove 0 mat hes. What
are the winning positions?
3. Can you see a pattern in the last two problems and the example dis ussed above
(in whi h a player is allowed to remove 1 or 2 mat hes)? In other words, an you
see how to win a game in whi h an allowed move is to remove at least one and at
most N mat hes, where N is some number xed in advan e?
4. There is one pile of mat hes. Ea h player is allowed to remove 1 , 3 or 4 mat hes.
What are the winning positions and what is the winning strategy?
5. There is one pile of mat hes. Ea h player is allowed to remove 1 , 3 or 4 mat hes,
ex ept that it is not allowed to repeat the opponent's last move. (So, if, say, your
opponent removes 1 mat h, your next move must be to remove 3 or 4 mat hes.)
What are the winning positions and what is the winning strategy?
1 Re all

that m mod 3 denotes the remainder after dividing m by 3 .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

43

4.2. Winning Strategies

6. There are two piles of mat hes. A move is to hoose one pile and, from that pile,
remove 1 , 2 or 3 mat hes. What are the winning positions and what is the
winning strategy?
7. There are two piles of mat hes. A move is to hoose one pile; from the left pile 1 ,
2 or 3 mat hes may be removed, and from the right pile 1 thru2 7 mat hes may
be removed. What are the winning positions and what is the winning strategy?
8. There are two piles of mat hes. A move is to hoose one pile; from the left pile, 1 ,
3 or 4 mat hes may be removed, and, from the right pile, 1 or 2 mat hes may
be removed. What are the winning positions and what is the winning strategy?

4.2

Winning Strategies

In this se tion, we formulate what is required of a winning strategy. We begin with the
simple mat hsti k game where a move is to remove one or two mat hes from a single
pile of mat hes; we show how to sear h systemati ally through all the positions of the
game, labelling ea h as either a winning or a losing position. Although a brute-for e
sear h, and thus not pra ti al for more ompli ated games, the algorithm does give a
better understanding of what is involved, and an be used as a basis for developing more
e ient solutions in parti ular ases.

4.2.1

Assumptions

We make a number of assumptions about the game, in order that the sear h will work.

 We assume that the number of positions is nite.


 We assume that the game is guaranteed to terminate no matter how the players
hoose their moves.

The rst assumption is ne essary be ause a one-by-one sear h of the positions an


never be omplete if the number of positions is in nite. The se ond assumption is
ne essary be ause the algorithm relies on being able to hara terise all positions as
either losing or winning; we ex lude the possibility that there are stalemate positions.
Stalemate positions are ones from whi h the players an ontinue the game inde nitely,
so that neither wins or loses.
2 We

use \thru" when we want to spe ify an in lusive range of numbers. For example, \ 1 thru 4 "
means the numbers 1 , 2 , 3 and 4 . The English expression \ 1 to 4 " is ambiguous about whether the
number 4 is in luded or not.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

44

4. Games

4.2.2

Labelling Positions

The rst step is to draw a dire ted graph depi ting all positions, and all moves in the
game. Fig. 4.1 is a graph of the mat hsti k game des ribed at the beginning of se tion
4.1.

Figure 4.1: Mat hsti k Game. Players may take one or two mat hes at ea h turn.
A dire ted graph has a set of nodes and a set of edges. Ea h edge is from one node
to another node. When graphs are drawn, nodes are depi ted by ir les, and edges are
depi ted by arrows pointing from the from node to the to node.
The nodes in g. 4.1 are labelled by a number, the number of mat hes remaining in
the pile. From the node labelled 0 , there are no edges. It is impossible to move from
the position in whi h no mat hes remain. From the node labelled 1 , there is exa tly one
edge, to the node labelled 0 . From the position in whi h one mat h remains, there is
only one move that an be made, namely to remove the remaining mat h. From all other
nodes, there are two edges. From the node labelled n , where n is at least 2 , there is
an edge to the node labelled n1 and an edge to the node labelled n2 . That is, from
a position in whi h the number of remaining mat hes is at least 2 , one is allowed to
remove one or two mat hes.
Having drawn the graph, we an begin labelling the nodes as either losing positions
or winning positions. A player who nds themself in a losing position will inevitably
lose, if playing against a perfe t opponent. A player who nds themself in a winning
position is guaranteed to win, provided the right hoi e of move is made at ea h turn.
The labelling rule has two parts, one for losing positions, the other for winning
positions:

 A node is labelled losing if every edge from the node is to a winning position.
 A node is labelled winning if there is an edge from the node to a losing position.

At rst sight, it may seem that it is impossible to begin to apply these rules; after
all, the rst rule de nes losing positions in terms of winning positions, whilst the se ond
rule does the reverse. It seems like a vi ious ir le! However, we an begin by labelling
as losing positions all the nodes with no outgoing edges. This is be ause, if there are no
edges from a node, the statement \every edge from the node is to a winning position" is
true. It is indeed the ase that all of the (non-existent) edges is to a winning position.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

45

4.2. Winning Strategies

This is an instan e of a general rule of logi . A statement of the form \every x has
property p " is what is alled a for-all quanti ation, or a universal quanti ation.
Su h a statement is said to be va uously true when there are no instan es of the \ x "
in the quanti ation. In a sense, the statement is \va uous" (i.e. empty) be ause it is a
statement about nothing.
Returning to g. 4.1, the node 0 is labelled \losing", be ause there are no edges
from it. It is indeed a losing position, be ause the rules of the game spe ify that a player
who annot make a move loses.
Next, nodes 1 and 2 are labelled \winning", be ause, from ea h, there is an edge to
0 , whi h we know to be a losing position. Note that the edges we have identi ed di tate
the move that should be made from these positions if the game is to be won.
Now, node 3 is labelled \losing", be ause both edges from node 3 are to nodes ( 1
and 2 ) that we have already labelled \winning". From a position in whi h there are 3
mat hes remaining, every move is to a position starting from whi h a win is guaranteed.
A player that nds themself in this position will eventually lose.
The pro ess we have des ribed repeats itself until all nodes have been labelled. Nodes
4 and 5 are labelled \winning", then node 6 is labelled \losing", then nodes 7 and 8
are labelled \winning", and so on.
Fig. 4.2 shows the state of the labelling pro ess at the point that node 7 has been
labelled but not node 8 . The ir les depi ting losing positions are drawn with thi k
lines; the ir les depi ting winning positions are the ones from whi h there is an edge
drawn with a thi k line. These edges depi t the winning move from that position.

Figure 4.2: Labelling Positions. Winning edges are indi ated by thi k edges.
Clearly, a pattern is emerging from this pro ess. The pattern is that the losing
positions are the ones where the number of mat hes is a multiple of 3 . The winning
positions are the remaining positions; the winning strategy is to remove one or two
mat hes so as to leave the opponent in a position where the number of mat hes is on e
again a multiple of 3 .

4.2.3

Formulating Requirements

The terminology we use to des ribe the winning strategy is to \maintain invariant"
the property that the number of mat hes is a multiple of 3 . In programming terms,

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

46

4. Games

we express this property using Hoare triples. Let n denote the number of mat hes in
the pile. Then, the orre tness of the winning strategy is expressed by the following
annotated program segment:
{

n is a multiple of 3 , and n 6= 0

if 1 n n := n1
;

2 n n := n2 fi

n is not a multiple of 3 }

n := n (n mod 3)
{

n is a multiple of 3 }

There are ve omponents of this program segment, ea h on a separate line. The rst
line is the pre ondition. This expresses the assumption that we begin from a position in
whi h the number of mat hes is a multiple of 3 , and non-zero.
The se ond line is a so- alled onditional statement. Conditional statements are
re ognised by \ if - fi " bra kets. Within these bra kets is a non-deterministi hoi e |
indi ated by the \ " symbol| among a number of so- alled guarded ommands. A
guarded ommand has the form b S , where b is a boolean-valued expression alled
the guard, and S is a statement alled the body. Starting in a given state, a onditional
statement is exe uted by hoosing a guarded ommand whose guard evaluates to true ,
and then exe uting its body. If several guards evaluate to true , an arbitrary hoi e of
ommand is made. If none of the guards evaluates to true , exe ution is aborted3 .
In this way, the if - fi statement in the se ond line models an arbitrary move. Removing one mat h is only allowed if 1 n ; hen e, the statement n := n1 is \guarded"
by this ondition. Similarly, removing two mat hes |modelled by the assignment
n := n2 | is \guarded" by the ondition 2 n . At least one of these guards, and
possibly both, is true be ause of the assumption that n 6= 0 .
The post ondition of the guarded ommand is the assertion \ n is not a multiple
of 3 ". The triple, omprising the rst three lines, thus asserts that, if the number of
mat hes is a multiple of 3 , and a valid move is made that redu es the number of mat hes
by one or two, then, on ompletion of the move, the number of mat hes will not be a
multiple of 3 .
The fourth line of the sequen e is the implementation of the winning strategy; spe ifi ally, remove n mod 3 mat hes. The fth line is the nal post ondition, whi h asserts
that, after exe ution of the winning strategy, the number of mat hes will again be a
multiple of 3 .
3 If

you are already familiar with a onventional programming language, you will be familiar with
deterministi onditional statements | so- alled if-then-else statements. In su h statements, the hoi e
of whi h of the optional statements should be exe uted is ompletely determined by the state of the
program variables. In a non-deterministi hoi e, as used here, the hoi e is not ompletely determined.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

47

4.2. Winning Strategies

In summary, beginning from a state in whi h n is a multiple of 3 , and making


an arbitrary move, results in a state in whi h n is not a multiple of 3. Subsequently,
removing n mod 3 mat hes results in a state in whi h n is again a multiple of 3 .
In general, a winning strategy is obtained by hara terising the losing positions by
some property, losing say. The end positions (the positions where the game is over)
must satisfy the property losing . The winning positions are then the positions that do
not satisfy losing . For ea h winning position, one has to identify a way of al ulating
a losing position to whi h to move; the algorithm that is used is the winning strategy.
More formally, the losing and winning positions, and the winning strategy must satisfy
the following spe i ation.
{ losing position, and not an end position }

make an arbitrary (legal) move


;

{ winning position, i.e. not a losing position }

apply winning strategy


{ losing position }

In summary, a winning strategy is a way of hoosing moves that divides the positions
into two types, the losing positions and the winning positions, in su h a way that the
following three properties hold:

 End positions are losing positions.


 From a losing position that is not an end position, every move is to a winning
position.

 From a winning position, it is always possible to apply the winning strategy, resulting in a losing position.

If both players are perfe t, the winner is de ided by the starting position. If the
starting position is a losing position, the se ond player is guaranteed to win. Vi eversa, if the starting position is a winning position, the rst player is guaranteed to win.
Starting from a losing position, one an only hope that one's opponent is not perfe t,
and will make a mistake.
Formally, a winning strategy maintains invariant the boolean quantity
(the number of moves remaining before the game ends is even) equals (the
position is a losing position).
Sin e end positions are losing positions, by assumption, and positions where the number
of moves remaining is zero, whi h is an even number, this quantity will always be true
in a game played by perfe t players.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

48

4. Games

We re ommend that you now try to solve the mat hsti k-game problem when the
rule is that any number of mat hes from 1 thru M may be removed at ea h turn. The
number M is a natural number, xed in advan e. We re ommend that you try to solve
this general problem by rst onsidering the ase that M is 0 . This ase has a very easy
solution, although it is a ase that is very often negle ted. Next, onsider the ase that M
is 1 . This ase also has an easy solution, but slightly more ompli ated. Now, ombine
these two ases with the ase that M is 2 , whi h is the ase we have just onsidered.
Do you see a pattern in the solutions? If you don't see a pattern immediately, try a bit
harder. As a last resort, try working out the ase that M is 3 . (Don't draw a diagram.
Constru t a table instead. A diagram is mu h too ompli ated.) Then, return to the
ases that M is 0 , 1 and 2 (in parti ular, the extreme ases 0 and 1 ) in order to
he k the pattern you have identi ed. Finally, formulate the orre tness of the strategy
by a sequen e of assertions and statements, as we did above for the ase that M is 2 .
Two players alternately name dates. The
winner is the player who names 31st De ember, and the starting date is 1st January.
Ea h part of this exer ise uses a di erent rule for the dates that a player is allowed to
name. For ea h, devise a winning strategy, stating whi h player should win. State also
if it depends on whether the year is a leap year or not.
Hint: in prin iple, you have to determine for ea h of 365 days (or 366 in the ase of a
leap year) whether naming the day results in losing against a perfe t player. In pra ti e,
a pattern soon be omes evident and the days in ea h month an be grouped together
into winning and losing days. Begin by identifying the days in De ember that one should
avoid naming.
Exercise 4.1 (31st December Game)

a) (Easy) A player an name the 1st of the next month, or in rease the day of the month
by an arbitrary amount. (For example, the rst player begins by naming 1st February,
or a date in January other than the 1st.)
b) (Harder) A player an in rease the day by one, leaving the month un hanged, or name
the 1st of the next month.

4.3

Subtraction-Set Games

A lass of mat hsti k games is based on a single pile of mat hes and a ( nite) set of
numbers; a move is to remove m mat hes, where m is an element of the given set. A
game in this lass is alled a subtra tion-set game, and the set of numbers is alled the
subtra tion set.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

49

4.3. Subtra tion-Set Games

The games we have just dis ussed are examples of mat hsti k games; if the rule is that
1 thru M may be removed at ea h turn, the subtra tion set is {1..M} . More interesting
examples are obtained by hoosing a subtra tion set with less regular stru ture.
For any given subtra tion set, the winning and losing positions an always be omputed. We exemplify the pro ess in this se tion by al ulating the winning and losing
positions when the allowed moves are:

 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 one-by-one 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.
Position 0
Type
L
Move

Table 4.1: Winning (W) and Losing (L) Positions for subtra tion set {1 , 3 , 4}

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

50

4. Games

Position 7
Type
L
Move

10

11

12

13

Table 4.2: Winning (W) and Losing (L) Positions for subtra tion set {1 , 3 , 4}
Continuing this pro ess, we get the next seven entries in the table: see table 4.2.
Comparing tables 4.1 and 4.2, we noti e that the pattern of winning and losing
positions repeats itself. On e the pattern begins repeating in this way, it will ontinue to
do so forever. We may therefore on lude that, for the subtra tion set {1 , 3 , 4} , whether
or not the position is a winning position an be determined by omputing the remainder,
r say, after dividing the number of mat hes by 7 . If r is 0 or 2 , the position is a losing
position. Otherwise, it is a winning position. The winning strategy is to remove 1 mat h
if r is 1 , remove 3 mat hes if r is 3 or 5 , and remove 4 mat hes if r is 4 or 6 .
The repetition in the pattern of winning and losing positions that is evident in this
example is a general property of subtra tion-set games, with the onsequen e that, for a
given subtra tion set, it is always possible to determine for an arbitrary position whether
or not it is a winning position (and, for the winning positions, a winning move). The
following argument gives the reason why.
Suppose a subtra tion set is given. Sin e the set is assumed to be nite, it must
have a largest element. Let this be M . Then, from ea h position, there are at most
M moves. For ea h position k , let W.k be true if k is a winning position, and false
otherwise. When k is at least M , W.k is ompletely determined by the sequen e
W.(k1) , W.(k2) , . . . , W.(kM) . Call this sequen e s.k . Now, there are only 2M
di erent sequen es of booleans of length M . As a onsequen e, the sequen e s.(M+1) ,
s.(M+2) , s.(M+3) , . . . must eventually repeat, and it must do so within at most 2M
steps. That is, for some j and k , with M j < k < M+2M , we must have s.j = s.k . It
follows that W.j = W.k and the sequen e W repeats from the k th position onwards.
For the example above, this analysis predi ts that the W-L pattern will repeat from
the 20 th position onwards. In fa t, it begins repeating mu h earlier. Generally, we
an say that the pattern of win-lose positions will repeat at position 2M+M , or before.
To determine whether an arbitrary position is a winning or losing position involves
omputing the status of ea h position k , for su essive values of k , until a repetition in
s.k is observed. If the repetition o urs at position R , then, for an arbitrary position
k , W.k equals W.(k mod R) .
Suppose there is one pile of mat hes. In ea h move, 2 , 5 or 6 mat hes
may be removed. (That is, the subtra tion set is {2 , 5 , 6} .)
Exercise 4.2

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

51

4.3. Subtra tion-Set Games

(a) For ea h n , 0 n < 22 , determine whether a pile of n mat hes is a winning or

losing position.

(b) Identify a pattern in the winning and losing positions. Spe ify the pattern by giving
pre ise details of a boolean fun tion of n that determines whether a pile of n

mat hes is a winning position or not.


Verify the pattern by onstru ting a table showing how the fun tion's value hanges
when a move is made.

Figure 4.3 shows a variant of snakes and ladders. In this game, there
is just one ounter. The two players take it in turn to move the ounter at most four
spa es forward. The start is square 1 and the nish is square 25 . As in the usual game
of snakes and ladders, if the ounter lands on the head of a snake, it falls down to the
tail of the snake; if the ounter lands at the foot of a ladder, it limbs to the top of the
ladder.
Exercise 4.3

21

22

23

24

25

20

19

18

17

16

11

12

13

14

15

10

Figure 4.3: Snakes and Ladders. Players take it in turn to move the ounter at most
four spa es forward.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

52

4. Games

(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 identi ed as winning or losing in this way. Explain

why.

4.4

Sums of Games

In this se tion, we look at how to exploit the stru ture of a game in order to ompute a
winning strategy more e e tively.
The later examples of mat hsti k games in se tion 4.1 have more than one pile of
mat hes. When a move is made, one of the piles must rst be hosen; then, mat hes
may be removed from the hosen pile a ording to some pres ribed rule, whi h may di er
from pile to pile. The game is thus a ombination of two games; this parti ular way of
ombining games is alled summing the games.
In general, given two games, ea h with its own rules for making a move, the sum of
the games is the game des ribed as follows. For larity, we all the two games the left
and the right game. A position in the sum game is the ombination of a position in the
left game, and a position in the right game. A move in the sum game is a move in one
of the games.
Figure 4.4 is an example of the sum of two games. Ea h graph represents a game,
where the positions are represented by the nodes, and the moves are represented by the
edges. Imagine a oin pla ed on a node. A move is then to displa e the oin along one
of the edges to another node. The nodes in the left graph and right graphs are named
by apital letters and small letters, respe tively, so that we an refer to them later.
In the \sum" of the games, two oins are used, one oin being pla ed over a node in
ea h of the two graphs. A move is then to hoose one of the oins, and displa e it along
an edge to another node. Thus, a position in the \sum" of the games is given by a pair
Xx where \X" names a node in the left graph, and \x" names a node in the right graph;
a move has the e e t of hanging exa tly one of \X" or \x".
Both the left and right games in g. 4.4 are unstru tured; onsequently, the brutefor e sear h pro edure des ribed in se tion 4.2.2 is unavoidable when determining their
winning and losing positions. However, the left game in g. 4.4 has 15 di erent postions,

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

53

4.4. Sums of Games

Figure 4.4: A Sum Game. The left and right games are represented by the two graphs.
A position is a pair Xx where \X" is the name of a node in the left graph, and \x" is
the name of a node in the right graph. A move hanges exa tly one of X or x.
and the right game has 11 ; thus, the sum of the two games has 1511 di erent positions.
For this game, and for sums of games in general, a brute-for e sear h is highly undesirable.
In this se tion, we study how to ompute a winning strategy for the sum of two games. We
nd that the omputational e ort is the sum (in the usual sense of addition of numbers)
of the e ort required to ompute winning and losing positions for the omponent games,
rather than the produ t. We nd, however, that it is not su ient to know just the
winning strategy for the individual games. Deriving a suitable generalisation forms the
ore of the analysis.

4.4.1

Symmetry

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

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

54

4. Games

one mat h from the hosen pile. Otherwise, there is no restri tion on the number of
mat hes that may be removed. As always, the game is lost when a player annot make
a move.
This game is the \sum" of two instan es of the same, very, very simple, game: given
a (single) pile of mat hes, a move is to remove at least one mat h from the pile. In this
simple game, the winning positions are, obviously, the positions in whi h the pile has at
least one mat h, and the winning strategy is to remove all the mat hes. The position in
whi h there are no mat hes remaining is the only losing position.
It qui kly be omes lear that knowing the winning strategy for the individual games
is insu ient to win the sum of the games. If a player removes all the mat hes from
one pile |that is, applies the winning strategy for the individual game| , the opponent
wins by removing the remaining mat hes in the other pile.
The symmetry between left and right allows us to easily spot a winning strategy.
Suppose we let m and n denote the number of mat hes in the two piles. In the end
position, there is an equal number of mat hes in both piles, namely 0 . That is, in the
end position, m = n = 0 . This suggests that the losing positions are given by m = n .
This is indeed the ase. From a position in whi h m = n , and a move is possible (that
is, either 1 m or 1 n ), any move will be to a position where m 6= n . Subsequently,
hoosing the pile with the larger number of mat hes, and removing the ex ess mat hes
from this pile, will restore the property that m = n .
Formally, the orre tness of the winning strategy is expressed by the following sequen e of assertions and program statements.
{
if

m = n (m 6= 0 n 6= 0)

1 m redu e m

1 n redu e n
fi
;

{
if

m 6= n

m < n n := n (nm)

n < m m := m (mn)
fi
{

m=n

The non-deterministi hoi e between redu ing m , in the ase that 1 m , and redu ing
n , in the ase that 1 n , models an arbitrary hoi e of move in the sum game. The
fa t that either m hanges in value, or n hanges in value, but not both, guarantees
m 6= n after ompletion of the move.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

55

4.4. Sums of Games

The property m 6= n is the pre ondition for the winning strategy to be applied.
Equivalently, m < n or n < m . In the ase that m < n , we infer that 1 nm n , so
that nm mat hes an be removed from the pile with n mat hes. Sin e, n(nm)
simpli es to m , it is lear that, after the assignment n := n(nm) , the property
m = n will hold. The ase n < m is symmetri .
The following sequen e of assertions and program statements summarises the argument just given for the validity of the winning strategy. Note how the two assignments
have been annotated with a pre ondition and a post ondition. The pre ondition expresses the legitima y of the move; the post ondition is the losing property that the
strategy is required to establish.
{

m 6= n

m<n n<m

if

}
}

m < n { 1 nm n } n := n (nm) { m = n }

n < m { 1 mn m } m := m (mn) { m = n }
fi
{

4.4.2

m=n

Keep It Symmetrical!

The game in se tion 4.4.1 is another example of the importan e of symmetry; the winning
strategy is to ensure that the opponent is always left in a position of symmetry between
the two individual omponents of the sum-game. We see shortly that this is how to win
all sum-games, no matter what the individual omponents are.
There are many examples of games where symmetry is the key to winning. Here is a
ouple. The solutions an be found at the end of the book.
The Daisy Problem Suppose a daisy has 16 petals arranged symmetri ally around

its entre. There are two players. A move involves removing one petal or two adja ent
petals. The winner is the one who removes the last petal. Who should win and what
is the winning strategy? Generalise your solution to the ase that there are initially n
petals and a move onsists of removing between 1 and M adja ent petals (where M is
xed in advan e of the game).
The Coin Problem Two players are seated at a re tangular table whi h initially is

bare. They ea h have an unlimited supply of ir ular oins of varying diameter. The
players take it in turns to pla e a oin on the table, su h that it does not overlap any

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

56

4. Games

Figure 4.5: A 16 -petal daisy


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?

4.4.3

More Simple Sums

Let us return to our mat hsti k games. A variation on the sum game in se tion 4.4.1 is
to restri t the number of mat hes that an be removed. Suppose the restri tion is that
at most K mat hes an be removed from either pile (where K is xed, in advan e).
The e e t of the restri tion is to disallow some winning moves. If, as before, m and
n denote the number of mat hes in the two piles, it is not allowed to remove mn
mat hes when K < mn . Consequently, the property m = n no longer hara terises the
losing positions. For example, if K is xed at 1 , the position in whi h one pile has two
mat hes whilst the se ond pile has no mat hes is a losing position: in this position a
player is for ed to move to a position in whi h one mat h remains; the opponent an
then remove the mat h to win the game.
A more signi ant e e t of the restri tion seems to be that the strategy of establishing
symmetry is no longer appli able. Worse is if we break symmetry further by imposing
di erent restri tions on the two piles: suppose, for example, we impose the limit M on
the number of mat hes that may be removed from the left pile, and N on the number of
mat hes that may be removed from the right pile, where M 6= N . Alternatively, suppose
the left and right games are ompletely di erent, for example, if one is a mat hsti k game
and the other is the daisy game. If this is the ase, how is it possible to maintain symmetry? Nevertheless, a form of \symmetry" is a key to the winning strategy: symmetry

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

57

4.4. Sums of Games

is too important to abandon so easily!


We saw, in se tion 4.2, that the way to win the one-pile 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 two-pile game, \symmetry" between the piles
is formulated as the property that
m mod (M+1) = n mod (N+1) .

( 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 satis ed. Also, the property an always be maintained following
an arbitrary move by the opponent, as given by the following annotated program segment.
{
if

m mod (M+1) = n mod (N+1) (m 6= 0 n 6= 0) }


1 m redu e m by at most M

1 n redu e n by at most N
fi
;

m mod (M+1) 6= n mod (N+1) }

if m mod (M+1) < n mod (N+1) n := n (n mod (N+1) m mod (M+1))


n mod (N+1) < m mod (M+1) m := m (m mod (M+1) n mod (N+1))
fi
{

m mod (M+1) = n mod (N+1) }

(Note: we dis uss later the full details of how to he k the assertions made in this
program segment.)

4.4.4

The MEX Function

The idea of de ning \symmetri " to be \the respe tive remainders are equal" an be
generalised to an arbitrary sum game.
Consider a game that is the sum of two games. A position in the sum game is a pair
( l,r ) where l is a position in the left game, and r is a position in the right game. A
move a e ts just one omponent; so, a move is modelled by either a (guarded) assignment

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

58

4. Games

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 de ne 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 position ( l,r ) satisfying L.l 6= R.r | . That is,
{
if

L.l = R.r (l is not an end position r is not an end position ) }


l is not an end position hange l

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 ) satisfying L.l 6= R.r | should result in a losing position |a position ( l,r ) satisfying L.l = R.r |.
That is,
{

L.l 6= R.r }

apply winning strategy


{

L.l = R.r }

We an satisfy the rst and se ond requirements if we de ne L and R to be fun tions


with range the set of natural numbers, and require that:

 For end positions l and r of the respe tive games, L.l = 0 = R.r .
 For every l su h that there is a move from l to l in the left game, L.l 6= L.l .

Similarly, for every r su h that there is a move from r to r in the right game,
R.r 6= R.r .

Note that the hoi e of the natural numbers as range of the fun tions, and the hoi e
of 0 as the fun tions' value at end positions is quite arbitrary. The advantage of this
hoi e arises from the third requirement. If L.l and R.r are di erent natural numbers,

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

59

4.4. Sums of Games

either L.l < R.r or R.r < L.l . This allows us to re ne 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

R.r < L.l hange l


fi
{

L.l = R.r }

For this to work, we require that:

 For any number m less than R.r , it is possible to move from r to a position r

su h that R.r = m . Similarly, for any number n less than L.l , it must be possible
to move from l to a position l su h that L.l = n .

The bulleted requirements are satis ed if we de ne the fun tions L and R to be the
so- alled \mex" fun tion. The pre ise de nition 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 de ned 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 .

4.4.5

Using the MEX Function

We use the game depi ted in g. 4.4 to illustrate the al ulation of mex numbers. Figure
4.6 shows the mex numbers of ea h of the nodes in their respe tive games.
The graphs do not have any systemati stru ture; onsequently, the only way to
ompute the mex numbers is by a brute-for e sear h of all positions. This is easily done
by hand. The end positions are ea h given mex number 0 . Subsequently, a mex number
an be given to a node when all its su essors have already been given a mex number.
(A su essor of a node p is a node q su h that there is an edge from p to q .) The

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

60

4. Games

Figure 4.6: Mex Numbers. The mex number of a node is the smallest natural number
not in luded among the mex numbers of its su essors.
number is, by de nition, 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 di erent 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
di erent from 2 . The rst player then repeats the strategy of ensuring that the mex
numbers are equal, until eventually the opponent an move no further.
Note that, be ause of the la k of stru ture of the individual games, we have to sear h
through all 15 positions of the left game and all 11 positions of the right game, in order
to al ulate the mex numbers of ea h position. In total, therefore, we have to sear h

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

61

4.4. Sums of Games

Figure 4.7: Computing mex numbers. The unlabelled node is given the mex number 2 .
through 26 di erent 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 e ort. Moreover, the saving grows as the size of the omponent games
in reases.
a) Constru t the subtra tion set game where there is one pile of mat hes
from whi h at most 2 , 5 or 6 mat hes may be removed. Cal ulate the mex number for
ea h position until you spot a pattern.
Give a general rule for omputing the mex number of a pile of m mat hes in this
game.
Exercise 4.4

b) Consider a game whi h is the sum of two games. In the left game, 1 or 2 mat hes
may be removed at ea h turn. In the right game, 2 , 5 or 6 mat hes may be removed.
In the sum game, a move is made by hoosing to play in the left game, or hoosing to
play in the right game.
The table below shows a number of di erent positions in this game. A position is
given by a pair of numbers: the number of mat hes in the left pile, and the number of
mat hes in the right pile.
Left Game Right Game \losing" or winning move
10
20
?
20
20
?
15
5
?
6
9
?
37
43
?
Table 4.3: Fill in entries marked \?"
For ea h position, state whether it is a winning or a losing position. For winning
positions, give the winning move in the form X m where \X" is one of \L" (for \left
game") or \R" (for right game), and m is the number of mat hes to be removed.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

62

4. Games

4.5

Summary

This hapter has been about determining winning strategies in simple two-person games.
The underlying theme of the hapter has been problem spe i ation. We have seen how
winning and losing positions are spe i ed. A pre ise, formal spe i ation enabled us to
formulate a brute-for e sear h pro edure to determine whi h positions are whi h.
Brute-for e sear h is only advisable for small, unstru tured problems. The analysis
of the \sum" of two games exempli es 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 e ort
needed to determine winning and losing positions in the \sum" of two games, ompared
to a brute-for e sear h.
Game theory is a ri h, well-explored area of Mathemati s, whi h we have only tou hed
upon in this hapter. It is a theory that is be oming in reasingly important in Computing S ien e. One reason for this is that problems that beset software design, su h as the
se urity of a system, are often modelled as a game, with the user of the software as the
adversary. Another reason is that games often provide ex ellent examples of \ omputational omplexity"; it is easy to formulate games having very simple rules but for whi h
no e ient algorithm implementing the winning strategy is known.
Mex numbers were introdu ed by Sprague and Grundy to solve the \Nim" problem,
and mex numbers are sometimes alled \Sprague-Grundy" numbers, after their originators. Nim is a well-known mat hsti k game involving three piles of mat hes. We have not
developed the theory su iently, in this hapter, to show how Nim, and sums of more
than two games, are solved using mex numbers. (What is missing is how to ompute the
mex number of the sum of two games.)

4.6

Bibliographic Remarks

The two-volume book \Winning Ways" [BCG82 by Berlekamp, Conway and Guy is the
bible of game theory.
The 31st De ember game (exer ise 4.1) is adapted from [DW00.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Chapter 5
Knights and Knaves
The island of knights and knaves is a tional island that is often used to test students'
ability to reason logi ally. The island has two types of natives, \knights" who always
tell the truth, and \knaves" who always lie. Logi puzzles involve dedu ing fa ts about
the island from statements made by its natives without knowing whether or not the
statements are made by a knight or a knave.
The temptation is to solve su h problems by ase analysis |in a problem involving n
natives, onsider the 2n di erent ases obtained by assuming that the individual natives
are knights or knaves| . Case analysis is a lumsy way of ta kling the problems. In
ontrast, these, and similar logi puzzles, are easy exer ises in the use of al ulational
logi , whi h we introdu e in this hapter.

5.1

Logic Puzzles

Here is a typi al olle tion of knights-and-knaves puzzles.


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
(a) Can it be determined whether the native is a knight or a knave?
(b) Can it be determined whether there is gold on the island?

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.

63

January 7, 2007

64

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?
5. Devise a question that allows you to determine whether a native is a knight.
6. What question should you ask A to determine whether B is a knight?
7. What question should you ask A to determine whether A and B are the same type
(i.e. both knights or both knaves)?
8. You would like to determine whether an odd number of A, B and C is a knight.
You may ask one yes/no question to any one of them. What is the question you
should ask?
9. A tourist omes to a fork in the road, where one bran h leads to a restaurant and
one doesn't. A native of the island is standing at the fork. Formulate a single
yes/no question that the tourist an ask su h that the answer will be yes if the left
fork leads to the restaurant, and otherwise the answer will be no.

5.2
5.2.1

Calculational Logic
Propositions

The algebra we learn at s hool is about al ulating with expressions whose values are
numbers. We learn, for example, how to manipulate an expression like m2n2 in order
to show that its value is the same as the value of (m+n)(mn) , independently of the
values of m and n . We say that m2n2 and (m+n)(mn) are equal, and write
m2n2 = (m+n)(mn) .

The basis for these al ulations is a set of laws. Laws are typi ally primitive, but general,
equalities between expressions. They are \primitive" in the sense that they annot be
broken down into simpler laws, and they are \general" in the sense that they hold
independently of the values of any variables in the onstituent expressions. We all them
axioms. Two examples of axioms, both involving zero are:
n+0 = n ,

and
nn = 0 ,

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

65

5.2. Cal ulational Logi

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) ,

whi h is true for all m , n and p .


(Cal ulational) logi is about al ulating with expressions whose values are so- alled
\booleans" | that is, either true or false . Examples of su h expressions are \it is
sunny" (whi h is either true or false depending on to when and where \it" refers) ,
n = 0 (whi h is either true or false depending on the value of n ), and n < n+1 (whi h
is true for all numbers n ). Boolean-valued expressions are alled propositions . Atomi
propositions are propositions that annot be broken down into simpler propositions. The
three examples above are all atomi . A non-atomi proposition would be, for example,
m < n < p , whi h an be broken down into the so- alled onjun tion of m < n and
n<p.
Logi is not on erned with the truth or otherwise of atomi propositions; that is the
on ern of the problem domain being dis ussed. Logi is about rules for manipulating
the logi al onne tives | the operators like \and", \or", and \if" that are used to
ombine atomi propositions.
Cal ulational logi pla es emphasis on equality of propositions, in ontrast to other
axiomatisations of logi , whi h emphasise logi al impli ation (if . . . then . . . ). Equality
is the most basi on ept of logi |a fa t rst re ognised by Gottfried Wilhelm Leibniz,
who lived from 1646 to 1716 and who was the rst to try to formulate logi al reasoning|
and equality of propositions is no ex eption. We see shortly that equality of propositions
is parti ularly spe ial, re ognition of whi h onsiderably enhan es the beauty and power
of reasoning with propositions.

5.2.2

Knights and Knaves

Equality of propositions is entral to solving puzzles about knights and knaves. Re all
that a knight always tells the truth, and a knave always lies. If A is a native of the island,
the statement \A is a knight" is either true or false , and so is a proposition. Also, the
statements made by the natives are propositions. A statement like \the restaurant is to
the left" is either true or false. Suppose A denotes the proposition \A is a knight", and
suppose native A makes a statement S. Then, the ru ial observation is that the values
of these two propositions are the same. That is,
A=S .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

66

5. Knights and Knaves

For example, if A says \the restaurant is to the left", then


A=L ,

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 on rms 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 di erent values of A
and B .
The al ulational properties of equality of booleans are dis ussed in the next se tion
before we return again to the knights and knaves.

5.2.3

Boolean Equality

Equality |on any domain of values| has a number of hara teristi properties. First, it
is re exive . That is x = x whatever the value (or type) of x . Se ond, it is symmetri .
That is, x = y is the same as y = x . Third, it is transitive . That is, if x = y and y = z
then x = z . Finally, if x = y and f is any fun tion then f.x = f.y (where the in x dot
denotes fun tion appli ation). This last rule is alled substitution of equals for equals
or Leibniz's rule .
Equality is a binary relation. When studying relations, re exivity, symmetry and
transitivity are properties that we look out for. Equality is also a fun tion. It is a
fun tion with range the boolean values true and false . When we study fun tions, the

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

67

5.2. Cal ulational Logi

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 .

They are also both symmetri : for all x and y ,


x+y = y+x

and
xy = yx .

Symmetry of equality, viewed as a fun tion, is just the same as symmetry of equality,
viewed as a relation. But, what about asso iativity of equality? Is equality an asso iative
operator?
The answer is that, in all but one ase, the question doesn't make sense. Asso iativity
of a binary fun tion only makes sense if the domains of its two arguments and the range
of its result are all the same. The expression (p = q) = r just doesn't make sense when
p , q and r are numbers, or hara ters, or sequen es, et . The one ex eption is equality
of boolean values. When p , q and r are all booleans it makes sense to ompare the
boolean p = q with r for equality. That is, (p = q) = r is a meaningful boolean value.
Similarly, so too is p = (q = r) . It also makes sense to ompare these two values for
equality. In other words, it makes sense to ask whether equality of boolean values is
asso iative | and, perhaps surprisingly, it is. That is, for all booleans p , q and r ,
(5.1)

[Associativity]

((p = q) = r) = (p = (q = r)) .

You should he k this property by onstru ting truth tables for (p = q) = r and for
p = (q = r) and omparing the entries. You should observe that the entries for whi h
(p = q) = r is true are those for whi h an odd number of p , q and r is true . If two of
the three are true, and the third is false , (p = q) = r is also false .
The asso iativity of equality is a very powerful property, for one be ause it enhan es
e onomy of expression. We will see several examples; an elementary example is the
following.
The re exivity of equality is expressed by the rule
(p = p) = true .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

68

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.

5.2.4

Hidden Treasures

We an now return to the island of knights and knaves, and dis over the hidden treasures.
Let us onsider the rst problem posed in se tion 5.1. What an we dedu e if a native
says \I am a knight equals there is gold on the island"? Let A stand for \the native is
a knight" and G stand for \there is gold on the island". Then the native's statement is
A = G , and we dedu e that
A = (A = G)

is true. So,
true
=

A's statement }

A = (A = G)
=

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.

January 7, 2007

69

5.2. Cal ulational Logi

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 simpli ed.

5.2.5

Equals for Equals

Equality is distinguished from other logi al onne tives by Leibniz's rule: if two expressions are equal, one expression an be substituted for the other. Here, we onsider one
simple example of the use of Leibniz's rule.
Suppose there are three natives of the island, A, B and C, and C says \A
and B are both the same type". Formulate a question that, when posed to
A, determines whether C is telling the truth.
To solve this problem, we let A , B and C denote the propositions A (respe tively,
B and C) is a knight. We also let Q be the unknown question.
The response we want is C . So, by the analysis in se tion 5.2.4, Q = (A = C) . But,
C's statement is A = B . So we know that C = (A = B) . Substituting equals for equals,
Q = (A = (A = B)) . But, A = (A = B) simpli es 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.

January 7, 2007

70

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) = B

true = B
=

{
B .

5.3

Equivalence and Continued Equalities

Asso iative fun tions are usually denoted by in x operators1 . The bene t in al ulations
is immense. If a binary operator is asso iative (that is, (xy)z = x(yz) for
all x , y and z ), we an write xyz without fear of ambiguity. The expression
be omes more ompa t be ause of the omission of parentheses. More importantly, the
expression is unbiased; we may hoose to simplify xy or yz depending on whi h is
the most onvenient. If the operator is also symmetri (that is, xy = yx for all x
and y ) the gain is even bigger, be ause then, if the operator is used to ombine several
subexpressions, we an hoose to simplify uw any pair of subexpressions u and w .
In x notation is also often used for binary relations. We write, for example, 0 m n .
Here, the operators are being used onjun tionally : the meaning is 0 m and m n .
In this way, the formula is more ompa t (sin e m is not written twi e). More importantly, we are guided to the inferen e that 0 n . The algebrai property that is being
hidden here is the transitivity of the at-most relation. If the relation between m and n
is m < n rather than m n and we write 0 m < n , we may infer that 0 < n . Here, the
inferen e is more omplex sin e there are two relations involved. But, it is an inferen e
that is so fundamental that the notation is designed to fa ilitate its re ognition.
In the ase of equality of boolean values, we have a dilemma. Do we understand
equality as a relation and read a ontinued expression of the form
x=y=z

in x operator is a symbol used to denote a fun tion of two arguments that is written between the
two arguments. The symbols \ + " and \ " are both in x operators, denoting addition and multipli ation,
respe tively.
1 An

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

71

5.3. Equivalen e and Continued Equalities

as asserting the equality of all of x , y and z ? Or do we read it \asso iatively" as


(x = y) = z ,

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 di erent onvention.
The solution to this dilemma is to use two di erent symbols to denote equality of
boolean values | the symbol \ = " when the transitivity of the equality relation is to be
emphasised and the symbol \ " when its asso iativity is to be exploited. A ordingly,
we write both p = q and p q . When p and q are expressions denoting boolean
values, these both mean the same. But a ontinued expression
pqr ,

omprising more than two boolean expressions onne ted by the \ " symbol, is to
be evaluated asso iatively |i.e. as (p q) r or p (q r) , whi hever is the most
onvenient| whereas a ontinued expression
p=q=r

is to be evaluated onjun tionally |i.e as p = q and q = r | . More generally, a


ontinued equality of the form
p1 = p2 = . . . = pn

means that all of p1 , p2 , . . . , pn are equal, whilst a ontinued equivalen e of the


form
p1 p2 . . . pn

has the meaning given by fully parenthesising the expression (in any way whatsover,
sin e the out ome is not a e ted) and then evaluating the expression as indi ated by the
hosen parenthesisation.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

72

5. Knights and Knaves

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.
(5.2)

[Reflexivity]

5.3.1

true p p .

Examples of the Associativity of Equivalence

This se tion ontains a ouple of beautiful examples illustrating the e e tiveness of the
asso iativity of equivalen e.
Even and Odd Numbers The rst example is the following property of the predi ate
even on numbers. (A number is even exa tly when it is a multiple of two.)
m+n is even m is even n is even .

It will help if we refer to whether or not a number is even or odd as the parity of the
number. Then, if we parenthesise the statement as
m+n is even (m is even n is even) ,

it states that the number m+n is even exa tly when the parities of m and n are the
same. Parenthesising it as
(m+n is even m is even) n is even ,

it states that the operation of adding a number n to a number m does not hange the
parity of m exa tly when n is even.
Another way of reading the statement is to use the fa t that, in general, the equivalen e p q r is true exa tly when an odd number of p , q and r is true. So the
property aptures four di erent ases:
or
or
or

(( m+n
(( m+n
(( m+n
(( m+n

is even)
is odd)
is odd)
is even)

and
and
and
and

(m
(m
(m
(m

is even)
is odd)
is even)
is odd)

and
and
and
and

(n
(n
(n
(n

is even))
is even))
is odd))
is odd)) .

The beauty of this example lies in the avoidan e of ase analysis. There are four
distin t ombinations of the two booleans \ m is even" and \ n is even". Using the
asso iativity of equivalen e the value of \ m+n is even" is expressed in one simple
formula, without any repetition of the omponent expressions, rather than as a list of
di erent ases. Avoidan e of ase analysis is vital to e e tive reasoning.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

5.3. Equivalen e and Continued Equalities

73

Sign of Non-Zero Numbers The sign of a number says whether or not the number
is positive. For non-zero numbers x and y , the produ t xy is positive if the signs of
x and y are equal. If the signs of x and y are di erent, the produ t xy is negative.
Assuming that x and y are non-zero, this rule is expressed as
xy is positive x is positive y is positive .

Just as for the predi ate even, this one statement neatly aptures a number of di erent
ases, even though no ase analysis is involved. Indeed, our justi ation of the rule is
the statement
xy is positive (x is positive y is positive) .

The other parenthesisation |whi h states that the sign of x is un hanged when it is
multiplied by y exa tly when y is positive| is obtained \for free" from the asso iativity
of boolean equality.

5.3.2

On Natural Language

Many mathemati ians and logi ians are not aware that equality of booleans is asso iative;
those that do are often unaware or dismissive of how e e tive its use an be. At the
present time, there is onsiderable resistan e to a shift in fo us from impli ation to
equality. Most ourses on logi introdu e boolean equality as \if and only if". It is akin
to introdu ing equality of numbers by rst introdu ing the at-most ( ) and at-least
( ) relations, and then de ning an \at-most and at-least" operator.
The most probable explanation lies in the fa t that many logi ians view the purpose
of logi as formalising \natural" or \intuitive" reasoning, and our \natural" tenden y
is not to reason in terms of equalities, but in ausal terms. (\If it is raining, I will
take my umbrella.") The equality symbol was rst introdu ed into mathemati s by
Robert Re orde in 1557, whi h, in the history of mathemati s, is quite re ent; were
equality \natural" it would have been introdu ed mu h earlier. Natural language has
no ounterpart to a ontinued equivalen e. To take a on rete example, the ontinued
equivalen e \a blind man an see through two eyes equivales a blind man an see through
one eye equivales a blind man an see through no eyes" may seem very odd, if not
nonsensi al, even though it is a tually true!
This fa t should not be a deterrent to the use of ontinued equivalen e. At one
time (admittedly, a very long time ago) there was probably similar resistan e to the
introdu tion of ontinued additions and multipli ations. The eviden e is still present
in the language we use today. For example, the most ommon way to express time is
in words: like \quarter to ten" or \ten past eleven". Cal ulational requirements (eg.
wanting to determine how long is it before the train is due to arrive) have in uen ed

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

74

5. Knights and Knaves

natural language so that, nowadays, people sometimes say, for example, 9:45 or 11:10 in
everyday spee h. But, we still don't nd it a eptable to say 10:70! Yet, this is what we
a tually use when we want to al ulate the time di eren e between 9.45 and 11:10. In
fa t, several laws of arithmeti , in luding asso iativity of addition, are fundamental to
the al ulation. Changes in natural language have o urred, and will ontinue to o ur,
as a result of progress in mathemati s, but will always lag a long way behind. The
language of mathemati s has developed in order to over ome the limitations of natural
language. The goal is not to mimi \natural" reasoning, but to provide a more e e tive
alternative.

5.4

Negation

Consider the following knights-and-knaves problem. There are two natives, A and B.
Native A says, \B is a knight equals I am not a knight". What an you determine about
A and B?
This problem involves a so- alled negation : the use of \not". Negation is a unary
operator (meaning that it is a fun tion with exa tly one argument) mapping a boolean
to a boolean, and is denoted by the symbol \ ", written as a pre x 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) ,

it fun tions as a de nition of negation. Reading it the other way:


(p p) = false

it provides a way of simplifying propositional expressions. In addition, the symmetry


of equivalen e means that we an rearrange the terms in a ontinued equivalen e in any
order we like. So, we also get the property:
p = (p false) .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

75

5.4. Negation

Returning to the knights-and-knaves problem, we are given that:


A B A .

This simpli es to B as follows:


A B A
=

rearranging terms }

A A B
=

law (5.3) with p := A

false B
=

law (5.3) with p := B and rearranging }

B .

So, B is a knave, but A ould be a knight or a knave. Note how (5.3) is used in two
di erent ways.
The law (5.3), in onjun tion with the symmetry and asso iativity of equivalen e,
provides a way of simplifying ontinued equivalen es in whi h one or more terms are
repeated and/or negated. Suppose, for example, we want to simplify
p p q p r q .

We begin by rearranging all the terms so that repeated o urren es of \ p " and \ q " are
grouped together. Thus we get
p p p q q r .

Now we an use (5.2) and (5.3) to redu e the number of o urren es of \ p " and \ q " to
at most one (possibly negated). In this parti ular example we obtain
true p false r .

Finally, we use (5.2) and (5.3) again. The result is that the original formula is simpli ed
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

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

76

5. Knights and Knaves

is simpli ed to
q + 2r

by ounting all the o urren es of p , q and r , an o urren e of p an elling out an


o urren e of p . Again, the details are di erent but the pro ess is essentially identi al.
The two laws (5.2) and (5.3) are all that is needed to de ne the way that negation
intera ts with equivalen e; using these two laws we an derive several other laws. A
simple example of how these two laws are ombined is a proof that false = true :
false
=

law p p false with p := false

false false
=

law true p p with p := false

true .

5.5

Contraposition

A rule that should now be obvious, but whi h is surprisingly useful, is the rule we all
ontraposition 2 .
(5.4)

[Contraposition]

p q p q .

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
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


2 Other

tion.

authors use the name \ ontraposition" for a less general rule ombining negation with impli a-

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

77

5.5. Contraposition
(even.n l)[n , l := n+1 , l]
=

rule of substitution }

even.(n+1) l
=

even.(n+1) (even.n)

(even.n) l
=

ontraposition }

even.n l .

We are given that, initially, the boat is on the left side. Sin e zero is an even number,
we on lude that even.n l is invariantly true . In words, the boat is on the left side
equivales the number of rossings is even.
Another example is the following. Suppose it is required to move a square arm hair
sideways by a distan e equal to its own width. (See gure 5.1.) However, the hair is
so heavy that it an only be moved by rotating it through 90 around one of its four
orners. Is it possible to move the hair as desired? If so, how? If not, why not?

Figure 5.1: Moving a heavy arm hair.


The answer is that it is impossible. Suppose the arm hair is initially positioned along
a north-south 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 north-south position on a bla k square to a
north-south position on a white square.
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 north-south and false for east-west). Then, rotating the arm hair
about any orner is represented by the assignment:
col , dir := col , dir

The rule of ontraposition states that an invariant of this assignment is


col dir .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

78

5. Knights and Knaves

So, the value of this expression will remain equal to its initial value, no matter how many
times the arm hair is rotated. But, moving the arm hair sideways one square hanges
the olour but does not hange the dire tion. That is, it hanges the value of col dir ,
and is impossible to a hieve by ontinually rotating the arm hair as pres ribed.
In words, an invariant of rotating the arm hair through 90 around a orner point is
the hair is on a bla k square the hair is fa ing north-south
whi h is false when the hair is on a white square and fa ing north-south.

90

Figure 5.2: Invariant when moving a heavy arm hair.

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
pla e up or down. A hessboard is an 88 grid of squares.
Show that it is impossible to move a knight from the bottom-left orner of a hessboard to the top-right 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 e e 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.
Exercise 5.5 (Knights Move)

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

79

5.6. Handshake Problems

5.6

Handshake Problems

Logi al properties of negation are fundamental to solving so- alled handshake problems .
The simplest example of a handshake problem is this: Suppose that at a party, some
people shake hands and some don't. Suppose ea h person ounts the number of times
they shake hands. Show that at least two people have the same ount.
Cru ial to how we solve this problem are the properties of shaking hands. These are
that it is a binary relation ; it is symmetri , and it is anti-re exive . It being a \binary
relation" on people, means that, for any two people |Ja k and Jill, say| Ja k shakes
hands with Jill is either true or false. (In general, a relation is any boolean-valued
fun tion. Binary means that it is a fun tion of two arguments.) It being a \symmetri "
relation means that, for any two people |Ja k and Jill, say| Ja k shakes hands with
Jill equivales Jill shakes hands with Ja k. Finally, it being \anti-re exive" means that
no-one shakes hands with themselves.
We are required to show that (at least) two people shake hands the same number
of times. Let us explore the onsequen es of the properties of the shake-hands relation
with respe t to the number of times that ea h person shakes hands.
Suppose there are n people. Then, everyone shakes hands with at most n people.
However, the anti-re exivity property is that noone shakes hands with themselves. We
on lude that everyone shakes hands with between 0 and n1 people.
There are n numbers in the range 0 to n1 . The negation of \two people shake
hands the same number of times" is \everyone shake hands a distin t number of times".
In parti ular, someone shakes hands 0 times and someone shakes hands n1 times.
The symmetry of the shake-hands relation makes this impossible.
Suppose we abbreviate \shake hands" to S , and suppose we use x and y to refer
to people. In this way, xSy is read as \person x shakes hands with person y ", or just
x shakes hands with y . Then the symmetry of \shakes hands" gives us the rule, for all
x and y ,
xSy ySx .

The ontrapositive of this rule is that, for all x and y ,


(xSy) (ySx) .

In words, x doesn't shake hands with y equivales y doesn't shake hands with x . Now,
suppose person a shakes hands with noone and person b shakes hands with everyone.
Then, in parti ular, a does not shake hands with b , i.e. (aSb) , and b shakes hands
with a , i.e. bSa . But then, substituting equals for equals, we have both (aSb) and
aSb , whi h is false.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

80

5. Knights and Knaves

The assumption that everyone shakes hands with a distin t number of people has
led to a ontradi tion, and so we on lude that two people must shake hands the same
number of times.
Note arefully how the symmetry and anti-re exivity of the shakes-hands relation
are ru ial. Were we to onsider a similar problem involving a di erent relation, the
out ome might be di erent. For example, if we repla e \shake hands" by some other
form of greeting like \bows or urtsies", whi h is not symmetri , the property need not
hold3. (Suppose there are two people, and one bows to the other, but the greeting is
not returned.) However, if \shake hands" is repla ed by \rub noses", the property does
hold. Like \shake hands", \rub noses" is a symmetri and anti-re exive relation.
Here is another handshaking problem. It's a bit more di ult to solve,
but the essen e of the problem remains the same: \shake hands" is a symmetri relation,
as is \don't shake hands".
Suppose a number of ouples (husband and wife) attend a party. Some people
shake hands, others do not. Husband and wife never shake hands. One person, the
\host", asks everyone else how many times they have shaken hands, and gets a different answer every time. How many times did the host and the host's partner shake
hands?
Exercise 5.6

5.7

Inequivalence

In the knights-and-knaves problem mentioned at the beginning of se tion 5.4, A might


have said \B is di erent 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.
(B A)
=

the law p p false with p := (B A)

B A false
=

the law p p false with p := A

B A .
3 At

the time of writing, anyone meeting the British Queen is required to bow or urtsey, whereas the
Queen never does. The relation is not symmetri .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

81

5.7. Inequivalen e

We have thus proved, for all propositions p and q ,


(5.7)

[Inequivalence]

(p q) p q .

Note how asso iativity of equivalen e has been used silently in this al ulation. Note
also how asso iativity of equivalen e in the summary of the al ulation gives us two
properties for the pri e of one. The rst is the one proved dire tly:
(p q) = (p q) ,

the se ond omes free with asso iativity:


((p q) p) = q .

The proposition (p q) is usually written p 6 q . The operator is alled inequivalen e


(or ex lusive-or , abbreviated xor ). Inequivalen e is also asso iative:
(p 6 q) 6 r
=

de nition of \ 6 ", applied twi e }

((p q) r)
=

(5.7), with p,q := (p q) , r

(p q) r
=

ontraposition (5.4), with p,q := p q , r

p q r
=

ontraposition (5.4), with p,q := p , q r

p (q r)
=

(5.7), with p,q := p , q r

(p (q r))
=

de nition of \ 6 ", applied twi e }

p 6 (q 6 r) .

As a result, we an write the ontinued inequivalen e p 6 q 6 r without fear of ambiguity4. Note that, as a byprodu t, we have shown that p 6 q 6 r and p q r are
equal.
As a nal worked example, we show that inequivalen e asso iates with equivalen e:
4 This

is to be read asso iatively, and should not be onfused with p 6= q 6= r , whi h some authors
o asionally write. Inequality is not transitive, so su h expressions should be avoided.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

82

5. Knights and Knaves


(p 6 q) r
=

expanding the de nition 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)
=

de nition of q 6 r

p (q 6 r) .

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 di eren e.)

Exercise 5.8

(a) false 6 false 6 false


(b) true 6 true 6 true 6 true
(c) false 6 true 6 false 6 true
(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

Exercise 5.9

Prove that true = false

Exercise 5.10 (Double Negation)

Prove the rule of double negation

p = p .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

83

5.8. Summary

Exercise 5.11 (Encryption)

The fa t that inequivalen e is asso iative, that is

(p 6 (q 6 r)) ((p 6 q) 6 r) ,

is used to en rypt data. To en rypt a single bit b of data, a key a is hosen and the
en rypted form of b that is transmitted is a 6 b . The re eiver de rypts the re eived
bit, c , using the same operation5. That is, the re eiver uses the same key a to ompute
a 6 c . Show that, if bit b is en rypted and then de rypted in this way, the result is b
independently of the key a .

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 di erent
types?
Exercise 5.12

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
ine e tive ase analysis that is often seen in solutions to logi puzzles.
The asso iativity of equivalen e an be di ult to get used to, parti ularly if one
tries to express its properties in natural language. However, this should not be used
as an ex use for ignoring it. The painful, enturies-long pro ess of a epting zero as a
number, and introdu ing the symbol \ 0 " to denote it, provides ample eviden e that the
adheren e to \natural" modes of reasoning is a major impediment to e e tive reasoning.
The purpose of a al ulus is not to mimi \natural" or \intuitive" reasoning, but to
provide a more powerful alternative.
The fa t that equality of boolean values is asso iative has been known sin e at least the
1920's , having been mentioned by Alfred Tarski in his PhD thesis, where its dis overy is
5 This

operation is usually alled \ex lusive-or" in texts on data en ryption; it is not ommonly known
that ex lusive-or and inequivalen e are the same. Inequivalen e an be repla ed by equivalen e in the
en ryption and de ryption pro ess. But, very few s ientists and engineers are aware of the algebrai
properties of equivalen e, and this possibility is never exploited!

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

84

5. Knights and Knaves

attributed to J. Lukasiewi z. (See the paper \On the primitive term of logisti " [Tar56;
Tarski is a famous logi ian.) Nevertheless, its usefulness was never re ognised until
brought to the fore by E.W. Dijkstra in his work on program semanti s and mathemati al
method. (See e.g. [DS90.)
The origin of the logi puzzles is Raymond Smullyan's book \What Is The Name Of
This Book?" [Smu78. This is an entertaining book whi h leads on from simple logi
puzzles to a dis ussion of the logi al paradoxes and Godel's unde idability theorem.
But Smullyan's proofs invariably involve detailed ase analyses. The exploitation of the
asso iativity of equivalen e in the solution of su h puzzles is due to Gerard Wiltink
[Wil87. For a omplete a ount of al ulational logi , whi h in ludes dis ussion of
onjun tion (\and"), disjun tion (\or"), follows-from (\if") and impli ation (\only if"),
see [Ba 03 or [GS93.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Chapter 6
Induction
\Indu tion" is the name given to a problem-solving te hnique based on using the solution
to small instan es of a problem to solve larger instan es of the problem.
The idea is that we somehow measure the \size" of instan es of a problem. For
example, the problem might involve a pile of mat hsti ks, where the number of mat hes
is a parameter; an instan e of the problem is then a parti ular pile of mat hes, and its
size is the number of mat hes in the pile. A requirement is that the size is a non-negative,
whole number | thus 0 , 1 , 2 , 3 , et . We use the term natural number for a nonnegative, whole number1. Usually, how the size of an instan e of a problem is measured
is quite obvious from the problem des ription.
Having de ided how to measure size, we then solve the problem in two steps. First, we
onsider problems of size 0 . This is alled the basis of the indu tion. Almost invariably,
su h problems are very easy to solve. (They are often dismissed as \trivial".) Se ond,
we show how to solve, for an arbitrary natural number n , a problem of size n+1 , given
a solution to a problem of size n . This is alled the indu tion step.
By this pro ess, we an solve problems of size 0 . We also know how to solve problems
of size 1 ; we apply the indu tion step to onstru t a solution to problems of size 1 from
the known solution to problems of size 0 . Then, we know how to solve problems of size
2 ; we apply the indu tion step again to onstru t a solution to problems of size 2 from
the known solution to problems of size 1 . And so it goes on. We an now solve problems
of size 3 , then problems of size 4 , et .

6.1

Example Problems

All the following problems an be solved by indu tion. In the rst, the size is the number
of lines. In the se ond and third problems, it is expli tly given by the parameter n , and
1 Warning :

Mathemati ians often ex lude the number 0 from the natural numbers. There are,
however, very good reasons why 0 should always be in luded, making a break with tradition imperative.

Algorithmi Problem Solving Roland Ba khouse.

85

January 7, 2007

86

6. Indu tion

in the fourth, it is the number of disks. In ea h ase, the basis should be easy. You then
have to solve the indu tion step. We dis uss ea h problem in turn in oming se tions.
1. Cutting the Plane
A number of straight lines are drawn a ross a sheet of paper, ea h line extending
all the way from from one border to another. See g. 6.1. In this way, the paper
is divided into a number of regions. Show that it is possible to olour ea h region
bla k or white so that no two adja ent regions have the same olour (that is, so
that the two regions on opposite sides of any line segment have di erent olours).

Figure 6.1: Bla k and White Colouring.


2. Triominoes
A square pie e of paper is divided into a grid of size 2n2n , 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 L-shape made of three
grid squares. Figure 6.2 shows, on the left, an 88 grid with one square overed.
On the right is a triomino.
Show that it is possible to over the remaining squares with (non-overlapping)
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 ket-shaped 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.

January 7, 2007

87

6.1. Example Problems

Figure 6.2: Triomino Problem.

Figure 6.3: Triomino Problem. Solution to g. 6.2.


Show that it is possible to over the remaining triangles with (non-overlapping)
trapeziums. See g. 6.5 for the solution in the ase that n is 2 .
NB. In lude the ase n = 0 in your solution.
4. Towers of Hanoi
The Towers of Hanoi problem omes from a puzzle marketed in 1883 by the Fren h
 douard Lu as, under the pseudonym M. Claus.
mathemati ian E
The puzzle is based on a legend a ording to whi h there is a temple in Bramah
where there are three giant poles xed in the ground. On the rst of these poles,
at the time of the world's reation, God pla ed sixty four golden disks, ea h of
di erent size, in de reasing order of size. (See g. 6.6.) The Brahmin monks were
given the task of moving the disks, one per day, from one pole to another a ording
to the rule that no disk may ever be above a smaller disk. The monks' task will
be omplete when they have su eeded in moving all the disks from the rst of the

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

88

6. Indu tion

Figure 6.4: A Pyramid of Equilateral Triangles.

Figure 6.5: Solution to g. 6.4.


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.

January 7, 2007

89

6.2. Cutting The Plane

Figure 6.6: Towers of Hanoi Problem

6.2

Cutting The Plane

Re all the statement of the problem:


A number of straight lines are drawn a ross a sheet of paper, ea h line
extending all the way from from one border to another. See g. 6.1. In this
way, the paper is divided into a number of regions. Show that it is possible
to olour ea h region bla k or white so that no two adja ent regions have the
same olour (that is, so that the two regions on opposite sides of any line
segment have di erent olours).
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.

January 7, 2007

90

6. Indu tion

The ase where there are zero lines is easy. The sheet of paper is divided into one
region, and this an be oloured bla k or white, either olouring meeting the onditions
of a solution (be ause there is no pair of adja ent regions).
For the indu tion step, we assume that a number of lines have been drawn on the
sheet of paper, and the di erent 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 e e 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 di erent olours. The task is to show how to modify
the olouring so that it does indeed be ome a satisfa tory solution.

Figure 6.7: Cutting the Plane. Additional line shown in red.


The key to a solution is to note that inverting the olours of any satisfa tory olouring (that is, hanging a bla k region to white, and vi e-versa) 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 satisfa tory olouring). It also gives a satisfa tory olouring of the right region (be ause the

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

91

6.2. Cutting The Plane

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 di erent.
Fig. 6.8 shows the e e t on our example. Blue has been used instead of bla k in
order to make the inversion of the olours more evident.

Figure 6.8: Cutting the Plane. The olours are inverted to one side of the additional line
(bla k is shown as blue to make lear whi h olours have hanged).
This ompletes the indu tion step. In order to apply the onstru tion to an instan e
of the problem with, say, seven lines, we begin by olouring the whole sheet of paper.
Then the lines are added one-by-one. Ea h time a line is added, the existing olouring
is modi ed as pres ribed in the indu tion step, until all seven lines have been added.
The algorithm is non-deterministi in several ways. The initial olouring of the sheet
of paper (bla k or white) is unspe i ed. The ordering of the lines (whi h to add rst,
whi h to add next, et .) is also unspe i ed. Finally, whi h region is hosen as the \left"
region, and whi h the \right" region is unspe i ed. This means that the nal olouring
may be a hieved in lots of di erent ways. But that doesn't matter. The nal olouring
is guaranteed to be \satisfa tory", as required in the problem spe i ation.
Che k your understanding by onsidering variations on the problem.
Why is it required that the lines are straight? How might this assumption be relaxed
without invalidating the solution.
The problem assumes the lines are drawn on a pie e of paper. Is the solution still
valid if the lines are drawn on the surfa e of a ball, or on the surfa e of a doughnut?
We remarked that the algorithm for olouring the plane is non-deterministi . How
many di erent olourings does it onstru t?
Exercise 6.1

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

92

6.3

6. Indu tion

Triominoes

As a se ond example of an indu tive onstru tion, let us onsider the grid problem posed
in se tion 6.1. Re all the statement of the problem.
A square pie e of paper is divided into a grid of size 2n2n , where n is
a natural number. The individual squares are alled grid squares. One grid
square is overed, and the others are left un overed. A triomino is an L-shape
made of three grid squares. Show that it is possible to over the remaining
squares with (non-overlapping) triominoes.
The obvious measure of the \size" of instan es of the problem, in this ase, is the
number n . We solve the problem by indu tion on n.
The base ase is when n equals 0 . The grid then has size 2020 , i.e. 11 . That
is, there is exa tly one square. This one square is, inevitably, the one that is overed,
leaving no squares un overed. It takes 0 triominoes to over no squares! This, then, is
how the base ase is solved.
Now, suppose we onsider a grid of size 2n+12n+1 . We make the indu tion hypothesis that it is possible to over any grid of size 2n2n with triominoes if, rst, an
arbitrary grid square has been overed. We have to show how to exploit this hypothesis
in order to over a grid of size 2n+12n+1 of whi h one suare has been overed.
A grid of size 2n+12n+1 an be subdivided into 4 grids ea h of size 2n2n , simply
by drawing horizontal and verti al dividing lines through the middle of ea h side. Let
us all the four grids the bottom-left, bottom-right, top-left, and top-right grids. One
grid square is already overed. This square will be in one of the four sub-grids. We may
assume that it is in the bottom-left grid. (If not, the entire grid an be rotated about
the entre so that it does be ome the ase.)
The bottom-left grid is thus a grid of size 2n2n of whi h one square has been
overed. By the indu tion hypothesis, the remaining squares in the bottom-left grid an
be overed with triominoes. This leaves us with the task of overing the bottom-right,
top-left and top-right grids with triominoes.
None of the squares in these three grids is overed, as yet. We an apply the indu tion
hypothesis to them if just one square in ea h of the three is overed. This is done by
pla ing a triomino at the jun tion of the three grids, as shown in g. 6.9.
Now, the indu tive hypothesis is applied to over the remaining squares of the bottomright, top-left and top-right grids with triominoes. On ompletion of this pro ess, the
entire 2n+12n+1 grid has been overed with triominoes.
Exercise 6.2

Solve the trapezium problem given in se tion 6.1.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

93

6.4. Looking For Patterns

Figure 6.9: Triomino Problem. Indu tive Step. The grid is divided into four sub-grids.
The overed square, shown in bla k, identi es one sub-grid. A triomino, shown in blue,
is pla ed at the jun tion of the other three grids. The indu tion hypothesis is then used
to over all four sub-grids with triominoes.

6.4

Looking For Patterns

In se tions 6.2 and 6.3, we have seen how indu tion is used to solve problems of a
given \size". Te hni ally, the pro ess we des ribed is alled \mathemati al indu tion";
\indu tion", as it is normally understood, is more general.
\Indu tion", as used in, for example, the experimental s ien es, refers to a pro ess of
reasoning whereby general laws are inferred from a olle tion of observations. A famous
example of indu tion is the pro ess that led Charles Darwin to formulate his theory
of evolution by natural sele tion, based on his observations of plant and animal life in
remote parts of the world. In simple terms, indu tion is about looking for patterns.
Laws formulated by a pro ess of indu tion go beyond the knowledge on whi h they
are based, thus introdu ing inherently new knowledge. In the experimental s ien es,
however, su h laws are only probably true; they are tested by the predi tions they
make, and may have to be dis arded if the predi tions turn out to be false. In ontrast,
dedu tion is the pro ess of inferring laws from existing laws, whereby the dedu tions
made are guaranteed to be true provided that the laws on whi h they are based are true.
In a sense, laws dedu ed from existing laws add nothing to our sto k of knowledge sin e
they are, at best, simply reformulations of existing knowledge.
Mathemati al indu tion is a ombination of indu tion and dedu tion. It's a pro ess of looking for patterns in a set of observations, formulating the patterns as onje tures, and then testing whether the onje tures an be dedu ed from existing knowledge.
Guess-and-verify is a brief way of summarising mathemati al indu tion. (Guessing is the
formulation of a onje ture; veri ation is the pro ess of dedu ing whether the guess is

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

94

6. Indu tion

orre t.)
Several of the mat hsti k games studied in hapter 4 provide good examples of mathemati al indu tion. Re all, for example, the game dis ussed in se tion 4.2.2: there is
one pile of mat hes from whi h it is allowed to remove one or two mat hes. Exploring
this game, we dis overed that a pile with 0 , 3 or 6 mat hes is a losing position, and
piles with 1 , 2 , 4 , 5 , 7 and 8 mat hes are winning positions. There seems to be
a pattern in these numbers: losing positions are the positions in whi h the number of
mat hes are a multiple of 3 , and winning positions are the remaining positions. This is a
onje ture about all positions made from observations on just nine positions. However,
we an verify that the onje ture is true by using mathemati al indu tion to onstru t
a winning strategy.
In order to use indu tion, we measure the \size" of a pile of mat hes not by the
number of mat hes but by the number of mat hes divided by 3 , rounded down to the
nearest natural number. So, the \size" of a pile of 0 , 1 or 2 mat hes is 0 , the \size"
of a pile of 3 , 4 or 5 mat hes is 1 , and so on. The indu tion hypothesis is that a pile
of 3n mat hes is a losing position, and a pile of 3n + 1 or 3n + 2 mat hes is a winning
position.
The basis for the indu tion is when n equals 0 . A pile of 0 mat hes is, indeed, a
losing position be ause, by de nition, 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 veri es 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.

January 7, 2007

95

6.5. The Need For Proof

6.5

The Need For Proof

When using indu tion, it is vital that any onje ture is properly veri ed. It is too easy to
extrapolate from a few ases to a more general laim that is not true. Many onje tures
turn out to be false; only by subje ting them to the rigours of proof an we be sure of
their validity. This se tion is about a non-trivial example of a false onje ture.
Suppose n points are marked on the ir umferen e of a ir ular ake and then the
ake is ut along the hords joining them. The points are hosen in su h a way that all
interse tion points of pairs of hords are distin t. The question is, in how many portions
does this ut the ake?
Figure 6.10 shows the ase when n is 1 , 2 , 3 or 4 .

Figure 6.10: Cutting the ake


The number of portions is su essively 1 , 2 , 4 and 8 . This suggests that the
number of portions, for arbitrary n , is 2n1 . Indeed, this onje ture is supported by
the ase that n = 5 . (We leave the reader to draw the gure.) In this ase, the number
of portions is 16 , whi h is 251 . However, for n = 6 , the number of portions is 31 !
(See g. 6.11.) Note that n = 6 is the rst ase in whi h the points are not allowed to
be pla ed at equal distan es around the perimeter.
Had we begun by onsidering the ase that n = 0 , suspi ions about the onje ture
would already have been raised | it doesn't make sense to say that there are 201 portions, even though utting the ake as stated does make sense! The easy, but inadequate
way out, is to dismiss this ase, and impose the requirement that n is di erent from 0 .
The derivation of the orre t formula for the number of portions is too ompli ated to
dis uss here, but it does in lude the ase that n equals 0 !

6.6

From Verification to Construction

In mathemati al texts, indu tion is often used to verify known formulae. Veri ation is
important but has a major drawba k | it seems that a substantial amount of lairvoyan e

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

96

6. Indu tion

Figure 6.11: Cutting the ake. The ase n = 6 . The number of portions is 31 , not 261
is needed to ome up with the formula that is to be veri ed. And, if one's onje ture is
wrong, veri ation gives little help in determining the orre t formula.
Indu tion is not important in omputing s ien e as a veri ation prin iple but be ause it is a fundamental prin iple in the onstru tion of omputer programs. This
se tion introdu es the use of indu tion in the onstru tion of mathemati al formulae.
The problem we onsider is how to determine a losed formula for the sum of the
k th powers of the rst n natural numbers.
A well-known formula gives the sum of the natural numbers from 1 thru n :
1+2+ ... +n =

1
n(n+1) .
2

Two other exer ises, often given in mathemati al texts, are to verify that
12 + 22 + . . . + n2 =

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

13 + 23 + . . . + n3 =

1 2
n (n+1)2 .
4

and

As well as being good examples of the strength of the prin iple of mathemati al indu tion,
the examples also illustrate the weakness of veri ation: the te hnique works if the answer
is known, but what happens if the answer is not already known! Suppose, for example,
that you are now asked to determine a losed formula for the sum of the 4 th powers of
the rst n numbers
14 + 24 + . . . + n4 = ? .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

6.6. From Veri ation to Constru tion

97

How would you pro eed? Veri ation, using the prin iple of mathemati al indu tion,
does not seem to be appli able unless we already know the right side of the equation.
Can you guess what the right side would be in this ase? Can you guess what the right
side would be in the ase that the term being summed is, say, k27 ? Almost ertainly,
not!
Constru ting solutions to non-trivial problems involves a reative pro ess. This means
that a ertain amount of guesswork is ne essary, and trial-and-error annot be ompletely
eliminated. Redu ing the guesswork to a minimum, repla ing it by mathemati al al ulation is the key to su ess.
Indu tion an be used to onstru t losed formulae for su h summations. The general
idea is to seek a pattern, formulate the pattern in pre ise mathemati al terms and then
verify the pattern. The key to su ess is simpli ity. Don't be over-ambitious. Leave the
work to mathemati al al ulation.
A simple pattern in the formulae displayed above is that, for m equal to 1 , 2 and
3 , the sum of the m th powers of the rst n numbers is a polynomial in n of degree
m+1 . (The sum of the rst n numbers is a quadrati fun tion of n , the sum of the
rst n squares is a ubi fun tion of n , and the sum of the rst n ubes is a quarti
fun tion of n .) This pattern is also on rmed in the (oft-forgotten) ase that m is 0 :
10 + 20 + . . . + n0 = n .

A strategy for determining a losed formula for, say, the sum of the fourth powers is thus
to guess that it is a fth degree polynomial in n and then use indu tion to al ulate
the oe ients. The al ulation in this ase is quite long, so let us illustrate the pro ess
by showing how to onstru t a losed formula for 1 + 2 + . . . + n . (Some readers will
already know a simpler way of deriving the formula in this parti ular ase. If this is the
ase, please bear with us. The method des ribed here is more general.)
We onje ture that the required formula is a se ond degree polynomial in n , say
a + bn + cn2 and then al ulate the oe ients a , b and c . Here is how the al ulation
goes.
For brevity, let us use S.n to denote
1+2+ ... +n .

We also use P.n to denote the proposition


S.n = a + bn + cn2 .

Then,
P.0
=

de nition of P

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

98

6. Indu tion
S.0 = a + b0 + c02
=

S.0 = 0 (the sum of an empty set of numbers

is zero) and arithmeti }


0=a .

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)
=

de nition 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
2

=c

1
2

=b .

From the onje ture that the sum of the rst n numbers is a quadrati in n , we have
thus al ulated that
1+2+ ... +n =

1
1
n + n2 .
2
2

Extrapolating from this al ulation, one an see that it embodies an algorithm to express
1m + 2m + . . . + nm as a polynomial fun tion for any given natural number m . The steps
in the algorithm are: postulate that the summation is a polynomial in n with degree
m+1 . Use the prin iple of mathemati al indu tion together with the fa ts that S.0 is 0
and S.(n+1) is S.n + (n+1)m (where S.n denotes 1m + 2m + . . . + nm ) to determine

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

99

6.7. Fake-Coin Dete tion

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

...

n ,

n1 +

...

1 .

...

n+1 .

and immediately below it


n

Then add the two rows together:


n+1 +

n+1 +

From the fa t that there are n o urren es of n+1 we on lude that the sum is
1
n(n+1) . However, this method annot be used for determining 1m + 2m + . . . + nm
2
for m greater than 1 . End of remark.
Exercise 6.3

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 n mat hes an be removed. By onsidering a few simple examples (for example, m
is 1 and n is arbitrary, or m is 2 and n is 3 ), formulate a general rule for determining

whi h are the winning positions and whi h are the losing positions, and what the winning
strategy is.
Avoid guessing the omplete solution. Try to identify a simple pattern in the way
winning and losing positions are grouped. Introdu e variables to represent the grouping,
and al ulate the values of the variables.

6.7

Fake-Coin Detection

The motto of se tion 6.6 an be summarised as \Don't guess! Cal ulate." We put this
into pra ti e in this se tion.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

100

6. Indu tion

Suppose we are given a number of oins, ea h of the same size and shape. We are
told that among them there is at most one \fake" oin, and all the rest are \genuine".
All \genuine" oins have the same weight, whereas a \fake" oin has a di erent weight
to a \genuine" oin. The problem is how to use the pair of s ales optimally in order to
nd the fake oin, if there is one.
Note the element of vagueness in this problem statement; we don't say what we mean
by using the s ales \optimally" This is deliberate. Often, an essential element of problem
solving is to learly identify the problem itself. Our formulation of the problem and its
eventual solution illustrates several other aspe ts of \real" problem solving. Several
stages are needed, in luding some \ba ktra king" and revision.

6.7.1

Problem Formulation

When we use a pair of s ales to ompare two weights |an operation that we all a
omparison | there are 3 possible out omes: the s ales may tip to the left, they may
balan e, or they may tip to the right. This means that with n omparisons, there are
at most 3n di erent out omes3. This gives an upper bound on what an be a hieved
using a pair of s ales.
Now, suppose we are given m oins, of whi h at most one is fake and the rest are
genuine. Then there are 1 + 2m di erent possibilities that an be observed with a pair
of s ales: \ 1 " possibility is that all oins are genuine; otherwise, there are \ 2 " ways
that ea h of the \ m " oins may be fake (by being lighter or heavier than a genuine
oin). This means that, with n omparisons, the number of oins among whi h at most
one fake oin an be dete ted is at most m , where 1 + 2m = 3n . More pre isely, if the
number, m , of oins is greater than (3n1)/2 , it is impossible to guarantee that a fake
oin an be found with n omparisons.
We have almost rea hed the point at whi h we an state our problem pre isely. We
onje ture that, given (3n1)/2 oins of whi h at most one is fake, it is possible to
establish that all are genuine or identify the fake oin (and whether it is lighter or
heavier than a genuine oin) using at most n omparisons.
For n equal to 0 , the onje ture is learly true; in this ase, there are no oins, all of
whi h are genuine. For n equal to 1 , however, we run into a problem. The assumption
is that there is one oin (sin e (311)/2 = 1 ). But how an we tell whether this one oin
is fake or genuine, if there are no other oins to ompare it with? Our onje ture has
broken down, and needs revision.
We propose to modify the onje ture by assuming that we have at our disposal at
least one additional oin that is known to be genuine. Thus, we are given (3n1)/2
oins about whi h we know nothing ex ept that at most one is fake, and we are also given
3 Note

the impli it use of indu tion here.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

101

6.7. Fake-Coin Dete tion

at least one oin that is known to be genuine. The problem is to onstru t an algorithm
that will identify the fake oin, if it exists, or determine that all oins are genuine, using
at most n omparisons.

6.7.2

Problem Solution

Our formulation of the problem begs the use of indu tion on the number of omparisons,
n , in its solution.
The Basis With zero omparisons, we an report immediately that all oins in a olle tion of (301)/2 are genuine. The base ase, n equal to 0 , is thus solved.
Induction Step Now, we ta kle the indu tion step. Suppose n is at least zero. For
brevity, let us use c.n to denote (3n1)/2 . By indu tion, we may assume that a fake
oin, if it exists, an be found among c.n oins using at most n omparisons. We have
to show how to nd a fake oin, if it exists, among c.(n+1) oins, using at most n+1

omparisons.
Consider the rst omparison. It involves putting some number of oins on the left
s ale, some on the right s ale, and leaving some on the table. To be able to draw any
on lusion from the omparison, the number of oins on the two s ales must be equal.
One possible onsequen e of the omparison is that the s ales balan e, from whi h one
infers that none of the oins on the s ales is fake. The algorithm would then pro eed to
try to nd a fake oin among the oins left on the table.
Combined with the indu tion hypothesis, this di tates that c.n oins must be left
on the table. This is be ause c.n is the maximum number of oins among whi h a fake
oin an be found with n omparisons.
It also di tates how many oins should be put on the s ales | this is the di eren e
between c.(n+1) and c.n . Now,
c.(n+1) = (3n+11)/2 = 3((3n1)/2) + 1 = 3 c.n + 1 .

So
c.(n+1) c.n = 2 c.n + 1 = 3n .

This is an odd number; it an be made even by using one of the oins we know to be
genuine. (Re all the assumption that we have at least one oin that is known to be
genuine, in addition to the c.(n+1) oins whose kind we must determine.) We on lude
that in the rst omparison, c.n + 1 oins should be put on ea h of the two s ales.
The next step is to determine what to do after the rst omparison is made. There are
three possible out omes, of whi h we have already dis ussed one. If the s ales balan e,

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

102

6. Indu tion

the fake oin should be sought among the c.n oins left on the table. The problem is
what to do if the s ales tip either to the left or to the right.
At this point, we realise that the indu tion hypothesis doesn't help. It is too weak! If
the s ales tip to one side, we an on lude that all the oins left on the table are genuine,
and an be eliminated from onsideration. But we are still left with 3n oins none of
whi h we know to be genuine. And ru ially, 3n is greater than c.n . We are unable to
apply the indu tion hypothesis to this number of oins.
The omparison does tell us something about the oins on the s ales. If the s ales tip
to one side, we know that all the oins on that side are possibly heavier than a genuine
oin, and all the oins on the other side are possibly lighter than a genuine oin. By
\possibly lighter" we mean genuine, or fake and lighter. By \possibly heavier" we mean
genuine, or fake and heavier. After the omparison, we an mark all the oins on the
s ales one way or the other.
The Marked Coin Problem In this way, in the ase that the s ales do not balan e,

the problem we started with has been redu ed to a di erent 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 di erent 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.

January 7, 2007

103

6.7. Fake-Coin Dete tion

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 identi ed 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+11)/2
is as follows.
Divide the oins into three groups of sizes (3n1)/2 , (3n1)/2 + 1 and (3n1)/2 .
Pla e the rst group on the left s ale together with the supplied genuine oin. Pla e the
se ond group on the right s ale, and leave the third group on the table. Determine the
out ome of the omparison, and pro eed as follows:

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

104

6. Indu tion

 If the s ales balan e, all the oins on the balan e are genuine. Apply the solution
to the unmarked- oin problem (indu tively) to the oins on the table.

 If the s ales tip to the left, the oins on the table are genuine. Mark all the oins on
the left s ale, with the ex eption of the supplied genuine oin as \possibly heavy".
Mark the oins on the right s ale as \possibly light". Apply the solution to the
marked- oin problem to the 3n marked oins.

 If the s ales tip to the right, the oins on the table are genuine. Mark all the oins

on the left s ale, with the ex eption of the supplied genuine oin as \possibly light".
Mark the oins on the right s ale as \possibly heavy". Apply the solution to the
marked- oin problem to the 3n marked oins.

We ask the reader to review the development of this algorithm. Note that at no stage
is a guess made at an indu tive hypothesis, even though the development ne essitates
several su h hypotheses. Quite the opposite: ea h hypothesis is systemati ally al ulated
from the available information. This is the epitome of the art of e e tive reasoning.
Suppose you are given a number of obje ts. All the obje ts have the
same weight, with the ex eption of one, alled the unique obje t, whi h has a di erent
weight. In all other respe ts, the obje ts are identi al. You are required to determine
whi h is the unique obje t. For this purpose, you are provided with a pair of s ales.
Show, by indu tion on m , that at most 2m omparisons are needed to identify
the unique obje t when the total number of obje ts is 3m . (Hint: for the indu tion step,
3m+1 obje ts an be split into 3 groups of 3m obje ts.)
Can you identify whether the unique obje t is lighter or heavier than all the other
obje ts?
Exercise 6.5

6.8

Summary

Indu tion is one of the most important problem-solving prin iples. The prin iple of
mathemati al indu tion is that instan es of a problem of arbitrary \size" an be solved
for all \sizes" if
(a) instan es of \size" 0 an be solved,
(b) given a method of solving instan es of \size" n , for arbitrary n , it is possible to
adapt the method to solve instan es of \size" n+1 .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

105

6.9. Bibliographi Notes

Using indu tion means looking for patterns. The pro ess may involve some reative
guesswork, whi h is then subje ted to the rigours of mathemati al dedu tion. The key
to su ess is to redu e the guesswork to a minimum, by striving for simpli ity, and using
mathemati al al ulation to ll in ompli ated details.

6.9

Bibliographic Notes

The solution to the fake- oin problem is a ombination of two papers by Edsger W.
Dijkstra [Dij90, Dij97.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

106

6. Indu tion

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Chapter 7
The Towers of Hanoi
This hapter is about the Towers of Hanoi problem. The problem is dis ussed in many
mathemati al texts, and is often used in omputing s ien e and arti ial intelligen e as
an illustration of \re ursion" as a problem-solving strategy.
The Towers of Hanoi problem is a puzzle that is quite di ult to solve without a
systemati problem-solving strategy. Indu tion gives a systemati way of onstru ting
a rst solution. However, this solution is undesirable. A better solution is obtained by
observing an invariant of the indu tive solution. In this way, this hapter brings together
a number of the te hniques dis ussed earlier: prin ipally indu tion and invariants, but
also the properties of logi al equivalen e.
For this problem, we begin with the solution of the problem. One reason for doing
so is to make lear where we are headed; the Towers of Hanoi problem is one that is not
solved in one go; several steps are needed before a satisfa tory solution is found. Another
reason is to illustrate how di ult it an be to understand why a orre t solution has
been found, if no information about the solution method is provided.

7.1
7.1.1

Specification and Solution


The End of the World!

The Towers of Hanoi problem omes from a puzzle marketed in 1883 by the Fren h
 douard Lu as, under the pseudonym M. Claus.
mathemati ian E
The puzzle is based on a legend a ording to whi h there is a temple in Bramah
where there are three giant poles xed in the ground. On the rst of these poles, at the
time of the world's reation, God pla ed sixty-four golden disks, ea h of di erent size,
in de reasing order of size. (See g. 7.1.) The Brahmin monks were given the task of
moving the disks, one per day, from one pole to another a ording to the rule that no
disk may ever be above a smaller disk. The monks' task will be omplete when they

Algorithmi Problem Solving Roland Ba khouse.

107

January 7, 2007

108

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!

Figure 7.1: Towers of Hanoi Problem

7.1.2

Iterative Solution

There is a very easy solution to the Towers of Hanoi problem that is easy to remember
and easy to exe ute. To formulate the solution, we assume that the poles are arranged at
the three orners of a triangle. Movements of the disks an then be su in tly des ribed
as either lo kwise or anti lo kwise movements. We assume that the problem is to move
all the disks from one pole to the next in a lo kwise dire tion. We also assume that days
are numbered from 0 onwards. On day 0 , the disks are pla ed in their initial position
and the monks begin moving the disks on day 1 . With these assumptions, the solution
is the following.
On every alternate day, beginning on the rst day, the smallest disk is moved. The
rule for moving the smallest disk is that it should y le around the poles. The
dire tion of rotation depends on the total number of disks. If the total number of
disks is odd, the smallest disk should y le in a lo kwise dire tion. Otherwise, it
should y le in an anti lo kwise dire tion.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

109

7.2. Indu tive Solution

On every other day, a disk other than the smallest disk is moved | subje t to the
rule that no disk may ever be above a smaller disk. It is easy to see that be ause
of this rule there is exa tly one move possible so long as not all the disks are on
one pole.
The algorithm terminates when no further moves are possible, that is, on an evennumbered day when all the disks are on one-and-the-same pole.
Try exe uting this algorithm yourself on, say, a 4 -disk puzzle. Take are to y le the
smallest disk on the odd-numbered moves and to obey the rule not to pla e a disk on
top of a disk smaller than itself on the even-numbered moves. If you do, you will nd
that the algorithm works. Depending on how mu h patien e you have, you an exe ute
the algorithm on larger and larger problems | 5 -disk, 6 -disk, and so on.

7.1.3

WHY?

Presenting the problem and its solution, like this, provides no help whatsoever in understanding how the solution is onstru ted. If anything, it only serves to impress |look
at how lever I am!| but in a reprehensible way. Matters would be made even worse
if we now pro eeded to give a formal mathemati al veri ation of the orre tness of the
algorithm. This is not how we intend to pro eed! Instead, we rst present an indu tive
solution of the problem. Then, by observing a number of invariants, we show how to
derive the algorithm above from the indu tive solution.

7.2

Inductive Solution

Constru ting a solution by indu tion on the number of disks is an obvious strategy.
Let us begin with an attempt at a simple-minded indu tive solution. Suppose that
the task is to move M disks from one spe i pole to another spe i pole. Let us
all these poles A and B, and the third pole C. (Later, we see that naming the poles is
inadvisable.)
As often happens, the basis is easy. When the number of disks is 0 no steps are
needed to omplete the task. For the indu tive step, we assume that we an move n
disks from A to B, and the problem is to show how to move n+1 disks from A to B.
Here, we soon get stu k! There is only a ouple of ways that the indu tion hypothesis
an be used, but these lead nowhere:
1. Move the top n disks from A to B. After doing this, we have exhausted all possibilities of using the indu tion hypothesis be ause n disks are now on pole B, and
we have no hypothesis about moving disks from this pole.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

110

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 formulating the indu tion hypothesis. It is not su ient to simply take the problem spe i ation
as indu tion hypothesis. This is be ause the problem spe i ation assumes that there
are exa tly M disks that are to be moved. When using indu tion, it is ne essary to move
n disks in the presen e of Mn other disks. If some of these Mn disks are smaller
than the n disks being moved, the requirement that a larger disk may not be pla ed on
top of a smaller disk may be violated. We need a stronger indu tion hypothesis.
The indu tion hypothesis we use is that it is possible to move the n smallest disks,
from one pole to its neighbour in the dire tion d , beginning from any valid starting
position (that is, a starting position in whi h the disks are distributed arbitrarily over
the poles, but no disk is on top of a disk smaller than itself).
In the ase that n is 0 , the sequen e of moves is the empty sequen e. In the ase of
n+1 disks we assume that we have a method of moving the n smallest disks from one
pole to either of its two neighbours. We must show how to move n+1 disks from one
pole to its neighbour in dire tion d , where d is either lo kwise or anti lo kwise. For
onvenien e, we assume that the disks are numbered from 1 upwards, with the smallest
disk being given number 1 .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

111

7.2. Indu tive Solution

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
de nes Hn.d to be a sequen e of pairs hk , d i where n is the number of disks, k is
a disk number and d and d are dire tions. Disks are numbered from 1 onwards,
disk 1 being the smallest. Dire tions are boolean values, true representing a lo kwise
movement and false an anti- lo kwise movement. The pair hk , d i means move the
disk numbered k from its urrent position in the dire tion d . The semi olon operator
on atenates sequen es together, [ ] denotes an empty sequen e and [x] is a sequen e
with exa tly one element x . Taking the pairs in order from left to right, the omplete
sequen e Hn.d pres ribes how to move the n smallest disks one-by-one from one pole
to its neighbour in the dire tion d , following the rule of never pla ing a larger disk on
top of a smaller disk.
H0.d = [ ]
Hn+1.d = Hn . d ; [hn+1 , di] ; Hn . d

Note that the pro edure name H re urs on the right side of the equation for Hn+1.d .
Be ause of this we have what is alled a re ursive solution to the problem. Re ursion
is a very powerful problem-solving te hnique, but unrestri ted use of re ursion an be
unreliable. The form of re ursion used here is limited; des ribing the solution as an
\indu tive" solution makes lear the limitation on the use of re ursion.
This indu tive pro edure gives us a way to generate the solution to the Towers of
Hanoi problem for any given value of n | we simply use the rules as left-to-right
rewrite rules until all o urren es of H have been eliminated. For example, here is how
we determine H2.cw . (We use cw and aw , meaning lo kwise and anti lo kwise, rather
than true and false in order to improve readability.)
H2.cw
=

2nd equation, n,d := 1,cw

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

112

7. The Towers of Hanoi


H1.aw ; [h2,cwi] ; H1.aw
=

2nd equation, n,d := 0,aw

H0.cw ; [h1,awi] ; H0.cw ; [h2,cwi] ; H0.cw ; [h1,awi] ; H0.cw


=

1st equation }

[ ] ; [h1,awi] ; [ ] ; [h2,cwi] ; [ ] ; [h1,awi] ; [ ]


=

on atenation of sequen es

[h1,awi , h2,cwi , h1,awi] .

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 e ort to put
into pra ti e. The omplete expansion of the equations in the ase of n = 3 takes 16
steps, in the ase of n = 4 takes 32 steps, and so on. This is not the easy solution that the
Bramin monks are using! The solution given in se tion 7.1.1 is an iterative solution to
the problem. That is, it is a solution that involves iteratively (i.e. repeatedly) exe uting
a simple pro edure dependent only on the urrent state. The implementation of the
indu tive solution, on the other hand, involves maintaining a sta k of the sequen e of
moves yet to be exe uted. The memory of Bramin monks is unlikely to be large enough
to do that!
The number of days the monks need to omplete their task is the length
of the sequen e H64.cw . Let Tn.d denote the length of the sequen e Hn.d . Derive
an indu tive de nition of T from the indu tive de nition of H . (You should nd that
Tn.d is independent of d .) Use this de nition to evaluate To , T1 and T2 . Hen e, or
otherwise, formulate a onje ture expressing Tn as an arithmeti fun tion of n . Prove
your onje ture by indu tion on n .
Exercise 7.1

Use indu tion to derive a formula for the number of di erent states in
the Towers of Hanoi problem.
Use indu tion to show how to onstru t a state-transition diagram that shows all
possible states of n disks on the poles, and the allowed moves between states.
Use the onstru tion to show that the above solution optimises the number of times
that disks are moved.
Exercise 7.2

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

113

7.3. The Iterative Solution

7.3

The Iterative Solution

Re all the iterative solution to the problem, presented in se tion 7.1.2. It has two main
elements: the rst is that the smallest disk y les around the poles (that is, its dire tion
of movement is invariantly lo kwise or invariantly anti lo kwise), the se ond is that
the disk to be moved alternates between the smallest disk and some other disk. In this
se tion, we show how these properties are derived from the indu tive solution.
Cyclic Movement of the Disks

In this se tion, we show that the smallest disk always y les around the poles. In fa t, we
do more than this. We show that all the disks y le around the poles, and we al ulate
the dire tion of movement of ea h.
The key is that, for all pairs hk , d i in the sequen e Hn+1.d the boolean value
even.k d is invariant (that is always true or always false). This is a simple onsequen e of the rule of ontraposition dis ussed in se tion 5.5. When the formula for
Hn+1.d is applied, the parameter \ n+1 " is repla ed by \ n " and \ d " is repla ed by
\ d ". Sin e even.(n+1) (even.n) , the value of even.(n+1) d remains onstant
under this assignment.
Whether even.k d is true or false (for all pairs hk , d i in the sequen e Hn+1.d )
will depend on the initial values of n and d . Let us suppose these are N and D . Then,
for all moves hk , di , we have
even.k d even.N D .

This formula allows us to determine the dire tion of movement d of disk k . Spe i ally,
if it is required to move an even number of disks in a lo kwise dire tion, all evennumbered disks should y le in a lo kwise dire tion, and all odd-numbered disks should
y le in an anti lo kwise dire tion. Vi e-versa, if it is required to move an odd number of
disks in a lo kwise dire tion, all even-numbered disks should y le in an anti lo kwise
dire tion, and all odd-numbered disks should y le in a lo kwise dire tion. In parti ular,
the smallest disk (whi h is odd-numbered) should y le in a dire tion opposite to D if
N is even, and the same dire tion as D if N is odd.
An explorer on e dis overed the Bramin temple and was able to se retly
observe the monks performing their task. At the time of his dis overy, the monks had
got some way to ompleting their task, so that the disks were arranged on all three poles.
The poles were arranged in a line and not at the orners of the triangle so he wasn't
sure whi h dire tion was lo kwise and whi h anti lo kwise. However, on the day of his
arrival he was able to observe the monks move the smallest disk from the middle pole
Exercise 7.3

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

114

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
e e t on the state of the puzzle. We now want to give a formal proof that the sequen e
Hn.d satis es this property.
Let us all a sequen e of numbers alternating if it has two properties. The rst
property is that onse utive elements alternate between one and a value greater than
one; the se ond property is that if the sequen e is non-empty then it begins and ends
with the value one. We write alt.ks if the sequen e ks has these two properties.
The sequen e of disks moved on su essive days, whi h we denote by diskn.d , is
obtained by taking the rst omponent of ea h of the pairs in Hn.d and ignoring the
se ond. Let the sequen e that is obtained in this way be denoted by diskn.d . Then,
from the de nition 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)) .

The proof is then:


alt.(diskn+1.d)
=

de nition }

alt . (diskn . d ; [n+1] ; diskn . d)

above property of alternating sequen es

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

115

7.4. Summary
alt.(diskn . d) ((diskn . d = [ ]) (n+1 = 1))
=

indu tion hypothesis applied to the rst onjun t,


straightforward property of diskn for the se ond.

true .

The explorer left the area and did not return until several years later.
On his return, he dis overed the monks in a state of great despair. It transpired that
one of the monks had made a mistake shortly after the explorer's rst visit but it had
taken the intervening time before they had dis overed the mistake. The state of the disks
was still valid but the monks had dis overed that they were no longer making progress
towards their goal; they had got into a never-ending loop!
Fortunately, the explorer was able to tell the monks how to pro eed in order to return
all the disks to one-and-the-same pole whilst still obeying the rules laid down to them
on the day of the world's reation. They would then be able to begin their task afresh.
What was the algorithm the explorer gave to the monks? Say why the algorithm
is orre t. (Hint: The disk being moved will still alternate between the smallest and
some other disk. You only have to de ide in whi h dire tion the smallest disk should
be moved. Be ause of the monks' mistake this will not be onstant. Make use of the
fa t that, beginning in a state in whi h n disks are all on the same pole, maintaining
invariant the relationship

Exercise 7.4

even.n d even.k d

for the dire tion d moved by disk k will move n disks in the dire tion d .)

Suppose ea h disk is oloured, red white or blue.


The olouring of disks is random; di erent disks may be oloured di erently.
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.
Exercise 7.5 (Coloured Disks)

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.

January 7, 2007

116

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.

7.5

Bibliographic Remarks

Information on the history of the Towers of Hanoi problem is taken from [Ste97. A proof
of the orre tness of the iterative solution was published in [BL80. The formulation and
proof presented here is based on [BF01.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Chapter 8
Knights Circuit
The problem ta kled in this hapter is a parti ularly hard one. Yet, by suitably de omposing the problem, ombined with e e tive reasoning skills, the problem be omes
solvable.
The problem is to nd a Knight's ir uit of a hessboard. That is, nd a sequen e of
moves that will take a Knight in a ir uit around all the squares of a hessboard, visiting
ea h square exa tly on e, and ending at the square at whi h the ir uit began.
The problem is an instan e of a sear h problem; in prin iple, it an be solved by a
systemati , exhaustive examination of all the paths a Knight an follow around a hessboard | a so- alled brute-for e sear h. However, there are 64 squares on a hessboard;
that means 64 moves have to be hosen, one for ea h square. From ea h of the orner
squares, there is a hoi e of just 2 moves, but from ea h of the 16 entral squares, there
is a hoi e of 8 moves (see g. 8.1); from the remaining squares either 4 or 6 moves are
possible. This gives a massive amount of hoi e in the paths that an be followed. Lots
of hoi e is usually not a problem but, when ombined with the very restri tive requirements that the path forms a ir uit that visits every square exa tly on e, it does be ome
a problem. The Knight's ir uit problem is hard be ause of this riti al ombination of
an explosion with an implosion of hoi e.
But, all is not lost. The squares on a hessboard are arranged in a very simple pattern,
and the Knight's moves, although many, are spe i ed by one simple rule (two squares
horizontally or verti ally, and one square in the opposite dire tion). There is a great
deal of stru ture, whi h we must endeavour to exploit.

8.1

Straight-Move Circuits

Finding a Knight's ir uit is too di ult to ta kle head on. Some experien e of ta kling
simpler ir uit problems is demanded.

Algorithmi Problem Solving Roland Ba khouse.

117

January 7, 2007

118

8. Knight's Cir uit

Figure 8.1: Knight's Moves.


Let's turn the problem on its head. Suppose you want to make a ir uit of a hessboard 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
straight-move 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 88 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 straight-move 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 8.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.

January 7, 2007

119

8.1. Straight-Move Cir uits

that ea h move is from one square to a di erent oloured square. Otherwise, the
answer does not depend on the sort of moves that are allowed.)
(b) For what values of m is it possible to omplete a straight-move ir uit of a board
of size 2m1 ? (A 2m1 board has one olumn of squares; the number of squares
is 2m .)
(c) Show that it is possible to omplete a straight-move ir uit of a 2n board for all
(positive) values of n . (A 2n board has two rows, ea h row having n squares.)

The on lusion of exer ise 8.1 is that a straight-move ir uit is only possible if the
board has size 2mn , for positive numbers m and n . That is, one side has length 2m
and the other has length n . (Both m and n must be non-zero be ause the problem
assumes the existen e of at least one starting square.) Also, a straight-move ir uit an
always be ompleted when the board has size 2n , for positive n . This suggests that
we now try to onstru t a straight-move ir uit of a 2mn board, for m at least one
and n greater than one, by indu tion on m , the 2n ase providing the basis for the
indu tive onstru tion.
To omplete the indu tive onstru tion, we need to onsider a board of size 2m n ,
where m is greater than 1 . Su h a onstru tion is hopeful be ause, when m is greater
than 1 , a 2m n board an be split into two boards of sizes 2p n and 2q n , say,
where both p and q are smaller than m and p+q equals m . We may take as the
indu tive hypothesis that a straight-move ir uit of both boards an be onstru ted. We
just need to ombine the two onstru tions.
The key to the ombination is the orner squares. There are two straight moves from
ea h of the orner squares, and any straight-move ir uit must use both. In parti ular,
it must use the horizontal moves. Now, imagine that a 2m n board is divided into a
2p n board and a 2q n board, with the former above the latter. (The onvention
we use is that the rst number gives the number of rows and the se ond the nunber
of olumns of the board.) The bottom-left orner of the 2p n board is immediately
above the top-left orner of the 2q n board. Constru t straight-move ir uits of these
two boards. Figure 8.2 shows the result diagrammati ally. The two horizontal, red
lines at the middle-left of the diagram depi t the horizontal moves that we know must
form part of the two ir uits. The blue dotted lines depi t the rest of the ir uits. (Of
ourse, the shape of the dotted lines gives no indi ation of the shape of the ir uit that
is onstru ted.)
Now, to ombine the ir uits to form one ir uit of the entire board, repla e the
horizontal moves from the bottom-left and top-left orners by verti al moves, as shown
by the verti al green lines in g. 8.3. This ompletes the onstru tion.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

120

8. Knight's Cir uit


n

2p

2m

2q

Figure 8.2: Combining straight-move ir uits. First, split the board into two smaller
boards and onstru t straight-move ir uits of ea h.
n

2p

2m

2q

Figure 8.3: Se ond, ombine the two ir uits as shown.


Figure 8.4 shows the ir uit that is onstru ted in this way for a 6 8 board. Effe tively, the basis of the indu tive algorithm onstru ts straight-move 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. 8.4.
As mentioned above, when a board has an odd number of squares, no
ir uit is possible.
Consider a 33 board. It is easy to onstru t a straight-move ir uit of all its squares
but the middle square. (See g. 8.5.) It is also possible to onstru t a straight-move
ir uit of all its squares but one of the orner squares. However, a straight-move ir uit
of all but one of the other four squares |the squares adja ent to a orner square, for
example, the middle-left square| annot be onstru ted.
Explore when it is possible, and when it is not possible, to onstru t a straight-move
ir uit of all the squares but one in a board of odd size.

Exercise 8.2

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

121

8.2. Supersquares

Figure 8.4: A straight-move ir uit for a 6 8 board.

Figure 8.5: Straight-move ir uits (shown in red) of a 3 3 board, omitting one of the
squares.

8.2

Supersquares

Let us now return to the Knight's- ir uit problem. The key to a solution is to exploit
what we know about straight moves. The way this is done is to imagine that the 8 8
hessboard is divided into a 4 4 board by grouping together 2 2 squares into \supersquares", as shown in g. 8.6.
If this is done, the Knight's moves an be lassi ed 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.

January 7, 2007

122

8. Knight's Cir uit

Figure 8.6: Chessboard divided into a 4 4 board of supersquares.


Knight from one supersquare to another along one of the diagonals through the starting
supersquare. In g. 8.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
1
0
1
0
1
00
11
0
1
0
1
0
1
0
1
00
11
0
1
0
1
0
1
0
1
11111111111111
00000000000000
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
1
11111111111111
00000000000000
0
1
0
1
0
1
0
1
0
1
0
1
0
1
0
0 1
1
0 1
01
0
1
Figure 8.7: Straight (Blue) and Diagonal (Red) Knight's Moves From Some Starting
Position (Bla k). Boundaries of the supersquares are indi ated by thi kened lines.
Fo using on the straight moves, we now make a ru ial observation. Figure 8.8 shows
the straight moves from one supersquare |the bottom-left supersquare| verti ally upwards and horizontally rightwards. The olours indi ate the moves that an be made.
For example, from the bottom-left red square a straight move an be made to the top-left
red square or to the bottom-right red square.
Observe the pattern of the olours. Verti al moves ip the olours around a verti al
axis, whilst horizontal moves ip them around a horizontal axis. (The verti al moves
inter hange the red-above-yellow and blue-above-green olumns; the horizontal moves
inter hange the red-next-to-blue row with the yellow-next-to-green row.)

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

8.2. Supersquares

111
000
00
11
00011
111
00
000
111
00
11
000
111
00
11
00
11
00
11
00011
111
0011
0011
00
00011
111
0011
0011
00
00011
111
0011
0011
00

123

Figure 8.8: Straight moves from the bottom-left supersquare.


Let us denote by v the operation of ipping the olumns of a 2 2 square ( v is short
for \verti al"). Similarly, let us denote by h (short for \horizontal") the operation of
ipping the rows of the square. Now, let an in x semi olon denote doing one operation
after another. So, for example, v ; h denotes the operation of rst ipping the olumns
and then ipping the rows of the square. Flipping the olumns and then ipping the
rows is the same as ipping the rows and then the olumns. That is,
v;h = h;v .

Both are equivalent to rotating the 2 2 square through 1800 about its entre. So, let
us use c (short for \ entre") as its name. That is, by de nition of c ,
(8.3)

v;h = c = h;v .

We have now identi ed three operations on a 2 2 square. There is a fourth operation,


whi h is the do-nothing operation. Elsewhere, we have used skip to name su h an
operation. Here, for greater brevity we use n (short for \no hange"). Flipping twi e
verti ally, or twi e horizontally, or rotating twi e through 1800 about the entre, all
amount to doing nothing. That is;
(8.4)

v;v = h;h = c;c = n .

Also, doing nothing before or after any operation is the same as doing the operation.
(8.5)

n;x = x;n = x .

The three equations (8.3), (8.4) and (8.5), together with the fa t that doing one operation
after another is asso iative (that is, doing one operation x followed by two operations
y and z in turn is the same as doing rst x followed by y and then on luding with
z | in symbols, x ; (y ; z) = (x ; y) ; z ) allow the simpli ation of any sequen e of the
operations to one operation. For example,

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

124

8. Knight's Cir uit


v;c;h
=

v;h = c

v;v;h;h
=

v;v = h;h = n

n ; x = x , with x := n

n;n
=

n .

In words, ipping verti ally, then rotating through 1800 about the entre, and then
ipping horizontally is the same as doing nothing. (Note how asso iativity is used impli itly between the rst and se ond steps. The use of an in x operator for \followed
by" fa ilitates this all-important al ulational te hnique.)
Constru t a two-dimensional table that shows the e e t of exe uting
two operations x and y in turn. The table should have four rows and four olumns,
ea h labelled by one of n , v , h and c . (Use the physi al pro ess of ipping squares to
onstru t the entries.)
Use the table to verify that, for x and y in the set {n,v,h,c} ,
Exercise 8.6

(8.7)

x;y = y;x .

Che k also that, for x and y in the set {n,v,h,c} ,


(8.8)

x ; (y ; z) = (x ; y) ; z .

(In prin iple, you need to onsider 43 , i.e. 64 , di erent ombinations. Think of ways to
redu e the amount of work.)

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.
Identify a omplete set of operations on a 2 2 square and extend table 3 so that it
shows the e e t of omposing any pair of operations.
Exercise 8.9

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

125

8.3. Partitioning the Board

8.3

Partitioning the Board

The identi ation of the four operations on supersquares is a signi ant step towards
solving the Knight's- ir uit problem. Suppose one of the supersquares is labelled \ n ".
Then the remaining fteen supersquares an be uniquely labelled as n , v , h or c
squares, depending on their position relative to the starting square. Figure 8.9 shows
how this is done. Suppose we agree that the bottom-left square is an \ n " square. Then
immediately above it is a \ v " square, to the right of it is an \ h " square, and diagonally
adja ent to it is a \ c " square. Supersquares further away are labelled using the rules
for omposing the operations.
As a onsequen e, all 64 squares of the hessboard are split into four disjoint sets. In
g. 8.9, the di erent sets are easily identi ed by the olour of the square. Two squares
have the same olour equivales they an be rea hed from ea h other by straight Knight's
moves. (That is, two squares of the same olour an be rea hed from ea h other by
straight Knight's moves, and two squares of di erent olour annot be rea hed from
ea h other by straight Knight's moves.)

111
000
000
111
00
11
00
11
000
111
000
111
00
11
00
11
v 11
c11
v 11
c11
000
111
000
111
00
00
000
111
000
111
00
00
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
n
h
n
h
000
111
00011
111
0011
00
000
111
00011
111
0011
00
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
v 11
c11
v 11
c11
000
111
000
111
00
00
000
111
000
111
00
00
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
000
111
000
111
00
11
00
11
n
h
n
h
000
111
00011
111
0011
00
000
111
00011
111
0011
00
000
111
00011
111
0011
00
000
111
00011
111
0011
00
Figure 8.9: Labelling Supersquares

Re all the dis ussion of straight-move ir uits in se tion 8.1. There we established

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

126

8. Knight's Cir uit

the simple fa t that it is possible to onstru t a straight-move ir uit of a board of whi h


one side has even length and the other side has length at least two. In parti ular, we
an onstru t a straight-move ir uit of a 4 4 board.
Now, a \straight" Knight's move is \straight" with respe t to the supersquares of a
hessboard. That means we an onstru t straight-move ir uits of ea h of the four sets
of squares on the hessboard. In g. 8.9, this means onstru ting a ir uit of all the red
squares, a ir uit of all the green squares, a ir uit of all the blue squares, and a ir uit
of all the yellow squares.
We now have four disjoint ir uits that together visit all the squares of the hessboard. The nal step is to ombine the ir uits into one. The way to do this is to exploit
the \diagonal" Knight's moves. (Refer ba k to g. 8.7 for the meaning of \diagonal" in
this ontext.)
A simple way of ombining the four ir uits is to ombine them in pairs, and then
ombine the two pairs. For example, we an ombine the red and blue ir uits into
a \red-blue" ir uit of half the board; similarly, we an ombine the green and yellow
ir uits into a \green-yellow" ir uit. Finally, by ombining the red-blue and green-yellow
ir uits, a omplete ir uit of the board is obtained.

Figure 8.10: S hema for forming \red-blue" and \green-yellow" ir uits. The straightmove ir uits are depi ted as oloured ir les, a single move in ea h being depi ted by
a dotted line. These straight moves are repla ed by the diagonal moves, shown as solid
lines.
Figure 8.10 shows s hemati ally how red-blue and green-yellow ir uits are formed;
in ea h ase, two straight moves (depi ted by dotted lines) in the respe tive ir uits are
repla ed by diagonal moves (depi ted by straight lines). Figure 8.11 shows one way of
hoosing the straight and diagonal moves in order to ombine red and blue ir uits, and
green and yellow ir uits | in ea h ase, two \parallel" straight moves are repla ed by
two \parallel" diagonal moves. Exploiting symmetry, it is easy to nd similar \parallel

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

127

8.3. Partitioning the Board

moves" with whi h to ombine red and yellow ir uits, or green and blue ir uits. On the
other hand, there are no diagonal moves from red to green, or from yellow to blue squares;
onsequently, it is impossible to onstru t a \red-green" or a \yellow-blue" ir uit.

11
00
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
00011
111
0011
00
000
111
00011
111
0011
00
00
11
00011
111
0011
00
000
111
00011
111
0011
00
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
00011
111
0011
00
000
111
00011
111
0011
00
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
000
111
00
11
00
11
000
111
000
111
00
11
00
11
00
11
00011
111
0011
00
000
111
00011
111
0011
00
00
11
00011
111
0011
00
000
111
00011
111
0011
00

Figure 8.11: Forming \red-blue" and \green-yellow" ir uits. The straight moves shown
as dotted lines are repla ed by the diagonal moves shown as solid lines.
Red and blue straight-move ir uits have been ombined in g. 8.12 to form a \redblue" ir uit. The method of ombination is indi ated by the dotted and solid lines: the
straight moves (dotted lines) are repla ed by diagonal moves (solid lines). To omplete a
ir uit of the whole board, with this red-blue ir uit as basis, a green-yellow ir uit has
to be onstru ted, and this ir uit ombined with the red-blue ir uit. This is left as an
exer ise.
A slight di ulty of this method is that it onstrains the straight-move ir uits that
an be made. For example, onsidering the suggested method for ombining red and blue
ir uits in g. 8.11, no onstraint is pla ed on the blue ir uit (be ause there is only
one way a straight-move ir uit of the blue squares an enter and leave the bottom-left
orner of the board). However, the straight-move ir uit of the red squares is onstrained
by the requirement that it make use of the move shown as a dotted line. The di ulty is
resolved by rst hoosing the ombining moves and then onstru ting the straight-move

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

128

8. Knight's Cir uit

Figure 8.12: A \red-blue" ir uit. \Parallel" red and blue moves, shown as dotted lines,
are repla ed by diagonal moves, shown as thi ker solid lines, thus ombining the two
ir uits.
ir uits appropriately.
In order to onstru t a Knight's ir uit of smaller size boards, the di erent pairs of
ombining moves need to be positioned as lose as possible together. This is possible in
the ase of an 8 6 board, but not for smaller boards.
Constru t a Knight's ir uit of an 8 8 board using the s heme dis ussed above. Do the same for an 8 6 board. Indi ate learly how the individual
ir uits have been ombined to form the entire ir uit.
Exercise 8.10

Figure 8.13 illustrates another way that the ir uits an be ombined.


The four straight-move 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 between 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. 8.13 have to be identi ed.
The key to doing this with a minimum of e ort 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
Exercise 8.11

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

129

8.3. Partitioning the Board

Figure 8.13: S hema for Combining Straight-Move Cir uits. Four straight moves (indi ated by dotted lines) are repla ed by four diagonal moves (indi ated by solid bla k
lines).
pair of green moves in the bottom-left orner, severly restri ts the hoi e of diagonal
moves; in ombination with symmetry, this makes the appropriate moves easy to nd.

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 straightmove ir uits. If you en ounter di ulties, try turning the board through 90 whilst
maintaining the orientation of the ombining moves.)

Exercise 8.12 Division of a board of size (4m + 2) (4n + 2) into supersquares yields
a (2m + 1) (2n + 1) \super" board. Be ause this superboard has an odd number of

(super) squares, no straight-move ir uit is possible, and the strategy used in exer ise
8.10 is not appli able. However, it is possible to onstru t Knight's ir uits for boards of
size (4m + 2) (4n + 2) , whenever, both m and n are at least 1 , by exploiting exer ise
8.2.
The strategy is to onstru t four straight-move ir uits of the board omitting one of
the supersquares. (Re all exer ise 8.2 for how this is done.) Then, for ea h ir uit, one

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

130

8. 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. 8.14.

11
00
00
11
000
111
000
111

Figure 8.14: Strategy for Constru ting a Knight's Cir uit of (4m + 2) (4n + 2) boards.
One supersquare is hosen, and four straight-move ir uits are onstru ted around the
remaining squares. These are then onne ted as shown.
Complete the details of this strategy for a 6 6 board. Make full advantage of the
symmetry of a 6 6 board. In order to onstru t the twelve ombining moves depi ted
in g. 8.14 , it su es to onstru t just three; the remaining nine an be found by a
rotating the moves through a right angle.
Explain how to use your solution for the 6 6 board to onstru t Knight's ir uits of any board of size (4m + 2) (4n + 2) , whenever, both m and n are at least
1.

8.4

Discussion

In the absen e of a systemati strategy, the Knight's ir uit problem is a truly di ult
problem to solve, whi h means it is a very good example of dis iplined problem-solving

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

131

8.4. Dis ussion

skills. The method we have used to solve the problem is essentially problem de omposition | redu ing the Knight's ir uit problem to onstru ting straight-move ir uits,
and ombining these together.
The key riterion for a good method is whether or not it an be extended to other
related problems. This is indeed the ase for the method we have used to solve the
Knight's ir uit problem. The method has been applied to onstru t a ir uit of an
8 8 hessboard, but the method an learly be applied to mu h larger hessboards.
The key ingredients are

 the lassi ation of moves as \straight" or \diagonal",


 straight-move ir uits of supersquares, and
 using diagonal moves to ombine straight-move ir uits .
On e the method has been fully understood, it is easy to remember these ingredients,
and reprodu e a Knight's ir uit on demand. Contrast this with remembering the ir uit
itself, whi h is obviously highly impra ti al, if not impossible.
A drawba k of the method is that it an only be applied to boards that an be divided
into supersquares. As we have seen, it is not possible to onstru t a Knight's ir uit of a
board with an odd number of squares. That leaves open the ases where the board has
size (2m) (2n + 1) , for some m and n . (That is, one side has even length and the
other has odd length.) For those interested, a omplete hara terisation of the sizes of
board for whi h a Knight's ir uit exists is given in se tion 8.5.
The Knight's- ir uit problem exempli es a number of mathemati al on epts whi h
you will probably en ounter elsewhere. The n , v , h and c operations together form
an example of a \group"; the relation on squares of being onne ted by straight moves
is an example of an \equivalen e relation", and the fa t that this relation partitions the
squares into four disjoint sets is an example of a general theorem about \equivalen e
relations".
The Knight's- ir uit problem is an instan e of a general lass of problems alled
\Hamiltonian-Cir uit Problems". In general, the input to a Hamiltonian- ir uit problem
is a so- alled \graph" (a network of \nodes" with \edges" onne ting the nodes) and the
requirement is to nd a path through the graph that visits ea h node exa tly on e, before
returning to the starting node. Hamiltonian- ir uit problems are, in turn, instan es of a
lass of problems alled \NP- omplete" problems. NP- omplete problems are problems
hara terised by ease of veri ation but di ulty of onstru tion. That is, given a
putative solution, it is easy to he k whether or not it is orre t (for example, given
any sequen ing of the squares on a hessboard, it is easy to he k whether or not it is a
Knight's ir uit); however, for the lass of NP- omplete problems, no e ient methods

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

132

8. 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.

8.5

Boards of Other Sizes

To be written.

8.6

Bibliographic Remarks

Solutions and histori al information on the Knight's ir uit problem an easily be found
on the internet. A ording to one of these [Ma Quarrie, St. Andrews Univ. the rst
Knight's tour is believed to have been found by al-Adli ar-Rumni in the ninth entury,
long before hess was invented.
The use of supersquares in solvingthe Knight's ir uit problem is due to Edsger W.
Dijkstra [Dij92. His solution is for a standard-sized hessboard and uses the method of
ombining straight-move ir uits des ribed in exer ise 8.11. The pairwise ombination
of straight-move ir uits is due to Diethard Mi haelis [private ommuni ation. Both
solutions involve sear hing for \parallel moves". Mi haelis's solution is slightly preferable
be ause just two pairs of \parallel moves" have to be found at ea h stage. Dijkstra's
solution involves sear hing for four pairs (twi e as many) at the same time, making it a
little bit harder to do. The solution to exer ise 8.12 was onstru ted by the author, with
useful feedba k from Mi haelis.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Solutions to Exercises
2.1 1233 games must be played. Let k be the number of players that have been kno ked
out, and let g be the number of games that have been played. Initially, k and g are
both equal to 0 . Every time a game is played, one more player is kno ked out. So, k
and g are always equal. To de ide the tournament, 12341 players must be kno ked

out. Hen e, this number of games must be played.


In general, if there are p players, the tournament onsists of p1 games.

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 , n1 , p1 .

Consider the di eren es mn , np , pm . It is easily he ked that parity of ea h


is un hanged by the assignment. (In ea h ase, the di eren e either is un hanged or
in reases or de reases by 2 .) Also, the number of odd di eren 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 di eren es, we on lude that the goal is impossible to rea h if the starting
state has zero odd di eren 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 di erent 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.
Formally, we exe ute:
do

mnp = m m,n,p := m+1 , n1 , p1

od

Algorithmi Problem Solving Roland Ba khouse.

133

January 7, 2007

134

Solutions to Exer ises

3.1
{

5C ||

2C,3H |3W| ; 2C,3H |1W| 2W ; 5H |3W| 2W


;

5H || 5W

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

3.2 We modify the solution to the ve- ouple problem, e e 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 ||

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

135

Solutions to Exer ises


1C,3H |3W| ; 1C,3H |1W| 2W ; 4H |2W| 2W
;

4H || 4W

4H |1W| 3W ; 1C |3H| 3W
;

1C || 3C }

1C |1C| 2C
;

2C || 2C }

2W |2H| 2C ; 2W |2W| 4H
;

4W || 4H

2W |2W| 4H ; 2W |1W| 1C,3H ; |3W| 1C,3H


{

|| 4C

3.3 Let M denote the apa ity of the boat, and let N denote the number of ouples.
We assume that N is at least 2 and M is at most the minimum of 3 and N/2 . (These
properties are ommon to the ases of a boat of apa ity 2 and 4 ouples, and a boat
of apa ity 3 and 6 ouples.)
Let lH denote the number of husbands on the left bank. The number of husbands
on the right bank, denoted rH , is then NlH . Similarly, let lW denote the number
of wives on the left bank. The number of wives on the right bank, denoted rW , is then
NlW .

We note that an invariant is

(13)

(lH = lW) (lH = 0) (lH = N) .

That is, either there are no single individuals on either bank, or all husbands are on one
of the two banks. It is a requirement of any solution that this property is an invariant. (If
not, either 0 < lH < lW or 0 < rH < rW . In words, the wives outnumber the husbands
on the left bank or on the right bank. In both ases, the solution is invalid.)
Now, we laim that, under the given assumptions on M and N , either
(a) The boat is on the left bank and

(14)

M < lH ,

or
(b) the boat is on the right bank and

(15)

M lH .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

136

Solutions to Exer ises

Property (a) is learly true initially. (Re all the assumptions about M and N .)
Now, suppose (a) is true and, then, a rossing is made from left to right. Note that
lH 6= 0 both before and after the rossing. (Before the rossing, lH = 0 is ex luded by the
assumption that M < lH . After the rossing, lH = 0 is impossible be ause at most M
husbands an ross together.) So, the invariant (13) an be strengthened: the rossing
is made starting in a state in whi h
(16)

(lH = lW) (lH = N) ,

and must result in a state satisfying this property. Otherwise, the rossing is invalid.
Note also that a left-to-right rossing auses lH and/or lW to de rease.
We onsider two ases before the rossing: lH = N , and (lH = lW) (lH 6= N) .
Sin e at most N/2 husbands an ross together, if lH = N before the rossing, at
least N/2 are left at the left bank. That is, (b) is true after the rossing.
If (lH = lW) (lH 6= N) before the rossing, the property lH = lW must be maintained by the rossing. (This is be ause (16) must be maintained and the rossing annot
in rease lH .) That is, an equal number of wives and husbands must make the rossing.
Sin e only one ouple an ross at one time, the value of lH is de reased by at most 1 .
But (14) is assumed to be true before the rossing; onsequently, (15) is true after the
rossing, and the boat is at the right bank. Thus, (b) is true after the rossing.
In both ases, we have established that, after the rossing, (b) is true.
Now suppose (b) is true and a rossing is made from right to left. A right-to-left
rossing auses lH and/or lW to in rease. Sin e lH 6= 0 before the rossing, and lH
does not de rease, lH 6= 0 after the rossing. (Stri tly, we need to assume that M is
non-zero in order to assert that lH 6= 0 . Obviously, the ase that the boat has apa ity
0 an be ex luded from onsideration!)
Again, we onsider two ases: this time, no husbands ross, and some husbands ross.
If husbands ross, the a t of rossing in reases lH ; so, after the rossing M < lH and
(of ourse) the boat is at the left bank. Thus, (a) is true after the rossing.
If only wives ross, it must be the ase that lH = N (be ause (16) must be maintained
and, if only wives ross, it is impossible to maintain that lH = lW ). That is, lH is
un hanged, and M < lH both before and after the rossing. After the rossing, the boat
is, of ourse, on the left side of the bank. Thus, (a) is true after the rossing.
In both ases, we have established that (a) is true after the rossing.
In summary, property (a) is true initially. Also, if (a) is true and a left-to-right
rossing is made, (b) be omes true; vi e-versa, if (b) is true and a right-to-left rossing
is made, (a) be omes true. So, at all times, either (a) or (b) is true. That is, it an never
be the ase that all husbands are at the right bank.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

137

Solutions to Exer ises

5 rossings are needed, of whi h 2 are return trips. So, 2 people never make
a return trip. Clearly, an optimal solution is found when persons 3 and 4 |the two

3.4

slowest| do not return. (Consider any sequen e of rossings that gets all four a ross
the bridge. If one or both of the two slowest makes a return journey, then one or both of
the two fastest does not make a return journey. By inter hanging a slower person with
a faster person in the sequen e, a new sequen e is found whi h is at least as fast as the
original sequen e | sin e the time for at least one return trip is redu ed and the time
for at most one forward trip is in reased by the same amount.)
There are two strategies for getting the two slowest a ross: let them ross together
or let them ross seperately. The strategy \let the two slowest ross together" is implemented by letting persons 1 and 2 (the two fastest) ross, with person 1 returning.
Then persons 3 and 4 ross, and person 2 returns. Finally, persons 1 and 2 ross
again. The total time taken using this strategy is
t2 + t1 + t4 + t2 + t2 .

If the two slowest ross seperately, then the best strategy is to let person 1 ross with
them and then return for the other. Implementing this strategy takes a total time of
t2 + t1 + t4 + t1 + t3 .

(This orresponds to person 2 and person 1 rossing, then person 1 returning, then
person 4 and person 1 rossing, then person 1 returning, and nally persons 1 and 3
rossing. The order in whi h persons 2 , 3 and 4 ross is, of ourse, immaterial. The
order hosen here fa ilitates the omparison of the times.)
Comparing the total times, the rst strategy should be used when t2+t2 t1+t3 and
the se ond strategy when t1+t3 t2+t2 . (There is a small element of nondeterminism
in this solution: when t2 + t2 = t1 + t3 an arbitrary hoi e may be made between the two
strategies.)
Applying this solution to the two spe i ases, we get:
(a) The times taken are 1 minute, 1 minute, 3 minutes and 3 minutes: Sin e 1+1 1+3 ,
the two slowest should ross together. The shortest time is 1+1+3+1+1 , i.e. 7

minutes.

(b) The times taken are 1 minute, 4 minutes, 4 minutes and 5 minutes. Sin e
1+5 4+4 , the two slowest should ross seperately. The shortest time is 4+1+5+1+4 ,
i.e. 15 minutes. (The shortest time if the two slowest ross together is 4+1+5+4+4 ,
i.e. 18 minutes

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

138

Solutions to Exer ises

3.5 62331 , i.e. 108 .

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 di eren e.
b) In De ember, the losing positions are the odd-numbered days and the winning positions are the even-numbered days. (Take are: The \losing positions" are the days that
the winning player names. This is in line with the terminology of losing and winning
positions.) That is, if the last-named day is an odd-numbered day, the player whose turn
it is must name an even-numbered day and, so, will eventually lose.
In parti ular, the player who names 1st De ember wins. Any day in November is
thus a winning position. In O tober, like De ember, the odd-numbered days are losing
positions, and any day in September is a winning position. Similarly, in August, the
odd-numbered days are losing positions, and any day in July is a winning position.
The pattern hanges in June, whi h has an even number of days. The player who
names 1st July loses; onsequently, any even-numbered day in June is a losing day.
This means that every even-numbered day in May is a winning day; ; also, every evennumbered day in April is a winning day. This means that 31st Mar h is a losing day.
Sin e Mar h has an odd number of days, the pattern we saw for De ember and November
re urs. Every odd-numbered day in Mar h is a losing day, and every day in February is
a winning day. Finally, the odd-numbered days in January are losing days.
We on lude that the se ond player is guaranteed to win. The strategy is to name
the 1st day of the following month when the last-named day is in November, September,
July or February. Otherwise, the strategy is to name the next day of the year.
Again, it does not matter if it is a leap-year.

4.2

The rst eleven positions:


Position 0
Type
L
Move

10

Table 1: Winning (W) and Losing (L) Positions for subtra tion set {2 , 5 , 6}

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

139

Solutions to Exer ises

The pattern repeats in the se ond eleven positions:


Position 11 12 13 14 15 16 17 18 19 20 21
Type
L L W W L W W W L W W
2
2
5
6
6
5
6
Move
Table 2: Winning (W) and Losing (L) Positions for subtra tion set {2 , 5 , 6}

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 identi ed as winning or losing positions are
ones in a y le of positions. Labelling of winning and losing positions assumes that every
game is guaranteed to terminate no matter how either player moves. If y les o ur this
assumption is not valid.
***Remainder to be written.***
4.3

4.4

To be written.

5.5

Let col be the olour of the square, and n be the number of moves. A move is

then

col , n := col , n+1 .

An invariant of this assignment is


col even.n .

An odd number of moves ( 63 ) is needed, but the olour of the square doesn't hange.
So, in order to move the knight as required, a hange has to be made to col even.n ,
whi h is impossible.

5.6 Suppose the number of ouples is n . There are 2n people who ea h shake hands
with between 0 and 2n 1 people. If the 2n 1 people all shake hands with a di erent
number of people, there must be someone who shakes hands with k people for ea h k
between 0 and 2n 1 .
If n is 1 , the only person other than the host is the host's partner. Sin e ouples
do not shake hands, both shake hands 0 times.
Now suppose that n is greater than 1. In this ase, there are at least two people

other than the host and the host's partner. Consider the two people who shake hands

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

140

Solutions to Exer ises

0 and 2n 1 times. The person who shakes hands 2n 1 times does so with everyone

ex ept their partner. By the symmetry of the shake-hands relation, it is thus the ase
that everyone ex ept that person's partner shakes hands with at least one person. It
follows that the two people who shake hands 0 and 2n 1 times are husband and wife.
Be ause neither is the host, it also follows that neither is the host's partner
Now suppose we dis ount this ouple. That is, we onsider the party onsisting of
the other n1 ouples. The number of times ea h person shakes hands is then redu ed
by one. So, again, all but the host have shaken hands a distin t number of times.
Repeating this pro ess, we eliminate all the ouples one by one until the party has
been redu ed to just the host and the host's partner. Ea h time, the number of times
the host and the host's partner shake hands is redu ed by one. The host and the host's
partner must therefore have both shaken hands n1 times.

5.8
(a) false
(b) false
(c) false
(d) p
(e) false
(f) q 6 r
(g) p
(h) true

5.9
true
=

law p p false with p := true

true false
=

law true p p with p := false

false .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

141

Solutions to Exer ises


5.10
p
=

law p p false with p := p

p false
=

law p p false with p := p


and symmetry of equivalen e }

p .

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
=

de nition of 6

false b
=

de nition of negation: (5.3) }

b .

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.

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.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

142

Solutions to Exer ises

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 di erent ways of olouring
the entire sheet of paper. However, two of these are unsatisfa tory be ause the olours
of regions adja ent at the hosen line must be di erent. This leaves exa tly two ways of
olouring the paper with n+1 lines.

6.5 When m is 0 , there is just one obje t. This is the unique obje t and 0 (whi h
equals 20 ) omparisons are needed to dis over that fa t.
Suppose now that m is greater than 0 . Split the 3m obje ts into 3 groups ea h of
3m1 obje ts. One of these 3 groups will have a di erent weight to the other two, whi h
will be of equal weight. At most 2 omparisons are needed to determine whi h of the
groups it is. Then, by indu tion, at most a further 2(m1) omparisons are required
to nd the unique obje t in that group. This gives a total of 2(m1) + 2 , i.e. 2m ,

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.

7.1 Formally we have


To.d
=

de nition of T

length(H0.d)
=

de nition of H0.d

length.[ ]
=

de nition of length

0 ,

and
Tn+1.d
=

de nition of T

length(Hn+1.d)
=

de nition of Hn+1.d

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

143

Solutions to Exer ises


length(Hn.d ; [hn+1 , di] ; Hn.d)
=

de nition of length

length(Hn.d) + length([hn+1 , di]) + length(Hn.d)


=

de nition 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 2n1 . The simple indu tive proof is omitted.

7.2 We begin by onsidering the permissible states that the puzzle may be in. In any

state, the disks on any one pole are in order of de reasing size. So, if we want to spe ify
the state of the puzzle we only need to spe ify whi h pole ea h disk is on. For example,
suppose there are ve disks and suppose we spe ify that disk 1 is on pole A , disk 2 is
on pole B , disks 3 and 4 are on pole A and disk 5 is on pole B . Then disk 4 must
be on the bottom of pole A , disk 3 must be on top of it, and disk 1 must be on top of
disk 3 . Also, disk 5 must be on the bottom of pole B and disk 2 must be on top of it.
No other arrangement of the disks satis es the rule that no disk is above a disk smaller
than itself.
The state of an n -disk puzzle an thus be spe i ed 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
di erent states in the n -disk problem.
Now we onsider the transitions between states. We onsider rst the problem where
there are no disks, then the 1 -disk problem, then the 2 -disk problem, and then we
onsider the general n -disk problem.
When there are no disks there is exa tly one state: the state when there are no disks
on any of the poles. This is shown in g. 15. (You may have di ulty seeing the gure.
It onsists of a single dot!)
We now explain how to onstru t the state-transition diagram for the ( n+1 )-disk
problem, for an arbitrary n , given that we have onstru ted the diagram for the n -disk

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

144

Solutions to Exer ises

Figure 15: State-transition diagram for 0-disk problem.


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 i es the lo ation of
the largest disk. Thus, ea h state in the state-transition diagram for the n -disk problem
gives rise to 3 states in the state-transition diagram for the ( n+1 )-disk problem. That
is, a state in the state-transition diagram for the ( n+1 )-disk problem is spe i ed 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 a e 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 state-transition diagram for the ( n+1 )-disk problem given the
state-transition diagram for the n -disk problem is to make three opies of the latter.
The p th opy is then modi ed 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 di erent
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 state-transition 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 .

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

145

Solutions to Exer ises

AnA

BnA

CnC

CnA

CnB

BnC

AnC

AnB

BnB

Figure 16: Constru tion of the state-transition diagram for the (n + 1) -disk problem

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

146

Solutions to Exer ises

7.3 Even, be ause the dire tion of movement is opposite to that of the smallest disk

(whi h has an odd number).

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 justi ed as follows. When step 1 is exe uted, we know that the
rst k1 disks are all on the pole in dire tion d from disk k . Progress is made if
these k smallest disks an be transferred to the same pole. To do this, it is ne essary
to move the k1 smallest disks in the dire tion d . The dire tion that disk 1 has to
be moved is thus d where
even.(k1) d even.1 d .

Simplifying, we get that d = (odd.k d ) . (In words, the dire tion that the smallest
disk is moved should be the same as the dire tion that disk k is moved, if k is also
odd; otherwise the smallest disk is moved in the opposite dire tion to disk k .) The
orre tness of the Towers of Hanoi program then guarantees that this a tion will initiate
a sequen e of moves after whi h all k1 disks will have been moved onto disk k . During
this sequen e of moves the smallest disk will ontinue to move in the same dire tion. On
ompletion, however, the dire tion of the smallest disk may or may not be reversed.
The only time that step 1 annot be exe uted is when all the disks are on the same
pole, as required.

The solution is to pla e the disks in order, starting with the largest and ending
with the smallest. Let k denote the number of the disks still to be repla ed; so, initially
k is N and we are done when k is 0 . Ea h time the value of k is reassigned, we ensure
that the k smallest disks are on the same pole.
If the k th disk is on the right pole, de rease k by 1 . Otherwise, suppose it needs
to be moved in dire tion d from its urrent position. Move the smallest k1 disks in
the dire tion d , then move disk k to its rightful position. Finally, de rease k by 1 .
Continue this pro ess until k is 0.
7.5

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

147

Solutions to Exer ises


8.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 di erent from the olour
of the starting square. So, after an odd number of moves, it is impossible to return
to the starting square.

(b) It's easy to see that a straight-move ir uit of a 21 board is possible |starting

at one of the squares, move to the other square and then move ba k again| , but,
otherwise, no straight-move ir uit is possible. If m is greater than 1 , at least one
square is two moves from the starting square; it is impossible to visit su h a square
and return to the starting square without visiting the in-between square more than
on e.

(c) See (b) for a ir uit of a 21 board. For n greater than 1 , a straight-move ir uit
of a 2n board is ompleted by starting at a orner, moving one-by-one to all the

squares in the same row, then returning via the se ond row.

8.2 For the 3 3 board, a ir uit an be onstru ted exa tly when the omitted square is

not adja ent to a orner square. For larger boards, the same ondition applies. Suppose
the oordinates of the omitted square are (m, n) . (It doesn't matter whether numbering
starts at zero or one.) Then a ir uit an be onstru ted of the remaining squares exa tly
when even.m = even.n . The onstru tion is to split the board into four re tangular
boards in su h a way that the to-be-omitted square is at a orner of a board with an odd
number of squares. The other three boards ea h have an even number of squares, and at
least one of them has at least one square. Constru t ir uits of these three boards, and
|indu tively, with the 3 3 board as the base ase| a ir uit of the board with the
omitted square. Then, onne t the ir uits together as shown in g. 17 .

8.6

n
v
h
c

n v
v n
h c
c h

h
c
n
v

c
h
v
n

Table 3: Sequential Composition of Flip Operations

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

148

Solutions to Exer ises


odd

even

odd

even

even

Figure 17: Straight-move ir uits (shown in red) of a 3 3 board, omitting one of the
squares.
Property (8.7) is veri ed by observing that the table is symmetri about the top-left
to bottom-right diagonal. Veri ation of the asso iativity property is mu h more tedious.
The ase that x , y or z is n an be dealt with simply. This leaves 27 other ases to
onsider. This is an example of a \tedious, but straightforward" proof!

8.9 To be added.

See g. 18.
The parallel moves used to onne t ir uits of di erent olours are indi ated by dotted
lines depi ting the straight moves, and solid bla k lines, depi ting the diagonal moves.
Note how the hoi e of parallel moves onstrains the hoi e of red ir uit. In fa t, the
red moves are entirely di tated by this hoi e. In ontrast, there is omplete freedom
in hoosing a blue or yellow ir uit. There is some freedom in hoosing a green ir uit,
but not omplete freedom. In this way, a substantial number of ir uits an be found all
based on the same set of ombining parallel moves. In the ir uit shown, the green, blue
and yellow ir uits were onstru ted by \ opying" the red ir uit.
The same set of ombining parallel moves an be used to onstru t a ir uit of an
8 6 board; all that is required is to \shorten" the straight-move ir uits in order to
a ommodate the smaller board. (But note that they annot be used to onstru t a
ir uit of a 6 8 board.)
8.10

8.11 Figure 19 shows details of how the straight-move ir uits are ombined. Moves

indi ated by dotted lines are repla ed by the diagonal moves indi ated by solid bla k
lines.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

149

Solutions to Exer ises

Figure 18: A Knight's Cir uit. Solid lines indi ate the ir uit. The dotted lines depi t
straight moves that are repla ed. The diagonal moves that repla e them are depi ted by
solid bla k lines.
Figure 20 shows the ir uits obtained in this way. The dotted lines are not part of
the ir uit; these are the moves that are repla ed.
In order to onstru t a ir uit for any board of size 4m 2n , where m is at least
2 and n is at least 3 , it su es to use the te hnique detailed in gs. 8.2 and 8.3 for
extending straight-move ir uits to boards of arbitrary size. This onstru tion has to
be applied four times, on e for ea h of the straight-move ir uits in the solution to the
8 6 -board problem.

8.12 We begin by identifying the moves shown in g. 8.14. See g. 21. (Note the

symmetry.)
Now it is easy to ll in the straight-move ir uits around the remaining squares. See
g. 22.
For the general problem, it is easy to extend the straight-move ir uits.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

150

Solutions to Exer ises

Figure 19: Details of how the four straight-move ir uits are ombined; the straight
moves indi ated by dotted lines are repla ed by diagonal moves indi ated by solid bla k
lines.)

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

151

Solutions to Exer ises

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.

January 7, 2007

152

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
represent the moves that repla e them.

Figure 22: Knight's Cir uit of a 6 6 board. The dotted lines do not form part of the
ir uit.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Bibliography
[Ba 03 Roland Ba khouse. Program Constru tion. Cal ulating Implementations
From Spe i ations. John Wiley & Sons, Ltd., 2003.
[BCG82 Elwyn R. Berlekamp, John H. Conway, and Ri hard K. Guy. Winning Ways,
volume I and II. A ademi Press, 1982.
[BF01

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. ir ulated privately,
September 1990.

[Dij92

Edsger W. Dijkstra. EWD1135: The knight's tour. ir ulated privately,


September 1992.

[Dij97

Edsger W. Dijkstra. EWD1260: The marked oins and the s ale. ir ulated
privately, Mar h 1997.

[DS90

Edsger W. Dijkstra and Carel S. S holten. Predi ate Cal ulus and Program
Semanti s. Texts and monographs in Computer S ien e. Springer-Verlag,
1990.

[DW00 John P. D'Angelo and Douglas B. West. Mathemati al Thinking. ProblemSolving and Proofs. Prenti e Hall, 2000.
[Gri81

David Gries. The S ien e of Programming. Springer-Verlag, 1981.

[GS93

David Gries and Fred B. S hneider. A Logi al Approa h to Dis rete Math.
Springer-Verlag, 1993.

Algorithmi Problem Solving Roland Ba khouse.

153

January 7, 2007

154

Solutions to Exer ises

[Lev03 Anany Levitin. Introdu tion to The Design And Analysis of Algorithms.
Addison Wesley, 2003.
[Smu78 Raymond Smullyan. What Is The Name Of This Book?
Englewood Cli s, N.J., 1978.
[Ste97

Prenti e-Hall,

Ian Stewart. The Magi al Maze. Weiden eld and Ni olson, London, 1997.

[Tar56 Alfred Tarski. Logi , Semanti s, Metamathemati s, papers from 1923 to


1938. Oxford University Press, 1956. Translated by J.H.Woodger.
[Wil87 J.G. Wiltink. A de ien y of natural dedu tion. Information Pro essing
Letters, 25:233{234, 1987.

Algorithmi Problem Solving Roland Ba khouse.

January 7, 2007

Você também pode gostar