Nondeterministic Finite Automata. Nondeterminism Subset Construction

Similar documents
Nondeterminism and Epsilon Transitions

Nondeterminism. Nondeterministic Finite Automata. Example: Moves on a Chessboard. Nondeterminism (2) Example: Chessboard (2) Formal NFA

Nondeterministic Finite Automata

Finite Automata. BİL405 - Automata Theory and Formal Languages 1

Regular Expressions and Language Properties

Chapter Five: Nondeterministic Finite Automata

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

Lecture 4 Nondeterministic Finite Accepters

T (s, xa) = T (T (s, x), a). The language recognized by M, denoted L(M), is the set of strings accepted by M. That is,

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Chap. 1.2 NonDeterministic Finite Automata (NFA)

Theory of Computation (I) Yijia Chen Fudan University

Finite Automata and Regular Languages

UNIT-II. NONDETERMINISTIC FINITE AUTOMATA WITH ε TRANSITIONS: SIGNIFICANCE. Use of ε-transitions. s t a r t. ε r. e g u l a r

Extended transition function of a DFA

CMPSCI 250: Introduction to Computation. Lecture #22: From λ-nfa s to NFA s to DFA s David Mix Barrington 22 April 2013

Deterministic Finite Automata. Non deterministic finite automata. Non-Deterministic Finite Automata (NFA) Non-Deterministic Finite Automata (NFA)

Lecture 1: Finite State Automaton

Closure under the Regular Operations

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

CS 154, Lecture 2: Finite Automata, Closure Properties Nondeterminism,

Closure Properties of Regular Languages. Union, Intersection, Difference, Concatenation, Kleene Closure, Reversal, Homomorphism, Inverse Homomorphism

CS 154, Lecture 3: DFA NFA, Regular Expressions

Nondeterministic finite automata

Lecture 3: Nondeterministic Finite Automata

Outline. Nondetermistic Finite Automata. Transition diagrams. A finite automaton is a 5-tuple (Q, Σ,δ,q 0,F)

CS 208: Automata Theory and Logic

Languages. Non deterministic finite automata with ε transitions. First there was the DFA. Finite Automata. Non-Deterministic Finite Automata (NFA)

Nondeterministic Finite Automata

Automata and Languages

CS 154 Formal Languages and Computability Assignment #2 Solutions

Nondeterministic Finite Automata

Nondeterministic Finite Automata

September 11, Second Part of Regular Expressions Equivalence with Finite Aut

CS 455/555: Finite automata

CS 154. Finite Automata, Nondeterminism, Regular Expressions

CSE 105 Theory of Computation Professor Jeanne Ferrante

Introduction to the Theory of Computation. Automata 1VO + 1PS. Lecturer: Dr. Ana Sokolova.

CSE 135: Introduction to Theory of Computation Nondeterministic Finite Automata (cont )

Intro to Theory of Computation

Subset construction. We have defined for a DFA L(A) = {x Σ ˆδ(q 0, x) F } and for A NFA. For any NFA A we can build a DFA A D such that L(A) = L(A D )

Introduction to the Theory of Computation. Automata 1VO + 1PS. Lecturer: Dr. Ana Sokolova.

Chapter 6: NFA Applications

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

Theory of Computation (II) Yijia Chen Fudan University

Theory of Languages and Automata

Finite Automata and Regular Languages (part III)

Regular Language Equivalence and DFA Minimization. Equivalence of Two Regular Languages DFA Minimization

Regular Expressions. Definitions Equivalence to Finite Automata

Course 4 Finite Automata/Finite State Machines

Fooling Sets and. Lecture 5

Harvard CS 121 and CSCI E-207 Lecture 4: NFAs vs. DFAs, Closure Properties

Decision, Computation and Language

CMSC 330: Organization of Programming Languages

CS243, Logic and Computation Nondeterministic finite automata

Computational Models Lecture 2 1

Computational Models Lecture 2 1

CS21 Decidability and Tractability

CSE 311 Lecture 25: Relating NFAs, DFAs, and Regular Expressions. Emina Torlak and Kevin Zatloukal

Sri vidya college of engineering and technology

Chapter 2: Finite Automata

Formal Models in NLP

Computational Theory

Introduction to Formal Languages, Automata and Computability p.1/51

Non-deterministic Finite Automata (NFAs)

Java II Finite Automata I

Recitation 2 - Non Deterministic Finite Automata (NFA) and Regular OctoberExpressions

(Refer Slide Time: 0:21)

Deterministic (DFA) There is a fixed number of states and we can only be in one state at a time

CMSC 330: Organization of Programming Languages. Theory of Regular Expressions Finite Automata

Constructions on Finite Automata

UNIT-III REGULAR LANGUAGES

CS 322 D: Formal languages and automata theory

Regular expressions and Kleene s theorem

Inf2A: Converting from NFAs to DFAs and Closure Properties

CSC236 Week 10. Larry Zhang

CSE 105 THEORY OF COMPUTATION

CPS 220 Theory of Computation REGULAR LANGUAGES

CISC 4090: Theory of Computation Chapter 1 Regular Languages. Section 1.1: Finite Automata. What is a computer? Finite automata

CSE 135: Introduction to Theory of Computation Nondeterministic Finite Automata

Finite Automata Part Two

Homomorphisms and Efficient State Minimization

Theory of computation: initial remarks (Chapter 11)

Clarifications from last time. This Lecture. Last Lecture. CMSC 330: Organization of Programming Languages. Finite Automata.

September 7, Formal Definition of a Nondeterministic Finite Automaton

Lecture 5: Minimizing DFAs

Takeaway Notes: Finite State Automata

Nondeterminism. September 7, Nondeterminism

Properties of Regular Languages. BBM Automata Theory and Formal Languages 1

Regular expressions and Kleene s theorem

3515ICT: Theory of Computation. Regular languages

Uses of finite automata

Finite Automata. Seungjin Choi

CSE 311: Foundations of Computing. Lecture 23: Finite State Machine Minimization & NFAs

Theory of Computation

Automata and Formal Languages - CM0081 Finite Automata and Regular Expressions

Lecture 2: Regular Expression

CS 121, Section 2. Week of September 16, 2013

CS 530: Theory of Computation Based on Sipser (second edition): Notes on regular languages(version 1.1)

Examples of Regular Expressions. Finite Automata vs. Regular Expressions. Example of Using flex. Application

Transcription:

Nondeterministic Finite Automata Nondeterminism Subset Construction 1

Nondeterminism A nondeterministic finite automaton has the ability to be in several states at once. Transitions from a state on an input symbol can be to any set of states. 2

Nondeterminism (2) Start in one start state. Accept if any sequence of choices leads to a final state. Intuitively: the NFA always guesses right. 3

Example: Moves on a States = squares. Chessboard Inputs = r (move to an adjacent red square) and b (move to an adjacent black square). Start state, final state are in opposite corners. 4

Example: Chessboard (2) 1 1 2 3 4 5 6 7 8 9 r b b 2 1 4 3 5 7 5 1 3 7 9 * r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 Accept, since final state reached 5

Formal NFA A finite set of states, typically Q. An input alphabet, typically Σ. A transition function, typically δ. A start state in Q, typically q 0. A set of final states F Q. 6

Transition Function of an NFA δ(q, a) is a set of states. Extend to strings as follows: Basis: δ(q, ε) = {q} Induction: δ(q, wa) = the union over all states p in δ(q, w) of δ(p, a) 7

Language of an NFA A string w is accepted by an NFA if δ(q 0, w) contains at least one final state. That is, there exists a sequence of valid transitions from q 0 to a final state given the input w. The language of the NFA is the set of strings it accepts. 8

Example NFA Set of all strings with two consecutive a s or two consecutive b s: Note that some states have an empty transition on an a or b, and some have multiple transitions on a or b. 9

Example 2: Language of an NFA 1 2 3 4 5 6 7 8 9 For our chessboard NFA we saw that rbb is accepted. If the input consists of only b s, the set of accessible states alternates between {5} and {1,3,7,9}, so only even-length, nonempty strings of b s are accepted. What about strings with at least one r? 10

Equivalence of DFA s, NFA s A DFA can be turned into an NFA that accepts the same language. If δ D (q, a) = p, let the NFA have δ N (q, a) = {p}. Then the NFA is always in a set containing exactly one state the state the DFA is in after reading the same input. 11

Equivalence (2) Surprisingly, for any NFA there is a DFA that accepts the same language. Proof is the subset construction. The number of states of the DFA can be exponential in the number of states of the NFA. Thus, NFA s accept exactly the regular languages. 12

Subset Construction Given an NFA with states Q, inputs Σ, transition function δ N, state state q 0, and final states F, construct equivalent DFA with: States 2 Q (Set of subsets of Q). Inputs Σ. Start state {q 0 }. Final states = all those with a member of F. 13

Critical Point The DFA states have names that are sets of NFA states. But as a DFA state, an expression like {p,q} must be read as a single symbol, not as a set. Analogy: a class of objects whose values are sets of objects of another class. 14

Subset Construction (2) The transition function δ D is defined by: δ D ({q 1,,q k }, a) is the union over all i = 1,,k of δ N (q i, a). Example: We ll construct the DFA equivalent of our chessboard NFA. 15

Example: Subset Construction * r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 r b {1} {2,4} {5} {2,4} {5} Alert: What we re doing here is the lazy form of DFA construction, where we only construct a state if we are forced to. 16

Example: Subset Construction * r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 r b {1} {2,4} {5} {2,4} {2,4,6,8} {1,3,5,7} {5} {2,4,6,8} {1,3,5,7} 17

Example: Subset Construction * r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 r b {1} {2,4} {5} {2,4} {2,4,6,8} {1,3,5,7} {5} {2,4,6,8} {1,3,7,9} {2,4,6,8} {1,3,5,7} * {1,3,7,9} 18

Example: Subset Construction * r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 r b {1} {2,4} {5} {2,4} {2,4,6,8} {1,3,5,7} {5} {2,4,6,8} {1,3,7,9} {2,4,6,8} {2,4,6,8} {1,3,5,7,9} {1,3,5,7} * {1,3,7,9} * {1,3,5,7,9} 19

Example: Subset Construction * r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 r b {1} {2,4} {5} {2,4} {2,4,6,8} {1,3,5,7} {5} {2,4,6,8} {1,3,7,9} {2,4,6,8} {2,4,6,8} {1,3,5,7,9} {1,3,5,7} {2,4,6,8} {1,3,5,7,9} * {1,3,7,9} * {1,3,5,7,9} 20

Example: Subset Construction * r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 r b {1} {2,4} {5} {2,4} {2,4,6,8} {1,3,5,7} {5} {2,4,6,8} {1,3,7,9} {2,4,6,8} {2,4,6,8} {1,3,5,7,9} {1,3,5,7} {2,4,6,8} {1,3,5,7,9} * {1,3,7,9} {2,4,6,8} {5} * {1,3,5,7,9} 21

Example: Subset Construction * r b 1 2,4 5 2 4,6 1,3,5 3 2,6 5 4 2,8 1,5,7 5 2,4,6,8 1,3,7,9 6 2,8 3,5,9 7 4,8 5 8 4,6 5,7,9 9 6,8 5 r b {1} {2,4} {5} {2,4} {2,4,6,8} {1,3,5,7} {5} {2,4,6,8} {1,3,7,9} {2,4,6,8} {2,4,6,8} {1,3,5,7,9} {1,3,5,7} {2,4,6,8} {1,3,5,7,9} * {1,3,7,9} {2,4,6,8} {5} * {1,3,5,7,9} {2,4,6,8} {1,3,5,7,9} 22

Proof of Equivalence: Subset Construction The proof is almost a pun. Show by induction on w that δ N (q 0, w) = δ D ({q 0 }, w) Basis: w = ε: δ N (q 0, ε) = δ D ({q 0 }, ε) = {q 0 }. 23

Induction Assume IH for strings shorter than w. Let w = xa; IH holds for x. Let δ N (q 0, x) = δ D ({q 0 }, x) = S. Let T = the union over all states p in S of δ N (p, a). Then δ N (q 0, w) = δ D ({q 0 }, w) = T. For NFA: the extension of δ N. For DFA: definition of δ D plus extension of δ D. That is, δ D (S, a) = T; then extend δ D to w = xa. 24

NFA s With ε-transitions We can allow state-to-state transitions on ε input. These transitions are done spontaneously, without looking at the input string. A convenience at times, but still only regular languages are accepted. 25

Example: ε-nfa A 1 0 ε B E 1 1 C 0 ε ε 0 F D * 0 1 ε A {E} {B} B {C} {D} C {D} D E {F} {B, C} F {D} 26

Closure of States CL(q) = set of states you can reach from state q following only arcs labeled ε. Example: CL(A) = {A}; 1 B 1 ε C 1 CL(E) = {B, C, D, E}. A ε ε Closure of a set of states = union of the closure of each state. 0 E 0 F 0 27 D

Extended Delta Basis: δ (q, ε) = CL(q). Induction: δ (q, xa) is computed as follows: 1. Start with (q, x) = S. δ 2. Take the union of CL(δ(p, a)) for all p in S. Intuition: δ (q, w) is the set of states you can reach from q following a path labeled w. And notice that δ(q, a) is not that set of states, for symbol a. 28

Example: Extended Delta δ (A, ε) = CL(A) = {A}. 1 A 0 1 C 1 δ (A, 0) = CL({E}) = {B, C, D, E}. δ (A, 01) = CL({C, D}) = {C, D}. Language of an ε-nfa is the set of strings w such that δ (q 0, w) contains a final state. ε B E 0 ε ε 0 F D 29

Equivalence of NFA, ε-nfa Every NFA is an ε-nfa. It just has no transitions on ε. Converse requires us to take an ε-nfa and construct an NFA that accepts the same language. We do so by combining ε transitions with the next transition on a real input. Warning: This treatment is a bit different from that in the text. 30

Picture of ε-transition Removal a a Transitions on ε a Transitions on ε 31

Picture of ε-transition Removal Text goes from here a a To here, and performs the subset construction Transitions on ε a Transitions on ε 32

Picture of ε-transition Removal To here, with no subset construction We ll go from here a a Transitions on ε a Transitions on ε 33

Equivalence (2) Start with an ε-nfa with states Q, inputs Σ, start state q 0, final states F, and transition function δ E. Construct an ordinary NFA with states Q, inputs Σ, start state q 0, final states F, and transition function δ N. 34

Equivalence (3) Compute δ N (q, a) as follows: 1. Let S = CL(q). 2. δ N (q, a) is the union over all p in S of δ E (p, a). F = the set of states q such that CL(q) contains a state of F. Intuition: δ N incorporates ε transitions before using a but not after. 35

Equivalence (4) Prove by induction on w that CL(δ N (q 0, w)) = δ E (q 0, w). Thus, the ε-nfa accepts w if and only if the ordinary NFA does. 36

* Interesting closures: CL(B) = {B,D}; CL(E) = {B,C,D,E} 0 1 ε A {E} {B} B {C} {D} C {D} D E {F} {B, C} F {D} ε-nfa * * * Since closures of B and E include final state D. Example: ε-nfato-nfa 0 1 A {E} {B} B {C} C {D} D E {F} {C, D} F {D} Since closure of E includes B and C; which have transitions on 1 to C and D. 37

Summary DFA s, NFA s, and ε NFA s all accept exactly the same set of languages: the regular languages. The NFA types are easier to design and may have exponentially fewer states than a DFA. But only a DFA can be implemented in linear time! 38