Discrete Math Lecture Notes
Total Page:16
File Type:pdf, Size:1020Kb
An Active Introduction to Discrete Mathematics and Algorithms Charles A. Cusack [email protected] David A. Santos Version 2.7.1 June 9, 2020 ii Copyright © 2020 Charles A. Cusack. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation License, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”. Copyright © 2007 David Anthony Santos. Permission is granted to copy, distribute and/or modify this document under the terms of the GNU Free Documentation Li- cense, Version 1.2 or any later version published by the Free Software Foundation; with no Invariant Sections, no Front-Cover Texts, and no Back-Cover Texts. A copy of the license is included in the section entitled “GNU Free Documentation License”. History • An Active Introduction to Discrete Mathematics and Algorithms, 2020, Charles A. Cusack. Addition of Reading Comprehension Questions for each chapter. Some added material in Chapters 2, 4, 5, and especially 10. A few new Problems in several chapters. Minor edits throughout. • An Active Introduction to Discrete Mathematics and Algorithms, 2019, 2018, 2017, Charles A. Cusack. Minor revisions and fixing of errors. A few additions and subtractions of material. • An Active Introduction to Discrete Mathematics and Algorithms, 2016, Charles A. Cusack. Minor revisions. Algorithm Analysis chapter had a few additions. • An Active Introduction to Discrete Mathematics and Algorithms, 2015, Charles A. Cusack. Minor revisions. Algorithm Analysis chapter had major revisions. • An Active Introduction to Discrete Mathematics and Algorithms, 2014, Charles A. Cusack. This is a significant revision of the 2013 version (thus the slight change in title). • An Introduction to Discrete Mathematics and Algorithms, 2013, Charles A. Cusack. This document draws some content from each of the following. – Discrete Mathematics Notes, 2008, David A. Santos. – More Discrete Mathematics, 2007, David A. Santos. – Number Theory for Mathematical Contests, 2007, David A. Santos. – Linear Algebra Notes, 2008, David A. Santos. – Precalculus, An Honours Course, 2008, David Santos. These documents are all available from http://www.opensourcemath.org/books/santos/, but the site appears not to be consistently available. About the cover The image on the cover is an example of mathematical art using Lego bricks. Go to https://www.instagram.com/ferzle/ to see more pictures of Dr. Cusack’s Lego art. iii Contents Preface v 6 Sequences and Summations 181 6.1 Sequences ................ 181 How to use this book vii 6.2 SumsandProducts. 195 6.3 Reading Comprehension Questions . 212 1 Motivation 1 6.4 Problems . 213 1.1 Some Problems . 2 7 Algorithm Analysis 217 2 Proof Methods 7 7.1 Asymptotic Notation . 217 2.1 Direct Proofs . 7 7.1.1 The Notations . 217 2.2 Implication and Its Friends . 16 7.1.2 Properties of the Notations . 227 2.3 Proof by Contradiction . 20 7.1.3 Proofs using the definitions . 231 7.1.4 Proofs using limits . 236 2.4 Proof by Contraposition . 29 7.2 CommonGrowthRates . 247 2.5 OtherProofTechniques . 31 7.3 Algorithm Analysis . 255 2.6 If and Only If Proofs . 33 7.3.1 Analyzing Algorithms . 256 2.7 Common Errors in Proofs . 35 7.3.2 Common Time Complexities . 264 2.8 More Practice . 39 7.3.3 Basic Sorting Algorithms . 268 2.9 Reading Comprehension Questions . 43 7.3.4 Basic Data Structures (Review) 272 2.10 Problems . 45 7.3.5 More Examples . 274 7.3.6 Binary Search . 283 3 Programming Fundamentals and Algo- 7.4 Reading Comprehension Questions . 286 rithms 47 7.5 Problems . 288 3.1 Algorithms . 47 3.2 The mod operator and Integer Division 51 8 Recursion, Recurrences, and Mathe- 3.3 If-then-else Statements . 58 matical Induction 295 3.4 The for loop . 60 8.1 Mathematical Induction . 295 8.1.1 The Basics . 296 3.5 Arrays.................. 63 8.1.2 Equalities/Inequalities . 301 3.6 The while loop . 67 8.1.3 Variations . 305 3.7 Reading Comprehension Questions . 70 8.1.4 Strong Induction . 309 3.8 Problems . 72 8.1.5 Induction Errors . 311 8.1.6 Summary/Tips . 313 4 Logic 77 8.2 Recursion . 316 4.1 Propositional Logic . 77 8.3 Solving Recurrence Relations . 324 4.1.1 Compound Propositions . 78 8.3.1 Substitution Method . 326 4.1.2 Truth Tables . 86 8.3.2 Iteration Method . 330 4.1.3 Precedence Rules . 88 8.3.3 Master Theorem . 338 4.2 Propositional Equivalence . 90 8.3.4 Linear Recurrence Relations . 340 4.3 Predicates and Quantifiers . 102 8.4 Analyzing Recursive Algorithms . 344 4.4 NormalForms. 112 8.4.1 Analyzing Quicksort . 348 4.5 Bitwise Operations . 116 8.5 Reading Comprehension Questions . 352 4.6 Reading Comprehension Questions . 119 8.6 Problems . 354 4.7 Problems . 122 9 Counting 357 9.1 The Sum and Product Rules . 357 5 Sets, Functions, and Relations 129 9.2 Pigeonhole Principle . 362 5.1 Sets ................... 129 9.3 Permutations and Combinations . 367 5.2 Set Operations . 136 9.3.1 Permutations without Repeti- 5.3 Functions . 147 tions . 367 5.4 Partitions and Equivalence Relations . 158 9.3.2 Permutations with Repetitions 370 5.5 Reading Comprehension Questions . 173 9.3.3 Combinations without Repeti- 5.6 Problems . 177 tions . 374 iv 9.3.4 Combinations with Repetitions 380 10.4 Handshaking Lemma . 410 9.4 Binomial Theorem . 382 10.5 GraphRepresentation . 412 9.5 Inclusion-Exclusion . 385 10.6 Problem Solving with Graphs . 416 9.6 Reading Comprehension Questions . 390 10.7 Reading Comprehension Questions . 424 9.7 Problems . 392 10.8 Problems . 427 11 Exercise Solutions 429 10 Graph Theory 397 10.1 TypesofGraphs . 397 GNU Free Documentation License 479 10.2 Graph Terminology . 401 10.3 SomeSpecialGraphs . 407 Index 483 Preface This book is an attempt to present some of the most important discrete mathematics concepts to computer science students in the context of algorithms. I wrote it for use as a textbook for half of a course on discrete mathematics and algorithms that we offer at Hope College. Since it was written for use in this particular context, it is important to note that the course (and therefore this book) has as a prerequisite what is typically referred to as CS2. Thus, it is assumed that the reader has had one or more programming courses (The example code in the book is very much like C++ or Java), has seen basic data structures (e.g. stacks, queues, linked lists, trees), has studied the standard sorting algorithms (e.g. selection sort, bubble sort, insertion sort, quicksort, and mergesort), and has seen recursion (although that material is reviewed in the book). In addition, it is assumed that the reader has seen the binary representation of integers. Finally, limits and derivatives are used in a few sections in the chapter on algorithm analysis because I (and I think many students) find it much easier to prove bounds using limits instead of the formal definitions, assuming they have had calculus. Because the majority of our students have had calculus, I do not review those concepts. I have found that even the students who have not had calculus can tackle this material with a little extra help, especially given the fact that the derivatives and limits they need to compute are pretty straightforward. Alternatively, that section can be skipped (although it makes the next section on growth rates a bit more difficult to read in detail). Some of the material is drawn from several open-source books by David Santos. Other material is from handouts I have written and used over the years. I have extensively edited the material from both sources, both for clarity and to emphasize the connections between the material and algorithms where possible. I have also added a significant amount of new material. The format of the material is also significantly different than it was in the original sources. I should mention that I never met David Santos, who apparently died in 2011. I stumbled upon his books in the summer of 2013 when I was searching for a discrete mathematics book to use in a new course. When I discovered that I could adapt his material for my own use, I decided to do so. Since clearly he has no knowledge of this book, he bears no responsibility for any of the edited content. Any errors or omissions are therefore mine. I appreciate any feedback you have. Please send any typos, formatting errors, other errors, suggestions, etc., to [email protected]. I would like to thank the following people for submitting feedback/errata (listed in no par- ticular order): Dan Zingaro, Mike Jipping, Steve Ratering, Victoria Gonda, Nathan Vance, Cole Watson, Kalli Crandell, John Dood, James Cerone, Coty Franklin, Kyle Magnuson, Karl-Dieter Crisman, Katie Brudos, Jonathan Senning, Matthew DeJongh, Julian Payne, Josiah Brouwer, and probably several others I forgot to mention (sorry!). Charles A. Cusack May, 2019 v vi How to use this book As the title of the book indicates, this is not a book that is just to be read. It was written so that the reader interacts with the material. If you attempt to just read what is written and take no part in the exercises that are embedded throughout, you will likely get very little out of it. Learning needs to be active, not passive. The more active you are as you ‘read’ the book, the more you will get out of it.