Finite Automata. Reading: Chapter 2



Similar documents
Finite Automata. Reading: Chapter 2

6.045: Automata, Computability, and Complexity Or, Great Ideas in Theoretical Computer Science Spring, Class 4 Nancy Lynch

Formal Languages and Automata Theory - Regular Expressions and Finite Automata -

Reading 13 : Finite State Automata and Regular Expressions

Introduction to Automata Theory. Reading: Chapter 1

Automata and Computability. Solutions to Exercises

CSC4510 AUTOMATA 2.1 Finite Automata: Examples and D efinitions Definitions

Regular Expressions and Automata using Haskell

Formal Deænition of Finite Automaton. 1. Finite set of states, typically Q. 2. Alphabet of input symbols, typically æ.

Finite Automata and Regular Languages

Compiler Construction

Automata and Formal Languages

Deterministic Finite Automata

Regular Languages and Finite State Machines

Automata on Infinite Words and Trees

CS154. Turing Machines. Turing Machine. Turing Machines versus DFAs FINITE STATE CONTROL AI N P U T INFINITE TAPE. read write move.

T Reactive Systems: Introduction and Finite State Automata

Turing Machines: An Introduction

6.080/6.089 GITCS Feb 12, Lecture 3

Honors Class (Foundations of) Informatics. Tom Verhoeff. Department of Mathematics & Computer Science Software Engineering & Technology

CSE 135: Introduction to Theory of Computation Decidability and Recognizability

3515ICT Theory of Computation Turing Machines

The Halting Problem is Undecidable

Informatique Fondamentale IMA S8

Scanner. tokens scanner parser IR. source code. errors

Genetic programming with regular expressions

C H A P T E R Regular Expressions regular expression

Pushdown automata. Informatics 2A: Lecture 9. Alex Simpson. 3 October, School of Informatics University of Edinburgh als@inf.ed.ac.

CS5236 Advanced Automata Theory

Regular Languages and Finite Automata

CS 3719 (Theory of Computation and Algorithms) Lecture 4

24 Uses of Turing Machines

Pushdown Automata. place the input head on the leftmost input symbol. while symbol read = b and pile contains discs advance head remove disc from pile

Automata Theory. Şubat 2006 Tuğrul Yılmaz Ankara Üniversitesi

Theory of Computation Chapter 2: Turing Machines

Overview of E0222: Automata and Computability

The Optimum One-Pass Strategy for Juliet

Increasing Interaction and Support in the Formal Languages and Automata Theory Course

SRM UNIVERSITY FACULTY OF ENGINEERING & TECHNOLOGY SCHOOL OF COMPUTING DEPARTMENT OF SOFTWARE ENGINEERING COURSE PLAN

1 Definition of a Turing machine

Full and Complete Binary Trees

(IALC, Chapters 8 and 9) Introduction to Turing s life, Turing machines, universal machines, unsolvable problems.

Today s Agenda. Automata and Logic. Quiz 4 Temporal Logic. Introduction Buchi Automata Linear Time Logic Summary

ω-automata Automata that accept (or reject) words of infinite length. Languages of infinite words appear:

Semantics of UML class diagrams

6.3 Conditional Probability and Independence

Omega Automata: Minimization and Learning 1

1. Nondeterministically guess a solution (called a certificate) 2. Check whether the solution solves the problem (called verification)

Lecture Note 1 Set and Probability Theory. MIT Spring 2006 Herman Bennett

Computational Models Lecture 8, Spring 2009

Course Manual Automata & Complexity 2015

Lecture I FINITE AUTOMATA

Graph Theory Problems and Solutions

Data Structures Fibonacci Heaps, Amortized Analysis

On Winning Conditions of High Borel Complexity in Pushdown Games

CAs and Turing Machines. The Basis for Universal Computation

Computability Theory

NFAs with Tagged Transitions, their Conversion to Deterministic Automata and Application to Regular Expressions

CS103B Handout 17 Winter 2007 February 26, 2007 Languages and Regular Expressions

Relational Databases

Philadelphia University Faculty of Information Technology Department of Computer Science First Semester, 2007/2008.

CS 301 Course Information

A first step towards modeling semistructured data in hybrid multimodal logic

ASSIGNMENT ONE SOLUTIONS MATH 4805 / COMP 4805 / MATH 5605

Models of Sequential Computation

On line construction of suffix trees 1

Model 2.4 Faculty member + student

6.080 / Great Ideas in Theoretical Computer Science Spring 2008

x a x 2 (1 + x 2 ) n.

COMPUTER SCIENCE TRIPOS

CMPSCI 250: Introduction to Computation. Lecture #19: Regular Expressions and Their Languages David Mix Barrington 11 April 2013

CHAPTER 7 GENERAL PROOF SYSTEMS

03 - Lexical Analysis

Cartesian Products and Relations

Diagonalization. Ahto Buldas. Lecture 3 of Complexity Theory October 8, Slides based on S.Aurora, B.Barak. Complexity Theory: A Modern Approach.

On String Languages Generated by Spiking Neural P Systems

Formal Grammars and Languages

Lecture 2: Universality

6.042/18.062J Mathematics for Computer Science. Expected Value I

1. Prove that the empty set is a subset of every set.

Testing LTL Formula Translation into Büchi Automata

How To Compare A Markov Algorithm To A Turing Machine

Regular Languages and Finite Automata

Lecture 2: Regular Languages [Fa 14]

Counters are sequential circuits which "count" through a specific state sequence.

Solutions to In-Class Problems Week 4, Mon.

Techniques algébriques en calcul quantique

Two Way F finite Automata and Three Ways to Solve Them

Two-dimensional Languages

Basics of Counting. The product rule. Product rule example. 22C:19, Chapter 6 Hantao Zhang. Sample question. Total is 18 * 325 = 5850

Constructing Automata from Temporal Logic Formulas : A Tutorial

Transcription:

Finite Automata Reading: Chapter 2 1

Finite Automata Informally, a state machine that comprehensively captures all possible states and transitions that a machine can take while responding to a stream (or sequence) of input symbols Recognizer for Regular Languages Deterministic Finite Automata (DFA) The machine can exist in only one state at any given time Non-deterministic Finite Automata (NFA) The machine can exist in multiple states at the same time 2

Deterministic Finite Automata - Definition A Deterministic Finite Automaton (DFA) consists of: Q ==> a finite set of states ==> a finite set of input symbols (alphabet) q 0 ==> astart state F ==> set of final states δ ==> a transition function, which is a mapping between Q x ==> Q A DFA is defined by the 5-tuple: {Q,, q 0,F, δ } 3

How to use a DFA? Input: a word w in * Question: Is w acceptable by the DFA? Steps: Start at the start state q 0 For every input symbol in the sequence w do Compute the next state from the current state, given the current input symbol in w and the transition function If after all symbols in w are consumed, the current state is one of the final states (F) then accept w; Otherwise, reject w. 4

Regular Languages Let L(A) be a language recognized by a DFA A. Then L(A) is called a Regular Language. Locate regular languages in the Chomsky Hierarchy 5

Example #1 Build a DFA for the following language: L = {w w is a binary string that contains 01 as a substring} Steps for building a DFA to recognize L: = {0,1} Decide on the states: Q Designate start t state t and final state(s) t δ: Decide on the transitions: Final states == same as accepting states Other states == same as non-accepting states 6

Regular expression: (0+1)*01(0+1)* DFA for strings containing 01 What makes the DFA deterministic? Q={q 0,q 1,q 2 } 1 0,1 0 start q 0 0 q 1 1 q2 Final state = {0,1} start state = q 0 F = {q 2 } Transition table symbols 0 1 q 0 q 1 q 0 What if the language g allows q 1 q 1 q 2 empty strings? *q 2 q 2 q 2 stat tes 7

Example #2 Clamping Logic: A clamping circuit waits for a 1 input, and turns on forever. However, to avoid clamping on spurious noise, we ll design a DFA that waits for two consecutive 1s in a row before clamping on. Build a DFA for the following language: g L = { w w is a bit string which contains the substring 11} State Design: q 0 : start state (initially off), also means the most recent input was not a 1 q 1 : has never seen 11 but the most recent input was a 1 q 2 : has seen 11 at least once 8

Example #3 Build a DFA for the following language: L = { w w has an even number of 0s and an even number of 1s}? Note: Alphabet implied is {0,1} 9

Extension of transitions (δ) to Paths (δ) δ (q 0,w) = ending state of the path taken from q 0 on input string w 0 δ (q 0,wa) = δ (δ(q 0,w), a) Exercise: Work out example #3 using the input sequence w=10010, a=1 10

Language of a DFA A DFA A accepts w if there is exactly a path from q 0 to an accepting (or final) state that is labeled by w i.e., L(A) = { w δ(q 0,w) Є F } I.e., L(A) = all strings that lead to a final state from q 0 11

Non-deterministic Finite Automata (NFA) A Non-deterministic Finite Automaton (NFA) is of course non-deterministic Implying that the machine can exist in more than one state at the same time Outgoing transitions could be non-deterministic q i 1 1 q j q k Each transition function therefore maps to a set of states 12

Non-deterministic Finite Automata (NFA) A Non-deterministic Finite Automaton (NFA) consists of: Q ==> a finite set of states ==> a finite set of input symbols (alphabet) q 0 ==> a start state F ==> set of final states δ ==> a transition function, which is a mapping between Q x ==> subset of Q An NFA is also defined by the 5-tuple: {Q,, q 0,F, δ } 13

How to use an NFA? Input: a word w in * Question: Is w acceptable by the NFA? Steps: Start at the start state q 0 For every input symbol in the sequence w do Determine all the possible next states from the current state, given the current input symbol in w and the transition function If after all symbols in w are consumed, at least one of the current states is a final state then accept w; Otherwise, reject w. 14

Regular expression: (0+1)*01(0+1)* NFA for strings containing 01 Why is this non-deterministic? 0,1 0,1 start 0 1 q2 q 0 q 1 What will happen if at state q 1 an input of 0 is received? ed Final state sta ates Q={q 0,q 1,q 2 } = {0,1} start state = q 0 F = {q 2 } Transition table symbols 0 1 q 0 {q 0,q 1 } {q 0 } q 1 Φ {q 2 } *q 2 {q 2 } {q 2 } 15

Example #2 Build an NFA for the following language: L = { w w ends in 01}? How about a DFA now? 16

Advantages & Caveats for NFA Great for modeling regular expressions String gprocessing e.g., grep, lexical analyzer But imaginary, in the sense that it has to be implemented deterministically in practice Could a non-deterministic state machine be implemented in practice? E.g., toss of a coin, a roll of dice 17

Differences: DFA vs. NFA DFA 1. All transitions are deterministic Each transition leads to exactly one state 2. For each state, transition on all possible symbols (alphabet) should be defined 3. Accepts input if the last state is in F 4. Sometimes harder to construct because of the number of states 5. Practical implementation is feasible NFA 1. Transitions could be nondeterministic A transition could lead to a subset of states 2. For each state, not all symbols necessarily have to be defined in the transition function 3. Accepts input if one of the last states is in F 4. Generally easier than a DFA to construct 5. Practical implementation has to be deterministic (so needs conversion to DFA) But, DFAs and NFAs are equivalent (in their power)!! 18

Extension of δ to NFA Paths Basis: δ (q, ) = {q} Induction: Let δ (q 0,w) = {p 1,p 2,p k } δ (p i,a) = S i for i=1,2...,k Then, δ (q 0,wa) = S 1 US 2 U US U k 19

Language of an NFA An NFA accepts w if there exists at least one path from the start state to an accepting (or final) state that is labeled by w L(N) = { w δ(q 0,w) F Φ } 20

Equivalence of DFA & NFA Should be true for any L Theorem: A language L is accepted by a DFA if and only if it is accepted by an NFA. Proof: 1. If part: Proof by subset construction (in the next few slides ) 2. Only-if part: Every DFA is a special case of an NFA where each state has exactly one transition for every input symbol. Therefore, ifli is accepted dby a DFA, iti is accepted dby a corresponding NFA. 21

Proof for the if-part If-part: A language L is accepted by a DFA if it is accepted by an NFA rephrasing Given any NFA N, we can construct a DFA D such that L(N)=L(D) How to construct a DFA from an NFA? Observation: the transition function of an NFA maps to subsets of states Idea: Make one DFA state for every possible subset of the NFA states Subset construction 22

NFA to DFA by subset construction Let N = {Q N,,δ N,q 0,F N } Goal: Build D={Q D,,δ D D,{q 0 },F D D} s.t. L(D)=L(N) Construction: 1. Q D = all subsets of Q N (i.e., power set) 2. F D =set of subsets S of Q N s.t. S F N Φ 3. δ D : for each subset S of Q N and for each input symbol a in : δ D (S,a) =Uδ δ N (p,a) p in s 23

Idea: To avoid enumerating all of power set, do lazy creation of states NFA to DFA construction: Example NFA: 01 0,1 L = {w w ends in 01} 0 q 0 q 1 1 q 2 DFA: 1 0 0 1 {q 0 } {q 0,q 1 } 0 {q 0,q 2 } 1 δ N 0 1 δ D 0 1 δ D 0 1 Ø Ø Ø {q 0 } {q 0,q 1 } {q 0 } q 0 {q 0,q 1 } {q 0 } {q 0 } {q 0,q 1 } {q 0 } {q 0,q 1 } {q 0,q 1 } {q 0,q 2 } q 1 Ø {q 2 } {q 1 } Ø {q 2 } *{q 0,q 2 } {q 0,q 1 } {q 0 } *q 2 Ø Ø *{q 2 } Ø Ø {q 0,q 1 } {q 0,q 1 } {q 0,q 2 } Remove states unreachable from q 0 *{q 0,q 2 } {q 0,q 1 } {q 0 } *{q 1,q 2 } Ø {q 2 } *{q 0,q 1,q 2 } {q 0,q 1 } {q 0,q 2 } 24

NFA to DFA: Repeating the example using LAZY CREATION NFA: 01 0,1 L = {w w ends in 01} 0 q 0 q 1 1 q 2 DFA: 1 0 0 1 {q 0 } {q 0,q 1 } 0 {q 0,q 2 } 1 δ N 0 1 q 0 {q 0,q 1 } {q 0 } q 1 Ø {q 2 } δ D 0 1 {q 0 } {q 0,q 1 } {q 0 } {q 0,q 1 } {q 0,q 1 } {q 0,q 2 } *q 2 Ø Ø *{q 0,q 2 } {q 0,q 1 } {q 0 } Main Idea: Introduce states as you go (on a need basis) 25

Correctness of subset construction Theorem: If D is the DFA constructed from NFA N by subset construction, then L(D)=L(N) Proof: Show that δ D ({q 0 },w) = δ N (q 0,w} Using induction on w s ws length: Let w = xa δ D ({q 0 }xa)=δ },xa) δ D ( δ N (q 0,x}, a ) = δ N (q 0,w} 26

A bad case for subset construction L = {w w is a binary string s.t., the n th symbol from its end is a 1} NFA has n+1 states DFA needs to have at least 2 n states Pigeon hole principle m holes and >m pigeons => at least one hole has to contain two or more pigeons 27

Dead states Example: A DFA for recognizing the key word while w h i l e q 0 q 1 q 2 q 3 q 4 q 5 Any other input symbol q dead 28

Applications Text indexing inverted indexing For each unique word in the database, store all locations that contain it using an NFA or a DFA Find pattern P in text t T Example: Google querying Et Extensions of fthis idea: PATRICIA tree, suffix tree 29

FA with Epsilon-Transitions We can allow -transitions in finite automata i.e., a state can jump to another state without consuming any input symbol Use: Makes it easier sometimes for NFA construction -NFAs have to one more column in their transition table 30

Example of an -NFA L = {w possibily empty w s.t. if non-empty will end in 01} start q 0 0,1 δ E 0 1 0 1 q 0 q 1 *q 0 Ø Ø {q 0,q 0 } q 0 {q 0,q 1 } {q 0 } {q 0 } q 1 Ø {q 2 } {q 1 } *q 2 Ø Ø {q 2 } q 2 -closure of a state q, ECLOSE(q), is the set of all states that can be reached from q by repeatedly making - transitions (including ECLOSE(q 0 ) itself). ECLOSE(q 0 ) 31

Equivalency of DFA, NFA, -NFA Theorem: A language L is accepted by some -NFA if and only if L is accepted by some DFA Proof: Idea: Use the same subset construction except include -closures 32

Eliminating -transitions Let E = {Q E,,δ E,q 0,F E } Goal: Build D={Q D,,δ D,{q D },F D } s.t. L(D)=L(E) Construction: ti 1. Q D = all reachable subsets of Q E factoring in -closures 2. q d = ECLOSE(q 0 ) 3. F D =subsets S in Q D s.t. S F E Φ 4. δ D : for each subset S of Q E and for each input symbol a in : Let R= p in Us δ E (p,a) δ D (S,a) = r in UR D( ECLOSE(r) () 33

Summary DFA Definition Transition diagrams & tables Regular language NFA Definition Transition diagrams & tables DFA vs. NFA NFA to DFA conversion using subset construction Equivalency of DFA & NFA Removal of redundant states and including dead states -transitions in NFA Pigeon hole principles Text searching applications 34