Roland Backhouse

ALGORITHMIC PROBLEM SOLVING

LY

Contents Preface

Algorithmic Problem Solving

C H A P T E R 1 – Introduction

C H A P T E R 2 – Invariants

2.3 2.4 2.5 2.6

O

H AP

2.2

Chocolate Bars 2.1.1 The Solution 2.1.2 The Mathematical Solution Empty Boxes 2.2.1 Review The Tumbler Problem 2.3.1 Non-deterministic Choice Tetrominoes Summary Bibliographic Remarks

C

2.1

3

Algorithms Algorithmic Problem Solving Overview Bibliographic Remarks

TE R

1.1 1.2 1.3 1.4

1

N

PART I

xi

3 4 5 6

7 10 10 11 16 19 22 23 24 30 34

C H A P T E R 3 – Crossing a River

35

3.1 3.2

36 37 37 39 41 42 42 43 44 45 48 50 54 63 65 65

PL

E

Problems Brute Force 3.2.1 Goat, Cabbage and Wolf 3.2.2 State-Space Explosion 3.2.3 Abstraction Nervous Couples 3.3.1 What Is the Problem? 3.3.2 Problem Structure 3.3.3 Denoting States and Transitions 3.3.4 Problem Decomposition 3.3.5 A Review Rule of Sequential Composition The Bridge Problem Conditional Statements Summary Bibliographic Remarks

SA

M

3.3

3.4 3.5 3.6 3.7 3.8

CONTENTS 67

4.1 4.2

67 69 69 70 72 74 78 79 81 82 83 87 91 92

4.5 4.6

C H A P T E R 5 – Knights and Knaves

PL

E

5.4

M

5.5 5.6

N

H AP

5.3

Logic Puzzles Calculational Logic 5.2.1 Propositions 5.2.2 Knights and Knaves 5.2.3 Boolean Equality 5.2.4 Hidden Treasures 5.2.5 Equals for Equals Equivalence and Continued Equalities 5.3.1 Examples of the Associativity of Equivalence 5.3.2 On Natural Language Negation 5.4.1 Contraposition 5.4.2 Handshake Problems 5.4.3 Inequivalence Summary Bibliographic Remarks

C

5.1 5.2

O

4.3 4.4

Matchstick Games Winning Strategies 4.2.1 Assumptions 4.2.2 Labelling Positions 4.2.3 Formulating Requirements Subtraction-Set Games Sums of Games 4.4.1 A Simple Sum Game 4.4.2 Maintain Symmetry! 4.4.3 More Simple Sums 4.4.4 Evaluating Positions 4.4.5 Using the Mex Function Summary Bibliographic Remarks

LY

C H A P T E R 4 – Games

TE R

vi

95 95 96 96 97 98 100 101 102 104 105 106 109 112 113 117 117

119

6.1 6.2 6.3 6.4 6.5 6.6

120 123 126 128 129 130

SA

C H A P T E R 6 – Induction Example Problems Cutting the Plane Triominoes Looking for Patterns The Need for Proof From Verification to Construction

CONTENTS 134 134

C H A P T E R 7 – Fake-Coin Detection

137

7.1 7.2

137 139 139 139 140 141 146 146

8.2 8.3 8.4 8.5

Specification and Solution 8.1.1 The End of the World! 8.1.2 Iterative Solution 8.1.3 Why? Inductive Solution The Iterative Solution Summary Bibliographic Remarks

H AP

8.1

TE R

C H A P T E R 8 – The Tower of Hanoi

N

7.3 7.4

Problem Formulation Problem Solution 7.2.1 The Basis 7.2.2 Induction Step 7.2.3 The Marked-Coin Problem 7.2.4 The Complete Solution Summary Bibliographic Remarks

LY

Summary Bibliographic Remarks

O

6.7 6.8

147 147 147 148 149 149 153 156 156

C H A P T E R 9 – Principles of Algorithm Design

157

9.1 9.2 9.3 9.4

158 160 163 166 170 174 176 177 178 180

PL

E

C

Iteration, Invariants and Making Progress A Simple Sorting Problem Binary Search Sam Loyd’s Chicken-Chasing Problem 9.4.1 Cornering the Prey 9.4.2 Catching the Prey 9.4.3 Optimality Projects Summary Bibliographic Remarks

M

9.5 9.6 9.7

183

10.1 10.2 10.3 10.4 10.5

183 185 187 189 193

SA

C H A P T E R 10 – The Bridge Problem Lower and Upper Bounds Outline Strategy Regular Sequences Sequencing Forward Trips Choosing Settlers and Nomads

vii

CONTENTS 10.6 10.7 10.8

The Algorithm Summary Bibliographic Remarks

196 199 200

C H A P T E R 11 – Knight’s Circuit

201

11.1 11.2 11.3 11.4 11.5

202 206 209 216 218

PART II Mathematical Techniques C H A P T E R 12 – The Language of Mathematics

12.4

M

TE R

H AP

PL

12.5

C

12.3

Variables, Expressions and Laws Sets 12.2.1 The Membership Relation 12.2.2 The Empty Set 12.2.3 Types/Universes 12.2.4 Union and Intersection 12.2.5 Set Comprehension 12.2.6 Bags Functions 12.3.1 Function Application 12.3.2 Binary Operators 12.3.3 Operator Precedence Types and Type Checking 12.4.1 Cartesian Product and Disjoint Sum 12.4.2 Function Types Algebraic Properties 12.5.1 Symmetry 12.5.2 Zero and Unit 12.5.3 Idempotence 12.5.4 Associativity 12.5.5 Distributivity/Factorisation 12.5.6 Algebras Boolean Operators 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

E

12.1 12.2

SA

12.6 12.7

N

LY

Straight-Move Circuits Supersquares Partitioning the Board Summary Bibliographic Remarks

O

viii

219 221 222 224 224 224 224 225 225 227 227 228 230 230 232 233 235 236 237 238 239 240 241 243 244 246 247 248 249 249 251 252

CONTENTS

12.9

255 256 257 259 260 262 264

LY

12.8

12.7.7 Equality 12.7.8 Equivalence Relations Calculations 12.8.1 Steps in a Calculation 12.8.2 Relations between Steps 12.8.3 ‘‘If’’ and ‘‘Only If’’ Exercises

13.6 13.7

H AP

C H A P T E R 14 – Quantifiers

O

267 269 270 271 274 275 276 279 281

285 285 286 287 289 291 297 297 298 300 301 302 303 303 303 304 304 304 306

C H A P T E R 15 – Elements of Number Theory

309

15.1 15.2 15.3 15.4

309 312 315 316

14.3

SA

M

PL

14.4

C

DotDotDot and Sigmas Introducing Quantifier Notation 14.2.1 Summation 14.2.2 Free and Bound Variables 14.2.3 Properties of Summation 14.2.4 Warning Universal and Existential Quantification 14.3.1 Universal Quantification 14.3.2 Existential Quantification Quantifier Rules 14.4.1 The Notation 14.4.2 Free and Bound Variables 14.4.3 Dummies 14.4.4 Range Part 14.4.5 Trading 14.4.6 Term Part 14.4.7 Distributivity Properties Exercises

E

14.1 14.2

267

Boolean Equality Negation Disjunction Conjunction Implication 13.5.1 Definitions and Basic Properties 13.5.2 Replacement Rules Set Calculus Exercises

TE R

13.1 13.2 13.3 13.4 13.5

N

C H A P T E R 13 – Boolean Algebra

14.5

Inequalities Minimum and Maximum The Divides Relation Modular Arithmetic

ix

CONTENTS

15.5

15.4.1 Integer Division 15.4.2 Remainders and Modulo Arithmetic Exercises

316 320 322

325

16.1 16.2

325 328 330 332 334 338 339 341 341 343 344 345 347 351 353 354 355 357 358 359 361 366

16.8

16.9

PL

E

Solutions to Exercises References

SA

M

Index

N O

TE R

16.5 16.6 16.7

H AP

16.3 16.4

Paths in a Directed Graph Graphs and Relations 16.2.1 Relation Composition 16.2.2 Union of Relations 16.2.3 Transitive Closure 16.2.4 Reflexive Transitive Closure Functional and Total Relations Path-Finding Problems 16.4.1 Counting Paths 16.4.2 Frequencies 16.4.3 Shortest Distances 16.4.4 All Paths 16.4.5 Semirings and Operations on Graphs Matrices Closure Operators Acyclic Graphs 16.7.1 Topological Ordering Combinatorics 16.8.1 Basic Laws 16.8.2 Counting Choices 16.8.3 Counting Paths Exercises

LY

C H A P T E R 16 â&#x20AC;&#x201C; Relations, Graphs and Path Algebras

C

x

369 405 407