UU UI U I I IUUI I I UI I UUI I U I UUU I U I UU UUUIUIUII UU 11 0 01 0 0 0 01100 1 100 010 0 0 0 0 01 0 1 0 11 00 1 101111 1 1 11 10 1 0 0 01 1 10 0 100 111 0 1 0 1 11 0 1 1 11 11 1 01 1100 1 0 01 11 1 1 1 10 11 1 011 101 1 1 1 1 10 1 1 1 00 10 0 010011 1 · 110 01 0 1 1 11 01 1 111 1 010 1 0 1 0 0 1 1 0 1 101100011 1 111 10 1 0 0 01 I 00 0 100 0 110 0 1 0 1 11 0 0 0 1 011111 11 0 0 01 10 1 1 1 10 0 11 011 0 100 1 1 1 11 1 1 1 1 010 11 10 1 0 00 0 00 0 1 1 10 11 011 1 001 1 0 1 00 1 1 1 0 101 10 0 1 0 110 1 01 0 0 0 01 00 1 00 1 010 0 1 0 101001 1 1 1 1 1 1 1 1 10 1 10 0 0 001 10 1 00 0 110 0 1 01110011 1 0 1 0 1 1 0 01 0 101 1 110 10 0 11 1 101 1 0 10010100 0 1 0 0 0 110 0 010 1 111 01 1 11 1 011 1 0 0 10001100 1 1 1 110 0 110 0 001 1 00 1 00 0 1 1 0 0 0 0 0 01111011 1 0 0 1 1011101 1 110 1 00 0 11 0 11 1 1 01 1 11110111 0 1 0 010011 1 10 0 11 0 1 1 0011 00 1 10001 000 1 1 000010 0 00 1 01 0 0 0 110 1 00 0 0110 11 0 1 1 1 0011 0 01 1 00 1 1 0 1 10 11 0 1 1 0 0 0 1 1011 0 00011 0 0 0 1 0 0 0 0 0 0 01 00 1 0011 0 1 0 1 0 1 1 1 0 0101 1100 1 0 0 0 0 1 1 1 1011 1110 0 0 0 1 11 10 1 0011 1 1 1 0 01 001 1101 0 1 1 1 1 1 1 1 1 0 0 0 0 1 1 111 0 11 1 0 0 01 0 11 1 1 0 1 1 00 0 ~-~~~=i!L&WUIU~ 1 1 1 1 1 0 0 1 1 0 1 1 0 1 1 1 1 1 1 1 1 A Mathematical Foundation for Computer Science REVISED PRELIMINARY EDITION David Mix Barrington Kendall Hunt publishing co mpany All chapter heading quotes are taken from Monty Python's Flying Circus: All the Words (Volumes I and 2) (New York: Pantheon Books, 1989) and Monty Python and The Holy Grail (Book) [Monti Python ik den Helie Grailen (Bok)} (New York, Methuen Inc., 1979). Excursion 1.11 uses text from Through the Looking Glass, and What Alice Found There by Lewis Carroll (London, McMillan, 1871) and Fox in Socks by Dr. Seuss (New York, Random House, 1965). Problems 2.6.2 and 2.6.3 use text from The Number oft he Beast by Robert A. Heinlein (New York: Fawcett, 1980). There are many references in the text to Godel, Escher, Bach: An Eternal Golden Braid by Douglas R. Hofstadter (New York: Basic Books, 1979). Cover image of Stalker Castle, Scotland, by Frank Parolek © Shutterstock, Inc. Kendall Hunt publishing company www.kendallhunt.com Send all inquiries to: 4050 Westmark Drive Dubuque, IA 52004-1840 Copyright© 2020 by Kendall Hunt Publishing Company Revised 2020 ISBN 978-1-7924-3760-l All rights reserved. No part of this publication may be reproduced, stored in a retrieval system, or transmitted, in any form or by any means, electronic, mechanical, photocopying, recording, or otherwise, without the prior written permission of the copyright owner. Published in the United States of America REVISED PRELIMINARY EDITION CONTENTS Chapter 1: Sets, Propositions, and Predicates 1-1 1.1: Sets 1-2 1.2: Strings and String Operations 1-11 1.3: Excursion: What is a Proof? 1-20 1.4: Propositions and Boolean Operations 1-24 1.5: Set Operations and Propositions About Sets 1-34 1.6: Truth-Table Proofs 1-45 1.7: Rules for Propositional Proofs 1-52 1.8: Propositional Proof Strategies 1-59 1.9: Excursion: A Murder Mystery 1-65 1. 10: Predicates 1-68 1.11: Excursion: Translating Predicates 1-75 Glossary for Chapter 1 1-78 Chapter 2: Quantifiers and Predicate Calculus 2-1 2.1: Relations 2-2 2.2: Excursion: Relational Databases 2-8 2.3: Quantifiers 2-10 2.4: Excursion: Translating Quantifiers 2-17 2.5: Operations on Languages 2-19 2.6: Proofs With Quantifiers 2-25 2.7: Excursion: Practicing Proofs 2-32 2.8: Properties of Binary Relations 2-34 2.9: Functions 2-41 2.10: Partial Orders 2-48 2.11: Equivalence Relations 2-55 Glossary for Chapter 2 2-62 111 Chapter 3: Number Theory 3-1 3.1: Divisibility and Primes 3-2 3.2: Excursion: Playing With Numbers 3-11 3.3: Modular Arithmetic 3-14 3.4: There are Infinitely Many Primes 3-23 3.5: The Chinese Remainder Theorem 3-28 3.6: The Fundamental Theorem of Arithmetic 3-35 3.7: Excursion: Expressing Predicates in Number Theory 3-43 3.8: The Ring of Congruence Classes 3-46 3.9: Finite Fields and Modular Exponentiation 3-52 3.10: Excursion: Certificates of Primality 3-58 3.11: The RSA Cryptosystem 3-61 Glossary for Chapter 3 3-71 Chapter 4: Recursion and Proof by Induction 4-1 4.1: Recursive Definition 4-2 4.2: Excursion: Recursive Algorithms 4-10 4.3: Proof By Induction for Naturals 4-13 4.4: Variations on Induction for Naturals 4-20 4.5: Excursion: Fibonacci Numbers 4-27 4.6: Proving the Basic Facts of Arithmetic 4-30 4.7: Recursive Definition for Strings 4-37 4.8: Excursion: Naturals and Strings 4-45 4.9: Graphs and Paths 4-47 4.10: Trees and Lisp Lists 4-56 4.11: Induction for Problem Solving 4-66 Glossary for Chapter 4 4-75 lV Chapter 5: Regular Expressions and Other Recursive Systems 5.1: Regular Expressions and Their Languages 5-2 5.2: Examples of Regular Languages 5-7 5.3: Excursion: Designing Regular Expressions 5-13 5.4: Proving Regular Language Identities 5-15 5 .5: Proving Properties of the Regular Languages 5-19 5.6: Excursion: Hofstadter's MU-Puzzle 5-28 5.7: Recursion and Induction in General 5-30 5.8: Top-Down and Bottom-Up Definitions 5-38 5.9: Excursion: Parsing Arithmetic Expressions 5-45 5.10: A Recursive Definition oflmperative Programs 5-47 5.11: Correctness oflmperative Programs 5-56 Glossary for Chapter 5 5-61 Chapter 9: Trees and Searching 9.1: Graphs, Trees, and Recursion 9-2 9 .2: Excursion: Boolean Expressions 9-10 9.3: Expressions and Recursive Algorithms 9-12 9.4: General Search Algorithms 9-20 9.5: General Depth-First and Breadth-First Search 9-28 9.6: Depth-First and Breadth-First Search on Graphs 9-36 9.7: Excursion: Middle-First Search 9-44 9.8: Uniform-Cost Search 9-46 9.9: A* Search 9-54 9.10: Adversary Search 9-60 9.11: Excursion: Hexapawn 9-69 Glossary for Chapter 9 9-71 V Chapter 14: Finite-State Machines 14.1: Deterministic Finite Automata 14-2 14.2: Proving that DFA's Can't Do Things 14-9 14.3: The Myhill-Nerode Theorem 14-16 14.4: Excursion: Syntactic Monoids 14-26 14.5: Nondeterministic Finite Automata 14-28 14.6: The Subset Construction: NFA's into DFA's 14-36 14.7: Killing A -Moves: A -NFA's into NFA's 14-43 14.8: Constructing A -NFA's From Regular Expressions 14-53 14.9: Excursion: Practicing Multiple Constructions 14-62 14.10: State Elimination: NFA's into Regular Expresions 14-65 14.11: Excursion: Another Way From NFA's to Regular Expressions 14-73 Glossary for Chapter 14 14-76 Chapter 15: A Brief Tour of Formal Language Theory 15.1: Two-Way Deterministic Finite Automata 15-3 15.2: Grammars, Regular and Otherwise 15-14 15.3: Context-Free Languages 15-22 15.4: Excursion: Chomsky Normal Form 15-31 15.5: CFL's and Pushdown Automata 15-34 15.6: Turing Machine Definitions 15-43 15.7: Excursion: Unrestricted Grammars 15-51 15.8: Turing Machine Semantics 15-54 15.9: Excursion: Turing-Hangable Languages 15-62 15.10: The Halting Problem and Unsolvability 15-63 15.11: A Brief Look at Complexity Theory 15-72 Glossary for Chapter 15 15-83 Vl S.1: Solutions to Exercises from Chapter 1 S-1 S.2: Solutions to Exercises from Chapter 2 S-16 S.3: Solutions to Exercises from Chapter 3 S-32 S.4: Solutions to Exercises from Chapter 4 S-48 S.5: Solutions to Exercises from Chapter 5 S-64 S.9: Solutions to Exercises from Chapter 9 S-80 S.14: Solutions to Exercises from Chapter 14 S-96 S.15: Solutions to Exercises from Chapter 15 S-113 FULL VERSION CONTENTS Chapter 1: Sets, Propositions and Predicates Chapter 2: Quantifiers and Predicate Calculus Chapter 3: Number Theory Chapter 4: Recursion and Proof By Induction Chapter 5: Regular Expressions and Other Recursive Systems Chapter 6: Fundamental Counting Problems Chapter 7: Further Topics in Combinatorics Chapter 8: Graphs Chapter 9: Trees and Searching Chapter 10: Discrete Probability Chapter 11: Reasoning About Uncertainty Chapter 12: Markov Processes and Classical Games Chapter 13: Information Theory Chapter 14: Finite-State Machines Chapter 15: A Brief Tour of Formal Language Theory vu AUTHOR'S NOTE • This Revised Preliminary Edition contains eight of the entire book's fifteen chapters, which form the text for COMPSCI 250 at UMass Amherst in Fall 2020. The final version will include these chapters and seven others which could be used in COMPSCI 240. • Each chapter has eight ordinary sections and three Excursions. In COMPSCI 250 the 50-minute lectures cover one or sometimes two ordinary sections. Excursions are used for team problem-solving sessions in the weekly discussions. Each ordinary section contains ten Exercises (w ith solutions in the back) and ten Problems (suitable for homework assignments). Grateful Thanks to: • First and foremost, my wife Jessica and daughter Julia. • The many teachers who helped form me as a mathematician, including David Cox, the late Jim Mauldon, and especially Mark Kidwell at Amherst College, Adrian Mathias at Cambridge University, and Mike Sipser at M.I.T. • Colleagues at UMass who helped form me as a teacher and COMPSCI 250 as a course, including Amy Rosenberg, Neil Immerman, Hava Siegelmann, Marius Minea, and dozens of graduate and undergraduate teaching assistants. • My students in this and other courses. • Emma Strubell, who made most of the diagrams in Chapters 1 and 2 and many of those in Chapter 14. • Bev Kraus and Lenell Wyman at Kendall Hunt. lX