## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Roland Ba khouse

May 28, 2008

2

Algorithmi Problem Solving

Roland Ba khouse.

May 28, 2008

Contents

1 Introduction

1

1.1

Algorithms

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

1

1.2

Bibliographi Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

3

2 Invariants

2.1

5

Cho olate Bars

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

2.1.1

The Solution

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

2.1.2

The Mathemati al Solution

8

8

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

9

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

12

2.2

Empty Boxes

2.3

The Tumbler Problem

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

13

2.4

Tetrominoes . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

15

2.5

Additional Exer ises

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

19

2.6

Bibliographi Remarks . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

20

3 Crossing a River

3.1

Problems

3.2

Brute For e

3.3

21

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

22

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

22

3.2.1

Goat, Cabbage and Wolf . . . . . . . . . . . . . . . . . . . . . . . .

22

3.2.2

State-Spa e Explosion

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

25

3.2.3

Abstra tion . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

26

Jealous Couples . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .

27

3.3.1

What's The Problem?

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

28

3.3.2

Problem Stru ture

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

28

3.3.3

Denoting States and Transitions

3.3.4

Problem De omposition

3.3.5

A Review

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

29

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

31

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

34

3.4

Rule of Sequential Composition . . . . . . . . . . . . . . . . . . . . . . . .

35

3.5

Summary

39

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

Algorithmi Problem Solving

Roland Ba khouse.

i

May 28, 2008

2008 . . 89 6. . . . . . . . . . . . . . . . . . . . .3 . . . . . . . . . . . 5. . 70 5. . . . . . . . . . . . . . . . .4 Hidden Treasures . . . . . . . . . . 72 5. . . . . . . . . . . .5 Summary . . . . . . . . . . 43 4. . . . . . . . . . . . . . . Equivalen e and Continued Equalities . . . . . . . . 80 5. . .ii 4 Games 41 4. . . . . . . . . . . . . . . . . . . . . . .4. 84 5. . . . . . . . . . . . . . . . . . . . . . . . . . .2 Knights and Knaves . . . . . . . . . . . . . . . . . . . . . . . . . . . .3 Formulating Requirements . . . .2. . . . . . . . . . . . . . . . 45 4. . . . . . . . . . . . . . . . . . . . . . . . 48 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 54 4. . . .1 Assumptions . . . . . . . . . . . . . . . . . . . 44 4. .2 Cutting The Plane . . . . . . 83 5. . . . . . . . . . . . . . . . .4 Negation . . . . . . . . . . . . . . . . . . . . . . . . 51 4. . . . . . .6 Handshake Problems . . . . . . . . . . . . . . . . . . . . . . .4. .2.1 Mat hsti k Games . . . . . .5 Equals for Equals . . . . 68 5.8 Summary . . . . . . . . . . . . . . . 55 4. . . .5 The Need For Proof 99 Algorithmi Problem Solving . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2. . . . . . .2. . . . . 65 5 Knights and Knaves 67 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Maintain Symmetry! . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .7 Inequivalen e . . . . . . . . . .4. .4. . . . . . . . . . . . . . . . . . . . . . .3 More Simple Sums . . . . . . . 57 4. .1 Example Problems . . 67 5. . . . . .3. . . . . . . . . . . . . . . . . . .4 Looking For Patterns . . .4 The MEX Fun tion . . . . . . . . . .6 Bibliographi Remarks . . . . . . . . . . . . . . .2. . . . . . . . . . . 93 6. . . . .2 Cal ulational Logi . . . . . . . . . . . . . . . . . . . 69 5. . 87 6 Induction 89 6. . . . . . . .1 Examples of the Asso iativity of Equivalen e 5. . . . . . . . . .1 Propositions 68 5. . . . . . . . . . . . . . . . . . . . . . . . . . . . .2. 78 5. . . . . .5 Contraposition . . .3 Triominoes . . . . . . . 56 4. . . . . . . . . 64 4. . . .4 Sums of Games . . . . . .5 Using the MEX Fun tion . . . Roland Ba khouse. . . 43 4. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 73 5. . . . . . . . .2. . . . . . . . . . . . . . . . . . . . . . . . . . . .4. . . . . . . . . . . . 96 6. . . 97 6. . . . . . . . . . . . . 77 5. . .1 Symmetry . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Labelling Positions . . . . . . . . .2 Winning Strategies . . . . . . . . . . 76 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . May 28. . . . . 41 4. . . . . . .2. . .1 Logi Puzzles . . .3 Boolean Equality . . . .2 On Natural Language 74 . . . . . . . . . . . . . .3.3 Subtra tion-Set Games . . . . . . . . . . . . . 59 4. .

iii 6.6 From Veri.

. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 The Towers of Hanoi 111 7. . . . . . . . . . .1 Spe i. . . . . 6. . . . . . . . . . . . . . . . . . ation to Constru tion 6. . . . . . . . . . . . . . . . . . . . . . . . . . . . .2 Problem Solution . . . . 6. . . . . . . . . . . . . . . . . . . . . . .8 Summary .9 Bibliographi Notes . . . . . . . .7 Fake-Coin Dete tion . . . . . . . . .7. . . . 6.7. . . . .1 Problem Formulation . . . . . 6. . . . . . . . .

. . . . . . . . Partitioning the Board Dis ussion . . . . . . .1. . Sequen ing Forward Trips . . . . . . .7 8. . .3 8. . . . . . . . . . . . . .4 Summary . . . . . . . . . . . . Bibliographi Remarks . . . . . . . . . . . . . . . . . . 137 141 145 150 152 152 Solutions to Exercises 153 Bibliography 177 Algorithmi Problem Solving Roland Ba khouse. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 2008 . . . . . . . . . . . . . . . . . . . . . Con lusion . . . . . Regular Sequen es . . . . . . . . . . . . . . .3 9. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .4 8. . . . . . . . . . . . . . .1. . . . . . . . . . . . . . . .1 9. . . . . . . . . .1 8. . . . . . . . . . . . . . . . . . 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Boards of Other Sizes Bibliographi Remarks . 7.6 8. .2 9. . . . . . . .5 9. . . . . . . . . 7.3 The Iterative Solution . . . . .3 WHY? . . . . . . . . . . . . . . 8 The Torch Problem 8. . . . . . . . . . . . . . . Choosing Settlers and Nomads The Algorithm . . . . . . . . . . . . . . . . . . . . . .5 8. . .5 Bibliographi Remarks . 7. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .1. . . . . . . . . . . . . . . . .2 8. . . . . . . . . . .2 Indu tive Solution . . . . . . . .1 The End of the World! 7. . . Outline Strategy . . . .4 9. . May 28. . . . . 9 Knight’s Circuit 9. . . . . . . . . . . . . . . . . . . ation and Solution . . . . . . . . . . . . . . . . . .8 111 111 112 113 113 117 119 120 121 Lower and Upper Bounds . .6 99 103 104 105 109 109 121 122 125 126 130 133 135 136 137 Straight-Move Cir uits Supersquares . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7. . . . . . . . . . . . . . . . . .2 Iterative Solution . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7. . . . . . . . . . . .

May 28.iv Algorithmi Problem Solving Roland Ba khouse. 2008 .

it is hardly in the embryoni stage of development. very new. Yet. Compared to its older sister |mathemati s| whi h is thousands of years old.Chapter 1 Introduction In histori al terms. The s ien e of omputing is yet newer. 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 . omputing s ien e is already having a major in uen e on our problem-solving skills. the digital omputer is very. amounting to a revolution in the art of ee tive reasoning.

1. and to hanges in the way that mathemati s is pra tised. This has led to advan es in logi .ne degree.1 Algorithms Solutions to programming problems are formulated as so- alled algorithms. These le tures form an introdu tion to problem-solving using the insights that have been gained in omputing s ien e. An algorithm is a well-de.

The four people take dierent amounts of time to ross the bridge. The . but they only have one tor h between them. Consider the following problem. that are exe uted in turn in order to solve the given problem. It is dark. whi h is typi al of some of the exer ises we dis uss. onsisting of a number of instru tions.ned pro edure. A on rete example may help to understand better the nature of algorithms and their relation to problem solving. and it is ne essary to use a tor h when rossing the bridge. when two ross together they must pro eed at the speed of the slowest. Four people wish to ross a bridge. The bridge is narrow and only two people an be on it at any one time. You may want to ta kle the problem before reading further.

rst person takes 1 minute to ross. the se ond 2 minutes. the third 5 minutes and the fourth 10 minutes. 2008 . The tor h must be ferried ba k and forth a ross Algorithmi Problem Solving Roland Ba khouse. 1 May 28.

instead. 2008 . an algorithm might input four numbers. is a mu h greater understanding of the solution. so that it is always arried when the bridge is rossed. most of the problems are quite diÆ ult. with only elementary mathemati al knowledge. The key to ee tive problem-solving is e onomy of thought and of expression | the avoidan e of unne essary detail and omplexity. May 28. like the bridge- rossing problem above. This book aims to impart these new skills and insights to a broad audien e. It aims to demonstrate the importan e of mathemati al al ulation. the algorithm should ompute an output whi h is related to the input by a ertain so- alled input-output relation. however. and output the total time needed to get all four a ross the bridge. but the hosen examples are typi ally not mathemati al. the output should be 30 and if the input is the numbers 1 . 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. the bridge- rossing problem an be generalised by allowing the number of people to be variable. A typi al instru tion will be: \persons x and y ross the bridge" or \person z rosses the bridge". be ause it is ne essary to formulate very learly and pre isely the pro edure for solving the problem. whereby a single error an ause an entire system to abruptly \ rash". Introdu tion the bridge. 20 . 6 the output should be 17 . 5 . For example. the harder it gets. 4 . 19 . an algorithm will have ertain inputs . 3 . it is perhaps not so surprising that the hallenges of algorithm design have had an immense impa t on our problem-solving skills. The book also aims to hallenge. An algorithm is typi ally more general than this. they are problems that are readily understood by a lay person. the rossing time for ea h person. The more general the problem. Coupled with the unforgiving nature of digital omputers. Formulating an algorithm makes problem-solving de idedly harder. Normally.2 1. thousands or even millions lines of ode. 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. for ea h input. Algorithmi Problem Solving Roland Ba khouse. at least to the untrained or poorly trained pra titioner. if the input is the numbers 1 . (For instan e. 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. using an example-driven approa h.) The advantage. Show that all four an ross the bridge within 17 minutes. The pro ess of formulating an algorithm demands a full understanding of why the algorithm is orre t. The input values are alled the parameters of the algorithm. In the ase of the bridge rossing problem.

2.3 1.2 Bibliographic Remarks I . Bibliographi Remarks 1.

The problem is also known as the \ ashlight" problem and the \U2" problem. Rote [Rot02℄ gives a omprehensive bibliography.rst found the bridge problem in [Lev03℄. 2008 . it is reputed to be used by at least one major software ompany in interviews for new employees. Algorithmi Problem Solving Roland Ba khouse. May 28.

Introdu tion Roland Ba khouse.4 Algorithmi Problem Solving 1. 2008 . May 28.

Chapter 2 Invariants \Invariant" means \not hanging". \law" and \pattern". An invariant of some pro ess is thus some attribute or property of the pro ess that doesn't hange. possibly the most important. The re ognition of invariants is an important problem-solving skill. We begin as we mean to go on. This hapter introdu es the notion of an invariant. We . and dis usses a number of examples of its use. Other names for \invariant" are \ onstant".

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

is now straightforward. it is your turn again. whi h we dis uss in detail. This pro ess is repeated as the problems get harder. we suggest a sequen e of steps whi h lead dire tly to the solution. The se ond problem. Then.rst problem we dis uss in detail. you should be able to solve the next ouple of problems. The third problem is quite easy. and then leave you to solve some more. From a proper understanding of the solution to these initial problems. and how to reason about assignments. but. we introdu e some basi skills related to omputer programming | the use of assignment statements. we demonstrate how to solve one problem. whi h otherwise would be quite hard. We leave it to you to solve. be ause the te hniques are new. You should . Along the way. but involves a new on ept. showing how an invariant is used to solve the problem.

(Thus ea h ut splits one pie e into two pie es. in the usual way. 1. A ut is made by taking a single pie e and utting along one of the grooves. Chocolate Bars. 5 May 28. 2008 . A re tangular ho olate bar is divided into squares by horizontal and verti al grooves. It is to be ut into individual squares.) Algorithmi Problem Solving Roland Ba khouse.nd them mu h easier to solve.

Invariants Figure 2.1 shows a 4×3 ho olate bar that has been ut into .6 2.

Tumblers.ve pie es. Eleven large empty boxes are pla ed on a table.1: Cho olate-Bar Problem. An unknown number of the medium boxes is sele ted and. eight small boxes are pla ed. How many uts in total are needed to ompletely ut the ho olate into all its pie es? 2. (See . Figure 2. eight medium boxes are pla ed. Empty Boxes. How many boxes are there in total? 3. The uts are indi ated by solid lines. Several tumblers are pla ed in a line on a table. into ea h. some are the right way up. Some tumblers are upside down. An unknown number of the boxes is sele ted and. into ea h. At the end of this pro ess there are 102 empty boxes.

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

May 28, 2008

7

to redu
e the number of balls in the urn to one by repeating the following pro
ess

as often as ne
essary.

Take any two balls out of the urn. If both have the same
olour, throw them away,

but put another bla
k ball into the urn; if they have dierent
olours then return

the white one to the urn and throw the bla
k one away.

Ea
h exe
ution of the above pro
ess redu
es the number of balls in the urn by one;

when only one ball is left the game is over. What, if anything,
an be said about

the
olour of the

(See .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.

3: Mutilated Chess Board been provided. 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.3. ea h domino will over exa tly two squares of the hessboard. 2.) An unlimited supply of dominoes has Figure 2. Tetrominoes A tetromino is a .g.

gure made from 4 squares of the same size. There are .

and I-tetrominoes. T. (See . Z-. L-.ve dierent tetrominoes. alled the O-.

Show that at least one side of the re tangle has an even number of squares. Overlapping tetrominoes or tetrominoes that sti k out from the sides of the board are not allowed.g. May 28. 2. 2008 . Algorithmi Problem Solving Roland Ba khouse.4. Assume that the board is made up of squares of the same size as the ones used to make the tetrominoes.) The following exer ises all on ern overing a re tangular board with tetrominoes. (a) Suppose a re tangular board is overed with tetrominoes.

Show that the number of squares is a multiple of 8 . L-.and I-tetromino (b) Suppose a re tangular board an be overed with T-tetrominoes.8 2. (d) An 8×8 board annot be overed with one O-tetromino and . T. Invariants Figure 2. ( ) Suppose a re tangular board an be overed with L-tetrominoes. Z-.4: O-. Show that the number of squares is a multiple of 8 .

we begin with one pie e and zero uts. 2008 . What doesn't hange. Why not? 2. or a \ onstant". It is to be ut into individual squares. May 28. in the usual way. This is an \invariant". however. It being a onstant means that it will always be one. Thus. Equivalently. and the number of pie es in reases by one.fteen L-tetrominoes. of the pro ess of utting the ho olate bar.1 The Solution The solution to the ho olate-bar problem is as follows.1. the number of uts in reases by one. is the dieren e between the number of uts and the number of pie es. So.1 Chocolate Bars Re all the problem statement: A re tangular ho olate bar is divided into squares by horizontal and verti al grooves. is one. the number of uts will always be one less than the number of pie es. the dieren e between the number of pie es and the number of uts. at the outset. A ut is made by taking a single pie e and utting along one of the grooves. the number of uts and the number of pie es both hange. Now. Algorithmi Problem Solving Roland Ba khouse. Whenever a ut is made. (Thus ea h ut splits one pie e into two pie es. That is. the number of pie es will always be one more than the number of uts.) How many uts in total are needed to ompletely ut the ho olate into all its pie es? 2. no matter how many uts have been made.

We let variable p ount the number of pie es. For more omplex problems.1. 2. Abstraction The mathemati al solution begins by introdu ing two variables. this is an easy problem to solve. the number of uts needed is one less than the number of pie es. we have used English to des ribe the solution. rather than formulate the solution in a mathemati al notation. be ause it is more su in t and more pre ise. mathemati al notation helps onsiderably. and we let variable c ount the number of uts.1. This . The values of these variables des ribe the state of the ho olate bar.2 The Mathematical Solution On e the skill of identifying invariants has been mastered.9 2. Let us use this problem to illustrate what we mean. For this reason. Cho olate Bars We on lude that to ut the ho olate bar into all its individual pie es.

a ordingly. The problem has be ome a \mathemati al" problem. That is. the variables p and c do not ompletely hara terise the state of the ho olate bar. say. We \abstra t" from the problem a olle tion of variables (or \parameters") that ompletely hara terise the essential elements of the problem. One of the inessential details is that the problem has anything to do with ho olate bars! This is totally irrelevant and. rather than a \real-world" problem. Other inessential details that have been eliminated are the sequen e of uts that have been made. Real-world problems are very hard. in ontrast. In this step. and the shapes and sizes of the resulting pie es. inessential details are eliminated. The problem ould equally well have been about utting postage stamps from a sheet of stamps. has been eliminated. making . to solve. four uts have been made. problems that su umb to mathemati al analysis are relatively easy. if not impossible. be ause it is about properties of numbers.rst step is alled abstra tion. Knowing that. or the sequen e of uts that have been made to rea h that state.

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

This is something we return to Algorithmi Problem Solving Roland Ba khouse. May 28.gure. 2008 .

10 2. The se ond is that . Invariants later.

gures often ontain mu h unne essary detail. as exempli.

ed by .

g. Our advi e is to use . 2.1.

whi h are two quite dierent things. is used instead of the assignment symbol. c := p+1 . c+1 ). c+1 .) The next step in the problem's solution is to model the pro ess of utting the ho olate bar. pronoun ed \be omes". Java being again an example. This is a major problem be ause it auses onfusion between assignments and equalities. This is a nuisan e. The two sides are separated by the assignment symbol \ := ". For example. p \be omes" p+1 . leading to diÆ ult-to-. In words. ea h expression on the right side. Some programming languages do not allow simultaneous assignments. \ = ". c+1 . but only that. respe tively. An assignment ee ts a hange of state.gures with the utmost aution. and the value of E after repla ing all variables as pres ribed by the assignment. (For example. mathemati al formulae are most often far more ee tive. An invariant of an assignment is some fun tion of the state whose value remains onstant under exe ution of the assignment. Java is an example. Instead of a simultaneous assignment. and then repla ing the values of p and c by these values. p−c is an invariant of the assignment p .) We an he k that E is an invariant simply by he king for equality between the value of E . An assignment is exe uted by evaluating. 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. and c \be omes" c+1 . 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. p−c is an expression depending on variables p and c . The right side is a omma-separated list of expressions (in this ase. restri ting the programmer to a single variable on the left side in all assignments. c ). This is how the assignment statement models the pro ess of making a single ut of the ho olate bar1 . No variable may o ur more than on e in the left side. in the urrent state. a left side and a right side. one has to write a sequen e of assignments. For example. Mu h worse is that the equality symbol. the state |the number of pie es and the number of uts| is hanged by evaluating p+1 and c+1 . p . Suppose E is an expression depending on the values of the state variables. their values being updated simultaneously on e the right side has been evaluated. p+1 . The left side is a omma-separated list of variables (in this ase. The list must have length equal to the number of variables on the left side. In our example. c := p+1 . An assignment statement has two sides. Most novi e programmers frequently make the mistake of onfusing the two. and even experien ed programmers sometimes do. We do so by means of the assignment statement Assignments p .

even if your tea hers do not do so. Algorithmi Problem Solving Roland Ba khouse. 2008 . always remember to pronoun e an assignment as \left side becomes right side". and not \left side equals right side". If you do write Java or C programs. May 28.nd errors.

2008 . n. c := p+1 . ls := rs . does not hange the value of Given an expression. m simulaneously de reasing m + 3×n . c+1 . The left side of this equality is the expression E is the expression E after repla ing all variables as pres ribed by the holds whatever the values of assignment p. n−1] = (3×n) + (m+3) + (n−1) The invariant rule for assignments is then the following. Algorithmi Problem Solving Roland Ba khouse. n−1] = (m+3) + 3×(n−1) (m+n+p)[m . and we onsider the assignment m . Here are some examples: (p−c)[p . n := m+3 .1. 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 . p := 3×n . May 28. p and c . As another example. n := m+3 . So. n−1 We he k that m + 3×n is invariant by he king that m + 3×n = (m+3) + 3×(n−1) . Simple algebra shows that this holds. E is an invariant of the assignment ls := rs if. and an assignment. in reasing n by 1. for all instan es of the variables in E. Cho olate Bars 11 p−c = (p+1) − (c+1) . n .c and the right side assignment.2. m+3 . This he ks that p−c is an invariant of the := p+1 . E. suppose we have two variables m and by 3. E[ls := rs] = E . c+1] = (p+1) − (c+1) (m + 3×n)[m .

Invariants Induction The .12 2.

So. p = 1 and c = 0 . So. p−c = 1 . So. p = s . p−c is invariant. c . at that time. Initially. the number of uts. When the bar has been ut into all its squares. initially.nal step in the solution of the ho olate problem is to exploit the invarian e of p−c . But. p−c = 1 no matter how many uts have been made. satis. where s is the number of squares.

doesn't play anymore). the value of the expression is un hanged (obviously. if the value of an expression is un hanged by some assignment to its variables. the value of the expression remains un hanged. for three times. Both times. the value will be un hanged no matter how many times the assignment is applied. An unknown number of the boxes is sele ted and into ea h eight medium boxes are pla ed. Two players ompete in ea h game. and seek an invariant.1 2 2. by assumption.es s−c = 1 . so the end result is also no hange. It is that. alled the prin iple of mathemati al indu tion. Note that the ase of zero times is in luded here. That is. Applying the assignment twi e means applying it on e and then on e again. An Algorithmi Problem Solving Roland Ba khouse. the value of the expression is un hanged. And so on. the loser is kno ked out (i. May 28. The winner of the tournament is the player that is left after all other players have been kno ked out. four times. The prin iple is very simple. How many games are played before the tournament winner is de ided? (Hint: hoose suitable variables. if the assignment is applied zero times. A number of important problem-solving prin iples have been introdu ed | abstra tion. This ompletes our dis ussion of the ho olate-bar problem. et . be ause applying the assignment zero times means doing nothing).2 Empty Boxes Try ta kling the empty-box problem. An important prin iple is being used here. Re all its statement. Summary A kno kout tournament is a series of games. We will see these prin iples again and again throughout these le tures. c = s−1 . invariants and indu tion. the winner arries on. 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). 2008 . It is very important not to forget zero. The number of uts is one less than the number of squares.e. In the ase of the ho olate-bar problem.) Exercise 2. Eleven large empty boxes are pla ed on a table. If the assignment is applied exa tly on e. Suppose there are 1234 players in a tournament. That is.

The Tumbler Problem unknown number of the medium boxes is sele ted and into ea h eight small boxes are pla ed.13 2. Identify the initial values of e and f . How many boxes are there in total? The following steps should help in determining the solution. At the end of this pro ess there are 102 empty boxes. Introdu e the variables e and f for the number of empty and the number of full boxes. 2. 1.3. respe tively. Identify the .

5.nal value of e . Combine the previous steps to dedu e the . 4. 3. Identify an invariant of the assignment. Model the pro ess of putting eight boxes inside a box as an assignment to e and f.

Hen e dedu e the .nal value of f .

May 28. an allowed move is to turn any two tumblers simultaneously.nal value of e+f . Several tumblers are pla ed in a line on a table. 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. There are three possible ee ts of turning two of the tumblers. All of these are irrelevant. 2008 . some are the right way up. Some tumblers are upside down. This is a key to ee tive problem-solving: keep it simple! 2.3 The Tumbler Problem Let us now look at how to solve the tumbler problem. or whi h are full and whi h are empty. However. It is required to turn all the tumblers the right way up. and a solution that introdu es variables representing these quantities is grossly over ompli ated. Re all the statement of the problem. Let us all it u . or the number of small boxes. Note that this solution does not try to ount the number of medium boxes. Two tumblers that are both the right way up are turned upside down. the tumblers may not be turned individually. This is modelled by the assignment Algorithmi Problem Solving Roland Ba khouse.

Finally. Turning two tumblers that are both upside down has the opposite ee t | u de reases by two. the third possibility is to exe ute skip . but it is better to have a name for the statement that does not depend on any variables. The hoi e of whi h of these three statements is exe uted is left unspe i. Invariants u := u+2 . one upside down. In this example. In programming terms. \Skip" means \do nothing" or \having no ee t". the other the right way up) has no ee t on u . We use the name skip . it is equivalent to the assignment u := u .14 2. This is modelled by the assignment u := u−2 . this is modelled by a so- alled skip statement. So. turning two tumblers that are the opposite way up (that is.

The parity of u is a boolean value: it is either true or false . we an dis ount skip . three. four. An invariant of the turning pro ess must therefore be an invariant of ea h of the three.) and it is false if u is odd (one. It is true if u is even (zero.ed. Everything is an invariant of skip . We therefore seek an invariant of the two assignments u := u+2 and u := u−2 . So. What does not hange if we add or subtra t two from u ? The answer is: the so- alled parity of u . two. eight et . .

(even.(u+2) = even. Zero is an even number. That is. Let us write even.ve. even. If there is an even number at the outset. Then. seven.). May 28. the parity of the number of upside-down tumblers will not hange.u)[u := u+2] = even. no matter how many times we turn two tumblers over. if there is an odd number at the outset.u . (even. That is. so the answer to the question is that there must be an even number of upside-down tumblers at the outset.(u−2) = even. there will always be an even number.u is an invariant of the assignment u := u+2 .u .u for this Boolean quantity. there will always be an odd number. even. The goal is to repeat the turning pro ess until there are zero upside-down tumblers.u)[u := u−2] = even.u is also an invariant of the assignment u := u−2 . Algorithmi Problem Solving Roland Ba khouse. Also. 2008 . et . We on lude that.

Suppose c denotes the number of overed squares. the number of squares along one side is m and the number along the other side is n . the solution to problem 6(a): if a re tangular board is overed by tetrominoes. 7 mod 4 is 3 . c = m×n and. It's a preliminary to solving 6(b). Have a peek if you want to. In words. and we say \ c is a multiple of 4 ". the words \is an invariant property" are omitted. May 28. The pro ess of solving more diÆ ult problems typi ally involves formulating and solving simpler subproblems. Show that the number of squares is a multiple of 8 . For example. the argument has been about tetrominoes in general.) Then. (That is.2. m×n is a multiple of 4 . The hessboard problem is a little harder. 2008 . Apply the method of introdu ing appropriate variables to des ribe the state of the balls in the urn. What we have just shown is. In fa t. pla ing a tetromino on the board is modelled by c := c+4 . Suppose a re tangular board an be overed with T-tetrominoes. Then. (Hint: use the olouring of the squares on the hessboard.) Problem 6(a) should be a bit easier. Identify an invariant. ( c mod 4 is the remainder after dividing c by 4 .4 Tetrominoes In this se tion. it must be the ase that either m or n (or both) is a multiple of 2 . but an be solved in the same way. Thus. Note that. suppose the tetrominoes over an m×n board. More often. illustrates a general phenomenon in solving problems . and draw the appropriate on lusion. Re all the problem. A brief analysis of this problem reveals an obvious invariant. c mod 4 is always 0 . and not parti ularly about T-tetrominoes. one ould say that a Algorithmi Problem Solving Roland Ba khouse. whi h we do |together with 6(a)| in the next se tion.4. So. This gives us the opportunity to introdu e a style of mathemati al al ulation that improves larity. so far. Now. and 16 mod 4 is 0 . we say \ c is a multiple of 4 is an invariant property". 2.) Initially c is 0 . The dis overy of a solution to problem 6(a). Then express the pro ess of removing and/or repla ing balls by a hoi e among a number of assignment statements. we present the solution of problem 6(b). in fa t. in this way. at least one of the sides of the re tangle must have even length. For the produ t m×n of two numbers m and n to be a multiple of 4 . c mod 4 is invariant. Tetrominoes 15 You should now be in a position to solve the problem of the bla k and white balls (problem 4 in the introdu tory se tion). so. so c mod 4 is 0 mod 4 . whi h is 0 .

This is a two-step al ulation. Invariants \diÆ ult" problem is one that involves putting together the solution to several simple problems. 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. Just keep on solving simple problems until you have rea hed your goal! At this point. \diÆ ult" problems be ome a lot more manageable. To introdu e the style we repeat the argument just given.16 2. Here it is in the new style: an m×n board is overed with tetrominoes ⇒ { invariant: c is a multiple of 4 . The . Looked at this way. c = m×n } m×n is a multiple of 4 ⇒ { property of multiples } m is a multiple of 2 ∨ n is a multiple of 2 .

m is a multiple of 2 or n is a multiple of 2 . 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. \an m×n board is overed with tetrominoes implies m×n is a multiple of 4 " or \an m×n board is overed with tetrominoes only if m×n is a multiple of 4 . if an m×n board has been overed.rst step is a so- alled \impli ation" step. as indi ated by the \ ⇒ " symbol. following the \ ⇒ " symbol is a hint why the statement is true. The se ond step is read as: If m×n is a multiple of 4 . proved earlier. (Alternatively. The step is read as If an m×n board is overed with tetrominoes. Again. the \ ⇒ " symbol signi.") The text between urly bra kets. the number of overed squares is m×n . m×n is a multiple of 4 . Here the hint is the ombination of the fa t.

The symbol \ ∨ " means \or". is less spe i. but not both. 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. The hint.e.es an impli ation. i. in this ase. A so- alled \ex lusive or" would mean that m is a multiple of 2 or n is a multiple of 2 . it would ex lude this possibility.

The on lusion of the al ulation is also an \if" statement. but you should have suÆ ient knowledge of multiplying numbers by 4 to a ept that the step is valid. You may or may not be familiar with the general theorem. May 28. 2008 . . It is: Algorithmi Problem Solving Roland Ba khouse. The property that is being alluded to has to do with expressing numbers as multiples of prime numbers.

Clearly. In luding the symbol \ ⇒ " makes lear the relation between the expressions it onne ts. Colouring this one square dierently from the other three suggests olouring the squares of the re tangle in the way a hessboard is oloured. we present al ulations in whi h \ ⇐ " is the onne ting symbol. and 4 is not a multiple of 8 . one with three bla k squares and one white square. This gives us two types. m is a multiple of 2 or n is a multiple of 2 . In luding hints within urly bra kets between two expressions means that the hints may be as long as we like. Tetrominoes If an m×n board is overed with tetrominoes. (See . Later. More importantly. Suppose we indeed olour the re tangle with bla k and white squares. whi h is often the most ee tive way to reason. Let us now ta kle problem 6(b) head on.) What distinguishes a T-tetronimo is that it has one square that is adja ent to the other three squares. We all them dark and light T-tetrominoes. A 4×1 board an be overed with 1 I-tetronimo. they may even in lude other sub al ulations. The T-tetrominoes should be oloured in the same way. and one with three white squares and one bla k square. the solution must take a ount of the shape of a T-tetronomo. This style of presenting a mathemati al al ulation reverses the normal style: mathemati al expressions are interspersed with text.4. as on a hessboard.17 2. rather than the other way around. (It isn't true for I-tetronimoes. it allows us to use other relations. Su h al ulations work ba kwards from a goal to what has been given.

d ounts the number of dark T-tetrominoes that have been used. Pla ing a dark tetromino on the board is modelled by the assignment d . 2008 . w+3 . b+1 .5. and l ounts the number of light tetrominoes. May 28. w := l+1 . In addition. The variable b ounts the number of overed bla k squares. 2. An invariant of both assignments is Algorithmi Problem Solving Roland Ba khouse.) 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. w := d+1 . whilst w ounts the number of overed white squares.g. b . w+1 . b+3 . b . Figure 2.5: Dark and light T-tetrominoes We introdu e four variables to des ribe the state of the board. Pla ing a light tetromino on the board is modelled by the assignment l .

w := d+1 . b+3 . w+1] = { de.18 2. b . sin e (b − 3×d − l)[d . Invariants b − 3×d − l .

b+1 . b . w+3] = { de.nition of substitution } (b+3) − 3×(d+1) − l = { arithmeti } b − 3×d − l and (b − 3×d − l)[l . w := l+1 .

no matter how many T-tetrominoes are pla ed on the board.nition of substitution } (b+1) − 3×d − (l+1) = { arithmeti } b − 3×d − l . Now. the initial value of b − 3×d − l is zero. another invariant of both assignments is w − 3×l − d . We an now solve the given problem. whi h means that the number of bla k squares equals the number of white squares } b=w ⇒ { b − 3×d − l = 0 w − 3×l − d = 0 } (b = w) ∧ (3×d + l = 3×l + d) Algorithmi Problem Solving Roland Ba khouse. it will always be zero. So. May 28. 2008 . Similarly. the value of w − 3×l − d will always be zero. Similarly. 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.

(How easily you an adapt the solution to one problem in order to solve another is a good measure of the ee tiveness of your solution method. It shouldn't be too diÆ ult to solve 6( ) be ause the solution to 6(b). you should be able to repeat the same argument as above.5.19 2. Similarly. We on lude that If a re tangular board is overed by T-tetrominoes. Indeed. Look at other ways of olouring the squares bla k and white. whi h suggests that it an be solved in a similar way. above. takes are to learly identify those steps where a property or properties of T-tetrominoes are used. the number of overed squares is divisible by 8 . You an now ta kle 6( ). Be areful to he k that all steps remain valid. the solution also learly identi. it an. The problem looks very mu h like 6(b). Additional Exer ises { ⇒ arithmeti } (b = w) ∧ (l = d) b − 3×d − l = 0 { ⇒ w − 3×l − d = 0 } b = w = 4×d = 4×l { ⇒ arithmeti } b+w = 8×d { ⇒ b+w is the number of overed squares } the number of overed squares is a multiple of 8 . Having found a suitable way.

2008 . Exercise 2. on e 6( ) has been solved. they are diÆ ult to adapt to new ir umstan es. May 28. The total number of obje ts is redu ed by repeatedly removing two obje ts of dierent kind. Good lu k! 2. and repla ing them by an obje t of the third kind.5 Additional Exercises Given is a bag of three kinds of obje ts. Identify exa t onditions in whi h it is possible to remove all the obje ts ex ept one.2 2 Algorithmi Problem Solving Roland Ba khouse.) Problem 6(d) is relatively easy. As a result.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.

The problems of the bla k and white balls is from [Gri81℄.2 was posed to me by Dmitri Chubarov. both new and old. The author of the problem is apparently not stated. It was posed (in a slightly dierent form) in the Russian national Mathemati s Olympiad in 1975 and appears in a book by Vasiliev entitled \Zada hi Vsesoyuzynykh Matemti heskikh Olympiad" published in Mos ow. Their publi ations ontain many examples of mathemati al puzzles. I do not know their origin. The domino and tumbler problems are old hestnuts.) Vierkant Voor Wiskunde |foursquare for mathemati s| is a foundation that promotes mathemati s in Dut h s hools. (See http://www.20 2. Algorithmi Problem Solving Roland Ba khouse.6 2. The tetromino problems I found in the 1999 Vierkant Voor Wiskunde alendar. I have made grateful use of them throughout this text. Exer ise 2. some of whi h I have made use of. Thanks go to Jeremy Weissman for suggestions on how to improve the presentation of the tetronimo problems. Invariants Bibliographic Remarks The empty-box problem was given to me by Wim Feijen. 2008 .nl/puzzels/.vierkantvoorwiskunde. 1988. May 28.

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

Crossing a River problem. Overweight Ann. the goat should not be left alone with the abbage (otherwise. 3. How an all three ouples get a ross the river? 3.1 Brute Force Goat. a abbage and a wolf a ross a river. How an the farmer a hieve the task? 2. They have one boat that is only big enough to a ommodate one adult or two hildren. the goat would eat the abbage). Cabbage and Wolf The goat-. However. Our main purpose in showing the brute-for e solution is to illustrate the pitfalls of Algorithmi Problem Solving Roland Ba khouse. Ann is Dee is 100 46 kilos.2. Col and Dee are on one side of a river. Problems Goat. How an all the adults and all the hildren ross the river? Make lear any assumptions you are obliged to make. Bob is 49 kilos. Adults and Children. Also. A farmer wishes to ferry a goat. his boat is only large enough to take one of them at a time. They have one rowing boat that an arry at most 100 kilos. A group of adults and hildren are on one side of a river.22 3. 4. 2008 . Bob.2 3. Bob an't row. The husbands are so jealous of ea h other that none is willing to allow their wife to be with another man.1 1. poor May 28. the wolf would eat the goat). if they are not themselves present. and the wolf should not be left alone with the goat (otherwise. Three ouples (husband and wife) wish to ross a river. Col is 52 kilos and kilos. making several trips a ross the river ne essary. They have one boat that an arry at most two people. making several trips a ross the river ne essary. The Jealous Couples. Cabbage and Wolf. making it impossible to solve even with the aid of a very powerful omputer.and wolf-problem is often used to illustrate brute-for e sear h. abbage. How an they all get to the other side? 3.

and the wolf should not be left alone with the goat (otherwise. a abbage and a wolf a ross a river. and we all their possible values L (for left) and R (for right). This means that we an represent a state by four variables. we introdu e some terminology that is useful when dis ussing the eÆ ien y of a parti ular solution to a problem. g (for goat). making several trips a ross the river ne essary. A farmer wishes to ferry a goat. How an the farmer a hieve the task? The problem involves four individuals. Brute For e problem-solving skills. ea h of whi h has one of two values. his boat is only large enough to take one of them at a time.2. the wolf would eat the goat). c (for abbage) and w (for wolf). In the goat-. so we do not need to introdu e a variable to represent its position. a state des ribes on whi h bank ea h of the four individuals an be found. and ways of hanging from one state to another | the state transitions. However. the goat should not be left alone with the abbage (otherwise. Note that the boat is always where the farmer is. A brute-for e sear h involves onstru ting a state-transition graph that models all possible states. the goat would eat the abbage). abbage-. We all the variables f (for farmer). Additionally. Also. wolf-problem.23 3. A value of L means at the left bank. A state transition is a hange of state that is allowed by the problem spe i. A value of R means \at the right bank". and ea h is at one of the two river banks.

whilst the abbage and wolf are at the left bank. ation. a diagram an be drawn depi ting a state-transition graph. The farmer and goat are at the right bank. However. two states between whi h there is a valid state transition are: 1. The lines have arrows on them if some state transitions are not reversible. For example. 2.e. The requirement that the goat annot be left alone with the abbage is expressed by the system invariant Algorithmi Problem Solving Roland Ba khouse. there are 24 (i. if so. and the state transitions are drawn as lines onne ting the ir les. sixteen) dierent ombinations of values. 2008 . If all state transitions are reversible. in this problem some of these ombinations are ex luded. the arrows are not ne essary and the diagram is alled an undire ted graph. All four are at the left bank. The states are drawn as ir les. For the very simplest problems. the diagram is alled a dire 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. May 28.

eliminating the ones that are not allowed.24 3. That is. or the goat and abbage are on dierent banks ( g 6= c ). Similarly. c and w are required to be equal. The table below shows the ten dierent ombinations. the total redu es to ten. but dierent from f . The graph in . (Noti e that when f and g are equal all ombinations of c and w are allowed. Crossing a River f = g = c ∨ g 6= c . we enumerate all the possible transitions between these states. when f and g are dierent.) f g c w 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. either the farmer. This ex ludes ases where g and c are equal. If we list all states. the requirement that the goat annot be left alone with the wolf is expressed by the system invariant f = g = w ∨ g 6= w . the goat and the abbage are all on the same bank ( f = g = c ).

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

and wolf-problem is not representative. 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. In the jargon used by omputing s ientists.1: Goat-. For other problems.2 State-Space Explosion There is often a tenden y to apply brute for e without thinking when fa ed with a new problem. 4. and returns with the goat. there are at least two transitions. Also.and wolf-problem. 3. given by the lower path. However. brute for e does not \s ale up" to larger problems. So. So. ea h of whom an be on one side or other of the Algorithmi Problem Solving Roland Ba khouse. Wolf-Problem 2. Cabbage-. In the \overweight" problem. and a manageable number of transitions. unlike in the goat-. The farmer takes the goat to the right bank. there are 16 possible states. The farmer takes the wolf to the right bank.2. from the initial state there are four dierent transitions. from most other states. it should only be used where it is unavoidable.1.25 3. too large even for the most diligent problem-solvers. Brute For e RRRL LLLL RRLL LLRL LRLL RLRR RRLR LLRR RRRR LLLR Figure 3. This is the path from LLRL to LLRR. abbage. 3. there are four named individuals and no restri tions on their being together on the same side of the bank. Here. 2008 . inter hanges \ abbage" and\wolf" in the se ond and third steps. The goat-. there are six individuals involved. This is the path from LLRR to RRRR. The alternative solution. May 28. and returns alone. Brute for e is only useful for very simple problems. no restri tion on the size of the state spa e is possible. the total number of transitions is large. The farmer takes the abbage to the right bank. the sear h spa e qui kly be omes mu h too large. abbage. This is the path from LRLL to LLRL. The situation in an unskilled solution of the \jealous- ouples" problem is even worse. the above |thoughtless!| solution has a manageable number of states.2.

Crossing a River river bank. the number of states is 26 . 64 ! That's an impossible number for any human being to ope with.26 3. there are .e. i. If we give ea h individual a distin t name. and we haven't even begun to ount the number of transitions. In another variation on the jealous- ouples problem.

namely that it an only be applied in spe i. 1024 . and the boat an take three people at a time. and a yet larger number of transitions. That means.e. not real problems. there are are 210 . i.ve ouples. if all are named. Take note: these are \toy" problems. dierent states. The \adults-and- hildren" problem illustrates another failing of brute for e.

and not in the general ase. ases. The number of adults and hildren is not spe i.

Yet. The goat- abbageand-wolf problem is a good example. But. We speak of an \exponential" growth in the number of states ( n is the exponent in 2n ). diagrams qui kly be ome a problem in themselves | apart from the size of paper needed. Drawing state-transition diagrams is equally inee tive. even for quite small n . do we really need to distinguish between all four? In the dis ussion of the state spa e. well- hosen problem. 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. In the goat- abbage-and-wolf problem. there are. how are the nodes to be pla ed on the paper so that the diagram be omes readable? 3. Instead. in prin iple. 2n is a very large number. the \ abbage" and the \wolf". Spe i. Whenever the state spa e of a lass of problems grows exponentially. But. But onstru ting a diagram is rarely helpful in problem-solving. A diagram an o asionally be used to illustrate the solution of a simple.3 Abstraction The state-spa e explosion is often aused by a failure to properly analyse a problem. the \goat".2. If there are n individuals in su h a problem. The so- alled \state-spa e explosion problem" gets in the way.ed in this problem. it is in fa t the easiest of all to solve. The river- rossing problems illustrate \state-spa e explosion" very well. it means that even the largest and fastest super omputers an only ta kle quite small instan es. distin t names are given to the \farmer". 2n dierent states to be onsidered. but not as mu h as one might expe t. we remarked on a \similarity" between the wolf and the abbage. a parti ularly frequent ause is unne essary or inappropriate naming.

This \similarity" also emerged in the solution: two solutions were obtained. In the restated problem. then. Why. we all Algorithmi Problem Solving Roland Ba khouse. this time with a naming onvention that omits the unne essary distin tion between the wolf and the abbage. are the \wolf" and the \ abbage" distinguished by giving them dierent names? Let us restate the problem. 2008 . May 28. the goat annot be left with either the wolf or the abbage. symmetri al in the inter hange of \wolf" and \ abbage". ally.

making it de idedly boring. May 28. Indeed. it is easy to dis over. and adds to the size of the state spa e. and redu ing a problem to its essentials is alled abstra tion. Their solutions are then put together to form a solution to the original problem. A farmer wishes to ferry an alpha and two betas a ross a river. How an the farmer a hieve the task? Now the problem be omes mu h easier to solve. an alpha should not be left alone with a beta. making use of general problem-solving prin iples. 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. However. 3. Also. and then one beta a ross. a problem has an inherent stru ture that fa ilitates de omposing the problem into smaller problems. his boat is only large enough to take one of them at a time. But. They have one boat that an arry at most two people. The smaller problems an then be further de omposed until they be ome suÆ iently manageable to be solvable by other means. 2008 . Be ause there is only one solution. The problem-solving prin iple that we learn from this example is very important. Bear it in mind as you read. 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. making several trips a ross the river ne essary. Re all its statement: Three ouples (husband and wife) wish to ross a river.3 Jealous Couples Very often. How an all three ouples get a ross the river? Algorithmi Problem Solving Roland Ba khouse. there is only one solution: Take the alpha a ross. returning with the alpha. and it is unne essary to onstru t a state-transition diagram for the problem. The pro ess of omitting unne essary detail. followed by the alpha. Then take the se ond beta a ross. Avoid unnecessary or inappropriate naming. When elements of a problem are given individual names. perhaps even by brute for e. making several trips a ross the river ne essary. it distinguishes them from other elements of the problem.27 3. It an be solved by brute for e.3. The jealous- ouples problem is an ex ellent example. Poor solutions to problems are ones that fail to \abstra t" adequately. Jealous Couples the goat an \alpha" and the abbage and the wolf \betas". it an be solved mu h more ee tively.

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. this means that a boat that an arry four people is suÆ ient to ferry an arbitrary number of ouples a ross the river. learly. and we have just shown that it is at most four. Then. Now. If the apa ity is one (or less) the maximum number of ouples is zero. ouples are not jealous of ea h other! ) Sin e a ouple is two people. Rather than ta kle the problem as stated. and then . In the original problem. by repla ing \thing" by \ ouple". the question is how many ouples an be ferried with a boat of apa ity two. there is no maximum. But. 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. 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). Alternatively. a boat with apa ity two is needed| and we are required to give a onstru tive proof that this is the ase. what is the minimum apa ity needed to ferry n ouples a ross the river? Obviously. followed by one returning from right to left. Suppose there is one boat that an arry two \things". and if the apa ity is four.28 3. This simple analysis gives us a dierent slant on the problem. Crossing a River What’s The Problem? Before we ta kle this parti ular problem. So.1 3. let us try to determine what the essen e of the problem is. and there are no other restri tions. we are given the answer |in the ase of three ouples. The new problems look more diÆ ult than the original. there is often an advantage in not knowing the answer | be ause we an sometimes gain insight by generalising. we an ta kle a related problem. and how many ouples an be ferried with a boat of apa ity three.3. until at most two remain on the left bank. namely. (After all. 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. what is the minimum apa ity needed to ferry three ouples a ross the river? More generally.

That is. First.3. 3. This suggests seeking a solution that gets ea h ouple a ross in turn.rst solving simpler instan es of the general problem. there are three ouples. we de ompose the problem into three subproblems: get the .2 Problem Structure The stru ture of this problem suggests several ways in whi h it might be de omposed.

get the se ond ouple a ross. A ording to the maxim \ladies before gentlemen". we ould try . Another de omposition is into husbands and wives. and get the third ouple a ross.rst ouple a ross.

followed by all the Algorithmi Problem Solving Roland Ba khouse.rst getting all the wives a ross. May 28. 2008 .

Jealous Couples husbands. Alternatively.3. letting \age go before beauty".29 3. we ould try .

there is no diÆ ulty in transferring the wives away from them. Getting all the husbands a ross . The reason is that. Getting all the wives a ross.rst getting all the husbands a ross. followed by all the wives. whilst their husbands remain at the left bank turns out to be easy. if the husbands all stay in one pla e.

abbage-. wolf-problem exhibits the leftright symmetry very well.) 3. seems mu h harder. this is a ru ial step in . The introdu tion of notation involves naming the elements of the problem that we want to distinguish. The diagram also illustrates the symmetry between the abbage and wolf. the result is a pro ess for getting the same group of people from right to left. whilst their wives stay at the left bank.3 Denoting States and Transitions We begin by introdu ing some lo al notation to make the solution strategy pre ise. getting the husbands to join their wives may prove to be harder than getting the wives to join their husbands. there doesn't seem mu h to hoose between them. As dis ussed earlier. by ignoring them and using brute-for e. (The state-transition diagram for the goat-. The pro ess of getting a group of people from left to right an always be reversed. This is indeed what we do.3. On the other hand. Ladies before gentleman. Perhaps a symmetri solution is possible! If that is the ase. one key stru tural property of the problem that we have not yet onsidered.rst. There is. and that is a major saving. or age before beauty. however. It is the symmetry between the left and right banks. we only need to do half the work. we lost the opportunity of a redu tion in eort. Both symmetries were to be expe ted from the problem statement.

2008 .nding a solution.2H || 2W . whilst 1C means a husband and wife who do form a ouple. Number is an extremely important mathemati al abstra tion. for example. wives and ouples that is relevant to the problem's solution. A state is denoted by two sequen es separated by bars. we use letters H . wife and ouple. respe tively. May 28. Note that we do not name the individual people as in. 1H. and all three wives are at the right bank. Clare et .2H means one ouple and two husbands. for example. These are pre eded by a number.1W means a husband and wife who do not form a ouple. Bob. W and C to mean husband. A se ond example of a state is 1C. An example is 3H || 3W . Ann. whi h denotes Algorithmi Problem Solving Roland Ba khouse. Here. A state des ribes a situation when ea h individual (husband or wife) is at one of the banks. It is only the number of husbands. 2H means two husbands. We exploit the notation to distinguish between ouples and individuals. We distinguish between states and a tions. 3C means three ouples and 1C. for example. whi h denotes the state in whi h all three husbands are at the left bank.

The starting state is thus 3C || and the required . 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.30 3.

leaving three husbands at the left bank and one wife at the right bank. An example is 3H |2W| 1W . this denotes the a tion of transporting two wives a ross the river. and. 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. Note that the notation for states and a tions does not spe ify the position or dire tion of the boat. ould be ambiguous.nishing state is || 3C . An a tion is when some individuals are being transported a ross the river. taken out of ontext.

1W || 1C. whilst all three wives are at the right bank. beginning in the state where two ouples and one husband are at the left bank. beginning with the state 3C || and ending with the state || 3C . 1C. and S denotes a sequen e of a tions. who is on the other side of the river). 3H |3W| is invalid be ause the boat an only arry at most two people. 2008 . In general. If p and q denote states. that alternates between states and a tions. Of ourse. but the he ks are straightforward. it will result in state q . for example. if the sequen e of a tions S is performed beginning in state p . Algorithmi Problem Solving Roland Ba khouse. So. Care is needed. an a tion ee ts a transition between states. Also. detailed solution to the problem is a sequen e. It is easy to make a mistake and make an invalid laim. { p } S { q } is the property that. May 28.1H || 1W } 3H |2W| 1W { 3H || 3W } is the property that.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.) Additional notation helps to express the result of a tions. a omplete. { 2C. (In the terminology of state-transition diagrams. An a tion results in a hange of state. letting two wives ross will result in a state in whi h all three husbands are at the left bank. we should always he k the validity of su h properties.ed. For example.

This strategy is realised by de omposing S0 into three sequen es S1 . 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. Our strategy an be summarised as exploiting two properties of the problem.4 Problem Decomposition Using this notation we an express our strategy for de omposing the problem. Jealous Couples 3. { 3H || 3W } S2 { 3W || 3H } . The fa t that it is easy to get the wives from one side to the other whilst their husbands remain on one bank. Finally. doing S1 followed by S2 followed by S3 . { 3W || 3H } S3 { || 3C } .3. whi h we denote by S1 . will a hieve the obje tive of hanging the state from the start state (everyone is at the left bank) to the . S3 .31 3. S2 and S3 su h that { 3C || } S1 { 3H || 3W } . The left-right symmetry. S2 . the sequen e S3 hanges the end state of S2 to the state where everyone is at the right bank. The goal is to onstru t a sequen e of a tions S0 satisfying { 3C || } S0 { || 3C } .3. So. The sequen e S2 hanges the end state of S1 to the state where the positions of the wives and husbands are reversed.

but. getting all the wives a ross the river. its task is to transfer all the wives from the right bank to the left bank. if we onstru t S1 . Algorithmi Problem Solving Roland Ba khouse. 2008 . Symmetry is aptured by making the fun tion of S3 entirely symmetri al to the fun tion of S1 . an odd number of rossings will be ne essary.nal state (everyone is at the right bank). We now have to ta kle the problem of onstru ting S1 and S2 . learly. The de omposition is into three omponents be ause we want to exploit symmetry. (It is a problem that an be solved by brute for e. If we onsider the reverse of S3 . As mentioned earlier. May 28.) Here is how it is a hieved. it is a simple task to onstru t S3 dire tly from it. So. leaving their husbands at the left bank is easy. if ne essary.

That is. As dis ussed above. { 2C.2H } |2W| 1C. We seek a solution that is symmetri al about the middle. { 1C. 3H |2W| 1W { 3H || 3W } .1H || 1W } 3H |2W| 1W { 3H || 3W } .2H |2W| . { 1W || 2C. for S2 .1H } 1W |1W| 1C. the sequen e S3 is the reverse of S1 : { 3W || 3H } 1W |2W| 3H .2H || 2W } 1C.2H |1W| 1W . { 3C || } 1C. and its .32 3. Crossing a River { 3C || } 1C. 1C. Note that.2H |1W| 1W .2H . We are now fa ed with the harder task of onstru ting S2 . { 2W || 1C.2H { || 3C } .2H |2W| . the starting position of the boat is the right bank.

The length of S2 must also be odd. T2 { 3W || 3H } .nishing position is the left bank. This is a requirement for S2 to follow S1 and be followed by S3 . Again. If the solution is to remain symmetri . 1C |1C| 1C . Algorithmi Problem Solving Roland Ba khouse. 2008 . it must surely take the following form: { 3H || 3W } T1 . May 28. we look for a de omposition into three subsequen es.

3.2H || 2W } 1C |2H| 2W { 1C || 2C } Symmetri ally. we have the omplete solution to the jealous- ouples problem: { 3C || } 1C. If the middle a tion is from right to left. { 3H || 3W } 3H |1W| 2W . { 2W || 1C. 3H |2W| 1W . 2008 . or a right-to-left rossing. This may be a left-to-right rossing. May 28. if the middle a tion is from left to right. 1C |2H| 2W . The task is now to onstru t the symmetri sequen es of a tions T1 and T2 . Vi e-versa. { 2C || 1C } 2W |2H| 1C . { 1C. for { T2 . 2C || 1C and results in the state There is little alternative but to use brute-for e sear h to try to determine whi h an be a hieved.3. the a tion must be pre eded by the state 1C || 2C . the a tion must be pre eded by the state 1C || 2C and results in the state 2C || 1C . whi h is not immediately lear. 1C.2H |2W| . Finally. It onsists of just two a tions: 3H || 3W } 3H |1W| 2W . Fortunately. putting everything together. we have: 2C || 1C } 2W |2H| 1C . { T1 is soon dis overed.2H |1W| 1W . Jealous Couples 33 Note parti ularly the middle a tion | 1C |1C| 1C | .2H } 2W |1W| 3H { 3W || 3H } . 2W |1W| 3H Algorithmi Problem Solving Roland Ba khouse. { 1C || 2C } 1C |1C| 1C .

so that you an fully appre iate how mu h more ee tive it is than brute-for e sear h.34 3. not all intermediate states are shown. but not every step. by re ording the main steps. (In this solution. Crossing a River .2H . |2W| 1C.5 A Review Pause awhile to review the method used to solve the jealous- ouples problem. This helps to do ument the solution. ea h de omposition is into three subsequen es. 1W |1W| 1C.2H { || 3C } . The onstru tion seeks at ea h stage to exploit the symmetry between the left and right banks.3. Sin e the number of rossings will inevitably be odd.) 3. Too mu h detail in program do umentation an be a hindran e. where the . { 3W || 3H } 1W |2W| 3H .

Naming the unknown sequen es. The . and to avoid error.rst and last are \mirror images" in some sense. and formally spe ifying their fun tion using the { p } S { q } notation helps to larify what has to be a hieved.

But. the solution to the problem annot be used in other ontexts. solve the following related problem: There are . For the proof of the pudding. it is easy to remember.nal solution involves eleven rossings. Moreover. making a re onstru tion of the solution very simple. be ause the solution method is well stru tured. That's too many for anyone to ommit to memory. but the solution method an.

Determine how to transport all the ouples a ross the river. Determine a solution to this problem. The ase that there are four jealous ouples. and their boat an arry a maximum of three.1 (Five-couple Problem) 2 Unfortunately. and their boat an arry a maximum of three individuals. but it is not symmetri . the symmetry between the left and right banks does not guarantee that every river- rossing problem has a symmetri solution. has a solution. Four is less than . The following hint may be helpful.ve jealous ouples. Exercise 3.

ve. you will have solved the problem of transporting . by now. and.

So.1). try to modify the solution for .ve ouples a ross the river (exer ise 3.

You should be able to .ve ouples to obtain a solution for four.

nd two solutions in this way. one being obtained from the other by reversing left and right.2 (Four-couple Problem) Algorithmi Problem Solving Roland Ba khouse. May 28. 2008 . Exercise 3.

A solution is symmetri if this transformation maps the solution to itself. it is impossible to transport six or more ouples a ross the river. It is alled a Hoare triple. Hint: Both problems an be handled together. if the boat an hold a maximum of two people. Show that. 2 3.3 At most half of the husbands an ross together. The boat an only hold one ouple. there is a transformation from solutions to solutions based on reversing left and right.4. if the boat an hold a maximum of three people. it is impossible to transport four or more ouples a ross the river. individual solutions need not be symmetri .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. That is.) 2 Show that.3. he was one of the . (Sir Tony Hoare is a British omputing s ientist who pioneered te hniques for formally verifying the orre tness of omputer programs. The ru ial properties are: Exercise 3. but the set of solutions is symmetri . Rule of Sequential Composition 35 (In general.

rst to use the notation.) A omputer program is spe i.

The allowed input values are spe i.ed by a relation between the input values and the output values.

and the output values are spe i.ed by a so- alled pre ondition. p .

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. If S is a program. For example. May 28. the program variables will satisfy property q . Pre onditions and post onditions are properties of the program variables. and p and q are properties of the program variables. if the program variables satisfy property p before exe ution of statement S . q .ed by a post ondition . fpgSfqg means that. afterwards. 2008 . exe ution of S is guaranteed to terminate and.

Crossing a River (sin e dividing by 0 is not allowed) and post ondition M = N×d + r ∧ 0 ≤ r < N .36 3. the spe i. If the program is S .

ation of the program is thus { N 6= 0 } S { M = N×d + r ∧ 0 ≤ r < N } . S1 . S2 . if S1 . Thus. A semi olon is usually used to denote sequen ing. S2 and S3 are programs. the individual omponents are exe uted one after the other. Programs are often omposed by sequen ing. S3 denotes the program that is exe uted by .

This is alled the sequential omposition of S1 . and then exe uting S3 . an intermediate ondition. then exe uting S2 . S2 and S3 . A sequential omposition is introdu ed into a program when the problem it solves is de omposed into subproblems. is invented. given a pre ondition p and a post ondition q . The problem of onstru ting a program S satisfying the spe i. In the ase of a de omposition into two omponents. r say.rst exe uting S1 .

ation fpgSfqg is then resolved by letting S be S1 . S2 and onstru ting S1 and S2 to satisfy the spe i.

This is what we did in solving the jealous- ouples problem. The intermediate onditions 3H || 3W and 3W || 3H were then introdu ed in order to make the . If the problem is de omposed into three subproblems. The initial problem statement has pre ondition 3C || and post ondition || 3C . ations fpgS frg 1 and frgS fqg 2 . two intermediate onditions are needed. The intermediate ondition r thus a ts as post ondition for S1 and pre ondition for S2 .

There are dierent ways of using the rule of sequential omposition. The stru ture of the given problem may suggest an appropriate intermediate ondition. the task is then to identify the intermediate ondition and the .rst de omposition. Alternatively. the problem may suggest an appropriate initial omputation S1 .

Conversely.nal omputation S2 . the problem may suggest an appropriate .

then the task be omes one of identifying the intermediate ondition r and the initial omputation S1 . May 28. Re all its statement: Algorithmi Problem Solving Roland Ba khouse.nal omputation S2 . 2008 . A on rete illustration is the bridge problem posed in hapter 1.

when two ross together they must pro eed at the speed of the slowest.4. Rule of Sequential Composition Four people wish to ross a bridge. The bridge is narrow and only two people an be on it at any one time. It is dark. The four people take dierent amounts of time to ross the bridge. and it is ne essary to use a tor h when rossing the bridge.37 3. The . but they only have one tor h between them.

It is easy to see that .rst person takes 1 minute to ross. the third 5 minutes and the fourth 10 minutes. the se ond 2 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 all four an ross the bridge within 17 minutes.

ve trips are needed to get all four people a ross the bridge. The .

The times have been hosen in this example so that the shortest time is a hieved when the two slowest ross together. Sin e there are only two return trips.) Using the times as identi. The question is whether they should ross together or seperately. See exer ise 3. to a hieve the shortest time. the two slowest should not make a return journey. at least two people never make a return trip. Clearly. (It isn't always the ase that the best strategy is to let the two slowest ross together.ve trips omprise three trips where two people ross together interleaved with two trips where one person returns with the tor h.4. and there are four people.

5.5. the task be omes one of determining sequen es of rossings S1 and S2 .10 || q } p |5.10 || q } { p.10 } .10| q as one step.5.10 } and { p || q.10 || } S1 { p. the solution will therefore in lude.5.ers for the individuals. Consider the tor h problem in the ase that the rossing times of the four individuals form the input parameters. Seeking to exploit symmetry.5. Suppose the . We leave you to omplete the rest.5. for some p and q .10 } S2 { || 1. and p and q su h that { 1. a rossing of the form p |5.2.10| q { p || q.2.

in an optimal solution. Apply your solution to the following two ases: Exercise 3. 2008 .rst person takes t1 minutes to ross. You may assume that. every forward trip involves two people and every return trip involves one person. the third t3 minutes and the fourth t4 minutes. Find a method of getting all four a ross that mimimises the total rossing time.4 (The Torch Problem) Algorithmi Problem Solving Roland Ba khouse. May 28. Assume that t1 ≤ t2 ≤ t3 ≤ t4 . the se ond t2 minutes.

3 minutes and 3 minutes. 4 minutes. Hint: In the spe i. 1 minute. (b) The times taken are 1 minute. Crossing a River (a) The times taken are 1 minute.38 3. 4 minutes and 5 minutes.

this isn't always the best strategy. 5 and 10 . Note that this exer ise is mu h harder than the spe i. 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. t3 and t4 are 1 .) (a) How many times must the tor h be arried a ross the bridge in order to get all four people a ross? (In lude rossings in both dire tions in the ount. In order to derive the solution methodi ally. What are the dierent strategies? Evaluate the time taken for ea h. Use a onditional statement to de ide whi h strategy to use. Hen e. (d) Give a solution to the general problem. what an you say about the number of people who do not make a return trip? Whi h of the four people would you hoose not to make a return trip? (Give a onvin ing argument to support your hoi e. (Note that steps (a) and (b) were already dis ussed above. we suggest the following steps. t2 . 2 .) How many of these are return trips? (b) Comparing the number of times a return journey must be made with the number of people.) (c) Now fo us on how to get the people who do not make a return trip a ross the bridge. ase dis ussed above (where t1 . onstru t a formula for the minimum time needed to get all four people a ross in the general ase. the shortest time is a hieved by letting the two slowest ross together. However. respe tively).

ase dis ussed above. May 28. The original problem was arefully worded so that this was not required. This would mean enumerating all the dierent ways of getting four people a ross the bridge.4 above). 2008 . How many ways are there? Exercise 3. 2 Suppose a brute-for e sear h is used to solve the tor h problem (exer ise 3.5 2 Algorithmi Problem Solving Roland Ba khouse. This is not just be ause the input times are parameters but primarily be ause you must establish without doubt that your solution annot be bettered.

problems.39 3. Summary 3. This suggests ta kling the problems by de omposing them into three omponents. Complexity theory. or similar. lends greater for e to this argument. we have ontrasted brute-for e sear h with problem de omposition. no matter how mu h bigger and faster omputers be ome. there is a symmetry between the left and right banks. Brute-for e sear h should only be used as a last resort.5. Modern omputer te hnology means that some problems that are too large for human beings to solve do be ome solvable.5 Summary In this hapter. even with the most powerful omputers. whi h you will study in a ourse on algorithm design. In all river- rossing. they an never ompete with the in rease in size of the state spa e aused by modest in reases in problem size. but the state-spa e explosion makes the method impra ti al for realisti problems. Problem de omposition seeks to exploit the inherent stru ture of the problem domain. the .

Along the way. they be ome more manageable. Unfortunately. in ontrast to the solutions. if the problems are ta kled in this way. Most importantly. and often have lear. whi h are only relevant to one parti ular problem. easily remembered and easily reprodu ed solutions. 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.rst and last being symmetri al in some way. the solution method an be applied repeatedly. but. this strategy has no guarantee of su ess.

we abstra t the few that are relevant. 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. introdu ing appropriate. learly de.

ned mathemati al notation to assist in the problem's solution. Algorithmi Problem Solving Roland Ba khouse. 2008 . May 28.

2008 .40 Algorithmi Problem Solving 3. May 28. Crossing a River Roland Ba khouse.

(Chess. The game ends when it is no longer possible to make a move.1 Matchstick Games A mat hsti k game is played with one or more piles of mat hes. it introdu es the importan e of algebra in problem solving. A winning strategy is then what we all \maintaining an invariant". The key to winning is the re ognition of invariants. a ording to a given rule. it will mean ensuring that the opponent is always pla ed in a position from whi h losing is inevitable. Games provide very good examples of algorithmi problem solving be ause playing games is all about winning. \Maintaining an invariant" is an important te hnique in algorithm development. in essen e. The hapter is also about trying to identify and exploit stru ture in problems. The goal is to have some method (i. in order to give the avour of the games that we onsider. 41 May 28. A mat hsti k game is an example of an impartial. Following it. for example. The next se tion introdu es a number of games with mat hsti ks.e. and bla k an only move bla k pie es. Moves involve removing one or more mat hes from one of the piles. be ause white an only move white pie es. and the other player is the winner. this hapter is a ontinuation of hapter 2. Two players take it in turn to make a move. 4.Chapter 4 Games This hapter is about how to win some simple two-person games.) \Complete information" means that both players Algorithmi Problem Solving Roland Ba khouse. 2008 . two-person game with omplete information. In this sense. is not impartial. 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). \algorithm") for de iding what to do so that the eventual out ome is a win. \Impartial" means that rules for moving apply equally to both players. So. Here. The player whose turn it is to move is the loser.

e. 9 et . If m is the number of mat hes in su h a position (so. 6 . The losing positions are the positions where the number of mat hes is a multiple of 3 (that is. in ard games like poker.). it is always possible to hara terise the positions as either winning or losing positions. Games know the omplete state of the game. This means that there are either 0 mat hes left. the number of mat hes is 0 . The opponent is then put in a position where the number of mat hes is a multiple of 3 . In ontrast. This is either 1 or 2 . In an impartial game that is guaranteed to terminate no matter how the players hoose their moves (i. and an allowed move is to remove 1 or 2 mat hes. The remaining positions are the winning positions. or any move they make will result in there again being a number of mat hes remaining that is not a multiple of 3 . that guarantees a win. The following exer ises ask you to do this in spe i.42 4. A winning position is one from whi h a perfe t player is always assured of a win. As an example. it is usual that ea h player does not know the ards held by the other player. the strategy is to remove m mod 3 mat hes1. A losing position is one from whi h a player an never win. 3 . and so the move is valid. when playing against a perfe t player. A winning strategy is an algorithm for hoosing moves from winning positions. in whi h ase the opponent loses. the players have in omplete information about the state of the game. suppose there is one pile of mat hes. the possibility of stalemate is ex luded). m is not a multiple of 3 ).

Ea h player is allowed to remove 1 mat h. ases. 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. There is one pile of mat hes. Ea h player is allowed to remove 0 mat hes. What are the winning positions? 2. There is one pile of mat hes. 1. 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. where N is some number .

your opponent removes 1 mat h. What are the winning positions and what is the winning strategy? 5. 3 or 4 mat hes. There is one pile of mat hes. ex ept that it is not allowed to repeat the opponent's last move.) What are the winning positions and what is the winning strategy? 1 Re all that m mod 3 denotes the remainder after dividing Algorithmi Problem Solving Roland Ba khouse.xed in advan e? 4. (So. Ea h player is allowed to remove 1 . your next move must be to remove 3 or 4 mat hes. 3 or 4 mat hes. m by 3. 2008 . May 28. if. Ea h player is allowed to remove 1 . say. There is one pile of mat hes.

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. from the right pile.2. the algorithm does give a better understanding of what is involved. What are the winning positions and what is the winning strategy? 8.1 Assumptions We make a number of assumptions about the game. and thus not pra ti al for more ompli ated games. There are two piles of mat hes. 2 or 3 mat hes. What are the winning positions and what is the winning strategy? 4. A move is to hoose one pile. 4. 1 . we show how to sear h systemati ally through all the positions of the game. remove 1 . labelling ea h as either a winning or a losing position. from the left pile. in order that the sear h will work.43 4. from that pile. and from the right pile 1 thru2 7 mat hes may be removed. 1 or 2 mat hes may be removed. A move is to hoose one pile. and. 2 or 3 mat hes may be removed. we formulate what is required of a winning strategy. Although a brute-for e sear h. A move is to hoose one pile and. from the left pile 1 .2. There are two piles of mat hes. We assume that the number of positions is . 3 or 4 mat hes may be removed. Winning Strategies 6. What are the winning positions and what is the winning strategy? 7. There are two piles of mat hes. and an be used as a basis for developing more eÆ ient solutions in parti ular ases.2 Winning Strategies In this se tion.

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

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.

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.nite. Stalemate positions are ones from whi h the players an ontinue the game inde. we ex lude the possibility that there are stalemate positions.

2 We use \thru" when we want to spe ify an in lusive range of numbers. Algorithmi Problem Solving Roland Ba khouse. May 28.nitely. so that neither wins or loses. 3 and 4 . 2 . The English expression \ 1 to 4 " is ambiguous about whether the number 4 is in luded or not. \ 1 thru 4 " means the numbers 1 . 2008 . For example.

2 Labelling Positions The .44 4. Games 4.2.

Ea h edge is from one node to another node.1.rst step is to draw a dire ted graph depi ting all positions.1 is a graph of the mat hsti k game des ribed at the beginning of se tion 4. 0 1 2 3 4 5 6 7 8 Figure 4. The nodes in . A dire ted graph has a set of nodes and a set of edges. Fig. When graphs are drawn.1: Mat hsti k Game. and all moves in the game. and edges are depi ted by arrows pointing from the from node to the to node. 4. Players may take one or two mat hes at ea h turn. nodes are depi ted by ir les.

Having drawn the graph. It is impossible to move from the position in whi h no mat hes remain. there is only one move that an be made.g. From the node labelled n . one is allowed to remove one or two mat hes. there is an edge to the node labelled n−1 and an edge to the node labelled n−2 . there is exa tly one edge. From all other nodes. there are two edges. 4. From the position in whi h one mat h remains. namely to remove the remaining mat h. That is.1 are labelled by a number. A player who . where n is at least 2 . there are no edges. from a position in whi h the number of remaining mat hes is at least 2 . the number of mat hes remaining in the pile. From the node labelled 0 . to the node labelled 0 . we an begin labelling the nodes as either losing positions or winning positions. From the node labelled 1 .

A player who . if playing against a perfe t opponent.nds themself in a losing position will inevitably lose.

provided the right hoi e of move is made at ea h turn. 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.nds themself in a winning position is guaranteed to win. At . The labelling rule has two parts. A node is labelled winning if there is an edge from the node to a losing position.

after all.rst sight. it may seem that it is impossible to begin to apply these rules. the .

rst rule de.

This is be ause. the statement \every edge from the node is to a winning position" is true.nes losing positions in terms of winning positions. May 28. It is indeed the ase that all of the (non-existent) edges is to a winning position. Algorithmi Problem Solving Roland Ba khouse. 2008 . if there are no edges from a node. whilst the se ond rule does the reverse. we an begin by labelling as losing positions all the nodes with no outgoing edges. It seems like a vi ious ir le! However.

2. A statement of the form \every x has property p " is what is alled a for-all quanti. Winning Strategies This is an instan e of a general rule of logi .45 4.

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.

In a sense. empty) be ause it is a statement about nothing.e. Returning to . ation. the statement is \va uous" (i.

nodes 1 and 2 are labelled \winning". Next. It is indeed a losing position.1. whi h we know to be a losing position. be ause the rules of the game spe ify that a player who annot make a move loses. be ause there are no edges from it. from ea h. be ause.g. there is an edge to 0 . 4. the node 0 is labelled \losing". Note that the edges we have identi.

every move is to a position starting from whi h a win is guaranteed. node 3 is labelled \losing". Now.ed di tate the move that should be made from these positions if the game is to be won. 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. A player that .

a pattern is emerging from this pro ess. The winning positions are the remaining positions. The pro ess we have des ribed repeats itself until all nodes have been labelled.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.nds themself in this position will eventually lose.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 . Clearly. In programming terms. the ir les depi ting winning positions are the ones from whi h there is an edge drawn with a thi k line. Algorithmi Problem Solving Roland Ba khouse. Fig. then nodes 7 and 8 are labelled \winning". 2008 . 0 1 2 3 4 5 6 7 8 Figure 4. Nodes 4 and 5 are labelled \winning". 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 . May 28. and so on. Winning edges are indi ated by thi k edges. 4.2: Labelling Positions. The pattern is that the losing positions are the ones where the number of mat hes is a multiple of 3 . then node 6 is labelled \losing". 4. These edges depi t the winning move from that position.

the orre tness of the winning strategy is expressed by the following annotated program segment: { n is a multiple of 3 .46 4. Games we express this property using Hoare triples. Let n denote the number of mat hes in the pile. { 2 } 2 ≤ n → n := n−2 fi n is not a multiple of 3 } n := n − (n mod 3) { n is a multiple of 3 } There are . and n 6= 0 if 1 ≤ n → n := n−1 . Then.

ve omponents of this program segment. The . ea h on a separate line.

Within these bra kets is a non-deterministi hoi e | indi ated by the \ 2 " symbol| among a number of so- alled guarded ommands. The triple. hen e. This expresses the assumption that we begin from a position in whi h the number of mat hes is a multiple of 3 . Starting in a given state. omprising the . Removing one mat h is only allowed if 1 ≤ n .fi statement in the se ond line models an arbitrary move. At least one of these guards. where b is a boolean-valued expression alled the guard. In this way. A guarded ommand has the form b → S . If none of the guards evaluates to true . If several guards evaluate to true . and then exe uting its body. the if . is true be ause of the assumption that n 6= 0 . and possibly both. removing two mat hes |modelled by the assignment n := n−2 | is \guarded" by the ondition 2 ≤ n . and S is a statement alled the body. Conditional statements are re ognised by \ if . the statement n := n−1 is \guarded" by this ondition. a onditional statement is exe uted by hoosing a guarded ommand whose guard evaluates to true . The se ond line is a so- alled onditional statement. The post ondition of the guarded ommand is the assertion \ n is not a multiple of 3 ". Similarly. an arbitrary hoi e of ommand is made.fi " bra kets. and non-zero. exe ution is aborted3 .rst line is the pre ondition.

spe ifi ally. thus asserts that. and a valid move is made that redu es the number of mat hes by one or two. then. The fourth line of the sequen e is the implementation of the winning strategy.rst three lines. on ompletion of the move. The . remove n mod 3 mat hes. the number of mat hes will not be a multiple of 3 . if the number of mat hes is a multiple of 3 .

fth line is the .

2008 . the number of mat hes will again be a multiple of 3 . Algorithmi Problem Solving Roland Ba khouse. 3 If you are already familiar with a onventional programming language. as used here. 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.nal post ondition. whi h asserts that. after exe ution of the winning strategy. In a non-deterministi hoi e. May 28. the hoi e is not ompletely determined. you will be familiar with deterministi onditional statements | so- alled if-then-else statements.

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

{ losing position. If the starting position is a losing position. From a losing position that is not an end position. and not an end position } make an arbitrary (legal) move . if the starting position is a winning position. i. ation. in su h a way that the following three properties hold: End positions are losing positions. the . the losing positions and the winning positions. the se ond player is guaranteed to win. From a winning position. If both players are perfe t. not a losing position } apply winning strategy { losing position } In summary. resulting in a losing position. every move is to a winning position. the winner is de ided by the starting position. { winning position. Vi eversa. it is always possible to apply the winning strategy. a winning strategy is a way of hoosing moves that divides the positions into two types.e.

Starting from a losing position. Algorithmi Problem Solving Roland Ba khouse. by assumption. and positions where the number of moves remaining is zero. this quantity will always be true in a game played by perfe t players. May 28. 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). Formally. and will make a mistake. one an only hope that one's opponent is not perfe t. whi h is an even number.rst player is guaranteed to win. 2008 . Sin e end positions are losing positions.

48 4. The number M is a natural number. 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. .

xed in advan e. We re ommend that you try to solve this general problem by .

rst onsidering the ase that M is 0 . Next. Constru t a table instead. try working out the ase that M is 3 . This ase also has an easy solution. whi h is the ase we have just onsidered. onsider the ase that M is 1 . As a last resort. Now. but slightly more ompli ated. This ase has a very easy solution. Do you see a pattern in the solutions? If you don't see a pattern immediately. A diagram is mu h too ompli ated. 1 and 2 (in parti ular. try a bit harder. the extreme ases 0 and 1 ) in order to he k the pattern you have identi. return to the ases that M is 0 . although it is a ase that is very often negle ted. ombine these two ases with the ase that M is 2 . (Don't draw a diagram.) Then.

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. Finally. For ea h. Two players alternately name dates. Ea h part of this exer ise uses a dierent rule for the dates that a player is allowed to name. stating whi h player should win. State also if it depends on whether the year is a leap year or not. The winner is the player who names 31st De ember. In pra ti e. Hint: in prin iple. Exercise 4. or in rease the day of the month by an arbitrary amount. and the starting date is 1st January. Begin by identifying the days in De ember that one should avoid naming. the . devise a winning strategy. formulate the orre tness of the strategy by a sequen e of assertions and statements.ed. (For example.1 (31st December Game) a) (Easy) A player an name the 1st of the next month. a pattern soon be omes evident and the days in ea h month an be grouped together into winning and losing days. as we did above for the ase that M is 2 .

3 Subtraction-Set Games A lass of mat hsti k games is based on a single pile of mat hes and a (. or a date in January other than the 1st.rst player begins by naming 1st February.) b) (Harder) A player an in rease the day by one. leaving the month un hanged. 2 4. or name the 1st of the next month.

nite) set of numbers. a move is to remove m mat hes. A game in this lass is alled a subtra tion-set game. and the set of numbers is alled the subtra tion set. May 28. Algorithmi Problem Solving Roland Ba khouse. 2008 . where m is an element of the given set.

If r is 0 or 2 . Beginning with position 0 . Subtra tion-Set Games 49 The games we have just dis ussed are examples of mat hsti k games.2. For example. 3 . the position is a losing Algorithmi Problem Solving Roland Ba khouse. Continuing this pro ess. Table 4. and a position is a winning position if there is a move from it to a losing position. We may therefore on lude that. or remove 1 mat h to move to position 2 . we noti e that the pattern of winning and losing positions repeats itself. In other words. More interesting examples are obtained by hoosing a subtra tion set with less regular stru ture.1 shows the entries when the size of the pile is at most 6 . remove four mat hes. \position 1 " means the position in whi h there is just one mat h in the pile. the subtra tion set is {1 .2.M} . the subtra tion set is {1. 4} .4. the winning and losing positions an always be omputed. after dividing the number of mat hes by 7 . positions 3 and 4 are winning positions be ause from both a move an be made to 0 . 2008 . 4} . it will ontinue to do so forever. We refer to the positions using this number. In the ase that the position is a winning position. 3 . The results are entered in a table. if the rule is that 1 thru M mat hes may be removed at ea h turn. we get the next seven entries in the table: see table 4. 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. For example.1 and 4.. So. \position 0 " means the position in whi h there are no mat hes remaining in the pile. r say. Note that there may be a hoi e of winning move. and the middle row shows whether or not it is a winning (W) or losing position (L). for the subtra tion set {1 . Positions in the game are given by the number of mat hes in the pile. Comparing tables 4. It suÆ es to enter just one move in the bottom row of the table. from position 3 there are two winning moves | remove 3 mat hes to move to position 0 . whether or not the position is a winning position an be determined by omputing the remainder. 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. May 28. 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 any given subtra tion set. and working one-by-one through the positions. On e the pattern begins repeating in this way. remove three mat hes. and so on.3. 2 is a losing position be ause the only move from 2 is to 1 . The top row is the position.

50

4. Games

Position 0

Type

L

Move

1

2

3

4

5

6

W

L

W

W

W

W

3

4

3

4

1

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

Position 7

Type

L

Move

8

9

10

11

12

13

W

L

W

W

W

W

3

4

3

4

1

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

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.(k−1) , W.(k−2) , . . . , W.(k−M) . Call this sequen
e s.k . Now, there are only 2M

dierent sequen
es of booleans of length M . As a
onsequen
e, the sequen
e s.(M+1) ,

s.(M+2) , s.(M+3) , . . . must eventually repeat, and it must do so within at most 2M

steps. That is, for some j and k , with M ≤ j < k < M+2M , we must have s.j = s.k . It

follows that W.j = W.k and the sequen
e W repeats from the k th position onwards.

For the example above, this analysis predi
ts that the W-L pattern will repeat from

the 20 th position onwards. In fa
t, it begins repeating mu
h earlier. Generally, we

an say that the pattern of win-lose positions will repeat at position 2M+M , or before.

To determine whether an arbitrary position is a winning or losing position involves

omputing the status of ea
h position k , for su
essive values of k , until a repetition in

s.k is observed. If the repetition o
urs at position R , then, for an arbitrary position

k , W.k equals W.(k mod R) .

Suppose there is one pile of mat
hes. In ea
h move, 2 , 5 or 6 mat
hes

may be removed. (That is, the subtra
tion set is {2 , 5 , 6} .)

Exercise 4.2

Algorithmi Problem Solving Roland Ba khouse.

May 28, 2008

51

4.4. Sums of 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.

2

**This exer
ise is
hallenging; its solution involves thinking beyond the
**

material presented in the rest of the
hapter.

Figure 4.3 shows a variant of snakes and ladders. In this game, there is just one

ounter. The two players take it in turn to move the
ounter at most four spa
es forward.

The start is square 1 and the

nish is square 25 . the winner is the .

rst to rea h the .

) (b) Identify the winning and losing positions. (These are not the same as the squares. it falls down to the tail of the snake. (c) Some of the positions annot be identi. if the ounter lands at the foot of a ladder. Use the rule that a losing position is one from whi h every move is to a winning position.nish. Think arefully about squares linked by a snake or a ladder. if the ounter lands on the head of a snake. Exercise 4. As in the usual game of snakes and ladders.3 (a) List the positions in this game. and a winning position is one from whi h there is a move to a losing position. it limbs to the top of the ladder.

we look at how to exploit the stru ture of a game in order to ompute a winning strategy more ee tively.1 have more than one pile of mat hes.4 Sums of Games In this se tion.ed as winning or losing in this way. 2 4. The later examples of mat hsti k games in se tion 4. one of the piles must . Explain why. When a move is made.

Algorithmi Problem Solving Roland Ba khouse. 2008 . The game is thus a ombination of two games. mat hes may be removed from the hosen pile a ording to some pres ribed rule. this parti ular way of ombining games is alled summing the games. May 28. whi h may dier from pile to pile. then.rst be hosen.

A move in the sum game is a move in one of the games. and the moves are represented by the edges.3: Snakes and Ladders. a position in the \sum" of the games is given by a pair Xx where \X" names a node in the left graph. A move is then to hoose one of the oins. given two games ea h with its own rules for making a move. two oins are used. Ea h graph represents a game. In the \sum" of the games. Both the left and right games in . the sum of the games is the game des ribed as follows. The nodes in the left graph and right graphs are named by apital letters and small letters. Players take it in turn to move the ounter at most four spa es forward. Imagine a oin pla ed on a node. A position in the sum game is the ombination of a position in the left game and a position in the right game. and displa e it along an edge to another node.4 is an example of the sum of two games. Figure 4. and \x" names a node in the right graph. one oin being pla ed over a node in ea h of the two graphs. In general. a move has the ee t of hanging exa tly one of \X" or \x". where the positions are represented by the nodes. so that we an refer to them later. Games 21 22 23 24 25 20 19 18 17 16 11 12 13 14 15 10 9 8 7 6 1 2 3 4 5 Figure 4. we all the two games the left and the right game. respe tively. Thus. A move is then to displa e the oin along one of the edges to another node. For larity.52 4.

4. 2008 .4 are unstru tured. onsequently. May 28.g. the brute- Algorithmi Problem Solving Roland Ba khouse.

2. Sums of Games O M N J K F L G H D A k h I B f C j g c E i d a e b Figure 4. The left and right games are represented by the two graphs.53 4. and \x" is the name of a node in the right graph.2 is unavoidable when determining their winning and losing positions. However. A move hanges exa tly one of X or x.4. A position is a pair Xx where \X" is the name of a node in the left graph. the left game in . for e sear h pro edure des ribed in se tion 4.4: A Sum Game.

4 has 15 dierent postions. and for sums of games in general. thus. we study how to ompute a winning strategy for the sum of two games. We .g. For this game. In this se tion. and the right game has 11 . the sum of the two games has 15×11 dierent positions. 4. a brute-for e sear h is highly undesirable.

We .nd that the omputational eort is the sum (in the usual sense of addition of numbers) of the eort required to ompute winning and losing positions for the omponent games. rather than the produ t.

nd. May 28. 2008 . Deriving a suitable generalisation forms the ore of the analysis. however. Algorithmi Problem Solving Roland Ba khouse. that it is not suÆ ient to know just the winning strategy for the individual games.

May 28. An allowed move is to hoose any one of the piles and remove at least one mat h from the hosen pile. m = n = 0 . game: given a (single) pile of mat hes. Suppose there are two piles of mat hes. there is an equal number of mat hes in both piles. This is indeed the ase. { if m 6= n } m < n → n := n − (n−m) 2 n < m → m := m − (m−n) fi Algorithmi Problem Solving Roland Ba khouse. any move will be to a position where m 6= n . As always. Formally. applies the winning strategy for the individual game| . This game is the \sum" of two instan es of the same. In this simple game. In the end position.54 4. hoosing the pile with the larger number of mat hes. the opponent wins by removing the remaining mat hes in the other pile. in the end position. 2008 .4. The symmetry between left and right allows us to easily spot a winning strategy. the winning positions are. If a player removes all the mat hes from one pile |that is. Otherwise. Subsequently. very. { if m = n ∧ (m 6= 0 ∨ n 6= 0) 1≤m → 2 1≤n → } redu e m redu e n fi . will restore the property that m = n . That is. the positions in whi h the pile has at least one mat h. The position in whi h there are no mat hes remaining is the only losing position. there is no restri tion on the number of mat hes that may be removed. the game is lost when a player annot make a move. either 1 ≤ m or 1 ≤ n ). and the winning strategy is to remove all the mat hes. the orre tness of the winning strategy is expressed by the following sequen e of assertions and program statements. very simple. obviously. 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. and removing the ex ess mat hes from this pile.1 Symmetry A Simple Sum Game We begin with a very simple example of the sum of two games. From a position in whi h m = n . This suggests that the losing positions are given by m = n . Games 4. Suppose we let m and n denote the number of mat hes in the two piles. and a move is possible (that is. namely 0 . a move is to remove at least one mat h from the pile.

Sin e. we infer that 1 ≤ n−m ≤ n . In the ase that m < n . in the ase that 1 ≤ n . The property m 6= n is the pre ondition for the winning strategy to be applied. or n hanges in value. and redu ing n . but not both. so that n−m mat hes an be removed from the pile with n mat hes. in the ase that 1 ≤ m . The fa t that either m hanges in value. n−(n−m) simpli. m < n or n < m .4. Equivalently.55 4. Sums of Games { m=n } The non-deterministi hoi e between redu ing m . guarantees m 6= n after ompletion of the move. models an arbitrary hoi e of move in the sum game.

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 .4. { m 6= n { m<n ∨ n<m if } } m < n → { 1 ≤ n−m ≤ n } n := n − (n−m) { m = n } 2 n < m → { 1 ≤ m−n ≤ m } m := m − (m−n) { m = n } fi { 4. The pre ondition expresses the legitima y of the move. We see shortly that this is how to win all sum-games. Note how the two assignments have been annotated with a pre ondition and a post ondition. The Daisy Problem Suppose a daisy has 16 petals arranged symmetri ally around its entre. Here is a ouple. after the assignment n := n−(n−m) . no matter what the individual omponents are.1 is another example of the importan e of symmetry. the property m = n will hold. it is lear that. The following sequen e of assertions and program statements summarises the argument just given for the validity of the winning strategy. 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. The solutions an be found at the end of the book. The ase n < m is symmetri . A move involves removing one petal or two adja ent petals.4.es to m . The winner is the one who removes the last petal. There are many examples of games where symmetry is the key to winning.2 m=n } Maintain Symmetry! The game in se tion 4. the post ondition is the losing property that the strategy is required to establish. There are two players.

May 28. 2008 . Algorithmi Problem Solving Roland Ba khouse.xed in advan e of the game).

56 4. Games Figure 4.3 More Simple Sums Let us return to our mat hsti k games. do you assume about the oins in order to justify your answer? 4.4. su h that it does not overlap any oin already on the table.5: A 16 -petal daisy 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 winner is the one who puts the last oin on the table.1 is to restri t the number of mat hes that an be removed. A variation on the sum game in se tion 4. The players take it in turns to pla e a oin on the table.4. Who should win and what is the winning strategy? (Harder ) What. Suppose the restri tion is that at most K mat hes an be removed from either pile (where K is . if anything.

If. m and n denote the number of mat hes in the two piles. in advan e). Consequently. The ee t of the restri tion is to disallow some winning moves. as before. For example. if K is .xed. it is not allowed to remove m−n mat hes when K < m−n . the property m = n no longer hara terises the losing positions.

the opponent an then remove the mat h to win the game. A more signi. 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.xed at 1 .

Worse is if we break symmetry further by imposing dierent restri tions on the two piles: suppose. we impose the limit M on the number of mat hes that may be removed from the left pile. where M 6= N . ant ee t of the restri tion seems to be that the strategy of establishing symmetry is no longer appli able. for example. May 28. suppose Algorithmi Problem Solving Roland Ba khouse. Alternatively. and N on the number of mat hes that may be removed from the right pile. 2008 .

for a pile of m mat hes. if one is a mat hsti k game and the other is the daisy game. the property is satis. for example. This suggests that. is to ontinually establish the property that the remainder after dividing the number of mat hes by M+1 is 0 . ( M is the maximum number of mat hes that an be removed from the left pile. \symmetry" between the piles is formulated as the property that m mod (M+1) = n mod (N+1) .) This. a form of \symmetry" is a key to the winning strategy: symmetry is too important to abandon so easily! We saw. the number m mod (M+1) determines whether the position is a winning position or not. in the two-pile game.4. is the orre t solution. and N is the maximum number that an be removed from the right pile. Sums of Games the left and right games are ompletely dierent. indeed.57 4. If this is the ase.2. In the end position. that the way to win the one-pile game. Thus. how is it possible to maintain symmetry? Nevertheless. where both piles have 0 mat hes. with the restri tion that at most M mat hes an be removed. in se tion 4.

as given by the following annotated program segment. the property an always be maintained following an arbitrary move by the opponent.4.) 4. { if m mod (M+1) = n mod (N+1) ∧ (m 6= 0 ∨ n 6= 0) } 1 ≤ m → redu e m by at most M 2 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)) 2 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. Also.4 The MEX Function The idea of de.ed.

Algorithmi Problem Solving Roland Ba khouse. May 28. 2008 .ning \symmetri " to be \the respe tive remainders are equal" an be generalised to an arbitrary sum game.

58 4. so. The idea is to de. Games Consider a game that is the sum of two games. A position in the sum game is a pair ( l.r ) where l is a position in the left game. A move ae ts just one omponent. a move is modelled by either a (guarded) assignment l := l ′ (for some l ′ ) to the left omponent or a (guarded) assignment r := r ′ (for some r ′ ) to the right omponent. and r is a position in the right game.

on left and right positions.r . 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.r ) is a losing position exa tly when L. respe tively.l = R.ne two fun tions L and R . in su h a way that a position ( l. say. The question is: what properties should these fun tions satisfy? In other words.

l = R.r ) satisfying L. First.l = R. should result in a winning position |a position ( l.l 6= R.r } .l 6= R. it must be the ase that L and R have equal values on end positions. We an satisfy the . Third. { L.r | should result in a losing position |a position ( l. applying the winning strategy. from a winning position |a position ( l. that is not an end position.r | . { if L.l = R. Se ond.r ∧ (l is not an end position ∨ r is not an end position ) } l is not an end position → hange l 2 r is not an end position → hange r fi { L.l 6= R.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. That is.r |.r } .l 6= R.r ) satisfying L.r ) satisfying L.r | .r ) satisfying L. sin e ( l.r } apply winning strategy { L. every allowed move from a losing position |a position ( l. ation.l = R. That is.

rst and se ond requirements if we de.

L. ′ ′ ′ Similarly. and require that: For end positions l and r of the respe tive games. Algorithmi Problem Solving Roland Ba khouse.l = 0 = R. 2008 .r 6= R.r ′ .ne L and R to be fun tions with range the set of natural numbers.l 6= L.r . For every l su h that there is a move from l to l in the left game. R. for every r ′ su h that there is a move from r to r ′ in the right game.l . L. May 28.

l and R.l < R.r < L. and the hoi e of 0 as the fun tions' value at end positions is quite arbitrary.l .r or R. If L.59 4.r are dierent natural numbers. The advantage of this hoi e arises from the third requirement. either L.4. Sums of Games Note that the hoi e of the natural numbers as range of the fun tions. This allows us to re.

(See below. For this to work. Similarly.r .r < L.l ′ = n . we require that: For any number m less than R.r → hange r 2 R.ne the pro ess of applying the winning strategy.l < R.r } .l .r < L.l → hange l fi { L.r } { if L. it is possible to move from r to a position r ′ su h that R.l 6= R.l < R. The bulleted requirements are satis. it must be possible to move from l to a position l ′ su h that L. for any number n less than L.l .l = R. by hoosing to move in the right game when L.r and hoosing to move in the left game when R.r ′ = m .) L.

ed if we de.

The pre ise de.ne the fun tions L and R to be the so- alled \mex" fun tion.

The mex value of p . denoted mexG.nition of this fun tion is as follows.p . Let p be a position in a game G . is de.

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. su h that There is no legal move in the game G from p to a position q satisfying mexG. A brief.4. 4. 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 . n .q = m .5 Using the MEX Function We use the game depi ted in .ned to be the smallest natural number. \Mex" is short for \minimal ex ludant".

4 to illustrate the al ulation of mex numbers. onsequently.g. the only way to ompute the mex numbers is by a brute-for e sear h of all positions. 4. This is easily done Algorithmi Problem Solving Roland Ba khouse. May 28. The graphs do not have any systemati stru ture. Figure 4.6 shows the mex numbers of ea h of the nodes in their respe tive games. 2008 .

by de. Subsequently. The mex number of a node is the smallest natural number not in luded among the mex numbers of its su essors. Games 2 1 2 0 0 0 1 1 0 0 0 3 1 0 0 2 1 0 0 0 0 1 1 2 1 0 Figure 4.6: Mex Numbers.) The number is.60 4. (A su essor of a node p is a node q su h that there is an edge from p to q . The end positions are ea h given mex number 0 . a mex number an be given to a node when all its su essors have already been given a mex number. by hand.

nition.7 shows a typi al situation. Fig. 4. The node at the top of the . the smallest number that is not in luded in the mex numbers of its su essors.

in either the left or right graph. The latter is larger ( 3 against 2 ). suppose we play this game. Let us suppose the starting position is \Ok". This is a winning position be ause the mex number of \O" is dierent from the mex number of \k". but there are su essors with the mex numbers 0 and 1 . Now. whi h has the same mex number as \O". the winning strategy is to move in the right graph to the node \i". In the situation shown. The . to a node with mex number dierent from 2 . The opponent is then obliged to move.gure is given a mex number when all its su essors have been given mex numbers. So. the mex number given to it is 2 be ause none of its su essors have been given this number.

rst player then repeats the strategy of ensuring that the mex numbers are equal. until eventually the opponent an move no further. May 28. 2008 . Algorithmi Problem Solving Roland Ba khouse.

the saving grows as the size of the omponent games in reases. Note that. give the winning move in the form X m where \X" is one of \L" (for Algorithmi Problem Solving Roland Ba khouse. May 28.4 Left Game Right Game \losing" or winning move 10 20 ? 20 20 ? 15 5 ? 6 9 ? 37 43 ? Table 4. In the sum game. therefore. For winning positions. 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.61 4.4. 5 or 6 mat hes may be removed. 5 or 6 mat hes may be removed. we have to sear h through 26 dierent positions. 165 . But. state whether it is a winning or a losing position. 2 . In the left game. The table below shows a number of dierent positions in this game. a) Consider the subtra tion-set game where there is one pile of mat hes from whi h at most 2 .7: Computing mex numbers.3: Fill in entries marked \?" For ea h position. A position is given by a pair of numbers: the number of mat hes in the left pile. Cal ulate the mex number for ea h position until you spot a pattern. In the right game. 1 or 2 mat hes may be removed at ea h turn. Moreover. Exercise 4. The unlabelled node is given the mex number 2 . be ause of the la k of stru ture of the individual games. Sums of Games 0 1 4 5 Figure 4. in order to al ulate the mex numbers of ea h position. This is a substantial saving in omputational eort. a move is made by hoosing to play in the left game. and the number of mat hes in the right pile. or hoosing to play in the right game. we have to sear h through all 15 positions of the left game and all 11 positions of the right game. In total. b) Consider a game whi h is the sum of two games. 2008 .

making a total of m×n squares. 2 A re tangular board is divided into m horizontal rows and n verti al olumns.62 4. if the board has 4×4 squares. The number of squares is alled the area of the board. where m and n are both stri tly positive integers.) For example. Games \left game") or \R" (for right game). and m is the number of mat hes to be removed. when a ut has been made a part whose area is at most the area of the other part is dis arded. (This means that the part with the smaller area is dis arded if the two parts have dierent areas. Also. if the board has 4×5 squares. and one of the two parts is hosen arbitrarily if the two areas are equal. 4×3 . or 4×4 squares. Ea h of the two players takes it in turn to ut the board either horizontally or verti ally along one of the dividing lines. (Boards with 3×4 and 4×3 squares are ee tively the same. a single move redu es it to 2×5 . 3×5 . A ut divides the board into two parts. the orientation of the board is not signi. a single move redu es it to either 2×4 or 3×4 squares. A game is played on the board as follows.

if the board has 5 olumns. for n less than 3 . al ulate whi h positions are winning and whi h positions are losing for the . 5 ≤ n . at whi h point the player whose turn it is to play loses. No other moves are possible be ause.) The game ends when the board has been redu ed to a 1×1 board. Exercise 4. At this point. The omponent games are opies of the same game. and for n greater than 4 .5 (a) For the omponent game. A move in the game is to repla e m by a number n su h that n < m ≤ 2n . 2n < 5 . at ea h move. This question is about al ulating the mex numbers of the omponent games in order to determine a winning move even when the board annot be made square. A position in the game is given by a stri tly positive integer m . ant. the player whose turn it is to play loses. This game is as follows. the number of olumns an be redu ed to 3 or 4 be ause 3 < 5 ≤ 6 and 4 < 5 ≤ 8 . This game is a sum game be ause. a hoi e is made between utting horizontally or verti ally. the game ends when m has been redu ed to 1 . For example. The game is easy to win if it is possible to make the board square.

rst 15 positions. Algorithmi Problem Solving Roland Ba khouse. May 28. A winning position is a position from whi h there is a move to a losing position. Make a general onje ture about the winning and losing positions in the omponent game and prove your onje ture. A losing position is a position from whi h every move is to a winning position. position 1 . The end position. Base your proof on the following fa ts. 2008 . is a losing position.

4. al ulate the mex number of ea h of the . Sums of Games (b) For the omponent game.63 4.

rst 15 positions. The . Give the results of your al ulation in the form of a table with two rows.

Split the table into four parts. Part i gives the mex numbers of positions 2i thru 2i+1−1 (where i begins at 0 ) as shown below.rst row is a number m and the se ond row is the mex number of position m . (The .

rst three entries have been ompleted as illustration.) Position: Mex Number: 1 0 Position: Mex Number: 2 3 1 0 Position: Mex Number: 4 5 6 7 ? ? ? ? Position: Mex Number: 8 9 10 11 12 13 14 15 ? ? ? ? ? ? ? ? (You should .

nd that the mex number of ea h of the losing positions (identi.

ed in part (a)) is 0 . You should also be able to observe a pattern in the way entries are .

the .) (c) Table 4. The pattern is based on whether the position is an even number or an odd number.lled in for part i+1 knowing the entries for part i .3 shows a position in the board game.

. Using your table of mex numbers.rst olumn shows the number of olumns and the se ond olumn the number of rows. or otherwise.

ll in \losing" if the position is a losing position. If the position is not a losing position. .

and n is the number whi h it should be ome. May 28. 2 Algorithmi Problem Solving Roland Ba khouse. \C" or \R" indi ates whether the move is to redu e the number of \C"olumns or the number of \R"ows.ll in a winning move either in the form \C n " or \R n ". 2008 . where n is an integer.

of Columns No.5 Summary This hapter has been about determining winning strategies in simple two-person games.64 4.4: Fill in entries marked \?" 4. Games No. of Rows 2 15 4 11 4 14 13 6 21 19 \losing" or winning move ? ? ? ? ? Table 4. The underlying theme of the hapter has been problem spe i.

We have seen how winning and losing positions are spe i. ation.

ed. A pre ise. formal spe i.

The analysis of the \sum" of two games exempli. Brute-for e sear h is only advisable for small. ation enabled us to formulate a brute-for e sear h pro edure to determine whi h positions are whi h. unstru tured problems.

Again.es the way stru ture is exploited in problem solving. 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.

and mex numbers are sometimes alled \Sprague-Grundy" numbers. ation of the \mex" fun tion on game positions. and sums of more than two games. ompared to a brute-for e sear h. well-explored area of Mathemati s. 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. are solved using mex numbers. it is easy to formulate games having very simple rules but for whi h no eÆ ient algorithm implementing the winning strategy is known. to show how Nim. We have not developed the theory suÆ iently. May 28. (What is missing is how to ompute the mex number of the sum of two games. 2008 . su h as the se urity of a system. with the user of the software as the adversary. Mex numbers were introdu ed by Sprague and Grundy to solve the \Nim" problem. whi h we have only tou hed upon in this hapter. in this hapter.) Algorithmi Problem Solving Roland Ba khouse. after their originators. are often modelled as a game. Another reason is that games often provide ex ellent examples of \ omputational omplexity". Nim is a well-known mat hsti k game involving three piles of mat hes. The use of mex fun tions substantially redu es the eort needed to determine winning and losing positions in the \sum" of two games. Game theory is a ri h.

2008 .5 was suggested by Atheer Aheer.65 4. Algorithmi Problem Solving Roland Ba khouse.6. Exer ise 4. May 28. The 31st De ember game (exer ise 4.1) is adapted from [DW00℄. Conway and Guy is the bible of game theory.6 Bibliographic Remarks The two-volume book \Winning Ways" [BCG82℄ by Berlekamp. Bibliographi Remarks 4.

May 28.66 Algorithmi Problem Solving 4. Games Roland Ba khouse. 2008 .

Chapter 5 Knights and Knaves The island of knights and knaves is a .

" 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. are easy exer ises in the use of al ulational logi . and similar logi puzzles. B and C. 1. these. It is rumoured that there is gold buried on the island. The island has two types of natives. 2008 . In ontrast. 67 May 28." What an be inferred about the number of knights? Algorithmi Problem Solving Roland Ba khouse. Case analysis is a lumsy way of ta kling the problems. The temptation is to solve su h problems by ase analysis |in a problem involving n natives. Will you get the same answer in ea h ase? 3. onsider the 2n dierent ases obtained by assuming that the individual natives are knights or knaves| . You ask both of them whether the other one is a knight. tional island that is often used to test students' ability to reason logi ally. \knights" who always tell the truth. and \knaves" who always lie. You ask one of the natives whether there is gold on the island. 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 native replies: \There is gold on this island is the same as I am a knight. whi h we introdu e in this hapter. There are three natives A. Suppose A says \B and C are the same type.1 Logic Puzzles Here is a typi al olle tion of knights-and-knaves puzzles. 5.

Knights and Knaves 4. We say that m2−n2 and (m+n)×(m−n) are equal. 2008 . What question should you ask A to determine whether A and B are the same type (i. What question should you pose to A to determine whether or not C is telling the truth? 5. You would like to determine whether an odd number of A. both knights or both knaves)? 8. Laws are typi ally primitive. What is the question you should ask? 9. 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. We learn. and otherwise the answer will be no. for example. They are \primitive" in the sense that they annot be broken down into simpler laws. where one bran h leads to a restaurant and one doesn't. equalities between expressions.2. and write m2−n2 = (m+n)×(m−n) . both involving zero are: n+0 = n . Devise a question that allows you to determine whether a native is a knight. We all them axioms. and they are \general" in the sense that they hold independently of the values of any variables in the onstituent expressions. May 28. Two examples of axioms. A tourist omes to a fork in the road. independently of the values of m and n . and n−n = 0 . 5. Suppose C says \A and B are as like as two peas in a pod". You may ask one yes/no question to any one of them.2 5. Algorithmi Problem Solving Roland Ba khouse. A native of the island is standing at the fork. 6. The basis for these al ulations is a set of laws. B and C is a knight.1 Calculational Logic Propositions The algebra we learn at s hool is about al ulating with expressions whose values are numbers.e. how to manipulate an expression like m2−n2 in order to show that its value is the same as the value of (m+n)×(m−n) . What question should you ask A to determine whether B is a knight? 7. but general.68 5.

(Cal ulational) logi is about al ulating with expressions whose values are so- alled \booleans" | that is. m < n < p . in ontrast to other axiomatisations of logi . . Equality is the most basi on ept of logi |a fa t . The three examples above are all atomi . either true or false . n = 0 (whi h is either true or false depending on the value of n ). for example. For example. Cal ulational Logi both of whi h are true whatever the value of the variable n . n and p . and \if" that are used to ombine atomi propositions. then . ). \or". . \asso iativity of addition" is the name given to the equality: (m+n)+p = m+(n+p) . . whi h is true for all m . The laws are often given names so that we an remember them more easily. whi h emphasise logi al impli ation (if . . Logi is about rules for manipulating the logi al onne tives | the operators like \and".2. that is the on ern of the problem domain being dis ussed. Cal ulational logi pla es emphasis on equality of propositions. 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. Atomi propositions are propositions that annot be broken down into simpler propositions. and n < n+1 (whi h is true for all numbers n ). We say they are true \for all n ". Boolean-valued expressions are alled propositions . A non-atomi proposition would be.69 5. Examples of su h expressions are \it is sunny" (whi h is either true or false depending on to when and where \it" refers) .

who lived from 1646 to 1716 and who was the .rst re ognised by Gottfried Wilhelm Leibniz.

Then. the statements made by the natives are propositions. Also.rst to try to formulate logi al reasoning| and equality of propositions is no ex eption. Suppose A denotes the proposition \A is a knight". That is. and suppose native A makes a statement S. and a knave always lies.2 Knights and Knaves Equality of propositions is entral to solving puzzles about knights and knaves. We see shortly that equality of propositions is parti ularly spe ial. and so is a proposition. 5.2. 2008 . May 28. If A is a native of the island. the ru ial observation is that the values of these two propositions are the same. A=S . Re all that a knight always tells the truth. the statement \A is a knight" is either true or false . re ognition of whi h onsiderably enhan es the beauty and power of reasoning with propositions. Algorithmi Problem Solving Roland Ba khouse. A statement like \the restaurant is to the left" is either true or false.

This doesn't tell us anything! A moment's thought on. or A is not a knight and the restaurant is not to the left. if A says \the restaurant is to the left". Using this rule. where L denotes the truth value of the statement \the restaurant is to the left". Knights and Knaves For example. then A=L .70 5. A is a knight and the restaurant is to the left. if A says \I am a knight". In words. we dedu e A=A .

If native A is asked a yes/no question Q . Asked the question \is B a knight?" A will respond \yes" if they are both the same type (i. For example. . A simple. Otherwise the response will be \no". 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. That is. Be ause these rules are equalities. the response to the question is the truth value of A = Q . Both knights and knaves would laim that they are knights. A's response is \yes" or \no" depending on the truth or falsity of A = B . as A = A .e.rms that this is what one would expe t. A = B ). asked the question \are you a knight" all natives will answer \yes". otherwise \no". the algebrai properties of equality play a entral role in the solution of logi puzzles formulated about the island.

x = y is the same as y = x . That is x = x whatever the value (or type) of x . B's response is B = A . if x = y and f is any fun tion then f. it is re exive . But. As dis ussed above. Third. Finally. First. therefore.x = f. it is transitive .rst example is if A is asked whether B is a knight. 5. That is. That is. Reversing the roles of A and B. the two responses will always be the same. Se ond. A's response is A = B . Note that this argument does not involve any ase analysis on the four dierent values of A and B .2. it is symmetri . equality is symmetri .y (where the in.3 Boolean Equality Equality |on any domain of values| has a number of hara teristi properties. 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. if x = y and y = z then x = z . and B is asked whether A is a knight.

Equality is a binary relation. This last rule is alled substitution of equals for equals or Leibniz's rule . re exivity. When studying relations. the Algorithmi Problem Solving Roland Ba khouse. It is a fun tion with range the boolean values true and false . 2008 . symmetry and transitivity are properties that we look out for. May 28. When we study fun tions.x dot denotes fun tion appli ation). Equality is also a fun tion.

or hara ters. But. That is. The re exivity of equality is expressed by the rule (p = p) = true . perhaps surprisingly. (p = q) = r is also false . in all but one ase. viewed as a fun tion. In other words. q and r are numbers. what about asso iativity of equality? Is equality an asso iative operator? The answer is that. (p = q) = r is a meaningful boolean value. or sequen es. the question doesn't make sense. an elementary example is the following. Cal ulational Logi sort of properties we look out for are asso iativity and symmetry. q and r are all booleans it makes sense to ompare the boolean p = q with r for equality. You should he k this property by onstru ting truth tables for (p = q) = r and for p = (q = r) and omparing the entries. y and z .71 5. q and r .1) [Associativity] ((p = q) = r) = (p = (q = r)) . so too is p = (q = r) . for one be ause it enhan es e onomy of expression. That is. 2008 . and the third is false . it makes sense to ask whether equality of boolean values is asso iative | and. When p . Symmetry of equality. x+y = y+x and x×y = y×x . for all booleans p . You should observe that the entries for whi h (p = q) = r is true are those for whi h an odd number of p . is just the same as symmetry of equality. et . it is. 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. Algorithmi Problem Solving Roland Ba khouse. May 28.2. The asso iativity of equality is a very powerful property. For example. viewed as a relation. The one ex eption is equality of boolean values. Similarly. They are also both symmetri : for all x and y . x + (y + z) = (x + y) + z and x × (y × z) = (x × y) × z . We will see several examples. It also makes sense to ompare these two values for equality. If two of the three are true. (5. q and r is true . addition and multipli ation are both asso iative: for all x . The expression (p = q) = r just doesn't make sense when p .

et . We use it several times below. string.72 5. for boolean p .4 Hidden Treasures We an now return to the island of knights and knaves.). whatever its type (number. and dis over the hidden treasures. But.2. Knights and Knaves This holds for all p . boolean. This rule is most ommonly used to simplify expressions by eliminating \ true " from an expression of the form p = true . 5. Let us onsider the . we an apply the asso iativity of equality to get: p = (p = true) .

We on lude that there is gold on the island. Suppose. that the native is at a fork in the road. but it is not possible to determine whether the native is a knight or a knave.rst problem posed in se tion 5. 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". So. now. and we dedu e that A = (A = G) is true.1. substitution of equals for equals } true = G = { equality is symmetri } G = true = { G = (G = true) } G . true = { A's statement } A = (A = G) = { equality of booleans is asso iative } (A = A) = G = { (A = A) = true . 2008 . Then the native's statement is A = G . May 28. You want to formulate Algorithmi Problem Solving Roland Ba khouse. and you want to determine whether the gold an be found by following the left or right fork.

Note that this analysis is valid independently of what L denotes. Then. Cal ulational Logi a question su h that the reply will be \yes" if the left fork should be followed. the question to be posed is P = A .73 5. Let Q be the question to be posed. Let L denote \the gold an be found by following the left fork. or whether there are any knaves on the island." The requirement is that L is the same as the response to the question. As usual. the question Q to be posed is L = A . we give the unknown a name. and \no" if the right fork should be followed. the question may be simpli. we require that L = (A = Q) . That is. L = (A = Q) = { equality is asso iative } (L = A) = Q . So.2. It might be that you want to determine whether there is a restaurant on the island. the response to the question will be A = Q . In general. That is. or whatever. But. 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' ". if it is required to determine whether some proposition P is true or false. In the ase of more omplex propositions P . as we saw earlier.

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

but set out as a al ulation of Q . Q = { rule for formulating questions Algorithmi Problem Solving Roland Ba khouse. 2008 .es to B . } May 28. with hints showing the steps taken at ea h stage. the question to be posed is \is B a knight?". Here is this argument again. So.

C = (A = B) . 5. Knights and Knaves A=C = { from C's statement.74 5. } A = (A = B) = { asso iativity of equality } (A = A) = B = { (A = A) = true } (true = B) = B } true = B = { B . substitution of equals for equals.3 Equivalence and Continued Equalities Asso iative fun tions are usually denoted by in.

The bene.x operators1 .

y and z ). (x⊕y)⊕z = x⊕(y⊕z) for all x . More importantly. we an hoose to simplify u⊕w for any pair of subexpressions u and w. The expression be omes more ompa t be ause of the omission of parentheses.t in al ulations is immense. x⊕y = y⊕x for all x and y ) the gain is even bigger. we an write x⊕y⊕z without fear of ambiguity. If the operator is also symmetri (that is. If a binary operator ⊕ is asso iative (that is. be ause then. the expression is unbiased. In. we may hoose to simplify x⊕y or y⊕z depending on whi h is the most onvenient. if the operator is used to ombine several subexpressions.

Do we understand in. the formula is more ompa t (sin e m is not written twi e). we are guided to the inferen e that 0 ≤ n . 0 ≤ m ≤ n . But. we have a dilemma.x notation is also often used for binary relations. Here. 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 . Here. In this way. More importantly. the operators are being used onjun tionally : the meaning is 0 ≤ m and m ≤ n . We write. 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. for example. the inferen e is more omplex sin e there are two relations involved. we may infer that 0 < n .

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.

1 An Algorithmi Problem Solving Roland Ba khouse. respe tively.x operators. denoting addition and multipli ation. May 28. 2008 .

in just the same way as we would read x+y+z ? The two readings are.3. as x = (y = z) . equally. unfortunately. not the same (for example true = false = false is false a ording to the . y and z ? Or do we read it \asso iatively" as (x = y) = z . or. Equivalen e and Continued Equalities equality as a relation and read a ontinued expression of the form x=y=z as asserting the equality of all of x .75 5.

pn are equal. we write both p = q and p ≡ q . A ordingly. these both mean the same.e as p = q and q = r | . There are advantages in both readings. When p and q are expressions denoting boolean values. whi hever is the most onvenient| whereas a ontinued expression p=q=r is to be evaluated onjun tionally |i. May 28. . as (p ≡ q) ≡ r or p ≡ (q ≡ r) . It would be very onfusing and. More generally. . = pn means that all of p1 . But a ontinued expression p≡q≡r . ≡ pn Algorithmi Problem Solving Roland Ba khouse. 2008 . a ontinued equality of the form p1 = p2 = . . . indeed. is to be evaluated asso iatively |i. dangerous to read x = y = z in any other way than x = y and y = z .rst reading but true a ording to the se ond and third readings).e. whilst a ontinued equivalen e of the form p1 ≡ p2 ≡ . The solution to this dilemma is to use two dierent symbols to denote equality of boolean values | the symbol \ = " when the transitivity of the equality relation is to be emphasised and the symbol \ ≡ " when its asso iativity is to be exploited. . the onjun tional reading (for other types) is so universally a epted |for good reasons| that it would be quite una eptable to try to impose a dierent onvention. omprising more than two boolean expressions onne ted by the \ ≡ " symbol. the meaning of a sequen e of expressions separated by equality symbols would depend on the type of the expressions. . Also. . p2 . and it is a major drawba k to have to hoose one in favour of the other. otherwise. .

Shortly. Moreover. They invariably involve a ontninued equivalen e. A .76 5. being an unfamiliar word. we re ommend that the \ ≡ " symbol is pronoun ed as \equivales". sin e the out ome is not ae ted) and then evaluating the expression as indi ated by the hosen parenthesisation. its use will help to avoid misunderstanding. we introdu e a number of laws governing boolean equality. Knights and Knaves has the meaning given by fully parenthesising the expression (in any way whatsover.

(5.2) [Reflexivity] 5.1 true ≡ p ≡ p .3.rst example is its re exivity. Even and Odd Numbers The . Examples of the Associativity of Equivalence This se tion ontains a ouple of beautiful examples illustrating the ee tiveness of the asso iativity of equivalen e.

in general. May 28. the equivalen e p ≡ q ≡ r is true exa tly when an odd number of p . if we parenthesise the statement as m+n is even ≡ (m is even ≡ n is even) . Another way of reading the statement is to use the fa t that. Parenthesising it as (m+n is even ≡ m is even) ≡ n is even .rst example is the following property of the predi ate even on numbers. So the property aptures four dierent ases: or or or (( m+n (( m+n (( m+n (( m+n is even) is odd) is odd) is even) and and and and (m (m (m (m is even) is odd) is even) is odd) and and and and Algorithmi Problem Solving Roland Ba khouse. (A number is even exa tly when it is a multiple of two. (n (n (n (n is even)) is even)) is odd)) is odd)) . Then. q and r is true. it states that the number m+n is even exa tly when the parities of m and n are the same. 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. It will help if we refer to whether or not a number is even or odd as the parity of the number. 2008 .) m+n is even ≡ m is even ≡ n is even .

the produ t x×y is positive if the signs of x and y are equal. For non-zero numbers x and y . the produ t x×y is negative. Assuming that x and y are non-zero. our justi. even though no ase analysis is involved. Sign of Non-Zero Numbers The sign of a number says whether or not the number is positive. Avoidan e of ase analysis is vital to ee tive reasoning. this one statement neatly aptures a number of dierent ases. If the signs of x and y are dierent. Using the asso iativity of equivalen e the value of \ m+n is even" is expressed in one simple formula. this rule is expressed as x×y is positive ≡ x is positive ≡ y is positive .3. There are four distin t ombinations of the two booleans \ m is even" and \ n is even". without any repetition of the omponent expressions.5. Just as for the predi ate even. Indeed. Equivalen e and Continued Equalities 77 The beauty of this example lies in the avoidan e of ase analysis. rather than as a list of dierent ases.

It is akin to introdu ing equality of numbers by .3. At the present time.2 On Natural Language Many mathemati ians and logi ians are not aware that equality of booleans is asso iative. 5. ation of the rule is the statement x×y is positive ≡ (x is positive ≡ y is positive) . there is onsiderable resistan e to a shift in fo us from impli ation to equality. those that do are often unaware or dismissive of how ee tive its use an be. 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. Most ourses on logi introdu e boolean equality as \if and only if".

rst introdu ing the at-most ( ≤ ) and at-least ( ≥ ) relations. and then de.

(\If it is raining. 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.ning an \at-most and at-least" operator. I will take my umbrella.") The equality symbol was . but in ausal terms.

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. To take a on rete example. Natural language has no ounterpart to a ontinued equivalen e. May 28. if not nonsensi al. even though it is a tually true! Algorithmi Problem Solving Roland Ba khouse. in the history of mathemati s.rst introdu ed into mathemati s by Robert Re orde in 1557. 2008 . is quite re ent. whi h. were equality \natural" it would have been introdu ed mu h earlier.

nowadays.78 5. people sometimes say. the most ommon way to express time is in words: like \quarter to ten" or \ten past eleven". for example. Knights and Knaves This fa t should not be a deterrent to the use of ontinued equivalen e. But. a very long time ago) there was probably similar resistan e to the introdu tion of ontinued additions and multipli ations. Cal ulational requirements (eg. wanting to determine how long is it before the train is due to arrive) have in uen ed natural language so that. For example. 9:45 or 11:10 in everyday spee h. At one time (admittedly. The eviden e is still present in the language we use today. we still don't .

Negation is a unary operator (meaning that it is a fun tion with exa tly one argument) mapping a boolean to a boolean. but will always lag a long way behind. The goal is not to mimi \natural" reasoning. Native A says. Changes in natural language have o urred. but to provide a more ee tive alternative. \B is a knight equals I am not a knight". In fa t. are fundamental to the al ulation. and is denoted by the symbol \ ¬ ". and will ontinue to o ur. What an you determine about A and B? This problem involves a so- alled negation : the use of \not". this is what we a tually use when we want to al ulate the time dieren e between 9. as a result of progress in mathemati s. There are two natives. The language of mathemati s has developed in order to over ome the limitations of natural language. 5. several laws of arithmeti . A and B. in luding asso iativity of addition.nd it a eptable to say 10:70! Yet. written as a pre.45 and 11:10.4 Negation Consider the following knights-and-knaves problem.

) The goal is to simplify this expression. we know that A ≡ S . if A makes a statement S . for this problem: A ≡ B ≡ ¬A . Reading this as ¬p = (p ≡ false) . In order to ta kle this problem. May 28.x to its argument. For arbitrary proposition p . (We swit h from \ = " to \ ≡ " here in order to exploit asso iativity. \ ¬p " is pronoun ed \not p ". Using the general rule that. If p is a boolean expression. the law governing ¬p is: (5.3) [Negation] ¬p ≡ p ≡ false . Algorithmi Problem Solving Roland Ba khouse. we get. it is ne essary to begin by formulating al ulational rules for negation. 2008 .

79 5. Negation it fun tions as a de.4.

So. This simpli. we are given that: A ≡ B ≡ ¬A . In addition. Reading it the other way: (¬p ≡ p) = false it provides a way of simplifying propositional expressions. Returning to the knights-and-knaves problem.nition of negation. we also get the property: p = (¬p ≡ false) . the symmetry of equivalen e means that we an rearrange the terms in a ontinued equivalen e in any order we like.

Suppose.3) with p := A } false ≡ B = { law (5.es to ¬B as follows: A ≡ B ≡ ¬A = { rearranging terms } ¬A ≡ A ≡ B = { law (5. May 28. we want to simplify ¬p ≡ p ≡ q ≡ ¬p ≡ r ≡ ¬q . Note how (5.2) and (5. We begin by rearranging all the terms so that repeated o urren es of \ p " and \ q " are grouped together. in onjun tion with the symmetry and asso iativity of equivalen e. but A ould be a knight or a knave.3) to redu e the number of o urren es of \ p " and \ q " to at most one (possibly negated).3) is used in two dierent ways.3) with p := B and rearranging } ¬B . B is a knave. provides a way of simplifying ontinued equivalen es in whi h one or more terms are repeated and/or negated. for example.3). In this parti ular example we obtain true ≡ p ≡ false ≡ r . 2008 . So. Now we an use (5. Algorithmi Problem Solving Roland Ba khouse. The law (5. Thus we get ¬p ≡ ¬p ≡ p ≡ q ≡ ¬q ≡ r .

we use (5. The result is that the original formula is simpli.2) and (5.3) again.80 5. Knights and Knaves Finally.

ed to ¬p ≡ r . this pro ess an be ompared with the simpli. Just as before.

where now negative terms may also appear. The expression p + (−p) + q + (−p) + r + q + (−q) + r + p is simpli. ation of an arithmeti expression involving ontinued addition.

q and r .3) are all that is needed to de.ed to q + 2r by ounting all the o urren es of p . Again. The two laws (5. the details are dierent but the pro ess is essentially identi al.2) and (5. an o urren e of −p an elling out an o urren e of p .

(See hapter 3. If we let n denote the number of rossings. The name refers to the use of the rule in the form (p ≡ q) = (¬p ≡ ¬q) .4) [Contraposition] p ≡ q ≡ ¬p ≡ ¬q . We used the rule of ontraposition impli itly in the river- rossing problems. but whi h is surprisingly useful.) Re all that ea h problem involves getting a group of people from one side of a river to another. 2008 . May 28.ne the way that negation intera ts with equivalen e. and l 2 Other tion. (5. is the rule we all ontraposition 2 . 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 . authors use the name \ ontraposition" for a less general rule ombining negation with impli a- Algorithmi Problem Solving Roland Ba khouse. 5. using these two laws we an derive several other laws. using one boat.5 Contraposition A rule that should now be obvious.

We are given that.n) } ¬(even. and the boat hanges side. initially. Another example is the following.n ≡ l is invariantly true .n ≡ l .n ≡ l)[n . the number of rossings in reases by one. we on lude that even. l := n+1 . the boat is on the left side equivales the number of rossings is even. In words. Sin e zero is an even number. a rossing of the river is modelled by the assignment: n . Suppose it is required to move a square arm hair sideways by a distan e equal to its own width.81 5. The rule of ontraposition tells us that even. (See .n) ≡ ¬l = { ontraposition } even. ¬l . ¬l] = { rule of substitution } even. l := n+1 .n ≡ l is invariant under this assignment. This is be ause (even.(n+1) ≡ ¬(even. Contraposition denote the boolean \the boat is on the left side of the river". In words. the boat is on the left side.5.(n+1) ≡ ¬l = { even.

) However. like a hess board. Is it possible to move the hair as desired? If so. Suppose the arm hair is initially positioned along a north-south axis.1. the hair is so heavy that it an only be moved by rotating it through 90◦ around one of its four orners. how? If not. that the oor is painted alternately with bla k and white squares.1: Moving a heavy arm hair.2. Algorithmi Problem Solving Roland Ba khouse. 2008 . The answer is that it is impossible.gure 5. why not? Figure 5. May 28. (See Figure 5. 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. also.) Suppose the arm hair is initially on a bla k square. Suppose. with ea h of the squares being the same size as the arm hair.

rotating the arm hair Now. moving the arm hair sideways one square hanges the olour but does not hange the dire tion. Roland Ba khouse. That is. A hessboard is an Algorithmi Problem Solving 8×8 grid of squares. ¬dir The rule of ontraposition states that an invariant of this assignment is col ≡ dir . May 28. Then. and dir represent the dire tion that the arm hair is fa ing (say. two pla es left or right and one pla e up or down. no matter how many times the arm hair is rotated. the value of this expression will remain equal to its initial value. Knights and Knaves col represent the olour of the square that the arm hair is on (say. an invariant of rotating the arm hair through the hair is on a bla k square ≡ 90◦ around a orner point is the hair is fa ing north-south whi h is false when the hair is on a white square and fa ing north-south.82 5. a knight's move is two pla es up or down and one pla e left or right. vi e versa.2: Invariant when moving a heavy arm hair. let boolean about any orner is represented by the assignment: col . But. 2008 .5 (Knight’s Move) In the game of hess. true for north-south and false for east-west). 90◦ Figure 5. In words. Exercise 5. dir := ¬col . and is impossible to a hieve by ontinually rotating the arm hair as pres ribed. or. it hanges the value of col ≡ dir . So. true for bla k and false for white).

Suppose ea h person ounts the number of times they shake hands. means that.6. There are n numbers in the range 0 to n−1 . We on lude that everyone shakes hands with between 0 and n−1 people. Suppose there are n people. Handshake Problems 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. In this way. for all x and y . xSy is read as \person x shakes hands with person y ". We are required to show that (at least) two people shake hands the same number of times. Hint: How many moves have to be made? Model a move in terms of the ee t on the number of moves and the olour of the square on whi h the knight is standing. identify a relation between the two that is invariant under a move. for any two people |Ja k and Jill. and suppose we use x and y to refer to people. May 28. say| Ja k shakes hands with Jill equivales Jill shakes hands with Ja k. These are that it is a binary relation .) It being a \symmetri " relation means that. a relation is any boolean-valued fun tion. Algorithmi Problem Solving Roland Ba khouse. someone shakes hands 0 times and someone shakes hands n−1 times. Finally. 2 5. 2008 . (In general. In parti ular. Suppose we abbreviate \shake hands" to S . Cru ial to how we solve this problem are the properties of shaking hands. or just x shakes hands with y . xSy ≡ ySx . The simplest example of a handshake problem is this: Suppose that at a party. everyone shakes hands with at most n people. Then. and it is anti-re exive . The symmetry of the shake-hands relation makes this impossible. However. Then the symmetry of \shakes hands" gives us the rule. the anti-re exivity property is that noone shakes hands with themselves. for any two people |Ja k and Jill. say| Ja k shakes hands with Jill is either true or false. Show that at least two people have the same ount. it being \anti-re exive" means that no-one shakes hands with themselves. 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.83 5. Binary means that it is a fun tion of two arguments. some people shake hands and some don't.6 Handshake Problems Logi al properties of negation are fundamental to solving so- alled handshake problems . The negation of \two people shake hands the same number of times" is \everyone shake hands a distin t number of times". it is symmetri . It being a \binary relation" on people.

Suppose a number of ouples (husband and wife) attend a party.) However.4. This statement is formulated as B 6= A . whereas the Queen never does. and b shakes hands with a . the out ome might be dierent. x doesn't shake hands with y equivales y doesn't shake hands with x . Then. Like \shake hands". the same as saying \B is a knight equals I am not a knight". Were we to onsider a similar problem involving a dierent relation. but the greeting is not returned. In words. substituting equals for equals. if we repla e \shake hands" by some other form of greeting like \bows or urtsies". and one bows to the other. ¬(xSy) ≡ ¬(ySx) . in fa t. and gets a different answer every time.7 Inequivalence In the knights-and-knaves problem mentioned at the beginning of se tion 5. Note arefully how the symmetry and anti-re exivity of the shakes-hands relation are ru ial. and so we on lude that two people must shake hands the same number of times. the property need not hold3. in order to exploit asso iativity. Some people shake hands. \rub noses" is a symmetri and anti-re exive relation. whi h is not symmetri . as is \don't shake hands". anyone meeting the British Queen is required to bow or urtsey. Note that we swit h from \ = " to \ ≡ " on e again. others do not. But then. How many times did the host and the host's partner shake hands? Exercise 5. suppose person a shakes hands with noone and person b shakes hands with everyone. Now. May 28. the \host". A might have said \B is dierent from myself". Husband and wife never shake hands. i. 3 At the time of writing. 2008 . The relation is not symmetri .e. in parti ular. for all x and y . or ¬(B = A) This is. It's a bit more diÆ ult to solve. ¬(aSb) . we have both ¬(aSb) and aSb . as the following al ulation shows. asks everyone else how many times they have shaken hands. Knights and Knaves The ontrapositive of this rule is that.e. One person. The assumption that everyone shakes hands with a distin t number of people has led to a ontradi tion. the property does hold. Here is another handshaking problem.84 5.6 2 5. but the essen e of the problem remains the same: \shake hands" is a symmetri relation. For example. whi h is false. a does not shake hands with b . Algorithmi Problem Solving Roland Ba khouse. (Suppose there are two people. if \shake hands" is repla ed by \rub noses". bSa . i.

(5.7) [Inequivalence] ¬(p ≡ q) ≡ p ≡ ¬q . The . for all propositions p and 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.7. We have thus proved. Inequivalen e ¬(B ≡ A) = { the law ¬p ≡ p ≡ false with p := (B ≡ A) } B ≡ A ≡ false = { the law ¬p ≡ p ≡ false with p := A } B ≡ ¬A .85 5.

the se ond omes free with asso iativity: (¬(p ≡ q) ≡ p) = ¬q . The operator is alled inequivalen e (or ex lusive-or . Inequivalen e is also asso iative: (p 6≡ q) 6≡ r = { de. abbreviated xor ).rst is the one proved dire tly: ¬(p ≡ q) = (p ≡ ¬q) . The proposition ¬(p ≡ q) is usually written p 6≡ q .

with p. applied twi e } ¬(¬(p ≡ q) ≡ r) = { (5.7).4).7).q := p ≡ q .nition of \ 6≡ ".4). q ≡ r } ¬(p ≡ ¬(q ≡ r)) = { de. with p. r } p ≡ q ≡ r = { ontraposition (5.q := ¬(p ≡ q) . r } ¬(p ≡ q) ≡ ¬r = { ontraposition (5.q := p .q := p . with p. q ≡ r } ¬p ≡ ¬(q ≡ r) = { (5. with p.

Algorithmi Problem Solving Roland Ba khouse. applied twi e } p 6≡ (q 6≡ r) . 2008 .nition of \ 6≡ ". May 28.

86 5. we have shown that p 6≡ q 6≡ r and p ≡ q ≡ r are equal. we an write the ontinued inequivalen e p 6≡ q 6≡ r without fear of ambiguity4. As a . as a byprodu t. Note that. Knights and Knaves As a result.

nal worked example. we show that inequivalen e asso iates with equivalen e: (p 6≡ q) ≡ r = { expanding the de.

r } p ≡ ¬(q ≡ r) = { de. the law (5.q := q.7) is applied in the form ¬(p ≡ q) ≡ ¬q ≡ p with p.nition of p 6≡ q } ¬(p ≡ q) ≡ r = { ¬(p ≡ q) ≡ p ≡ ¬q } p ≡ ¬q ≡ r = { using symmetry of equivalen e.

whi h some authors o asionally write. 2008 . Inequality is not transitive.nition of q 6≡ r } p ≡ (q 6≡ r) .) Exercise 5. and should not be onfused with p 6= q 6= r . so su h expressions should be avoided.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 4 This is to be read asso iatively. Algorithmi Problem Solving Roland Ba khouse. May 28. Simplify the following. rearranging the variables and/or onstants doesn't make any dieren e. Also. (Note that in ea h ase it does not matter in whi h order you evaluate the subexpressions.

Exploitation of the asso iativity of equivalen e eliminates the tedious and inee tive ase analysis that is often seen in solutions to logi puzzles. Show that. Inequivalen e an be repla ed by equivalen e in the But.9 Prove that ¬true = false 2 Exercise 5. and this possibility is never exploited! Algorithmi Problem Solving Roland Ba khouse.11 (Encryption) (p 6≡ (q 6≡ r)) ≡ ((p 6≡ q) 6≡ r) . That is. in addition to the standard properties of equality. en ryption and de ryption pro ess. the result is b independently of the key a . that is Exercise 5. is used to en rypt data. 2 On the island of knights and knaves.8 Summary In this hapter. A and B. we have used simple logi puzzles to introdu e logi al equivalen e | the equality of boolean values| .8. To en rypt a single bit b of data. Equivalen e has the remarkable property of being asso iative. the most fundamental logi al operator. the re eiver uses the same key a to ompute a 6≡ c . 5 This operation is usually alled \ex lusive-or" in texts on data en ryption. c . What question should you ask A to determine whether A and B are dierent types? Exercise 5. The re eiver de rypts the re eived bit. if bit b is en rypted and then de rypted in this way.10 (Double Negation) Prove the rule of double negation ¬¬p = p .87 5. Summary 2 Exercise 5. May 28. a key a is hosen and the en rypted form of b that is transmitted is a 6≡ b .12 2 5. very few s ientists and engineers are aware of the algebrai properties of equivalen e. you en ounter two natives. 2008 . using the same operation5. 2 The fa t that inequivalen e is asso iative. it is not ommonly known that ex lusive-or and inequivalen e are the same.

parti ularly if one tries to express its properties in natural language.W. 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. having been mentioned by Alfred Tarski in his PhD thesis. where its dis overy is attributed to J. Dijkstra in his work on program semanti s and mathemati al method.g. 2008 .) Nevertheless. see [Ba 03℄ or [GS93℄. May 28. whi h in ludes dis ussion of onjun tion (\and"). Tarski is a famous logi ian. [DS90℄. provides ample eviden e that the adheren e to \natural" modes of reasoning is a major impediment to ee tive reasoning. but to provide a more powerful alternative. disjun tion (\or"). and introdu ing the symbol \ 0 " to denote it. enturies-long pro ess of a epting zero as a number. The fa t that equality of boolean values is asso iative has been known sin e at least the 1920's . The painful. Lukasiewi z. (See e. For a omplete a ount of al ulational logi . The purpose of a al ulus is not to mimi \natural" or \intuitive" reasoning. But Smullyan's proofs invariably involve detailed ase analyses.88 5. (See the paper \On the primitive term of logisti " [Tar56℄.) The origin of the logi puzzles is Raymond Smullyan's book \What Is The Name Of This Book?" [Smu78℄. follows-from (\if") and impli ation (\only if"). Algorithmi Problem Solving Roland Ba khouse. its usefulness was never re ognised until brought to the fore by E. The exploitation of the asso iativity of equivalen e in the solution of su h puzzles is due to Gerard Wiltink [Wil87℄. Knights and Knaves The asso iativity of equivalen e an be diÆ ult to get used to. this should not be used as an ex use for ignoring it. However.

su h problems are very easy to solve. we then solve the problem in two steps. First. where the number of mat hes is a parameter. 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 .1 Example Problems All the following problems an be solved by indu tion. the problem might involve a pile of mat hsti ks. et . 6. (They are often dismissed as \trivial". given a solution to a problem of size n . a problem of size n+1 . 3 . The idea is that we somehow measure the \size" of instan es of a problem. then problems of size 4 . In the . 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 . We an now solve problems of size 3 . This is alled the basis of the indu tion. And so it goes on. we know how to solve problems of size 2 . we onsider problems of size 0 .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. whole number | thus 0 . Usually. We also know how to solve problems of size 1 . an instan e of the problem is then a parti ular pile of mat hes. whole number1. 2 . how the size of an instan e of a problem is measured is quite obvious from the problem des ription.) Se ond. By this pro ess. This is alled the indu tion step. for an arbitrary natural number n . et . we show how to solve. and its size is the number of mat hes in the pile. we an solve problems of size 0 . A requirement is that the size is a non-negative. For example. We use the term natural number for a nonnegative. Having de ided how to measure size. Almost invariably. 1 . Then.

There are. 89 May 28. 2008 . very good reasons why 0 0 from the natural numbers. and 1 Warning : Mathemati ians often ex lude the number however. making a break with tradition imperative. the size is the number of lines. should always be in luded. it is expli tly given by the parameter n . Algorithmi Problem Solving Roland Ba khouse.rst. In the se ond and third problems.

it is the number of disks. See . You then have to solve the indu tion step.90 6. the basis should be easy. We dis uss ea h problem in turn in oming se tions. Cutting the Plane A number of straight lines are drawn a ross a sheet of paper. 1. ea h line extending all the way from from one border to another. In ea h ase. Indu tion in the fourth.

Show that it is possible to olour ea h region bla k or white so that no two adja ent regions have the same olour (that is. so that the two regions on opposite sides of any line segment have dierent olours). and the others are left un overed.g. Trapeziums An equilateral triangle. where n is a natural number2.) NB: The ase n = 0 should be in luded in your solution. with side of length 2n for some natural number n .2 shows. the paper is divided into a number of regions. In this way. See . Figure 6. Show that it is possible to over the remaining squares with (non-overlapping) triominoes. On the right is a triomino. 6. A triomino is an L-shape made of three grid squares. is made of smaller equilateral triangles. (Fig. 2. A bu ket-shaped trapezium is made from three equilateral triangles. 3.1: Bla k and White Colouring. Figure 6. One grid square is overed.1. The topmost equilateral triangle is overed. an 8×8 grid with one square overed. 6. on the left. The individual squares are alled grid squares. Triominoes A square pie e of paper is divided into a grid of size 2n×2n .3 shows a solution in one ase.

4. 2 Re all that the natural numbers are the numbers 0.g. 1. 6. 2008 . et . May 28. 2. Algorithmi Problem Solving Roland Ba khouse.

3: Triomino Problem. Example Problems Figure 6.1.91 6. Solution to .2: Triomino Problem. Figure 6.

g. Show that it is possible to over the remaining triangles with (non-overlapping) trapeziums.2. 6. See .

5 for the solution in the ase that n is 2 . 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. In lude the ase n = 0 in your solution. Claus. 6. NB. 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 .g.

On the .xed in the ground.

rst of these poles. in de reasing order of size. ea h of dierent size. at the time of the world's reation. (See . God pla ed sixty four golden disks.

6. one per day.g. 6. The monks' task will be omplete when they have su eeded in moving all the disks from the . from one pole to another a ording to the rule that no disk may ever be above a smaller disk.) The Brahmin monks were given the task of moving the disks.

2008 .rst of the Algorithmi Problem Solving Roland Ba khouse. May 28.

4: A Pyramid of Equilateral Triangles. Figure 6.92 6.5: Solution to . Indu tion Figure 6.

) Algorithmi Problem Solving Roland Ba khouse.g. it does provide the basis for onstru ting a so- alled iterative solution to the problem. May 28. on the day that they omplete their task. poles to the se ond and. 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.4. However. 2008 . 6.

ea h line extending all the way from from one border to another.93 6. See .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.2. Cutting The Plane Figure 6.

the number of lines is an obvious measure of the \size" of the problem.g. For this problem. thus. For brevity. The goal is. we all a olouring of the regions with the property that no two adja ent regions have the same olour a satisfa tory olouring.1. to solve the problem \by indu tion on the number of lines". Algorithmi Problem Solving Roland Ba khouse. assuming that we an solve the problem when there are n lines |this is the indu tion step| . May 28. so that the two regions on opposite sides of any line segment have dierent olours). 2008 . 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. 6. the paper is divided into a number of regions. In this way. 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.

6.94 6. The sheet of paper is divided into one region. either olouring meeting the onditions of a solution (be ause there is no pair of adja ent regions). su h pairs of regions will have the same olour. For the indu tion step. and so the existing olouring is not satisfa tory. We now suppose that an additional line is drawn on the paper. and this an be oloured bla k or white. Indu tion The ase where there are zero lines is easy. Fig. The plane has been divided into twelve regions by .7 is an example. and the dierent regions have been oloured bla k or white so that no two adja ent regions have the same olour. we assume that a number of lines have been drawn on the sheet of paper. This assumption is alled the indu tion hypothesis. This will divide some of the existing regions into two.

either side of the additional line. 2008 . Figure 6. easily remembered. Let us all these regions the left and right regions. Additional line shown in red. In order to guarantee that. shown in red for larity. and invert all the olours in that region. It is just a onvenient. Now. the regions have the same olour. 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). as required. It also gives a satisfa tory olouring of the right region (be ause the Algorithmi Problem Solving Roland Ba khouse. May 28. This has had the ee t of dividing four of the regions into two. and vi e-versa) also gives a satisfa tory olouring. The key to a solution is to note that inverting the olours of any satisfa tory olouring (that is. way of naming the regions. we do not imply that the additional line must be from top to bottom of the page. hoose. the left region. (By this hoi e of names. has been added.) Note that the assumed olouring is a satisfa tory olouring of the left region and of the right region.7: Cutting the Plane. hanging a bla k region to white. all regions have opposite olour.ve lines. say. On either side of the red line. adja ent regions have dierent olours. the additional line divides the sheet of paper into two regions. Elsewhere. The task is to show how to modify the olouring so that it does indeed be ome a satisfa tory solution. thus in reasing the number of regions by four. An additional line. and the regions oloured bla k and white.

6.95 6. Figure 6. In order to apply the onstru tion to an instan e of the problem with. Blue has been used instead of bla k in order to make the inversion of the olours more evident.8: Cutting the Plane. Cutting The Plane olouring hasn't hanged.2. say. 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). we begin by olouring the whole sheet of paper.8 shows the ee t on our example. This ompletes the indu tion step. Also. be ause they have hanged from being the same to being dierent. seven lines. the olouring of adja ent regions at the boundary of the left and right regions is satisfa tory. Then the lines are added one-by-one. Ea h time a line is added. Fig. and was satisfa tory already). the existing olouring is modi.

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 as pres ribed in the indu tion step.

The ordering of the lines (whi h to add .ed.

) is also unspe i. et .rst. whi h to add next.

ed. and whi h the \right" region is unspe i. whi h region is hosen as the \left" region. Finally.

ed. This means that the .

But that doesn't matter. The .nal olouring may be a hieved in lots of dierent ways.

nal olouring is guaranteed to be \satisfa tory". as required in the problem spe i.

The problem assumes the lines are drawn on a pie e of paper. Che k your understanding by onsidering variations on the problem. May 28. or on the surfa e of a doughnut? We remarked that the algorithm for olouring the plane is non-deterministi .1 2 Algorithmi Problem Solving Roland Ba khouse. ation. 2008 . Is the solution still valid if the lines are drawn on the surfa e of a ball. How many dierent olourings does it onstru t? Exercise 6. Why is it required that the lines are straight? How might this assumption be relaxed without invalidating the solution.

is the number n . This one square is. The obvious measure of the \size" of instan es of the problem. Re all the statement of the problem. the one that is overed. We solve the problem by indu tion on n. i. suppose we onsider a grid of size 2n+1×2n+1 . and the others are left un overed. We make the indu tion hypothesis that it is possible to over any grid of size 2n×2n with triominoes if. Indu tion Triominoes As a se ond example of an indu tive onstru tion. A triomino is an L-shape made of three grid squares. A square pie e of paper is divided into a grid of size 2n×2n . . where n is a natural number.3 6. there is exa tly one square. Show that it is possible to over the remaining squares with (non-overlapping) triominoes. The individual squares are alled grid squares. in this ase. is how the base ase is solved.1. One grid square is overed. That is. Now. then. leaving no squares un overed. The grid then has size 20×20 . let us onsider the grid problem posed in se tion 6.96 6. It takes 0 triominoes to over no squares! This. 1×1 . inevitably.e. The base ase is when n equals 0 .

bottom-right. We an apply the indu tion hypothesis to them if just one square in ea h of the three is overed. We have to show how to exploit this hypothesis in order to over a grid of size 2n+1×2n+1 of whi h one square has been overed. an arbitrary grid square has been overed. Let us all the four grids the bottom-left.) The bottom-left grid is thus a grid of size 2n×2n of whi h one square has been overed. the remaining squares in the bottom-left grid an be overed with triominoes.rst. One grid square is already overed. top-left. (If not. A grid of size 2n+1×2n+1 an be subdivided into 4 grids ea h of size 2n×2n . as yet. This is done by pla ing a triomino at the jun tion of the three grids. and top-right grids. simply by drawing horizontal and verti al dividing lines through the middle of ea h side. This leaves us with the task of overing the bottom-right. the entire grid an be rotated about the entre so that it does be ome the ase. top-left and top-right grids with triominoes. This square will be in one of the four sub-grids. We may assume that it is in the bottom-left grid. By the indu tion hypothesis. as shown in . None of the squares in these three grids is overed.

May 28. 6. the indu tive hypothesis is applied to over the remaining squares of the bottomright. top-left and top-right grids with triominoes.g. Now.9. 2 Algorithmi Problem Solving Roland Ba khouse. On ompletion of this pro ess. the entire 2n+1×2n+1 grid has been overed with triominoes. Exercise 6. 2008 .2 Solve the trapezium problem given in se tion 6.1.

9: Triomino Problem. The grid is divided into four sub-grids. shown in bla k.4. Looking For Patterns Figure 6. The overed square.97 6. identi. Indu tive Step.

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

ation is the pro ess of dedu ing whether the guess is Algorithmi Problem Solving Roland Ba khouse. 2008 . May 28.

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

the opponent is left with either 3n + 2 or 3n + 1 mat hes. A pile of 1 or 2 mat hes is a winning position be ause the player an remove the mat hes. whose turn it is. and a pile of 3(n+1) + 1 or 3(n+1) + 2 mat hes is a winning position. by the indu tion hypothesis. We have to show that a pile of 3(n+1) mat hes is a losing position. and a pile of 3n + 1 or 3n + 2 mat hes is a winning position. Now. But. By taking 1 mat h in the . the game is lost when it is no longer possible to move. this leaves the opponent in a winning position. we assume that a pile of 3n mat hes is a losing position.nition. The player. suppose there are 3(n+1) + 1 or 3(n+1) + 2 mat hes. That is. must remove 1 or 2 mat hes. Suppose there are 3(n+1) mat hes. for the indu tion step. leaving the opponent in a losing position. Hen e. the position in whi h there are 3(n+1) is a losing position. leaving either 3(n+1) − 1 or 3(n+1) − 2 behind. Now.

the player leaves the opponent in a position where there are 3(n+1) mat hes. and thus veri.rst ase. the positions in whi h there are 3(n+1) + 1 or 3(n+1) + 2 are both winning positions. and 2 mat hes in the se ond ase. Hen e. This we now know to be a losing position. This ompletes the indu tive onstru tion of the winning moves.

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. 2008 . May 28.

5.99 6. it is vital that any onje ture is properly veri. The Need For Proof 6.5 The Need For Proof When using indu tion.

The points are hosen in su h a way that all interse tion points of pairs of hords are distin t. This suggests that the number of portions. It is too easy to extrapolate from a few ases to a more general laim that is not true. for arbitrary n . Indeed. This se tion is about a non-trivial example of a false onje ture. 2 . Many onje tures turn out to be false. 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. 4 and 8 .10 shows the ase when n is 1 . this onje ture is supported by the ase that n = 5 . is 2n−1 . 3 or 4 . in how many portions does this ut the ake? Figure 6. (We leave the reader to draw the .10: Cutting the ake The number of portions is su essively 1 .ed. only by subje ting them to the rigours of proof an we be sure of their validity. 2 . Figure 6. The question is.

) In this ase. for n = 6 .gure. whi h is 25−1 . However. the number of portions is 31 ! (See . the number of portions is 16 .

) Note that n = 6 is the .11.g. 6.

The derivation of the orre t formula for the number of portions is too ompli ated to dis uss here. and impose the requirement that n is dierent from 0 . is to dismiss this ase. suspi ions about the onje ture would already have been raised | it doesn't make sense to say that there are 20−1 portions. but inadequate way out.rst ase in whi h the points are not allowed to be pla ed at equal distan es around the perimeter.6 From Verification to Construction In mathemati al texts. indu tion is often used to verify known formulae. Veri. even though utting the ake as stated does make sense! The easy. but it does in lude the ase that n equals 0 ! 6. Had we begun by onsidering the ase that n = 0 .

ation is important but has a major drawba k | it seems that a substantial amount of lairvoyan e Algorithmi Problem Solving Roland Ba khouse. 2008 . May 28.

The ase n = 6 .11: Cutting the ake.100 6. not 26−1 is needed to ome up with the formula that is to be veri. Indu tion Figure 6. The number of portions is 31 .

And. if one's onje ture is wrong.ed. veri.

Indu tion is not important in omputing s ien e as a veri. ation gives little help in determining the orre t formula.

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 .

. often given in mathemati al texts.rst n natural numbers. . +n = 1 n(n+1) . the examples also illustrate the weakness of veri.. A well-known formula gives the sum of the natural numbers from 1 thru n : 1+2+ . . 2 Two other exer ises. . . 4 and As well as being good examples of the strength of the prin iple of mathemati al indu tion. + n3 = 1 2 n (n+1)2 . are to verify that 12 + 22 + . + n2 = 1 n(n+1)(2n + 1) 6 13 + 23 + .

for example. but what happens if the answer is not already known! Suppose. that you are now asked to determine a losed formula for the sum of the 4 th powers of the . ation: the te hnique works if the answer is known.

rst n numbers 14 + 24 + . Algorithmi Problem Solving Roland Ba khouse. . 2008 . . + n4 = ? . May 28.

From Veri.6.6.

ation to Constru tion 101 How would you pro eed? Veri.

Redu ing the guesswork to a minimum. and trial-and-error annot be ompletely eliminated. 2 and 3 . does not seem to be appli able unless we already know the right side of the equation. the sum of the m th powers of the . ation. for m equal to 1 . The key to su ess is simpli ity. A simple pattern in the formulae displayed above is that. Don't be over-ambitious. repla ing it by mathemati al al ulation is the key to su ess. say. Leave the work to mathemati al al ulation. 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. not! Constru ting solutions to non-trivial problems involves a reative pro ess. k27 ? Almost ertainly. using the prin iple of mathemati al indu tion. formulate the pattern in pre ise mathemati al terms and then verify the pattern. This means that a ertain amount of guesswork is ne essary. The general idea is to seek a pattern. Indu tion an be used to onstru t losed formulae for su h summations.

(The sum 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 .

and the sum of the .rst n squares is a ubi fun tion of n .

rst n ubes is a quarti fun tion of n .) This pattern is also on.

A strategy for determining a losed formula for. + n0 = n . . say. the sum of the fourth powers is thus to guess that it is a . .rmed in the (oft-forgotten) ase that m is 0 : 10 + 20 + .

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

nition of P } Algorithmi Problem Solving Roland Ba khouse. 2008 . May 28.

(n+1) = de. Then P. the oeÆ ient of n0 . To do so.102 6. So the basis of the indu tion has allowed us to dedu e that a .n is true. we al ulate b and c . Indu tion S.0 = 0 (the sum of an empty set of numbers { is zero) and arithmeti } 0=a . we make the indu tion hypothesis that 0 ≤ n and P.0 = a + b×0 + c×02 = S. Now. is 0 .

n + n + 1 = b(n+1) + c(n+1)2 = assumption: P. { S. a = 0 { } S.n + n + 1 } S. a = 0 .(n+1) = S.n . Also.(n+1) = b(n+1) + c(n+1)2 = heading for use of the indu tion hypothesis.nition of P . { That is. From the onje ture that the sum of the .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 . S.

+ nm as a polynomial fun tion for any given natural number m .n + (n+1)m (where S.. .. 2 2 Extrapolating from this al ulation. The steps in the algorithm are: postulate that the summation is a polynomial in n with degree m+1 .(n+1) is S. we have thus al ulated that 1+2+ .0 is 0 and S. 2008 .n denotes 1m + 2m + . . + nm ) to determine Algorithmi Problem Solving Roland Ba khouse. one an see that it embodies an algorithm to express 1m + 2m + . May 28. . Use the prin iple of mathemati al indu tion together with the fa ts that S. .rst n numbers is a quadrati in n . +n = 1 1 n + n2 .

Try to identify a simple pattern in the way winning and losing positions are grouped. . Algorithmi Problem Solving Roland Ba khouse. .4 n 1 n 2 6. . 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) . n+1 2 Consider a mat hsti k game with one pile of mat hes from whi h m thru mat hes an be removed.6 an be summarised as \Don't guess! Cal ulate. However. . .. . and what the winning strategy is. Remark : In the ase of the sum 1 + 2 + .3 Use the te hnique just demonstrated to onstru t losed formulae for 10 + 20 + ." We put this into pra ti e in this se tion. + n+1 . solve the system of equations. + n there is an easier way to derive the orre t formula. . or m is 2 and n is 3 ). . . By onsidering a few simple examples (for example. Exercise 6. + n2 .. Then add the two rows together: + n+1 + .103 6. . m is and is arbitrary. + 1 . May 28.7 Fake-Coin Detection The motto of se tion 6. End of remark. Exercise 6. Fake-Coin Dete tion a system of simultaneous equations in the oeÆ ients. formulate a general rule for determining whi h are the winning positions and whi h are the losing positions.. + nm 2 for m greater than 1 . . Simply write down the required sum 1 + 2 + . 2008 . Introdu e variables to represent the grouping. this method annot be used for determining 1m + 2m + . and al ulate the values of the variables. and immediately below it n + n−1 + . + n .7. Finally. Avoid guessing the omplete solution. + n0 and 12 + 22 + .

We are told that among them there is at most one \fake" oin. All \genuine" oins have the same weight. Indu tion Suppose we are given a number of oins. The problem is how to use the pair of s ales optimally in order to . and all the rest are \genuine".104 6. whereas a \fake" oin has a dierent weight to a \genuine" oin. ea h of the same size and shape.

and needs revision. More pre isely. if there are no other oins to ompare it with? Our onje ture has broken down. in this ase. if the number. Then there are 1 + 2m dierent possibilities that an be observed with a pair of s ales: \ 1 " possibility is that all oins are genuine. Algorithmi Problem Solving Roland Ba khouse. Several stages are needed. however. We have almost rea hed the point at whi h we an state our problem pre isely. or they may tip to the right. Now. there are at most 3n dierent out omes3. we are given (3n−1)/2 oins about whi h we know nothing ex ept that at most one is fake. Often. and we are also given 3 Note the impli it use of indu tion here. they may balan e. of oins is greater than (3n−1)/2 .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. For n equal to 1 . of whi h at most one is fake and the rest are genuine. it is impossible to guarantee that a fake oin an be found with n omparisons. if there is one. given (3n−1)/2 oins of whi h at most one is fake. otherwise. the onje ture is learly true. we run into a problem. 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. 6. 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. the number of oins among whi h at most one fake oin an be dete ted is at most m . This means that.7. This gives an upper bound on what an be a hieved using a pair of s ales. This is deliberate. 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. For n equal to 0 . We onje ture that. suppose we are given m oins. Note the element of vagueness in this problem statement. The assumption is that there is one oin (sin e (31−1)/2 = 1 ). But how an we tell whether this one oin is fake or genuine. m . in luding some \ba ktra king" and revision. May 28. with n omparisons. 2008 . all of whi h are genuine. where 1 + 2m = 3n . we don't say what we mean by using the s ales \optimally". Thus. there are no oins.nd the fake oin. 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.

using at most n omparisons. 6. if it exists. in its solution. We have to show how to . For brevity. The base ase. an be found among c. or determine that all oins are genuine. The Basis With zero omparisons. The problem is to onstru t an algorithm that will identify the fake oin. Induction Step Now.7. Suppose n is at least zero. we ta kle the indu tion step. n equal to 0 . By indu tion. if it exists. is thus solved. let us use c. Fake-Coin Dete tion at least one oin that is known to be genuine.2 Problem Solution Our formulation of the problem begs the use of indu tion on the number of omparisons.105 6. n . we an report immediately that all oins in a olle tion of (30−1)/2 are genuine.n oins using at most n omparisons. we may assume that a fake oin.n to denote (3n−1)/2 .7.

nd a fake oin. if it exists. using at most n+1 omparisons.(n+1) oins. among c. Consider the .

from whi h one infers that none of the oins on the s ales is fake.rst omparison. the number of oins on the two s ales must be equal. The algorithm would then pro eed to try to . and leaving some on the table. To be able to draw any on lusion from the omparison. It involves putting some number of oins on the left s ale. some on the right s ale. One possible onsequen e of the omparison is that the s ales balan e.

Combined with the indu tion hypothesis.) We on lude that in the .n + 1 = 3n .nd a fake oin among the oins left on the table. c.n + 1 .(n+1) = (3n+1−1)/2 = 3×((3n−1)/2) + 1 = 3 × c.(n+1) − c. Now.n oins must be left on the table. It also di tates how many oins should be put on the s ales | this is the dieren e between c.(n+1) and c.n is the maximum number of oins among whi h a fake oin an be found with n omparisons. this di tates that c. This is be ause c.n .(n+1) oins whose kind we must determine. it an be made even by using one of the oins we know to be genuine. in addition to the c. (Re all the assumption that we have at least one oin that is known to be genuine. This is an odd number.n = 2 × c. So c.

n + 1 oins should be put on ea h of the two s ales.rst omparison. The next step is to determine what to do after the . c.

There are three possible out omes.rst omparison is made. of whi h we have already dis ussed one. 2008 . Algorithmi Problem Solving Roland Ba khouse. May 28. If the s ales balan e.

the base ase is easy. n ) omparisons are needed to determine this fa t. The Marked Coin Problem In this way.e. Exa tly one of the oins is fake. there is one oin. And ru ially.106 6. Indu tion the fake oin should be sought among the c. and an be eliminated from onsideration. we an on lude that all the oins left on the table are genuine. If the s ales tip to one side. The new problem is this. But we are still left with 3n oins none of whi h we know to be genuine. It is too weak! If the s ales tip to one side. That is. For the indu tion step. Suppose we are supplied with 3n+1 marked oins. 3n is greater than c. the problem we started with has been redu ed to a dierent problem. Again. 0 (i. By \possibly lighter" we mean genuine. and all the oins on the other side are possibly lighter than a genuine oin. By \possibly heavier" we mean genuine. We are unable to apply the indu tion hypothesis to this number of oins. Constru t an algorithm that will determine. or fake and heavier. or fake and lighter. we know that all the oins on that side are possibly heavier than a genuine oin. and all the rest are genuine. The omparison does tell us something about the oins on the s ales. The problem is what to do if the s ales tip either to the left or to the right.n oins left on the table. we an mark all the oins on the s ales one way or the other. At this point. we realise that the indu tion hypothesis doesn't help. In the . the fake oin among 3n marked oins. ea h of whi h is marked either \possibly light" or \possibly heavy". Suppose a number of oins are supplied.n . in the ase that the s ales do not balan e. with at most n omparisons. After the omparison. we pro eed as for the earlier problem. whi h must be the fake oin. If n equals 0 .

as already determined. To draw any on lusion from the omparison.rst omparison. The oins are marked in two dierent ways. Combining this with the markings. we need to determine how to pla e the oins a ording to their markings. we on lude that all oins on the left s ale are possibly heavy. Now. Similarly. We al ulate the numbers as follows. l1+h1 and l2+h2 should be equal. Furthermore. they should equal 3n . Suppose l1 possibly light oins are pla ed on the left s ale and l2 possibly light oins on the right s ale. the oins must be divided equally: 3n oins must be left on the table. 2008 . In order to apply the indu tion hypothesis in the ase that the s ales balan e. and some are left on the table. So. May 28. suppose h1 possibly heavy oins are pla ed on the left s ale and h2 possibly heavy oins on the right s ale. That is. if the omparison auses the s ales to tip to the left. we on lude that the l1 possibly light oins Algorithmi Problem Solving Roland Ba khouse. some oins are put on the left s ale. and thus 3n put on the left s ale and 3n on the right s ale. and all the oins on the right s ale are possibly light. some on the right. we require that the number of oins on the left s ale equals the number on the right.

The fake oin is identi. The Complete Solution This ompletes the solution to the marked- oin problem. until ea h s ale has its full omplement of 3n oins. we require that the number of oins not eliminated be equal to 3n . this leaves h1+l2 oins to be investigated further. whatever the out ome of the omparison. leaving l1+h2 oins to be investigated further. Again.7. one on the left and one on the right. if the s ale tips to the right. in order to apply the indu tion hypothesis.107 6. Simply pla e the oins on the s ales two at a time. the h1 possibly heavy oins on the left s ale and the l2 possibly heavy oins on the right s ale are genuine. at least two of them will have the same marking. We must arrange the oins so that ea h s ale ontains equal numbers of oins of the same kind. we infer that l1 = l2 and h1 = h2 . by hoosing any three oins. 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). Conversely. The hoi e an always be made be ause there are always at least three oins from whi h to hoose. This requirement an be met. This imposes the requirement that h1+l2 = l1+h2 = 3n . Together with l1+h1 = l2+h2 . and thus to the unmarked- oin problem. always hoosing two oins with the same marking.

Pro eed with the possibly heavy oins on the left s ale and the possibly light oins on the right s ale. A ording to the out ome of the omparison. So too are the possibly light oins on the right s ale and the possibly heavy oins on the left s ale. If the s ales tip to the right. in su h a way that there is an equal number of possibly light oins on ea h of the s ale. (3n−1)/2 + 1 and (3n−1)/2 . So too are the possibly light oins on the left s ale and the possibly heavy oins on the right s ale. If the s ales balan e. Divide the oins into three groups of sizes (3n−1)/2 . The solution to the unmarked- oin problem when the number of oins is (3n+1−1)/2 is as follows. one of the following is exe uted. all the oins on the s ales are genuine. Pro eed with the oins left on the table. Pro eed with the possibly heavy oins on the right s ale and the possibly light oins on the left s ale. If the s ales tip to the left. Pla e the . the oins on the table are genuine.ed from a olle tion of 3n+1 marked oins by pla ing 3n oins on ea h s ale. the oins on the table are genuine.

May 28. and leave the third group on the table. Determine the out ome of the omparison. 2008 . and pro eed as follows: Algorithmi Problem Solving Roland Ba khouse.rst group on the left s ale together with the supplied genuine oin. Pla e the se ond group on the right s ale.

with the ex eption of the supplied genuine oin as \possibly heavy".108 6.5 2 Given are n obje ts. If the s ales tip to the left. Mark all the oins on the left s ale. alled the unique obje t. Suppose you are given a number of obje ts. Apply the solution to the unmarked- oin problem (indu tively) to the oins on the table. you are provided with a pair of s ales. with the ex eption of one. In all other respe ts. even though the development ne essitates several su h hypotheses. all the oins on the balan e are genuine. Indu tion If the s ales balan e. You are required to determine whi h is the unique obje t. the oins on the table are genuine. If the s ales tip to the right. This is the epitome of the art of ee tive reasoning. with the ex eption of the supplied genuine oin as \possibly light". Apply the solution to the marked- oin problem to the 3n marked oins. 3m+1 obje ts an be split into 3 groups of 3m obje ts. whi h is the lighter and whi h is the heavier. A pair of s ales is provided so that it is possible to determine. Mark the oins on the right s ale as \possibly light". the obje ts are identi al. whi h has a dierent weight. Mark all the oins on the left s ale. Mark the oins on the right s ale as \possibly heavy". Apply the solution to the marked- oin problem to the 3n marked oins. for any two of the obje ts. All the obje ts have the same weight. (Hint: for the indu tion step. Quite the opposite: ea h hypothesis is systemati ally al ulated from the available information. For this purpose. where 1 ≤ n . a) How many omparisons are needed to . Show. Note that at no stage is a guess made at an indu tive hypothesis.) Can you identify whether the unique obje t is lighter or heavier than all the other obje ts? Exercise 6. that at most 2×m omparisons are needed to identify the unique obje t when the total number of obje ts is 3m . by indu tion on m . We ask the reader to review the development of this algorithm. ea h of dierent weight. the oins on the table are genuine.

that it is possible to determine whi h is the lightest and whi h is the heaviest obje t using 2n − 3 omparisons. B . C and D . Show how to . ) Suppose there are 4 obje ts with weights A . by indu tion on n .nd the lightest obje t? b) Show. Assume that 2 ≤ n . and suppose A < B and C < D .

nd the lightest and heaviest of all four with two additional omparisons. Use this to show how to .

as in your solution to part (b)). May 28. Exercise 6.6 Algorithmi Problem Solving Roland Ba khouse.nd the lightest and heaviest of 4 obje ts using 4 omparisons (and not 5 . 2008 .

Summary d) Suppose there are 2m obje ts.109 6. Show. that it is possible to .8. where 1 ≤ m . by indu tion on m .

and using mathemati al al ulation to . (Hint: make use of ( ). 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. The pro ess may involve some reative guesswork. by striving for simpli ity. it is possible to adapt the method to solve instan es of \size" n+1 . whi h is then subje ted to the rigours of mathemati al dedu tion.) 2 6. for arbitrary n . Using indu tion means looking for patterns. (b) given a method of solving instan es of \size" n .nd the lightest and heaviest obje ts using 3m − 2 omparisons. The key to su ess is to redu e the guesswork to a minimum.8 Summary Indu tion is one of the most important problem-solving prin iples.

Dij97℄.ll in ompli ated details. Algorithmi Problem Solving Roland Ba khouse. 6. Dijkstra [Dij90. 2008 . May 28.9 Bibliographic Notes The solution to the fake- oin problem is a ombination of two papers by Edsger W.

2008 .110 Algorithmi Problem Solving 6. May 28. Indu tion Roland Ba khouse.

Chapter 7 The Towers of Hanoi This hapter is about the Towers of Hanoi problem. and is often used in omputing s ien e and arti. The problem is dis ussed in many mathemati al texts.

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 .

A better solution is obtained by observing an invariant of the indu tive solution. 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. However. In this way.rst solution. under the pseudonym M. but also the properties of logi al equivalen e. the Towers of Hanoi problem is one that is not solved in one go.1 7. For this problem. this hapter brings together a number of the te hniques dis ussed earlier: prin ipally indu tion and invariants. we begin with the solution of the problem.1. this solution is undesirable. 7.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. Claus. several steps are needed before a satisfa tory solution is found. 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 . One reason for doing so is to make lear where we are headed.

xed in the ground. On the .

God pla ed sixty-four golden disks. (See . at the time of the world's reation. ea h of dierent size. in de reasing order of size.rst of these poles.

from one pole to another a ording to the rule that no disk may ever be above a smaller disk. 7.1.) The Brahmin monks were given the task of moving the disks. one per day. 2008 .g. 111 May 28. The monks' task will be omplete when they Algorithmi Problem Solving Roland Ba khouse.

The Towers of Hanoi have su eeded in moving all the disks from the .112 7.

On every alternate day. the world will ome to an end! Figure 7. On day 0 . With these assumptions. we assume that the poles are arranged at the three orners of a triangle.rst of the poles to the se ond and. To formulate the solution. the solution is the following. Movements of the disks an then be su in tly des ribed as either lo kwise or anti lo kwise movements.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. We assume that the problem is to move all the disks from one pole to the next in a lo kwise dire tion. beginning on the . on the day that they omplete their task. We also assume that days are numbered from 0 onwards. the disks are pla ed in their initial position and the monks begin moving the disks on day 1 .1: Towers of Hanoi Problem 7.1.

If the total number of disks is odd. Algorithmi Problem Solving Roland Ba khouse. The dire tion of rotation depends on the total number of disks. Otherwise. The rule for moving the smallest disk is that it should y le around the poles. the smallest disk is moved. the smallest disk should y le in a lo kwise dire tion.rst day. May 28. it should y le in an anti lo kwise dire tion. 2008 .

that is. a 4 -disk puzzle. The algorithm terminates when no further moves are possible. 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. 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. on an evennumbered day when all the disks are on one-and-the-same pole. you will . a disk other than the smallest disk is moved | subje t to the rule that no disk may ever be above a smaller disk. If you do. say.2.113 7. Try exe uting this algorithm yourself on. Indu tive Solution On every other day.

1. and so on.nd that the algorithm works. you an exe ute the algorithm on larger and larger problems | 5 -disk. provides no help whatsoever in understanding how the solution is onstru ted. Matters would be made even worse if we now pro eeded to give a formal mathemati al veri. it only serves to impress |look at how lever I am!| but in a reprehensible way.3 WHY? Presenting the problem and its solution. 6 -disk. Depending on how mu h patien e you have. like this. 7. If anything.

ation of the orre tness of the algorithm. we . This is not how we intend to pro eed! Instead.

Suppose that the task is to move M disks from one spe i. 7.2 Inductive Solution Constru ting a solution by indu tion on the number of disks is an obvious strategy.rst present an indu tive solution of the problem. Then. we show how to derive the algorithm above from the indu tive solution. by observing a number of invariants. Let us begin with an attempt at a simple-minded indu tive solution.

pole to another spe i.

After doing this. Algorithmi Problem Solving Roland Ba khouse. the basis is easy. Here.) As often happens. For the indu tive step. we see that naming the poles is inadvisable. 2008 . May 28. and the problem is to show how to move n+1 disks from A to B. and the third pole C. Let us all these poles A and B. we soon get stu k! There is only a ouple of ways that the indu tion hypothesis an be used. and we have no hypothesis about moving disks from this pole. we have exhausted all possibilities of using the indu tion hypothesis be ause n disks are now on pole B. (Later. When the number of disks is 0 no steps are needed to omplete the task. but these lead nowhere: 1. pole. Move the top n disks from A to B. we assume that we an move n disks from A to B.

On e again. and we have no hypothesis about moving disks from this pole. Move the smallest disk from A to C. we have exhausted all possibilities of using the indu tion hypothesis. The Towers of Hanoi 2. be ause n disks are now on pole B.114 7. The mistake we have made is to be too spe i. Then move the remaining n disks from A to B.

about the indu tion hypothesis. The way out is to generalise by introdu ing one or more parameters to model the start and .

we make a ru ial de ision. we observe that the problem exhibits a rotational symmetry. as we did in . The rotational symmetry is obvious when the poles are pla ed at the orners of an equilateral triangle. B and C. At this point. Rather than name the poles (A. say).nish positions of the disks.

namely. as is often done.g. we need only say whether it is to be moved lo kwise or anti lo kwise from its urrent position.1. 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 . Also. where d is either lo kwise or anti lo kwise. The importan e of this observation is that only one additional parameter needs to be introdu ed. The alternative of naming the poles leads to the introdu tion of two additional parameters. That is. the start and . (This rotational symmetry is obs ured by pla ing the poles in a line. in order to spe ify how a parti ular disk is to be moved. the dire tion of movement.) The problem does not hange when we rotate the poles and disks about the entre of the triangle. 7.

It is not suÆ ient to simply take the problem spe i. Now. we an return to the indu tive solution again.nish positions of the disks. This is mu h more ompli ated sin e it involves unne essary additional detail. We need to take are in formulating the indu tion hypothesis.

This is be ause the problem spe i. ation as indu tion hypothesis.

We must show how to move n+1 disks from one pole to its neighbour in dire tion d . We need a stronger indu tion hypothesis. we assume that the disks are numbered from 1 upwards. where d is either lo kwise or anti lo kwise. with the smallest disk being given number 1 . The indu tion hypothesis we use is that it is possible to move the n smallest disks. Algorithmi Problem Solving Roland Ba khouse. the requirement that a larger disk may not be pla ed on top of a smaller disk may be violated. 2008 . it is ne essary to move n disks in the presen e of M−n other disks. ation assumes that there are exa tly M disks that are to be moved. but no disk is on top of a disk smaller than itself). If some of these M−n disks are smaller than the n disks being moved. May 28. 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. beginning from any valid starting position (that is. For onvenien e. from one pole to its neighbour in the dire tion d . a starting position in whi h the disks are distributed arbitrarily over the poles. When using indu tion.

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. However. (This a tion may pla e disk n+1 on top of another disk.2. We an begin by moving the n smallest disks in the dire tion d . and all n+1 smallest disks have now been moved from their original position to the neighbouring pole in dire tion d .) Finally. This pla es them above disk n+1 . 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 . Indu tive Solution Given the goal of exploiting the indu tion hypothesis. there is little hoi e of what to do. Then disk n+1 an be moved in the dire tion d. The ode de.115 7. The following ode summarises this indu tive solution to the problem. we use the indu tion hypothesis again to move the n smallest disks in the dire tion ¬d . or in the dire tion ¬d .

The semi olon operator on atenates sequen es together.d pres ribes how to move the n smallest disks one-by-one from one pole to its neighbour in the dire tion d . d ′ i where n is the number of disks. following the rule of never pla ing a larger disk on top of a smaller disk. meaning lo kwise and anti lo kwise.d := 1. n. 2008 .) H2. di] .nes Hn. For example. Taking the pairs in order from left to right.d to be a sequen e of pairs hk .d . The pair hk . but unrestri ted use of re ursion an be unreliable. 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.cw } Algorithmi Problem Solving Roland Ba khouse. [ ] denotes an empty sequen e and [x] is a sequen e with exa tly one element x . May 28.d = [ ] Hn+1. rather than true and false in order to improve readability. H0. d ′ i means move the disk numbered k from its urrent position in the dire tion d ′ .d = Hn . Dire tions are boolean values. here is how we determine H2. k is a disk number and d and d ′ are dire tions. The form of re ursion used here is limited. the omplete sequen e Hn. (We use cw and aw .cw . true representing a lo kwise movement and false an anti- lo kwise movement. Re ursion is a very powerful problem-solving te hnique. ¬d Note that the pro edure name H re urs on the right side of the equation for Hn+1. des ribing the solution as an \indu tive" solution makes lear the limitation on the use of re ursion. Hn . [hn+1 . Be ause of this we have what is alled a re ursive solution to the problem. ¬d . disk 1 being the smallest.cw = { 2nd equation. Disks are numbered from 1 onwards.

[ ] . The implementation of the indu tive solution.aw } H0. H0.1.awi] .aw = { 2nd equation. [ ] = { on atenation of sequen es } [h1.cw . and so on.1 is an iterative solution to the problem.cw . H1.d := 0. As an exer ise you should determine H3.awi] . you will qui kly dis over that this indu tive solution to the problem takes a lot of eort to put into pra ti e.e. H0. The omplete expansion of the equations in the ase of n = 3 takes 16 steps. [h1.aw in the same way. H0.cwi .d denote the length of the sequen e Hn. it is a solution that involves iteratively (i. on the other hand.awi] . [ ] . [h1. If you do.cwi] .cw = { 1st equation } [ ] . Let Tn.awi] . [h2. h1.cwi] . 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.awi . [h2. in the ase of n = 4 takes 32 steps.cw .aw .d . Derive an indu tive de. h2. That is.116 7.awi] . involves maintaining a sta k of the sequen e of moves yet to be exe uted. The Towers of Hanoi H1. n.cw .cwi] . [h1. This is not the easy solution that the Bramin monks are using! The solution given in se tion 7. [h1. [h2. repeatedly) exe uting a simple pro edure dependent only on the urrent state.

nition of T from the indu tive de.

(You should .nition of H .

) Use this de.d is independent of d .nd that Tn.

2008 . T1 and T2 . Use indu tion to show how to onstru t a state-transition diagram that shows all possible states of n disks on the poles. Hen e.1 2 Use indu tion to derive a formula for the number of dierent states in the Towers of Hanoi problem. Use the onstru tion to show that the above solution optimises the number of times that disks are moved.2 2 Algorithmi Problem Solving Roland Ba khouse. Exercise 7. and the allowed moves between states. May 28. or otherwise. Exercise 7. Prove your onje ture by indu tion on n .nition to evaluate To . formulate a onje ture expressing Tn as an arithmeti fun tion of n .

The Iterative Solution 7.3.1.2. It has two main elements: the .3 The Iterative Solution Re all the iterative solution to the problem.117 7. presented in se tion 7.

the se ond is that the disk to be moved alternates between the smallest disk and some other disk. the value of even. we show that the smallest disk always y les around the poles.d the boolean value even. This formula allows us to determine the dire tion of movement d of disk k . Spe i. The key is that. for all pairs hk .k ≡ d ′ is invariant (that is always true or always false). the parameter \ n+1 " is repla ed by \ n " and \ d " is repla ed by \ ¬d ". its dire tion of movement is invariantly lo kwise or invariantly anti lo kwise).(n+1) ≡ d remains onstant under this assignment. d ′ i in the sequen e Hn+1. and we al ulate the dire tion of movement of ea h. for all moves hk . Whether even. In this se tion.d ) will depend on the initial values of n and d .d is applied. we do more than this. Cyclic Movement of the Disks In this se tion. Then.k ≡ d ≡ even. This is a simple onsequen e of the rule of ontraposition dis ussed in se tion 5. When the formula for Hn+1. d ′ i in the sequen e Hn+1. Sin e even. we show how these properties are derived from the indu tive solution.rst is that the smallest disk y les around the poles (that is. Let us suppose these are N and D .(n+1) ≡ ¬(even. In fa t.5. We show that all the disks y le around the poles.N ≡ D .n) . di . we have even.k ≡ d ′ is true or false (for all pairs hk .

and all odd-numbered 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. At the time of his dis overy. so that the disks were arranged on all three poles. all evennumbered disks should y le in a lo kwise dire tion. However. all even-numbered disks should y le in an anti lo kwise dire tion. and the same dire tion as D if N is odd. the smallest disk (whi h is odd-numbered) should y le in a dire tion opposite to D if N is even. In parti ular. 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. Vi e-versa. the monks had got some way to ompleting their task. if it is required to move an odd number of disks in a lo kwise dire tion. ally. on the day of his arrival he was able to observe the monks move the smallest disk from the middle pole Exercise 7. May 28.3 Algorithmi Problem Solving Roland Ba khouse. 2008 . An explorer on e dis overed the Bramin temple and was able to se retly observe the monks performing their task. if it is required to move an even number of disks in a lo kwise dire tion.

118 7. We now want to give a formal proof that the sequen e Hn. On the next day. The Towers of Hanoi to the rightmost pole. And. two onse utive moves of a disk other than the smallest have no ee t on the state of the puzzle.d satis. he saw the monks move a disk from the middle pole to the leftmost pole. two onse utive moves of the smallest disk are wasteful as they an always be ombined into one. By examining the puzzle itself. namely that the disk that is moved alternates between the smallest disk and some other disk. Did the disk being moved have an even number or an odd number? 2 Alternate Disks We now turn to the se ond major element of the solution. After all. it is not diÆ ult to see that this must be the ase.

Let us all a sequen e of numbers alternating if it has two properties.es this property. The .

whi h we denote by diskn. The sequen e of disks moved on su essive days.d .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. is obtained by taking the .

d and ignoring the se ond.rst omponent of ea h of the pairs in Hn. Then. from the de.d . Let the sequen e that is obtained in this way be denoted by diskn.

alt.d = diskn . The proof is then: alt.d) = { de.nition of H we get: disk0.(diskn. for a sequen e ks and number k . ks) ⇐ alt. [k] . The base ase.(diskn+1. ¬d . is learly true be ause an empty sequen e has no onse utive elements. For the indu tion step. n = 0 . the property of alternating sequen es on whi h the proof depends is that. Our goal is to prove alt.d = [ ] diskn+1. diskn . [n+1] .ks ∧ ((ks = [ ]) ≡ (k = 1)) .d) . The proof is by indu tion on n . ¬d .(ks .

(diskn . ¬d) ⇐ { above property of alternating sequen es Algorithmi Problem Solving Roland Ba khouse. diskn . 2008 .nition } alt . ¬d . } May 28. [n+1] .

4. ¬d) ∧ ((diskn .(diskn . ¬d = [ ]) ≡ (n+1 = 1)) = { indu tion hypothesis applied to the .119 7. Summary alt.

The explorer left the area and did not return until several years later. On his return. straightforward property of diskn for the se ond. It transpired that one of the monks had made a mistake shortly after the explorer's . he dis overed the monks in a state of great despair.rst onjun t. } true .

The hapter began Algorithmi Problem Solving Roland Ba khouse.4 even. they had got into a never-ending loop! Fortunately. Exercise 7.rst visit but it had taken the intervening time before they had dis overed the mistake. maintaining invariant the relationship Exercise 7. 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. May 28. and all the blue disks are on the third pole. Make use of the fa t that. They would then be able to begin their task afresh.5 (Coloured Disks) 2 7. 2008 . initially.) 2 Suppose ea h disk is oloured. Devise an algorithm that will sort the disks so that all the red disks are on one pole. beginning in a state in whi h n disks are all on the same pole. The state of the disks was still valid but the monks had dis overed that they were no longer making progress towards their goal. You may assume that.n ≡ d ≡ even.4 Summary In this hapter we have seen how to use indu tion to onstru t a solution to the Towers of Hanoi problem. (Hint: The disk being moved will still alternate between the smallest and some other disk. dierent disks may be oloured dierently. Be ause of the monks' mistake this will not be onstant. red white or blue. Several indu tive onstru tions have been dis ussed. You only have to de ide in whi h dire tion the smallest disk should be moved.k ≡ d ′ for the dire tion d ′ moved by disk k will move n disks in the dire tion d . The olouring of disks is random. What was the algorithm the explorer gave to the monks? Say why the algorithm is orre t. all the white disks are on another pole. all disks are on one pole.

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. 2008 . 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.5 Bibliographic Remarks Information on the history of the Towers of Hanoi problem is taken from [Ste97℄. May 28. 7. The formulation and proof presented here is based on [BF01℄. 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. This solution was then tranformed to an iterative solution.120 7. The graph was used to justify an indu tive solution to the problem itself. Algorithmi Problem Solving Roland Ba khouse. A proof of the orre tness of the iterative solution was published in [BL80℄.

The generalisation is to onsider an arbitrary number of people. Spe i.Chapter 8 The Torch Problem In this hapter. the task is to get all the people a ross a bridge in the optimal time. we present a solution to a more general version of the tor h problem in exer ise 3.4.

the problem we dis uss is the following.j whenever i < j . we an always onsider pairs (t.i . where i ranges over people. where i < j . we assume that t.i < t. For simpli ity. ordered lexi ographi ally. parti ularly in omparison to the . If the given times are su h that t. we obtain a total ordering on times with the desired property. Assuming that the rossing times are distin t makes the arguments simpler. N people wish to ross a bridge. Renaming the rossing \times" to be su h pairs. Person i takes time t.1 Lower and Upper Bounds The derivation that follows is quite long and surprisingly diÆ ult. The bridge is narrow and at most 2 people an be on it at any one time. (This means that we assume the people are ordered a ording to rossing time and that their rossing times are distin t. but is not essential. ally. when two ross together they must pro eed at the speed of the slowest. i) .i = t.) 8.i to ross the bridge. The people are numbered from 1 thru N.j for some i and j . 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. Constru t an algorithm that will get all N people a ross in the shortest time.

whi h is quite simple. This has to do with the dieren e between establishing an \upper bound" and a \lower bound" on the rossing times.nal algorithm. It's important to appre iate where pre isely the diÆ ulties lie. 121 May 28. 2008 . Algorithmi Problem Solving Roland Ba khouse.

In other words. The Tor h Problem In the original problem given in hapter 1 . We an use the same instan e of the tor h problem to further illustrate the dieren e between lower and upper bounds. A mu h harder problem is to show that 17 minutes is a lower bound on the time taken. the question asked was to show that all four an ross the bridge within 17 minutes. an upper bound is established by exhibiting a sequen e of rossings that takes the required time. the question asks for a so- alled upper bound on the time taken. Most of us. 2 minutes. 5 minutes and 10 minutes. when onfronted with the tor h problem above. there are four people with rossing times of 1 minute. will .122 8. Cru ially. Showing that it is a lower bound means showing that the time an never be bettered. In general.

that the optimal solution is to let one person a ompany all the others one-by-one a ross the bridge. 8. The more eÆ ient algorithm assumes a knowledge of algorithm development that goes beyond the material in this book. It may be. The greatest eort goes into showing that the algorithm simultaneously establishes a lower bound on the rossing time.) Similarly. by exhibiting the rossing sequen e that gets all four people a ross in 17 minutes does not prove that this time annot be bettered.e. (Indeed. ea h time returning with the tor h for the next person. The way to avoid unne essary detail is to fo us on what we all the \forward trips". The algorithm we derive is quite simple but. it is not. Su h a solution takes a total time of 2+1+5+1+10 . i. on its own. it only establishes an upper bound on the optimal rossing time. we present two algorithms for onstru ting an optimal sequen e. then it is extremely inee tive. for example. By exhibiting the rossing sequen e. If our solution method requires that we detail in what order the people ross.rst explore the solution in whi h the fastest person a ompanies the others a ross the bridge. In this hapter. we have not established that it is a lower bound. the goal is to onstru t an algorithm for s heduling N people to ross the bridge. Algorithmi Problem Solving Roland Ba khouse. The ombination of equal lower and upper bounds is alled an exa t bound. whi h is a very large number even for quite small values of N . we have established that 19 minutes is an upper bound on the rossing time. In se tion 8. 2008 .6. Doing so is mu h harder than just onstru ting the sequen e. The number of dierent orderings is (N−1)! . 19 minutes. The problem asks for a sequen e of rossings but there is an enormous amount of freedom in the order in whi h rossings are s heduled.2 Outline Strategy On e again. the main issue we have to over ome is the avoidan e of unne essary detail. this is what is meant by an optimal solution. May 28.

the tor h must always be arried. if we an ompute the optimal olle tion of forward trips. and a return trip is a rossing in the opposite dire tion.123 8. we mean a \bag" of sets of people. The idea is that. First. the return trips needed to sequen e them orre tly an be easily dedu ed. In order to turn this idea into an ee tive solution. we need to pro eed more formally.2. Informally. when rossing the bridge. by the \ olle tion" of forward trips. This means that rossings alternate between \forward"and \return" trips. whereas a set is de. where a forward trip is a rossing in the desired dire tion. The mathemati al notion of a \bag" (or \multiset" as it is sometimes alled) is similar to a set but. the forward trips do the work whilst the return trips servi e the forward trips. Outline Strategy Re all that.

a bag is de.ned solely by whether or not a value is an element of the set.

For example. a bag of oloured marbles would be spe i.ned by the number of times ea h value o urs in the set.

b s and c s in whi h a o urs on e.ed by saying how many red marbles are in the bag. For brevity. for example). b o urs twi e and c o urs no times. how many blue marbles. and so on. for example. 2∗b . we also write {1∗a . 2∗b . Even though when we write down an expression denoting a bag we are for ed to list the elements in a ertain order (alphabeti al order in {1∗a . the order has no signi. It is important to stress that a bag is dierent from a sequen e. We will write. 2∗b} to denote the same bag. 0∗c} to denote a bag of a s. 0∗c} . {1∗a .

If we are obliged to distinguish between forward and return trips. for example. The expressions {1∗a . A trip is given by the set of people involved in the trip. 2∗b .3} is a trip in whi h persons 1 and 3 ross. {1. 0∗c} and {2∗b . 0∗c} both denote the same bag. we pre. So. an e. 1∗a .

As we said above.x the trip with either \ + " (for forward) or \ − " (for return).3} denotes a forward trip made by persons 1 and 3 and −{2} denotes a return trip made by person 2 . We all a sequen e of trips that gets everyone a ross in a ordan e with the rules a valid sequen e. our fo us will be on omputing the bag of forward trips in an optimal sequen e of trips. We begin by establishing a number of properties of sequen es of trips that allow us to do this. We will say that one valid sequen e subsumes another valid sequen e if the time taken by the . So +{1.

The problem is to . Note that the subsumes relation is re exive (every valid sequen e subsumes itself) and transitive (if valid sequen e a subsumes valid sequen e b and valid sequen e b subsumes valid sequen e c then valid sequen e a subsumes valid sequen e c ).rst is at most the time taken for the se ond.

2008 . Formally. Algorithmi Problem Solving Roland Ba khouse.nd a valid sequen e that subsumes all valid sequen es. a valid sequen e is a set of numbered trips with the following two properties: The trips are sets. May 28. ea h set has one or two elements. and the number given to a trip is its position in the sequen e (where numbering begins from 1 ).

(A trip T is made by person i if i∈T . The Tor h Problem Odd-numbered trips in the sequen e are alled forward trips. beginning and ending with forward trips. even-numbered trips are alled return trips. The trips made by ea h individual person alternate between forward and return trips.) Immediate onsequen es of this de.124 8. The length of the sequen e is odd.

nition whi h play a ru ial role in .

A regular sequen e is a valid sequen e that onsists entirely of regular forward and return trips.nding an optimal sequen e are: The number of forward trips is one more than the number of return trips. and a regular return trip means a return trip made by exa tly one person. The number of forward trips made by ea h individual person is one more than the number of return trips made by that person. A regular forward trip means a forward trip made by two people. The .

1) is to show that every valid sequen e is subsumed by one in whi h all trips are regular.rst step (lemma 8. The signi.

) Most importantly. (Knowing the bag of forward trips. not even the order in whi h they are made needs to be known. we an thus repla e the problem of . This is be ause ea h person makes one fewer return trips than forward trips. The number of forward trips is N−1 and the number of return trips is N−2 . an e of this is threefold. knowing just the bag of forward trips in a regular sequen e is suÆ ient to re onstru t a valid regular sequen e. (Re all that N is the number of people. In this way.) The time taken by a regular sequen e an be evaluated knowing only whi h forward trips are made. the time taken for the return trips an be al ulated. it is easy to determine how many times ea h person makes a return trip. Sin e all su h sequen es take the same total time.

nding an optimal sequen e of forward and return trips by the problem of .

We prove the obvious property that. 2008 . May 28. We an then use indu tion to determine the omplete solution. the two slowest people do not return. Algorithmi Problem Solving Roland Ba khouse.nding an optimal bag of forward trips Finding an optimal bag of forward trips is then a hieved by fo using on whi h people do not make a return trip. in an optimal solution.

Every valid sequen e ontaining irregular trips is subsumed by a stri tly faster valid sequen e without irregular trips. Regular Sequen es 8.125 8. Lemma 8.1 Proof Suppose a given valid sequen e ontains irregular trips.3.3 Regular Sequences Re all that a \regular" sequen e is a sequen e in whi h ea h forward trip involves two people and ea h return trip involves one person. We an always restri t attention to regular sequen es be ause of the following lemma. We onsider two ases: the .

rst irregular trip is forward and the .

rst irregular trip is ba kward. If the .

q} v −{p.rst irregular trip is ba kward. p say. u . (Note that the forward trip made by p involves two people be ause it is assumed that the . and remove p from both trips. More formally. hoose an arbitrary person. suppose the sequen e has the form u +{p. Identify the forward trip made by p prior to the ba kward trip. v and w are subsequen es and p o urs nowhere in v . making the trip.r} w where q and r are people.

) The number of irregular rossings is not redu ed. Now suppose the . The time taken is no greater sin e. and it is true for p be ause the trips made by p have hanged by the removal of onse utive forward and return trips. we have to he k that the trips made by ea h individual ontinue to alternate between forward and return. This is true for individuals other than p be ause the points at whi h they ross remain un hanged. for any x and y . but the total number of person-trips is redu ed. sin e a new irregular forward trip has been introdu ed. t.) Repla e the sequen e by u +{q} v −{r} w This results in a valid sequen e.rst irregular trip is ba kward.p ↑ y ≥ x+y . (To he k that the sequen e remains valid.p ↑ x + t. the time for whi h is no greater than the original sequen e.

rst irregular trip is forward. There are two ases to onsider: the irregular trip is the very .

rst in the sequen e. and it is not the very .

rst. If the .

Clearly. Also. If the . the total time taken is redu ed. sin e times are positive.) These two rossings an be removed. the number of person-trips is redu ed. it means that one person rosses and then immediately returns. (We assume that N is at least 2 .rst trip in the sequen e is not regular.

rst irregular rossing is a forward trip but not the very .

2008 .rst. let us suppose it is person p who rosses. and suppose q is the person who returns immediately before Algorithmi Problem Solving Roland Ba khouse. May 28.

126 8. The Tor h Problem this forward trip. (There is only one su h person be ause of the assumption that p 's forward trip is the .

r} w −{q} +{p} v (where w does not involve p or q ) to u +{p. transform u +{q.p > t. (This is be ause we assume that p ′ s forward trip is the . it must involve q and not p . the total rossing time is redu ed (sin e.r} w v .p ↑ x ). There are two ases: it is a forward trip or it is a return trip. Moreover. That is. for any x . and the number of person-trips is also redu ed. Swap p with q in this trip and remove q 's return trip and p 's irregular rossing.q ↑ x + t. The result is a valid sequen e. If it is a return trip.q + t.rst irregular trip. suppose the sequen e has the form u −{q} +{p} v Consider the latest rossing that pre edes q ′ s return trip and involves p or q. it is made by one person only. t. If it is a forward trip.) That is.

That is.) That person must be p .rst irregular trip in the sequen e.p + t. and the number of person-trips is also redu ed. the total rossing time is redu ed (sin e. transform u −{p} w −{q} +{p} v (where w does not involve p or q ) to u −{q} w v . t. 2 8. Swap p with q in this return trip. The result is a valid sequen e.p > t.q + t. subsumes the given valid sequen e and has a smaller person-trip ount. We have now des ribed how to transform a valid sequen e that has at least one irregular rossing. Moreover. Repeating this pro ess whilst there are still irregular rossings is therefore guaranteed to terminate with a valid sequen e that is regular. and remove q 's return trip and p 's irregular rossing.4 Sequencing Forward Trips Lemma 8. the transformation has the ee t of stri tly de reasing the total time taken.1 has three signi.q ).

May 28. it means that the number of forward trips in an optimal sequen e is N−1 and the number of return trips is N−2 . This is be ause every subsequen e omprising a forward trip followed by a return trip in reases Algorithmi Problem Solving Roland Ba khouse. ant orollaries. First. 2008 .

127 8. after the . Sequen ing Forward Trips the number of people that have rossed by one.4. and the last trip in reases the number by two. Thus.

1∗{7.2} means that persons 1 and 2 make 3 forward trips together. This is a non-obvious property of the forward trips and to prove that it is indeed the ase we need to make some ru ial observations. whi h we identify with numbers in the range 1 thru N . 7 and 8 ) all make 1 forward trip and. so. 1∗{1. 6 .3↑t.1 is that. after 2×(N−2) + 1 trips everyone has rossed. similarly. The number of times a person o urs is the number of forward trips made by that person. This is be ause the bag of forward trips enables us to determine how many times ea h individual makes a forward trip. given just the bag of forward trips orresponding to a regular sequen e. suppose the forward trips in a regular sequen e are as follows: 3∗{1. The remaining people ( 4 . it is possible to onstru t a regular sequen e to get everyone a ross with the same olle tion of forward trips. it means that the total time taken to omplete any regular sequen e an be evaluated if only the bag of forward trips in the sequen e is known. not even the order in whi h the trips are made is needed.6) + 1 × (t. N−2 people have rossed and 2 have not. person 2 makes 3 forward trips and hen e 2 return trips. 1∗{3. re all that 3∗{1. 1∗{3. The elements of the sets in F are people. and hen e 3 return trips.8) + 3 × t. The total time taken is thus: 3 × (t.2} . Division of people into these two Algorithmi Problem Solving Roland Ba khouse. et . whilst person 3 makes 2 forward trips and hen e 1 return trip.7↑t. and the bottom line gives the time taken by the return trips. ea h with exa tly two elements and ea h having a ertain multipli ity.1↑t. 5 . For example.5} . 1∗{1. The third important orollary of lemma 8. and ea h number in the range must o ur at least on e. Hen e. Note that no indi ation is given of the order in whi h the forward trips o ur in the sequen e.8} (The trips are seperated by ommas. person 1 makes 4 forward trips.) Then. hen e.6} .2) + 1 × (t. We will all a person a settler if they make only one forward trip. no return trips.5) + 1 × (t.3↑t. May 28. That is.1↑t.2 + 1 × t. ounting the number of o urren es of ea h person in the bag. 2008 . and the total number of return trips is 6 . Se ond. from whi h the total time for the return trips an be omputed.rst 2×(N−2) trips. we all a person a nomad if they make more than one forward trip.3 (The top line gives the time taken by the forward trips.) Note that the total number of forward trips is 7 (one less than the number of people).6} means that persons 1 and 6 make one forward trip together.4} .4) + 1 × (t.1 + 2 × t. F is a olle tion of sets. Suppose F is a bag of forward trips orresponding to some regular sequen e. the number of times ea h individual returns an be omputed.

128 8. If both people in a trip are settlers we say the trip is hard . The Tor h Problem types auses the trips in F to be divided into three types depending on the number of settlers in the trip. if one of the people is a settler and the other a nomad. we say the trip is .

rm . .

the number of .nally. the number of hard trips. #firm and #soft to denote the number of nomads. if both people are nomads we say the trip is soft. #hard . Now. suppose we use #nomad .

in the olle tion F . respe tively. sin e ea h nomad makes one more forward trip than return trip. Sin e ea h soft trip ontributes 2 to the number of forward trips made by nomads. The number of return trips equals the total number of forward trips made by individual nomads less the number of nomads. Then. and ea h . the number of trips in F is #hard + #firm + #soft .rm trips and the number of soft trips.

Equivalently.rm trip ontributes 1 . But the number of forward trips is one more than the number of return trips. That is. the number of return trips is thus 2 × #soft + #firm − #nomad . #hard + #firm + #soft = 2 × #soft + #firm − #nomad + 1 . #hard + #nomad = #soft + 1 . We summarise these properties in the following de.

F and #nomad.nition.F denote. respe tively. then (8. every person is an element of at least one trip in F . ea h trip in F involves two people.F + 1 .F + #nomad. the number of trips in F that involve two settlers. the number of trips in F that involve no settlers. Definition 8.2 (Regular Bag) {i | 1 ≤ i ≤ N} is alled a regular N Suppose N is at least 2 .) h∀i : 1 ≤ i ≤ N : h∃T : T ∈F : i∈T ii (Informally. 2008 . #soft. May 28. A bag of subsets of bag if it has the following properties: Ea h element of F has size two.F = #soft.) If #hard. and the number of nomads in F .3) 2 #hard. (Informally. Algorithmi Problem Solving Roland Ba khouse. Formally. what we have proved is the following.F .

Then.i × rF.ii where (8.F .F = 0 ⇒ #hard. Moreover.4.6) rF.i is the number of times that person i returns. by de.F = 1 ∧ #firm.ii × #FT i + hΣi :: t. If #nomad.5) hΣT : T ∈F : h⇑i : i∈T : t.F = 0 = #soft. Sequen ing Forward Trips 129 Lemma 8.7) #nomad. the bag of forward trips F that is obtained from the sequen e by forgetting the numbering of the trips is a regular N bag. there are no nomads and hen e.) 2 Immediate onsequen es of (8. the time taken is (8. Let #FT denote the multipli ity of T in F.8. ( rF.4 Given a valid regular sequen e of trips to get N people a ross.F is zero.i = hΣT : T ∈F ∧ i∈T : #FT i − 1 .3) are: (8. the time taken by the sequen e of trips an be evaluated from F . where N is at least 2 .

no soft or .nition.

The onverse is immediate from (8.F ≥ 1 ⇒ #soft. It is the ase that #nomad. If there is only one nomad. a valid regular sequen e of trips an be onstru ted from F to get N people a ross. It follows from (8.F is at least 1 . Now we an show how to onstru t a regular sequen e from F . not all an ross at on e and. The easiest ase is when F onsists of exa tly one trip (with multipli ity one).F = 0 = #soft.3) that #hard.F . G iven N (at least 2 ) and a regular N bag.9) N > 2 ∧ #hard. by (8.F is at least 1 .5).F ≥ 1 .F = 1 .8) #nomad.3) that #soft. That is #soft. #nomad. We need to onsider three ases.F is zero.3). The time taken by the sequen e is given by (8. The sequen e is then just this one trip. (8.F is 1 . The se ond ase is also easy. It follows from (8.F also equals zero. (8.F = 1 ≡ #hard. every trip in F is . In this ase. So. #hard. there are no trips involving two nomads.rm trips. so.3). Lemma 8. If N (the number of people) is greater than 2 .10 Proof The proof is by indu tion on the size of the bag F .

2008 .rm. That is. May 28. The sequen e is simply obtained by listing all the elements of F in an arbitrary Algorithmi Problem Solving Roland Ba khouse.s} where n is the nomad and s is a settler. ea h trip has the form {n.

The Tor h Problem order and inserting a return trip by the nomad n in between ea h pair of forward trips of whi h the .130 8.

#soft. It follows.F is at least 1 .9). In this ase. by (8.s} for some s .rst is the trip {n.F is non-zero and F has more than one trip. by de. The third ase is that #hard.

2 Optimisation Problem Lemmas 8. there is a regular sequen e orresponding to F ′ whi h gets the remaining people a ross.m} where n and m are both nomads.4 and 8. (That is. Choose any soft trip in F .nition of soft that #nomad. Redu e the multipli ity of the hosen hard trip and the hosen soft trip in F by 1 .) We get a new bag F ′ in whi h the number of trips made by ea h of n and m has been redu ed by 1 and the number of people has been redu ed by 2 .10 have a signi.m} and is followed by the return of n . remove one o urren e of ea h from F . Suppose it is {n. By indu tion.F is at least 2 . then an arbitrary hard trip and then the return of m . Constru t the sequen e whi h begins with the trip {n.

we seek a regular bag as de. Instead of seeking a sequen e of rossings of optimal duration. ant impa t on how to solve the general ase of the bridge problem.

ned in de.

5).2 that optimises the time given by (8. In solving this problem. The value of the . it is useful to introdu e some terminology when dis ussing the time taken as given by (8.5).nition 8. It is this problem that we now solve. There are two summands in this formula.

we all h⇑i : i∈T : t.i the return time of person i (or person i 's return time). 8.rst summand we all F 's total forward time and the value of the se ond summand F 's total return time. Given a bag F and a trip T in F . More spe i.5 Choosing Settlers and Nomads This se tion is about how to hoose settlers and nomads. We establish that the settlers are the slowest people and the nomads are the fastest.) For ea h person i .i × rF.ii × #FT the forward time of T in F . (Sometimes \in F " is omitted if this is lear from the ontext. we all the value of t.

q) an inversion if. within F . Call a pair of people (p. ally. additionally. Every regular bag is subsumed by a regular bag for whi h all settlers are slower than all nomads. Lemma 8. q is a nomad and p is faster than q . person 2 may also be a nomad. 2008 . Proof Algorithmi Problem Solving Roland Ba khouse. we establish that in an optimal solution there are at most 2 nomads. May 28. p is a settler. Person 1 is always a nomad if N is greater than 2 .11 Suppose the regular N bag F is given.

Moreover. The forward times for the trips not involving p or q are. Choosing Settlers and Nomads Choose any inversion (p.p .q − t. un hanged. Suppose q and p are inter hanged everywhere in F .q ).p . the return time is learly redu ed by at least t. We get a regular N bag. This is veri.131 8. of ourse.q − t.p < t. The forward time for the trips originally involving q are not in reased (be ause t.5. The forward time for the one trip originally involving p is in reased by an amount that is at most t. q) .

ed by onsidering two ases. The .

it suÆ es to observe that t. the number of inversions is de reased by at least one. In this ase. arithmeti } t. Finally.r} where r 6= q . monotoni ity of max } t.p) = { distributivity of sum over max. swapping p and q has no ee t on the trip. The net ee t is that the total time taken does not in rease.r .q − t.q ↑ (t. a bag F is obtained that subsumes the given bag. Also.p ≤ t.r + (t. the transformed bag subsumes the original bag. the times for all other forward trips are un hanged. by repeating the pro ess of identifying and eliminating inversions.p ↑ t. In this ase.p)) ≥ { t.rst ase is when the trip involving p is {p.q .q} . Thus.q ↑ t.q − t. the trip involving p is {p. That is.12 In any . In the se ond ase.r + (t. and the in rease in time taken is 0 . 2 Every regular N bag is subsumed by a regular N bag F with the following properties: Corollary 8.

the nomad is person 1 . (Note that this is the empty set when j equals 1 .) The multipli ity of {1. we may assume that the nomads are slower than the settlers. Every soft trip in F is {1.rm trip in F .2} . and the hard trips are {k: 0 ≤ k < j−1: {N − 2×k . (Note: the multipli ity of this trip in the bag an be an arbitrary number.11. From 8. in luding 0 .) Suppose F is a regular N bag that optimises the total travel time. Suppose there is a .2} in F is j . N − 2×k − 1}} . for some j where 1 ≤ j ≤ N÷2 .

This has no ee t on the forward Proof Algorithmi Problem Solving Roland Ba khouse. 2008 . Repla e i in one o urren e of the trip by person 1 . May 28.rm trip in F in whi h the nomad is person i where i is not 1 .

The Tor h Problem time. in the ase of person 1 .132 8. property (8. whi h ontradi ts F being optimal.3). The number of trips made by i de reases by one. in reases. We laim that this results in a regular N bag.1 ). So it remains to he k property (8. the total return time is redu ed (by t. If person i is still a nomad after the repla ement. but remains positive be ause i is a nomad in F .3) is maintained be ause the type (hard.4 for the properties required of a regular bag.i − t. (Please refer to lemma 8. The number of trips made by all other persons either remains un hanged or. . sin e i is slower than the other person in the trip. the size of ea h set in F is un hanged.) Of ourse. However.

person i may be ome a settler. If so. The se ond trip is either a . That is. person i is an element of two trips in F. However. the number of nomads may be de reased by the repla ement.rm or soft) of ea h trip remains un hanged.

rm trip in F and be omes a hard trip. or it is a soft trip in F and be omes .

rm. for some j where j is at least 2 . and there are j−1 hard trips.2} . Now suppose there is a soft trip in F dierent from {1.2} results in a regular bag with a stri tly smaller total travel time. it is easy to he k that (8. ontradi ting F being optimal. In both ases. We may now on lude from (8.3) is maintained. When there are no soft trips. A similar argument to the one above shows that repla ing the trip by {1. all the trips are .2} in F is j .3) that either there are no soft trips or the multipli ity of {1.

rm or hard. as we have shown. person 1 is the only nomad in . But.

it follows that person 1 is the only nomad in F and. Thus persons 1 and 2 are the elements of one (. from (8.rm trips and there are no nomads in hard trips. that there are no hard trips.3).

N − 2×k − 1}} . (The multipli ity of ea h hard trip is 1 . when j is at least 2 .rm) trip in F . Then the settlers are persons 3 thru N . so we an ignore the distin tion between bags and sets. It remains to show that. the hard trips form the set {k: 0 ≤ k < j−1: {N − 2×k .) Assume that the number of soft trips is j where j is at least two. the remaining N − 2×(j−2) being elements of . and 2×(j−1) of them are elements of hard trips.

Any regular bad learly remains regular under any permutation of the settlers.rm trips. So we have to show that hoosing the settlers so that the hard trips are .

2 Algorithmi Problem Solving Roland Ba khouse. This is done by indu tion on the number of settlers in the hard trips.lled in order of slowness gives the optimal arrangement. May 28. 2008 .

j = hΣi : 3 ≤ i ≤ N − 2×(j−1) : t. Suppose F is an optimal solution satisfying the properties listed in orollary 8.1 + (j−1) × t.(N−2i)i . if j is the multipli ity of {1.14) HF. Then. where HF. The Algorithm 8.2 .13 (8.2} in F .j + FF.6.6 The Algorithm We an now solve the problem. and FF. (The .12. the total time taken by F is Lemma 8.ii .j = hΣi : 0 ≤ i < j−1 : t.2 + (N−j−1) × t.j + j × t.133 8.

and the last two terms give the return times.1 + 1 × t.2 + (N−1−1) × t.ii + (N−2) × t. the value of j is 1 . If there are no soft trips.rst three terms give the forward times. But HF.) There are two ases to onsider.1 + (1−1) × t.1 + FF.2 = { de. In this ase.1 . the total time taken is Proof hΣi : 2 ≤ i ≤ N : t.

1 = { arithmeti } hΣi : 2 ≤ i ≤ N : t. In this ase. HF.1 .2 + (N−2) × t.ii + t. the value of j is equal to the number of soft trips and is at least 2 .ii + (N−2) × t.nition of HF and FF .j is the forward time for the hard trips in F and FF. If there are soft trips. arithmeti } 0 + hΣi : 3 ≤ i ≤ N : t.j is the forward time for the .

de.1 and person 2 's return time is (j−1) × t. Finally. whi h is what we expe t the number of return trips to be.2 is the forward time for the j soft trips. (Person 2 is an element of j forward trips. where j is at least 2 .rm trips in F . person 1 's return time is (N−j−1) × t.2 . and person 1 is an element of j + (N − 2×(j−1) − 3 + 1) forward trips. Note that the sum of j−1 and N−j−1 is N−2 . Also.) 2 For all j . j × t.

2008 . May 28. Now. OT . we observe that Algorithmi Problem Solving Roland Ba khouse.j to be the optimal time taken by a regular N bag where the multipli ity of {1.2} in the bag is j .j is given by (8.ne OT .14). That is.

(N−2j+2) + t.j = −(t.j an be determined by a sear h for the point at whi h the dieren e fun tion hanges from being negative to being positive.(N−2j+1) + 2 × t. If the test evaluates to false.j = t.1 ≤ −t. the remaining N−2j+2 people are s heduled to ross in N−2j+1 . this is followed by the return of person 1 .j = −t.134 8. The simplest way to do this and simultaneously onstru t a regular sequen e to get all N people a ross is to use a linear sear h. At ea h iteration the test 2 × t. That is.(N−2j+2) .(j+1) − HF. Note that OT .2 − t.2 − t. OT .(N−2k+1) ≤ t.2 − t. OT .1 + t.j in reases as j in reases. beginning with j assigned to 1.(N−2j+1) = t is in reasing { } j ≤ k . If it evaluates to true . N−2j+1} and then the return of person 2 .(k+1) − OT .(N−2j+1)) .j ≤ OT .k = above { } −t. the soft trip {1.(j+1) − FF.(N−2k+1) + 2 × t.1 = arithmeti } { t. A onsequen e is that the minimum value of OT .2 ≤ t.(j+1) − OT .(j+1) − OT .(N−2j+1) is performed.(j+1) − OT . The Tor h Problem HF.1 .(N−2j+1) + 2 × t. As a onsequen e.2} is s heduled. and FF. then the hard trip {N−2j+2 .

This is en oded as follows.ki Algorithmi Problem Solving Roland Ba khouse. When the number N is large.(k+1) ≤ OT . In ea h trip one person rosses a ompanied by person 1 .j := 1 . { 2≤N } i. May 28. 2008 . N÷2 . in between two su h trips person 1 returns. { Invariant: 1 ≤ i ≤ j ≤ N÷2 ∧ h∀k : 1 ≤ k < i : OT .rm trips. the number of tests an be redu ed by using binary sear h to determine the optimal value of j .

Con lusion ∧ h∀k : j ≤ k < N÷2 : OT .(k+1) ≤ OT . j is the multipli ity of {1.2 ≥ t.j is the required optimal time.(N−2m+1) → i := m 2 2 × t.(k+1) ≥ OT .2 ≤ t.ki ∧ h∀k : j ≤ k < N÷2 : OT .2} in an optimal regular bag and OT .(k+1) ≥ OT .1 + t.(N−2m+1) → j := m fi od 1 ≤ j ≤ N÷2 { ∧ h∀k : 1 ≤ k < j : OT .7.135 8. Thus.ki } do i < j → m := (i+j)÷2 .ki } . if 2 × t.1 + t. j−1 is the number of hard trips. Corollary 8. On termination.12 spe i.

es the omposition of the hard trips and lemma 8.10 spe i.

es how they are s heduled. The remaining N−2j+2 people are then s heduled to ross in N−2j+1 .

and the \nomads" (the people who do make return trips) are the fastest. this property ould have been very diÆ ult to establish. 8. The greatest hallenge in an optimisation problem of this nature is to establish without doubt that the algorithm onstru ts a solution that an not be bettered. A major step in solving the problem was to eliminate the need to onsider sequen es of rossings and to fo us on the bag of forward trips. Even though these properties may seem unsurprising and the . The proof of the fa t that there are at most two nomads is made parti ularly easy by the fo us on the bag of forward trips. Via a number of lemmas.7 Conclusion In this hapter. An optimal sequen e is \regular" | that is. Many of the properties we proved are not surprising. the \settlers" (the people who never make a return trip) are the slowest. ea h forward trip is made by two people and ea h return trip is made by one. Less obvious is that there are at most two nomads and the number of \hard" trips (trips made by two settlers) is one less than the number of trips that the two fastest people make together. if we had had to reason about the sequen e of trips. we have presented an algorithm to solve the tor h problem for an arbitrary number of people and arbitrary individual rossing times. we established a number of properties of an optimum bag of forward trips whi h then enabled us to onstru t the required algorithm.rm trips as des ribed above.

May 28. it is important to appre iate that proof is required Algorithmi Problem Solving Roland Ba khouse.nal algorithm (in retrospe t) perhaps even \obvious". 2008 .

it is also sometimes alled the \bridge problem". this hapter is about the apa ity 2 problem.136 8.8 Bibliographic Remarks In Ameri an-English. 8. In terms of this more general problem. This dieren e is super. the tor h problem is alled the \ ashlight problem". Rote des ribes the solution in terms of \multigraphs" rather than bags. Always beware of laims that something is \obvious". The solution presented here is based on a solution to the yet-more-general tor h problem in whi h the apa ity of the bridge is a parameter [Ba 07℄. the solution is essentially the same as a solution given by Rote [Rot02℄. The Tor h Problem | the interest in the tor h problem is that the most \obvious" solution (letting the fastest person a ompany everyone else a ross the bridge) is not always the best solution.

The main dieren e is lemma 8. the apa ity of the bridge is 3 and the travel times are 1 . (If N = 5 . Rote does not prove that any regular bag an be onverted into a sequen e of rossings. Algorithmi Problem Solving Roland Ba khouse. Many \obvious" properties of an optimal solution turn out to be false.10. of ourse. his a ount is in orre t. in ontrast to the use of bags whi h does. 1 . . . is just a set of two people. 2t2 − t1 might not o ur in the list | the standard 1 . (Stri tly. Ea h edge of a \multigraph" onne ts two people and. an be determined easily by lo ating the value 2t2 − t1 in the sorted list of ti 's" but. Rote's formulation of the solution in terms of \multigraphs" does not appear to generalise. 2 . and doesn't suggest using binary sear h. the shortest time is 8 . whi h is a hieved using 5 rossings. 4 . 2008 . it is no longer the ase that an optimal solution uses a minimum number of rossings. but only proves that it is possible for the regular bags orresponding to optimal rossings. He does laim that this is indeed the ase.) The notion of \regularity" of rossings has to be generalised in a way that allows for some forward trips not to be \full" (in the sense that the full apa ity of the bridge is not utilised). the problem is mu h harder to solve. hen e. at one point. ial. For example. The shortest time using 3 rossings is 9 . Rote des ribes the linear-sear h method of determining the optimal solution. 5 and 10 minute problem is an example!) Rote gives a omprehensive bibliography in luding pointing out one publi ation where the algorithm is in orre t. When the apa ity of the bridge is also a parameter. 4 and 4 . He says \the optimal value . May 28.

by suitably de omposing the problem.Chapter 9 Knight’s Circuit The problem ta kled in this hapter is a parti ularly hard one. the problem be omes solvable. ombined with ee tive reasoning skills. Yet. The problem is to .

.nd a Knight's ir uit of a hessboard. That is.

From ea h of the orner squares. one for ea h square. However. visiting ea h square exa tly on e. in prin iple. but from ea h of the 16 entral squares.nd a sequen e of moves that will take a Knight in a ir uit around all the squares of a hessboard. there are 64 squares on a hessboard. it an be solved by a systemati . that means 64 moves have to be hosen. exhaustive examination of all the paths a Knight an follow around a hessboard | a so- alled brute-for e sear h. there is a hoi e of just 2 moves. there is a hoi e of 8 moves (see . and ending at the square at whi h the ir uit began. The problem is an instan e of a sear h problem.

Lots of hoi e is usually not a problem but. But. The Knight's ir uit problem is hard be ause of this riti al ombination of an explosion with an implosion of hoi e. when ombined with the very restri tive requirements that the path forms a ir uit that visits every square exa tly on e. and the Knight's moves. 9. all is not lost. although many.g. This gives a massive amount of hoi e in the paths that an be followed. are spe i. it does be ome a problem.1). The squares on a hessboard are arranged in a very simple pattern. from the remaining squares either 4 or 6 moves are possible.

137 May 28.ed by one simple rule (two squares horizontally or verti ally. There is a great deal of stru ture. whi h we must endeavour to exploit.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. 9. 2008 . Algorithmi Problem Solving Roland Ba khouse. and one square in the opposite dire tion).

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.138 9. Let's turn the problem on its head.1: Knight's Moves. What sort of moves would you hoose? The obvious . Knight's Cir uit Figure 9.

then return to the starting square. although it isn't immediately obvious.rst answer is to allow moves from any square to any other square. but ex luding diagonal moves. (These are the moves that a King an make. In that ase. Let's onsider hoosing from a restri ted set of moves. But that is just too easy.) We all these moves straight moves. hoose a move to a square that has not yet been visited until all the squares are exhausted. You may be able to . whatever its size | starting from any square. it's always possible to onstru t a ir uit of any board. The simplest move is one square horizontally or verti ally. Is it possible to make a ir uit of a hessboard just with straight moves? The answer is yes.

but let us try to .nd a straight-move ir uit by trial and error.

to the left or right. rather than restri t the problem to an 8×8 board. May 28.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. please ta kle the following exer ise. beginning and ending at the same square. or up or down. is it possible to visit every square exa tly on e. is it possible to omplete a straight-move ir uit of the entire board? That is. Exercise 9. As is often the ase. Its solution is relatively straightforward.nd one more systemati ally. (Hint: ru ial is Algorithmi Problem Solving Roland Ba khouse. let us onsider an arbitrary re tangular board. Assuming ea h move is by one square only. making \straight" moves at ea h step? In order to gain some familiarity with the problem. it is easier to solve a more general problem. 2008 .

We may take as the indu tive hypothesis that a straight-move ir uit of both boards an be onstru ted. (Both m and n must be non-zero be ause the problem assumes the existen e of at least one starting square. a 2m × n board an be split into two boards of sizes 2p × n and 2q × n . (The onvention we use is that the .1 is that a straight-move ir uit is only possible if the board has size 2m×n . ea h row having n squares.139 9. This suggests that we now try to onstru t a straight-move ir uit of a 2m×n board. for m at least one and n greater than one.) (c) Show that it is possible to omplete a straight-move ir uit of a 2×n board for all (positive) values of n . by indu tion on m . Otherwise. the answer does not depend on the sort of moves that are allowed.) 2 The on lusion of exer ise 9. There are two straight moves from ea h of the orner squares. The key to the ombination is the orner squares. (A 2×n board has two rows. We just need to ombine the two onstru tions. imagine that a 2m × n board is divided into a 2p × n board and a 2q × n board. where m is greater than 1 . Now. when m is greater than 1 .1.) Also. the number of squares is 2m . the 2×n ase providing the basis for the indu tive onstru tion. say. a straight-move ir uit an always be ompleted when the board has size 2×n . That is. for positive numbers m and n . with the former above the latter. for positive n . To omplete the indu tive onstru tion. we need to onsider a board of size 2m × n . and any straight-move ir uit must use both. In parti ular. it must use the horizontal moves. Su h a onstru tion is hopeful be ause. where both p and q are smaller than m and p+q equals m . Straight-Move Cir uits that ea h move is from one square to a dierent oloured square. one side has length 2m and the other has length n .) (b) For what values of m is it possible to omplete a straight-move ir uit of a board of size 2m×1 ? (A 2m×1 board has one olumn of squares.

Figure 9.) Now.rst number gives the number of rows and the se ond the nunber of olumns of the board. the shape of the dotted lines gives no indi ation of the shape of the ir uit that is onstru ted. 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. (Of ourse. The blue dotted lines depi t the rest of the ir uits.) 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. repla e the horizontal moves from the bottom-left and top-left orners by verti al moves.2 shows the result diagrammati ally. as shown by the verti al green lines in . to ombine the ir uits to form one ir uit of the entire board.

May 28. Algorithmi Problem Solving Roland Ba khouse.3. 2008 .g. This ompletes the onstru tion. 9.

split the board into two smaller boards and onstru t straight-move ir uits of ea h. n 2p 2m 2q Figure 9. The indu tion step then ombines them by repla ing horizontal moves by the green verti al moves shown in . First.140 9. Effe tively. Figure 9.3: Se ond.4 shows the ir uit that is onstru ted in this way for a 6 × 8 board. ombine the two ir uits as shown.2: Combining straight-move ir uits. Knight's Cir uit n 2p 2m 2q Figure 9. the basis of the indu tive algorithm onstru ts straight-move ir uits of three 2 × 8 boards.

It is easy to onstru t a straight-move ir uit of all its squares but the middle square. 9. (See . Consider a 3×3 board. As mentioned above. no ir uit is possible. when a board has an odd number of squares.4.g.

and when it is not possible. a straight-move ir uit of all but one of the other four squares |the squares adja ent to a orner square. the middle-left square| annot be onstru ted. However.5.2 Algorithmi Problem Solving Roland Ba khouse. Exercise 9. May 28. for example.g. 2008 .) It is also possible to onstru t a straight-move ir uit of all its squares but one of the orner squares. 9. to onstru t a straight-move ir uit of all the squares but one in a board of odd size. Explore when it is possible.

2 9.2. omitting one of the squares.2 Supersquares Let us now return to the Knight's- ir uit problem.141 9.4: A straight-move ir uit for a 6 × 8 board. as shown in .5: Straight-move ir uits (shown in red) of a 3 × 3 board. Supersquares Figure 9. Figure 9. 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". The key to a solution is to exploit what we know about straight moves.

9. the Knight's moves an be lassi.g. If this is done.6.

a Knight's move is straight if it takes it from one supersquare to another supersquare either verti ally above or below.ed into two types: Straight moves are moves that are \straight" with respe t to the supersquares. that is. or horizontally to the left or to the right. Diagonal moves are moves that are not straight with respe t to the supersquares. 2008 . May 28. a move is diagonal if it takes the Algorithmi Problem Solving Roland Ba khouse.

In . Knight's Cir uit Figure 9.6: Chessboard divided into a 4 × 4 board of supersquares.142 9. Knight from one supersquare to another along one of the diagonals through the starting supersquare.

Figure 9. whilst horizontal moves ip them around a horizontal axis. (The verti al moves inter hange the red-above-yellow and blue-above-green olumns.7: Straight (Blue) and Diagonal (Red) Knight's Moves From Some Starting Position (Bla k). Fo using on the straight moves. the straight moves are to the blue positions. 9. The olours indi ate the moves that an be made. For example. Boundaries of the supersquares are indi ated by thi kened lines. May 28. the horizontal moves inter hange the red-next-to-blue row with the yellow-next-to-green row.8 shows the straight moves from one supersquare |the bottom-left supersquare| verti ally upwards and horizontally rightwards. the boundaries of the supersquares are indi ated by thi kened lines.7. 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. 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 9. Observe the pattern of the olours. the starting position of the Knight is shown in bla k. Verti al moves ip the olours around a verti al axis.) Algorithmi Problem Solving Roland Ba khouse.g. and the diagonal moves are to the red positions. we now make a ru ial observation. 2008 .

let us denote by h (short for \horizontal") the operation of ipping the rows of the square. Let us denote by v the operation of ipping the olumns of a 2 × 2 square ( v is short for \verti al"). Supersquares 111 000 00 11 00011 111 00 000 111 00 11 000 111 00 11 00 11 000 111 00011 111 0011 00 000 111 00011 111 0011 00 000 111 00011 111 0011 00 000 111 143 Figure 9. Now. let an in.8: Straight moves from the bottom-left supersquare.2. Similarly.9.

v .x semi olon denote doing one operation after another. h denotes the operation of . for example. So.

by de. That is. let us use c (short for \ entre") as its name. Both are equivalent to rotating the 2 × 2 square through 1800 about its entre.v . Flipping the olumns and then ipping the rows is the same as ipping the rows and then the olumns.rst ipping the olumns and then ipping the rows of the square.h = h. So. v. That is.

3) v. (9.nition of c . We have now identi.h = c = h.v .

whi h is the do-nothing operation. all amount to doing nothing.3).c = n . for greater brevity we use n (short for \no hange"). doing one operation x followed by two operations y and z in turn is the same as doing . Also. That is. together with the fa t that doing one operation after another is asso iative (that is.5) n.ed three operations on a 2 × 2 square.5). (9. Flipping twi e verti ally. or twi e horizontally. we have used skip to name su h an operation. (9.4) v. The three equations (9. doing nothing before or after any operation is the same as doing the operation. (9. There is a fourth operation. Elsewhere.n = x . Here. or rotating twi e through 1800 about the entre.v = h.x = x.h = c.4) and (9.

z ) allow the simpli. z) = (x .rst x followed by y and then on luding with z | in symbols. x . y) . (y .

Algorithmi Problem Solving Roland Ba khouse. ation of any sequen e of the operations to one operation. 2008 . For example. May 28.

h = n { n . Knight's Cir uit v. then rotating through 1800 about the entre. (Note how asso iativity is used impli itly between the . x = x .h = { v. and then ipping horizontally is the same as doing nothing. with x := n } n.h = { v.144 9.c. ipping verti ally.n = } n .h. In words.v.h = c } v.v = h.

rst and se ond steps. The use of an in.

) Use the table to verify that. h and c . for x and y in the set {n. (Avoid onstru ting the omplete table be ause it is quite large!) Exercise 9. v . Identify a omplete set of operations on a 2 × 2 square and extend your solution to exer ise 9. z) = (x .v. (In prin iple.6 (9.e. r .c} . ea h labelled by one of n . i.x operator for \followed by" fa ilitates this all-important al ulational te hnique. y) . z .c} .v.y = y. you need to onsider 43 . (9. in one ase lo kwise and in the other anti lo kwise. Exercise 9. 2008 . for x and y in the set {n. dierent ombinations. Let r denote the lo kwise rotation and let a denote the anti lo kwise rotation. (y .8) x .9 2 Algorithmi Problem Solving Roland Ba khouse. Constru t a table that shows the ee t of performing any two of the operations n .) 2 Two other operations that an be done on a 2 × 2 square are to rotate it about the entre through 90◦ .x . The table should have four rows and four olumns. a or c in sequen e. Che k also that.6 so that it is possible to determine the ee t of omposing any pair of operations. (Use the physi al pro ess of ipping squares to onstru t the entries.h.) Constru t a two-dimensional table that shows the ee t of exe uting two operations x and y in turn.7) x. Think of ways to redu e the amount of work. May 28. 64 .h.

Partitioning the Board 9.3.3 Partitioning the Board The identi.145 9.

ation of the four operations on supersquares is a signi.

Then the remaining . Suppose one of the supersquares is labelled \ n ". ant step towards solving the Knight's- ir uit problem.

v . depending on their position relative to the starting square. all 64 squares of the hessboard are split into four disjoint sets.fteen supersquares an be uniquely labelled as n . h or c squares. to the right of it is an \ h " square. and diagonally adja ent to it is a \ c " square. As a onsequen e. Then immediately above it is a \ v " square. Figure 9.9 shows how this is done. Supersquares further away are labelled using the rules for omposing the operations. Suppose we agree that the bottom-left square is an \ n " square. In .

9.9.g. the dierent sets are easily identi.

two squares of the same olour an be rea hed from ea h other by straight Knight's moves. and two squares of dierent olour annot be rea hed from ea h other by straight Knight's moves. (That is. May 28.ed by the olour of the square. There we established Algorithmi Problem Solving Roland Ba khouse.1.) 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 9. 2008 .9: Labelling Supersquares Re all the dis ussion of straight-move ir uits in se tion 9. Two squares have the same olour equivales they an be rea hed from ea h other by straight Knight's moves.

we an onstru t a straight-move ir uit of a 4 × 4 board. Now. That means we an onstru t straight-move ir uits of ea h of the four sets of squares on the hessboard. 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. a \straight" Knight's move is \straight" with respe t to the supersquares of a hessboard. In .146 9. In parti ular.

and a ir uit of all the yellow squares. a ir uit of all the blue squares. a ir uit of all the green squares. The . 9.9. We now have four disjoint ir uits that together visit all the squares of the hessboard.g. this means onstru ting a ir uit of all the red squares.

The way to do this is to exploit the \diagonal" Knight's moves.nal step is to ombine the ir uits into one. (Refer ba k to .

Figure 9. Figure 9. by ombining the red-blue and green-yellow ir uits.11 shows one way of hoosing the straight and diagonal moves in order to ombine red and blue ir uits. Figure 9. in ea h ase. These straight moves are repla ed by the diagonal moves.g. 9. we an ombine the green and yellow ir uits into a \green-yellow" ir uit.10 shows s hemati ally how red-blue and green-yellow ir uits are formed. and green and yellow ir uits | in ea h ase. it is easy to . shown as solid lines. a single move in ea h being depi ted by a dotted line. The straightmove ir uits are depi ted as oloured ir les. For example.) A simple way of ombining the four ir uits is to ombine them in pairs. similarly.10: S hema for forming \red-blue" and \green-yellow" ir uits. Exploiting symmetry. we an ombine the red and blue ir uits into a \red-blue" ir uit of half the board.7 for the meaning of \diagonal" in this ontext. and then ombine the two pairs. a omplete ir uit of the board is obtained. Finally. two \parallel" straight moves are repla ed by two \parallel" diagonal moves. 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).

2008 . May 28.nd similar \parallel Algorithmi Problem Solving Roland Ba khouse.

3. On the other hand.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. onsequently. there are no diagonal moves from red to green. Partitioning the Board moves" with whi h to ombine red and yellow ir uits.147 9. Red and blue straight-move ir uits have been ombined in . 11 00 000 111 00 11 00 11 000 111 000 111 00 11 000 111 00 11 00011 111 0011 00 000 111 00011 111 00 000 111 00 11 00011 111 0011 00 000 111 00011 111 00 000 111 00 11 000 111 00 11 00 11 000 111 000 111 00 11 000 111 00 11 000 111 00 11 00 11 000 111 000 111 00 11 000 111 00 11 000 111 00 11 00 11 000 111 000 111 00 11 000 111 00 11 000 111 00 11 00 11 000 111 000 111 00 11 000 111 00 11 00011 111 0011 00 000 111 00011 111 00 000 111 00 11 000 111 00 11 00 11 000 111 000 111 00 11 000 111 00 11 000 111 00 11 00 11 000 111 000 111 00 11 000 111 00 11 00011 111 0011 00 000 111 00011 111 00 000 111 00 11 00011 111 0011 00 000 111 00011 111 00 000 111 Figure 9. it is impossible to onstru t a \red-green" or a \yellow-blue" ir uit. or green and blue ir uits. or from yellow to blue squares.

9. A slight diÆ ulty of this method is that it onstrains the straight-move ir uits that an be made. To omplete a ir uit of the whole board. 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). a green-yellow ir uit has to be onstru ted. This is left as an exer ise. onsidering the suggested method for ombining red and blue ir uits in . and this ir uit ombined with the red-blue ir uit. For example. with this red-blue ir uit as basis.12 to form a \redblue" ir uit.g.

9. 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.g. However. 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).11. The diÆ ulty is resolved by .

May 28.rst hoosing the ombining moves and then onstru ting the straight-move Algorithmi Problem Solving Roland Ba khouse. 2008 .

\Parallel" red and blue moves. Indi ate learly how the individual ir uits have been ombined to form the entire ir uit. are repla ed by diagonal moves. but not for smaller boards.13 illustrates another way that the ir uits an be ombined. shown as thi ker solid lines. the result is a ir uit of the omplete board. The four straight-move ir uits are depi ted as ir les. This is possible in the ase of an 8 × 6 board. Knight's Cir uit Figure 9.148 9. To arry out this plan. the four diagonal moves in . Exercise 9. thus ombining the two ir uits. one segment of whi h has been attened and repla ed by a dotted line. shown as dotted lines. ir uits appropriately. In order to onstru t a Knight's ir uit of smaller size boards. the dierent pairs of ombining moves need to be positioned as lose as possible together.10 2 Figure 9. Constru t a Knight's ir uit of an 8 × 8 board using the s heme dis ussed above.12: A \red-blue" ir uit. If these are repla ed by diagonal moves (represented in the diagram by solid bla k lines). Do the same for an 8 × 6 board. The dotted lines represent straight moves between onse utive points.

9.13 have to be identi.g.

and parallel blue and yellow moves. 2008 . May 28.ed. say.11 Algorithmi Problem Solving Roland Ba khouse. the above solution involved seeking parallel red and blue moves.) Choosing to start from. the Exercise 9. The key to doing this with a minimum of eort is to seek parallel red and green moves. (In ontrast. whilst exploiting symmetry.

149 9. 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. Partitioning the Board Figure 9. this makes the appropriate moves easy to . severly restri ts the hoi e of diagonal moves.3. in ombination with symmetry.13: S hema for Combining Straight-Move Cir uits.

one Algorithmi Problem Solving Roland Ba khouse. no straight-move ir uit is possible. more are has to be taken in the onstru tion of the straightmove ir uits. try turning the board through 90◦ whilst maintaining the orientation of the ombining moves. whenever. However. by exploiting exer ise 9. and the strategy used in exer ise 9.) 2 Exercise 9.nd.2 for how this is done. Constru t a knight's ir uit of an s heme. Be ause this superboard has an odd number of (super) squares. If you en ounter diÆ ulties. for ea h ir uit. The strategy is to onstru t four straight-move ir uits of the board omitting one of the supersquares. (The onstru tion of the ir uit is easier for an 8 × 8 board than for a 6 × 8 board be ause. it is possible to onstru t Knight's ir uits for boards of size (4m + 2) × (4n + 2) . (Re all exer ise 9. 2008 .10 is not appli able.12 Division of a board of size (4m + 2) × (4n + 2) into supersquares yields a (2m + 1) × (2n + 1) \super" board. in the latter ase. for any 8×8 and a 6×8 board using the above Explain how to extend your onstru tion to any board of size m and n su h that m≥2 and 4m × 2n n≥3.2. May 28.) Then. both m and n are at least 1 .

150 9. Knight's Cir uit move is repla ed by two moves |a straight move and a diagonal move| both with end points in the omitted supersquare. This s heme is illustrated in .

14. Complete the details of this strategy for a 6 × 6 board.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. 11 00 00 11 000 111 000 111 Figure 9.g. These are then onne ted as shown. In order to onstru t the twelve ombining moves depi ted in . 9. Make full advantage of the symmetry of a 6 × 6 board.

g. whenever.14 . the remaining nine an be found by rotating the moves through a right angle. 9. whi h means it is a very good example of dis iplined problem-solving Algorithmi Problem Solving Roland Ba khouse. 2 9. it suÆ es to onstru t just three. the Knight's ir uit problem is a truly diÆ ult problem to solve.4 Discussion In the absen e of a systemati strategy. 2008 . both m and n are at least 1. May 28. 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) .

This is indeed the ase for the method we have used to solve the Knight's ir uit problem. Dis ussion skills. The method has been applied to onstru t a ir uit of an 8 × 8 hessboard. The key ingredients are the lassi.4. The key riterion for a good method is whether or not it an be extended to other related problems.151 9. and ombining these together. but the method an learly be applied to mu h larger hessboards. 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.

A drawba k of the method is that it an only be applied to boards that an be divided into supersquares. it is not possible to onstru t a Knight's ir uit of a board with an odd number of squares. ation of moves as \straight" or \diagonal". a omplete hara terisation of the sizes of board for whi h a Knight's ir uit exists is given in se tion 9. for some m and n . whi h is obviously highly impra ti al. (That is. That leaves open the ases where the board has size (2m) × (2n + 1) .) For those interested. As we have seen.5. and using diagonal moves to ombine straight-move ir uits . Contrast this with remembering the ir uit itself. straight-move ir uits of supersquares. if not impossible. and reprodu e a Knight's ir uit on demand. it is easy to remember these ingredients. one side has even length and the other has odd length. The Knight's- ir uit problem exempli. On e the method has been fully understood.

es a number of mathemati al on epts whi h you will probably en ounter elsewhere. The n . In general. 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". 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". The Knight's- ir uit problem is an instan e of a general lass of problems alled \Hamiltonian-Cir uit Problems". v . 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 .

before returning to the starting node.nd a path through the graph that visits ea h node exa tly on e. instan es of a lass of problems alled \NP- omplete" problems. in turn. Hamiltonian- ir uit problems are. NP- omplete problems are problems hara terised by ease of veri.

May 28. it is easy to he k whether or not it is orre t (for example. no eÆ ient methods Algorithmi Problem Solving Roland Ba khouse. for the lass of NP- omplete problems. however. given a putative solution. That is. 2008 . ation but diÆ ulty of onstru tion. it is easy to he k whether or not it is a Knight's ir uit). given any sequen ing of the squares on a hessboard.

A ording to one of these [Ma Quarrie. 9. St.℄ the .5 Boards of Other Sizes To be written. Andrews Univ.6 Bibliographic Remarks Solutions and histori al information on the Knight's ir uit problem an easily be found on the internet. \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. 9. Knight's Cir uit are known at this time for onstru ting solutions.152 9.

The solution to exer ise 9. 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.11. 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 9. The pairwise ombination of straight-move ir uits is due to Diethard Mi haelis [private ommuni ation℄. long before hess was invented. making it a little bit harder to do.rst Knight's tour is believed to have been found by al-Adli ar-Rumni in the ninth entury. 2008 .12 was onstru ted by the author. May 28. The use of supersquares in solving the Knight's ir uit problem is due to Edsger W. Algorithmi Problem Solving Roland Ba khouse. with useful feedba k from Mi haelis. Dijkstra's solution involves sear hing for four pairs (twi e as many) at the same time.

Initially.p := m+1 . In general. 1234−1 players must be kno ked out. Hen e. n−1 . 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. k and g are both equal to 0 .3H |3W| . (In ea h ase. 2 3. 2 2. 5H |3W| 2W . 2008 .1 1233 games must be played. either zero or two). and redu e the number of obje ts. p−m . k and g are always equal. if there are p players. Consider the dieren es m−n .1 { 5C || } 2C.3H |1W| 2W . Let k be the number of players that have been kno ked out. To de ide the tournament. The repla ement pro ess is modelled by the assignment m. we on lude that the goal is impossible to rea h if the starting state has zero odd dieren es. the number of odd dieren es of any three numbers is always even (i. p−1 .Solutions to Exercises 2. Every time a game is played. 153 May 28.e. { 5H || 5W } Algorithmi Problem Solving Roland Ba khouse.2 Let m . the tournament onsists of p−1 games. n and p denote the number of obje ts of ea h kind. this number of games must be played. one more player is kno ked out. and let g be the number of games that have been played. 2C. 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. Choosing to in rease the number of obje ts of the kind that o urs least frequently will maintain this invariant. If there is more than one obje t remaining.n. It is easily he ked that the parity of ea h is un hanged by the assignment.) Also. there must be two obje ts of dierent kind. the dieren e either is un hanged or in reases or de reases by 2 . So. n−p . Sin e the goal is to rea h a state in whi h there are two odd dieren es.

Solutions to Exer ises 154 5H |2W| 3W . { 5W || 5H } 2W |3W| 5H .3H { || 5C } .2 We modify the solution to the .3H . 2C |3H| 3W . { 2C || 3C } 2C |1C| 2C . 2W |1W| 2C. 3W |2W| 5H . 2 3. |3W| 2C. { 3C || 2C } 3W |3H| 2C .

2008 . We get: { 4C || } 1C. { 4H || 4W } 4H |1W| 3W . ee tively by leaving one ouple behind on the left bank. 3W |1W| 4H . { 2C || 2C } 2C |1C| 1C . 2W |1W| 1C.3H |3W| . 2C |2H| 2W . we get the se ond solution: { 4C || } 1C.3H |3W| .3H |1W| 2W . 4H |2W| 2W . { 4H || 4W } 4H |2W| 2W . { 1C || 3C } 1C |1C| 2C Algorithmi Problem Solving Roland Ba khouse. 1C |3H| 3W . By reversing left and right.3H |1W| 2W . { 4W || 4H } 2W |2W| 4H .3H { || 4C } . May 28.3H . |3W| 1C. 1C.ve- ouple problem. 1C. 4H |2W| 2W . { 3C || 1C } 3W |3H| 1C .

or all husbands are on one of the two banks. lH = 0 Algorithmi Problem Solving Roland Ba khouse. (Re all the assumptions about M and N . Property (a) is learly true initially. (If not. |3W| 1C. under the given assumptions on (a) M and N. 2W |1W| 1C. and let . either there are no single individuals on either bank. The number of wives on the right bank. In both ases. the wives outnumber the husbands on the left bank or on the right bank. After the rossing. assumption that M < lH . and a boat ouples. The number of husbands on the right bank. We note that an invariant is (lH = lW) ∨ (lH = 0) ∨ (lH = N) . It is a requirement of any solution that this property is an invariant.) denote the number of husbands on the left bank. we laim that. is at most the minimum of properties are ommon to the ases of a boat of apa ity of apa ity Let lH 3 and 6 2 and 4 3 and N/2 . Note that lH 6= 0 both before and after the rossing. (Before the rossing. (These ouples.Solutions to Exer ises . either The boat is on the left bank and (14) M < lH . either 0 < lH < lW or 0 < rH < rW . (13) That is. denoted rH . 2W |2W| 4H . a rossing is made from left to right. 2008 . 2 3. let lW denote the number of wives on the left bank. suppose (a) is true and.) Now. Similarly. is then N−lW .3 Let We assume that N is at least 2 and M N denote the number of ouples. the solution is invalid. or (b) the boat is on the right bank and (15) M ≤ lH . then. { 155 2C || 2C } 2W |2H| 2C .3H .3H { || 4C } M denote the apa ity of the boat.) Now. { 4W || 4H } 2W |2W| 4H . In words. is then N−lH . denoted rW . lH = 0 is ex luded by the is impossible be ause at most M May 28.

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

May 28.) A onsequen e is that an optimal solution onsists of exa tly 5 rossings. Sin e ea h return trip is made by just one person. 2 people never make Algorithmi Problem Solving Roland Ba khouse. of whi h 2 are return trips. See hapter 8 for a general argument why this assumption may be made. 2008 . ation of this assumption here.

an optimal solution is found when persons 3 and 4 |the two slowest| do not return. with person 1 returning. Clearly. (Consider any sequen e of rossings that gets all four a ross the bridge. Then persons 3 and 4 ross. (The in.Solutions to Exer ises 157 a return trip. then one or both of the two fastest does not make a return journey. The total time taken using this strategy is t1↑t2 + t1 + t3↑t4 + t2 + t1↑t2 . 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. By inter hanging a slower person with a faster person in the sequen e. and person 2 returns. The strategy \let the two slowest ross together" is implemented by letting persons 1 and 2 (the two fastest) ross. Finally. If one or both of the two slowest makes a return journey.) There are two strategies for getting the two slowest a ross: let them ross together or let them ross seperately. persons 1 and 2 ross again.

t1↑t2 simpli. Be ause we assume that t1 ≤ t2 ≤ t3 ≤ t4 .x operator \ ↑ " denotes maximum.

es to t2 and t3↑t4 simpli.

and . then person 1 returning. (This orresponds to person 1 and person 2 rossing.) 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 t1↑t2 + t1 + t1↑t4 + t1 + t1↑t3 .es to t4 . then person 1 and person 4 rossing. However. then person 1 returning. not simplifying the formula just yet makes it is easy to identify the people in ea h rossing.

The order hosen here fa ilitates the omparison of the times.) Comparing the total times. immaterial. 3 and 4 ross is.nally persons 1 and 3 rossing. the . of ourse. The order in whi h persons 2 .

) Applying this solution to the two spe i. (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.rst strategy should be used when t2+t2 ≤ t1+t3 and the se ond strategy when t1+t3 ≤ t2+t2 .

e. the two slowest should ross together. 18 minutes. 4 minutes. 1 minute. 3 minutes and 3 minutes: Sin e 1+1 ≤ 1+3 . we get: (a) The times taken are 1 minute. The shortest time is 4+1+5+1+4 .) Algorithmi Problem Solving Roland Ba khouse. 2008 .e.e. i. May 28. (b) The times taken are 1 minute. 15 minutes. ases. i. 4 minutes and 5 minutes. the two slowest should ross seperately. (The shortest time if the two slowest ross together is 4+1+5+4+4 . Sin e 1+5 ≤ 4+4 . i. 7 minutes. The shortest time is 1+1+3+1+1 .

in August. whi h has an even number of days. the last day of November).2 The . The pattern hanges in June. The opponent is then for ed to name the 1st of the next month. This is for ed by the opponent naming 31st O tober. We on lude that the se ond player is guaranteed to win.Solutions to Exer ises 158 2 3. This means that 31st Mar h is a losing day. the winning strategy is to name the last day of the month. i. the pattern we saw for De ember and November re urs. This is in line with the terminology of losing and winning positions. and any day in September is a winning position.) That is. Similarly. Otherwise. The player who names 1st July loses. and any day in July is a winning position. Every odd-numbered day in Mar h is a losing day. also. (Take are: The \losing positions" are the days that the winning player names. 2 4. This means that every even-numbered day in May is a winning day. like De ember.e. the strategy is to name the next day of the year. Whether the year is a leap year or not makes no dieren e. the odd-numbered days are losing positions. September. Again. This is for ed by the opponent naming 30th November (that is. 2 4.5 6×2×3×3×1 . any even-numbered day in June is a losing day. the odd-numbered days in January are losing days. other than 31st De ember results in losing. In general. . b) In De ember. In O tober. Sin e Mar h has an odd number of days. the losing positions are the odd-numbered days and the winning positions are the even-numbered days. the odd-numbered days are losing positions. the player who names 1st De ember wins. Any day in November is thus a winning position. the player whose turn it is must name an even-numbered day and. 108 . July or February. In parti ular. so. and every day in February is a winning day. be ause the opponent an then name 30th November. if the last-named day is an odd-numbered day.1 a) Naming any day in De ember. onsequently. The strategy is to name the 1st day of the following month when the last-named day is in November. Similarly. will eventually lose. Finally. every evennumbered day in April is a winning day. it does not matter if it is a leap-year. naming any day other than 30th November results in losing.

2 The squares that are not positions are the ones at the foot of a ladder or at the head of a snake. The pattern repeats in the se ond eleven positions.rst eleven positions are shown in table 1. See table 2. Positions that annot be identi.

2008 .3 Algorithmi Problem Solving Roland Ba khouse.ed as winning or losing positions are 4. May 28.

The mex Algorithmi Problem Solving Roland Ba khouse. winning positions or stalemate positions. Any other move from 4 would be to a winning position. Position Type Move to square 1 2 4 5 7 13 14 16 18 21 22 23 24 25 WWS WW L WWL WWWWL 3 3 6 9 9 18 18 25 25 25 25 Table 3: Snakes and Ladders. this is learly the wrong thing to do. Labelling of winning and losing positions assumes that every game is guaranteed to terminate no matter how either player moves. the positions are hara terised as losing positions. From this position. When a game has y les. If y les o ur this assumption is not valid. May 28. a winning position is one from whi h there is a move to a losing position. Losing (L) and Stalemate (S) positions 2 4.Solutions to Exer ises Position 0 Type L Move 159 1 2 3 4 5 6 7 8 9 10 L W W L W W W L W W 2 2 5 6 6 5 6 Table 1: Winning (W) and Losing (L) Positions for subtra tion set {2 . and a stalemate position is one from whi h there is a move to a stalemate position and there are no moves to losing positions. Position 4 is the only stalemate position.4 a) See table 4 for the mex numbers up to and in luding position 10. 6} attributable to y les in the positions. 5 . a y le is a sequen e of moves that begins and ends at the same position. The opponent will then use the same strategy and the game will ontinue forever. From a stalemate position the best strategy is to move to a stalemate position sin e. Table 3 shows all positions and the winning move from winning positions. A losing position is one from whi h every move is to a winning position. 6} Position 11 12 13 14 15 16 17 18 19 20 21 Type L L W W L W W W L W W 2 2 5 6 6 5 6 Move Table 2: Winning (W) and Losing (L) positions for subtra tion set {2 . Winning (W). 5 . 2008 . if there is an alternative of moving to a winning position. a move to square 6 has the ee t of returning the ounter to position 4 .

for all i . But n < 2i+1−1 ≤ 2×n = { meaning of ontinued equalities } n < 2i+1−1 ∧ 2i+1−1 ≤ 2×n = { integer inequalities . When i equals 0 . The proof is in two parts: we show that. 5 . all other positions are winning positions. 2008 . } May 28. (Other answers an be given for the winning moves. also. 2i+1−1 equals 1 . for all i . 6} . the mex number of position m is m mod 3 . from a position n where.) 2 4. Together with the mex numbers for the right game given above. Position 1 is an end position and thus a losing position.5 (a) The losing positions are positions 2i+1−1 where i is a natural number. 0 Position Type L Mex Number 0 1 L 0 2 W 1 3 W 1 4 L 0 5 W 2 6 W 1 7 W 3 8 L 0 9 W 2 10 W 1 Table 4: Mex numbers for subtra tion set {2 . symmetry of ∧ Algorithmi Problem Solving Roland Ba khouse. every move from position 2i+1−1 is to a position n where 2i−1 < n < 2i+1−1 . we an omplete table 5. Left Game Right Game \losing" or winning move 10 20 R14 20 20 losing 15 5 R0 6 9 R4 37 43 losing Table 5: Winning moves In the left game. When i is greater than 0 .Solutions to Exer ises 160 numbers repeat from here on. n 6= 2i+1−1 we show that we an hoose i so that there is a move from n to position 2i−1 . the mex number for position m is equal to the mex number for position m mod 11 . every move from position 2i+1−1 is to a position n where n < 2i+1−1 ≤ 2×n . that is.

Solutions to Exer ises 161 2i+1−2 < 2×n ∧ n < 2i+1−1 = { monotoni ity of 2× } 2i−1 < n ∧ n < 2i+1−1 = { meaning of ontinued equalities } 2i−1 < n < 2i+1−1 . This establishes the .

n 6= 2i+1−1 . for all i . For the se ond part. Then. suppose that. Let i be the largest number su h that 2i−1 < n .rst part. by de.

That is. n ≤ 2i+1−1 . But then there is a move from n to 2i−1 = { de. 2i−1 < n ≤ 2i+1−1 .nition of i .

n 6= 2i+1−1 } 2i−1 < n ≤ 2i+1−1 = { de.nition of legal moves } 2i−1 < n ≤ 2×(2i−1) = { arithmeti } 2i−1 < n ≤ 2i+1−2 = { integer inequalities } 2i−1 < n < 2i+1−1 = { assumption: for all i .

nition of i } true . (b) Position: Mex Number: 1 0 Position: Mex Number: 2 3 1 0 Position: Mex Number: 4 5 6 7 2 1 3 0 Position: Mex Number: 8 9 10 11 12 13 14 15 4 2 5 1 6 3 7 0 Algorithmi Problem Solving Roland Ba khouse. May 28. 2008 .

in order to move the knight as required. 2 5.6 Suppose the number of ouples is n . there are at least two people other than the host and the host's partner.n . Be ause neither is the host. It follows that the two people who shake hands 0 and 2n − 2 times are husband and wife.5 then Let col be the olour of the square. it also follows that neither is the host's partner. there must be someone who shakes hands with k people for ea h k between 0 and 2n − 2 (in lusive). and n be the number of moves. the only person other than the host is the host's partner. An invariant of this assignment is col ≡ even. Algorithmi Problem Solving Roland Ba khouse. If 2n − 1 of them |everyone but the host| shake hands with a dierent number of people. but the olour of the square doesn't hange. May 28. 2008 .n . it is thus the ase that everyone ex ept that person's partner shakes hands with at least one person.Solutions to Exer ises 162 No. of Rows 2 15 4 11 4 14 13 6 21 19 \losing" or winning move C1 (or R11) C2 (or R9) R9 losing C19 (or R10) Table 6: Solution to re tangle game In general. of ourse). Sin e ouples do not shake hands. in luding the host.(2×n) equals n and mex. By the symmetry of the shake-hands relation.n . of Columns No. In this ase. who ea h shake hands with between 0 and 2n − 2 people. So. A move is col . both shake hands 0 times. The person who shakes hands 2n − 2 times does so with everyone ex ept their partner (and themself. n := ¬col . n+1 . whi h is impossible. There are 2n people. a hange has to be made to col ≡ even. If n is 1 .(2×n + 1) equals mex. An odd number of moves ( 63 ) is needed. Now suppose that n is greater than 1. 2 5. Consider the two people who shake hands 0 and 2n − 2 times. mex.

9 ¬true = { law ¬p ≡ p ≡ false law true ≡ p ≡ p law ¬p ≡ p ≡ false with p := true } true ≡ false = { with p := false } false . The number of times ea h person shakes hands is then redu ed by one. Ea h time.10 ¬¬p = { Algorithmi Problem Solving with p := ¬p Roland Ba khouse. The host and the host's partner must therefore have both shaken hands n−1 times. the number of times the host and the host's partner shake hands is redu ed by one. the other n−1 That is. 2008 . 2 5.Solutions to Exer ises 163 Now suppose we dis ount this ouple. we onsider the party onsisting of ouples. 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. again. So. 2 5. all but the host have shaken hands a distin t number of times.8 (a) false (b) false (c) false (d) p (e) false (f) q 6≡ r (g) p (h) true 2 5. } May 28.

But. 2 5.11 The pro ess of de ryption after en ryption omputes a 6≡ (a 6≡ b) . a 6≡ (a 6≡ b) = { 6≡ is asso iative } (a 6≡ a) 6≡ b = { ( a 6≡ a ≡ false ) } false 6≡ b = { de.Solutions to Exer ises 164 ¬p ≡ false = { law ¬p ≡ p ≡ false with p := p and symmetry of equivalen e } p .

nition of 6≡ } false ≡ ¬b = { de.

2 5.nition of negation: (5. Assume indu tively that. there are exa tly two olourings. When there are n+1 lines. May 28. A line on a ball does this. If the lines are not straight and they interse t in a segment of a line. This is learly the ase when there are no lines. The solution remains valid provided every line uts the surfa e in two. This is be ause. the olours of the adja ent regions are not the same before the inversion takes pla e ontrary to the assertion made above. 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. 2 6. Cut the paper along the hosen line. for ea h half. i. along the line segment. Combining these gives four dierent ways of olouring the entire sheet of paper. However.3) } b .1 It is required that any two lines interse t in a single point. The number of olourings is always two no matter how many lines there are. hoose any one of the lines. In words. Q ≡ ¬B . whereas a line on a doughnut need not. ask A whether B is a knave.12 Let Q be the question. 2008 .e. Q ≡ A ≡ A 6≡ B . two of these are unsatisfa tory be ause the olours Algorithmi Problem Solving Roland Ba khouse. Then.

there is just one obje t.5 When m is 0 .Solutions to Exer ises 165 of regions adja ent at the hosen line must be dierent. Split the 3m obje ts into 3 groups ea h of 3m−1 obje ts. at most a further 2×(m−1) omparisons are required to . At most 2 omparisons are needed to determine whi h of the groups it is. This leaves exa tly two ways of olouring the paper with n+1 lines. One of these 3 groups will have a dierent weight to the other two. This is the unique obje t and 0 (whi h equals 2×0 ) omparisons are needed to dis over that fa t. Suppose now that m is greater than 0 . 2 6. Then. by indu tion. whi h will be of equal weight.

This gives a total of 2×(m−1) + 2 . It is possible to determine whether the unique obje t is lighter or heavier than the others (although. omparisons as required by the indu tion hypothesis. the answer is that it is both lighter and heavier than all the rest). It an be de ided in the . 2×m . in the ase that there is just one obje t. i.e.nd the unique obje t in that group.

assume that n−1 omparisons are needed to . it is lear that 0 omparisons are needed. 2 a) For n = 1 .rst two omparisons. For the indu tion step.

nd the lightest of n obje ts. To .

nd the lightest of n+1 obje ts. use n−1 omparisons to .

one extra omparison has been made. it is lear that 1 omparison is needed.nd the lightest of n obje ts. The lightest of the two is the lightest of them all. b) For n = 2 . For the indu tion step. assume that 2n − 3 omparisons are needed to . Also. then ompare this obje t with the ( n+1 )th obje t. making n in total.

To .nd the lightest and heaviest of n obje ts.

nd the lightest and heaviest of n+1 obje ts. use 2n − 3 omparisons to .

making (2n − 3) + 2 . The heaviest of the two is the heaviest of the four. Call the ( n+1 )th obje t N .e. The lightest of L and N is the lightest of them all.nd the lightest and heaviest of n obje ts. i. Compare B and D . and the heaviest of H and N is the heaviest of them all. . To weigh four obje ts. This requires two extra omparisons. 2(n+1) − 3 in total. Call these L and H . The lightest of the two is the lightest of the four. ) Compare A and C .

Call the lighter one A and the heavier one B .rst ompare two. ompare the remaining two obje ts and all the lighter one C and the heavier one D . Then pro eed as above. d) For m = 1 . Likewise. it is lear that 1 omparison is needed to .

Sele t and ompare any two of the obje ts. Suppose there are 2(m+1) obje ts. And. Let the lightest be A and the heaviest B . we an . 1 = 3×1 − 2 . By indu tion.nd the lightest and heaviest of 2 obje ts.

su h that A < B and 6. C and D . A . We now have four obje ts. May 28. 2008 . respe tively.6 Algorithmi Problem Solving Roland Ba khouse.nd the lightest and heaviest of the remaining 2m obje ts in 3m − 2 omparisons. B . Let these be C and D .

And. 2 7. the lightest and heaviest of these four an be found in 2 further omparisons.1 Formally we have To.d = { de. By part ( ). These are then the lightest and heaviest of all 2(m+1) obje ts.Solutions to Exer ises 166 C < D . the total number of omparisons is 1 + (3m − 2) + 2 whi h equals 3(m+1) − 2 .

d) = { de.nition of T } length(H0.

nition of H0.d } length.[ ] = { de.

and Tn+1.d = { de.nition of length } 0 .

d) = { de.nition of T } length(Hn+1.

¬d) = { de. ¬di] . [hn+1 .d } length(Hn. Hn.nition of Hn+1.¬d .

¬d) + length([hn+1 .nition of length } length(Hn. ¬di]) + length(Hn.¬d) = { de.

May 28. 2 .¬d . .d = 0 Tn+1. 2008 .d = 2 × Tn.d (in parti ular the repeated multipli ation by 2 ) suggest that Tn.d is 2n−1 .d is 1 and T2. just as we did for the equations for H .¬d + 1 .d is 3 (in ea h ase for all d ). 2 Algorithmi Problem Solving Roland Ba khouse. T1.¬d + 1 + Tn. That is. . The simple indu tive proof is omitted. This and the form of the equation for Tn+1.nition of T (twi e) and length } Tn. .d is 0 . . To. 1 . If we expand these equations for n = 0 . we dis over that To.

So. For example. suppose there are . In any state. the disks on any one pole are in order of de reasing size.Solutions to Exer ises 167 ` Figure 15: State-transition diagram for 0-disk problem.2 We begin by onsidering the permissible states that the puzzle may be in. 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. 7.

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. and disk 1 must be on top of disk 3 . 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 .ve disks and suppose we spe ify that disk 1 is on pole A . disk 3 must be on top of it. disk 2 is on pole B . Also.

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 .

Now we onsider the transitions between states.rst name in the sequen e is the lo ation of disk 1 . We onsider . and so on. the k th name in the sequen e is the lo ation (pole name) of disk k . That is. Sin e ea h disk may be on one of three poles we on lude that there are 3n dierent states in the n -disk problem. the se ond is the lo ation of disk 2 .

This is shown in . then the 2 -disk problem. and then we onsider the general n -disk problem. then the 1 -disk problem.rst the problem where there are no disks. When there are no disks there is exa tly one state: the state when there are no disks on any of the poles.

g. (You may have diÆ ulty seeing the . 15.

gure. (See . 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 problem.

16.g.) 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. May 28. 2008 . Thus. ea h state in the state-transition diagram for the n -disk problem Algorithmi Problem Solving Roland Ba khouse.

Solutions to Exer ises 168 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.

B or C . Consider .ed by a sequen e of n pole numbers followed by the pole name A . 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.

But its position doesn't ae t the permissibility or otherwise of a move of a smaller disk. The . where the pole name p is either A . 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.rst moving a disk other than the largest disk. When doing so. B or C . the largest disk may be on pole A . B or C .

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.

Being the largest disk it may only be moved if all the other disks are on one and the same pole dierent to the pole that the largest disk is on. or three edges in the undire ted state-transition diagram: an edge onne ting the states AnB and AnC . The onstru tion is shown s hemati ally in . the disk numbered n+1 . This gives six possibilities for moving disk n+1 . Now onsider moving the largest disk. an edge onne ting the states BnC and BnA and an edge onne ting the states CnA and CnB .ed by simply adding p at the end of ea h sequen e of pole numbers labelling the nodes.

e. be ause the dire tion of movement is opposite to that of the smallest disk (whi h has an odd number). 2 7.k ≡ d ′ .g.) Set d to odd. when it is no longer possible to determine k in step 1). 16. 2 7. 2.4 The algorithm is to repeatedly exe ute the following pro edure until it an no longer be exe uted (i. the three inner triangles representing the set of all moves that do not move disk n+1 . (Re all that disk 1 is the smallest disk.3 Even. of ourse). Suppose it is possible to move disk k in the dire tion d ′ . 1. 3. The orre tness is justi. Move disk k (in the dire tion d ′ . Move the smallest disk in the dire tion d . where k > 1 .

we know that the .ed as follows. When step 1 is exe uted.

To do this.rst k−1 disks are all on the pole in dire tion ¬d ′ from disk k . it is ne essary to move the k−1 smallest disks in the dire tion ¬d ′ . May 28. Progress is made if these k smallest disks an be transferred to the same pole. 2008 . The dire tion that disk 1 has to be moved is thus d where Algorithmi Problem Solving Roland Ba khouse.

Solutions to Exer ises 169 AnA ` A AA ` ` ` ` ` ` B A` BnC ` A AA AA n ` ` ` CnC AA A` AnC ` ` ` ` AA A` ` ` ` AnB Figure 16: Constru tion of the state-transition diagram for the Algorithmi Problem Solving ` ` ` CnA ` ` ` A` A A A ` CnB A AA ` ` ` ` ` ` ` Roland Ba khouse. BnB (n + 1) -disk problem May 28. 2008 .

starting with the largest and ending with the smallest. Otherwise.Solutions to Exer ises 170 even. moving one-by-one to all the squares in the same row. suppose it needs to be moved in dire tion d from its urrent position. we get that d = (odd.1 (a) The number of moves that have to be made equals the number of squares. During this sequen e of moves the smallest disk will ontinue to move in the same dire tion.) The orre tness of the Towers of Hanoi program then guarantees that this a tion will initiate a sequen e of moves after whi h all k−1 disks will have been moved onto disk k . but. Finally. Simplifying. initially k is N and we are done when k is 0 . as required.(k−1) ≡ ¬d ′ ≡ even. no straight-move ir uit is possible. the dire tion of the smallest disk may or may not be reversed. the dire tion that the smallest disk is moved should be the same as the dire tion that disk k is moved. a straight-move ir uit of a 2×n board is ompleted by starting at a orner. The only time that step 1 annot be exe uted is when all the disks are on the same pole. (c) See (b) for a ir uit of a 2×1 board. otherwise the smallest disk is moved in the opposite dire tion to disk k . Move the smallest k−1 disks in the dire tion ¬d . (In words. we ensure that the k smallest disks are on the same pole. 2008 . at least one square is two moves from the starting square. then move disk k to its rightful position. de rease k by 1 . On ompletion. the olour of the urrent square is dierent from the olour of 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. if k is also odd. then returning via the se ond row. after an odd number of moves. de rease k by 1 . 2 Algorithmi Problem Solving Roland Ba khouse.5 2 9. After an odd number of moves. move to the other square and then move ba k again| .k ≡ d ′ ) . so. Ea h time the value of k is reassigned. If the k th disk is on the right pole.1 ≡ d . For n greater than 1 . otherwise. 2 The solution is to pla e the disks in order. however. it is impossible to return to the starting square. If m is greater than 1 . (b) It's easy to see that a straight-move ir uit of a 2×1 board is possible |starting at one of the squares. Continue this pro ess until k is 0. Let k denote the number of the disks still to be repla ed. May 28. So. 7.

(It doesn't matter whether numbering starts at zero or one. the same ondition applies. Then.2 For the 3 × 3 board. n) . with the 3 × 3 board as the base ase| a ir uit of the board with the omitted square.Solutions to Exer ises 171 9. Constru t ir uits of these three boards. 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. For larger boards.m = even. a ir uit an be onstru ted exa tly when the omitted square is not adja ent to a orner square. The other three boards ea h have an even number of squares. onne t the ir uits together as shown in .n .) Then a ir uit an be onstru ted of the remaining squares exa tly when even. and |indu tively. and at least one of them has at least one square. Suppose the oordinates of the omitted square are (m.

17 . omitting one of the squares. n v h c n v h c n v v n h c c h h c n v c h v n Table 7: Sequential Composition of Flip Operations Property (9.7) is veri.6 .g. 2 9. odd even odd even even Figure 17: Straight-move ir uits (shown in red) of a 3 × 3 board.

Veri.ed by observing that the table is symmetri about the top-left to bottom-right diagonal.

The ase that x . 2008 . May 28. y or z is n an be dealt with simply. ation of the asso iativity property is mu h more tedious. This leaves 27 other ases to Algorithmi Problem Solving Roland Ba khouse.

but straightforward" proof! 2 9. it suÆ es to understand how all the entries are generated by a small set of primitive transformations. (To be ompleted. n r a c n r a c n r r c a n c a a n c r c a r n Table 8: Sequential Composition of Rotation Operations There are 24 dierent ways to assign a dierent olour to ea h of the squares in a 2 × 2 board. so that the size of the full table is 24 × 24 ! Rather than omplete the full table. This is an example of a \tedious.Solutions to Exer ises 172 onsider.9 .) 2 See .

18. all that is required is to \shorten" the straight-move ir uits in order to a ommodate the smaller board. In the ir uit shown. Note how the hoi e of parallel moves onstrains the hoi e of red ir uit. a substantial number of ir uits an be found all based on the same set of ombining parallel moves.10 2 9. and solid bla k lines. the red moves are entirely di tated by this hoi e. depi ting the diagonal moves. Moves indi ated by dotted lines are repla ed by the diagonal moves indi ated by solid bla k lines. Figure 20 shows the ir uits obtained in this way. In this way. (But note that they annot be used to onstru t a ir uit of a 6 × 8 board. the green. The parallel moves used to onne t ir uits of dierent olours are indi ated by dotted lines depi ting the straight moves.g. There is some freedom in hoosing a green ir uit. it suÆ es to use the te hnique detailed in . In ontrast. In order to onstru t a ir uit for any board of size 4m × 2n . In fa t. blue and yellow ir uits were onstru ted by \ opying" the red ir uit. there is omplete freedom in hoosing a blue or yellow ir uit.) 9. these are the moves that are repla ed. but not omplete freedom. The dotted lines are not part of the ir uit.11 Figure 19 shows details of how the straight-move ir uits are ombined. The same set of ombining parallel moves an be used to onstru t a ir uit of an 8 × 6 board. where m is at least 2 and n is at least 3 .

3 for Algorithmi Problem Solving Roland Ba khouse. 2008 .gs. 9. May 28.2 and 9.

on e for ea h of the straight-move ir uits in the solution to the 8 × 6 -board problem.Solutions to Exer ises 173 Figure 18: A Knight's Cir uit.12 We begin by identifying the moves shown in . 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. This onstru tion has to be applied four times. Solid lines indi ate the ir uit. 2 9. extending straight-move ir uits to boards of arbitrary size.

14.g. See . 9.

) Now it is easy to . (Note the symmetry. 21.g.

See .ll in the straight-move ir uits around the remaining squares.

it is easy to extend the straight-move ir uits. May 28. For the general problem. 2008 .g. 22. 2 Algorithmi Problem Solving Roland Ba khouse.

the straight moves indi ated by dotted lines are repla ed by diagonal moves indi ated by solid bla k lines.Solutions to Exer ises 174 Figure 19: Details of how the four straight-move ir uits are ombined. 2008 . May 28.) Algorithmi Problem Solving Roland Ba khouse.

(Dotted lines are not part of the ir uit. as detailed in . these are the moves that are repla ed by diagonal moves.Solutions to Exer ises 175 Figure 20: Knight's Cir uit of an 8 × 6 and an 8 × 8 board.

May 28. 19.g.) Algorithmi Problem Solving Roland Ba khouse. 2008 .

Algorithmi Problem Solving Roland Ba khouse. Straight moves are oloured. The dotted lines represent the moves that are repla ed. Figure 22: Knight's Cir uit of a 6×6 board. Diagonal moves are shown in bla k.Solutions to Exer ises 176 Figure 21: Details of Combining Cir uits. May 28. The dotted lines do not form part of the ir uit. 2008 . Solid lines represent the moves that repla e them.

Cal ulating Implementations From Spe i.Bibliography [Ba 03℄ [Ba 07℄ Program Constru tion.

cs. Guy. s. John Wiley Sons.. W. and Ri hard K.cs. 10:243{244. Roland Ba khouse. Roland Ba khouse. [BF01℄ Roland Ba khouse and Maarten Fokkinga. EWD1260: The marked oins and http://www. Springer-Verlag. Septem- ber 1992. The asso iativity of equivalen e Information Pro essing Letters. Dijkstra. tour.cs. volume I and II. 1990. Septem- ber 1990. August 2007. Problem- Prenti e Hall. the s ale. Buneman and L.PDF. 77:71{ 76. Ltd. 177 May 28. [Dij97℄ Edsger W. Dijkstra. [BCG82℄ Elwyn R. Mar h 1997. Semanti s.utexas.a . EWD1135: The knight's http://www. EWD1083: The Information Pro- balan e and the http://www. 2003. 2008 . [DW00℄ John P. D'Angelo and Douglas B. [DS90℄ Edsger W.edu/users/EWD/ewd10xx/EWD1083. A ademi Press. Winning Ways. 2000. Solving and Proofs. Roland Ba khouse. West. S holten. [Dij92℄ Edsger W. Conway. John H. Available at http://www. [Dij90℄ Edsger essing Letters. 2001.PDF. Predi ate Cal ulus and Program Texts and monographs in Computer S ien e. The Towers of Hanoi Problem. [BL80℄ P. Dijkstra and Carel S.edu/users/EWD/ewd12xx/EWD1260.uk/ r b/MPC/papers.utexas. The tor h problem.PDF. 1980. and the Towers of Hanoi Problem. Levy. Berlekamp.utexas. 1982.edu/users/EWD/ewd11xx/EWD1135. oins.nott. ations. Algorithmi Problem Solving Mathemati al Thinking. Dijkstra.

Springer-Verlag. Bulletin of the European Asso- . to The Design And Analysis of Algorithms [Rot02℄ Gunter Rote. . N. 2003. Translated by J. O tober 2002. Metamathemati s. A Logi al Approa h to Dis rete Math . papers from 1938. Introdu tion Addison Wesley.Woodger. Springer-Verlag. [GS93℄ David Gries and Fred B. What Is Englewood Clis. [Tar56℄ Alfred Tarski. S hneider. Crossing the bridge at night.H. 1978. The S ien e of Programming [Lev03℄ Anany Levitin.J. Semanti s. The Name Of This Book? Prenti e-Hall. 1993. 78:241{246.Solutions to Exer ises 178 [Gri81℄ David Gries. iation for Theoreti al Computer S ien e [Smu78℄ Raymond Smullyan. The Magi al Maze . Oxford University Press. 1981. [Ste97℄ Ian Stewart. Weiden. .. 1956. Logi .

A de. London. 1997.eld and Ni olson.G. Wiltink. [Wil87℄ J.

25:233{234. 1923 to Information Pro essing May 28. ien y of natural dedu tion. 2008 . Algorithmi Problem Solving Roland Ba khouse. 1987. Letters.

- Freedom to Innovate
- StudentSlides_ProblemSolving
- Lecture 1 - Realfasfsdfas
- Completed Warm Up
- Bad News Letter
- An Introduction to IIFEs - Immediately Invoked Function Expressions
- Hrm Project
- An Interview is Conducted by the Interviewer So as to Fill Their Need of the Right Employee
- Meetings
- fedo assesment
- Algo Problem Solving
- PDF Introduction to Algorithms Lesson Plan
- Problem Solving
- negative letter
- 2-8
- play
- 02 Systematic Problem Solving
- lz term iii math lesson rev
- APC 100 167 YC,YF,YS Lecture 9 Ch 10 Group Problem Solving Posted
- Subordinate Styles
- Tell Me About Yourself
- Bain
- 28bcaHow Good Are You at Solving Problems
- rubric of the collaborative work skills
- 7 Step to Solving
- Chess Benefits Overview
- webquest rubric
- LEAD Test
- Key Selection Criteria.pdf
- t

Skip carousel

- UT Dallas Syllabus for cs3333.001.11s taught by Jeyakesavan Veerasamy (veerasam)
- Improving Accuracy of Text Classification for SMS Data
- Adoption of Parallel Genetic Algorithms for the Solution of System of Equations
- Retiming Of Delayed Least Mean Square Algorithm for Adaptive Filter
- UT Dallas Syllabus for cs6363.002.08s taught by Balaji Raghavachari (rbk)
- Appraisal of PSO Algorithm over Genetic Algorithm in WSN Using NS2
- Tmp 8232
- Clustering on Uncertain Data
- Analysis & Design Algorithm MCQ'S
- Cluster Analysis Techniques in Data Mining
- UT Dallas Syllabus for cs4349.501 06f taught by Ramaswamy Chandrasekaran (chandra)
- Hashing Algorithm
- UT Dallas Syllabus for opre7313.001.08f taught by Milind Dawande (milind)
- A Survey on Gesture Recognition
- tmpDF60.tmp
- Help the Genetic Algorithm to Minimize the Urban Traffic on Intersections
- tmpC0CF.tmp
- Determining the shortest path for Travelling Salesman Problem using Nearest Neighbor Algorithm
- Comparative Analysis of Optimization Algorithms Based on Hybrid Soft Computing Algorithm
- tmp904.tmp
- tmpAA72
- Principles of parallel algorithm models and their objectives
- UT Dallas Syllabus for se3345.502.07f taught by Ivor Page (ivor)
- As 2805.5.1-1992 Electronic Funds Transfer - Requirements for Interfaces Ciphers - Data Encipherment Algorith
- UT Dallas Syllabus for cs3345.501 05s taught by Greg Ozbirn (ozbirn)
- tmpCB3F.tmp
- Public Cloud Partition Using Load Status Evaluation and Cloud Division Rules
- Improved Positive and Negative Quantitative Association Rule Mining using SAM
- A review on Development of novel algorithm by combining Wavelet based Enhanced Canny edge Detection and Adaptive Filtering Method for Human Emotion Recognition
- UT Dallas Syllabus for cs2305.002 05f taught by Timothy Farage (tfarage)

Skip carousel

- UT Dallas Syllabus for ce6354.001.08f taught by Rym Mili (rmili)
- UT Dallas Syllabus for cs6354.001.07s taught by Rym Mili (rmili)
- As NZS ISO IEC 19759-2007 Software Engineering - Guide to the Software Engineering Body of Knowledge (SWEBOK)
- Purifying Test Cases for Improving Fault Localization
- Software Engineering
- UT Dallas Syllabus for cs3354.002 05f taught by Anthony Sullivan (sulliva)
- UT Dallas Syllabus for cs6354.001 06f taught by Rym Mili (rmili)
- Implementation of a new Size Estimation Model
- zen.digital Announces BuzzJS 2.0 Conference for January 27th, 2017
- UT Dallas Syllabus for cs3354.501 05f taught by Hieu Vu (hdv013000)
- The 6th Underhanded C Contest - ObsessBook

Sign up to vote on this title

UsefulNot usefulRead Free for 30 Days

Cancel anytime.

Close Dialog## Are you sure?

This action might not be possible to undo. Are you sure you want to continue?

Close Dialog## This title now requires a credit

Use one of your book credits to continue reading from where you left off, or restart the preview.

Loading