Lecture 4: Nondeterministic Finite Automata

Similar documents
Lecture 3: Finite Automata. Finite Automata. Deterministic Finite Automata. Summary. Dr Kieran T. Herley

CSE 105 THEORY OF COMPUTATION

CSE 105 THEORY OF COMPUTATION

CMSC 330: Organization of Programming Languages

Lecture 3: Nondeterministic Finite Automata

Context-Free Languages

Finite-State Machines (Automata) lecture 12

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

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

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 35

COM364 Automata Theory Lecture Note 2 - Nondeterminism

Nondeterministic Finite Automata

Lecture 1: Finite State Automaton

Decision, Computation and Language

Deterministic Finite Automaton (DFA)

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Nondeterminism. September 7, Nondeterminism

Languages, regular languages, finite automata

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

Pushdown automata. Twan van Laarhoven. Institute for Computing and Information Sciences Intelligent Systems Radboud University Nijmegen

Harvard CS 121 and CSCI E-207 Lecture 10: CFLs: PDAs, Closure Properties, and Non-CFLs

Computational Models - Lecture 4

Closure under the Regular Operations

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

Automata and Languages

September 7, Formal Definition of a Nondeterministic Finite Automaton

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

Computational Models - Lecture 5 1

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

CPS 220 Theory of Computation Pushdown Automata (PDA)

Finite Automata Part Two

Inf2A: Converting from NFAs to DFAs and Closure Properties

PS2 - Comments. University of Virginia - cs3102: Theory of Computation Spring 2010

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

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

Java II Finite Automata I

Computability Theory

Please give details of your answer. A direct answer without explanation is not counted.

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

Introduction to Kleene Algebras

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

Nondeterministic Finite Automata

CS 4120 Lecture 3 Automating lexical analysis 29 August 2011 Lecturer: Andrew Myers. 1 DFAs

Lecture 17: Language Recognition

Finite Automata. Dr. Neil T. Dantam. Fall CSCI-561, Colorado School of Mines. Dantam (Mines CSCI-561) Finite Automata Fall / 43

Intro to Theory of Computation

CSE 211. Pushdown Automata. CSE 211 (Theory of Computation) Atif Hasan Rahman

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

Theory of Computation (I) Yijia Chen Fudan University

Compilers. Lexical analysis. Yannis Smaragdakis, U. Athens (original slides by Sam

Theory of Computation

Pushdown Automata: Introduction (2)

Finite Automata. Finite Automata

Finite Automata - Deterministic Finite Automata. Deterministic Finite Automaton (DFA) (or Finite State Machine)

Chapter Five: Nondeterministic Finite Automata

Rumination on the Formal Definition of DPDA

CS 154 Formal Languages and Computability Assignment #2 Solutions

Nondeterministic finite automata

CSE 105 THEORY OF COMPUTATION

Extended transition function of a DFA

Automata and Formal Languages - CM0081 Finite Automata and Regular Expressions

Theory of Computation

Pushdown Automata (Pre Lecture)

CS 154, Lecture 3: DFA NFA, Regular Expressions

Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. Pushdown Automata. The stack

Theory of Computation

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

Non-deterministic Finite Automata (NFAs)

Student#: CISC-462 Exam, December XY, 2017 Page 1 of 12

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

Nondeterministic Finite Automata. Nondeterminism Subset Construction

CSE 105 Theory of Computation Professor Jeanne Ferrante

Sri vidya college of engineering and technology

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Lecture 5: Minimizing DFAs

Automata: a short introduction

HKN CS/ECE 374 Midterm 1 Review. Nathan Bleier and Mahir Morshed

Nondeterministic Finite Automata

Lecture 4 Nondeterministic Finite Accepters

CS21 Decidability and Tractability

cse303 ELEMENTS OF THE THEORY OF COMPUTATION Professor Anita Wasilewska

Finite Automata and Regular Languages (part III)

CSC236 Week 11. Larry Zhang

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

Finite Automata and Regular languages

Pushdown Automata. We have seen examples of context-free languages that are not regular, and hence can not be recognized by finite automata.

FORMAL LANGUAGES, AUTOMATA AND COMPUTATION

Nondeterministic Finite Automata

CSE 105 THEORY OF COMPUTATION

Equivalence of DFAs and NFAs

CS21 Decidability and Tractability

Regular Expressions and Language Properties

More on Finite Automata and Regular Languages. (NTU EE) Regular Languages Fall / 41

THEORY OF COMPUTATION (AUBER) EXAM CRIB SHEET

CSE 135: Introduction to Theory of Computation Nondeterministic Finite Automata

Computer Sciences Department

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

Chapter 5. Finite Automata

Algorithms for NLP

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

Transcription:

Lecture 4: Nondeterministic Finite Automata Dr Kieran T. Herley Department of Computer Science University College Cork 2017-2018 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 1 / 1

Nondeterministic Finite Automata NFA Notes Allows edges labelled ɛ Possibly several ɛ-edges per state (DFA disallows both) Subsumes DFA concept Seemingly more expressive/powerful (But actually not) KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 2 / 1

NFA cont d A Nondeterministic Finite Automaton (NFA) consists of Σ, S, s 0, A As for DFA T = (S, E) a directed graph in which each edge in E is labelled with one or more elements of Σ {ɛ}. 1 1 Definition allows state to have multiple edges bearing same label, unlike DFA. We will not use this feature (except for ɛ-edges). KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 3 / 1

Acceptance Criterion Intuitively, the automaton accepts string x 1 x 2 x n if there is a path in T from the start state to one of the accept states whose edge labels concatenated together yield x 1 x 2 x n. Note: ɛ labels disappear in concatenation, e.g. accepts ɛ ɛ a ɛ ɛ = a KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 4 / 1

Acceptance Criterion Formal Version Definition More formally, a path e 1, e 2, e m yields y 1, y 2,, y m if for each i edge e i bears label y i. The automaton accepts string x 1 x 2 x n if there is a path e 1, e 2, e m in T from the start state to one of the accept states that yields some y 1, y 2,, y m such that All other strings are rejected. y 1 y 2 y m = x 1 x 2 x n KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 5 / 1

Example Recall A ɛ B = A B. Accepted Strings a b abba ɛ Rejected Strings? KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 6 / 1

NFA Example 1 Accepts a b aaa Rejects ab ba In other words... KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 7 / 1

NFA Example 1 Accepts a b aaa Rejects ab ba In other words...all monotonous strings i.e. all as or all bs. (Proof?) KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 7 / 1

NFA Example 2 Accepts abb ababababb Rejects abba abbaaaaa In other words... KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 8 / 1

NFA Example 2 Accepts abb ababababb Rejects abba abbaaaaa In other words...strings that end with abb. (Proof?) KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 8 / 1

Some Terminology ɛ-closure ɛ-closure(s) set of NFA states reachable from state s using ɛ edges only 0 {0, 1, 2, 4, 7} 2 {2} 6 {1, 2, 4, 6, 7} ɛ-closure(s) set of NFA states reachable from any state s S using ɛ edges only KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 9 / 1

Operation of NFA Initially one token on each state in ɛ-closure(s 0 ) i.e. potentially multiple token-bearing states One step per input symbol (input symbol x): advance tokens along x-edge(s) emanating from current states (remove token if no such edge(s), also duplicates) replicate each token in each state t to states in ɛ-closure(t) Illustration (one step) KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 10 / 1

Computing ɛ-closure Closure is essentially a reachability problem in a directed graph Stack-based depth-first search in transition graph beginning at states in X and using ɛ labelled edges only: Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) ɛadj(t) denotes the nodes to which there is an ɛ-edge leading from t. KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 11 / 1

Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1

Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1

Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 1 1 2, 4 0, 1, 7, 2, 4 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1

Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 1 1 2, 4 0, 1, 7, 2, 4 2, 4 4 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1

Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 1 1 2, 4 0, 1, 7, 2, 4 2, 4 4 2 2 KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1

Computing ɛ-closure cont d Algorithm Closure(X): Push states in X onto stack S C X while S not empty do Pop t, the top element, off S for each u ɛadj(t) do if u C then C C {u} Push u unto S return (C) S t Adj C 0 0 0 0 1, 7 0, 1, 7 1 7 7 1 1 2, 4 0, 1, 7, 2, 4 2, 4 4 2 2 Returns C = {0, 1, 2, 4, 7}. KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 12 / 1

NFA Simulation Algorithm Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure(M.moveTo(S, ch)) if S M.accept then return true else return false M.start M.accept start state accept states M.moveTo transition function for M, i.e. moveto(s, x) is set of states reachable from states in S using a single x-edge. Note: Use two stacks to represent S and C and alternate at each iteration efficient. KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 13 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Automaton Input String N((a b) ) babbb Accept! Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 14 / 1

Example of NFA Execution Trace S ch moveto Closure(Move(...)) 0, 1, 2, 4, 7 b 5 1, 2, 4, 5, 6, 7 1, 2, 4, 5, 6, 7 a 3 1, 2, 3, 4, 6, 7 1, 2, 3, 4, 6, 7 b 5 1, 2, 4, 5, 6, 7 1, 2, 4, 5, 6, 7 b 5 1, 2, 4, 5, 6, 7 1, 2, 4, 5, 6, 7 b 5 1, 2, 4, 5, 6, 7 Outcome Accept string since S contains accept state 7 at conclusion. KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 15 / 1

Correctness Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false Some Notation X S i S i = x 1 x 2 x 3 x n Set S at beginning of iteration i = 1, 2, Final state designated S n+1 ɛ-closure of S i KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 16 / 1

Correctness cont d Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure(M.MoveTo(S, ch)) if S M.accept then return true else return false Acceptance Criterion The automaton accepts string x 1 x 2 x n if there is a path in T from the start state to one of the accept states whose edge labels concatenated together yield x 1 x 2 x n. Path implies true returned Path has structure shown below Easy to see algorithm returns true KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 17 / 1

Correctness cont d Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure(M.MoveTo(S, ch)) if S M.accept then return true else return false Acceptance Criterion The automaton accepts string x 1 x 2 x n if there is a path in T from the start state to one of the accept states whose edge labels concatenated together yield x 1 x 2 x n. True implies path existance Consider relationships between sets S 1, S 2, S n+1 Implies existance of path KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 18 / 1

Transformation not straightforward, but intuitively each DFA state KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 19 / 1 DFA vs NFA Theorem For every NFA, there is an equivalent DFA, i.e. one that accepts precisely the same language.

DFA vs NFA cont d Algorithm DfaAccept(M, X): s M.start ch X.nextChar() while ch eof do s M.moveTo(s, ch) ch X.nextChar() if s in M.accept then return true else return false O( X ) running time Other Considerations NFA-to-DFA transformation takes time Algorithm NfaAccept(M, X): S Closure({M.start}) while ch eof do S Closure( M.MoveTo(S, ch)) if S M.accept then return true else return false O( X n) running time where n = # states/edges DFA can have upto 2 n states (n = # states of NFA equivalent); hence can be space-hungry (large tables!). KH (26/09/17) Lecture 4: Nondeterministic Finite Automata 2017-2018 20 / 1