Table Of Content(cid:22)(cid:146)(cid:148)(cid:139)(cid:144)(cid:137)(cid:135)(cid:148)(cid:3)(cid:24)(cid:144)(cid:134)(cid:135)(cid:148)(cid:137)(cid:148)(cid:131)(cid:134)(cid:151)(cid:131)(cid:150)(cid:135)(cid:3)(cid:23)(cid:135)(cid:154)(cid:150)(cid:149)
(cid:3)(cid:139)(cid:144)(cid:3)(cid:16)(cid:131)(cid:150)(cid:138)(cid:135)(cid:143)(cid:131)(cid:150)(cid:139)(cid:133)(cid:149)(cid:3)(cid:131)(cid:144)(cid:134)(cid:3)(cid:23)(cid:135)(cid:133)(cid:138)(cid:144)(cid:145)(cid:142)(cid:145)(cid:137)(cid:155)
Series Editors
(cid:13)(cid:145)(cid:144)(cid:131)(cid:150)(cid:138)(cid:131)(cid:144) (cid:16)(cid:484) (cid:5)(cid:145)(cid:148)(cid:153)(cid:135)(cid:139)(cid:144)
(cid:11)(cid:135)(cid:142)(cid:137)(cid:135) (cid:11)(cid:145)(cid:142)(cid:134)(cid:135)(cid:144)
Editorial Board
(cid:15)(cid:139)(cid:149)(cid:131)(cid:3)(cid:10)(cid:145)(cid:142)(cid:134)(cid:132)(cid:135)(cid:148)(cid:137)
(cid:4)(cid:148)(cid:143)(cid:139)(cid:144)(cid:3)(cid:12)(cid:149)(cid:141)(cid:135)
(cid:19)(cid:131)(cid:142)(cid:142)(cid:135)(cid:3)(cid:8)(cid:484)(cid:23)(cid:484)(cid:3)(cid:13)(cid:145)(cid:148)(cid:137)(cid:135)(cid:144)(cid:149)(cid:135)(cid:144)
(cid:22)(cid:150)(cid:135)(cid:146)(cid:138)(cid:135)(cid:144)(cid:3)(cid:16)(cid:484)(cid:3)(cid:21)(cid:145)(cid:132)(cid:139)(cid:144)(cid:149)(cid:145)(cid:144)
(cid:4)(cid:142)(cid:135)(cid:154)(cid:131)(cid:144)(cid:134)(cid:135)(cid:148)(cid:3)(cid:22)(cid:138)(cid:135)(cid:144)
(cid:4)(cid:142)(cid:137)(cid:145)(cid:148)(cid:139)(cid:150)(cid:138)(cid:143)(cid:149)(cid:3)(cid:131)(cid:144)(cid:134)(cid:3)(cid:19)(cid:148)(cid:145)(cid:137)(cid:148)(cid:131)(cid:143)(cid:143)(cid:139)(cid:144)(cid:137)
(cid:19)(cid:148)(cid:145)(cid:132)(cid:142)(cid:135)(cid:143)(cid:149)(cid:3)(cid:131)(cid:144)(cid:134)(cid:3)(cid:22)(cid:145)(cid:142)(cid:151)(cid:150)(cid:139)(cid:145)(cid:144)(cid:149)
(cid:22)(cid:135)(cid:133)(cid:145)(cid:144)(cid:134)(cid:3)(cid:8)(cid:134)(cid:139)(cid:150)(cid:139)(cid:145)(cid:144)
ABC
AlexanderShen
Laboratoired’InformatiqueFondamentaledeMarseille(LIF)
CNRS,Universite´delaMe´diterrane´e,Universite´deProvence
CMI39RueJoliot-Curie
13453MarseilleCedex13
France
alexander.shen@lif.univ-mrs.fr
and
RussianAcademyofSciences
InstituteforInformationTransmissionProblems
BolshoyKaretnyper.19
Moscow,GSP-4,127994
Russia
SeriesEditors
JonathanM.Borwein,FRSC HelgeHolden
ProfessorLaureate DepartmentofMathematicalSciences
DirectorCentrefor NorwegianUniversityofScienceand
ComputerAssistedResearchMathematics Technology
anditsApplications,CARMA AlfredGetzvei1
SchoolofMathematical&PhysicalSciences NO-7491Trondheim
UniversityofNewcastle Norway
CallaghanNSW2308 holden@math.ntnu.no
Australia
Jonathan.Borwein@newcastle.edu.au
ISSN1867-5506 e-ISSN1867-5514
ISBN978-1-4419-1747-8 e-ISBN978-1-4419-1748-5
DOI10.1007/978-1-4419-1748-5
SpringerNewYorkDordrechtHeidelbergLondon
LibraryofCongressControlNumber:2009942420
MathematicsSubjectClassification(2000):65K05,65Yxx,90Cxx,68-01,68W40
1stedition: c Birkha¨user1997
Reprintof1⃝steditioninseries:‘ModernBirkha¨userClassics’ c Birkha¨user2008
⃝
2ndedition: c SpringerScience+BusinessMedia,LLC2010
⃝
Allrights reserved. Thisworkmaynotbetranslated orcopiedinwholeorinpartwithoutthewrittenper-
missionofthepublisher(SpringerScience+Business Media,LLC,233SpringStreet,NewYork,NY10013,
USA),exceptforbriefexcerptsinconnectionwithreviewsorscholarlyanalysis.Useinconnectionwithany
formofinformationstorageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilar
methodologynowknownorhereafterdevelopedisforbidden.
The use in this publication of trade names, trademarks, service marks, and similar terms, even if they are
notidentifiedassuch,isnottobetakenasanexpressionofopinionastowhetherornottheyaresubjectto
proprietaryrights.
Printedonacid-freepaper
SpringerispartofSpringerScience+BusinessMedia(www.springer.com)
TothememoryofAnnaPogossiants
Contents
Prefacetothesecondedition ....................................... xi
1 Variables,expressions,assignments ............................. 1
1.1 Problemswithoutarrays..................................... 1
1.2 Arrays.................................................... 15
1.3 Inductivefunctions ......................................... 28
2 Generationofcombinatorialobjects ............................ 33
2.1 Sequences................................................. 33
2.2 Permutations .............................................. 34
2.3 Subsets ................................................... 35
2.4 Partitions ................................................. 37
2.5 Graycodesandsimilarproblems.............................. 39
2.6 Someremarks ............................................. 44
2.7 Counting.................................................. 46
3 Treetraversal(backtracking) .................................. 49
3.1 Queensnotattackingeachother:positiontree................... 49
3.2 Treetraversal .............................................. 50
3.3 Queens:positiontreeimplementation.......................... 56
3.4 Backtrackinginotherproblems............................... 59
4 Sorting ..................................................... 61
4.1 Quadraticalgorithms........................................ 61
4.2 Sortinginn log noperations................................. 62
4.3 Applicationsofsorting ...................................... 69
4.4 Lowerboundforthenumberofcomparisons.................... 70
4.5 Problemsrelatedtosorting................................... 72
vii
viii Contents
5 Finite-statealgorithmsintextprocessing......................... 77
5.1 Compoundsymbols,comments,etc............................ 77
5.2 Numbersinput............................................. 79
6 Datatypes .................................................. 83
6.1 Stacks .................................................... 83
6.2 Queues ................................................... 89
6.3 Sets ...................................................... 97
6.4 Priorityqueues............................................. 100
7 Recursion................................................... 103
7.1 Examples ................................................. 103
7.2 Trees:recursiveprocessing .................................. 106
7.3 Thegenerationofcombinatorialobjects;search ................. 109
7.4 Otherapplicationsofrecursion ............................... 113
8 Recursiveandnon-recursiveprograms .......................... 119
8.1 Tableofvalues(dynamicprogramming) ....................... 119
8.2 Stackofpostponedtasks..................................... 123
8.3 Difficultcases ............................................. 126
9 Graphalgorithms ............................................ 129
9.1 Shortestpaths.............................................. 129
9.2 Connectedcomponents,breadth-anddepth-firstsearch........... 133
10 Patternmatching............................................. 139
10.1 Simpleexample............................................ 139
10.2 Repetitionsinthepattern .................................... 142
10.3 Auxiliarylemmas .......................................... 143
10.4 Knuth–Morris–Prattalgorithm................................ 144
10.5 Boyer–Moorealgorithm ..................................... 146
10.6 Rabin–Karpalgorithm ...................................... 148
10.7 Automataandmorecomplicatedpatterns....................... 149
10.8 Suffixtrees................................................ 156
11 Gamesanalysis .............................................. 167
11.1 Gameexamples ............................................ 167
11.2 Gamecost................................................. 169
11.3 Computingthegamecostbybacktracking...................... 176
11.4 Alpha-betapruning ......................................... 178
11.5 Aretrospectiveanalysis ..................................... 182
Contents ix
12 Optimalcoding .............................................. 185
12.1 Codes .................................................... 185
12.2 TheKraft–McMillaninequality............................... 186
12.3 Huffmancode ............................................. 189
12.4 Shannon–Fanocode ........................................ 191
13 Setrepresentation.Hashing.................................... 195
13.1 Hashingwithopenaddressing ................................ 195
13.2 Hashingusinglists ......................................... 198
14 Sets,trees,andbalancedtrees.................................. 203
14.1 Setrepresentationusingtrees................................. 203
14.2 Balancedtrees ............................................. 210
15 Context-freegrammars ....................................... 221
15.1 Generalparsingalgorithm ................................... 221
15.2 Recursive-descentparsing ................................... 226
15.3 ParsingalgorithmforLL(1)-grammars......................... 236
16 Left-to-rightparsing(LR) ..................................... 243
16.1 LR-processes .............................................. 243
16.2 LR(0)-grammars ........................................... 248
16.3 SLR(1)-grammars .......................................... 252
16.4 LR(1)-grammars,LALR(1)-grammars ......................... 253
16.5 Generalremarksaboutparsingalgorithms...................... 256
Furtherreading.................................................. 257
Appendix:CandPascalexamples .................................. 259
Index........................................................... 267
Preface to the Second Edition
Somebodyoncesaidthatonemayprovethecorrectnessofanalgorithm,butnotofa
program.Oneofthemaingoalsofthisbookistoconvincethereaderthatthingsare
notsobad.
A well-known programmer, C.A.R. Hoare, said that the beauty of a program is
notanadditionalbenefitbutacriterionthatseparatessuccessfromfailure.If,while
solvingproblemsinthisbook,youcometoappreciatethebeautyofawell-written
programwitheachpartinitscorrectplace,theauthor’sgoalwillhavebeenreached.
Theoreticallythisbookcanbeusedtostudyprogrammingwithoutacomputer:
onecouldwrite(correct)programswithpencilandpaper.Butinpracticetheability
toruntheprogramsisachallengeandarewardthatmakesprogrammingafun.
Wehaveutilizedtheproblem-solutionformat.Somechaptersarecollectionsof
problemshavingacommontopic,whileothersaredevotedtoonespecificalgorithm
(e.g.,chapter16coversLR(1)-parsing).Thechaptersaremoreorlessindependent,
buttheconcludingchaptersaremoredifficult.Chapters1–7covermaterialusually
includedinundergraduatecourseswhilechapters15–16aremoreappropriatefora
graduatecompilercourse.Ineachchapterwehavetriedtogiveproblemsatdifferent
levelsstartingwitheasyexercises.
Problems are usually provided with solutions, answers or hints. However, we
strongly recommend to read the solution only after the reader makes a good faith
attempttosolveitindependently.
The book is restricted to “micro-programming” leaving aside another very im-
portanttopic:howtosplittheprogramintoamanageablepartswithniceinterfaces
between them. (Probably this can be learned only by reading and modifying rather
largeprograms.)
Pascalisusedasaprogramminglanguage;thoughbeingoutdated,itisreason-
ably clear, so the readers familiar with any other procedural language (C, Modula,
Oberon,etc.)willencounternodifficulties.Forthereader’sconvenience,ashortap-
pendix is added that lists basic differences between Pascal and C. It is intended to
help the reader who knows C to understand the program notation in the book (but
cannotreplacetextbooksonC).
xi