ebook img

DISCRETE MATHEMATICS for Computer Science PDF

280 Pages·15.278 MB·English
Save to my drive
Quick download
Download
Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.

Preview DISCRETE MATHEMATICS for Computer Science

DISCRETE MATHEMATICS for Computer Science Alexander Golovnev, Alexander S. Kulikov, Vladimir V. Podolskii, Alexander Shen Welcome! Thankyoufordownloadingthisbook!ItsupplementstheIntroductiontoDiscreteMathematicsfor ComputerScience specializationatCourseraandcontainsmanyinteractivepuzzles,autograded quizzes,andcodesnippets.Theyareintendedtohelpyoutodiscoverimportantideasindiscrete mathematicsonyourown,andtoshowyoucorrespondingapplicationsoftheseideasincomputer science. This book contains material corresponding to the first two courses in the associated spe- cializationatCoursera:MathematicalThinkinginComputerScience andCombinatoricsand Probability . Futureeditionswillcovertheadditionalthreecourses: GraphTheory, Number TheoryandCryptography,andDeliveryProblem. Thereare300+problemsand150+codesnippetsinthebook.Mostoftheproblemscomewith solutionsand130+ofthemaregradedautomatically(allowingyoutogetinstantfeedback).We’re constantlyworkingonextendingandimprovingthisbook.Pleaseaskquestions,reporttypos,and suggestimprovementsthroughthisform.Checkhttps://leanpub.com/discrete-math forupdates ofthebook. Contents 0 About the Book . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7 0.1 ActiveLearning 7 0.2 Problem-basedLearning 7 0.3 PythonProgrammingLanguage 8 0.4 Acknowledgments 9 I Mathematical Thinking in Computer Science 1 Proofs: Convincing Arguments . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13 1.1 WarmUp 13 1.2 ExistenceProofs 20 2 Finding an Example . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 31 2.1 HowtoFindanExample 31 2.2 Optimality 39 2.3 ComputerSearch 49 3 Recursion and Induction . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 57 3.1 Recursion 57 3.2 Induction 75 4 Logic . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 93 4.1 ExamplesandCounterexamples 93 4.2 Logic 97 4.3 ReductioadAbsurdum 103 5 Invariants . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 109 5.1 DoubleCounting 109 5.2 SearchingforInvariants 111 5.3 Termination 112 5.4 EvenandOddNumbers 115 6 Project: 15-Puzzle . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 121 6.1 ThePuzzle 121 6.2 PermutationsandTranspositions 122 6.3 Why15-puzzleHasNoSolution 133 6.4 When15-puzzleHasaSolution 135 6.5 Implementation 142 7 Appendix . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 149 7.1 CuttingaFigure 149 7.2 UsingSAT-solvers 149 7.3 UsingILP-solvers 153 7.4 VisualizingFootballFans 156 II Combinatorics and Probability 8 Basic Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 161 8.1 StartingtoCount 161 8.2 RecursiveCounting 167 8.3 TuplesandPermutations 173 9 Binomial Coefficients . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 181 9.1 NumberofGamesinaTournament 181 9.2 Combinations 184 9.3 BinomialTheorem 190 9.4 PracticeCounting 193 10 Advanced Counting . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 197 10.1 Review 197 10.2 CombinationswithRepetitions 198 10.3 PracticeCounting 202 11 Probability . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 211 11.1 WhatisProbability? 211 11.2 Probability:Do’sandDon’ts 227 11.3 ConditionalProbability 241 11.4 MontyHallParadox 253 12 Random Variables . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 259 12.1 RandomVariablesandTheirExpectations 259 12.2 LinearityofExpectation 268 12.3 ExpectationisNotAll 271 12.4 Markov’sInequality 273 13 Dice Games . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 275 13.1 DiceGameProblem 275 13.2 OptimalStrategy 278 0. About the Book 0.1 Active Learning Thisbookcoversideasandconceptsindiscretemathematicswhichareneededinvariousbranches ofcomputerscience.Tomakethelearningprocessmoreefficientandenjoyable,weusethefol- lowingactivelearningcomponentsimplementedthroughourIntroductiontoDiscreteMathematics forComputerSciencespecialization atCoursera. Interactivepuzzles provide you with a fun way to “invent” the key ideas on your own. The puzzlesaremobile-friendly,soyoucanplaywiththemanywhere.Thegoalofeverypuzzle is to give you a clean and easy way to state problems where nothing distracts you from inventingamethodforsolvingit. Inturn,thecorrespondingmethodusuallyhasawide rangeofapplicationstovariousproblemsincomputerscience. Autogradedquizzes allowyoutoimmediatelycheckyourunderstandingafterlearninganew conceptoridea. Codesnippets arehelpfulintwoways:1)theyshowyouhowideasfromdiscretemathematics areusedinprogramming,and2)theyserveasinteractiveexamplesandchallenges:tweak thegivenpieceofcode,runit,andseewhathappens. Programmingchallenges willhelpyoutosolidifyyourunderstanding.AsDonaldKnuthsaid, “IfindthatIdon’tunderstandthingsunlessItrytoprogramthem.” 0.2 Problem-based Learning Throughoutthebook(andtheassociatedspecializationatCoursera)wefollowa“trythisbefore weexplaineverything”approach:wealwaysaskyoutosolveaproblemfirst,andthenweexplain howtosolveitandintroduceimportantideasneededtosolveit.Webelieve,thiswayyouwillget adeeperunderstandingandalsodevelopabetterappreciationforthebeautyoftheunderlying ideas(nottomentiontheself-confidencethatyougetifyouinventtheseideasonyourown!). Don’tbediscouragedifyoucan’tsolvealltheproblems. Justhavingattemptedthemisoften enoughtoengageyourmind,andmakeyoumorecuriousaboutthesolution. Weusethefollowingtwobasictypesofquestionsinthebook. Stopandthink questionsinviteyoutoslowdownandcontemplatethecurrentmaterialbefore continuingtothenexttopic.Wealwaysprovideananswertothecorrespondingquestion rightafterit. Westronglyencourageyou(asthenamesuggests)tostopanddoyourbest toanswerthequestion. 8 Chapter0.AbouttheBook Problems usuallyrequiremoreefforttosolve.Weusesomeofthemtowarmyouupandtodevelop yourcuriosity.Suchproblemsarefollowedbydetailedsolutions.Someotherproblemsare leftforyouasexercises. ManyquestionsinthebookaregradedautomaticallythroughCoursera.Theyaremarkedwith: Tryit:Coursera ,external . Boththeselinksareclickable:thefirstoneopensthecorrespondingautogradedpuzzleatCoursera (thisrequiresanactivesubscriptiontothespecialization),thesecondoneopensthecorresponding interactivepuzzle(andrequiresnosubscription).Atthesametime,thebookisself-contained:if youareunabletowatchthevideosandaccesstheinteractivepuzzlesatCoursera,justreadthe bookandsolvetheproblemsonapieceofpaper. 0.3 Python Programming Language 0.3.1 WhyProgramming? Whyonearthdowestartthebookwithdiscussingaprogramminglanguage?Afterall,thisisamath (ratherthanprogramming)book! That’strue.Butwebelievethatmanypiecesofcodeshowninthisbookwillhelpyouinmany ways: • Theywillshowyouarichvarietyofapplicationsofdiscretemathideasinvariousbranches ofcomputerscience. • Codesnippetscanserveasinteractiveexamples: youmaywanttotweakthegivenpiece ofcode,runit,andseewhathappens. • Bytryingtoimplementaparticularidea,youareforcedtounderstandeverysingledetail ofit. • Itisofteneasiertoreasonintermsofspecificobjectsinprogrammingratherthanabstract mathematicalconcepts. Wehavesetupeverythinginawaythatwillallowyoutorunthecodesnippetsusedinthis bookevenifyouhavenevertriedtowriteaprogrambefore.Youdon’tevenneedtoinstallorsetup anything:everythingcanberuninthecloud,throughyourInternetbrowser.Atthesametime,we alsoprovideinstructionsforthosewhowouldliketolearnthebasicsofPythonwhilelearning discretemath. 0.3.2 WhyPython? OK,let’sdosomeprogrammingwhilelearningdiscretemath.ButwhyPythoninsteadofanyother popularprogramminglanguage? LetusconvinceyouthatPythonisanexcellentchoiceforourpurposes. High-levellanguage. It is particularly easy to start using Python (even if you haven’t pro- grammed before). The syntax is reader friendly (and close to a natural language). The codeiscompact:mostofthepiecesofcodeinthisbookarelessthantenlineslong! Interactivemode. Itcanbeusedinaninteractivemode(alsoknownasREPL ,forread-eval- print loop). This allows you to talk to your computer using Python as a language: the computerthenreadsyourinput,evaluatesit,andprintstheresult.Thisway,youworkstuff outandgetinstantfeedbackfromthemachine. “Batteriesincluded”. ThePythonstandardlibrary offersawiderangeoffacilities,andmany externallibrariesareavailableaswell.Inparticular,thiswillallowustogeneratearandom sequence,plotafunction,anddrawagraphinjustonelineofcode! This (partly) explains why Python is often used for software prototyping, and in such areas asmachinelearning,datascience,andwebdevelopment. Ofcourse,advantagesalwayscomeatthecostofsomedisadvantages.Thehigh-levelnessof Pythonmakesitlessflexibleinperformancetuning.ThisisOKforus,aswewillonlybeusing simplesnippetsofcodewhereoptimizingisnotanissue. 0.4 Acknowledgments 9 0.3.3 HowtoCatchUpwithPython? OK,let’stry!WheredoIstart? Locally. ToinstallPythononyourmachine,gototheGetStarted sectionofpython.org and followtheinstructions.IfyouarenewtoPython,weencourageyoutoinstallPyCharm tostartworkingwithPython:this(freeofcharge)professionalIDEwillmaketheprocess ofwritingandrunningyourcodesmootherandmoreefficient. Inthecloud. Alternatively,youmayrunallourcodesnippetsfromyourInternetbrowser,without installingorconfiguringanythingonyourmachine.Todothis,visittherepositorypage andclickthebadge“OpeninColab”.Thiswillshowyoualistofnotebooksthatcanberun inaninteractivemoderightinyourbrowser(togetherwithlinkstoatutorialonnotebooks). 0.4 Acknowledgments Thisbookwasgreatlyimprovedbytheeffortsofalargenumberofindividualswhomweoweadebt ofgratitude. WethankthestudentsoftheCourseraspecializationaswellasthestudentsoftheModern SoftwareEngineeringB.Sc.programatSt.PetersburgStateUniversityfortheircontinuousand valuablefeedback.WealsothankJerryAllen,HuckBennett,MarieBrodsky,AnujKumarKarmakar, andTerenceMinerbrookforcarefullyreadinganearlierdraftofthisbook. WearegratefultoAntonKonevandDariaBorisyakforleadingthedevelopmentofinteractive puzzles.WethankVitaliyPolshkovforreviewingourPythoncode.

See more

The list of books you might like

Most books are stored in the elastic cloud where traffic is expensive. For this reason, we have a limit on daily download.