Introduction to the Theory of Computation Some Notes for CIS262
Total Page:16
File Type:pdf, Size:1020Kb
Introduction to the Theory of Computation Some Notes for CIS262 Jean Gallier Department of Computer and Information Science University of Pennsylvania Philadelphia, PA 19104, USA e-mail: [email protected] c Jean Gallier Please, do not reproduce without permission of the author December 26, 2017 2 Contents 1 Introduction 7 2 Basics of Formal Language Theory 9 2.1 Alphabets,Strings,Languages . .. 9 2.2 OperationsonLanguages. .. .. 15 3 DFA’s, NFA’s, Regular Languages 19 3.1 Deterministic Finite Automata (DFA’s) . 20 3.2 The“Cross-product”Construction . ... 25 3.3 Nondeteterministic Finite Automata (NFA’s) . .. 27 3.4 ǫ-Closure...................................... 30 3.5 ConvertinganNFAintoaDFA ......................... 32 3.6 Finite State Automata With Output: Transducers . .... 36 3.7 An Application of NFA’s: Text Search . 40 4 Hidden Markov Models (HMMs) 45 4.1 HiddenMarkovModels(HMMs). 45 4.2 The Viterbi Algorithm and the Forward Algorithm . 58 5 Regular Languages, Minimization of DFA’s 67 5.1 DirectedGraphsandPaths ........................... 67 5.2 LabeledGraphsandAutomata ......................... 70 5.3 The Closure Definition of the Regular Languages . .. 72 5.4 RegularExpressions ............................... 75 5.5 RegularExpressionsandRegularLanguages . ... 76 5.6 RegularExpressionsandNFA’s . .. .. 78 5.7 Applications of Regular Expressions . 86 5.8 Summary of Closure Properties of the Regular Languages . ...... 87 5.9 Right-Invariant Equivalence Relations on Σ∗ .................. 88 5.10 Finding minimal DFA’s . 98 5.11 State Equivalence and Minimal DFA’s . 100 5.12ThePumpingLemma............................... 111 5.13 A Fast Algorithm for Checking State Equivalence . 115 3 4 CONTENTS 6 Context-Free Grammars And Languages 127 6.1 Context-FreeGrammars . .. .. 127 6.2 DerivationsandContext-FreeLanguages . .... 128 6.3 NormalFormsforContext-FreeGrammars . .... 134 6.4 RegularLanguagesareContext-Free . 141 6.5 Useless Productions in Context-Free Grammars . .... 142 6.6 TheGreibachNormalForm ........................... 144 6.7 LeastFixed-Points ................................ 145 6.8 Context-Free Languages as Least Fixed-Points . ...... 147 6.9 Least Fixed-Points andtheGreibachNormalForm . 151 6.10 TreeDomainsandGornTrees . 156 6.11DerivationsTrees ................................. 160 6.12Ogden’sLemma.................................. 162 6.13PushdownAutomata ............................... 168 6.14 FromContext-FreeGrammarsToPDA’s . 172 6.15 FromPDA’sToContext-FreeGrammars . 173 6.16 The Chomsky-Schutzenberger Theorem . ..... 175 7 A Survey of LR-Parsing Methods 177 7.1 LR(0)-CharacteristicAutomata . 177 7.2 Shift/ReduceParsers ............................... 186 7.3 ComputationofFIRST.............................. 188 7.4 The Intuition Behind the Shift/Reduce Algorithm . 189 7.5 TheGraphMethodforComputingFixedPoints . 190 7.6 ComputationofFOLLOW ............................ 192 7.7 Algorithm T raverse ............................... 193 7.8 More on LR(0)-CharacteristicAutomata . 195 7.9 LALR(1)-LookaheadSets. 195 7.10 Computing FIRST, FOLLOW, etc. in the Presence of ǫ-Rules . 197 7.11 LR(1)-CharacteristicAutomata . 204 8 RAM Programs, Turing Machines 209 8.1 PartialFunctionsandRAMPrograms . 209 8.2 Definition of a Turing Machine . 215 8.3 ComputationsofTuringMachines. 217 8.4 RAM-computable functions are Turing-computable . .... 220 8.5 Turing-computable functions are RAM-computable . .... 221 8.6 Computably Enumerable and Computable Languages . 222 8.7 The Primitive Recursive Functions . 223 8.8 ThePartialComputableFunctions . 229 9 Universal RAM Programs and the Halting Problem 235 9.1 PairingFunctions................................. 235 CONTENTS 5 9.2 EquivalenceofAlphabets............................. 238 9.3 CodingofRAMPrograms ............................ 242 9.4 Kleene’s T -Predicate ............................... 250 9.5 A Simple Function Not Known to be Computable . 252 9.6 A Non-Computable Function; Busy Beavers . 254 10 Elementary Recursive Function Theory 259 10.1 AcceptableIndexings ............................... 259 10.2UndecidableProblems .............................. 262 10.3 Listable (Recursively Enumerable) Sets . 267 10.4 Reducibility and Complete Sets . 272 10.5TheRecursionTheorem ............................. 276 10.6ExtendedRiceTheorem ............................. 280 10.7 CreativeandProductiveSets . 283 11 Listable and Diophantine Sets; Hilbert’s Tenth 287 11.1 Diophantine Equations; Hilbert’s Tenth Problem . 287 11.2 Diophantine Sets and Listable Sets . 290 11.3 SomeApplicationsoftheDPRMTheorem . 294 12 The Post Correspondence Problem; Applications 299 12.1 ThePostCorrespondenceProblem . 299 12.2 Some Undecidability Results for CFG’s . 300 12.3 MoreUndecidablePropertiesofLanguages . .... 303 13 Computational Complexity; and 305 P N P 13.1 The Class .................................... 305 P 13.2 DirectedGraphs,Paths. 307 13.3EulerianCycles .................................. 308 13.4 Hamiltonian Cycles . 309 13.5 Propositional Logic and Satisfiability . 310 13.6 The Class , -Completeness ....................... 314 N P N P 13.7 TheCook-LevinTheorem ............................ 319 14 Some -Complete Problems 331 N P 14.1 StatementsoftheProblems . 331 14.2 Proofs of -Completeness ........................... 342 N P 14.3 Succinct Certificates, co , and ..................... 355 N P EX P 15 Primality Testing is in 361 N P 15.1 PrimeNumbersandCompositeNumbers . 361 15.2 Methods for Primality Testing . 362 15.3 Modular Arithmetic, the Groups Z/nZ, (Z/nZ)∗ ................ 365 6 CONTENTS 15.4 TheLucasTheorem;LucasTrees . 374 15.5 Algorithms for Computing Powers Modulo m ................. 379 15.6 PRIMES is in ................................ 381 N P Chapter 1 Introduction The theory of computation is concerned with algorithms and algorithmic systems: their design and representation, their completeness, and their complexity. The purpose of these notes is to introduce some of the basic notions of the theory of computation, including concepts from formal languages and automata theory, the theory of computability, some basics of recursive function theory, and an introduction to complexity theory. Other topics such as correctness of programs will not be treated here (there just isn’t enough time!). The notes are divided into three parts. The first part is devoted to formal languages and automata. The second part deals with models of computation, recursive functions, and undecidability. The third part deals with computational complexity, in particular the classes and . P N P 7 8 CHAPTER 1. INTRODUCTION Chapter 2 Basics of Formal Language Theory 2.1 Alphabets, Strings, Languages Our view of languages is that a language is a set of strings. In turn, a string is a finite sequence of letters from some alphabet. These concepts are defined rigorously as follows. Definition 2.1. An alphabet Σ is any finite set. We often write Σ = a ,...,a . The a are called the symbols of the alphabet. { 1 k} i Examples: Σ= a { } Σ= a, b, c { } Σ= 0, 1 { } Σ= α,β,γ,δ,ǫ,λ,ϕ,ψ,ω,µ,ν,ρ,σ,η,ξ,ζ { } A string is a finite sequence of symbols. Technically, it is convenient to define strings as functions. For any integer n 1, let ≥ [n]= 1, 2,...,n , { } and for n = 0, let [0] = . ∅ Definition 2.2. Given an alphabet Σ, a string over Σ (or simply a string) of length n is any function u:[n] Σ. → The integer n is the length of the string u, and it is denoted as u . When n = 0, the special string | | u: [0] Σ of length 0 is called the empty string, or null string, and is denoted as ǫ. → 9 10 CHAPTER 2. BASICS OF FORMAL LANGUAGE THEORY Given a string u:[n] Σ of length n 1, u(i) is the i-th letter in the string u. For simplicity of notation, we→ denote the string≥u as u = u1u2 ...un, with each u Σ. i ∈ For example, if Σ = a, b and u: [3] Σ is defined such that u(1) = a, u(2) = b, and u(3) = a, we write { } → u = aba. Other examples of strings are work, fun, gabuzomeuh Strings of length 1 are functions u: [1] Σ simply picking some element u(1) = ai in Σ. Thus, we will identify every symbol a Σ→ with the corresponding string of length 1. i ∈ The set of all strings over an alphabet Σ, including the empty string, is denoted as Σ∗. Observe that when Σ = , then ∅ ∗ = ǫ . ∅ { } When Σ = , the set Σ∗ is countably infinite. Later on, we will see ways of ordering and enumerating6 ∅ strings. Strings can be juxtaposed, or concatenated. Definition 2.3. Given an alphabet Σ, given any two strings u:[m] Σ and v :[n] Σ, the concatenation u v (also written uv) of u and v is the string → → · uv :[m + n] Σ, defined such that → u(i) if 1 i m, uv(i)= v(i m) if m≤+1≤ i m + n. − ≤ ≤ In particular, uǫ = ǫu = u. Observe that uv = u + v . | | | | | | For example, if u = ga, and v = buzo, then uv = gabuzo It is immediately verified that u(vw)=(uv)w. 2.1. ALPHABETS, STRINGS, LANGUAGES 11 Thus, concatenation is a binary operation on Σ∗ which is associative and has ǫ as an identity. Note that generally, uv = vu, for example for u = a and v = b. 6 Given a string u Σ∗ and n 0, we define un recursively as follows: ∈ ≥ u0 = ǫ un+1 = unu (n 0). ≥ Clearly, u1 = u, and it is an easy exercise to show that unu = uun, for all n 0. ≥ For the induction step, we have un+1u =(unu)u by definition of un+1 =(uun)u by the induction hypothesis = u(unu) by associativity = uun+1 by definition of un+1. Definition 2.4. Given an alphabet Σ, given any two strings u, v Σ∗ we define the following ∈ notions as follows: u is a prefix of v iff there is some y Σ∗ such that ∈ v = uy. u is a suffix of v iff there is some x Σ∗ such that ∈ v = xu. u is a substring of v iff there are some x, y Σ∗ such that ∈ v = xuy. We say that u is a proper prefix (suffix, substring) of v iff u is a prefix (suffix, substring) of v and u = v. 6 For example, ga is a prefix of gabuzo, zo is a suffix of gabuzo and buz is a substring of gabuzo. Recall that a partial ordering on a set S is a binary relation S S which is reflexive, transitive, and antisymmetric.≤ ≤ ⊆ × The concepts of prefix, suffix, and substring, define binary relations on Σ∗ in the obvious way. It can be shown that these relations are partial orderings.