Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Steve Wolfman, based on notes by Patrice Belleville and others.

Slides:



Advertisements
Similar presentations
CS 345: Chapter 9 Algorithmic Universality and Its Robustness
Advertisements

Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
CSE 105 Theory of Computation Alexander Tsiatas Spring 2012 Theory of Computation Lecture Slides by Alexander Tsiatas is licensed under a Creative Commons.
CSE 105 Theory of Computation Alexander Tsiatas Spring 2012 Theory of Computation Lecture Slides by Alexander Tsiatas is licensed under a Creative Commons.
Finite Automata CPSC 388 Ellen Walker Hiram College.
1 1 CDT314 FABER Formal Languages, Automata and Models of Computation Lecture 3 School of Innovation, Design and Engineering Mälardalen University 2012.
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
January 7, 2015CS21 Lecture 21 CS21 Decidability and Tractability Lecture 2 January 7, 2015.
CS21 Decidability and Tractability
Snick  snack CPSC 121: Models of Computation 2009 Winter Term 1 DFAs in Depth Steve Wolfman, based on notes by Patrice Belleville and others.
NFAs Sipser 1.2 (pages 47–54). CS 311 Fall Recall… Last time we showed that the class of regular languages is closed under: –Complement –Union.
1 Introduction to Computability Theory Lecture12: Decidable Languages Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture12: Reductions Prof. Amos Israeli.
Finite Automata Great Theoretical Ideas In Computer Science Anupam Gupta Danny Sleator CS Fall 2010 Lecture 20Oct 28, 2010Carnegie Mellon University.
1 Introduction to Computability Theory Lecture2: Non Deterministic Finite Automata Prof. Amos Israeli.
1 Introduction to Computability Theory Lecture3: Regular Expressions Prof. Amos Israeli.
Introduction to Computability Theory
NFAs Sipser 1.2 (pages 47–54). CS 311 Mount Holyoke College 2 Recall… Last time we showed that the class of regular languages is closed under: –Complement.
1 Introduction to Computability Theory Discussion1: Non-Deterministic Finite Automatons Prof. Amos Israeli.
Lecture 3UofH - COSC Dr. Verma 1 COSC 3340: Introduction to Theory of Computation University of Houston Dr. Verma Lecture 3.
Lexical Analysis III Recognizing Tokens Lecture 4 CS 4318/5331 Apan Qasem Texas State University Spring 2015.
CS5371 Theory of Computation
Courtesy Costas Busch - RPI1 Non Deterministic Automata.
1 Undecidability Andreas Klappenecker [based on slides by Prof. Welch]
Snick  snack CPSC 121: Models of Computation 2011 Winter Term 1 Revisiting Induction Steve Wolfman, based on work by Patrice Belleville and others 1.
Automata theory and formal languages Andrej Bogdanov The Chinese University of Hong Kong Fall 2008.
Automata & Formal Languages, Feodor F. Dragan, Kent State University 1 CHAPTER 1 Regular Languages Contents Finite Automata (FA or DFA) definitions, examples,
Snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Benjamin Israel Notes heavily borrowed from Steve Wolfman’s,
CS5371 Theory of Computation Lecture 6: Automata Theory IV (Regular Expression = NFA = DFA)
CS5371 Theory of Computation Lecture 10: Computability Theory I (Turing Machine)
Fall 2006Costas Busch - RPI1 Non-Deterministic Finite Automata.
CS5371 Theory of Computation Lecture 4: Automata Theory II (DFA = NFA, Regular Language)
CS5371 Theory of Computation Lecture 8: Automata Theory VI (PDA, PDA = CFG)
Introduction to Finite Automata Adapted from the slides of Stanford CS154.
Costas Busch - LSU1 Non-Deterministic Finite Automata.
1 Introduction to Computability Theory Lecture11: The Halting Problem Prof. Amos Israeli.
Foundations of (Theoretical) Computer Science Chapter 1 Lecture Notes (Section 1.2: NFA’s) David Martin With some modifications.
Remaining Topics Decidability Concept 4.1 The Halting Problem 4.2
Functions Definition & purpose Notation Functions on binary / returning binary values Finite automaton model We haven’t completely left the world of counting.
Nondeterministic Finite Automata CS 130: Theory of Computation HMU textbook, Chapter 2 (Sec 2.3 & 2.5)
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
REGULAR LANGUAGES.
1 Unit 1: Automata Theory and Formal Languages Readings 1, 2.2, 2.3.
Introduction to CS Theory Lecture 3 – Regular Languages Piotr Faliszewski
Lecture 05: Theory of Automata:08 Kleene’s Theorem and NFA.
CSE 105 Theory of Computation Alexander Tsiatas Spring 2012 Theory of Computation Lecture Slides by Alexander Tsiatas is licensed under a Creative Commons.
4b 4b Lexical analysis Finite Automata. Finite Automata (FA) FA also called Finite State Machine (FSM) –Abstract model of a computing entity. –Decides.
1 CD5560 FABER Formal Languages, Automata and Models of Computation Lecture 3 Mälardalen University 2010.
CMSC 330: Organization of Programming Languages Theory of Regular Expressions Finite Automata.
1Computer Sciences Department. Book: INTRODUCTION TO THE THEORY OF COMPUTATION, SECOND EDITION, by: MICHAEL SIPSER Reference 3Computer Sciences Department.
CS 203: Introduction to Formal Languages and Automata
Recognising Languages We will tackle the problem of defining languages by considering how we could recognise them. Problem: Is there a method of recognising.
CS 154 Formal Languages and Computability February 9 Class Meeting Department of Computer Science San Jose State University Spring 2016 Instructor: Ron.
Nondeterministic Finite Automata (NFAs). Reminder: Deterministic Finite Automata (DFA) q For every state q in Q and every character  in , one and only.
Donghyun (David) Kim Department of Mathematics and Computer Science North Carolina Central University 1 Chapter 4 Decidability Some slides are in courtesy.
Finite Automata Great Theoretical Ideas In Computer Science Victor Adamchik Danny Sleator CS Spring 2010 Lecture 20Mar 30, 2010Carnegie Mellon.
Automata & Formal Languages, Feodor F. Dragan, Kent State University 1 CHAPTER 3 The Church-Turing Thesis Contents Turing Machines definitions, examples,
CSCI 4325 / 6339 Theory of Computation Zhixiang Chen.
LECTURE 5 Scanning. SYNTAX ANALYSIS We know from our previous lectures that the process of verifying the syntax of the program is performed in two stages:
Complexity and Computability Theory I Lecture #5 Rina Zviel-Girshin Leah Epstein Winter
Pushdown Automata Chapter 12. Recognizing Context-Free Languages Two notions of recognition: (1) Say yes or no, just like with FSMs (2) Say yes or no,
CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014CSC-305 Design and Analysis of AlgorithmsBS(CS) -6 Fall-2014 Design and Analysis of Algorithms.
 2005 SDU Lecture11 Decidability.  2005 SDU 2 Topics Discuss the power of algorithms to solve problems. Demonstrate that some problems can be solved.
Lecture #4 Thinking of designing an abstract machine acts as finite automata. Advanced Computation Theory.
Lecture #5 Advanced Computation Theory Finite Automata.
THEORY OF COMPUTATION Lecture One: Automata Theory Automata Theory.
CPSC 121: Models of Computation 2008/9 Winter Term 2
CPSC 121: Models of Computation 2016W2
Automata theory and formal languages COS 3112 – AUTOMATA THEORY PRELIM PERIOD WEEK 1 AND 2.
Presentation transcript:

snick  snack CPSC 121: Models of Computation 2010 Winter Term 2 DFAs in Depth Steve Wolfman, based on notes by Patrice Belleville and others

Outline Prereqs, Learning Goals, and !Quiz Notes Formally Specifying DFAs Designing DFAs Analyzing DFAs: Can DFAs Count? Non-Deterministic Finite Automata: Are They More Powerful than DFAs? Combining DFA Languages CS Theory and the Halting Problem 2

Learning Goals: “Pre-Class” (REPEAT) The pre-class goals are to be able to: –Trace the operation of a deterministic finite- state automaton (represented as a diagram) on an input, including indicating whether the DFA accepts or rejects the input. –Deduce the language accepted by a simple DFA after working through multiple example inputs. 3

Learning Goals: In-Class By the end of this unit, you should be able to for the exam: –Build a DFA to recognize a particular language. [2010W2: already worked on this with Benj (guest lecturer)] –Identify the language recognized by a particular DFA. –Connect the graphical and formal representations of DFAs and illustrate how each part works.

Learning Goals: In-Class By the end of this unit, you should be able to for yourself: –Use the formalisms that we’ve learned so far (especially power sets and set union) to illustrate that nondeterministic finite automata are no more powerful than deterministic finite automata. –Demonstrate through a simple contradiction argument that there are interesting properties of programs that cannot be computed in general. –Describe how the theoretical story 121 has been telling connects to the branch of CS theory called automata theory.

Where We Are in The Big Stories Theory How do we model computational systems? Now: Establishing profound results about the power (and lack thereof) of our DFA model and of general computational systems. Hardware How do we build devices to compute? Now: Understood, in depth, a full-blown computer! 6 In other words: SUCCESS!!

Outline Prereqs, Learning Goals, and !Quiz Notes Formally Specifying DFAs Designing DFAs Analyzing DFAs: Can DFAs Count? Non-Deterministic Finite Automata: Are They More Powerful than DFAs? Combining DFA Languages CS Theory and the Halting Problem 7

Reminder: Formal DFA Specification Ithe (finite) set of letters in the input language. Sthe (finite) set of states. s 0 the start state; s 0  S Fthe set of accepting states; F  S N : S  I  S is the transition function. a b b a,b a

One Way to Formalize DFA Operation The algorithm “ RunDFA ” runs a given DFA on an input, resulting in “ Yes ”, “ No ”, or (if the input is invalid) “ Huh? ” RunDFA((I, S, s 0, F, N), in) 1.If in is empty, then: if s 0  F, return “ Yes ”, else return “ No ”. 2.Otherwise, let in 0 be the first element of in and in rest be the rest of in. 3.If in 0  I, return “ Huh? ” 4.Otherwise, let s' = N(s 0, in 0 ). 5.Return RunDFA((I, S, s', F, N), in rest ). a b b a,b a

Formal DFA Example (Fixed) A DFA is a five-tuple: (I, S, s 0, F, N) a b b a,b What is I ? S ? s 0 ? F ? N ? a,b a

Running the DFA How does the DFA runner work? a b b a,b a Input: abbab Only partly on the exam. (You should be able to simulate a DFA, but needn’t use RunDFA.)

Outline Prereqs, Learning Goals, and !Quiz Notes Formally Specifying DFAs Designing DFAs Analyzing DFAs: Can DFAs Count? Non-Deterministic Finite Automata: Are They More Powerful than DFAs? Combining DFA Languages CS Theory and the Halting Problem 12

Strategy for Designing a DFA To design a DFA for some language, try: 1.Should the empty string be accepted? If so, draw an accepting start state; else, draw a rejecting one. 2.From that state, consider what each letter would lead to. Group letters that lead to the same outcome. (Some may lead back to the start state if they’re no different from “starting over”.) 3.Decide for each new state whether it’s accepting. 4.Repeat steps 2 and 3 for new states as long as you have unfinished states, always trying to reuse existing states! As you go, give as descriptive names as you can to your states, to figure out when to reuse them!

Problem: Design a DFA to Recognize “Decimal Numbers”

Is this a decimal number? Let’s build a DFA to check whether an input is a decimal number. Which of the following is a decimal number? Rules for whether something is a decimal number?

Is this a decimal number? Something followed by ? is optional. Anything followed by a + can be repeated. [0-9] is a digit, an element of the set {0, 1, …, 9}. – is literally –. \. means. () are used to group elements together. Rules for a decimal number: -?[0-9]+(\.[0-9]+)?

Is this a decimal number? Let’s build a DFA to check whether an input is a decimal number. Which of the following is a decimal number? Rules for whether something is a decimal number?

Problem: Design a DFA to Recognize “Decimal Numbers” Problem: Design a DFA to recognize decimal numbers, defined using the regular expression: -?[0-9]+(\.[0-9]+)?

More DFA Design Problems Design a DFA that recognizes words that have two or more contiguous sequences of vowels. (This is a rough stab at “multisyllabic” words.) Design a DFA that recognizes base 4 numbers with at least one digit in which the digits are in sorted order. Design a DFA that recognizes strings containing the letters a, b, and c in which all the a s come before the first c and no two b s neighbour each other.

Outline Prereqs, Learning Goals, and !Quiz Notes Formally Specifying DFAs Designing DFAs Analyzing DFAs: Can DFAs Count? Non-Deterministic Finite Automata: Are They More Powerful than DFAs? Combining DFA Languages CS Theory and the Halting Problem 20

Can a DFA Count? Problem: Design a DFA to recognize a n b n : the language that includes all strings that start with some number of a s and end with the same number of b s.

DFAs Can’t Count Let’s prove it. The heart of the proof is the insight that a DFA can only have a finite number of states and so can only “count up” a finite number of a s. We proceed by contradiction. Assume some DFA recognizes the language a n b n.

DFAs Can’t Count Assume DFA counter recognizes a n b n. counter must have some finite number of states k = |S counter |. Consider the input a k b k. counter must repeat (at least) one state as it processes the k a s in that string. (Because it starts in s 0 and transitions k times; if it didn’t repeat a state, that would be k+1 states, which is greater than |S counter |.) (In fact, it repeats each state after the first one it repeats up to the b.)

DFAs Can’t Count Consider the input a k b k. counter must repeat (at least) one state as it processes the k a s in that string. counter accepts a k b k, meaning it ends in an accepting state.... Doesn’t necessarily look like this, but similar. a a aa a a b...

DFAs Can’t Count Now, consider the number of a s that are processed between the first and second time visiting the repeated state. Call it r. Give counter a (k-r) b k instead. (Note: r > 0.)... a a aa a a b

DFAs Can’t Count Give counter a (k-r) b k instead. counter must accept a (k-r) b k, which is not in the language a n b n. Contradiction! Thus, no DFA recognizes a n b n... a a aa a a b

Outline Prereqs, Learning Goals, and !Quiz Notes Formally Specifying DFAs Designing DFAs Analyzing DFAs: DFAs Can’t Count Non-Deterministic Finite Automata: Are They More Powerful than DFAs? Combining DFA Languages CS Theory and the Halting Problem 27

Let’s Try Something More Powerful (?): NFAs An NFA is like a DFA except: Any number (zero or more) of arcs can lead from each state for each letter of the alphabet There may be many start states When we run a DFA, we put a finger on the current state and then read one letter of the input at a time, transitioning from state to state. When we run an NFA, we may need many fingers to keep track of many current states. When we transition, we go to all the possible next states.

NFAs Continued An NFA is like a DFA except: Any number (zero or more) of arcs can lead from each state for each letter of the alphabet There may be many start states A DFA accepts when we end in an accepting state. An NFA accepts when at least one of the states we end in is accepting.

Another way to think of NFAs An NFA is a non-deterministic finite automaton. Instead of doing one predictable thing at each state given an input, it may have choices of what to do. If one of the choices leads to accepting the input, the NFA will make that choice. (It “magically” knows which choice to make.)

Decimal Number NFA... Is Easy! s1s2 -?[0-9]+(\.[0-9]+)? int dot real 0-9

Are NFAs More Powerful than DFAs? Problem: Can we prove or disprove that every language an NFA can recognize can be recognized by a DFA as well?? Strategy: Given an NFA, show how to build a DFA (i.e., I, S, s 0, F, and N) that accepts/rejects the same strings. Hint: Formally, what is the “group of states” an NFA can be in at any given time? Build the parts of the DFA based on that (i.e., S is the set of all such things).

Worked Problem: Are NFAs More Powerful than DFAs? Problem: Prove that every language an NFA can recognize can be recognized by a DFA as well. WLOG, consider an arbitrary NFA “C”. We’ll build a DFA “D” that does the same thing. We now build each element of the five-tuple defining D. We do so in such a way that D faithfully simulates N’s operation.

Worked Problem: Are NFAs More Powerful than DFAs? C has an alphabet I C. Let I D = I C. They operate on the same strings.

Worked Problem: Are NFAs More Powerful than DFAs? C’s states are S C. Let S D = P (S C ). Each state in D represents being in a subset of the states in C. (“Having fingers” on some of C’s states.) This is the central idea of the whole proof. (“ P ” is “power set”)

Worked Problem: Are NFAs More Powerful than DFAs? C starts in some subset S 0C of S C. Let s 0D = S 0C. D’s start state represents being in the set of start states in C. This is fine because each state in D is already a set of states in C anyway!

Worked Problem: Are NFAs More Powerful than DFAs? C has accepting states F C. Let F D = {S  S C | S  F C   }. A state in D is accepting if it represents a set of states in C that contains at least one accepting state. So, every subset of C’s states such that at least one of its states is an accepting state.

Worked Problem: Are NFAs More Powerful than DFAs? C has zero or more arcs leading out of each state in S C for each letter in I C. D’s transition function N D (s D, i) operates on a state s D that actually represents a set of states from S C. So, we just need to figure out all the states that now need “fingers on them”:

Worked Problem: Are NFAs More Powerful than DFAs? C has an alphabet I C. Let I D = I C. (They operate on the same strings.) C’s states are S C. Let S D = P (S C ). (Each state in D represents being in a subset of the states in C.) C starts in some subset S 0C of S C. Let s 0D = S 0C. (D’s start state represents being in the set of start states in C.) C has accepting states F C. Let F D = {S  S C | S  F C   }. (A state in D is accepting if it represents a set of states in C that contains at least one accepting state.) C has zero or more arcs leading out of each state in S C for each letter in I C. N D (s D, i) operates on a state s D that actually represents a set of states from S C. So: (“ P ” is “power set”)

Decimal Number NFA... as a DFA {s1, s2}{s2} {s2, int} {dot} {real} { } 0-9 . else LOTS of states left off that are unreachable from the start state.

Outline Prereqs, Learning Goals, and !Quiz Notes Formally Specifying DFAs Designing DFAs Analyzing DFAs: DFAs Can’t Count Non-Deterministic Finite Automata: No More Powerful than DFAs Combining DFA Languages CS Theory and the Halting Problem 41

Combining Languages Problem: Given a DFA that recognizes language A and a DFA that recognizes language B, can you build a DFA that recognizes: Any string in language A or B Only strings in both languages A and B Only strings that are composed of a string from A followed by a string from B

Worked Problem: Combining Languages Problem: Design a combined DFA “C” for any string in BOTH language A and B. First, why bother? Well, how could you build “a DFA over {a, b} that recognizes strings with an even number of b’s and exactly one a”?

Worked Problem: Combining Languages Problem: Design a combined DFA “C” for any string in BOTH language A and B. Strategy: build a DFA that “keeps track” of where it is in both DFA “A” and DFA “B”. Let I C = I A (which must also equal I B ). Let S C = S A  S B. Let s C0 = (s A0, s B0 ). Let F C = {(s A, s B )  S C | s A  F A  s B  F B }. (Only “double” states that accept in both DFA A and B.) Let N C ((s A, s B ), s) = (N A (s A, s), N B (s B, s)). (Transition to the next “double” state by tracking where A and B would go on each part of the state.) Idea: send one “finger” tracking through A and one through B.

Outline Prereqs, Learning Goals, and !Quiz Notes Formally Specifying DFAs Designing DFAs Analyzing DFAs: DFAs Can’t Count Non-Deterministic Finite Automata: No More Powerful than DFAs Combining DFA Languages CS Theory and the Halting Problem 45

Punch Line to one Big Story: Automata Theory DFAs are just as powerful as NFAs (although it may take 2 n states in a DFA to simulate an n-state NFA). One direction CS theory goes is exploring what different models of computation can do and how they’re related: automata theory. Computers (at least ones with “an arbitrary amount of memory”) are more powerful than DFAs; they can count. But… Are all models limited?

The Halting Problem A decision algorithm outputs only “yes” or “no” for an input (like a DFA). If we’re really smart, we can probably write a program to solve any given decision problem (in a finite amount of time), right? How about this one: Given a program and its input, decide whether the program ever finishes (halts). I.e., does it go into an infinite loop? This was one of the deepest questions in math (and CS!) in 1900, posed by David Hilbert.

The Halting Problem OK, let’s say we write the code for this inside the method doesHalt : public static boolean doesHalt(String program, String input) { // Don’t know what goes here, // but we assume (for contradiction!) it exists. } (define (does-halt program input) ;; Don’t know what goes here, ;; but we assume (for contradiction!) it exists. )

The Halting Problem Now, remember Russell’s Paradox and the self-referential “set that contains all sets that do not contain themselves”? How about a program that takes a program as input and checks whether it halts when given itself as input… public static void main(String[] args) { // Assume the input is args[1]. if (doesHalt(args[1], args[1])) while(true) ; else return; } Let’s call our program Russ, for short. It halts only if its input, if run on itself, would not halt.

The Halting Problem Now, remember Russell’s Paradox and the self-referential “set that contains all sets that do not contain themselves”? How about a program that takes a program as input and checks whether it halts when given itself as input… (define (russ input) (if (does-halt input input) (russ input) true)) Let’s call our program Russ, for short. It halts only if its input, if run on itself, would not halt.

The Halting Problem Now, run Russ with Russ itself as input. Does Russ halt under those circumstances or not? Remember: Russ halts if and only if: its input does not halt when run on itself. QED, Halt, Full Stop. Proof (originally) thanks to Kurt Gödel.