Active Introduction to Discrete Mathematics and Algorithms
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.6.3 March 30, 2018 ii Copyright c 2016 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 c 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, 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. It shows 9 different Latin squares of order 16. Go to https://www.instagram.com/ferzle/ to see more pictures of Dr. Cusack’s Lego art. iii Contents Preface v 7 Algorithm Analysis 195 7.1 Asymptotic Notation . 195 How to use this book vii 7.1.1 The Notations . 195 7.1.2 Properties of the Notations . 205 1 Motivation 1 7.1.3 Proofs using the definitions . 209 1.1 Some Problems . 2 7.1.4 Proofs using limits . 214 2 Proof Methods 7 7.2 CommonGrowthRates . 225 2.1 Direct Proofs . 7 7.3 Algorithm Analysis . 233 2.2 Implication and Its Friends . 16 7.3.1 Analyzing Algorithms . 234 2.3 Proof by Contradiction . 20 7.3.2 Common Time Complexities . 242 2.4 Proof by Contraposition . 29 7.3.3 Basic Sorting Algorithms . 246 2.5 OtherProofTechniques . 31 7.3.4 Basic Data Structures (Review) 250 2.6 If and Only If Proofs . 33 7.3.5 More Examples . 252 2.7 Common Errors in Proofs . 35 7.3.6 Binary Search . 261 2.8 More Practice . 38 7.4 Problems . 264 2.9 Problems . 42 8 Recursion, Recurrences, and Mathe- 3 Programming Fundamentals and Algo- matical Induction 271 rithms 45 8.1 Mathematical Induction . 271 3.1 Algorithms . 45 8.1.1 The Basics . 272 3.2 The mod operator and Integer Division 49 8.1.2 Equalities/Inequalities . 277 3.3 If-then-else Statements . 56 8.1.3 Variations . 280 3.4 The for loop . 58 8.1.4 Strong Induction . 284 3.5 Arrays.................. 61 8.1.5 Induction Errors . 286 3.6 The while loop . 65 8.1.6 Summary/Tips . 288 3.7 Problems . 68 8.2 Recursion . 291 8.3 Solving Recurrence Relations . 299 4 Logic 71 8.3.1 Substitution Method . 301 4.1 Propositional Logic . 71 8.3.2 Iteration Method . 305 4.1.1 Compound Propositions . 72 8.3.3 Master Theorem . 313 4.1.2 Truth Tables . 80 8.3.4 Linear Recurrence Relations . 315 4.1.3 Precedence Rules . 82 8.4 Analyzing Recursive Algorithms . 319 4.2 Propositional Equivalence . 84 8.4.1 Analyzing Quicksort . 323 4.3 Predicates and Quantifiers . 96 4.4 NormalForms. 105 8.5 Problems . 327 4.5 Bitwise Operations . 108 9 Counting 331 4.6 Problems . 111 9.1 The Multiplication and Sum Rules . 331 5 Sets, Functions, and Relations 117 9.2 Pigeonhole Principle . 336 5.1 Sets ................... 117 9.3 Permutations and Combinations . 342 5.2 Set Operations . 123 9.3.1 Permutations without Repeti- 5.3 Functions . 134 tions . 342 5.4 Partitions and Equivalence Relations . 145 9.3.2 Permutations with Repetitions 345 5.5 Problems . 159 9.3.3 Combinations without Repeti- tions . 349 6 Sequences and Summations 163 9.3.4 Combinations with Repetitions 355 6.1 Sequences ................ 163 9.4 Binomial Theorem . 359 6.2 SumsandProducts. 176 9.5 Inclusion-Exclusion . 362 6.3 Problems . 192 9.6 Problems . 368 iv 10 Graph Theory 373 10.8 Planarity . 393 10.1 TypesofGraphs . 373 10.9 Problems . 395 10.2 Graph Terminology . 377 10.3 SomeSpecialGraphs . 382 11 Selected Solutions 397 10.4 Handshaking Lemma . 385 10.5 GraphRepresentation . 387 GNU Free Documentation License 445 10.6 Problem Solving with Graphs . 388 10.7 Traversability . 391 Index 449 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. 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. This is still a work in progress, so 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, Coty Franklin, Kyle Magnuson, Katie Brudos, Jonathan Senning, Matthew DeJongh, Julian Payne, Josiah Brouwer, and probably several others I forgot to mention. Charles A. Cusack July, 2014 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. That will translate to better learning. And it will also translate to a higher grade. So whether you are motivated by learning (which is my hope) or merely by getting a certain grade, your path will be the same–use this book as described below. The content is presented in the following manner. First, concepts and definitions are given– generally one at a time. Then one or more examples that illustrate the concept/definition will be given. After that you will find one or more exercises of various kinds. This is where this book differs from most. Instead of piling on more examples that you merely read and think you understand, you will be asked to solve some for yourself so that you can be more confident that you really do understand. Some of the exercises are just called Exercises. They are very similar to the examples, except that you have to provide the solution. There are also Fill in the details which provide part of the solution, but ask you to provide some of the details. The point of these is to help you think about some of the finer details that you might otherwise miss. There are also Questions of various kinds that get you thinking about the concepts. Finally, there are Evaluate exercises. These ask you to look at solutions written by others and determine whether or not they are correct. More precisely, your goal is to try to find as many errors in the solutions as you can. Usually there will be one or more errors in each solution, but occasionally a correct solution will be given, so pay careful attention to every detail. The point of these exercises is to help you see mistakes before you make them. Many of these exercises are based on solutions from previous students, so they often represent the common mistakes students make. Hopefully if you see someone else make these mistakes, you will be less likely to make them yourself. The point of the exercises is to get you thinking about and interacting with the material.