Introduction to Theory of Computation
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to Theory of Computation Anil Maheshwari Michiel Smid School of Computer Science Carleton University Ottawa Canada anil,michiel @scs.carleton.ca { } April 17, 2019 ii Contents Contents Preface vi 1 Introduction 1 1.1 Purposeandmotivation ..................... 1 1.1.1 Complexitytheory .................... 2 1.1.2 Computability theory . 2 1.1.3 Automatatheory ..................... 3 1.1.4 Thiscourse ........................ 3 1.2 Mathematical preliminaries . 4 1.3 Prooftechniques ......................... 7 1.3.1 Directproofs ....................... 8 1.3.2 Constructiveproofs. 9 1.3.3 Nonconstructiveproofs . 10 1.3.4 Proofsbycontradiction. 11 1.3.5 The pigeon hole principle . 12 1.3.6 Proofsbyinduction. 13 1.3.7 Moreexamplesofproofs . 15 Exercises................................. 18 2 Finite Automata and Regular Languages 21 2.1 An example: Controling a toll gate . 21 2.2 Deterministic finite automata . 23 2.2.1 Afirstexampleofafiniteautomaton . 26 2.2.2 Asecondexampleofafiniteautomaton . 28 2.2.3 A third example of a finite automaton . 29 2.3 Regularoperations ........................ 31 2.4 Nondeterministic finite automata . 35 2.4.1 Afirstexample ...................... 35 iv Contents 2.4.2 Asecondexample..................... 37 2.4.3 Athirdexample...................... 38 2.4.4 Definition of nondeterministic finite automaton . 39 2.5 EquivalenceofDFAsandNFAs . 41 2.5.1 Anexample ........................ 44 2.6 Closureundertheregularoperations . 48 2.7 Regularexpressions. .. .. 52 2.8 Equivalence of regular expressions and regular languages . 56 2.8.1 Every regular expression describes a regular language . 57 2.8.2 Converting a DFA to a regular expression . 60 2.9 The pumping lemma and nonregular languages . 67 2.9.1 Applications of the pumping lemma . 69 2.10Higman’sTheorem ........................ 76 2.10.1 Dickson’sTheorem . 76 2.10.2 ProofofHigman’sTheorem . 77 Exercises................................. 80 3 Context-Free Languages 91 3.1 Context-freegrammars . 91 3.2 Examplesofcontext-freegrammars . 94 3.2.1 Properlynestedparentheses . 94 3.2.2 A context-free grammar for a nonregular language . 95 3.2.3 A context-free grammar for the complement of a non- regularlanguage ..................... 97 3.2.4 A context-free grammar that verifies addition . 98 3.3 Regularlanguagesarecontext-free. 100 3.3.1 Anexample ........................102 3.4 Chomskynormalform . .104 3.4.1 Anexample ........................109 3.5 Pushdownautomata . .112 3.6 Examplesofpushdownautomata . 116 3.6.1 Properlynestedparentheses . 116 3.6.2 Strings of the form 0n1n .................117 3.6.3 Strings with b in the middle . 118 3.7 Equivalence of pushdown automata and context-free grammars 120 3.8 The pumping lemma for context-free languages . 124 3.8.1 Proofofthepumpinglemma. .125 3.8.2 Applications of the pumping lemma . 128 Contents v Exercises.................................132 4 Turing Machines and the Church-Turing Thesis 137 4.1 Definition of a Turing machine . 137 4.2 ExamplesofTuringmachines . .141 4.2.1 Accepting palindromes using one tape . 141 4.2.2 Accepting palindromes using two tapes . 142 4.2.3 Accepting anbncn usingonetape. .143 4.2.4 Accepting anbncn using tape alphabet a, b, c, ✷ . .145 { } 4.2.5 Accepting ambncmn usingonetape. .147 4.3 Multi-tape Turing machines . 148 4.4 TheChurch-TuringThesis . .151 Exercises.................................152 5 Decidable and Undecidable Languages 157 5.1 Decidability . 157 5.1.1 The language ADFA ....................158 5.1.2 The language ANFA ....................159 5.1.3 The language ACFG ....................160 5.1.4 The language ATM ....................161 5.1.5 The Halting Problem . 163 5.2 Countablesets...........................164 5.2.1 The Halting Problem revisited . 168 5.3 Rice’sTheorem ..........................170 5.3.1 ProofofRice’sTheorem . .171 5.4 Enumerability...........................173 5.4.1 Hilbert’s problem . 174 5.4.2 The language ATM ....................176 5.5 Where does the term “enumerable” come from? . 177 5.6 Mostlanguagesarenotenumerable . 180 5.6.1 The set of enumerable languages is countable . 180 5.6.2 The set of all languages is not countable . 181 5.6.3 There are languages that are not enumerable . 183 5.7 The relationship between decidable and enumerable languages 184 5.8 A language A such that both A and A are not enumerable . 186 5.8.1 EQ TM isnotenumerable. .186 5.8.2 EQ TM isnotenumerable. .188 Exercises.................................189 vi Contents 6 Complexity Theory 197 6.1 The running time of algorithms . 197 6.2 The complexity class P ......................199 6.2.1 Someexamples ......................199 6.3 The complexity class NP .....................202 6.3.1 P is contained in NP ...................208 6.3.2 Deciding NP-languages in exponential time . 208 6.3.3 Summary .........................210 6.4 Non-deterministic algorithms . 211 6.5 NP-completelanguages . .213 6.5.1 Twoexamplesofreductions . .215 6.5.2 Definition of NP-completeness . 220 6.5.3 An NP-completedominogame . .222 6.5.4 Examples of NP-completelanguages . 231 Exercises.................................235 7 Summary 239 Preface This is a free textbook for an undergraduate course on the Theory of Com- putation, which we have been teaching at Carleton University since 2002. Until the 2011/2012 academic year, this course was offered as a second-year course (COMP 2805) and was compulsory for all Computer Science students. Starting with the 2012/2013 academic year, the course has been downgraded to a third-year optional course (COMP 3803). We have been developing this book since we started teaching this course. Currently, we cover most of the material from Chapters 2–5 during a 12-week term with three hours of classes per week. The material from Chapter 6, on Complexity Theory, is taught in the third-year course COMP 3804 (Design and Analysis of Algorithms). In the early years of COMP 2805, we gave a two-lecture overview of Complexity Theory at the end of the term. Even though this overview has disappeared from the course, we decided to keep Chapter 6. This chapter has not been revised/modified for a long time. The course as we teach it today has been influenced by the following two textbooks: Introduction to the Theory of Computation (second edition), by Michael • Sipser, Thomson Course Technnology, Boston, 2006. Einf¨uhrung in die Theoretische Informatik, by Klaus Wagner, Springer- • Verlag, Berlin, 1994. Besides reading this text, we recommend that you also take a look at these excellent textbooks, as well as one or more of the following ones: Elements of the Theory of Computation (second edition), by Harry • Lewis and Christos Papadimitriou, Prentice-Hall, 1998. viii Introduction to Languages and the Theory of Computation (third edi- • tion), by John Martin, McGraw-Hill, 2003. Introduction to Automata Theory, Languages, and Computation (third • edition), by John Hopcroft, Rajeev Motwani, Jeffrey Ullman, Addison Wesley, 2007. Please let us know if you find errors, typos, simpler proofs, comments, omissions, or if you think that some parts of the book “need improvement”. Chapter 1 Introduction 1.1 Purpose and motivation This course is on the Theory of Computation, which tries to answer the following questions: What are the mathematical properties of computer hardware and soft- • ware? What is a computation and what is an algorithm? Can we give rigorous • mathematical definitions of these notions? What are the limitations of computers? Can “everything” be com- • puted? (As we will see, the answer to this question is “no”.) Purpose of the Theory of Computation: Develop formal math- ematical models of computation that reflect real-world computers. This field of research was started by mathematicians and logicians in the 1930’s, when they were trying to understand the meaning of a “computation”. A central question asked was whether all mathematical problems can be solved in a systematic way. The research that started in those days led to computers as we know them today. Nowadays, the Theory of Computation can be divided into the follow- ing three areas: Complexity Theory, Computability Theory, and Automata Theory. 2 Chapter 1. Introduction 1.1.1 Complexity theory The main question asked in this area is “What makes some problems com- putationally hard and other problems easy?” Informally, a problem is called “easy”, if it is efficiently solvable. Exam- ples of “easy” problems are (i) sorting a sequence of, say, 1,000,000 numbers, (ii) searching for a name in a telephone directory, and (iii) computing the fastest way to drive from Ottawa to Miami. On the other hand, a problem is called “hard”, if it cannot be solved efficiently, or if we don’t know whether it can be solved efficiently. Examples of “hard” problems are (i) time table scheduling for all courses at Carleton, (ii) factoring a 300-digit integer into its prime factors, and (iii) computing a layout for chips in VLSI. Central Question in Complexity Theory: Classify problems ac- cording to their degree of “difficulty”. Give a rigorous proof that problems that seem to be “hard” are really “hard”. 1.1.2 Computability theory In the 1930’s, G¨odel, Turing, and Church discovered that some of the fun- damental mathematical problems cannot be solved by a “computer”. (This may sound strange, because computers were invented only in the 1940’s). An example of such a problem is “Is an arbitrary mathematical statement true or false?” To attack such a problem, we need formal definitions of the notions of computer, • algorithm, and • computation. • The theoretical models that were proposed in order to understand solvable and unsolvable problems led to the development of real computers. Central Question in Computability Theory: Classify problems as being solvable or unsolvable. 1.1. Purpose and motivation 3 1.1.3 Automata theory Automata Theory deals with definitions and properties of different types of “computation models”. Examples of such models are: Finite Automata. These are used in text processing, compilers, and • hardware design. Context-Free Grammars. These are used to define programming lan- • guages and in Artificial Intelligence. Turing Machines. These form a simple abstract model of a “real” • computer, such as your PC at home.