CSE 135: Introduction to Theory of Computation Nondeterministic Finite Automata

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

Nondeterministic Finite Automata

CSE 135: Introduction to Theory of Computation Equivalence of DFA and NFA

Chapter Five: Nondeterministic Finite Automata

Finite Automata. Mahesh Viswanathan

Nondeterminism. September 7, Nondeterminism

Closure under the Regular Operations

Nondeterministic Finite Automata

Chap. 1.2 NonDeterministic Finite Automata (NFA)

Theory of Computation (I) Yijia Chen Fudan University

CSE 105 Theory of Computation Professor Jeanne Ferrante

CS 154. Finite Automata, Nondeterminism, Regular Expressions

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

September 7, Formal Definition of a Nondeterministic Finite Automaton

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

Lecture 3: Nondeterministic Finite Automata

Inf2A: Converting from NFAs to DFAs and Closure Properties

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

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

Finite Automata. Seungjin Choi

Context-Free Languages

Deterministic Finite Automata (DFAs)

Deterministic Finite Automata (DFAs)

CS 154, Lecture 3: DFA NFA, Regular Expressions

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

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

Computer Sciences Department

Recap DFA,NFA, DTM. Slides by Prof. Debasis Mitra, FIT.

Nondeterministic finite automata

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

Formal Definition of Computation. August 28, 2013

Finite Automata and Regular languages

Theory of Languages and Automata

Automata and Languages

Extended transition function of a DFA

Let us first give some intuitive idea about a state of a system and state transitions before describing finite automata.

CPS 220 Theory of Computation REGULAR LANGUAGES

CS 455/555: Finite automata

Finite Automata Part Two

Non Determinism of Finite Automata

CISC 4090 Theory of Computation

Finite Automata. Finite Automata

Intro to Theory of Computation

2017/08/29 Chapter 1.2 in Sipser Ø Announcement:

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Fooling Sets and. Lecture 5

Lecture 1: Finite State Automaton

CS243, Logic and Computation Nondeterministic finite automata

Deterministic Finite Automata (DFAs)

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Deterministic Finite Automata (DFAs)

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

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

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

CSE 105 THEORY OF COMPUTATION

Non-deterministic Finite Automata (NFAs)

Nondeterministic Finite Automata

Automata Theory. Lecture on Discussion Course of CS120. Runzhe SJTU ACM CLASS

Takeaway Notes: Finite State Automata

Automata and Formal Languages - CM0081 Non-Deterministic Finite Automata

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,

CS21 Decidability and Tractability

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

UNIT-III REGULAR LANGUAGES

Great Theoretical Ideas in Computer Science. Lecture 4: Deterministic Finite Automaton (DFA), Part 2

Nondeterministic Finite Automata. Nondeterminism Subset Construction

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

Automata: a short introduction

CSE 105 THEORY OF COMPUTATION

Theory of Computation (II) Yijia Chen Fudan University

Turing Machines (TM) Deterministic Turing Machine (DTM) Nondeterministic Turing Machine (NDTM)

Chapter 6: NFA Applications

C2.1 Regular Grammars

Closure under the Regular Operations

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 )

C2.1 Regular Grammars

Theory of Computation p.1/?? Theory of Computation p.2/?? Unknown: Implicitly a Boolean variable: true if a word is

Lecture 4 Nondeterministic Finite Accepters

CS:4330 Theory of Computation Spring Regular Languages. Finite Automata and Regular Expressions. Haniel Barbosa

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

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

Finite Automata Part Two

CpSc 421 Homework 1 Solutions

CSE 105 Homework 3 Due: Monday October 23, Instructions. should be on each page of the submission.

Theory of Computation

Introduction to Languages and Computation

CSE 105 THEORY OF COMPUTATION

CSC236 Week 11. Larry Zhang

October 6, Equivalence of Pushdown Automata with Context-Free Gramm

CSE 105 THEORY OF COMPUTATION

Chapter 5. Finite Automata

Lecture Notes On THEORY OF COMPUTATION MODULE -1 UNIT - 2

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

FORMAL LANGUAGES, AUTOMATA AND COMPUTABILITY

CS 208: Automata Theory and Logic

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

Computational Models - Lecture 1 1

CISC4090: Theory of Computation

Decision, Computation and Language

Classes and conversions

Transcription:

CSE 135: Introduction to Theory of Computation Nondeterministic Finite Automata Sungjin Im University of California, Merced 1-27-215

Nondeterminism Michael Rabin and Dana Scott (1959) Michael Rabin Dana Scott Nondeterminism Given a current state of the machine and input symbol to be read, the next state is not uniquely determined.

Comparison to DFAs Nondeterministic Finite Automata (NFA) NFAs have 3 features when compared with DFAs.

Comparison to DFAs Nondeterministic Finite Automata (NFA) NFAs have 3 features when compared with DFAs. 1. Ability to take a step without reading any input symbol

Comparison to DFAs Nondeterministic Finite Automata (NFA) NFAs have 3 features when compared with DFAs. 1. Ability to take a step without reading any input symbol 2. A state may have no transition on a particular symbol

Comparison to DFAs Nondeterministic Finite Automata (NFA) NFAs have 3 features when compared with DFAs. 1. Ability to take a step without reading any input symbol 2. A state may have no transition on a particular symbol 3. Ability to transition to more than one state on a given symbol

ɛ-transitions Transitions without reading input symbols Example The British spelling of color is colour. In a web search application, you may want to recognize both variants. q c q 1 o q 2 l q 3 o q 4 u q 5 r q 6 ɛ NFA with ɛ-transitions

No transitions Example q 1 q 1... No -transition out of initial state In the above automaton, if the string starts with a then the string has no computation (i.e., rejected).

Multiple Transitions, 1, 1 q ɛ q q 1 q p q ɛ has two -transitions

Parallel Computation View At each step, the machine forks a thread corresponding to one of the possible next states. If a state has an ɛ-transition, then you fork a new process for each of the possible ɛ-transitions, without reading any input symbol If the state has multiple transitions on the current input symbol read, then fork a process for each possibility If from current state of a thread, there is no transition on the current input symbol then the thread dies

Parallel Computation View: An Example, 1, 1 q ɛ q q 1 q p ɛ Example NFA

Parallel Computation View: An Example, 1, 1 q ɛ q ɛ q 1 q p Example NFA q ɛ q q ɛ q ɛ 1 1 1 q p X q ɛ q p q ɛ q ɛ q q p q q ɛ q X ɛ q Computation on 1

Nondeterministic Acceptance Parallel Computation View Input is accepted if after reading all the symbols, one of the live threads of the automaton is in a final/accepting state.

Nondeterministic Acceptance Parallel Computation View Input is accepted if after reading all the symbols, one of the live threads of the automaton is in a final/accepting state. If none of the live threads are in a final/accepting state, the input is rejected.

Nondeterministic Acceptance Parallel Computation View Input is accepted if after reading all the symbols, one of the live threads of the automaton is in a final/accepting state. If none of the live threads are in a final/accepting state, the input is rejected., 1, 1 q ɛ q q 1 q p ɛ 1 is accepted because one thread of computation is ɛ 1 q ɛ q q qp qp qp

Computation: Guessing View The machine magically guesses the choices that lead to acceptance

Computation: Guessing View The machine magically guesses the choices that lead to acceptance Example q c q 1 o q 2 l q 3 o q 4 u ɛ q 5 r q 6 NFA M color After seeing colo the automaton guesses if it will see the british or the american spelling. If it guesses american then it moves without reading the next input symbol.

Observations: Guessing View If there is a sequence of choices that will lead to the automaton (not dying and) ending up in an accept state, then those choices will be magically guessed

Observations: Guessing View If there is a sequence of choices that will lead to the automaton (not dying and) ending up in an accept state, then those choices will be magically guessed On the other hand, if the input will not be accepted then no guess will lead the to automaton being in an accept state

Observations: Guessing View If there is a sequence of choices that will lead to the automaton (not dying and) ending up in an accept state, then those choices will be magically guessed On the other hand, if the input will not be accepted then no guess will lead the to automaton being in an accept state On the input colobr, whether automaton Mcolor guesses british or american, it will not proceed when it reads b.

Example I, 1 q 1, 1, 1 q 1 q 2 q 3

Example I, 1 q 1, 1, 1 q 1 q 2 q 3 Automaton accepts strings having a 1 two positions from end of input The automaton guesses at some point that the 1 it is seeing is 2 positions from end of input.

Example II A ɛ B ɛ D F C E

Example II A ɛ B ɛ D F C E NFA accepting strings of s where the length is either a multiple 2 or 3 The NFA guesses at the begining whether it will see a multiple of 2 or 3, and then confirms that the guess was correct.

Example III, 1, 1 q ɛ q q 1 q p

Example III, 1, 1 q ɛ q q 1 q p NFA accepting strings with 1 as substring At some point the NFA guesses that the pattern 1 is starting and then checks to confirm the guess.

Nondeterministic Finite Automata (NFA) Formal Definition Definition A nondeterministic finite automaton (NFA) is M = (Q, Σ, δ, q, F ), where Q is the finite set of states Σ is the finite alphabet δ : q Q initial state F Q final/accepting states

Nondeterministic Finite Automata (NFA) Formal Definition Definition A nondeterministic finite automaton (NFA) is M = (Q, Σ, δ, q, F ), where Q is the finite set of states Σ is the finite alphabet δ : Q (Σ {ɛ}) P(Q), where P(Q) is the powerset of Q q Q initial state F Q final/accepting states

Example of NFA, 1, 1 q ɛ q q 1 q p Transition Diagram of NFA

Example of NFA, 1, 1 q ɛ q q 1 q p Transition Diagram of NFA Formally, the NFA is M 1 = ({q ɛ, q, q, q p }, {, 1}, δ, q ɛ, {q p }) where δ is given by δ(q ɛ, ) = {q ɛ, q } δ(q ɛ, 1) = {q ɛ } δ(q, ) = {q } δ(q, 1) = {q p } δ(q p, ) = {q p } δ(q p, 1) = {q p } δ is in all other cases.

Computation Definition For an NFA M = (Q, Σ, δ, q, F ), string w, and states q 1, q 2 Q, w we say q 1 M q 2 if there is one thread of computation on input w from state q 1 that ends in q 2.

Computation Definition For an NFA M = (Q, Σ, δ, q, F ), string w, and states q 1, q 2 Q, w we say q 1 M q 2 if there is one thread of computation on input w w from state q 1 that ends in q 2. Formally, q 1 M q 2 if there is a sequence of states r, r 1,... r k and a sequence x 1, x 2,... x k, where for each i, x i Σ {ɛ}, such that

Computation Definition For an NFA M = (Q, Σ, δ, q, F ), string w, and states q 1, q 2 Q, w we say q 1 M q 2 if there is one thread of computation on input w w from state q 1 that ends in q 2. Formally, q 1 M q 2 if there is a sequence of states r, r 1,... r k and a sequence x 1, x 2,... x k, where for each i, x i Σ {ɛ}, such that r = q 1,

Computation Definition For an NFA M = (Q, Σ, δ, q, F ), string w, and states q 1, q 2 Q, w we say q 1 M q 2 if there is one thread of computation on input w w from state q 1 that ends in q 2. Formally, q 1 M q 2 if there is a sequence of states r, r 1,... r k and a sequence x 1, x 2,... x k, where for each i, x i Σ {ɛ}, such that r = q 1, for each i, r i+1 δ(r i, x i+1 ),

Computation Definition For an NFA M = (Q, Σ, δ, q, F ), string w, and states q 1, q 2 Q, w we say q 1 M q 2 if there is one thread of computation on input w w from state q 1 that ends in q 2. Formally, q 1 M q 2 if there is a sequence of states r, r 1,... r k and a sequence x 1, x 2,... x k, where for each i, x i Σ {ɛ}, such that r = q 1, for each i, r i+1 δ(r i, x i+1 ), r k = q 2, and

Computation Definition For an NFA M = (Q, Σ, δ, q, F ), string w, and states q 1, q 2 Q, w we say q 1 M q 2 if there is one thread of computation on input w w from state q 1 that ends in q 2. Formally, q 1 M q 2 if there is a sequence of states r, r 1,... r k and a sequence x 1, x 2,... x k, where for each i, x i Σ {ɛ}, such that r = q 1, for each i, r i+1 δ(r i, x i+1 ), r k = q 2, and w = x 1 x 2 x 3 x k

Example Computation, 1, 1 q ɛ q q 1 q p ɛ q ɛ 1 M q p because taking r = q ɛ, r 1 = q, r 2 = q, r 3 = q p, r 4 = q p, r 5 = q p, and x 1 =, x 2 = ɛ, x 3 = 1, x 4 =, x 5 =, we have x 1 x 2 x 5 = ɛ1 = 1 r i+1 δ(r i, x i+1 )

Defining ˆ Definition For an NFA M = (Q, Σ, δ, q, F ), string w, and state q 1 Q, we say ˆ (q 1, w) to denote states of all the active threads of computation on input w from q 1.

Defining ˆ Definition For an NFA M = (Q, Σ, δ, q, F ), string w, and state q 1 Q, we say ˆ (q 1, w) to denote states of all the active threads of computation on input w from q 1. Formally, ˆ (q 1, w) = {q Q q 1 w M q}

Example, 1, 1 q ɛ q q 1 q p ɛ Example NFA

Example, 1, 1 q ɛ q ɛ q 1 q p Example NFA q ɛ q q ɛ q ɛ 1 1 1 q p X q ɛ q p q ɛ q ɛ q q p q q ɛ q X ɛ q Computation on 1

Example, 1, 1 q ɛ q ɛ q 1 q p Example NFA ˆ (q ɛ, 1) = {q, q p, q, q ɛ } q ɛ q q ɛ q ɛ 1 1 1 q p X q ɛ q p q ɛ q ɛ q q p q q ɛ q X ɛ q Computation on 1

Acceptance/Recognition Definition For an NFA M = (Q, Σ, δ, q, F ) and string w Σ, we say M accepts w iff ˆ (q, w) F

Acceptance/Recognition Definition For an NFA M = (Q, Σ, δ, q, F ) and string w Σ, we say M accepts w iff ˆ (q, w) F Definition The language accepted or recognized by NFA M over alphabet Σ is L(M) = {w Σ M accepts w}.

Acceptance/Recognition Definition For an NFA M = (Q, Σ, δ, q, F ) and string w Σ, we say M accepts w iff ˆ (q, w) F Definition The language accepted or recognized by NFA M over alphabet Σ is L(M) = {w Σ M accepts w}. A language L is said to be accepted/recognized by M if L = L(M).

Observations about NFAs Observation 1 For NFA M, string w and state q 1 it could be that

Observations about NFAs Observation 1 For NFA M, string w and state q 1 it could be that ˆ (q 1, w) =

Observations about NFAs Observation 1 For NFA M, string w and state q 1 it could be that ˆ (q 1, w) = ˆ (q 1, w) has more than one element

Observations about NFAs Observation 1 For NFA M, string w and state q 1 it could be that ˆ (q 1, w) = ˆ (q 1, w) has more than one element Observation 2 However, the following proposition about DFAs continues to hold for NFAs

Observations about NFAs Observation 1 For NFA M, string w and state q 1 it could be that ˆ (q 1, w) = ˆ (q 1, w) has more than one element Observation 2 However, the following proposition about DFAs continues to hold for NFAs For NFA M, strings u and v, and state q, ˆ (q, uv) = ˆ (q, v) q ˆ (q,u)

Using Nondeterminism When designing an NFA for a language

Using Nondeterminism When designing an NFA for a language You follow the same methodology as for DFAs, like identifying what needs to be remembered

Using Nondeterminism When designing an NFA for a language You follow the same methodology as for DFAs, like identifying what needs to be remembered But now, the machine can guess at certain steps

Back to the Future Problem For Σ = {, 1, 2, #}, let L = {w#c w Σ, c Σ, and c occurs in w} So 111# L but 111#2 L. Design an NFA recognizing L.

Back to the Future Problem For Σ = {, 1, 2, #}, let L = {w#c w Σ, c Σ, and c occurs in w} So 111# L but 111#2 L. Design an NFA recognizing L. Solution Read symbols of w, i.e., portion of input before # is seen Guess at some point that current symbol in w is going to be the same as c ; store this symbol in the state Read the rest of w On reading #, check that the symbol immediately after is the one stored, and that the input ends immediately after that.

Back to the Future The Automaton, 1, 2, 1, 2 q s 2 1 q, 1, 2 q 1, 1, 2 # # q q 1 1 2 q f q 2 # q 2 L(M) = {w#c c occurs in w}

Halving a Language Definition For a language L, define 1 2L as follows. 1 L = {x y. x = y and xy L} 2 In other words, 1 2L consists of the first halves of strings in L

Halving a Language Definition For a language L, define 1 2L as follows. 1 L = {x y. x = y and xy L} 2 In other words, 1 2L consists of the first halves of strings in L Example If L = {1,, 1, 111} then 1 2 L =.

Halving a Language Definition For a language L, define 1 2L as follows. 1 L = {x y. x = y and xy L} 2 In other words, 1 2L consists of the first halves of strings in L Example If L = {1,, 1, 111} then 1 2L = {,, 11}.

Recognizing Halves of Regular Languages Proposition If L is recognized by a DFA M then there is a NFA N such that L(N) = 1 2 L.

Recognizing Halves of Regular Languages Proposition If L is recognized by a DFA M then there is a NFA N such that L(N) = 1 2 L. Proof Idea On input x, need to check if x is the first half of some string w = xy that is accepted by M. Run M on input x; let M be in state q i after reading all of x Guess a string y such that y = x Check if M reaches a final state on reading y from q i

Recognizing Halves of Regular Languages Proposition If L is recognized by a DFA M then there is a NFA N such that L(N) = 1 2 L. Proof Idea On input x, need to check if x is the first half of some string w = xy that is accepted by M. Run M on input x; let M be in state q i after reading all of x Guess a string y such that y = x Check if M reaches a final state on reading y from q i How do you guess a string y of equal length to x using finite memory? Seems to require remembering the length of x!

Fixing the Idea Problem How do you guess a string y of equal length to x using finite memory?

Fixing the Idea Problem and Fix(?) How do you guess a string y of equal length to x using finite memory? Guess one symbol of y as you read one symbol of x!

Fixing the Idea Problem and Fix(?) How do you guess a string y of equal length to x using finite memory? Guess one symbol of y as you read one symbol of x! How do you run M on y from q i, if you cannot store all the symbols of y?

Fixing the Idea Problem and Fix(?) How do you guess a string y of equal length to x using finite memory? Guess one symbol of y as you read one symbol of x! How do you run M on y from q i, if you cannot store all the symbols of y? Run M on y as you guess each symbol, without waiting to finish the execution on x!

Fixing the Idea Problem and Fix(?) How do you guess a string y of equal length to x using finite memory? Guess one symbol of y as you read one symbol of x! How do you run M on y from q i, if you cannot store all the symbols of y? Run M on y as you guess each symbol, without waiting to finish the execution on x! If we don t first execute M on x, how do we know the state q i from which we have to execute y from?

Fixing the Idea Problem and Fix(?) How do you guess a string y of equal length to x using finite memory? Guess one symbol of y as you read one symbol of x! How do you run M on y from q i, if you cannot store all the symbols of y? Run M on y as you guess each symbol, without waiting to finish the execution on x! If we don t first execute M on x, how do we know the state q i from which we have to execute y from? Guess it! And then check that running M on x does indeed end in q i, your guessed state.

New Algorithm On input x, NFA N 1. Guess state q i and place left finger on (initial state of M) q and right finger on q i 2. As characters of x are read, N moves the left finger along transitions dictated by x and simultaneously moves the right finger along nondeterministically chosen transitions labelled by some symbol

New Algorithm On input x, NFA N 1. Guess state q i and place left finger on (initial state of M) q and right finger on q i 2. As characters of x are read, N moves the left finger along transitions dictated by x and simultaneously moves the right finger along nondeterministically chosen transitions labelled by some symbol 3. Accept if after reading x, left finger is at q i (state initially guessed for right finger) and right finger is at an accepting state

New Algorithm On input x, NFA N 1. Guess state q i and place left finger on (initial state of M) q and right finger on q i 2. As characters of x are read, N moves the left finger along transitions dictated by x and simultaneously moves the right finger along nondeterministically chosen transitions labelled by some symbol 3. Accept if after reading x, left finger is at q i (state initially guessed for right finger) and right finger is at an accepting state Things to remember: initial guess for right finger, and positions of left and right finger.

Algorithm on Example 11 L and so x = 1 1 2 L q 1 q 1 1 q 2 1 1 q 3 DFA M

Algorithm on Example q 1 q 1 1 q 2 1 1 q 3 11 L and so x = 1 1 2 L NFA N execution on x = 1 is String Read Left Finger Right Finger ɛ q q 2 1 q 1 q 2 1 q 3 q 1 1 q 2 q 3 DFA M

Algorithm on Example q 1 q 1 1 q 2 1 1 q 3 DFA M 11 L and so x = 1 1 2 L NFA N execution on x = 1 is String Read Left Finger Right Finger ɛ q q 2 1 q 1 q 2 =? 1 q 3 1 q 2 q 1 q 3 accept?

Formal Construction of NFA N States and Initial State Given M = (Q, Σ, δ, q, F ) recognizing L define N = (Q, Σ, δ, q, F ) that recognizes 1 2 L Q = Q Q Q {s}, where s Q s is a new start state Other states are of the form left finger, initial guess, right finger ; initial guess records the initial guess for the right finger q = s

Formal Construction of NFA N Transitions and Final States Transitions δ (s, ɛ) = { q, q i, q i q i Q} Guess the state q i that the input will lead to δ ( q i, q j, q k, a) = { q l, q j, q m δ(q i, a) = q l, b Σ. δ(q k, b) = q m } b is the guess for the next symbol of y and initial guess does not change F = { q i, q i, q j q i Q, q j F }