Algorithmic Problem Solving Roland Backhouse University ofNottingham @WILEY AJohnWiley&Sons.Ltd..Publication Thiseditionfirstpublished2011 ©2011JohnWiley86SonsLtd. Registeredoffice JohnWiley8:SonsLtd.,TheAtrium,SouthernGate,Chichester,WestSussex,P019SSQ,UnitedKingdom Fordetailsofourglobaleditorialoffices,forcustomerservicesandforinformationabouthowtoapplyfor permissiontoreusethecopyrightmaterialinthisbookpleaseseeourwebsiteatwww.wiley.com. TherightsofRolandBackhousetobeidentifiedastheauthorofthisworkhasbeenassertedinaccordancewith theCopyright,DesignsandPatentsAct1988. Allrightsreserved.Nopartofthispublicationmaybereproduced,storedinaretrievalsystem,ortransmitted,in anyformorbyanymeans,electronic,mechanical,photocopying,recordingorotherwise,exceptaspermittedby theUKCopyright,DesignsandPatentsAct1988,withoutthepriorpermissionofthepublisher. Wileyalsopublishesitsbooksinavarietyofelectronicformats.Somecontentthatappearsinprintmaynotbe availableinelectronicbooks. Designationsusedbycompaniestodistinguishtheirproductsareoftenclaimedastrademarks.Allbrandnames andproductnamesusedinthisbookaretradenames,servicemarks,trademarksorregisteredtrademarksof theirrespectiveowners.Thepublisherisnotassociatedwithanyproductorvendormentionedinthisbook.This publicationisdesignedtoprovideaccurateandauthoritativeinformationinregardtothesubjectmattercovered. Itissoldontheunderstandingthatthepublisherisnotengagedinrenderingprofessionalservices.Ifprofessional adviceorotherexpertassistanceisrequired,theservicesofacompetentprofessionalshouldbesought. LibraryofCongressCataloging-in-PublicationData Backhouse,RolandC.,1948- Algorithmicproblemsolving/RolandBackhouse. p.cm. Includesbibliographicalreferencesandindex. ISBN978-0-470-68453-5(pbk.:alk.paper) 1.Computeralgorithms.2.Problemsolving.I.Title. QA76.9.A43B342011 005.1-ch3 2011022716 AcataloguerecordforthisbookisavailablefromtheBritishLibrary. Setin9/12SabonbyLaserwordsPrivateLtd.,Chennai,India. PrintedinGreatBritainbyT]International,Padstow,Cornwall CONTENTS CHAPTER 4 - Games 4.1 Matchstick Games 4.2 WinningStrategies 4.2.1 Assumptions 4.2.2 LabellingPositions 4.2.3 FormulatingRequirements 4.3 Subtraction-SetGames 4.4 SumsofGames 4.4.1 A Simple SumGame 4.4.2 Maintain Symmetry! 4.4.3 More Simple Sums 4.4.4 EvaluatingPositions 4.4.5 UsingtheMexFunction 4.5 Summary 4.6 BibliographicRemarks CHAPTER 5 - Knights and Knaves 5.1 Logic Puzzles 5.2 Calculational Logic 5.2.1 Propositions 5.2.2 Knights andKnaves 5.2.3 Boolean Equality 5.2.4 HiddenTreasures 5.2.5 Equals forEquals 5.3 Equivalence and Continued Equalities 5.3.1 Examples oftheAssociativityofEquivalence 5.3.2 On Natural Language 5.4 Negation 5.4.1 Contraposition 5.4.2 Handshake Problems 5.4.3 Inequivalence 5.5 Summary 5.6 BibliographicRemarks CHAPTER 6 - Induction 6.1 Example Problems 6.2 Cuttingthe Plane 6.3 Triominoes 6.4 LookingforPatterns 6.5 TheNeed forProof 6.6 FromVerification to Construction CONTENTS 10.6 TheAlgorithm 10.7 Summary 10.8 Bibliographic Remarks CHAPTER 11 - Knight's Circuit 11.1 Straight-Move Circuits 11.2 Supersquares 11.3 Partitioningthe Board 11.4 Summary 11.5 Bibliographic Remarks PART II Mathematical Techniques CHAPTER 12 - The Language of Mathematics 12.1 Variables, Expressions and Laws 12.2 Sets 12.2.1 The Membership Relation 12.2.2 The Empty Set 12.2.3 Types/Universes 12.2.4 Unionand Intersection 12.2.5 Set Comprehension 12.2.6 Bags 12.3 Functions 12.3.1 FunctionApplication 12.3.2 Binary Operators 12.3.3 OperatorPrecedence 12.4 TypesandType Checking 12.4.1 CartesianProductand DisjointSum 12.4.2 FunctionTypes 12.5 AlgebraicProperties 12.5.1 Symmetry 12.5.2 Zero andUnit 12.5.3 Idempotence 12.5.4 Associativity 12.5.5 Distributivity/Factorisation 12.5.6 Algebras 12.6 Boolean Operators 12.7 Binary Relations 12.7.1 Reflexivity 12.7.2 Symmetry 12.7.3 Converse 12.7.4 Transitivity 12.7.5 Anti-symmetry 12.7.6 Orderings CONTENTS 15.4.1 IntegerDivision 15.4.2 Remainders and ModuloArithmetic 15.5 Exercises CHAPTER 16 - Relations. Graphs and Path Algebras 16.1 Pathsin aDirected Graph 16.2 Graphs and Relations 16.2.1 Relation Composition 16.2.2 UnionofRelations 16.2.3 TransitiveClosure 16.2.4 ReflexiveTransitive Closure 16.3 Functional andTotalRelations 16.4 Path-FindingProblems 16.4.1 CountingPaths 16.4.2 Frequencies 16.4.3 ShortestDistances 16.4.4 AllPaths 16.4.5 Semirings and Operations onGraphs 16.5 Matrices 16.6 Closure Operators 16.7 Acyclic Graphs 16.7.1 Topological Ordering 16.8 Combinatorics 16.8.1 Basic Laws 16.8.2 CountingChoices 16.8.3 CountingPaths 16.9 Exercises Solutions to Exercises References Index Preface In the modern, developed world our lifestyles, our livelihoods and even our lives have become highly dependent oncomputer technology in a way thatwould have been unimaginablefiftyyearsago.Underpinningthedevelopmentsthathavemadethispossible hasbeenafundamentalchangeinthenatureofourproblem-solvingskills:inmanycases, the solution to a difficult and challenging problem has to be formulated as a computer program to be executed by a dumb machine. Not only have the size and complexity of theproblemswetacklechangedbeyondmeasure,theprecisionandaccuracywithwhich oursolutions need to be formulated have undergone a revolution. Algorithmic problem solvingisabouttheskillsthatareneededto meetthe newchallengesthatwe face. This book is based on a first-year, first-semester module that was first introduced at the University of Nottingham in September 2003. Initially the module was optional but, in 2006, itwas made compulsory for all first-year Computer Science and Software Engineering students (as well as still being optional for students on other degree programmes) and has remained so ever since. The module is taught at each of the University’sthreecampuses in China,Malaysia and theUK. The aim of the book is to instill good problem-solving skills, particularly but not exclusively incaseswhere thesolutionto a probleminvolvesthedesignofanalgorithm. Theapproachisproblem-driven:themainpartofthebookconsistsofaseriesofexamples thatintroducetheprinciplesofalgorithmicproblemsolvingina systematicfashion.The use of a problem-driven approach is crucial to exciting students’ natural propensity to take on a challenge; however, examples without theory are meaningless, so the second part of the book is about the mathematics that underpins the principles. Boxes within thetextpointthereadertothemathematics thatis relevanttoa particularexample. The presentation deviates from traditional mathematical practice in a number ofways. The treatment of boolean algebra and logic, for example, is non-standard and some notationisalso non-standard.These deviationsare, however, basedondevelopments in algorithmic problem solving that have been well understood for at least twenty years. (Althoughtwentyyearsisaveryshorttime-spaninthedevelopmentofmathematics,itisa relativelylongperiodinthemoderncomputerage.)Potentialteacherswhoarenotalready convinced orunfamiliarwith thematerialare askedto approach itwithanopen mind. The majority of the book is class-tested but not all, and not all at the level ofthe first year,firstsemesterofanundergraduatedegree. ForthreeyearsItaughtthemathematical techniques (Part II ofthe book) in a module that was given in the same semester as the PREFACE moduleonalgorithmicproblemsolving(PartI),butthatisnolongerthecase.Also,both parts of the book cover more material than I am able to cover in any one year. Some topics (such as boolean algebra) could be presented at pre-university level but some (in the laterchapters) are betterpostponed to lateryears. Havingan excess ofexample problems has the advantage ofofferinga choice. My own practice is to base assessment firmly on coursework that involves the students in active problem solving and which I vary from year to year so that no two years are the same. Model solutions to all exercises, which I have given to students for feedback purposes, are included at the end ofthe text. Solutions are omitted, however, for some exercises that I call “projects” and some projects that I set have not been included in the text. (Model solutions to the projects aregiven to the students in hard-copy form in orderto be abletoreuse themwithouttheriskofplagiarism.) Whenteachingatopicsuchasthis,itisveryimportantthattheexamplesarechallenging but within the grasp of the students. The problems I have chosen are most often ones thatthereadershouldbeabletosolveinabrute-forcefashionwithoutanymathematical knowledge but which can be solved much more effectively with the knowledge. The hidden agenda in the book is to engage the readerin the process ofdoing mathematics: theprocessofmodellingandcalculatingthesolutionstoproblemsevenwhenmathematics does notseemto bedirectlyrelevant. The presentation here is very strongly influenced by the writings of Edsger W. Dijk- stra (1930—2002). Dijkstra is renowned for his contributions to algorithm design. In addition, throughout his career he put much effort into trying to articulate and, by so doing, improve mathematical method. My own view is that his contribution to mathematical method is yet greater than his (phenomenal) contribution to computing science. OccasionallyI haveusedhiswords (orparaphrasesofhiswords) withoutdirect acknowledgement; like true proverbs, they deserve the acceptance thatonlycomes with anonymity. One such is Dijkstra’s definition of mathematics as “the art of effective reasoning” (see Chapter 12). I will never be able to emulate his problem-solving skills but I do hope that by trying to continue his work on mathematical method, I might encourage others tothinkmorecriticallyaboutanddeliberatelyarticulategoodandbad problem-solvingskills. I have received a lot of support from colleagues in the preparation of the book and I would like to thank them all. I would particularly like to thank Diethard Michaelis and David Gries, both ofwhom have given very detailed criticism and suggestions for improvement. Particular thanks also go to those who have helped with the teaching of the module: Siang Yew Chong and John Woodward have taught the module in MalaysiaandChina,respectively,forseveralyears.JoaoFerreira alsotaughtthemodule inNottinghamforoneyearwhenIwasonsabbaticalandhasassistedmeformanyyears with classes and with marking coursework and examinations, as have Wei Chen and
Description: