"Mathematics for Computer Science" (MCS)
Total Page:16
File Type:pdf, Size:1020Kb
“mcs” — 2016/9/28 — 23:07 — page i — #1 Mathematics for Computer Science revised Wednesday 28th September, 2016, 23:07 Eric Lehman Google Inc. F Thomson Leighton Department of Mathematics and the Computer Science and AI Laboratory, Massachussetts Institute of Technology; Akamai Technologies Albert R Meyer Department of Electrical Engineering and Computer Science and the Computer Science and AI Laboratory, Massachussetts Institute of Technology 2016, Eric Lehman, F Tom Leighton, Albert R Meyer. This work is available under the terms of the Creative Commons Attribution-ShareAlike 3.0 license. “mcs” — 2016/9/28 — 23:07 — page ii — #2 “mcs” — 2016/9/28 — 23:07 — page iii — #3 Contents I Proofs Introduction 3 0.1 References4 1 What is a Proof? 5 1.1 Propositions5 1.2 Predicates8 1.3 The Axiomatic Method8 1.4 Our Axioms9 1.5 Proving an Implication 11 1.6 Proving an “If and Only If” 13 1.7 Proof by Cases 15 1.8 Proof by Contradiction 16 1.9 Good Proofs in Practice 17 1.10 References 19 2 The Well Ordering Principle 29 2.1 Well Ordering Proofs 29 2.2 Template for Well Ordering Proofs 30 2.3 Factoring into Primes 32 2.4 Well Ordered Sets 33 3 Logical Formulas 47 3.1 Propositions from Propositions 48 3.2 Propositional Logic in Computer Programs 51 3.3 Equivalence and Validity 54 3.4 The Algebra of Propositions 56 3.5 The SAT Problem 61 3.6 Predicate Formulas 62 3.7 References 67 4 Mathematical Data Types 93 4.1 Sets 93 4.2 Sequences 98 4.3 Functions 99 4.4 Binary Relations 101 4.5 Finite Cardinality 105 “mcs” — 2016/9/28 — 23:07 — page iv — #4 Contentsiv 5 Induction 125 5.1 Ordinary Induction 125 5.2 Strong Induction 134 5.3 Strong Induction vs. Induction vs. Well Ordering 139 6 State Machines 157 6.1 States and Transitions 157 6.2 The Invariant Principle 158 6.3 Partial Correctness & Termination 166 6.4 The Stable Marriage Problem 171 7 Recursive Data Types 199 7.1 Recursive Definitions and Structural Induction 199 7.2 Strings of Matched Brackets 203 7.3 Recursive Functions on Nonnegative Integers 207 7.4 Arithmetic Expressions 210 7.5 Induction in Computer Science 215 8 Infinite Sets 243 8.1 Infinite Cardinality 244 8.2 The Halting Problem 253 8.3 The Logic of Sets 257 8.4 Does All This Really Work? 260 II Structures Introduction 283 9 Number Theory 285 9.1 Divisibility 285 9.2 The Greatest Common Divisor 290 9.3 Prime Mysteries 296 9.4 The Fundamental Theorem of Arithmetic 298 9.5 Alan Turing 302 9.6 Modular Arithmetic 306 9.7 Remainder Arithmetic 308 9.8 Turing’s Code (Version 2.0) 311 9.9 Multiplicative Inverses and Cancelling 313 9.10 Euler’s Theorem 317 9.11 RSA Public Key Encryption 322 9.12 What has SAT got to do with it? 324 “mcs” — 2016/9/28 — 23:07 — page v — #5 Contentsv 9.13 References 325 10 Directed graphs & Partial Orders 363 10.1 Vertex Degrees 365 10.2 Walks and Paths 366 10.3 Adjacency Matrices 369 10.4 Walk Relations 372 10.5 Directed Acyclic Graphs & Scheduling 373 10.6 Partial Orders 381 10.7 Representing Partial Orders by Set Containment 385 10.8 Linear Orders 386 10.9 Product Orders 386 10.10 Equivalence Relations 387 10.11 Summary of Relational Properties 389 11 Communication Networks 423 11.1 Complete Binary Tree 423 11.2 Routing Problems 423 11.3 Network Diameter 424 11.4 Switch Count 425 11.5 Network Latency 426 11.6 Congestion 426 11.7 2-D Array 427 11.8 Butterfly 429 11.9 Benesˇ Network 431 12 Simple Graphs 443 12.1 Vertex Adjacency and Degrees 443 12.2 Sexual Demographics in America 445 12.3 Some Common Graphs 447 12.4 Isomorphism 449 12.5 Bipartite Graphs & Matchings 451 12.6 Coloring 456 12.7 Simple Walks 461 12.8 Connectivity 463 12.9 Forests & Trees 468 12.10 References 476 13 Planar Graphs 515 13.1 Drawing Graphs in the Plane 515 13.2 Definitions of Planar Graphs 515 13.3 Euler’s Formula 526 “mcs” — 2016/9/28 — 23:07 — page vi — #6 Contentsvi 13.4 Bounding the Number of Edges in a Planar Graph 527 13.5 Returning to K5 and K3;3 528 13.6 Coloring Planar Graphs 529 13.7 Classifying Polyhedra 531 13.8 Another Characterization for Planar Graphs 534 III Counting Introduction 543 14 Sums and Asymptotics 545 14.1 The Value of an Annuity 546 14.2 Sums of Powers 552 14.3 Approximating Sums 554 14.4 Hanging Out Over the Edge 558 14.5 Products 564 14.6 Double Trouble 567 14.7 Asymptotic Notation 570 15 Cardinality Rules 595 15.1 Counting One Thing by Counting Another 595 15.2 Counting Sequences 596 15.3 The Generalized Product Rule 599 15.4 The Division Rule 603 15.5 Counting Subsets 606 15.6 Sequences with Repetitions 608 15.7 Counting Practice: Poker Hands 611 15.8 The Pigeonhole Principle 616 15.9 Inclusion-Exclusion 625 15.10 Combinatorial Proofs 631 15.11 References 635 16 Generating Functions 673 16.1 Infinite Series 673 16.2 Counting with Generating Functions 675 16.3 Partial Fractions 681 16.4 Solving Linear Recurrences 684 16.5 Formal Power Series 689 16.6 References 692 “mcs” — 2016/9/28 — 23:07 — page vii — #7 Contentsvii IV Probability Introduction 709 17 Events and Probability Spaces 711 17.1 Let’s Make a Deal 711 17.2 The Four Step Method 712 17.3 Strange Dice 721 17.4 The Birthday Principle 728 17.5 Set Theory and Probability 730 17.6 References 734 18 Conditional Probability 741 18.1 Monty Hall Confusion 741 18.2 Definition and Notation 742 18.3 The Four-Step Method for Conditional Probability 744 18.4 Why Tree Diagrams Work 746 18.5 The Law of Total Probability 754 18.6 Simpson’s Paradox 756 18.7 Independence 758 18.8 Mutual Independence 760 18.9 Probability versus Confidence 764 19 Random Variables 793 19.1 Random Variable Examples 793 19.2 Independence 795 19.3 Distribution Functions 796 19.4 Great Expectations 805 19.5 Linearity of Expectation 816 20 Deviation from the Mean 847 20.1 Markov’s Theorem 847 20.2 Chebyshev’s Theorem 850 20.3 Properties of Variance 854 20.4 Estimation by Random Sampling 860 20.5 Confidence in an Estimation 863 20.6 Sums of Random Variables 865 20.7 Really Great Expectations 874 21 Random Walks 899 21.1 Gambler’s Ruin 899 “mcs” — 2016/9/28 — 23:07 — page viii — #8 Contentsviii 21.2 Random Walks on Graphs 909 V Recurrences Introduction 927 22 Recurrences 929 22.1 The Towers of Hanoi 929 22.2 Merge Sort 932 22.3 Linear Recurrences 936 22.4 Divide-and-Conquer Recurrences 943 22.5 A Feel for Recurrences 950 Bibliography 957 Bibliography 957 Glossary of Symbols 961 Index 964 “mcs” — 2016/9/28 — 23:07 — page 1 — #9 I Proofs “mcs” — 2016/9/28 — 23:07 — page 2 — #10 “mcs” — 2016/9/28 — 23:07 — page 3 — #11 Introduction This text explains how to use mathematical models and methods to analyze prob- lems that arise in computer science. Proofs play a central role in this work because the authors share a belief with most mathematicians that proofs are essential for genuine understanding. Proofs also play a growing role in computer science; they are used to certify that software and hardware will always behave correctly, some- thing that no amount of testing can do. Simply put, a proof is a method of establishing truth. Like beauty, “truth” some- times depends on the eye of the beholder, and it should not be surprising that what constitutes a proof differs among fields. For example, in the judicial system, legal truth is decided by a jury based on the allowable evidence presented at trial. In the business world, authoritative truth is specified by a trusted person or organization, or maybe just your boss. In fields such as physics or biology, scientific truth is confirmed by experiment.1 In statistics, probable truth is established by statistical analysis of sample data. Philosophical proof involves careful exposition and persuasion typically based on a series of small, plausible arguments. The best example begins with “Cogito ergo sum,” a Latin sentence that translates as “I think, therefore I am.” This phrase comes from the beginning of a 17th century essay by the mathematician/philosopher, Rene´ Descartes, and it is one of the most famous quotes in the world: do a web search for it, and you will be flooded with hits. Deducing your existence from the fact that you’re thinking about your existence is a pretty cool and persuasive-sounding idea. However, with just a few more lines 1Actually, only scientific falsehood can be demonstrated by an experiment—when the experiment fails to behave as predicted. But no amount of experiment can confirm that the next experiment won’t fail. For this reason, scientists rarely speak of truth, but rather of theories that accurately predict past, and anticipated future, experiments. “mcs” — 2016/9/28 — 23:07 — page 4 — #12 0.1. References4 of argument in this vein, Descartes goes on to conclude that there is an infinitely beneficent God. Whether or not you believe in an infinitely beneficent God, you’ll probably agree that any very short “proof” of God’s infinite beneficence is bound to be far-fetched.