Table Of ContentUndergraduate Topics in Computer Science
Undergraduate Topics in Computer Science (UTiCS) delivers high-quality instructional content for
undergraduates studying in all areas of computing and information science. From core foundational
andtheoreticalmaterialtofinal-yeartopicsandapplications,UTiCSbookstakeafresh,concise,and
modern approach and are ideal for self-study or for a one- or two-semester course. The texts are
all authored by established experts in their fields, reviewed by an international advisory board, and
containnumerousexamplesandproblems.Manyincludefullyworkedsolutions.
For furthervolumes:
http://www.springer.com/series/7592
Tom Jenkyns (cid:129) Ben Stephenson
Fundamentals of Discrete
Math for Computer
Science
A Problem-Solving Primer
TomJenkyns BenStephenson
DepartmentofMathematics DepartmentofComputerScience
BrockUniversity UniversityofCalgary
ON,Canada AB,Canada
ISSN1863-7310
ISBN978-1-4471-4068-9 ISBN978-1-4471-4069-6(eBook)
DOI10.1007/978-1-4471-4069-6
SpringerLondonHeidelbergNewYorkDordrecht
LibraryofCongressControlNumber:2012945303
#Springer-VerlagLondon2013
Thisworkissubjecttocopyright.AllrightsarereservedbythePublisher,whetherthewholeorpartof
thematerialisconcerned,specificallytherightsoftranslation,reprinting,reuseofillustrations,recitation,
broadcasting,reproductiononmicrofilmsorinanyotherphysicalway,andtransmissionorinformation
storageandretrieval,electronicadaptation,computersoftware,orbysimilarordissimilarmethodology
nowknownorhereafterdeveloped.Exemptedfromthislegalreservationarebriefexcerptsinconnection
withreviewsorscholarlyanalysisormaterialsuppliedspecificallyforthepurposeofbeingenteredand
executed on a computer system, for exclusive use by the purchaser of the work. Duplication of this
publicationorpartsthereofispermittedonlyundertheprovisionsoftheCopyrightLawofthePublisher’s
location,initscurrentversion,andpermissionforusemustalwaysbeobtainedfromSpringer.Permissions
forusemaybeobtainedthroughRightsLinkattheCopyrightClearanceCenter.Violationsareliableto
prosecutionundertherespectiveCopyrightLaw.
Theuseofgeneraldescriptivenames,registerednames,trademarks,servicemarks,etc.inthispublication
doesnotimply,evenintheabsenceofaspecificstatement,thatsuchnamesareexemptfromtherelevant
protectivelawsandregulationsandthereforefreeforgeneraluse.
Whiletheadviceandinformationinthisbookarebelievedtobetrueandaccurateatthedateofpublication,
neithertheauthorsnortheeditorsnorthepublishercanacceptanylegalresponsibilityforanyerrorsor
omissionsthatmaybemade.Thepublishermakesnowarranty,expressorimplied,withrespecttothe
materialcontainedherein.
Printedonacid-freepaper
SpringerispartofSpringerScience+BusinessMedia(www.springer.com)
Preface
Thisbookisdirectedtocomputersciencestudentsatthebeginningoftheirstudies.
Itpresentstheelementsofdiscretemathematicsinaformaccessibletothemandin
awaythatwillimprovetheirprogrammingcompetence.Itfocusesonthosetopics
withdirectrelevancetocomputerscience.
Mostchaptersareintroducedwithanexamplethatsetsthestageforitscontent.
Chapter1beginswithasettingforthewholebook:Howdoyoudesignalgorithms
tosolvestandardcomputingproblems?Howdoyouknowyouralgorithmwillwork
correctlyforeverysuitableinput?Howlongwillyouralgorithmtaketogenerateits
output?
Inourview,teachingismuchmorethanpresentingcontent,andwehavewritten
thistextasadesignfortheexperienceofstudentswithoursubject.Humanminds
attachtagstoinformationindicatingitssignificancewhicharedeterminedbytheir
experienceatthepointofacquisition.Wewantthatexperiencetobepositive.
Ourtextempowersstudentstothinkcritically,tobeeffective problemsolvers,
to integrate theory and practice, and to recognize the importance of abstraction.
It engages them with memorable, motivating examples. It challenges them with
many new ideas, methods, and rigorous thinking. And we hope it entertains them
likenoothertextbookwith“Math”initstitle.
This book introduces much of the “culture” of computer science and the com-
monknowledgesharedbyallcomputerscientists(beyondprogramming).Muchof
itisdevotedtothebasicsolutionstofundamentalproblemsallcomputerscientists
know: how to search a list for a particular target; how to sort a list into a natural
order for searching; how to generate all objects, subsets, or sequences of some
kind in such an order; how to traverse all of the nodes in a graph; and especially,
how to compare the efficiency of algorithms and how to prove their correctness.
Our constant theme is the relevance of the mathematics we present to computer
science.
Perhaps the most distinguishing feature of this text is its informal and inter-
active nature. Detailedwalkthroughsofalgorithms appearfrombeginningtoend.
We try to motivate material by inserting provocative questions and commentary
into the prose. We try to simulate a conversation with our reader, more like our
lecturesandlesslikeotherpedanticandponderousmathematicstexts.Weemploy
the symbol “//” to indicate “comments,” to indicate asides (especially expanding
and explaining mathematical arguments), as signals of what comes next, and as
v
vi Preface
prompts ofnatural questionswe wanttoraise inthe reader’s mind. We have kept
thetext(words,sentences,andparagraphs)shortandtothepoint,andwehaveused
font changes, bold, italic, and underlining to capture, focus, and recapture our
reader’sattention.
But this book is a mathematics text. We attempt to expand students’ intuition
with precise mathematical language and ideas. Although detailed proofs are
delayeduntilChap.3,thefundamentalnatureofproofinmathematicsisexplained,
maintained,andappliedrepeatedlytoprovingthecorrectnessofalgorithms.Oneof
thepurposesofthetextistoprovideatoolboxofusefulalgorithmssolvingstandard
problemsincomputerscience.Theotherpurposeistoprovideacatalogueofuseful
concepts without which the underlying theory of computer science cannot be
understood.
Acknowledgments
Wewishtoacknowledgethesupportandencouragementofourfriendsandfamilies
throughoutthelonggestationofthisbook,especiallyEleanor,Glenys,Janice,and
Flora.Wearealsogratefultothedepartmentsofmathematicsandcomputerscience
atBrockUniversitywhoindulgedtheexperimentationinformandcontentofthis
project over two decades of “class testing.” Finally, we are indebted to Eric R.
MullerwhoinitiatedtheprototypecourseatBrock.
vii
Contents
1 Algorithms,Numbers,andMachines.................................. 1
1.1 WhatIsanAlgorithm?.............................................. 4
1.2 IntegerAlgorithmsandComplexity................................ 8
1.2.1 PrimeTesting ............................................... 9
1.2.2 RealNumbers............................................... 11
1.2.3 MorePrimeTesting......................................... 12
1.2.4 PrimeFactorization......................................... 14
1.2.5 Logarithms.................................................. 16
1.2.6 GreatestCommonDivisor.................................. 18
1.3 MachineRepresentationofNumbers............................... 21
1.3.1 ApproximationErrors....................................... 23
1.3.2 Base2,8,and16............................................ 26
1.4 NumericalSolutions ................................................ 35
1.4.1 Newton’sMethodforSquareRoots........................ 35
1.4.2 TheBisectionAlgorithm.................................... 37
Exercises.................................................................. 41
2 Sets,Sequences,andCounting ......................................... 43
2.1 Na¨ıveSetTheory ................................................... 43
2.1.1 TheDiabolicalLibrarian.................................... 46
2.1.2 OperationsonSetsandCardinality......................... 47
2.1.3 ThePigeonholePrinciple................................... 49
2.2 Sequences........................................................... 51
2.2.1 TheCharacteristicSequenceofaSubset................... 53
2.3 Counting............................................................. 54
2.3.1 Numberofk-Sequencesonann-Set........................ 55
2.3.2 NumberofSubsetsofann-Set............................. 56
2.3.3 Numberofk-Permutationsonann-Set..................... 56
2.3.4 n-Factorial................................................... 57
2.3.5 Numberofk-Subsetsofann-Set........................... 58
2.3.6 Pascal’sTriangle............................................ 61
2.3.7 CountingAlgorithmically(WithoutaFormula)............ 64
ix